From 3d89fb691a5deb412f4d395fd87a1a43c18ef3a8 Mon Sep 17 00:00:00 2001
From: TheBrokenRail <connor24nolan@live.com>
Date: Fri, 22 Nov 2024 22:02:55 -0500
Subject: [PATCH] Some Fixes

---
 CMakeLists.txt                        |  7 +++++--
 dependencies/glfw/CMakeLists.txt      |  1 -
 dependencies/imgui/CMakeLists.txt     |  2 +-
 launcher/src/logger/logger.cpp        |  9 ++-------
 libreborn/include/libreborn/util.h    |  6 +++++-
 libreborn/src/util/util.cpp           | 23 ++++++++++++++++++++++-
 media-layer/CMakeLists.txt            |  2 +-
 media-layer/core/CMakeLists.txt       | 12 +++---------
 media-layer/trampoline/CMakeLists.txt |  9 ++++++---
 mods/src/misc/misc.cpp                |  9 ++-------
 mods/src/screenshot/screenshot.cpp    | 10 ++--------
 11 files changed, 49 insertions(+), 41 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 679036c0..98f4027f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -60,7 +60,10 @@ string(CONCAT COMPILE_FLAGS_SETUP
     "set(CMAKE_CXX_STANDARD 20)\n"
 
     # Skip RPath
-    "set(CMAKE_SKIP_BUILD_RPATH TRUE)"
+    "set(CMAKE_SKIP_BUILD_RPATH TRUE)\n"
+
+    # Always Build Shared Libraries
+    "set(BUILD_SHARED_LIBS TRUE CACHE BOOL \"\" FORCE)"
 )
 if(BUILD_ARM_COMPONENTS)
     string(CONCAT COMPILE_FLAGS_SETUP
@@ -122,7 +125,7 @@ endif()
 
 # Install SDK
 if(BUILD_ARM_COMPONENTS)
-    install(EXPORT sdk DESTINATION "${MCPI_SDK_DIR}" FILE "sdk-targets.cmake" EXPORT_LINK_INTERFACE_LIBRARIES)
+    install(EXPORT sdk DESTINATION "${MCPI_SDK_DIR}" FILE "sdk-targets.cmake")
     file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/sdk.cmake"
         # Sanity Check
         "${ARM_SANITY_CHECK}\n"
diff --git a/dependencies/glfw/CMakeLists.txt b/dependencies/glfw/CMakeLists.txt
index 8addea7d..7504dc83 100644
--- a/dependencies/glfw/CMakeLists.txt
+++ b/dependencies/glfw/CMakeLists.txt
@@ -6,7 +6,6 @@ add_compile_options(-w)
 ## GLFW
 
 # Download
-set(BUILD_SHARED_LIBS TRUE CACHE BOOL "" FORCE)
 set(GLFW_BUILD_EXAMPLES FALSE CACHE BOOL "" FORCE)
 set(GLFW_BUILD_TESTS FALSE CACHE BOOL "" FORCE)
 set(GLFW_BUILD_DOCS FALSE CACHE BOOL "" FORCE)
diff --git a/dependencies/imgui/CMakeLists.txt b/dependencies/imgui/CMakeLists.txt
index 4f077e22..6d5a5a33 100644
--- a/dependencies/imgui/CMakeLists.txt
+++ b/dependencies/imgui/CMakeLists.txt
@@ -20,7 +20,7 @@ setup_header_dirs(imgui
     "${CMAKE_CURRENT_SOURCE_DIR}/src/backends"
     "${CMAKE_CURRENT_SOURCE_DIR}/src/misc/cpp"
 )
-find_package(OpenGL REQUIRED)
+find_package(OpenGL REQUIRED QUIET)
 target_link_libraries(imgui PUBLIC glfw OpenGL::GL)
 
 # Fonts
diff --git a/launcher/src/logger/logger.cpp b/launcher/src/logger/logger.cpp
index de2b5e15..0c53d377 100644
--- a/launcher/src/logger/logger.cpp
+++ b/launcher/src/logger/logger.cpp
@@ -5,7 +5,6 @@
 #include <cstdio>
 #include <csignal>
 #include <sys/stat.h>
-#include <ctime>
 #include <string>
 #include <fcntl.h>
 
@@ -38,17 +37,13 @@ static void setup_log_file() {
     const std::string logs = get_logs_folder();
 
     // Get Timestamp
-    time_t raw_time;
-    time(&raw_time);
-    const tm *time_info = localtime(&raw_time);
-    char time[512];
-    strftime(time, 512, "%Y-%m-%d", time_info);
+    std::string time = format_time("%Y-%m-%d");
 
     // Get Log Filename
     std::string file;
     int num = 1;
     do {
-        file = std::string(time) + '-' + std::to_string(num) + ".log";
+        file = time + '-' + std::to_string(num) + ".log";
         log_filename = logs + '/' + file;
         num++;
     } while (access(log_filename.c_str(), F_OK) != -1);
diff --git a/libreborn/include/libreborn/util.h b/libreborn/include/libreborn/util.h
index 858fb3f6..144fc472 100644
--- a/libreborn/include/libreborn/util.h
+++ b/libreborn/include/libreborn/util.h
@@ -56,4 +56,8 @@ void safe_write(int fd, const void *buf, size_t size);
     extern size_t name##_len
 
 // Profile Directory
-std::string home_get();
\ No newline at end of file
+std::string home_get();
+
+// Format Time
+std::string format_time(const char *fmt);
+std::string format_time(const char *fmt, int time);
\ No newline at end of file
diff --git a/libreborn/src/util/util.cpp b/libreborn/src/util/util.cpp
index 9898c20c..7343f4e5 100644
--- a/libreborn/src/util/util.cpp
+++ b/libreborn/src/util/util.cpp
@@ -2,6 +2,7 @@
 #include <sys/file.h>
 #include <sys/stat.h>
 #include <cstring>
+#include <ctime>
 
 #include <libreborn/util.h>
 #include <libreborn/config.h>
@@ -113,4 +114,24 @@ std::string home_get() {
         IMPOSSIBLE();
     }
     return std::string(home) + std::string(get_home_subdirectory_for_game_data());
-}
\ No newline at end of file
+}
+
+// Format Time
+static std::string _format_time(const char *fmt, const time_t raw_time) {
+    const tm *time_info = localtime(&raw_time);
+    if (time_info == nullptr) {
+        ERR("Unable To Determine Current Time: %s", strerror(errno));
+    }
+    char buf[512];
+    strftime(buf, sizeof(buf), fmt, time_info);
+    return std::string(buf);
+}
+std::string format_time(const char *fmt) {
+    time_t raw_time;
+    time(&raw_time);
+    return _format_time(fmt, raw_time);
+}
+std::string format_time(const char *fmt, const int time) {
+    // This Will Break In 2038
+    return _format_time(fmt, time);
+}
diff --git a/media-layer/CMakeLists.txt b/media-layer/CMakeLists.txt
index f23e9cb7..95299bc9 100644
--- a/media-layer/CMakeLists.txt
+++ b/media-layer/CMakeLists.txt
@@ -11,6 +11,6 @@ if(BUILD_MEDIA_LAYER_CORE)
 endif()
 
 # Add Trampoline
