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

114 lines
4.1 KiB
C++
Raw Normal View History

2020-12-02 23:18:49 +00:00
#include <string>
#include <fstream>
#include <streambuf>
2020-12-04 17:17:51 +00:00
#include <cstring>
2020-12-02 23:18:49 +00:00
2021-01-27 21:26:19 +00:00
#include <libreborn/libreborn.h>
2020-12-02 23:18:49 +00:00
#include "../feature/feature.h"
#include "misc.h"
2021-02-03 22:00:02 +00:00
#include <libreborn/minecraft.h>
2020-12-02 23:18:49 +00:00
// Read Asset File
2020-12-04 17:17:51 +00:00
static AppPlatform_readAssetFile_return_value AppPlatform_readAssetFile_injection(__attribute__((unused)) unsigned char *app_platform, std::string const& path) {
// Read File
2020-12-02 23:18:49 +00:00
std::string full_path("./data/");
full_path.append(path);
std::ifstream stream(full_path);
std::string str((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
2020-12-04 17:17:51 +00:00
// Return String
AppPlatform_readAssetFile_return_value ret;
ret.length = str.length();
ret.data = strdup(str.c_str());
return ret;
2020-12-02 23:18:49 +00:00
}
2021-02-17 16:31:01 +00:00
// Add Item To Inventory
2020-12-02 23:18:49 +00:00
static void inventory_add_item(unsigned char *inventory, unsigned char *item, bool is_tile) {
unsigned char *item_instance = (unsigned char *) ::operator new(ITEM_INSTANCE_SIZE);
2021-02-16 17:26:40 +00:00
ALLOC_CHECK(item_instance);
2020-12-02 23:18:49 +00:00
item_instance = (*(is_tile ? ItemInstance_constructor_tile : ItemInstance_constructor_item))(item_instance, item);
(*FillingContainer_addItem)(inventory, item_instance);
}
2021-02-16 17:26:40 +00:00
// Expand Creative Inventory
2020-12-02 23:18:49 +00:00
static int32_t Inventory_setupDefault_FillingContainer_addItem_call_injection(unsigned char *filling_container, unsigned char *item_instance) {
// Call Original
int32_t ret = (*FillingContainer_addItem)(filling_container, item_instance);
// 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);
2020-12-04 17:17:51 +00:00
for (int i = 0; i < 16; i++) {
if (i == 15) {
// Bonemeal Is Already In The Creative Inventory
continue;
}
2021-02-10 20:25:56 +00:00
unsigned char *item_instance = (unsigned char *) ::operator new(ITEM_INSTANCE_SIZE);
2021-02-16 17:26:40 +00:00
ALLOC_CHECK(item_instance);
2020-12-02 23:18:49 +00:00
item_instance = (*ItemInstance_constructor_item_extra)(item_instance, *Item_dye_powder, 1, i);
(*FillingContainer_addItem)(filling_container, item_instance);
}
inventory_add_item(filling_container, *Item_camera, false);
// 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_web, 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);
return ret;
}
2021-02-16 17:26:40 +00:00
// Print Chat To Log
2021-02-16 19:35:03 +00:00
static bool Gui_addMessage_recursing = false;
2021-02-16 17:26:40 +00:00
static void Gui_addMessage_injection(unsigned char *gui, std::string const& text) {
2021-02-22 03:43:57 +00:00
// Sanitize Message
char *new_message = strdup(text.c_str());
ALLOC_CHECK(new_message);
2021-02-22 03:51:01 +00:00
sanitize_string(&new_message, -1, 1);
2021-02-22 03:43:57 +00:00
// Process Message
2021-02-16 19:35:03 +00:00
if (!Gui_addMessage_recursing) {
// Start Recursing
Gui_addMessage_recursing = true;
2021-02-16 17:26:40 +00:00
2021-02-16 19:35:03 +00:00
// Print Log Message
2021-02-22 03:43:57 +00:00
fprintf(stderr, "[CHAT]: %s\n", new_message);
2021-02-16 19:35:03 +00:00
// Call Original Method
2021-02-22 03:43:57 +00:00
(*Gui_addMessage)(gui, std::string(new_message));
2021-02-16 19:35:03 +00:00
// End Recursing
Gui_addMessage_recursing = false;
} else {
// Call Original Method
2021-02-22 03:43:57 +00:00
(*Gui_addMessage)(gui, std::string(new_message));
2021-02-16 19:35:03 +00:00
}
2021-02-22 03:43:57 +00:00
// Free
free(new_message);
2021-02-16 17:26:40 +00:00
}
// Init
2021-06-17 21:32:24 +00:00
void _init_misc_cpp() {
2020-12-02 23:18:49 +00:00
// Implement AppPlatform::readAssetFile So Translations Work
overwrite((void *) AppPlatform_readAssetFile, (void *) AppPlatform_readAssetFile_injection);
if (feature_has("Expand Creative Inventory")) {
// Add Extra Items To Creative Inventory (Only Replace Specific Function Call)
overwrite_call((void *) 0x8e0fc, (void *) Inventory_setupDefault_FillingContainer_addItem_call_injection);
}
2021-02-16 17:26:40 +00:00
// Print Chat To Log
overwrite_calls((void *) Gui_addMessage, (void *) Gui_addMessage_injection);
}