From b2db6bcfd2380f6a11a7f652180994b4b15b2ad4 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Sun, 16 Jun 2024 00:47:36 -0400 Subject: [PATCH] Some Clean Up --- launcher/CMakeLists.txt | 5 +++-- launcher/src/crash-report.c | 5 ----- libreborn/include/libreborn/log.h | 10 +++------- libreborn/include/libreborn/string.h | 2 +- libreborn/src/util/exec.c | 1 - libreborn/src/util/log.c | 23 ----------------------- libreborn/src/util/string.c | 16 +++++++--------- mods/include/mods/version/version.h | 2 +- mods/src/chat/chat.cpp | 19 +++++++++---------- mods/src/misc/logging.cpp | 2 +- mods/src/misc/misc.cpp | 2 +- mods/src/screenshot/screenshot.cpp | 26 ++++++++++---------------- mods/src/version/version.cpp | 16 ++++++---------- 13 files changed, 42 insertions(+), 87 deletions(-) diff --git a/launcher/CMakeLists.txt b/launcher/CMakeLists.txt index f5d8afeda6..f29460336f 100644 --- a/launcher/CMakeLists.txt +++ b/launcher/CMakeLists.txt @@ -35,8 +35,9 @@ file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/launcher.desktop" "Categories=Game;\n" ) file(APPEND "${CMAKE_CURRENT_BINARY_DIR}/launcher.desktop" - "Terminal=true\n" - "NoDisplay=true\n" + "Terminal=false\n" + "StartupNotify=false\n" + "StartupWMClass=${MCPI_APP_ID}\n" ) install( FILES "${CMAKE_CURRENT_BINARY_DIR}/launcher.desktop" diff --git a/launcher/src/crash-report.c b/launcher/src/crash-report.c index fe5fd6fbe8..8502089480 100644 --- a/launcher/src/crash-report.c +++ b/launcher/src/crash-report.c @@ -193,11 +193,9 @@ void setup_crash_report() { fprintf(poll_fds[i].fd == output_pipe[PIPE_READ] ? stdout : stderr, "%s", buf); // Write To log - reborn_lock_log(); if (write(reborn_get_log_fd(), buf, bytes_read) == -1) { ERR("Unable To Write Log Data: %s", strerror(errno)); } - reborn_unlock_log(); } } else { // File Descriptor No Longer Accessible @@ -229,13 +227,10 @@ void setup_crash_report() { // Print Exit Code Log Line fprintf(stderr, "%s", exit_code_line); - // Write Exit Code Log Line - reborn_lock_log(); if (write(reborn_get_log_fd(), exit_code_line, strlen(exit_code_line)) == -1) { ERR("Unable To Write Exit Code To Log: %s", strerror(errno)); } - reborn_unlock_log(); // Free Exit Code Log Line free(exit_code_line); diff --git a/libreborn/include/libreborn/log.h b/libreborn/include/libreborn/log.h index da5d72d759..785f3d335e 100644 --- a/libreborn/include/libreborn/log.h +++ b/libreborn/include/libreborn/log.h @@ -10,21 +10,17 @@ extern "C" { // Log File #define MCPI_LOG_ENV "_MCPI_LOG" int reborn_get_log_fd(); -void reborn_lock_log(); -void reborn_unlock_log(); void reborn_close_log(); void reborn_set_log(const char *file); // Debug Logging #define MCPI_DEBUG_ENV "MCPI_DEBUG" extern const char *reborn_debug_tag; int reborn_get_debug_fd(); -void reborn_lock_debug(); -void reborn_unlock_debug(); // Logging -#define INFO(format, ...) { fprintf(stderr, "[INFO]: " format "\n", ##__VA_ARGS__); } -#define WARN(format, ...) { fprintf(stderr, "[WARN]: " format "\n", ##__VA_ARGS__); } -#define RAW_DEBUG(tag, format, ...) { reborn_lock_debug(); dprintf(reborn_get_debug_fd(), "[DEBUG]: %s" format "\n", tag, ##__VA_ARGS__); reborn_unlock_debug(); } +#define INFO(format, ...) fprintf(stderr, "[INFO]: " format "\n", ##__VA_ARGS__) +#define WARN(format, ...) fprintf(stderr, "[WARN]: " format "\n", ##__VA_ARGS__) +#define RAW_DEBUG(tag, format, ...) dprintf(reborn_get_debug_fd(), "[DEBUG]: %s" format "\n", tag, ##__VA_ARGS__) #define DEBUG(format, ...) RAW_DEBUG(reborn_debug_tag, format, ##__VA_ARGS__) #define ERR(format, ...) { fprintf(stderr, "[ERR]: (%s:%i): " format "\n", __FILE__, __LINE__, ##__VA_ARGS__); exit(EXIT_FAILURE); } #define IMPOSSIBLE() ERR("This Should Never Be Called") diff --git a/libreborn/include/libreborn/string.h b/libreborn/include/libreborn/string.h index c2593a5700..abe8c72078 100644 --- a/libreborn/include/libreborn/string.h +++ b/libreborn/include/libreborn/string.h @@ -32,7 +32,7 @@ extern "C" { #endif // Sanitize String -void sanitize_string(char **str, int max_length, unsigned int allow_newlines); +void sanitize_string(char *str, int max_length, unsigned int allow_newlines); // CP437 char *to_cp437(const char *input); diff --git a/libreborn/src/util/exec.c b/libreborn/src/util/exec.c index 4aad9e2a3b..c9e19ddd08 100644 --- a/libreborn/src/util/exec.c +++ b/libreborn/src/util/exec.c @@ -55,7 +55,6 @@ char *run_command(const char *const command[], int *exit_status, size_t *output_ close(output_pipe[1]); // Setup stderr - reborn_lock_debug(); // Lock Released On Process Exit dup2(reborn_get_debug_fd(), STDERR_FILENO); // Run diff --git a/libreborn/src/util/log.c b/libreborn/src/util/log.c index b1dea25d34..64b098044b 100644 --- a/libreborn/src/util/log.c +++ b/libreborn/src/util/log.c @@ -2,7 +2,6 @@ #include #include #include -#include #include #include @@ -29,18 +28,6 @@ int reborn_get_log_fd() { // Return return reborn_get_log_fd(); } -void reborn_lock_log() { - int ret = flock(reborn_get_log_fd(), LOCK_EX); - if (ret != 0) { - ERR("Unable To Lock Log: %s", strerror(errno)); - } -} -void reborn_unlock_log() { - int ret = flock(reborn_get_log_fd(), LOCK_UN); - if (ret != 0) { - ERR("Unable To Unlock Log: %s", strerror(errno)); - } -} __attribute__((destructor)) void reborn_close_log() { if (log_fd >= 0) { close(log_fd); @@ -60,14 +47,4 @@ static int should_print_debug_to_stderr() { } int reborn_get_debug_fd() { return should_print_debug_to_stderr() ? STDERR_FILENO : reborn_get_log_fd(); -} -void reborn_lock_debug() { - if (!should_print_debug_to_stderr()) { - reborn_lock_log(); - } -} -void reborn_unlock_debug() { - if (!should_print_debug_to_stderr()) { - reborn_unlock_log(); - } } \ No newline at end of file diff --git a/libreborn/src/util/string.c b/libreborn/src/util/string.c index e9e713e110..dd2f704d59 100644 --- a/libreborn/src/util/string.c +++ b/libreborn/src/util/string.c @@ -1,22 +1,20 @@ -#include - #include // Sanitize String -void sanitize_string(char **str, int max_length, unsigned int allow_newlines) { +void sanitize_string(char *str, int max_length, unsigned int allow_newlines) { // Store Message Length - int length = strlen(*str); + size_t length = strlen(str); // Truncate Message - if (max_length != -1 && length > max_length) { - (*str)[max_length] = '\0'; + if (max_length >= 0 && length > ((size_t) max_length)) { + str[max_length] = '\0'; length = max_length; } // Loop Through Message if (!allow_newlines) { - for (int i = 0; i < length; i++) { - if ((*str)[i] == '\n') { + for (size_t i = 0; i < length; i++) { + if (str[i] == '\n') { // Replace Newline - (*str)[i] = ' '; + str[i] = ' '; } } } diff --git a/mods/include/mods/version/version.h b/mods/include/mods/version/version.h index 065b59a9ea..f6cc07c816 100644 --- a/mods/include/mods/version/version.h +++ b/mods/include/mods/version/version.h @@ -1,5 +1,5 @@ #pragma once extern "C" { -char *version_get(); +const char *version_get(); } \ No newline at end of file diff --git a/mods/src/chat/chat.cpp b/mods/src/chat/chat.cpp index 56c01702c9..fa549fe7eb 100644 --- a/mods/src/chat/chat.cpp +++ b/mods/src/chat/chat.cpp @@ -26,11 +26,9 @@ std::string chat_send_api_command(Minecraft *minecraft, std::string str) { } // Send API Chat Command -static void send_api_chat_command(Minecraft *minecraft, char *str) { - char *command = nullptr; - safe_asprintf(&command, "chat.post(%s)\n", str); +static void send_api_chat_command(Minecraft *minecraft, const char *str) { + const std::string command = std::string("chat.post(") + str + ")\n"; chat_send_api_command(minecraft, command); - free(command); } // Send Message To Players @@ -38,12 +36,13 @@ std::string _chat_get_prefix(char *username) { return std::string("<") + username + "> "; } void chat_send_message(ServerSideNetworkHandler *server_side_network_handler, char *username, char *message) { - char *full_message = nullptr; - safe_asprintf(&full_message, "%s%s", _chat_get_prefix(username).c_str(), message); - sanitize_string(&full_message, MAX_CHAT_MESSAGE_LENGTH, 0); - std::string cpp_string = full_message; - free(full_message); - server_side_network_handler->displayGameMessage(&cpp_string); + std::string full_message = _chat_get_prefix(username) + message; + char *raw_str = strdup(full_message.c_str()); + ALLOC_CHECK(raw_str); + sanitize_string(raw_str, MAX_CHAT_MESSAGE_LENGTH, 0); + full_message = raw_str; + free(raw_str); + server_side_network_handler->displayGameMessage(&full_message); } // Handle Chat packet Send void chat_handle_packet_send(Minecraft *minecraft, ChatPacket *packet) { diff --git a/mods/src/misc/logging.cpp b/mods/src/misc/logging.cpp index c30289df8a..b70677f297 100644 --- a/mods/src/misc/logging.cpp +++ b/mods/src/misc/logging.cpp @@ -12,7 +12,7 @@ static void Gui_addMessage_injection(Gui_addMessage_t original, Gui *gui, std::s // Sanitize Message char *new_message = strdup(text->c_str()); ALLOC_CHECK(new_message); - sanitize_string(&new_message, -1, 1); + sanitize_string(new_message, -1, 1); std::string cpp_str = new_message; // Process Message diff --git a/mods/src/misc/misc.cpp b/mods/src/misc/misc.cpp index dd15b7e465..b6b41a3915 100644 --- a/mods/src/misc/misc.cpp +++ b/mods/src/misc/misc.cpp @@ -217,7 +217,7 @@ static void LoginPacket_read_injection(LoginPacket_read_t original, LoginPacket // Sanitize char *new_username = strdup(c_str); ALLOC_CHECK(new_username); - sanitize_string(&new_username, MAX_USERNAME_LENGTH, 0); + sanitize_string(new_username, MAX_USERNAME_LENGTH, 0); // Set New Username rak_string->Assign(new_username); // Free diff --git a/mods/src/screenshot/screenshot.cpp b/mods/src/screenshot/screenshot.cpp index 384f110218..cb4c345caa 100644 --- a/mods/src/screenshot/screenshot.cpp +++ b/mods/src/screenshot/screenshot.cpp @@ -14,12 +14,12 @@ #include // Ensure Screenshots Folder Exists -static void ensure_screenshots_folder(char *screenshots) { +static void ensure_screenshots_folder(const char *screenshots) { // Check Screenshots Folder struct stat obj = {}; if (stat(screenshots, &obj) != 0 || !S_ISDIR(obj.st_mode)) { // Create Screenshots Folder - int ret = mkdir(screenshots, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + const int ret = mkdir(screenshots, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); if (ret != 0) { // Unable To Create Folder ERR("Error Creating Directory: %s: %s", screenshots, strerror(errno)); @@ -45,8 +45,7 @@ void screenshot_take(const char *home) { } // Get Directory - char *screenshots = nullptr; - safe_asprintf(&screenshots, "%s/screenshots", home); + const std::string screenshots = std::string(home) + "/screenshots"; // Get Timestamp time_t rawtime; @@ -57,16 +56,13 @@ void screenshot_take(const char *home) { strftime(time, TIME_SIZE, "%Y-%m-%d_%H.%M.%S", timeinfo); // Ensure Screenshots Folder Exists - ensure_screenshots_folder(screenshots); + ensure_screenshots_folder(screenshots.c_str()); // Prevent Overwriting Screenshots int num = 1; - char *file = nullptr; - safe_asprintf(&file, "%s/%s.png", screenshots, time); - while (access(file, F_OK) != -1) { - free(file); - file = nullptr; - safe_asprintf(&file, "%s/%s-%i.png", screenshots, time, num); + std::string file = screenshots + '/' + time + ".png"; + while (access(file.c_str(), F_OK) != -1) { + file = screenshots + '/' + time + '-' + std::to_string(num) + ".png"; num++; } @@ -98,14 +94,12 @@ void screenshot_take(const char *home) { glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); // Save Image - if (save_png(file, pixels, line_size, width, height)) { - WARN("Screenshot Failed: %s", file); + if (save_png(file.c_str(), pixels, line_size, width, height)) { + WARN("Screenshot Failed: %s", file.c_str()); } else { - INFO("Screenshot Saved: %s", file); + INFO("Screenshot Saved: %s", file.c_str()); } // Free - free(file); - free(screenshots); free(pixels); } diff --git a/mods/src/version/version.cpp b/mods/src/version/version.cpp index 6260c6219e..049e1a54b9 100644 --- a/mods/src/version/version.cpp +++ b/mods/src/version/version.cpp @@ -5,18 +5,14 @@ #include // Get New Version -char *version_get() { - static char *version = nullptr; +const char *version_get() { + static std::string version = ""; // Load - if (version == nullptr) { - safe_asprintf(&version, "%s / Reborn v%s", Strings::minecraft_pi_version, reborn_get_version()); + if (version.empty()) { + version = std::string(Strings::minecraft_pi_version) + " / Reborn v" + reborn_get_version(); } // Return - return version; -} -// Free -__attribute__((destructor)) static void _free_version() { - free(version_get()); + return version.c_str(); } // Injection For Touch GUI Version @@ -30,7 +26,7 @@ void init_version() { // Touch GUI overwrite(Common_getGameVersionString, Common_getGameVersionString_injection); // Normal GUI - patch_address((void *) &Strings::minecraft_pi_version, version_get()); + patch_address((void *) &Strings::minecraft_pi_version, (void *) version_get()); // Log INFO("Starting Minecraft: Pi Edition (%s)", version_get());