Improve feature_has

This commit is contained in:
TheBrokenRail 2024-11-21 22:07:50 -05:00
parent d3b70878be
commit 454734ab68
10 changed files with 46 additions and 34 deletions

View File

@ -19,11 +19,7 @@ add_executable(launcher
src/client/configuration.cpp src/client/configuration.cpp
src/client/cache.cpp src/client/cache.cpp
src/client/ui.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 target_link_libraries(launcher
reborn-util reborn-util
LIB_LIEF LIB_LIEF

View File

@ -10,7 +10,6 @@
#include "cache.h" #include "cache.h"
#include "configuration.h" #include "configuration.h"
#include "flags/flags.h"
// Get Cache Path // Get Cache Path
static std::string get_cache_path() { static std::string get_cache_path() {

View File

@ -4,9 +4,10 @@
#include "../options/parser.h" #include "../options/parser.h"
#include "cache.h" #include "cache.h"
#include "flags/flags.h"
#include "../ui/frame.h" #include "../ui/frame.h"
#include <libreborn/flags.h>
// Default Configuration // Default Configuration
#define DEFAULT_USERNAME "StevePi" #define DEFAULT_USERNAME "StevePi"
#define DEFAULT_RENDER_DISTANCE "Short" #define DEFAULT_RENDER_DISTANCE "Short"

View File

@ -12,7 +12,11 @@ add_library(reborn-util SHARED
src/util/log.cpp src/util/log.cpp
src/util/cp437.cpp src/util/cp437.cpp
src/util/env.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) target_link_libraries(reborn-util PRIVATE utf8cpp)
if(TARGET glfw) if(TARGET glfw)
target_sources(reborn-util PRIVATE src/util/glfw.cpp) target_sources(reborn-util PRIVATE src/util/glfw.cpp)

View File

@ -6,7 +6,7 @@
#include <functional> #include <functional>
// Seperator // Seperator
#define SEPERATOR_CHAR '|' #define FLAG_SEPERATOR_CHAR '|'
// Flag // Flag
struct FlagNode { struct FlagNode {

View File

@ -2,6 +2,7 @@
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <unistd.h>
// Log File // Log File
int reborn_get_log_fd(); int reborn_get_log_fd();
@ -13,15 +14,18 @@ int reborn_get_debug_fd();
// 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, ...) dprintf(reborn_get_debug_fd(), "[DEBUG]: %s" format "\n", tag, ##__VA_ARGS__) #define DEBUG(format, ...) dprintf(reborn_get_debug_fd(), "[DEBUG]: %s" format "\n", reborn_debug_tag, ##__VA_ARGS__)
#define DEBUG(format, ...) RAW_DEBUG(reborn_debug_tag, format, ##__VA_ARGS__) #define ERR(format, ...) \
#define ERR(format, ...) { fprintf(stderr, "[ERR]: (%s:%i): " format "\n", __FILE__, __LINE__, ##__VA_ARGS__); _exit(EXIT_FAILURE); } ({ \
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")
#define CONDITIONAL_ERR(is_error, ...) \ #define CONDITIONAL_ERR(is_error, ...) \
{ \ ({ \
if ((is_error)) { \ if ((is_error)) { \
ERR(__VA_ARGS__); \ ERR(__VA_ARGS__); \
} else { \ } else { \
WARN(__VA_ARGS__); \ WARN(__VA_ARGS__); \
} \ } \
} })

View File

@ -2,9 +2,9 @@
#include <algorithm> #include <algorithm>
#include <unordered_set> #include <unordered_set>
#include <libreborn/libreborn.h> #include <libreborn/log.h>
#include <libreborn/flags.h>
#include "flags.h" #include <libreborn/util.h>
// All Flags // All Flags
static unsigned int find_indent_level(std::string &str) { 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) { root.for_each_const([&out](const FlagNode &flag) {
if (flag.value) { if (flag.value) {
if (!out.empty()) { if (!out.empty()) {
out += SEPERATOR_CHAR; out += FLAG_SEPERATOR_CHAR;
} }
out += flag.name; out += flag.name;
} }
@ -79,7 +79,7 @@ Flags &Flags::operator=(const std::string &str) {
std::unordered_set<std::string> to_enable; std::unordered_set<std::string> to_enable;
std::stringstream stream(str); std::stringstream stream(str);
std::string flag_name; 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()) { if (!flag_name.empty()) {
to_enable.insert(flag_name); to_enable.insert(flag_name);
} }

View File

@ -1,8 +1,7 @@
#include <algorithm> #include <algorithm>
#include <libreborn/libreborn.h> #include <libreborn/log.h>
#include <libreborn/flags.h>
#include "flags.h"
// Flag // Flag
static int next_id; static int next_id;
@ -75,7 +74,7 @@ void FlagNode::add_flag(std::string line) {
if (!value_set) { if (!value_set) {
ERR("Invalid Feature Flag Line: %s", line.c_str()); 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"); ERR("Feature Flag Contains Invalid Character");
} }
// Create // Create

View File

@ -1,7 +1,10 @@
#include <stdlib.h> #include <cstdlib>
#include <string.h> #include <cstring>
#include <libreborn/libreborn.h> #include <libreborn/log.h>
#include <libreborn/util.h>
#include <libreborn/env.h>
#include <libreborn/flags.h>
#include <mods/feature/feature.h> #include <mods/feature/feature.h>
@ -12,18 +15,24 @@ bool _feature_has(const char *name, const int server_default) {
return server_default > 0; return server_default > 0;
} }
// Get Value // Get Value
char *env = getenv(MCPI_FEATURE_FLAGS_ENV); static Flags flags = Flags::get();
char *features = strdup(env != nullptr ? env : ""); static bool loaded = false;
char *tok = strtok(features, "|"); if (!loaded) {
bool ret = false; const char *env = getenv(MCPI_FEATURE_FLAGS_ENV);
while (tok != nullptr) { if (env) {
if (strcmp(tok, name) == 0) { flags = env;
ret = true;
break;
} }
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 // Log
DEBUG("Feature: %s: %s", name, ret ? "Enabled" : "Disabled"); DEBUG("Feature: %s: %s", name, ret ? "Enabled" : "Disabled");