Some Clean Up

This commit is contained in:
TheBrokenRail 2024-06-16 00:47:36 -04:00
parent 36fe263ede
commit b2db6bcfd2
13 changed files with 42 additions and 87 deletions

View File

@ -35,8 +35,9 @@ file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/launcher.desktop"
"Categories=Game;\n" "Categories=Game;\n"
) )
file(APPEND "${CMAKE_CURRENT_BINARY_DIR}/launcher.desktop" file(APPEND "${CMAKE_CURRENT_BINARY_DIR}/launcher.desktop"
"Terminal=true\n" "Terminal=false\n"
"NoDisplay=true\n" "StartupNotify=false\n"
"StartupWMClass=${MCPI_APP_ID}\n"
) )
install( install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/launcher.desktop" FILES "${CMAKE_CURRENT_BINARY_DIR}/launcher.desktop"

View File

@ -193,11 +193,9 @@ void setup_crash_report() {
fprintf(poll_fds[i].fd == output_pipe[PIPE_READ] ? stdout : stderr, "%s", buf); fprintf(poll_fds[i].fd == output_pipe[PIPE_READ] ? stdout : stderr, "%s", buf);
// Write To log // Write To log
reborn_lock_log();
if (write(reborn_get_log_fd(), buf, bytes_read) == -1) { if (write(reborn_get_log_fd(), buf, bytes_read) == -1) {
ERR("Unable To Write Log Data: %s", strerror(errno)); ERR("Unable To Write Log Data: %s", strerror(errno));
} }
reborn_unlock_log();
} }
} else { } else {
// File Descriptor No Longer Accessible // File Descriptor No Longer Accessible
@ -229,13 +227,10 @@ void setup_crash_report() {
// Print Exit Code Log Line // Print Exit Code Log Line
fprintf(stderr, "%s", exit_code_line); fprintf(stderr, "%s", exit_code_line);
// Write Exit Code Log Line // Write Exit Code Log Line
reborn_lock_log();
if (write(reborn_get_log_fd(), exit_code_line, strlen(exit_code_line)) == -1) { 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)); ERR("Unable To Write Exit Code To Log: %s", strerror(errno));
} }
reborn_unlock_log();
// Free Exit Code Log Line // Free Exit Code Log Line
free(exit_code_line); free(exit_code_line);

View File

