minecraft-pi-reborn/mods/src/extra.cpp

191 lines
7.7 KiB
C++
Raw Normal View History

#include <string>
#include <fstream>
#include <streambuf>
2020-10-04 00:30:15 +00:00
#include <vector>
#include <unistd.h>
2020-10-04 00:30:15 +00:00
#include <libcore/libcore.h>
#include "extra.h"
2020-10-10 23:02:13 +00:00
#include "cxx11_util.h"
#include "minecraft.h"
2020-10-10 23:02:13 +00:00
#include <cstdio>
2020-10-04 00:30:15 +00:00
extern "C" {
2020-11-20 21:38:23 +00:00
// Read Asset File
static cxx11_string AppPlatform_readAssetFile_injection(__attribute__((unused)) unsigned char *app_platform, std::string const& path) {
std::string full_path("./data/");
2020-10-10 23:02:13 +00:00
full_path.append(path);
std::ifstream stream(full_path);
std::string str((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
2020-10-10 23:02:13 +00:00
return create_cxx11_string(str.c_str());
}
2020-10-04 00:30:15 +00:00
2020-11-20 21:38:23 +00:00
// Open Sign Screen
static void LocalPlayer_openTextEdit_injection(unsigned char *local_player, unsigned char *sign) {
2020-10-04 00:30:15 +00:00
if (*(int *)(sign + 0x18) == 4) {
unsigned char *minecraft = *(unsigned char **) (local_player + 0xc90);
unsigned char *screen = (unsigned char *) ::operator new(0xd0);
screen = (*TextEditScreen)(screen, sign);
2020-11-02 14:58:43 +00:00
(*Minecraft_setScreen)(minecraft, screen);
2020-10-04 00:30:15 +00:00
}
}
#define BACKSPACE_KEY 8
static int is_valid_key(char key) {
return (key >= 32 && key <= 126) || key == BACKSPACE_KEY;
}
2020-11-20 21:38:23 +00:00
// Store Text Input
2020-10-04 00:30:15 +00:00
std::vector<char> input;
2020-10-14 17:40:32 +00:00
void extra_key_press(char key) {
2020-10-04 00:30:15 +00:00
if (is_valid_key(key)) {
2020-10-04 20:45:00 +00:00
input.push_back(key);
2020-10-04 00:30:15 +00:00
}
}
2020-10-14 17:40:32 +00:00
void extra_clear_input() {
2020-10-04 00:30:15 +00:00
input.clear();
}
2020-11-20 21:38:23 +00:00
// Handle Text Input
static void TextEditScreen_updateEvents_injection(unsigned char *screen) {
2020-11-20 21:38:23 +00:00
// Call Original Method
2020-11-02 14:58:43 +00:00
(*Screen_updateEvents)(screen);
2020-10-04 00:30:15 +00:00
if (*(char *)(screen + 4) == '\0') {
uint32_t vtable = *((uint32_t *) screen);
for (char key : input) {
if (key == BACKSPACE_KEY) {
// Handle Backspace
2020-11-02 14:58:43 +00:00
(*(Screen_keyPressed_t *) (vtable + 0x6c))(screen, BACKSPACE_KEY);
2020-10-04 00:30:15 +00:00
} else {
// Handle Nrmal Key
2020-11-02 14:58:43 +00:00
(*(Screen_keyboardNewChar_t *) (vtable + 0x70))(screen, key);
2020-10-04 00:30:15 +00:00
}
}
}
2020-10-14 17:40:32 +00:00
extra_clear_input();
2020-10-04 00:30:15 +00:00
}
2020-11-02 17:34:11 +00:00
static void inventory_add_item(unsigned char *inventory, unsigned char *item, bool is_tile) {
unsigned char *item_instance = (unsigned char *) ::operator new(0xc);
item_instance = (*(is_tile ? ItemInstance_tile : ItemInstance_item))(item_instance, item);
(*FillingContainer_addItem)(inventory, item_instance);
}
2020-11-02 17:42:06 +00:00
static int32_t FillingContainer_addItem_injection(unsigned char *filling_container, unsigned char *item_instance) {
2020-11-02 17:34:11 +00:00
// Call Original
2020-11-02 17:42:06 +00:00
int32_t ret = (*FillingContainer_addItem)(filling_container, item_instance);
2020-11-02 17:34:11 +00:00
2020-11-20 18:36:03 +00:00
// Add Items
inventory_add_item(filling_container, *item_flintAndSteel, false);
inventory_add_item(filling_container, *item_snowball, false);
inventory_add_item(filling_container, *item_egg, false);
inventory_add_item(filling_container, *item_shears, false);
for (int i = 0; i < 15; i++) {
unsigned char *item_instance = (unsigned char *) ::operator new(0xc);
item_instance = (*ItemInstance_damage)(item_instance, *item_dye_powder, 1, i);
(*FillingContainer_addItem)(filling_container, item_instance);
2020-11-02 17:34:11 +00:00
}
2020-11-20 18:36:03 +00:00
// Add Tiles
inventory_add_item(filling_container, *tile_water, true);
inventory_add_item(filling_container, *tile_lava, true);
inventory_add_item(filling_container, *tile_calmWater, true);
inventory_add_item(filling_container, *tile_calmLava, true);
inventory_add_item(filling_container, *tile_glowingObsidian, true);
inventory_add_item(filling_container, *tile_topSnow, true);
inventory_add_item(filling_container, *tile_ice, true);
inventory_add_item(filling_container, *tile_invisible_bedrock, true);
2020-11-02 17:42:06 +00:00
return ret;
2020-11-02 17:34:11 +00:00
}
2020-11-20 18:36:03 +00:00
static void Minecraft_tick_injection(unsigned char *minecraft, int32_t param_1, int32_t param_2) {
// Call Original Method
(*Minecraft_tick)(minecraft, param_1, param_2);
// Tick Dynamic Textures
unsigned char *textures = *(unsigned char **) (minecraft + 0x164);
if (textures != NULL) {
(*Textures_tick)(textures, true);
}
}
2020-11-23 23:03:28 +00:00
// Get Minecraft From Screen
static unsigned char *get_minecraft_from_screen(unsigned char *screen) {
return *(unsigned char **) (screen + 0x14);
}
// Redirect Create World Button To SimpleLevelChooseScreen
#define WORLD_NAME "world"
#define SIMPLE_LEVEL_CHOOSE_SCREEN_SIZE 0x68
static void SelectWorldScreen_tick_injection(unsigned char *screen) {
bool create_world = *(bool *) (screen + 0xfc);
if (create_world) {
// Get New World Name
std::string new_name;
(*SelectWorldScreen_getUniqueLevelName)(new_name, screen, WORLD_NAME);
// Create SimpleLevelChooseScreen
unsigned char *new_screen = (unsigned char *) ::operator new(SIMPLE_LEVEL_CHOOSE_SCREEN_SIZE);
(*SimpleChooseLevelScreen)(new_screen, new_name);
// Set Screen
unsigned char *minecraft = get_minecraft_from_screen(screen);
(*Minecraft_setScreen)(minecraft, new_screen);
// Finish
*(bool *) (screen + 0xf9) = true;
} else {
(*SelectWorldScreen_tick)(screen);
}
}
static void Touch_SelectWorldScreen_tick_injection(unsigned char *screen) {
bool create_world = *(bool *) (screen + 0x154);
if (create_world) {
// Get New World Name
std::string new_name;
(*Touch_SelectWorldScreen_getUniqueLevelName)(new_name, screen, WORLD_NAME);
// Create SimpleLevelChooseScreen
unsigned char *new_screen = (unsigned char *) ::operator new(SIMPLE_LEVEL_CHOOSE_SCREEN_SIZE);
(*SimpleChooseLevelScreen)(new_screen, new_name);
// Set Screen
unsigned char *minecraft = get_minecraft_from_screen(screen);
(*Minecraft_setScreen)(minecraft, new_screen);
// Finish
*(bool *) (screen + 0x151) = true;
} else {
(*Touch_SelectWorldScreen_tick)(screen);
}
}
2020-10-04 00:30:15 +00:00
__attribute((constructor)) static void init() {
// Implement AppPlatform::readAssetFile So Translations Work
overwrite((void *) AppPlatform_readAssetFile, (void *) AppPlatform_readAssetFile_injection);
2020-10-04 00:30:15 +00:00
2020-10-14 17:40:32 +00:00
if (extra_has_feature("Fix Sign Placement")) {
2020-10-04 00:30:15 +00:00
// Fix Signs
patch_address(LocalPlayer_openTextEdit_vtable_addr, (void *) LocalPlayer_openTextEdit_injection);
patch_address(TextEditScreen_updateEvents_vtable_addr, (void *) TextEditScreen_updateEvents_injection);
2020-10-04 00:30:15 +00:00
}
2020-11-02 17:34:11 +00:00
2020-11-20 18:36:03 +00:00
if (extra_has_feature("Expand Creative Inventory")) {
// Add Extra Items To Creative Inventory (Only Replace Specific Function Call)
overwrite_call((void *) 0x8e0fc, (void *) FillingContainer_addItem_injection);
}
2020-11-20 18:37:54 +00:00
if (extra_has_feature("Animated Water")) {
2020-11-20 18:36:03 +00:00
// Tick Dynamic Textures (Animated Water)
overwrite_calls((void *) Minecraft_tick, (void *) Minecraft_tick_injection);
2020-11-02 17:34:11 +00:00
}
2020-11-23 23:03:28 +00:00
// Hijack Create World Button
patch_address(SelectWorldScreen_tick_vtable_addr, (void *) SelectWorldScreen_tick_injection);
patch_address(Touch_SelectWorldScreen_tick_vtable_addr, (void *) Touch_SelectWorldScreen_tick_injection);
// Make The SimpleChooseLevelScreen Back Button Go To SelectWorldScreen Instead Of StartMenuScreen
unsigned char simple_choose_level_screen_back_button_patch[4] = {0x05, 0x10, 0xa0, 0xe3};
patch((void *) 0x31144, simple_choose_level_screen_back_button_patch);
2020-10-04 00:30:15 +00:00
}
}