minecraft-pi-reborn/mods/src/misc/misc.c

386 lines
16 KiB
C
Raw Normal View History

#include <stdlib.h>
#include <string.h>
2021-06-17 21:32:24 +00:00
#include <unistd.h>
2021-11-14 04:29:48 +00:00
#include <GLES/gl.h>
2021-01-27 21:26:19 +00:00
#include <libreborn/libreborn.h>
2021-09-12 03:18:12 +00:00
#include <symbols/minecraft.h>
2022-03-16 23:49:13 +00:00
2022-03-15 01:51:38 +00:00
#include <SDL/SDL.h>
2022-03-16 23:49:13 +00:00
#include <media-layer/core.h>
2020-12-02 23:18:49 +00:00
2022-06-25 21:30:08 +00:00
#include <mods/init/init.h>
#include <mods/feature/feature.h>
#include "misc-internal.h"
#include <mods/misc/misc.h>
2020-12-02 23:18:49 +00:00
2022-07-14 02:11:46 +00:00
// Classic HUD
#define DEFAULT_HUD_PADDING 2
2022-07-14 23:52:51 +00:00
#define NEW_HUD_PADDING 1
2022-07-14 02:11:46 +00:00
#define HUD_ELEMENT_WIDTH 82
#define HUD_ELEMENT_HEIGHT 9
2022-07-14 23:52:51 +00:00
#define TOOLBAR_HEIGHT 22
#define SLOT_WIDTH 20
2022-07-14 02:11:46 +00:00
#define DEFAULT_BUBBLES_PADDING 1
#define NUMBER_OF_SLOTS 9
static int use_classic_hud = 0;
static void Gui_renderHearts_GuiComponent_blit_hearts_injection(unsigned char *component, int32_t x_dest, int32_t y_dest, int32_t x_src, int32_t y_src, int32_t width_dest, int32_t height_dest, int32_t width_src, int32_t height_src) {
unsigned char *minecraft = *(unsigned char **) (component + Gui_minecraft_property_offset);
x_dest -= DEFAULT_HUD_PADDING;
2022-07-14 23:52:51 +00:00
float width = ((float) *(int32_t *) (minecraft + Minecraft_screen_width_property_offset)) * *InvGuiScale;
2022-07-15 05:37:12 +00:00
float height = ((float) *(int32_t *) (minecraft + Minecraft_screen_height_property_offset)) * *InvGuiScale;
x_dest += (width - (NUMBER_OF_SLOTS * SLOT_WIDTH)) / 2;
2022-07-14 02:11:46 +00:00
y_dest -= DEFAULT_HUD_PADDING;
2022-07-14 23:52:51 +00:00
y_dest += height - HUD_ELEMENT_HEIGHT - TOOLBAR_HEIGHT - NEW_HUD_PADDING;
2022-07-14 02:11:46 +00:00
// Call Original Method
(*GuiComponent_blit)(component, x_dest, y_dest, x_src, y_src, width_dest, height_dest, width_src, height_src);
}
static void Gui_renderHearts_GuiComponent_blit_armor_injection(unsigned char *component, int32_t x_dest, int32_t y_dest, int32_t x_src, int32_t y_src, int32_t width_dest, int32_t height_dest, int32_t width_src, int32_t height_src) {
unsigned char *minecraft = *(unsigned char **) (component + Gui_minecraft_property_offset);
x_dest -= DEFAULT_HUD_PADDING + HUD_ELEMENT_WIDTH;
float width = ((float) *(int32_t *) (minecraft + Minecraft_screen_width_property_offset)) * *InvGuiScale;
2022-07-15 05:37:12 +00:00
float height = ((float) *(int32_t *) (minecraft + Minecraft_screen_height_property_offset)) * *InvGuiScale;
x_dest += width - ((width - (NUMBER_OF_SLOTS * SLOT_WIDTH)) / 2) - HUD_ELEMENT_WIDTH;
2022-07-14 02:11:46 +00:00
y_dest -= DEFAULT_HUD_PADDING;
2022-07-14 23:52:51 +00:00
y_dest += height - HUD_ELEMENT_HEIGHT - TOOLBAR_HEIGHT - NEW_HUD_PADDING;
2022-07-14 02:11:46 +00:00
// Call Original Method
(*GuiComponent_blit)(component, x_dest, y_dest, x_src, y_src, width_dest, height_dest, width_src, height_src);
}
static void Gui_renderBubbles_GuiComponent_blit_injection(unsigned char *component, int32_t x_dest, int32_t y_dest, int32_t x_src, int32_t y_src, int32_t width_dest, int32_t height_dest, int32_t width_src, int32_t height_src) {
unsigned char *minecraft = *(unsigned char **) (component + Gui_minecraft_property_offset);
x_dest -= DEFAULT_HUD_PADDING;
2022-07-14 23:52:51 +00:00
float width = ((float) *(int32_t *) (minecraft + Minecraft_screen_width_property_offset)) * *InvGuiScale;
2022-07-15 05:37:12 +00:00
float height = ((float) *(int32_t *) (minecraft + Minecraft_screen_height_property_offset)) * *InvGuiScale;
x_dest += (width - (NUMBER_OF_SLOTS * SLOT_WIDTH)) / 2;
2022-07-14 02:11:46 +00:00
y_dest -= DEFAULT_HUD_PADDING + DEFAULT_BUBBLES_PADDING + HUD_ELEMENT_HEIGHT;
2022-07-14 23:52:51 +00:00
y_dest += height - HUD_ELEMENT_HEIGHT - TOOLBAR_HEIGHT - HUD_ELEMENT_HEIGHT - NEW_HUD_PADDING;
2022-07-14 02:11:46 +00:00
// Call Original Method
(*GuiComponent_blit)(component, x_dest, y_dest, x_src, y_src, width_dest, height_dest, width_src, height_src);
}
2021-11-14 04:29:48 +00:00
// Additional GUI Rendering
static int hide_chat_messages = 0;
static int render_selected_item_text = 0;
static void Gui_renderChatMessages_injection(unsigned char *gui, int32_t y_offset, uint32_t max_messages, bool disable_fading, unsigned char *font) {
2022-07-14 02:11:46 +00:00
// Handle Classic HUD
if (use_classic_hud) {
unsigned char *minecraft = *(unsigned char **) (gui + Gui_minecraft_property_offset);
if (!(*Minecraft_isCreativeMode)(minecraft)) {
2022-07-14 23:52:51 +00:00
y_offset -= (HUD_ELEMENT_HEIGHT * 2) + NEW_HUD_PADDING;
2022-07-14 02:11:46 +00:00
}
}
2021-02-17 16:31:01 +00:00
// Call Original Method
2021-11-14 04:29:48 +00:00
if (!hide_chat_messages) {
(*Gui_renderChatMessages)(gui, y_offset, max_messages, disable_fading, font);
}
2021-02-17 16:31:01 +00:00
// Render Selected Item Text
2021-11-14 04:29:48 +00:00
if (render_selected_item_text) {
// Fix GL Mode
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Calculate Selected Item Text Scale
unsigned char *minecraft = *(unsigned char **) (gui + Gui_minecraft_property_offset);
int32_t screen_width = *(int32_t *) (minecraft + Minecraft_screen_width_property_offset);
float scale = ((float) screen_width) * *InvGuiScale;
// Render Selected Item Text
(*Gui_renderOnSelectItemNameText)(gui, (int32_t) scale, font, y_offset - 0x13);
}
2021-02-17 16:31:01 +00:00
}
// Reset Selected Item Text Timer On Slot Select
static uint32_t reset_selected_item_text_timer = 0;
static void Gui_tick_injection(unsigned char *gui) {
// Call Original Method
(*Gui_tick)(gui);
2021-11-14 04:29:48 +00:00
2021-02-17 16:31:01 +00:00
// Handle Reset
2021-11-14 04:29:48 +00:00
if (render_selected_item_text) {
float *selected_item_text_timer = (float *) (gui + Gui_selected_item_text_timer_property_offset);
if (reset_selected_item_text_timer) {
// Reset
*selected_item_text_timer = 0;
reset_selected_item_text_timer = 0;
}
2021-02-17 16:31:01 +00:00
}
}
// Trigger Reset Selected Item Text Timer On Slot Select
static void Inventory_selectSlot_injection(unsigned char *inventory, int32_t slot) {
// Call Original Method
(*Inventory_selectSlot)(inventory, slot);
2021-11-14 04:29:48 +00:00
2021-02-17 16:31:01 +00:00
// Trigger Reset Selected Item Text Timer
2021-11-14 04:29:48 +00:00
if (render_selected_item_text) {
reset_selected_item_text_timer = 1;
}
2021-02-17 16:31:01 +00:00
}
2022-07-14 23:52:51 +00:00
// Translucent Toolbar
2022-07-16 02:08:12 +00:00
static GLfloat reset_red;
static GLfloat reset_green;
static GLfloat reset_blue;
static GLfloat reset_alpha;
2022-07-14 23:52:51 +00:00
static void Gui_renderToolBar_injection(unsigned char *gui, float param_1, int32_t param_2, int32_t param_3) {
// Call Original Method
2022-07-16 02:08:12 +00:00
int was_blend_enabled = glIsEnabled(GL_BLEND);
if (!was_blend_enabled) {
glEnable(GL_BLEND);
}
2022-07-14 23:52:51 +00:00
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
(*Gui_renderToolBar)(gui, param_1, param_2, param_3);
2022-07-16 02:08:12 +00:00
glColor4f(reset_red, reset_green, reset_blue, reset_alpha);
if (!was_blend_enabled) {
glDisable(GL_BLEND);
}
2022-07-14 23:52:51 +00:00
}
2022-07-16 02:08:12 +00:00
static void Gui_renderToolBar_glColor4f_injection(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) {
2022-07-14 23:52:51 +00:00
// Fix Alpha
glColor4f(red, green, blue, 1.0f);
2022-07-16 02:08:12 +00:00
// Stroe For Reset
reset_red = red;
reset_green = green;
reset_blue = blue;
reset_alpha = alpha;
2022-07-14 23:52:51 +00:00
}
// Sanitize Username
2022-07-14 02:11:46 +00:00
#define MAX_USERNAME_LENGTH 16
static void LoginPacket_read_injection(unsigned char *packet, unsigned char *bit_stream) {
// Call Original Method
(*LoginPacket_read)(packet, bit_stream);
// Prepare
unsigned char *rak_string = packet + LoginPacket_username_property_offset;
// Get Original Username
unsigned char *shared_string = *(unsigned char **) (rak_string + RakNet_RakString_sharedString_property_offset);
char *c_str = *(char **) (shared_string + RakNet_RakString_SharedString_c_str_property_offset);
// Sanitize
char *new_username = strdup(c_str);
ALLOC_CHECK(new_username);
sanitize_string(&new_username, MAX_USERNAME_LENGTH, 0);
// Set New Username
(*RakNet_RakString_Assign)(rak_string, new_username);
// Free
free(new_username);
}
2021-07-16 22:17:49 +00:00
// Fix RakNet::RakString Security Bug
//
// RakNet::RakString's format constructor is often given unsanitized user input and is never used for formatting,
// this is a massive security risk, allowing clients to run arbitrary format specifiers, this disables the
// formatting functionality.
static unsigned char *RakNet_RakString_injection(unsigned char *rak_string, const char *format, ...) {
// Call Original Method
2022-04-16 20:38:09 +00:00
return (*RakNet_RakString)(rak_string, "%s", format);
2021-07-16 22:17:49 +00:00
}
// Print Error Message If RakNet Startup Fails
static char *RAKNET_ERROR_NAMES[] = {
"Success",
"Already Started",
"Invalid Socket Descriptors",
"Invalid Max Connections",
"Socket Family Not Supported",
"Part Already In Use",
"Failed To Bind Port",
"Failed Test Send",
"Port Cannot Be 0",
"Failed To Create Network Thread",
"Couldn't Generate GUID",
"Unknown"
};
#ifdef MCPI_SERVER_MODE
#define PRINT_RAKNET_STARTUP_FAILURE ERR
#else
#define PRINT_RAKNET_STARTUP_FAILURE WARN
#endif
static RakNet_StartupResult RakNetInstance_host_RakNet_RakPeer_Startup_injection(unsigned char *rak_peer, unsigned short maxConnections, unsigned char *socketDescriptors, uint32_t socketDescriptorCount, int32_t threadPriority) {
// Call Original Method
RakNet_StartupResult result = (*RakNet_RakPeer_Startup)(rak_peer, maxConnections, socketDescriptors, socketDescriptorCount, threadPriority);
// Print Error
if (result != RAKNET_STARTED) {
PRINT_RAKNET_STARTUP_FAILURE("Failed To Start RakNet: %s", RAKNET_ERROR_NAMES[result]);
}
// Return
return result;
}
2021-09-12 03:18:12 +00:00
// Fix Bug Where RakNetInstance Starts Pinging Potential Servers Before The "Join Game" Screen Is Opened
static unsigned char *RakNetInstance_injection(unsigned char *rak_net_instance) {
// Call Original Method
unsigned char *result = (*RakNetInstance)(rak_net_instance);
// Fix
*(unsigned char *) (rak_net_instance + RakNetInstance_pinging_for_hosts_property_offset) = 0;
// Return
return result;
}
2022-01-02 20:26:56 +00:00
// Close Current Screen On Death To Prevent Bugs
static void LocalPlayer_die_injection(unsigned char *entity, unsigned char *cause) {
// Close Screen
unsigned char *minecraft = *(unsigned char **) (entity + LocalPlayer_minecraft_property_offset);
(*Minecraft_setScreen)(minecraft, NULL);
// Call Original Method
(*LocalPlayer_die)(entity, cause);
}
// Fix Furnace Not Checking Item Auxiliary When Inserting New Item
static int32_t FurnaceScreen_handleAddItem_injection(unsigned char *furnace_screen, int32_t slot, ItemInstance const *item) {
// Get Existing Item
unsigned char *tile_entity = *(unsigned char **) (furnace_screen + FurnaceScreen_tile_entity_property_offset);
unsigned char *tile_entity_vtable = *(unsigned char **) tile_entity;
FurnaceTileEntity_getItem_t FurnaceTileEntity_getItem = *(FurnaceTileEntity_getItem_t *) (tile_entity_vtable + FurnaceTileEntity_getItem_vtable_offset);
ItemInstance *existing_item = (*FurnaceTileEntity_getItem)(tile_entity, slot);
// Check Item
int valid;
if (item->id == existing_item->id && item->auxiliary == existing_item->auxiliary) {
// Item Matches, Is Valid
valid = 1;
} else {
// Item Doesn't Match, Check If Existing Item Is Empty
if ((existing_item->id | existing_item->count | existing_item->auxiliary) == 0) {
// Existing Item Is Empty, Is Valid
valid = 1;
} else {
// Existing Item Isn't Empty, Isn't Valid
valid = 0;
}
}
// Call Original Method
if (valid) {
// Valid
return (*FurnaceScreen_handleAddItem)(furnace_screen, slot, item);
} else {
// Invalid
return 0;
}
}
2022-03-15 01:51:38 +00:00
// Custom Cursor Rendering
//
// The default behavior for Touch GUI is to only render the cursor when the mouse is clicking, this fixes that.
// This also makes the cursor always render if the mouse is unlocked, instead of just when there is a Screen showing.
static void GameRenderer_render_injection(unsigned char *game_renderer, float param_1) {
// Call Original Method
(*GameRenderer_render)(game_renderer, param_1);
// Check If Cursor Should Render
if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_OFF) {
2022-05-11 22:24:03 +00:00
// Fix GL Mode
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2022-03-15 01:51:38 +00:00
// Get X And Y
float x = (*Mouse_getX)() * (*InvGuiScale);
float y = (*Mouse_getY)() * (*InvGuiScale);
// Render Cursor
unsigned char *minecraft = *(unsigned char **) (game_renderer + GameRenderer_minecraft_property_offset);
(*renderCursor)(x, y, minecraft);
}
}
2022-07-10 14:37:19 +00:00
// Get Real Selected Slot
int32_t misc_get_real_selected_slot(unsigned char *player) {
// Get Selected Slot
unsigned char *inventory = *(unsigned char **) (player + Player_inventory_property_offset);
int32_t selected_slot = *(int32_t *) (inventory + Inventory_selectedSlot_property_offset);
// Linked Slots
int32_t linked_slots_length = *(int32_t *) (inventory + FillingContainer_linked_slots_length_property_offset);
if (selected_slot < linked_slots_length) {
int32_t *linked_slots = *(int32_t **) (inventory + FillingContainer_linked_slots_property_offset);
selected_slot = linked_slots[selected_slot];
}
// Return
return selected_slot;
}
// Init
static void nop() {
}
2020-12-02 23:18:49 +00:00
void init_misc() {
2022-01-02 20:26:56 +00:00
// Remove Invalid Item Background (A Red Background That Appears For Items That Are Not Included In The gui_blocks Atlas)
2022-04-10 00:01:16 +00:00
if (feature_has("Remove Invalid Item Background", server_disabled)) {
2021-03-05 00:27:24 +00:00
unsigned char invalid_item_background_patch[4] = {0x00, 0xf0, 0x20, 0xe3}; // "nop"
2020-12-02 23:18:49 +00:00
patch((void *) 0x63c98, invalid_item_background_patch);
}
2022-07-14 02:11:46 +00:00
// Classic HUD
if (feature_has("Classic HUD", server_disabled)) {
use_classic_hud = 1;
overwrite_call((void *) 0x266f8, (void *) Gui_renderHearts_GuiComponent_blit_hearts_injection);
overwrite_call((void *) 0x26758, (void *) Gui_renderHearts_GuiComponent_blit_hearts_injection);
overwrite_call((void *) 0x267c8, (void *) Gui_renderHearts_GuiComponent_blit_hearts_injection);
overwrite_call((void *) 0x2656c, (void *) Gui_renderHearts_GuiComponent_blit_armor_injection);
overwrite_call((void *) 0x268c4, (void *) Gui_renderBubbles_GuiComponent_blit_injection);
}
2021-11-14 04:29:48 +00:00
// Render Selected Item Text + Hide Chat Messages
2022-04-10 00:01:16 +00:00
hide_chat_messages = feature_has("Hide Chat Messages", server_disabled);
render_selected_item_text = feature_has("Render Selected Item Text", server_disabled);
2021-11-14 04:29:48 +00:00
overwrite_calls((void *) Gui_renderChatMessages, (void *) Gui_renderChatMessages_injection);
overwrite_calls((void *) Gui_tick, (void *) Gui_tick_injection);
overwrite_calls((void *) Inventory_selectSlot, (void *) Inventory_selectSlot_injection);
2021-02-17 16:31:01 +00:00
2022-07-14 23:52:51 +00:00
// Translucent Toolbar
if (feature_has("Translucent Toolbar", server_disabled)) {
overwrite_calls((void *) Gui_renderToolBar, (void *) Gui_renderToolBar_injection);
overwrite_call((void *) 0x26c5c, (void *) Gui_renderToolBar_glColor4f_injection);
}
// Sanitize Username
patch_address(LoginPacket_read_vtable_addr, (void *) LoginPacket_read_injection);
2021-07-16 22:17:49 +00:00
// Fix RakNet::RakString Security Bug
overwrite_calls((void *) RakNet_RakString, (void *) RakNet_RakString_injection);
// Print Error Message If RakNet Startup Fails
overwrite_call((void *) 0x73778, (void *) RakNetInstance_host_RakNet_RakPeer_Startup_injection);
2021-07-16 22:17:49 +00:00
2021-09-12 03:18:12 +00:00
// Fix Bug Where RakNetInstance Starts Pinging Potential Servers Before The "Join Game" Screen Is Opened
overwrite_calls((void *) RakNetInstance, (void *) RakNetInstance_injection);
2022-01-02 20:26:56 +00:00
// Close Current Screen On Death To Prevent Bugs
2022-04-10 00:01:16 +00:00
if (feature_has("Close Current Screen On Death", server_disabled)) {
2022-01-02 20:26:56 +00:00
patch_address(LocalPlayer_die_vtable_addr, (void *) LocalPlayer_die_injection);
}
// Fix Furnace Not Checking Item Auxiliary When Inserting New Item
2022-04-10 00:01:16 +00:00
if (feature_has("Fix Furnace Not Checking Item Auxiliary", server_disabled)) {
overwrite_calls((void *) FurnaceScreen_handleAddItem, (void *) FurnaceScreen_handleAddItem_injection);
}
2022-03-15 01:51:38 +00:00
// Improved Cursor Rendering
2022-04-10 00:01:16 +00:00
if (feature_has("Improved Cursor Rendering", server_disabled)) {
2022-03-15 01:51:38 +00:00
// Disable Normal Cursor Rendering
unsigned char disable_cursor_patch[4] = {0x00, 0xf0, 0x20, 0xe3}; // "nop"
patch((void *) 0x4a6c0, disable_cursor_patch);
// Add Custom Cursor Rendering
overwrite_calls((void *) GameRenderer_render, (void *) GameRenderer_render_injection);
}
2022-03-16 23:49:13 +00:00
// Disable V-Sync
if (feature_has("Disable V-Sync", server_enabled)) {
2022-03-16 23:49:13 +00:00
media_disable_vsync();
}
2022-07-16 02:08:12 +00:00
// Force EGL
if (feature_has("Force EGL", server_disabled)) {
media_force_egl();
}
// Remove Forced GUI Lag
2022-07-13 23:32:08 +00:00
if (feature_has("Remove Forced GUI Lag (Can Break Joining Servers)", server_enabled)) {
overwrite_calls((void *) sleepMs, (void *) nop);
}
2021-11-14 04:29:48 +00:00
// Init C++ And Logging
2021-06-17 21:32:24 +00:00
_init_misc_cpp();
2021-11-14 04:29:48 +00:00
_init_misc_logging();
2022-06-27 18:47:55 +00:00
_init_misc_api();
2021-06-17 21:32:24 +00:00
}