minecraft-pi-reborn/mods/src/misc/api.cpp

161 lines
5.1 KiB
C++
Raw Normal View History

2024-04-03 07:19:12 +00:00
#include <utility>
2022-06-27 18:47:55 +00:00
#include <vector>
#include <libreborn/libreborn.h>
#include <symbols/minecraft.h>
#include <mods/misc/misc.h>
#include "misc-internal.h"
2022-07-10 14:37:19 +00:00
// Callbacks
2024-02-03 03:25:22 +00:00
#define STORE_CALLBACK(name, type) \
static std::vector<misc_update_function_##type##_t> &get_misc_##name##_functions() { \
2024-01-06 11:30:23 +00:00
static std::vector<misc_update_function_##type##_t> functions; \
2022-07-10 14:37:19 +00:00
return functions; \
} \
2024-02-03 03:25:22 +00:00
void misc_run_on_##name(misc_update_function_##type##_t function) { \
get_misc_##name##_functions().push_back(function); \
}
#define SETUP_CALLBACK(name, type) \
STORE_CALLBACK(name, type) \
2024-01-06 11:30:23 +00:00
static void handle_misc_##name(type *obj) { \
for (misc_update_function_##type##_t function : get_misc_##name##_functions()) { \
2024-01-07 08:23:43 +00:00
function(obj); \
2022-07-10 14:37:19 +00:00
} \
}
2022-06-27 18:47:55 +00:00
// Run Functions On Update
2024-01-06 11:30:23 +00:00
SETUP_CALLBACK(update, Minecraft);
2022-06-27 18:47:55 +00:00
// Handle Custom Update Behavior
2024-04-03 07:19:12 +00:00
static void Minecraft_update_injection(Minecraft_update_t original, Minecraft *minecraft) {
2022-06-27 18:47:55 +00:00
// Call Original Method
2024-04-03 07:19:12 +00:00
original(minecraft);
2022-06-27 18:47:55 +00:00
// Run Functions
2022-07-10 14:37:19 +00:00
handle_misc_update(minecraft);
2022-06-27 18:47:55 +00:00
}
// Run Functions On Tick
2024-01-06 11:30:23 +00:00
SETUP_CALLBACK(tick, Minecraft);
2022-06-27 18:47:55 +00:00
// Handle Custom Tick Behavior
2024-04-03 07:19:12 +00:00
static void Minecraft_tick_injection(Minecraft_tick_t original, Minecraft *minecraft, int32_t param_1, int32_t param_2) {
2022-06-27 18:47:55 +00:00
// Call Original Method
2024-04-03 07:19:12 +00:00
original(minecraft, param_1, param_2);
2022-06-27 18:47:55 +00:00
// Run Functions
2022-07-10 14:37:19 +00:00
handle_misc_tick(minecraft);
2022-06-27 18:47:55 +00:00
}
// Run Functions On Recipes Setup
2024-01-06 11:30:23 +00:00
SETUP_CALLBACK(recipes_setup, Recipes);
2022-06-27 18:47:55 +00:00
// Handle Custom Recipes Setup Behavior
2024-04-03 07:19:12 +00:00
static Recipes *Recipes_injection(Recipes_constructor_t original, Recipes *recipes) {
2022-06-27 18:47:55 +00:00
// Call Original Method
2024-04-03 07:19:12 +00:00
original(recipes);
2022-06-27 18:47:55 +00:00
// Run Functions
2022-07-10 14:37:19 +00:00
handle_misc_recipes_setup(recipes);
2022-06-27 18:47:55 +00:00
// Return
return recipes;
}
// Run Functions On Furnace Recipes Setup
2024-01-06 11:30:23 +00:00
SETUP_CALLBACK(furnace_recipes_setup, FurnaceRecipes);
2022-06-27 18:47:55 +00:00
// Handle Custom Furnace Recipes Setup Behavior
2024-04-03 07:19:12 +00:00
static FurnaceRecipes *FurnaceRecipes_injection(FurnaceRecipes_constructor_t original, FurnaceRecipes *recipes) {
2022-06-27 18:47:55 +00:00
// Call Original Method
2024-04-03 07:19:12 +00:00
original(recipes);
2022-06-27 18:47:55 +00:00
// Run Functions
2022-07-10 14:37:19 +00:00
handle_misc_furnace_recipes_setup(recipes);
2022-06-27 18:47:55 +00:00
// Return
return recipes;
}
2022-07-10 14:37:19 +00:00
// Run Functions On Creative Inventory Setup
2024-01-06 11:30:23 +00:00
SETUP_CALLBACK(creative_inventory_setup, FillingContainer);
2022-07-10 14:37:19 +00:00
// Handle Custom Creative Inventory Setup Behavior
2024-01-06 11:30:23 +00:00
static void Inventory_setupDefault_FillingContainer_addItem_call_injection(FillingContainer *filling_container, ItemInstance *item_instance) {
2022-07-10 14:37:19 +00:00
// Call Original Method
2024-01-07 08:23:43 +00:00
FillingContainer_addItem(filling_container, item_instance);
2022-07-10 14:37:19 +00:00
// Run Functions
handle_misc_creative_inventory_setup(filling_container);
}
// Run Functions On Tiles Setup
2024-01-06 11:30:23 +00:00
SETUP_CALLBACK(tiles_setup, void);
2022-07-10 14:37:19 +00:00
// Handle Custom Tiles Setup Behavior
2024-04-03 07:19:12 +00:00
static void Tile_initTiles_injection(Tile_initTiles_t original) {
2022-07-10 14:37:19 +00:00
// Run Functions
2024-04-02 23:22:01 +00:00
handle_misc_tiles_setup(nullptr);
2022-07-10 14:37:19 +00:00
// Call Original Method
2024-04-03 07:19:12 +00:00
original();
2022-07-10 14:37:19 +00:00
}
// Run Functions On Items Setup
2024-01-06 11:30:23 +00:00
SETUP_CALLBACK(items_setup, void);
2022-07-10 14:37:19 +00:00
// Handle Custom Items Setup Behavior
2024-04-03 07:19:12 +00:00
static void Item_initItems_injection(Item_initItems_t original) {
// Call Original Method
2024-04-03 07:19:12 +00:00
original();
2022-07-10 14:37:19 +00:00
// Run Functions
2024-04-02 23:22:01 +00:00
handle_misc_items_setup(nullptr);
}
2022-07-10 14:37:19 +00:00
// Run Functions On Language Setup
SETUP_CALLBACK(language_setup, void);
// Handle Custom Items Setup Behavior
2024-04-03 07:19:12 +00:00
static void I18n_loadLanguage_injection(I18n_loadLanguage_t original, AppPlatform *app, std::string language_name) {
2022-07-10 14:37:19 +00:00
// Call Original Method
2024-04-03 07:19:12 +00:00
original(app, std::move(language_name));
// Run Functions
2024-04-02 23:22:01 +00:00
handle_misc_language_setup(nullptr);
2022-07-10 14:37:19 +00:00
}
2024-02-03 03:25:22 +00:00
// Run Functions On GUI Key Press
STORE_CALLBACK(game_key_press, key_press)
static bool handle_misc_game_key_press(Minecraft *minecraft, int key) {
for (misc_update_function_key_press_t function : get_misc_game_key_press_functions()) {
if (function(minecraft, key)) {
return true;
}
}
return false;
}
// Handle Key Presses
2024-04-03 07:19:12 +00:00
static void Gui_handleKeyPressed_injection(Gui_handleKeyPressed_t original, Gui *self, int key) {
2024-02-03 03:25:22 +00:00
// Run Functions
if (handle_misc_game_key_press(self->minecraft, key)) {
return;
}
// Call Original Method
2024-04-03 07:19:12 +00:00
original(self, key);
2024-02-03 03:25:22 +00:00
}
2022-06-27 18:47:55 +00:00
// Init
void _init_misc_api() {
// Handle Custom Update Behavior
2024-04-03 07:19:12 +00:00
overwrite_virtual_calls(Minecraft_update, Minecraft_update_injection);
2022-06-27 18:47:55 +00:00
// Handle Custom Tick Behavior
2024-04-03 07:19:12 +00:00
overwrite_calls(Minecraft_tick, Minecraft_tick_injection);
2022-06-27 18:47:55 +00:00
// Handle Custom Recipe Setup Behavior
2024-04-03 07:19:12 +00:00
overwrite_calls(Recipes_constructor, Recipes_injection);
overwrite_calls(FurnaceRecipes_constructor, FurnaceRecipes_injection);
2022-07-10 14:37:19 +00:00
// Handle Custom Creative Inventory Setup Behavior
overwrite_call((void *) 0x8e0fc, (void *) Inventory_setupDefault_FillingContainer_addItem_call_injection);
// Handle Custom Item/Tile Init Behavior
2024-04-03 07:19:12 +00:00
overwrite_calls(Tile_initTiles, Tile_initTiles_injection);
overwrite_calls(Item_initItems, Item_initItems_injection);
// Handle Custom Language Entries
2024-04-03 07:19:12 +00:00
overwrite_calls(I18n_loadLanguage, I18n_loadLanguage_injection);
2024-02-03 03:25:22 +00:00
// Handle Key Presses
2024-04-03 07:19:12 +00:00
overwrite_calls(Gui_handleKeyPressed, Gui_handleKeyPressed_injection);
2022-06-27 18:47:55 +00:00
}