Improve feature_has
This commit is contained in:
parent
d3b70878be
commit
454734ab68
@ -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
|
||||
|
@ -10,7 +10,6 @@
|
||||
|
||||
#include "cache.h"
|
||||
#include "configuration.h"
|
||||
#include "flags/flags.h"
|
||||
|
||||
// Get Cache Path
|
||||
static std::string get_cache_path() {
|
||||
|
@ -4,9 +4,10 @@
|
||||
|
||||
#include "../options/parser.h"
|
||||
#include "cache.h"
|
||||
#include "flags/flags.h"
|
||||
#include "../ui/frame.h"
|
||||
|
||||
#include <libreborn/flags.h>
|
||||
|
||||
// Default Configuration
|
||||
#define DEFAULT_USERNAME "StevePi"
|
||||
#define DEFAULT_RENDER_DISTANCE "Short"
|
||||
|
@ -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)
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <functional>
|
||||
|
||||
// Seperator
|
||||
#define SEPERATOR_CHAR '|'
|
||||
#define FLAG_SEPERATOR_CHAR '|'
|
||||
|
||||
// Flag
|
||||
struct FlagNode {
|
@ -2,6 +2,7 @@
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <unistd.h>
|
||||
|
||||
// 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__); \
|
||||
} \
|
||||
}
|
||||
})
|
||||
|
@ -2,9 +2,9 @@
|
||||
#include <algorithm>
|
||||
#include <unordered_set>
|
||||
|
||||
#include <libreborn/libreborn.h>
|
||||
|
||||
#include "flags.h"
|
||||
#include <libreborn/log.h>
|
||||
#include <libreborn/flags.h>
|
||||
#include <libreborn/util.h>
|
||||
|
||||
// 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<std::string> 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);
|
||||
}
|
@ -1,8 +1,7 @@
|
||||
#include <algorithm>
|
||||
|
||||
#include <libreborn/libreborn.h>
|
||||
|
||||
#include "flags.h"
|
||||
#include <libreborn/log.h>
|
||||
#include <libreborn/flags.h>
|
||||
|
||||
// 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
|
@ -1,7 +1,10 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <cstdlib>
|
||||
#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>
|
||||
|
||||
@ -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");
|
||||
|
Loading…
Reference in New Issue
Block a user