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

167 lines
6.6 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
}
2021-02-17 16:31:01 +00:00
// Death Messages
static std::string get_death_message(unsigned char *player) {
// Get Username
char *username = *(char **) (player + Player_username_property_offset);
// Prepare Death Message
std::string message;
message.append(username);
message.append(" has died");
// Return
return message;
}
// Common Death Message Logic
static void Player_actuallyHurt_injection_helper(unsigned char *player, int32_t damage, bool is_local_player) {
// Store Old Health
int32_t old_health = *(int32_t *) (player + Mob_health_property_offset);
// Call Original Method
(*(is_local_player ? LocalPlayer_actuallyHurt : Mob_actuallyHurt))(player, damage);
// Store New Health
2021-02-17 16:37:10 +00:00
int32_t new_health = *(int32_t *) (player + Mob_health_property_offset);
2021-02-17 16:31:01 +00:00
// Get Variables
unsigned char *minecraft = *(unsigned char **) (player + (is_local_player ? LocalPlayer_minecraft_property_offset : ServerPlayer_minecraft_property_offset));
unsigned char *rak_net_instance = *(unsigned char **) (minecraft + Minecraft_rak_net_instance_property_offset);
unsigned char *rak_net_instance_vtable = *(unsigned char **) rak_net_instance;
// Only Run On Server-Side
RakNetInstance_isServer_t RakNetInstance_isServer = *(RakNetInstance_isServer_t *) (rak_net_instance_vtable + RakNetInstance_isServer_vtable_offset);
if ((*RakNetInstance_isServer)(rak_net_instance)) {
// Check Health
2021-02-17 16:37:10 +00:00
if (new_health < 1 && old_health >= 1) {
2021-02-17 16:31:01 +00:00
// Get Death Message
std::string message = get_death_message(player);
// Post Death Message
unsigned char *server_side_network_handler = *(unsigned char **) (minecraft + Minecraft_network_handler_property_offset);
(*ServerSideNetworkHandler_displayGameMessage)(server_side_network_handler, message);
}
}
}
// ServerPlayer Death Message Logic
static void ServerPlayer_actuallyHurt_injection(unsigned char *player, int32_t damage) {
Player_actuallyHurt_injection_helper(player, damage, false);
}
// LocalPlayer Death Message Logic
static void LocalPlayer_actuallyHurt_injection(unsigned char *player, int32_t damage) {
Player_actuallyHurt_injection_helper(player, damage, true);
}
2020-12-02 23:18:49 +00:00
void init_misc_cpp() {
// 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);
2021-02-17 16:31:01 +00:00
// Death Messages
patch_address(ServerPlayer_actuallyHurt_vtable_addr, (void *) ServerPlayer_actuallyHurt_injection);
patch_address(LocalPlayer_actuallyHurt_vtable_addr, (void *) LocalPlayer_actuallyHurt_injection);
2020-12-02 23:18:49 +00:00
}