Store Server List In Launcher Cache

This commit is contained in:
TheBrokenRail 2024-12-14 03:08:19 -05:00
parent c6048ec4fb
commit 587ba38ffe
64 changed files with 312 additions and 339 deletions

View File

@ -1,4 +1,4 @@
#include <libreborn/env.h>
#include <libreborn/env/env.h>
#include "bootstrap.h"
#include "../util/util.h"

View File

@ -2,9 +2,9 @@
#include <vector>
#include <libreborn/log.h>
#include <libreborn/env.h>
#include <libreborn/env/env.h>
#include <libreborn/config.h>
#include <libreborn/exec.h>
#include <libreborn/util/exec.h>
#include "../util/util.h"
#include "bootstrap.h"

View File

@ -1,5 +1,5 @@
#include <libreborn/log.h>
#include <libreborn/exec.h>
#include <libreborn/util/exec.h>
#include <libreborn/config.h>
#include "bootstrap.h"

View File

@ -5,7 +5,7 @@
#include <cstring>
#include <libreborn/log.h>
#include <libreborn/util.h>
#include <libreborn/util/util.h>
#include "bootstrap.h"
#include "../util/util.h"

View File

@ -4,7 +4,7 @@
#include <LIEF/ELF.hpp>
#include <libreborn/util.h>
#include <libreborn/util/util.h>
#include <libreborn/config.h>
#include "bootstrap.h"

View File

