Fix Chat Bugs

This commit is contained in:
TheBrokenRail 2021-02-16 17:08:43 -05:00
parent 6a6e973867
commit 4f4cdf915f
7 changed files with 58 additions and 7 deletions

View File

@ -92,6 +92,12 @@ static Minecraft_isLevelGenerated_t Minecraft_isLevelGenerated = (Minecraft_isLe
typedef int32_t (*Minecraft_isCreativeMode_t)(unsigned char *minecraft); typedef int32_t (*Minecraft_isCreativeMode_t)(unsigned char *minecraft);
static Minecraft_isCreativeMode_t Minecraft_isCreativeMode = (Minecraft_isCreativeMode_t) 0x17270; static Minecraft_isCreativeMode_t Minecraft_isCreativeMode = (Minecraft_isCreativeMode_t) 0x17270;
typedef void (*Minecraft_releaseMouse_t)(unsigned char *minecraft);
static Minecraft_releaseMouse_t Minecraft_releaseMouse = (Minecraft_releaseMouse_t) 0x15d30;
typedef void (*Minecraft_grabMouse_t)(unsigned char *minecraft);
static Minecraft_grabMouse_t Minecraft_grabMouse = (Minecraft_grabMouse_t) 0x15d10;
static uint32_t Minecraft_screen_width_property_offset = 0x20; // int32_t static uint32_t Minecraft_screen_width_property_offset = 0x20; // int32_t
static uint32_t Minecraft_server_side_network_handler_property_offset = 0x174; // ServerSideNetworkHandler * static uint32_t Minecraft_server_side_network_handler_property_offset = 0x174; // ServerSideNetworkHandler *
static uint32_t Minecraft_rak_net_instance_property_offset = 0x170; // RakNetInstance * static uint32_t Minecraft_rak_net_instance_property_offset = 0x170; // RakNetInstance *
@ -103,6 +109,7 @@ static uint32_t Minecraft_options_property_offset = 0x3c; // Options
static uint32_t Minecraft_hit_result_property_offset = 0xc38; // HitResult static uint32_t Minecraft_hit_result_property_offset = 0xc38; // HitResult
static uint32_t Minecraft_progress_property_offset = 0xc60; // int32_t static uint32_t Minecraft_progress_property_offset = 0xc60; // int32_t
static uint32_t Minecraft_command_server_property_offset = 0xcc0; // CommandServer * static uint32_t Minecraft_command_server_property_offset = 0xcc0; // CommandServer *
static uint32_t Minecraft_screen_property_offset = 0xc10; // Screen *
// CommandServer // CommandServer

View File

@ -84,9 +84,16 @@ void chat_queue_message(char *message) {
pthread_mutex_unlock(&queue_mutex); pthread_mutex_unlock(&queue_mutex);
} }
// Empty Queue // Empty Queue
unsigned int old_chat_counter = 0;
void chat_send_messages(unsigned char *minecraft) { void chat_send_messages(unsigned char *minecraft) {
// Lock // Lock
pthread_mutex_lock(&queue_mutex); pthread_mutex_lock(&queue_mutex);
// If Message Was Submitted, No Other Chat Windows Are Open, And The Game Is Not Paused, Then Re-Lock Cursor
unsigned int new_chat_counter = chat_get_counter();
if (old_chat_counter > new_chat_counter && new_chat_counter == 0 && (*(unsigned char **) (minecraft + Minecraft_screen_property_offset)) == NULL) {
(*Minecraft_grabMouse)(minecraft);
}
old_chat_counter = new_chat_counter;
// Loop // Loop
for (unsigned int i = 0; i < queue.size(); i++) { for (unsigned int i = 0; i < queue.size(); i++) {
send_api_chat_command(minecraft, (char *) queue[i].c_str()); send_api_chat_command(minecraft, (char *) queue[i].c_str());

View File

@ -5,6 +5,8 @@ extern "C" {
#endif #endif
void chat_open(); void chat_open();
unsigned int chat_get_counter();
void chat_queue_message(char *message); void chat_queue_message(char *message);
void chat_send_messages(unsigned char *minecraft); void chat_send_messages(unsigned char *minecraft);

View File

@ -62,6 +62,13 @@ static char *run_command(char *command, int *return_code) {
return output; return output;
} }
// Count Chat Windows
static pthread_mutex_t chat_counter_lock = PTHREAD_MUTEX_INITIALIZER;
static unsigned int chat_counter = 0;
unsigned int chat_get_counter() {
return chat_counter;
}
// Chat Thread // Chat Thread
static void *chat_thread(__attribute__((unused)) void *nop) { static void *chat_thread(__attribute__((unused)) void *nop) {
// Prepare // Prepare
@ -84,12 +91,21 @@ static void *chat_thread(__attribute__((unused)) void *nop) {
// Free // Free
free(output); free(output);
} }
// Update Counter
pthread_mutex_lock(&chat_counter_lock);
chat_counter--;
pthread_mutex_unlock(&chat_counter_lock);
// Return // Return
return NULL; return NULL;
} }
// Create Chat Thead // Create Chat Thead
void chat_open() { void chat_open() {
// Update Counter
pthread_mutex_lock(&chat_counter_lock);
chat_counter++;
pthread_mutex_unlock(&chat_counter_lock);
// Start Thread
pthread_t thread; pthread_t thread;
pthread_create(&thread, NULL, chat_thread, NULL); pthread_create(&thread, NULL, chat_thread, NULL);
} }

View File

@ -281,13 +281,14 @@ HOOK(SDL_PollEvent, int, (SDL_Event *event)) {
input_third_person(); input_third_person();
handled = 1; handled = 1;
} else if (event->key.keysym.sym == SDLK_t) { } else if (event->key.keysym.sym == SDLK_t) {
// Release Mouse Immediately // Only When In-Game With No Other Chat Windows Open
SDL_WM_GrabInput(SDL_GRAB_OFF); if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON && chat_get_counter() == 0) {
// Stop Tracking Mouse // Release Mouse
glfw_key(glfw_window, GLFW_KEY_TAB, -1, GLFW_PRESS, -1); input_set_mouse_grab_state(1);
glfw_key(glfw_window, GLFW_KEY_TAB, -1, GLFW_RELEASE, -1); // Open Chat
// Open Chat chat_open();
chat_open(); }
// Mark Handled
handled = 1; handled = 1;
} }
} else if (event->type == SDL_MOUSEBUTTONDOWN || event->type == SDL_MOUSEBUTTONUP) { } else if (event->type == SDL_MOUSEBUTTONDOWN || event->type == SDL_MOUSEBUTTONUP) {

View File

@ -26,6 +26,12 @@ void input_third_person() {
third_person_toggle++; third_person_toggle++;
} }
// Set mouse Grab State
static int mouse_grab_state = 0;
void input_set_mouse_grab_state(int state) {
mouse_grab_state = state;
}
// Handle Input Fixes // Handle Input Fixes
static void Minecraft_tickInput_injection(unsigned char *minecraft) { static void Minecraft_tickInput_injection(unsigned char *minecraft) {
// Call Original Method // Call Original Method
@ -61,6 +67,16 @@ static void Minecraft_tickInput_injection(unsigned char *minecraft) {
// Send Queued Chat Message // Send Queued Chat Message
chat_send_messages(minecraft); chat_send_messages(minecraft);
// Set Mouse Grab State
if (mouse_grab_state == -1) {
// Grab
(*Minecraft_grabMouse)(minecraft);
} else if (mouse_grab_state == 1) {
// Un-Grab
(*Minecraft_releaseMouse)(minecraft);
}
mouse_grab_state = 0;
} }
#include <SDL/SDL_events.h> #include <SDL/SDL_events.h>

View File

@ -13,6 +13,8 @@ void input_third_person();
void input_set_is_left_click(int val); void input_set_is_left_click(int val);
void input_set_mouse_grab_state(int state);
void init_input_cpp(); void init_input_cpp();
#ifdef __cplusplus #ifdef __cplusplus