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"
)
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"

View File

@ -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);

View File

@ -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")

View File

@ -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);

View File

@ -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

View File

@ -2,7 +2,6 @@
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/file.h>
#include <libreborn/log.h>
#include <libreborn/exec.h>
@ -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);
@ -61,13 +48,3 @@ 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();
}
}

View File

@ -1,22 +1,20 @@
#include <stdint.h>
#include <libreborn/string.h>
// 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] = ' ';
}
}
}

View File

@ -1,5 +1,5 @@
#pragma once
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
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) {

View File

@ -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

View File

@ -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

View File

@ -14,12 +14,12 @@
#include <mods/screenshot/screenshot.h>
// 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);
}

View File

@ -5,18 +5,14 @@
#include <mods/init/init.h>
// 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());