@ -6,7 +6,8 @@
#include <unistd.h>
#include <libreborn/log.h>
#include <libreborn/util.h>
#include <libreborn/util/util.h>
#include <libreborn/util/io.h>
#include "cache.h"
#include "configuration.h"
@ -17,10 +18,23 @@ static std::string get_cache_path() {
}
// Load
template <typename T>
static T simple_read(std::ifstream &stream) {
T out;
stream.read((char *) &out, sizeof(T));
return out;
}
template <>
std::string simple_read<std::string>(std::ifstream &stream) {
std::string out;
if (!std::getline(stream, out, '\0')) {
out = "";
}
return out;
}
static void read_cache(std::ifstream &stream, State &ret) {
// Cache Version
unsigned char cache_version;
stream.read((char *) &cache_version, 1);
const unsigned char cache_version = simple_read<unsigned char>(stream);
if (stream.eof()) {
// Unable To Read Version
WARN("Unable To Read Launcher Cache Version");
@ -28,10 +42,10 @@ static void read_cache(std::ifstream &stream, State &ret) {
}
// Support Older Versions
bool load_gui_scale = true;
bool load_new_fields = true;
if (cache_version == 0) {
// Pre-v3.0.0 Cache
load_gui_scale = false;
load_new_fields = false;
} else if (cache_version != (unsigned char) CACHE_VERSION) {
// Invalid Version
WARN("Invalid Launcher Cache Version (Expected: %i, Actual: %i)", CACHE_VERSION, (int) cache_version);
@ -40,21 +54,18 @@ static void read_cache(std::ifstream &stream, State &ret) {
// Load Username And Render Distance
State state;
std::getline(stream, state.username, '\0');
std::getline(stream, state.render_distance, '\0');
if (load_gui_scale) {
stream.read((char *) &state.gui_scale, sizeof(float));
state.username = simple_read<std::string>(stream);
state.render_distance = simple_read<std::string>(stream);
if (load_new_fields) {
state.gui_scale = simple_read<float>(stream);
state.servers.load(simple_read<std::string>(stream));
}
// Load Feature Flags
std::unordered_map<std::string, bool> flags;
std::string flag;
while (!stream.eof() && std::getline(stream, flag, '\0')) {
if (!flag.empty()) {
bool is_enabled = false;
stream.read((char *) &is_enabled, sizeof(bool));
flags[flag] = is_enabled;
}
while (!stream.eof()) {
std::string flag = simple_read<std::string>(stream);
flags[flag] = simple_read<bool>(stream);
stream.peek();
}
state.flags.from_cache(flags);
@ -101,24 +112,30 @@ State load_cache() {
}
// Save
static void write_env_to_stream(std::ostream &stream, const std::string &value) {
stream.write(value.c_str(), int(value.size()) + 1);
template <typename T>
static void simple_write(std::ostream &stream, const T &val) {
stream.write((const char *) &val, sizeof(T));
}
template <>
void simple_write<std::string>(std::ostream &stream, const std::string &val) {
stream.write(val.c_str(), int(val.size()) + 1);
}
void write_cache(std::ostream &stream, const State &state) {
// Save Cache Version
constexpr unsigned char cache_version = CACHE_VERSION;
stream.write((const char *) &cache_version, 1);
simple_write(stream, cache_version);
// Save Username And Render Distance
write_env_to_stream(stream, state.username);
write_env_to_stream(stream, state.render_distance);
stream.write((const char *) &state.gui_scale, sizeof(float));
simple_write(stream, state.username);
simple_write(stream, state.render_distance);
simple_write(stream, state.gui_scale);
simple_write(stream, state.servers.to_string());
// Save Feature Flags
const std::unordered_map<std::string, bool> flags_cache = state.flags.to_cache();
for (const std::pair<const std::string, bool> &it : flags_cache) {
stream.write(it.first.c_str(), int(it.first.size()) + 1);
stream.write((const char *) &it.second, sizeof(bool));
simple_write(stream, it.first);
simple_write(stream, it.second);
}
}
void save_cache(const State &state) {

View File

@ -1,6 +1,6 @@
#include <sstream>
#include <libreborn/env.h>
#include <libreborn/env/env.h>
#include "configuration.h"
#include "cache.h"
@ -28,6 +28,7 @@ void State::update(const bool save) {
update_from_env(MCPI_USERNAME_ENV, username, save);
update_from_env(MCPI_RENDER_DISTANCE_ENV, render_distance, save);
update_from_env(MCPI_GUI_SCALE_ENV, gui_scale, save);
update_from_env(MCPI_SERVER_LIST_ENV, servers, save);
}
bool State::operator==(const State &other) const {
std::ostringstream one;

View File

@ -5,8 +5,8 @@
#include "../options/parser.h"
#include "../ui/frame.h"
#include <libreborn/flags.h>
#include <libreborn/servers.h>
#include <libreborn/env/flags.h>
#include <libreborn/env/servers.h>
// Default Configuration
#define DEFAULT_USERNAME "StevePi"
@ -22,6 +22,7 @@ struct State {
// Properties
std::string username;
std::string render_distance;
ServerList servers;
float gui_scale;
Flags flags;
};
@ -32,24 +33,20 @@ struct ConfigurationUI final : Frame {
int render() override;
private:
// Bottom Row
int get_render_distance_index() const;
int draw_bottom(bool hide_reset_revert) const;
[[nodiscard]] int get_render_distance_index() const;
[[nodiscard]] int draw_bottom() const;
// General
void draw_main() const;
// Advanced
void draw_advanced() const;
static void draw_category(FlagNode &category);
// Server List
bool are_servers_unsaved() const;
void draw_servers();
void draw_server_list();
void draw_servers() const;
void draw_server_list() const;
// State
const State original_state;
State &state;
bool &save_settings;
// Server List
ServerList last_saved_servers;
ServerList servers;
};
// Handle Non-Launch Commands

View File

@ -1,8 +1,7 @@
#include <vector>
#include <limits>
#include <cmath>
#include <libreborn/util.h>
#include <libreborn/util/util.h>
#include "configuration.h"
@ -16,13 +15,6 @@ static constexpr std::array render_distances = {
"Tiny"
};
// Tooltips/Text
static const char *revert_text = "Revert";
static const char *revert_tooltip_text = "Last Saved";
static std::string make_tooltip(const std::string &text, const std::string &type) {
return "Use " + text + ' ' + type;
}
// Construct
static constexpr int size = 400;
ConfigurationUI::ConfigurationUI(State &state_, bool &save_settings_):
@ -33,7 +25,6 @@ ConfigurationUI::ConfigurationUI(State &state_, bool &save_settings_):
// Render
int ConfigurationUI::render() {
bool on_servers_tab = false;
if (ImGui::BeginChild("Main", ImVec2(0, -ImGui::GetFrameHeightWithSpacing() /* Leave Room For Bottom Row */), ImGuiChildFlags_None, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse)) {
// Tabs
if (ImGui::BeginTabBar("TabBar")) {
@ -48,28 +39,25 @@ int ConfigurationUI::render() {
ImGui::EndTabItem();
}
// Servers Tab
if (ImGui::BeginTabItem("Servers", nullptr, are_servers_unsaved() ? ImGuiTabItemFlags_UnsavedDocument : ImGuiTabItemFlags_None)) {
if (ImGui::BeginTabItem("Servers")) {
draw_servers();
ImGui::EndTabItem();
on_servers_tab = true;
}
ImGui::EndTabBar();
}
}
ImGui::EndChild();
// Bottom Row
return draw_bottom(on_servers_tab);
return draw_bottom();
}
// Bottom Row
int ConfigurationUI::draw_bottom(const bool hide_reset_revert) const {
int ConfigurationUI::draw_bottom() const {
// Reset Settings
if (!hide_reset_revert) {
const State default_state;
constexpr const char *tooltip_type = "Settings";
std::vector<std::tuple<std::string, std::string, const State *>> reset_options = {
{revert_text, make_tooltip(revert_tooltip_text, tooltip_type), &original_state},
{"Reset", make_tooltip("Default", tooltip_type), &default_state},
{"Revert", "Last Saved", &original_state},
{"Reset", "Default", &default_state},
};
for (const std::tuple<std::string, std::string, const State *> &option : reset_options) {
const State &new_state = *std::get<2>(option);
@ -77,35 +65,23 @@ int ConfigurationUI::draw_bottom(const bool hide_reset_revert) const {
if (ImGui::Button(std::get<0>(option).c_str())) {
state = new_state;
}
ImGui::SetItemTooltip("%s", std::get<1>(option).c_str());
ImGui::SetItemTooltip("Use %s Settings", std::get<1>(option).c_str());
ImGui::EndDisabled();
ImGui::SameLine();
}
}
// Right-Align Buttons
int ret = 0;
bool unsaved_servers = are_servers_unsaved();
draw_right_aligned_buttons({quit_text, "Launch"}, [&ret, unsaved_servers](const int id, const bool was_clicked) {
draw_right_aligned_buttons({quit_text, "Launch"}, [&ret](const int id, const bool was_clicked) {
if (id == 0) {
// Quit
if (was_clicked) {
ret = -1;
}
ImGui::SetItemTooltip("Changes Will Not Be Saved!");
// Disable Launch if Server List Is Unsaved
if (unsaved_servers) {
ImGui::BeginDisabled();
}
} else if (id == 1) {
} else if (was_clicked) {
// Launch
if (unsaved_servers) {
ImGui::SetItemTooltip("Server List Is Unsaved");
ImGui::EndDisabled();
}
if (was_clicked) {
ret = 1;
}
}
});
// Return
return ret;
@ -188,42 +164,23 @@ void ConfigurationUI::draw_category(FlagNode &category) {
}
// Servers
bool ConfigurationUI::are_servers_unsaved() const {
return servers.to_string() != last_saved_servers.to_string();
}
void ConfigurationUI::draw_servers() {
// Add/Clear
void ConfigurationUI::draw_servers() const {
// Add
bool scroll_to_bottom = false;
if (ImGui::Button("Add")) {
servers.entries.emplace_back("", DEFAULT_MULTIPLAYER_PORT);
state.servers.entries.emplace_back("", DEFAULT_MULTIPLAYER_PORT);
scroll_to_bottom = true;
}
ImGui::SameLine();
ImGui::BeginDisabled(servers.entries.empty());
if (ImGui::Button("Clear")) {
servers.entries.clear();
}
ImGui::EndDisabled();
ImGui::SameLine();
// Revert/Save
int clicked_button = -1;
ImGui::BeginDisabled(!are_servers_unsaved());
draw_right_aligned_buttons({revert_text, "Save"}, [&clicked_button](const int id, const bool was_clicked) {
if (id == 0) {
ImGui::SetItemTooltip("%s", make_tooltip(revert_tooltip_text, "Server List").c_str());
}
if (was_clicked) {
clicked_button = id;
}
// Clear
bool should_clear = false;
ImGui::BeginDisabled(state.servers.entries.empty());
draw_right_aligned_buttons({"Clear"}, [&should_clear](__attribute__((unused)) const int id, const bool was_clicked) {
should_clear = was_clicked;
});
ImGui::EndDisabled();
if (clicked_button == 1) {
// Save
servers.save();
last_saved_servers = servers;
} else if (clicked_button == 0) {
// Revert To Last Saved Server List
servers = last_saved_servers;
if (should_clear) {
state.servers.entries.clear();
}
// List
if (ImGui::BeginChild("ServerList", ImVec2(0, 0), ImGuiChildFlags_Borders)) {
@ -234,9 +191,25 @@ void ConfigurationUI::draw_servers() {
}
ImGui::EndChild();
}
void ConfigurationUI::draw_server_list() {
for (std::vector<ServerList::Entry>::size_type i = 0; i < servers.entries.size(); ++i) {
ServerList::Entry &entry = servers.entries[i];
static int server_list_address_filter(ImGuiInputTextCallbackData *data) {
// Lowercase
constexpr std::pair lower_alpha = {'a', 'z'};
constexpr std::pair upper_alpha = {'A', 'Z'};
ImWchar &x = data->EventChar;
if (x >= upper_alpha.first && x <= upper_alpha.second) {
x += lower_alpha.first - upper_alpha.first;
}
// Check Characters
return (x >= lower_alpha.first && x <= lower_alpha.second) || x == '.' ? 0 : 1;
}
static int server_list_port_filter(ImGuiInputTextCallbackData *data) {
// Only Allow Integers
const ImWchar &x = data->EventChar;
return x >= '0' && x <= '9' ? 0 : 1;
}
void ConfigurationUI::draw_server_list() const {
for (std::vector<ServerList::Entry>::size_type i = 0; i < state.servers.entries.size(); ++i) {
ServerList::Entry &entry = state.servers.entries[i];
// Calculate Item Widths
const ImGuiStyle &style = ImGui::GetStyle();
@ -254,7 +227,7 @@ void ConfigurationUI::draw_server_list() {
// Address
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x - width_needed);
ImGui::InputTextWithHint((base_label + address_hint).c_str(), address_hint, &entry.first, ImGuiInputTextFlags_CharsNoBlank);
ImGui::InputTextWithHint((base_label + address_hint).c_str(), address_hint, &entry.first, ImGuiInputTextFlags_CallbackCharFilter, server_list_address_filter);
ImGui::PopItemWidth();
// Port
@ -262,7 +235,7 @@ void ConfigurationUI::draw_server_list() {
std::string port_str = port > 0 ? std::to_string(port) : "";
ImGui::SameLine();
ImGui::PushItemWidth(port_width);
if (ImGui::InputTextWithHint((base_label + port_hint).c_str(), port_hint, &port_str, ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_NoHorizontalScroll)) {
if (ImGui::InputTextWithHint((base_label + port_hint).c_str(), port_hint, &port_str, ImGuiInputTextFlags_CallbackCharFilter | ImGuiInputTextFlags_NoHorizontalScroll, server_list_port_filter)) {
port = ServerList::parse_port(port_str);
}
ImGui::PopItemWidth();
@ -270,7 +243,7 @@ void ConfigurationUI::draw_server_list() {
// Delete
ImGui::SameLine();
if (ImGui::Button((delete_text + base_label).c_str())) {
servers.entries.erase(servers.entries.begin() + int(i));
state.servers.entries.erase(state.servers.entries.begin() + int(i));
i--;
}
}

View File

@ -1,8 +1,8 @@
#include <fstream>
#include <libreborn/util.h>
#include <libreborn/util/util.h>
#include <libreborn/config.h>
#include <libreborn/exec.h>
#include <libreborn/util/exec.h>
#include "logger.h"
#include "../ui/frame.h"

View File

@ -8,9 +8,11 @@
#include <string>
#include <fcntl.h>
#include <libreborn/exec.h>
#include <libreborn/util/exec.h>
#include <libreborn/log.h>
#include <libreborn/util.h>
#include <libreborn/util/util.h>
#include <libreborn/util/string.h>
#include <libreborn/util/io.h>
#include <libreborn/config.h>
#include "logger.h"
@ -37,7 +39,7 @@ static void setup_log_file() {
const std::string logs = get_logs_folder();
// Get Timestamp
std::string time = format_time("%Y-%m-%d");
const std::string time = format_time("%Y-%m-%d");
// Get Log Filename
std::string file;

View File

@ -1,7 +1,7 @@
#include <cstdlib>
#include <libreborn/env.h>
#include <libreborn/util.h>
#include <libreborn/env/env.h>
#include <libreborn/util/util.h>
#include <libreborn/config.h>
#include "bootstrap/bootstrap.h"

View File

@ -1,7 +1,7 @@
#include <argp.h>
#include <libreborn/config.h>
#include <libreborn/env.h>
#include <libreborn/env/env.h>
#include <trampoline/types.h>
#include "parser.h"
@ -20,7 +20,7 @@ static argp_option options_data[] = {
#undef OPTION
{nullptr, 0, nullptr, 0, "Environmental Variables:", 0},
#define ENV(name, doc) {#name, env_key--, nullptr, OPTION_DOC | OPTION_NO_USAGE | (is_env_var_internal(name##_ENV) ? OPTION_HIDDEN : 0), doc, 0},
#include <libreborn/env-list.h>
#include <libreborn/env/list.h>
#ifdef MCPI_BUILD_RUNTIME
#include <trampoline/env-list.h>
#endif
@ -34,7 +34,7 @@ static argp_option options_data[] = {
case key: \
options->name = true; \
break;
static error_t parse_opt(int key, __attribute__((unused)) char *arg, argp_state *state) {
static error_t parse_opt(const int key, __attribute__((unused)) char *arg, argp_state *state) {
options_t *options = (options_t *) state->input;
switch (key) {
#include "option-list.h"
@ -45,7 +45,7 @@ static error_t parse_opt(int key, __attribute__((unused)) char *arg, argp_state
}
#undef OPTION
static argp argp = {options_data, parse_opt, nullptr, doc, nullptr, nullptr, nullptr};
options_t parse_options(int argc, char *argv[]) {
options_t parse_options(const int argc, char *argv[]) {
options_t options = {};
argp_parse(&argp, argc, argv, 0, nullptr, &options);
return options;

View File

@ -6,8 +6,8 @@
#include <imgui_impl_opengl2.h>
#include <libreborn/log.h>
#include <libreborn/glfw.h>
#include <libreborn/util.h>
#include <libreborn/util/glfw.h>
#include <libreborn/util/util.h>
// Init/Cleanup
Frame::Frame(const char *title, const int width, const int height) {

View File

@ -3,7 +3,7 @@
#include "util.h"
#include <libreborn/log.h>
#include <libreborn/env.h>
#include <libreborn/env/env.h>
#include <libreborn/config.h>
// $PATH

View File

@ -1,5 +1,6 @@
#include <libreborn/log.h>
#include <libreborn/util.h>
#include <libreborn/util/util.h>
#include <libreborn/util/io.h>
#include <libreborn/config.h>
#include "../bootstrap/bootstrap.h"

View File

@ -1,5 +1,5 @@
#include <libreborn/log.h>
#include <libreborn/exec.h>
#include <libreborn/util/exec.h>
#include "util.h"

View File

@ -11,14 +11,14 @@ add_library(reborn-util SHARED
src/util/util.cpp
src/util/log.cpp
src/util/cp437.cpp
src/util/env.cpp
src/util/env/env.cpp
src/util/config.cpp
src/util/flags/node.cpp
src/util/flags/flags.cpp
src/util/flags/available-feature-flags # Show In IDE
src/util/servers.cpp
src/util/env/flags/node.cpp
src/util/env/flags/flags.cpp
src/util/env/flags/available-feature-flags # Show In IDE
src/util/env/servers.cpp
)
embed_resource(reborn-util src/util/flags/available-feature-flags)
embed_resource(reborn-util src/util/env/flags/available-feature-flags)
target_link_libraries(reborn-util PRIVATE utf8cpp)
if(TARGET glfw)
target_sources(reborn-util PRIVATE src/util/glfw.cpp)

View File

@ -1,22 +0,0 @@
#pragma once
#include <string>
#define ENV(name, ...) extern const char *const name##_ENV;
#include "env-list.h"
#undef ENV
bool is_env_var_internal(const char *env);
void clear_internal_env_vars();
// Set Environmental Variable
void set_and_print_env(const char *name, const char *value);
// Convert Variable To Value And Vice-Versa
struct Flags;
std::string obj_to_env_value(const std::string &obj);
std::string obj_to_env_value(const float &obj);
std::string obj_to_env_value(const Flags &obj);
void env_value_to_obj(std::string &out, const char *value);
void env_value_to_obj(float &out, const char *value);
void env_value_to_obj(Flags &out, const char *value);

26
libreborn/include/libreborn/env/env.h vendored Normal file
View File

@ -0,0 +1,26 @@
#pragma once
#include <string>
#define ENV(name, ...) constexpr const char *name##_ENV = #name;
#include "list.h"
#undef ENV
bool is_env_var_internal(const char *env);
void clear_internal_env_vars();
// Set Environmental Variable
void setenv_safe(const char *name, const char *value);
void set_and_print_env(const char *name, const char *value);
// Convert Variable To Value And Vice-Versa
struct Flags;
struct ServerList;
#define overload(type) \
std::string obj_to_env_value(const type &obj); \
void env_value_to_obj(type &out, const char *value)
overload(std::string);
overload(float);
overload(Flags);
overload(ServerList);
#undef overload

View File

@ -2,6 +2,7 @@
ENV(MCPI_FEATURE_FLAGS, "Client-Mode Feature Flags")
ENV(MCPI_USERNAME, "Player Username")
ENV(MCPI_RENDER_DISTANCE, "Render Distance")
ENV(MCPI_SERVER_LIST, "Server List")
// Game Assets
ENV(_MCPI_REBORN_ASSETS_PATH, "")
ENV(_MCPI_VANILLA_ASSETS_PATH, "")

View File

@ -11,10 +11,9 @@ struct ServerList {
typedef std::pair<std::string, port_t> Entry;
// Load
static port_t parse_port(const std::string &s);
ServerList();
void load(const std::string &str);
// Save
std::string to_string() const;
void save() const;
[[nodiscard]] std::string to_string() const;
// Entries
std::vector<Entry> entries{};
std::vector<Entry> entries;
};

View File

@ -9,7 +9,7 @@
// fork() With I/O
struct Process {
static constexpr int fd_count = 3;
Process(pid_t pid_, std::array<int, fd_count> fds_);
Process(pid_t pid_, const std::array<int, fd_count> &fds_);
[[nodiscard]] int close() const;
const pid_t pid;
const std::array<int, fd_count> fds;

View File

@ -0,0 +1,17 @@
#pragma once
#include <cstddef>
// Safe Version Of pipe()
struct Pipe {
Pipe();
const int read;
const int write;
};
// Lock File
int lock_file(const char *file);
void unlock_file(const char *file, int fd);
// Safe write()
void safe_write(int fd, const void *buf, size_t size);

View File

@ -8,3 +8,7 @@ void sanitize_string(std::string &str, int max_length, bool allow_newlines);
// CP437
std::string to_cp437(const std::string &input);
std::string from_cp437(const std::string &input);
// Format Time
std::string format_time(const char *fmt);
std::string format_time(const char *fmt, int time);

View File

@ -3,7 +3,7 @@
#include <dlfcn.h>
#include <string>
#include "log.h"
#include "../log.h"
// Align Number
int align_up(int x, int alignment);
@ -24,20 +24,9 @@ int align_up(int x, int alignment);
} \
extern "C" __attribute__((__used__)) return_type name args
// Safe Version Of pipe()
struct Pipe {
Pipe();
const int read;
const int write;
};
// Check If Two Percentages Are Different Enough To Be Logged
bool is_progress_difference_significant(int32_t new_val, int32_t old_val);
// Lock File
int lock_file(const char *file);
void unlock_file(const char *file, int fd);
// Check $DISPLAY
void reborn_check_display();
@ -47,9 +36,6 @@ const char *get_home_subdirectory_for_game_data();
// Make Sure Directory Exists
void ensure_directory(const char *path);
// Safe write()
void safe_write(int fd, const void *buf, size_t size);
// embed_resource()
#define EMBEDDED_RESOURCE(name) \
extern unsigned char name[]; \
@ -58,10 +44,6 @@ void safe_write(int fd, const void *buf, size_t size);
// Profile Directory
std::string home_get();
// Format Time
std::string format_time(const char *fmt);
std::string format_time(const char *fmt, int time);
// Default MCPI Port
// This Macro DOES NOT Control MCPI
#define DEFAULT_MULTIPLAYER_PORT 19132

View File

@ -1,7 +1,7 @@
#include <cstdlib>
#include <libreborn/config.h>
#include <libreborn/env.h>
#include <libreborn/env/env.h>
// Access Configuration At Runtime
const char *reborn_get_version() {

View File

@ -2,7 +2,7 @@
#include "utf8.h"
#include <libreborn/string.h>
#include <libreborn/util/string.h>
// Conversion Functions
static std::u32string to_utf32(const std::string &s) {

View File

@ -1,29 +1,24 @@
#include <libreborn/env.h>
#include <libreborn/exec.h>
#include <libreborn/env/env.h>
#include <libreborn/util/exec.h>
#include <libreborn/log.h>
#include <libreborn/flags.h>
// Define Constants
#define ENV(name, ...) const char *const name##_ENV = #name;
#include <libreborn/env-list.h>
#undef ENV
#include <libreborn/env/flags.h>
#include <libreborn/env/servers.h>
// Clear Internal Variables
bool is_env_var_internal(const char *env) {
return env[0] == '_';
}
void clear_internal_env_vars() {
DEBUG("Clearing Internal Environmental Variables...");
#define ENV(name, ...) \
if (is_env_var_internal(name##_ENV)) { \
set_and_print_env(name##_ENV, nullptr); \
}
#include <libreborn/env-list.h>
#include <libreborn/env/list.h>
#undef ENV
}
// Set Environmental Variable
static void setenv_safe(const char *name, const char *value) {
void setenv_safe(const char *name, const char *value) {
if (value != nullptr) {
setenv(name, value, 1);
} else {
@ -48,6 +43,9 @@ std::string obj_to_env_value(const float &obj) {
std::string obj_to_env_value(const Flags &obj) {
return obj.to_string();
}
std::string obj_to_env_value(const ServerList &obj) {
return obj.to_string();
}
void env_value_to_obj(std::string &out, const char *value) {
out = value;
}
@ -57,3 +55,6 @@ void env_value_to_obj(float &out, const char *value) {
void env_value_to_obj(Flags &out, const char *value) {
out.from_string(value);
}
void env_value_to_obj(ServerList &out, const char *value) {
out.load(value);
}

View File

@ -3,8 +3,8 @@
#include <unordered_set>
#include <libreborn/log.h>
#include <libreborn/flags.h>
#include <libreborn/util.h>
#include <libreborn/env/flags.h>
#include <libreborn/util/util.h>
// All Flags
static unsigned int find_indent_level(std::string &str) {

View File

@ -1,7 +1,7 @@
#include <algorithm>
#include <libreborn/log.h>
#include <libreborn/flags.h>
#include <libreborn/env/flags.h>
// Flag
static int next_id;

View File

@ -2,13 +2,8 @@
#include <sstream>
#include <limits>
#include <libreborn/servers.h>
#include <libreborn/util.h>
// File
static std::string get_server_list_path() {
return home_get() + "/servers.txt";
}
#include <libreborn/env/servers.h>
#include <libreborn/util/util.h>
// Seperator
#define PORT_SEPERATOR_CHAR ':'
@ -22,27 +17,22 @@ ServerList::port_t ServerList::parse_port(const std::string &s) {
}
return static_cast<port_t>(port);
}
ServerList::ServerList() {
void ServerList::load(const std::string &str) {
// Clear
entries.clear();
// Open File
std::ifstream server_list_file(get_server_list_path());
if (!server_list_file) {
// File Does Not Exist
return;
}
std::stringstream server_list_file(str);
// Iterate Lines
std::string line;
while (std::getline(server_list_file, line)) {
// Check Line
if (!line.empty()) {
// Skip Comments
if (line[0] == '#') {
continue;
}
// Parse
std::string address;
std::string port_str;
size_t separator_pos = line.find_last_of(PORT_SEPERATOR_CHAR);
const size_t separator_pos = line.find_last_of(PORT_SEPERATOR_CHAR);
if (separator_pos == std::string::npos) {
port_str = std::to_string(DEFAULT_MULTIPLAYER_PORT);
address = line;
@ -54,29 +44,13 @@ ServerList::ServerList() {
entries.emplace_back(address, parse_port(port_str));
}
}
// Close
server_list_file.close();
}
// Save
std::string ServerList::to_string() const {
std::stringstream out;
out << "# External Servers File\n";
for (const Entry &entry : entries) {
out << entry.first << PORT_SEPERATOR_CHAR << std::to_string(entry.second) << '\n';
}
return out.str();
}
void ServerList::save() const {
// Open File
std::ofstream file(get_server_list_path());
if (!file) {
// Failure
WARN("Unable To Save Server List");
}
// Write
file << to_string();
// Close
file.close();
}

View File

@ -6,12 +6,12 @@
#include <sys/poll.h>
#include <cstring>
#include <libreborn/util.h>
#include <libreborn/log.h>
#include <libreborn/exec.h>
#include <libreborn/util/exec.h>
#include <libreborn/util/io.h>
// Fork
Process::Process(const pid_t pid_, const std::array<int, 3> fds_): pid(pid_), fds(fds_) {}
Process::Process(const pid_t pid_, const std::array<int, fd_count> &fds_): pid(pid_), fds(fds_) {}
int Process::close() const {
for (const int fd : fds) {
::close(fd);

View File

@ -1,8 +1,8 @@
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <libreborn/glfw.h>
#include <libreborn/util.h>
#include <libreborn/util/glfw.h>
#include <libreborn/util/util.h>
#include <libreborn/log.h>
#include <libreborn/config.h>

View File

@ -3,43 +3,28 @@
#include <string>
#include <libreborn/log.h>
#include <libreborn/env.h>
#include <libreborn/env/env.h>
// Debug Tag
const char *reborn_debug_tag = "";
// /dev/null FD
static int null_fd = -1;
static void setup_null_fd() {
if (null_fd == -1) {
null_fd = open("/dev/null", O_WRONLY | O_APPEND);
}
}
__attribute__((destructor)) static void close_null_fd() {
close(null_fd);
}
// Log File
static int log_fd = -1;
static constexpr int unset_fd = -1;
static int log_fd = unset_fd;
int reborn_get_log_fd() {
if (log_fd >= 0) {
if (log_fd != unset_fd) {
return log_fd;
}
// Open Log File
const char *fd_str = getenv(_MCPI_LOG_FD_ENV);
if (fd_str) {
log_fd = std::stoi(fd_str);
} else {
setup_null_fd();
log_fd = null_fd;
}
log_fd = fd_str ? std::stoi(fd_str) : -2;
// Return
return reborn_get_log_fd();
}
void reborn_set_log(const int fd) {
// Set Variable
log_fd = -1;
set_and_print_env(_MCPI_LOG_FD_ENV, fd >= 0 ? std::to_string(fd).c_str() : nullptr);
log_fd = unset_fd;
setenv_safe(_MCPI_LOG_FD_ENV, fd >= 0 ? std::to_string(fd).c_str() : nullptr);
}
// Debug Logging

View File

@ -1,21 +1,42 @@
#include <libreborn/string.h>
#include <ctime>
#include <cstring>
#include <libreborn/util/string.h>
#include <libreborn/log.h>
// Sanitize String
void sanitize_string(std::string &str, const int max_length, const bool allow_newlines) {
// Store Message Length
size_t length = str.size();
// Truncate Message
if (max_length >= 0 && length > ((size_t) max_length)) {
if (max_length >= 0 && str.size() > ((size_t) max_length)) {
str = str.substr(0, max_length);
length = max_length;
}
// Loop Through Message
if (!allow_newlines) {
for (size_t i = 0; i < length; i++) {
if (str[i] == '\n') {
for (char &x : str) {
if (x == '\n') {
// Replace Newline
str[i] = ' ';
x = ' ';
}
}
}
}
// 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);
}

View File

@ -2,12 +2,13 @@
#include <sys/file.h>
#include <sys/stat.h>
#include <cstring>
#include <ctime>
#include <cmath>
#include <libreborn/util.h>
#include <libreborn/util/util.h>
#include <libreborn/util/io.h>
#include <libreborn/config.h>
#include <libreborn/env.h>
#include <libreborn/env/env.h>
#include <libreborn/log.h>
// Align Number
int align_up(int x, const int alignment) {
@ -102,6 +103,9 @@ void ensure_directory(const char *path) {
// Write To FD
void safe_write(const int fd, const void *buf, const size_t size) {
if (fd < 0) {
return;
}
const ssize_t bytes_written = write(fd, buf, size);
if (bytes_written < 0) {
ERR("Unable To Write Data: %s", strerror(errno));
@ -116,23 +120,3 @@ std::string home_get() {
}
return std::string(home) + std::string(get_home_subdirectory_for_game_data());
}
// 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);
}

View File

@ -5,8 +5,8 @@
#include <SDL/SDL.h>
#include <libreborn/log.h>
#include <libreborn/string.h>
#include <libreborn/glfw.h>
#include <libreborn/util/string.h>
#include <libreborn/util/glfw.h>
#include <libreborn/config.h>
#include <media-layer/core.h>

View File

@ -2,7 +2,7 @@
#include <GLES/gl.h>
#include <libreborn/log.h>
#include <libreborn/util.h>
#include <libreborn/util/util.h>
#include "common/common.h"

View File

@ -1,7 +1,7 @@
#include <GLES/gl.h>
#include <libreborn/patch.h>
#include <libreborn/util.h>
#include <libreborn/util/util.h>
#include <symbols/minecraft.h>

View File

@ -1,8 +1,8 @@
#include <ctime>
#include <libreborn/patch.h>
#include <libreborn/util.h>
#include <libreborn/env.h>
#include <libreborn/util/util.h>
#include <libreborn/env/env.h>
#include <libreborn/config.h>
#include <symbols/minecraft.h>

View File

@ -1,5 +1,5 @@
#include <libreborn/patch.h>
#include <libreborn/string.h>
#include <libreborn/util/string.h>
#include <symbols/minecraft.h>

View File

@ -1,7 +1,7 @@
#include <SDL/SDL.h>
#include <media-layer/core.h>
#include <libreborn/util.h>
#include <libreborn/util/util.h>
#include <libreborn/config.h>
#include <symbols/minecraft.h>

View File

@ -3,8 +3,8 @@
#include <libreborn/log.h>
#include <libreborn/config.h>
#include <libreborn/env.h>
#include <libreborn/flags.h>
#include <libreborn/env/env.h>
#include <libreborn/env/flags.h>
#include <mods/feature/feature.h>

View File

@ -4,7 +4,7 @@
#include <symbols/minecraft.h>
#include <libreborn/patch.h>
#include <libreborn/util.h>
#include <libreborn/util/util.h>
#include <mods/text-input-box/TextInputScreen.h>
#include <mods/touch/touch.h>

View File

@ -1,7 +1,7 @@
#include <vector>
#include <libreborn/patch.h>
#include <libreborn/util.h>
#include <libreborn/util/util.h>
#include <symbols/minecraft.h>
#include <GLES/gl.h>

View File

@ -2,7 +2,7 @@
#include <libreborn/patch.h>
#include <libreborn/config.h>
#include <libreborn/env.h>
#include <libreborn/env/env.h>
#include <symbols/minecraft.h>

View File

@ -1,6 +1,6 @@
#include <libreborn/patch.h>
#include <libreborn/env.h>
#include <libreborn/util.h>
#include <libreborn/env/env.h>
#include <libreborn/util/util.h>
#include <symbols/minecraft.h>

View File

@ -1,8 +1,8 @@
#include <string>
#include <libreborn/patch.h>
#include <libreborn/string.h>
#include <libreborn/util.h>
#include <libreborn/util/string.h>
#include <libreborn/util/util.h>
#include <symbols/minecraft.h>

View File

@ -12,10 +12,10 @@
#include <media-layer/core.h>
#include <libreborn/patch.h>
#include <libreborn/string.h>
#include <libreborn/util/string.h>
#include <libreborn/config.h>
#include <libreborn/util.h>
#include <libreborn/env.h>
#include <libreborn/util/util.h>
#include <libreborn/env/env.h>
#include <symbols/minecraft.h>

View File

@ -1,8 +1,8 @@
#include <cmath>
#include <libreborn/patch.h>
#include <libreborn/env.h>
#include <libreborn/util.h>
#include <libreborn/env/env.h>
#include <libreborn/util/util.h>
#include <symbols/minecraft.h>

View File

@ -3,7 +3,7 @@
#include <symbols/minecraft.h>
#include <libreborn/patch.h>
#include <libreborn/util.h>
#include <libreborn/util/util.h>
#include <media-layer/core.h>

View File

@ -5,7 +5,8 @@
#include <symbols/minecraft.h>
#include <libreborn/patch.h>
#include <libreborn/servers.h>
#include <libreborn/env/servers.h>
#include <libreborn/env/env.h>
#include <mods/init/init.h>
#include <mods/feature/feature.h>
@ -14,6 +15,14 @@
static void iterate_servers(const std::function<void(const char *address, ServerList::port_t port)> &callback) {
// Load
static ServerList server_list;
static bool loaded = false;
if (!loaded) {
const char *str = getenv(MCPI_SERVER_LIST_ENV);
if (str != nullptr) {
env_value_to_obj(server_list, str);
}
loaded = true;
}
// Loop
for (const ServerList::Entry &entry : server_list.entries) {
if (!entry.first.empty() && entry.second > 0) {

View File

@ -1,7 +1,7 @@
#include <libreborn/log.h>
#include <libreborn/exec.h>
#include <libreborn/util/exec.h>
#include <libreborn/config.h>
#include <libreborn/util.h>
#include <libreborn/util/util.h>
#include <symbols/minecraft.h>
#include <GLES/gl.h>

View File

@ -6,9 +6,9 @@
#include <libreborn/patch.h>
#include <libreborn/config.h>
#include <libreborn/env.h>
#include <libreborn/string.h>
#include <libreborn/util.h>
#include <libreborn/env/env.h>
#include <libreborn/util/string.h>
#include <libreborn/util/util.h>
#include <symbols/minecraft.h>

View File

@ -4,8 +4,8 @@
#include <unistd.h>
#include <fcntl.h>
#include <libreborn/util.h>
#include <libreborn/env.h>
#include <libreborn/util/util.h>
#include <libreborn/env/env.h>
#include <mods/override/override.h>
#include <mods/init/init.h>

View File

@ -6,7 +6,8 @@
#include "stb_image_write.h"
#include <libreborn/log.h>
#include <libreborn/util.h>
#include <libreborn/util/util.h>
#include <libreborn/util/string.h>
#include <libreborn/config.h>
#include <GLES/gl.h>
@ -43,7 +44,7 @@ void screenshot_take(Gui *gui) {
}
// Get Timestamp
std::string time = format_time("%Y-%m-%d_%H.%M.%S");
const std::string time = format_time("%Y-%m-%d_%H.%M.%S");
// Prevent Overwriting Screenshots
int num = 0;

View File

@ -12,9 +12,9 @@
#include <SDL/SDL.h>
#include <libreborn/patch.h>
#include <libreborn/env.h>
#include <libreborn/string.h>
#include <libreborn/util.h>
#include <libreborn/env/env.h>
#include <libreborn/util/string.h>
#include <libreborn/util/util.h>
#include <symbols/minecraft.h>

View File

@ -2,8 +2,8 @@
#include <vector>
#include <libreborn/patch.h>
#include <libreborn/env.h>
#include <libreborn/exec.h>
#include <libreborn/env/env.h>
#include <libreborn/util/exec.h>
#include <libreborn/config.h>
#include <symbols/minecraft.h>

View File

@ -5,7 +5,7 @@
#include <GLES/gl.h>
#include <libreborn/patch.h>
#include <libreborn/util.h>
#include <libreborn/util/util.h>
#include <libreborn/config.h>
#include <symbols/minecraft.h>

View File

@ -2,8 +2,8 @@
#include <libreborn/patch.h>
#include <libreborn/config.h>
#include <libreborn/util.h>
#include <libreborn/exec.h>
#include <libreborn/util/util.h>
#include <libreborn/util/exec.h>
#include <symbols/minecraft.h>