2021-02-16 17:26:40 +00:00
|
|
|
#include <string>
|
2021-02-22 03:16:05 +00:00
|
|
|
#include <cstring>
|
2021-02-16 17:26:40 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <vector>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
#include <libreborn/libreborn.h>
|
|
|
|
#include <libreborn/minecraft.h>
|
|
|
|
|
|
|
|
#include "../init/init.h"
|
|
|
|
|
|
|
|
#include "chat.h"
|
|
|
|
|
2021-02-22 03:16:05 +00:00
|
|
|
// Message Limitations
|
2021-02-22 03:43:57 +00:00
|
|
|
#define MAX_CHAT_MESSAGE_LENGTH 512
|
2021-02-22 03:16:05 +00:00
|
|
|
|
2021-02-16 17:26:40 +00:00
|
|
|
// Send API Command
|
|
|
|
static void send_api_command(unsigned char *minecraft, char *str) {
|
|
|
|
struct ConnectedClient client;
|
|
|
|
client.sock = -1;
|
|
|
|
client.str = "";
|
|
|
|
client.time = 0;
|
|
|
|
unsigned char *command_server = *(unsigned char **) (minecraft + Minecraft_command_server_property_offset);
|
|
|
|
if (command_server != NULL) {
|
|
|
|
(*CommandServer_parse)(command_server, client, str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Send API Chat Command
|
|
|
|
static void send_api_chat_command(unsigned char *minecraft, char *str) {
|
|
|
|
char *command = NULL;
|
2021-06-17 21:32:24 +00:00
|
|
|
safe_asprintf(&command, "chat.post(%s)\n", str);
|
2021-02-16 17:26:40 +00:00
|
|
|
send_api_command(minecraft, command);
|
|
|
|
free(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send Message To Players
|
|
|
|
static void send_message(unsigned char *server_side_network_handler, char *username, char *message) {
|
|
|
|
char *full_message = NULL;
|
2021-06-17 21:32:24 +00:00
|
|
|
safe_asprintf(&full_message, "<%s> %s", username, message);
|
2021-02-22 03:51:01 +00:00
|
|
|
sanitize_string(&full_message, MAX_CHAT_MESSAGE_LENGTH, 0);
|
2021-02-16 17:26:40 +00:00
|
|
|
(*ServerSideNetworkHandler_displayGameMessage)(server_side_network_handler, std::string(full_message));
|
|
|
|
free(full_message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manually Send (And Loopback) ChatPacket
|
|
|
|
static void CommandServer_parse_CommandServer_dispatchPacket_injection(unsigned char *command_server, unsigned char *packet) {
|
|
|
|
unsigned char *minecraft = *(unsigned char **) (command_server + CommandServer_minecraft_property_offset);
|
|
|
|
if (minecraft != NULL) {
|
|
|
|
unsigned char *rak_net_instance = *(unsigned char **) (minecraft + Minecraft_rak_net_instance_property_offset);
|
|
|
|
unsigned char *rak_net_instance_vtable = *(unsigned char **) rak_net_instance;
|
|
|
|
RakNetInstance_isServer_t RakNetInstance_isServer = *(RakNetInstance_isServer_t *) (rak_net_instance_vtable + RakNetInstance_isServer_vtable_offset);
|
|
|
|
if ((*RakNetInstance_isServer)(rak_net_instance)) {
|
|
|
|
// Hosting Multiplayer
|
|
|
|
char *message = *(char **) (packet + ChatPacket_message_property_offset);
|
2021-02-17 16:31:01 +00:00
|
|
|
unsigned char *server_side_network_handler = *(unsigned char **) (minecraft + Minecraft_network_handler_property_offset);
|
2021-02-16 17:26:40 +00:00
|
|
|
send_message(server_side_network_handler, *default_username, message);
|
|
|
|
} else {
|
|
|
|
// Client
|
|
|
|
RakNetInstance_send_t RakNetInstance_send = *(RakNetInstance_send_t *) (rak_net_instance_vtable + RakNetInstance_send_vtable_offset);
|
|
|
|
(*RakNetInstance_send)(rak_net_instance, packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle ChatPacket Server-Side
|
|
|
|
static void ServerSideNetworkHandler_handle_ChatPacket_injection(unsigned char *server_side_network_handler, unsigned char *rak_net_guid, unsigned char *chat_packet) {
|
|
|
|
unsigned char *player = (*ServerSideNetworkHandler_getPlayer)(server_side_network_handler, rak_net_guid);
|
|
|
|
if (player != NULL) {
|
|
|
|
char *username = *(char **) (player + Player_username_property_offset);
|
|
|
|
char *message = *(char **) (chat_packet + ChatPacket_message_property_offset);
|
|
|
|
send_message(server_side_network_handler, username, message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message Queue
|
|
|
|
static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
static std::vector<std::string> queue;
|
|
|
|
// Add To Queue
|
2021-06-17 21:32:24 +00:00
|
|
|
void _chat_queue_message(char *message) {
|
2021-02-16 17:26:40 +00:00
|
|
|
// Lock
|
|
|
|
pthread_mutex_lock(&queue_mutex);
|
|
|
|
// Add
|
|
|
|
std::string str;
|
|
|
|
str.append(message);
|
|
|
|
queue.push_back(str);
|
|
|
|
// Unlock
|
|
|
|
pthread_mutex_unlock(&queue_mutex);
|
|
|
|
}
|
|
|
|
// Empty Queue
|
2021-02-16 22:08:43 +00:00
|
|
|
unsigned int old_chat_counter = 0;
|
2021-02-16 17:26:40 +00:00
|
|
|
void chat_send_messages(unsigned char *minecraft) {
|
|
|
|
// Lock
|
|
|
|
pthread_mutex_lock(&queue_mutex);
|
2021-02-16 22:08:43 +00:00
|
|
|
// 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;
|
2021-02-16 17:26:40 +00:00
|
|
|
// Loop
|
|
|
|
for (unsigned int i = 0; i < queue.size(); i++) {
|
|
|
|
send_api_chat_command(minecraft, (char *) queue[i].c_str());
|
|
|
|
}
|
|
|
|
queue.clear();
|
|
|
|
// Unlock
|
|
|
|
pthread_mutex_unlock(&queue_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init
|
|
|
|
void init_chat() {
|
|
|
|
// Disable Original ChatPacket Loopback
|
2021-03-05 00:27:24 +00:00
|
|
|
unsigned char disable_chat_packet_loopback_patch[4] = {0x00, 0xf0, 0x20, 0xe3}; // "nop"
|
2021-02-16 17:26:40 +00:00
|
|
|
patch((void *) 0x6b490, disable_chat_packet_loopback_patch);
|
|
|
|
// Manually Send (And Loopback) ChatPacket
|
|
|
|
overwrite_call((void *) 0x6b518, (void *) CommandServer_parse_CommandServer_dispatchPacket_injection);
|
|
|
|
// Re-Broadcast ChatPacket
|
|
|
|
patch_address(ServerSideNetworkHandler_handle_ChatPacket_vtable_addr, (void *) ServerSideNetworkHandler_handle_ChatPacket_injection);
|
|
|
|
}
|