minecraft-pi-reborn/mods/src/game-mode/game-mode.cpp

60 lines
2.7 KiB
C++
Raw Normal View History

2021-01-27 21:26:19 +00:00
#include <libreborn/libreborn.h>
2020-12-02 23:18:49 +00:00
2021-06-28 21:00:29 +00:00
#include "game-mode.h"
2020-12-02 23:18:49 +00:00
2021-02-03 22:00:02 +00:00
#include <libreborn/minecraft.h>
2020-12-02 23:18:49 +00:00
// Get Minecraft From Screen
static unsigned char *get_minecraft_from_screen(unsigned char *screen) {
2020-12-18 03:22:56 +00:00
return *(unsigned char **) (screen + Screen_minecraft_property_offset);
2020-12-02 23:18:49 +00:00
}
// Redirect Create World Button To SimpleLevelChooseScreen
#define WORLD_NAME "world"
static void SelectWorldScreen_tick_injection(unsigned char *screen) {
2020-12-18 03:22:56 +00:00
bool create_world = *(bool *) (screen + SelectWorldScreen_should_create_world_property_offset);
2020-12-02 23:18:49 +00:00
if (create_world) {
// Get New World Name
2020-12-05 01:54:29 +00:00
std::string new_name = (*SelectWorldScreen_getUniqueLevelName)(screen, WORLD_NAME);
2020-12-02 23:18:49 +00:00
// Create SimpleLevelChooseScreen
unsigned char *new_screen = (unsigned char *) ::operator new(SIMPLE_LEVEL_CHOOSE_SCREEN_SIZE);
2021-02-16 17:26:40 +00:00
ALLOC_CHECK(new_screen);
2020-12-02 23:18:49 +00:00
(*SimpleChooseLevelScreen)(new_screen, new_name);
// Set Screen
unsigned char *minecraft = get_minecraft_from_screen(screen);
(*Minecraft_setScreen)(minecraft, new_screen);
// Finish
2020-12-18 03:22:56 +00:00
*(bool *) (screen + SelectWorldScreen_world_created_property_offset) = true;
2020-12-02 23:18:49 +00:00
} else {
(*SelectWorldScreen_tick)(screen);
}
}
static void Touch_SelectWorldScreen_tick_injection(unsigned char *screen) {
2020-12-18 03:22:56 +00:00
bool create_world = *(bool *) (screen + Touch_SelectWorldScreen_should_create_world_property_offset);
2020-12-02 23:18:49 +00:00
if (create_world) {
// Get New World Name
2020-12-05 01:54:29 +00:00
std::string new_name = (*Touch_SelectWorldScreen_getUniqueLevelName)(screen, WORLD_NAME);
2020-12-02 23:18:49 +00:00
// Create SimpleLevelChooseScreen
unsigned char *new_screen = (unsigned char *) ::operator new(SIMPLE_LEVEL_CHOOSE_SCREEN_SIZE);
2021-02-16 17:26:40 +00:00
ALLOC_CHECK(new_screen);
2020-12-02 23:18:49 +00:00
(*SimpleChooseLevelScreen)(new_screen, new_name);
// Set Screen
unsigned char *minecraft = get_minecraft_from_screen(screen);
(*Minecraft_setScreen)(minecraft, new_screen);
// Finish
2020-12-18 03:22:56 +00:00
*(bool *) (screen + Touch_SelectWorldScreen_world_created_property_offset) = true;
2020-12-02 23:18:49 +00:00
} else {
(*Touch_SelectWorldScreen_tick)(screen);
}
}
2021-06-17 21:32:24 +00:00
void _init_game_mode_cpp() {
2020-12-02 23:18:49 +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
2021-03-05 00:27:24 +00:00
unsigned char simple_choose_level_screen_back_button_patch[4] = {0x05, 0x10, 0xa0, 0xe3}; // "mov r1, #0x5"
2020-12-02 23:18:49 +00:00
patch((void *) 0x31144, simple_choose_level_screen_back_button_patch);
2021-06-29 02:59:24 +00:00
patch((void *) 0x3134c, simple_choose_level_screen_back_button_patch);
2021-06-28 21:00:29 +00:00
}