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

67 lines
1.9 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>
2021-09-12 03:18:12 +00:00
#include <symbols/minecraft.h>
2020-12-02 23:18:49 +00:00
#include "../feature/feature.h"
#include "misc.h"
// 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-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
2021-07-04 23:02:45 +00:00
if (feature_has("Load Language Files", 1)) {
overwrite((void *) AppPlatform_readAssetFile, (void *) AppPlatform_readAssetFile_injection);
}
2020-12-02 23:18:49 +00:00
2021-02-16 17:26:40 +00:00
// Print Chat To Log
overwrite_calls((void *) Gui_addMessage, (void *) Gui_addMessage_injection);
}