From f8836fb2ae819432039977c88292c7728888ecb2 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Sun, 21 Feb 2021 14:53:17 -0500 Subject: [PATCH] Remove SDL Dependency --- Dockerfile.client | 6 ++-- build/build-libpng12.sh | 6 ++-- launcher/CMakeLists.txt | 25 ++++++++++++++- launcher/src/launcher.c | 40 ++++++++++++------------ {mods => launcher}/src/stubs/EGL.c | 7 +---- {mods => launcher}/src/stubs/GLESv2.c | 0 launcher/src/stubs/SDL.cpp | 41 +++++++++++++++++++++++++ {mods => launcher}/src/stubs/bcm_host.c | 0 mods/CMakeLists.txt | 21 ++----------- mods/src/compat/compat.c | 6 ++-- mods/src/compat/compat.h | 3 -- 11 files changed, 97 insertions(+), 58 deletions(-) rename {mods => launcher}/src/stubs/EGL.c (93%) rename {mods => launcher}/src/stubs/GLESv2.c (100%) create mode 100644 launcher/src/stubs/SDL.cpp rename {mods => launcher}/src/stubs/bcm_host.c (100%) delete mode 100644 mods/src/compat/compat.h diff --git a/Dockerfile.client b/Dockerfile.client index fd80ed2a2..9b23080cc 100644 --- a/Dockerfile.client +++ b/Dockerfile.client @@ -5,7 +5,7 @@ ENV DEBIAN_FRONTEND noninteractive RUN \ # Install Runtime Dependencies apt-get update && \ - apt-get install -y --no-install-recommends tini libgles1 libx11-6 libsdl1.2debian zlib1g libfreeimage3 libglfw3 xinput libxfixes3 gosu tk && \ + apt-get install -y --no-install-recommends tini libgles1 libx11-6 zlib1g libfreeimage3 libglfw3 xinput libxfixes3 gosu tk && \ rm -rf /var/lib/apt/lists/* # Compile Environment @@ -43,9 +43,7 @@ RUN \ mkdir -p /home && \ chmod -R a+rw /home -# Include Exported Libraries -COPY --from=build /app/export / -# Include Mods +# Copy Build COPY --from=build /app/minecraft-pi /app/minecraft-pi WORKDIR /app/minecraft-pi diff --git a/build/build-libpng12.sh b/build/build-libpng12.sh index 0e620a6f1..c885a9e1d 100755 --- a/build/build-libpng12.sh +++ b/build/build-libpng12.sh @@ -6,10 +6,12 @@ git clone --depth 1 https://git.code.sf.net/p/libpng/code libpng -b libpng12 cd libpng -./configure --prefix /usr +./configure make -j$(nproc) -make install DESTDIR=/app/export + +mkdir -p ../minecraft-pi/lib +cp -L .libs/libpng12.so.0 ../minecraft-pi/lib cd ../ diff --git a/launcher/CMakeLists.txt b/launcher/CMakeLists.txt index bc47c53f1..3f0dd298c 100644 --- a/launcher/CMakeLists.txt +++ b/launcher/CMakeLists.txt @@ -4,8 +4,31 @@ project(launcher) add_compile_options(-Wall -Wextra -Werror) +## Launcher + add_executable(launcher src/launcher.c) # Install install(TARGETS launcher DESTINATION /) -install(PROGRAMS src/run.sh DESTINATION /) \ No newline at end of file +install(PROGRAMS src/run.sh DESTINATION /) + +## Stubs + +# Stub RPI-Specific Graphics +add_library(bcm_host SHARED src/stubs/bcm_host.c) + +# Stub EGL +add_library(EGL SHARED src/stubs/EGL.c) + +# Stub SDL +add_library(SDL-1.2 SHARED src/stubs/SDL.cpp) +target_link_libraries(SDL-1.2 pthread) +set_target_properties(SDL-1.2 PROPERTIES SOVERSION "0") + +# MCPI Links Against GLESv2 But Uses GLESv1_CM +add_library(GLESv2 SHARED src/stubs/GLESv2.c) +target_link_libraries(GLESv2 GLESv1_CM) +target_link_options(GLESv2 PRIVATE "-Wl,--no-as-needed") + +# Install Stubs +install(TARGETS bcm_host EGL SDL-1.2 GLESv2 DESTINATION /lib) \ No newline at end of file diff --git a/launcher/src/launcher.c b/launcher/src/launcher.c index 9fec5570c..297eeacc6 100644 --- a/launcher/src/launcher.c +++ b/launcher/src/launcher.c @@ -9,10 +9,10 @@ #include #include +// Check String Prefix/Suffix static int starts_with(const char *s, const char *t) { return strncmp(s, t, strlen(t)) == 0; } - static int ends_with(const char *s, const char *t) { size_t slen = strlen(s); size_t tlen = strlen(t); @@ -20,6 +20,7 @@ static int ends_with(const char *s, const char *t) { return strcmp(s + slen - tlen, t) == 0; } +// Set Environmental Variable static void trim(char *value) { // Remove Trailing Colon int length = strlen(value); @@ -27,26 +28,30 @@ static void trim(char *value) { value[length - 1] = '\0'; } } - static void set_and_print_env(char *name, char *value) { - // Set Variable With Not Trailing Colon + // Set Variable With No Trailing Colon trim(value); fprintf(stderr, "Set %s = %s\n", name, value); setenv(name, value, 1); } +// Get Environmental Variable static char *get_env_safe(const char *name) { // Get Variable Or Blank String If Not Set char *ret = getenv(name); return ret != NULL ? ret : ""; } -static void load(char **ld_path, char **ld_preload, char *folder) { +// Get All SO Files In Folder +static void load(char **ld_preload, char *folder) { int folder_name_length = strlen(folder); + // Retry Until Successful while (1) { + // Open Folder DIR *dp = opendir(folder); if (dp != NULL) { + // Loop Through Folder struct dirent *entry = NULL; errno = 0; while (1) { @@ -75,14 +80,14 @@ static void load(char **ld_path, char **ld_preload, char *folder) { fprintf(stderr, "Error Reading Directory: %s: %s\n", folder, strerror(errno)); exit(EXIT_FAILURE); } else { + // Done! break; } } + // Close Folder closedir(dp); - // Add To LD_LIBRARY_PATH - asprintf(ld_path, "%s:%s", *ld_path, folder); - + // Exit Function return; } else if (errno == ENOENT) { // Folder Doesn't Exists, Attempt Creation @@ -92,6 +97,7 @@ static void load(char **ld_path, char **ld_preload, char *folder) { fprintf(stderr, "Error Creating Directory: %s: %s\n", folder, strerror(errno)); exit(EXIT_FAILURE); } + // Continue Retrying } else { // Unable To Open Folder fprintf(stderr, "Error Opening Directory: %s: %s\n", folder, strerror(errno)); @@ -121,31 +127,25 @@ int main(__attribute__((unused)) int argc, char *argv[]) { } free(minecraft_folder); - char *ld_path = NULL; - - // Start Configuring LD_LIBRARY_PATH - asprintf(&ld_path, "/usr/arm-linux-gnueabihf/lib"); + // Configure LD_LIBRARY_PATH + char *ld_path; + asprintf(&ld_path, "./lib:/usr/arm-linux-gnueabihf/lib:%s", get_env_safe("LD_LIBRARY_PATH")); + set_and_print_env("LD_LIBRARY_PATH", ld_path); + free(ld_path); // Start Configuring LD_PRELOAD char *ld_preload = NULL; asprintf(&ld_preload, "%s", get_env_safe("LD_PRELOAD")); // Load Mods From ./mods - load(&ld_path, &ld_preload, "./mods/"); + load(&ld_preload, "./mods/"); // Loads Mods From ~/.minecraft-pi/mods char *home_mods = NULL; asprintf(&home_mods, "%s/.minecraft-pi/mods/", getenv("HOME")); - load(&ld_path, &ld_preload, home_mods); + load(&ld_preload, home_mods); free(home_mods); - // Add Existing LD_LIBRARY_PATH - asprintf(&ld_path, "%s:%s", ld_path, get_env_safe("LD_LIBRARY_PATH")); - - // Set LD_LIBRARY_PATH - set_and_print_env("LD_LIBRARY_PATH", ld_path); - free(ld_path); - // Set LD_PRELOAD set_and_print_env("LD_PRELOAD", ld_preload); free(ld_preload); diff --git a/mods/src/stubs/EGL.c b/launcher/src/stubs/EGL.c similarity index 93% rename from mods/src/stubs/EGL.c rename to launcher/src/stubs/EGL.c index 3b2fba999..a27852dcd 100644 --- a/mods/src/stubs/EGL.c +++ b/launcher/src/stubs/EGL.c @@ -1,8 +1,6 @@ #include -#include "../compat/compat.h" - -// EGL/SDL Is Replaced With GLFW +// EGL Is Replaced With GLFW EGLDisplay eglGetDisplay(__attribute__((unused)) NativeDisplayType native_display) { return 0; @@ -34,9 +32,6 @@ EGLBoolean eglDestroyContext(__attribute__((unused)) EGLDisplay display, __attri EGLBoolean eglTerminate(__attribute__((unused)) EGLDisplay display) { return EGL_TRUE; } - -// Send Buffer Swap Request To GLFW EGLBoolean eglSwapBuffers(__attribute__((unused)) EGLDisplay display, __attribute__((unused)) EGLSurface surface) { - compat_eglSwapBuffers(); return EGL_TRUE; } \ No newline at end of file diff --git a/mods/src/stubs/GLESv2.c b/launcher/src/stubs/GLESv2.c similarity index 100% rename from mods/src/stubs/GLESv2.c rename to launcher/src/stubs/GLESv2.c diff --git a/launcher/src/stubs/SDL.cpp b/launcher/src/stubs/SDL.cpp new file mode 100644 index 000000000..5546758ba --- /dev/null +++ b/launcher/src/stubs/SDL.cpp @@ -0,0 +1,41 @@ +#include +#include +#include + +#include + +// SDL Is Replaced With GLFW + +int SDL_Init(__attribute__((unused)) uint32_t flags) { + return 0; +} + +void SDL_Quit() { + exit(0); +} + +// Event Queue + +static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER; +static std::vector queue; + +int SDL_PollEvent(SDL_Event *event) { + pthread_mutex_lock(&queue_mutex); + int ret; + if (queue.size() > 0) { + *event = queue[0]; + queue.erase(queue.begin()); + ret = 1; + } else { + ret = 0; + } + pthread_mutex_unlock(&queue_mutex); + return ret; +} + +int SDL_PushEvent(SDL_Event *event) { + pthread_mutex_lock(&queue_mutex); + queue.push_back(*event); + pthread_mutex_unlock(&queue_mutex); + return 1; +} diff --git a/mods/src/stubs/bcm_host.c b/launcher/src/stubs/bcm_host.c similarity index 100% rename from mods/src/stubs/bcm_host.c rename to launcher/src/stubs/bcm_host.c diff --git a/mods/CMakeLists.txt b/mods/CMakeLists.txt index a284c08e5..97ac21910 100644 --- a/mods/CMakeLists.txt +++ b/mods/CMakeLists.txt @@ -12,9 +12,6 @@ add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0) # Add libreborn add_subdirectory(../libreborn libreborn) -# Include Libraries Exported To Runtime Environment -include_directories(/app/export/include) - # Find GLFW find_package(glfw3 3.3 REQUIRED) @@ -64,19 +61,5 @@ target_link_libraries(test reborn) add_library(init SHARED src/init/init.c) target_link_libraries(init compat server game_mode camera input misc options textures chat test) -## Stubs - -# Stub RPI-Specific Graphics -add_library(bcm_host SHARED src/stubs/bcm_host.c) - -# Stub EGL -add_library(EGL SHARED src/stubs/EGL.c) -target_link_libraries(EGL compat) - -# MCPI Links Against GLESv2 But Uses GLESv1_CM -add_library(GLESv2 SHARED src/stubs/GLESv2.c) -target_link_libraries(GLESv2 GLESv1_CM) -target_link_options(GLESv2 PRIVATE "-Wl,--no-as-needed") - -## Install -install(TARGETS init compat readdir feature screenshot override server game_mode camera input misc options textures chat test bcm_host EGL GLESv2 DESTINATION /mods) \ No newline at end of file +## Install Mods +install(TARGETS init compat readdir feature screenshot override server game_mode camera input misc options textures chat test DESTINATION /mods) \ No newline at end of file diff --git a/mods/src/compat/compat.c b/mods/src/compat/compat.c index 33110b4d7..d47d0240c 100644 --- a/mods/src/compat/compat.c +++ b/mods/src/compat/compat.c @@ -13,6 +13,7 @@ #include #include #include +#include #include @@ -22,8 +23,6 @@ #include "../chat/chat.h" #include "../init/init.h" -#include "compat.h" - static GLFWwindow *glfw_window; static int is_server = 0; @@ -212,11 +211,12 @@ HOOK(SDL_WM_SetCaption, void, (const char *title, __attribute__((unused)) const } } -void compat_eglSwapBuffers() { +HOOK(eglSwapBuffers, EGLBoolean, (__attribute__((unused)) EGLDisplay display, __attribute__((unused)) EGLSurface surface)) { if (!is_server) { // Don't Swap Buffers In A Context-Less Window glfwSwapBuffers(glfw_window); } + return EGL_TRUE; } static int is_fullscreen = 0; diff --git a/mods/src/compat/compat.h b/mods/src/compat/compat.h deleted file mode 100644 index 8311d8bb4..000000000 --- a/mods/src/compat/compat.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -void compat_eglSwapBuffers(); \ No newline at end of file