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

104 lines
2.8 KiB
C++
Raw Normal View History

2021-11-14 04:29:48 +00:00
#include <string>
#include <libreborn/libreborn.h>
#include <symbols/minecraft.h>
2022-06-25 21:30:08 +00:00
#include "misc-internal.h"
#include <mods/misc/misc.h>
2021-11-14 04:29:48 +00:00
// Print Chat To Log
static bool Gui_addMessage_recursing = false;
2024-01-06 11:30:23 +00:00
static void Gui_addMessage_injection(Gui *gui, std::string *text) {
2021-11-14 04:29:48 +00:00
// Sanitize Message
2024-01-06 11:30:23 +00:00
char *new_message = strdup(text->c_str());
2021-11-14 04:29:48 +00:00
ALLOC_CHECK(new_message);
sanitize_string(&new_message, -1, 1);
2024-01-06 11:30:23 +00:00
std::string cpp_str = new_message;
2021-11-14 04:29:48 +00:00
// Process Message
if (!Gui_addMessage_recursing) {
// Start Recursing
Gui_addMessage_recursing = true;
// Print Log Message
2022-07-20 06:58:14 +00:00
char *safe_message = from_cp437(new_message);
fprintf(stderr, "[CHAT]: %s\n", safe_message);
free(safe_message);
2021-11-14 04:29:48 +00:00
// Call Original Method
2024-01-07 08:23:43 +00:00
Gui_addMessage(gui, &cpp_str);
2021-11-14 04:29:48 +00:00
// End Recursing
Gui_addMessage_recursing = false;
} else {
// Call Original Method
2024-01-07 08:23:43 +00:00
Gui_addMessage(gui, &cpp_str);
2021-11-14 04:29:48 +00:00
}
// Free
free(new_message);
}
2024-01-06 11:30:23 +00:00
void misc_add_message(Gui *gui, const char *text) {
std::string str = text;
Gui_addMessage_injection(gui, &str);
2022-06-27 01:17:52 +00:00
}
2021-11-14 04:29:48 +00:00
// Print Progress Reports
static int last_progress = -1;
static const char *last_message = NULL;
2024-01-06 11:30:23 +00:00
static void print_progress(Minecraft *minecraft) {
2024-01-07 08:23:43 +00:00
const char *message = Minecraft_getProgressMessage(minecraft);
2024-01-06 11:30:23 +00:00
int32_t progress = minecraft->progress;
2024-01-07 08:23:43 +00:00
if (Minecraft_isLevelGenerated(minecraft)) {
2021-11-14 04:29:48 +00:00
message = "Ready";
progress = -1;
}
if (message != NULL) {
bool message_different = message != last_message;
bool progress_significant = is_progress_difference_significant(progress, last_progress);
if (message_different || progress_significant) {
if (progress != -1) {
INFO("Status: %s: %i%%", message, progress);
} else {
INFO("Status: %s", message);
}
if (message_different) {
last_message = message;
}
if (progress_significant) {
last_progress = progress;
}
}
}
}
// Print Progress Reports Regularly
2024-01-06 11:30:23 +00:00
static void Minecraft_update_injection(Minecraft *minecraft) {
2021-11-14 04:29:48 +00:00
// Print Progress Reports
print_progress(minecraft);
}
// Log When Game Is Saved
2024-01-06 11:30:23 +00:00
void Level_saveLevelData_injection(Level *level) {
2021-11-14 04:29:48 +00:00
// Print Log Message
2022-05-03 00:43:52 +00:00
DEBUG("Saving Game");
2021-11-14 04:29:48 +00:00
// Call Original Method
2024-01-07 08:23:43 +00:00
Level_saveLevelData(level);
2021-11-14 04:29:48 +00:00
}
// Init
void _init_misc_logging() {
// Print Chat To Log
overwrite_calls((void *) Gui_addMessage, (void *) Gui_addMessage_injection);
// Print Progress Reports
misc_run_on_update(Minecraft_update_injection);
// Print Log On Game Save
overwrite_calls((void *) Level_saveLevelData, (void *) Level_saveLevelData_injection);
2022-05-14 03:27:06 +00:00
// Disable stdout Buffering
setvbuf(stdout, NULL, _IONBF, 0);
2021-11-14 04:29:48 +00:00
}