From 454734ab68c8d3143ca8c100666e3d0109d3cecf Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Thu, 21 Nov 2024 22:07:50 -0500 Subject: [PATCH] Improve feature_has --- launcher/CMakeLists.txt | 4 --- launcher/src/client/cache.cpp | 1 - launcher/src/client/configuration.h | 3 +- libreborn/CMakeLists.txt | 4 +++ .../include/libreborn}/flags.h | 2 +- libreborn/include/libreborn/log.h | 14 +++++--- .../src/util}/flags/available-feature-flags | 0 .../src/util}/flags/flags.cpp | 10 +++--- .../src/util}/flags/node.cpp | 7 ++-- mods/src/feature/feature.cpp | 35 ++++++++++++------- 10 files changed, 46 insertions(+), 34 deletions(-) rename {launcher/src/client/flags => libreborn/include/libreborn}/flags.h (97%) rename {launcher/src/client => libreborn/src/util}/flags/available-feature-flags (100%) rename {launcher/src/client => libreborn/src/util}/flags/flags.cpp (94%) rename {launcher/src/client => libreborn/src/util}/flags/node.cpp (94%) diff --git a/launcher/CMakeLists.txt b/launcher/CMakeLists.txt index cad5dac2ab..181f8d5322 100644 --- a/launcher/CMakeLists.txt +++ b/launcher/CMakeLists.txt @@ -19,11 +19,7 @@ add_executable(launcher src/client/configuration.cpp src/client/cache.cpp src/client/ui.cpp - src/client/flags/node.cpp - src/client/flags/flags.cpp - src/client/flags/available-feature-flags # Show In IDE ) -embed_resource(launcher src/client/flags/available-feature-flags) target_link_libraries(launcher reborn-util LIB_LIEF diff --git a/launcher/src/client/cache.cpp b/launcher/src/client/cache.cpp index e52bde5e99..579cf92710 100644 --- a/launcher/src/client/cache.cpp +++ b/launcher/src/client/cache.cpp @@ -10,7 +10,6 @@ #include "cache.h" #include "configuration.h" -#include "flags/flags.h" // Get Cache Path static std::string get_cache_path() { diff --git a/launcher/src/client/configuration.h b/launcher/src/client/configuration.h index 0eff011b6a..10a8125fea 100644 --- a/launcher/src/client/configuration.h +++ b/launcher/src/client/configuration.h @@ -4,9 +4,10 @@ #include "../options/parser.h" #include "cache.h" -#include "flags/flags.h" #include "../ui/frame.h" +#include + // Default Configuration #define DEFAULT_USERNAME "StevePi" #define DEFAULT_RENDER_DISTANCE "Short" diff --git a/libreborn/CMakeLists.txt b/libreborn/CMakeLists.txt index 43b93e0d95..9ffbc1aafe 100644 --- a/libreborn/CMakeLists.txt +++ b/libreborn/CMakeLists.txt @@ -12,7 +12,11 @@ add_library(reborn-util SHARED src/util/log.cpp src/util/cp437.cpp src/util/env.cpp + src/util/flags/node.cpp + src/util/flags/flags.cpp + src/util/flags/available-feature-flags # Show In IDE ) +embed_resource(reborn-util src/util/flags/available-feature-flags) target_link_libraries(reborn-util PRIVATE utf8cpp) if(TARGET glfw) target_sources(reborn-util PRIVATE src/util/glfw.cpp) diff --git a/launcher/src/client/flags/flags.h b/libreborn/include/libreborn/flags.h similarity index 97% rename from launcher/src/client/flags/flags.h rename to libreborn/include/libreborn/flags.h index f9e5b7042e..974811fa82 100644 --- a/launcher/src/client/flags/flags.h +++ b/libreborn/include/libreborn/flags.h @@ -6,7 +6,7 @@ #include // Seperator -#define SEPERATOR_CHAR '|' +#define FLAG_SEPERATOR_CHAR '|' // Flag struct FlagNode { diff --git a/libreborn/include/libreborn/log.h b/libreborn/include/libreborn/log.h index b833bb73f4..8fa97a124f 100644 --- a/libreborn/include/libreborn/log.h +++ b/libreborn/include/libreborn/log.h @@ -2,6 +2,7 @@ #include #include +#include // Log File int reborn_get_log_fd(); @@ -13,15 +14,18 @@ int reborn_get_debug_fd(); // 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, ...) 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 DEBUG(format, ...) dprintf(reborn_get_debug_fd(), "[DEBUG]: %s" format "\n", reborn_debug_tag, ##__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") #define CONDITIONAL_ERR(is_error, ...) \ - { \ + ({ \ if ((is_error)) { \ ERR(__VA_ARGS__); \ } else { \ WARN(__VA_ARGS__); \ } \ - } + }) diff --git a/launcher/src/client/flags/available-feature-flags b/libreborn/src/util/flags/available-feature-flags similarity index 100% rename from launcher/src/client/flags/available-feature-flags rename to libreborn/src/util/flags/available-feature-flags diff --git a/launcher/src/client/flags/flags.cpp b/libreborn/src/util/flags/flags.cpp similarity index 94% rename from launcher/src/client/flags/flags.cpp rename to libreborn/src/util/flags/flags.cpp index fd7b7de860..99971883d8 100644 --- a/launcher/src/client/flags/flags.cpp +++ b/libreborn/src/util/flags/flags.cpp @@ -2,9 +2,9 @@ #include #include -#include - -#include "flags.h" +#include +#include +#include // All Flags static unsigned int find_indent_level(std::string &str) { @@ -67,7 +67,7 @@ Flags::operator std::string() const { root.for_each_const([&out](const FlagNode &flag) { if (flag.value) { if (!out.empty()) { - out += SEPERATOR_CHAR; + out += FLAG_SEPERATOR_CHAR; } out += flag.name; } @@ -79,7 +79,7 @@ Flags &Flags::operator=(const std::string &str) { std::unordered_set to_enable; std::stringstream stream(str); std::string flag_name; - while (std::getline(stream, flag_name, SEPERATOR_CHAR)) { + while (std::getline(stream, flag_name, FLAG_SEPERATOR_CHAR)) { if (!flag_name.empty()) { to_enable.insert(flag_name); } diff --git a/launcher/src/client/flags/node.cpp b/libreborn/src/util/flags/node.cpp similarity index 94% rename from launcher/src/client/flags/node.cpp rename to libreborn/src/util/flags/node.cpp index bfa231e636..1ed4b4d7ca 100644 --- a/launcher/src/client/flags/node.cpp +++ b/libreborn/src/util/flags/node.cpp @@ -1,8 +1,7 @@ #include -#include - -#include "flags.h" +#include +#include // Flag static int next_id; @@ -75,7 +74,7 @@ void FlagNode::add_flag(std::string line) { if (!value_set) { ERR("Invalid Feature Flag Line: %s", line.c_str()); } - if (line.rfind(SEPERATOR_CHAR, 0) != std::string::npos) { + if (line.rfind(FLAG_SEPERATOR_CHAR, 0) != std::string::npos) { ERR("Feature Flag Contains Invalid Character"); } // Create diff --git a/mods/src/feature/feature.cpp b/mods/src/feature/feature.cpp index 056e7fccd7..dc20a9b000 100644 --- a/mods/src/feature/feature.cpp +++ b/mods/src/feature/feature.cpp @@ -1,7 +1,10 @@ -#include -#include +#include +#include -#include +#include +#include +#include +#include #include @@ -12,18 +15,24 @@ bool _feature_has(const char *name, const int server_default) { return server_default > 0; } // Get Value - char *env = getenv(MCPI_FEATURE_FLAGS_ENV); - char *features = strdup(env != nullptr ? env : ""); - char *tok = strtok(features, "|"); - bool ret = false; - while (tok != nullptr) { - if (strcmp(tok, name) == 0) { - ret = true; - break; + static Flags flags = Flags::get(); + static bool loaded = false; + if (!loaded) { + const char *env = getenv(MCPI_FEATURE_FLAGS_ENV); + if (env) { + flags = env; } - tok = strtok(nullptr, "|"); + loaded = true; + } + int ret = -1; + flags.root.for_each_const([&ret, &name](const FlagNode &flag) { + if (flag.name == std::string(name)) { + ret = flag.value; + } + }); + if (ret == -1) { + ERR("Invalid Feature Flag: %s", name); } - free(features); // Log DEBUG("Feature: %s: %s", name, ret ? "Enabled" : "Disabled");