@ -10,21 +10,17 @@ extern "C" {
// Log File // Log File
#define MCPI_LOG_ENV "_MCPI_LOG" #define MCPI_LOG_ENV "_MCPI_LOG"
int reborn_get_log_fd(); int reborn_get_log_fd();
void reborn_lock_log();
void reborn_unlock_log();
void reborn_close_log(); void reborn_close_log();
void reborn_set_log(const char *file); void reborn_set_log(const char *file);
// Debug Logging // Debug Logging
#define MCPI_DEBUG_ENV "MCPI_DEBUG" #define MCPI_DEBUG_ENV "MCPI_DEBUG"
extern const char *reborn_debug_tag; extern const char *reborn_debug_tag;
int reborn_get_debug_fd(); int reborn_get_debug_fd();
void reborn_lock_debug();
void reborn_unlock_debug();
// Logging // Logging
#define INFO(format, ...) { fprintf(stderr, "[INFO]: " format "\n", ##__VA_ARGS__); } #define INFO(format, ...) fprintf(stderr, "[INFO]: " format "\n", ##__VA_ARGS__)
#define WARN(format, ...) { fprintf(stderr, "[WARN]: " 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 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 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 ERR(format, ...) { fprintf(stderr, "[ERR]: (%s:%i): " format "\n", __FILE__, __LINE__, ##__VA_ARGS__); exit(EXIT_FAILURE); }
#define IMPOSSIBLE() ERR("This Should Never Be Called") #define IMPOSSIBLE() ERR("This Should Never Be Called")

View File

@ -32,7 +32,7 @@ extern "C" {
#endif #endif
// Sanitize String // 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 // CP437
char *to_cp437(const char *input); char *to_cp437(const char *input);

View File

@ -55,7 +55,6 @@ char *run_command(const char *const command[], int *exit_status, size_t *output_
close(output_pipe[1]); close(output_pipe[1]);
// Setup stderr // Setup stderr
reborn_lock_debug(); // Lock Released On Process Exit
dup2(reborn_get_debug_fd(), STDERR_FILENO); dup2(reborn_get_debug_fd(), STDERR_FILENO);
// Run // Run

View File

@ -2,7 +2,6 @@
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <sys/file.h>
#include <libreborn/log.h> #include <libreborn/log.h>
#include <libreborn/exec.h> #include <libreborn/exec.h>
@ -29,18 +28,6 @@ int reborn_get_log_fd() {
// Return // Return
return reborn_get_log_fd(); 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() { __attribute__((destructor)) void reborn_close_log() {
if (log_fd >= 0) { if (log_fd >= 0) {
close(log_fd); close(log_fd);
@ -60,14 +47,4 @@ static int should_print_debug_to_stderr() {
} }
int reborn_get_debug_fd() { int reborn_get_debug_fd() {
return should_print_debug_to_stderr() ? STDERR_FILENO : reborn_get_log_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();
}
} }

View File

@ -1,22 +1,20 @@
#include <stdint.h>
#include <libreborn/string.h> #include <libreborn/string.h>
// Sanitize String // 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 // Store Message Length
int length = strlen(*str); size_t length = strlen(str);
// Truncate Message // Truncate Message
if (max_length != -1 && length > max_length) { if (max_length >= 0 && length > ((size_t) max_length)) {
(*str)[max_length] = '\0'; str[max_length] = '\0';
length = max_length; length = max_length;
} }
// Loop Through Message // Loop Through Message
if (!allow_newlines) { if (!allow_newlines) {
for (int i = 0; i < length; i++) { for (size_t i = 0; i < length; i++) {
if ((*str)[i] == '\n') { if (str[i] == '\n') {
// Replace Newline // Replace Newline
(*str)[i] = ' '; str[i] = ' ';
} }
} }
} }

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
extern "C" { extern "C" {
char *version_get(); const char *version_get();
} }

View File

@ -26,11 +26,9 @@ std::string chat_send_api_command(Minecraft *minecraft, std::string str) {
} }
// Send API Chat Command // Send API Chat Command
static void send_api_chat_command(Minecraft *minecraft, char *str) { static void send_api_chat_command(Minecraft *minecraft, const char *str) {
char *command = nullptr; const std::string command = std::string("chat.post(") + str + ")\n";
safe_asprintf(&command, "chat.post(%s)\n", str);
chat_send_api_command(minecraft, command); chat_send_api_command(minecraft, command);
free(command);
} }
// Send Message To Players // Send Message To Players
@ -38,12 +36,13 @@ std::string _chat_get_prefix(char *username) {
return std::string("<") + username + "> "; return std::string("<") + username + "> ";
} }
void chat_send_message(ServerSideNetworkHandler *server_side_network_handler, char *username, char *message) { void chat_send_message(ServerSideNetworkHandler *server_side_network_handler, char *username, char *message) {
char *full_message = nullptr; std::string full_message = _chat_get_prefix(username) + message;
safe_asprintf(&full_message, "%s%s", _chat_get_prefix(username).c_str(), message); char *raw_str = strdup(full_message.c_str());
sanitize_string(&full_message, MAX_CHAT_MESSAGE_LENGTH, 0); ALLOC_CHECK(raw_str);
std::string cpp_string = full_message; sanitize_string(raw_str, MAX_CHAT_MESSAGE_LENGTH, 0);
free(full_message); full_message = raw_str;
server_side_network_handler->displayGameMessage(&cpp_string); free(raw_str);
server_side_network_handler->displayGameMessage(&full_message);
} }
// Handle Chat packet Send // Handle Chat packet Send
void chat_handle_packet_send(Minecraft *minecraft, ChatPacket *packet) { void chat_handle_packet_send(Minecraft *minecraft, ChatPacket *packet) {

View File

@ -12,7 +12,7 @@ static void Gui_addMessage_injection(Gui_addMessage_t original, Gui *gui, std::s
// Sanitize Message // Sanitize Message
char *new_message = strdup(text->c_str()); char *new_message = strdup(text->c_str());
ALLOC_CHECK(new_message); ALLOC_CHECK(new_message);
sanitize_string(&new_message, -1, 1); sanitize_string(new_message, -1, 1);
std::string cpp_str = new_message; std::string cpp_str = new_message;
// Process Message // Process Message

View File

@ -217,7 +217,7 @@ static void LoginPacket_read_injection(LoginPacket_read_t original, LoginPacket
// Sanitize // Sanitize
char *new_username = strdup(c_str); char *new_username = strdup(c_str);
ALLOC_CHECK(new_username); ALLOC_CHECK(new_username);
sanitize_string(&new_username, MAX_USERNAME_LENGTH, 0); sanitize_string(new_username, MAX_USERNAME_LENGTH, 0);
// Set New Username // Set New Username
rak_string->Assign(new_username); rak_string->Assign(new_username);
// Free // Free

View File

@ -14,12 +14,12 @@
#include <mods/screenshot/screenshot.h> #include <mods/screenshot/screenshot.h>
// Ensure Screenshots Folder Exists // Ensure Screenshots Folder Exists
static void ensure_screenshots_folder(char *screenshots) { static void ensure_screenshots_folder(const char *screenshots) {
// Check Screenshots Folder // Check Screenshots Folder
struct stat obj = {}; struct stat obj = {};
if (stat(screenshots, &obj) != 0 || !S_ISDIR(obj.st_mode)) { if (stat(screenshots, &obj) != 0 || !S_ISDIR(obj.st_mode)) {
// Create Screenshots Folder // 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) { if (ret != 0) {
// Unable To Create Folder // Unable To Create Folder
ERR("Error Creating Directory: %s: %s", screenshots, strerror(errno)); ERR("Error Creating Directory: %s: %s", screenshots, strerror(errno));
@ -45,8 +45,7 @@ void screenshot_take(const char *home) {
} }
// Get Directory // Get Directory
char *screenshots = nullptr; const std::string screenshots = std::string(home) + "/screenshots";
safe_asprintf(&screenshots, "%s/screenshots", home);
// Get Timestamp // Get Timestamp
time_t rawtime; time_t rawtime;
@ -57,16 +56,13 @@ void screenshot_take(const char *home) {
strftime(time, TIME_SIZE, "%Y-%m-%d_%H.%M.%S", timeinfo); strftime(time, TIME_SIZE, "%Y-%m-%d_%H.%M.%S", timeinfo);
// Ensure Screenshots Folder Exists // Ensure Screenshots Folder Exists
ensure_screenshots_folder(screenshots); ensure_screenshots_folder(screenshots.c_str());
// Prevent Overwriting Screenshots // Prevent Overwriting Screenshots
int num = 1; int num = 1;
char *file = nullptr; std::string file = screenshots + '/' + time + ".png";
safe_asprintf(&file, "%s/%s.png", screenshots, time); while (access(file.c_str(), F_OK) != -1) {
while (access(file, F_OK) != -1) { file = screenshots + '/' + time + '-' + std::to_string(num) + ".png";
free(file);
file = nullptr;
safe_asprintf(&file, "%s/%s-%i.png", screenshots, time, num);
num++; num++;
} }
@ -98,14 +94,12 @@ void screenshot_take(const char *home) {
glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
// Save Image // Save Image
if (save_png(file, pixels, line_size, width, height)) { if (save_png(file.c_str(), pixels, line_size, width, height)) {
WARN("Screenshot Failed: %s", file); WARN("Screenshot Failed: %s", file.c_str());
} else { } else {
INFO("Screenshot Saved: %s", file); INFO("Screenshot Saved: %s", file.c_str());
} }
// Free // Free
free(file);
free(screenshots);
free(pixels); free(pixels);
} }

View File

@ -5,18 +5,14 @@
#include <mods/init/init.h> #include <mods/init/init.h>
// Get New Version // Get New Version
char *version_get() { const char *version_get() {
static char *version = nullptr; static std::string version = "";
// Load // Load
if (version == nullptr) { if (version.empty()) {
safe_asprintf(&version, "%s / Reborn v%s", Strings::minecraft_pi_version, reborn_get_version()); version = std::string(Strings::minecraft_pi_version) + " / Reborn v" + reborn_get_version();
} }
// Return // Return
return version; return version.c_str();
}
// Free
__attribute__((destructor)) static void _free_version() {
free(version_get());
} }
// Injection For Touch GUI Version // Injection For Touch GUI Version
@ -30,7 +26,7 @@ void init_version() {
// Touch GUI // Touch GUI
overwrite(Common_getGameVersionString, Common_getGameVersionString_injection); overwrite(Common_getGameVersionString, Common_getGameVersionString_injection);
// Normal GUI // Normal GUI
patch_address((void *) &Strings::minecraft_pi_version, version_get()); patch_address((void *) &Strings::minecraft_pi_version, (void *) version_get());
// Log // Log
INFO("Starting Minecraft: Pi Edition (%s)", version_get()); INFO("Starting Minecraft: Pi Edition (%s)", version_get());