-if(MCPI_USE_MEDIA_LAYER_TRAMPOLINE OR BUILD_ARM_COMPONENTS)
+if(MCPI_USE_MEDIA_LAYER_TRAMPOLINE)
     add_subdirectory(trampoline)
 endif()
diff --git a/media-layer/core/CMakeLists.txt b/media-layer/core/CMakeLists.txt
index ef2af879..db368fc3 100644
--- a/media-layer/core/CMakeLists.txt
+++ b/media-layer/core/CMakeLists.txt
@@ -1,7 +1,7 @@
 project(media-layer-core)
 
 # Build
-add_library(media-layer-core-real SHARED
+add_library(media-layer-core SHARED
     src/base.cpp
     src/window/media.cpp
     src/window/cursor.cpp
@@ -14,18 +14,12 @@ add_library(media-layer-core-real SHARED
     src/audio/file.cpp
 )
 
-# Set Name
-set_target_properties(media-layer-core-real PROPERTIES OUTPUT_NAME "media-layer-core")
-if(BUILD_NATIVE_COMPONENTS)
-    add_library(media-layer-core ALIAS media-layer-core-real)
-endif()
-
 # Install
-setup_library(media-layer-core-real TRUE FALSE)
+setup_library(media-layer-core TRUE TRUE)
 
 # Link
 find_library(OPENAL_LIBRARY NAMES openal REQUIRED)
-target_link_libraries(media-layer-core-real
+target_link_libraries(media-layer-core
     PUBLIC
         media-layer-headers
         reborn-util
diff --git a/media-layer/trampoline/CMakeLists.txt b/media-layer/trampoline/CMakeLists.txt
index 8214b643..d15ef9cd 100644
--- a/media-layer/trampoline/CMakeLists.txt
+++ b/media-layer/trampoline/CMakeLists.txt
@@ -7,7 +7,11 @@ set(MEDIA_LAYER_TRAMPOLINE_SRC src/media-layer-core.cpp src/GLESv1_CM.cpp)
 if(BUILD_NATIVE_COMPONENTS)
     # Host Component
     add_library(media-layer-trampoline src/host/host.cpp ${MEDIA_LAYER_TRAMPOLINE_SRC})
-    target_link_libraries(media-layer-trampoline reborn-util media-layer-core trampoline-headers)
+    target_link_libraries(media-layer-trampoline
+        reborn-util
+        media-layer-core
+        trampoline-headers
+    )
     target_compile_definitions(media-layer-trampoline PRIVATE MEDIA_LAYER_TRAMPOLINE_HOST)
     # Install
     setup_library(media-layer-trampoline TRUE TRUE)
@@ -20,9 +24,8 @@ elseif(BUILD_ARM_COMPONENTS)
         PRIVATE
             reborn-util
             trampoline-headers
-            rt
     )
     target_compile_definitions(media-layer-core PRIVATE MEDIA_LAYER_TRAMPOLINE_GUEST)
     # Install
-    setup_library(media-layer-core "${MCPI_USE_MEDIA_LAYER_TRAMPOLINE}" TRUE)
+    setup_library(media-layer-core TRUE TRUE)
 endif()
diff --git a/mods/src/misc/misc.cpp b/mods/src/misc/misc.cpp
index 83cd8aea..76601953 100644
--- a/mods/src/misc/misc.cpp
+++ b/mods/src/misc/misc.cpp
@@ -46,7 +46,7 @@ static void LoginPacket_read_injection(LoginPacket_read_t original, LoginPacket
 
 // Fix RakNet::RakString Security Bug
 //
-// RakNet::RakString's format constructor is often given unsanitized user input and is never used for formatting,
+// RakNet::RakString's format constructor is often given un-sanitized user input and is never used for formatting,
 // this is a massive security risk, allowing clients to run arbitrary format specifiers, this disables the
 // formatting functionality.
 RakNet_RakString_constructor_2_t RakNet_RakString_constructor_2 = (RakNet_RakString_constructor_2_t) 0xea5cc;
@@ -259,12 +259,7 @@ static ItemInstance *Item_getCraftingRemainingItem_injection(__attribute__((unus
 // Display Date In Select World Screen
 static std::string AppPlatform_linux_getDateString_injection(__attribute__((unused)) AppPlatform_linux *app_platform, const int time) {
     // From https://github.com/ReMinecraftPE/mcpe/blob/56e51027b1c2e67fe5a0e8a091cefe51d4d11926/platforms/sdl/base/AppPlatform_sdl_base.cpp#L68-L84
-    const time_t tt = time;
-    tm t = {};
-    gmtime_r(&tt, &t);
-    char buf[2048];
-    strftime(buf, sizeof buf, "%b %d %Y %H:%M:%S", &t);
-    return std::string(buf);
+    return format_time("%b %d %Y %H:%M:%S", time);
 }
 
 // Missing Strings
diff --git a/mods/src/screenshot/screenshot.cpp b/mods/src/screenshot/screenshot.cpp
index be27959b..a0f61e51 100644
--- a/mods/src/screenshot/screenshot.cpp
+++ b/mods/src/screenshot/screenshot.cpp
@@ -1,8 +1,6 @@
 #include <cstdlib>
 #include <cstdio>
 #include <unistd.h>
-#include <ctime>
-#include <sys/stat.h>
 
 #include "stb_image.h"
 #include "stb_image_write.h"
@@ -45,17 +43,13 @@ void screenshot_take(Gui *gui) {
     }
 
     // Get Timestamp
-    time_t raw_time;
-    time(&raw_time);
-    const tm *time_info = localtime(&raw_time);
-    char time[512];
-    strftime(time, 512, "%Y-%m-%d_%H.%M.%S", time_info);
+    std::string time = format_time("%Y-%m-%d_%H.%M.%S");
 
     // Prevent Overwriting Screenshots
     int num = 0;
     std::string filename;
     do {
-        filename = std::string(time);
+        filename = time;
         if (num > 0) {
             filename += '-' + std::to_string(num);
         }