From 09c8af039644dee811bf958549725e1646d4929b Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Sun, 24 Nov 2024 19:57:52 -0500 Subject: [PATCH] Simplify Cache Loading --- launcher/src/client/cache.cpp | 29 ++++++++++----------------- launcher/src/client/cache.h | 13 ++---------- launcher/src/client/configuration.cpp | 15 +++++++------- launcher/src/client/configuration.h | 3 +-- launcher/src/client/ui.cpp | 5 ++--- 5 files changed, 24 insertions(+), 41 deletions(-) diff --git a/launcher/src/client/cache.cpp b/launcher/src/client/cache.cpp index 2fb298ad2c..b6a5c4f4f4 100644 --- a/launcher/src/client/cache.cpp +++ b/launcher/src/client/cache.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include #include @@ -18,25 +17,18 @@ static std::string get_cache_path() { } // Load -launcher_cache empty_cache = { - .username = DEFAULT_USERNAME, - .render_distance = DEFAULT_RENDER_DISTANCE, - .feature_flags = {} -}; -launcher_cache load_cache() { +State load_cache() { // Log DEBUG("Loading Launcher Cache..."); // Return Value - launcher_cache ret = empty_cache; + State ret; // Open File std::ifstream stream(get_cache_path(), std::ios::in | std::ios::binary); if (!stream) { - // Fail - struct stat s = {}; // No Warning If File Doesn't Exist - if (stat(get_cache_path().c_str(), &s) == 0) { + if (errno != ENOENT) { WARN("Unable To Open Launcher Cache For Loading"); } } else { @@ -54,28 +46,29 @@ launcher_cache load_cache() { WARN("Invalid Launcher Cache Version (Expected: %i, Actual: %i)", CACHE_VERSION, (int) cache_version); } else { // Load Username And Render Distance - launcher_cache cache; - std::getline(stream, cache.username, '\0'); - std::getline(stream, cache.render_distance, '\0'); + State state; + std::getline(stream, state.username, '\0'); + std::getline(stream, state.render_distance, '\0'); // Load Feature Flags + std::unordered_map 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)); - cache.feature_flags[flag] = is_enabled; + flags[flag] = is_enabled; } stream.peek(); } + state.flags.from_cache(flags); - // Finish + // Check For Error if (!stream) { - // Fail WARN("Failure While Loading Launcher Cache"); } else { // Success - ret = cache; + ret = state; } } diff --git a/launcher/src/client/cache.h b/launcher/src/client/cache.h index 6b5c3f7473..f989510088 100644 --- a/launcher/src/client/cache.h +++ b/launcher/src/client/cache.h @@ -1,22 +1,13 @@ #pragma once -#include -#include - // Cache Version #define CACHE_VERSION 0 // Load Cache -struct launcher_cache { - std::string username; - std::string render_distance; - std::unordered_map feature_flags; -}; -extern launcher_cache empty_cache; -launcher_cache load_cache(); +struct State; +State load_cache(); // Save Cache -struct State; void save_cache(const State &state); // Wipe Cache diff --git a/launcher/src/client/configuration.cpp b/launcher/src/client/configuration.cpp index 05bcabbdf8..9c928a5b54 100644 --- a/launcher/src/client/configuration.cpp +++ b/launcher/src/client/configuration.cpp @@ -5,11 +5,10 @@ #include "cache.h" // State -State::State(const launcher_cache &cache): flags("") { - username = cache.username; - render_distance = cache.render_distance; +State::State(): flags("") { + username = DEFAULT_USERNAME; + render_distance = DEFAULT_RENDER_DISTANCE; flags = Flags::get(); - flags.from_cache(cache.feature_flags); } template static void update_from_env(const char *env, T &value, const bool save) { @@ -52,10 +51,12 @@ void handle_non_launch_client_only_commands(const options_t &options) { // Configure Client Options void configure_client(const options_t &options) { // Load Cache - const launcher_cache cache = options.no_cache ? empty_cache : load_cache(); + State state; + if (!options.no_cache) { + state = load_cache(); + } - // Setup State - State state(cache); + // Read From Environment state.update(false); // --default diff --git a/launcher/src/client/configuration.h b/launcher/src/client/configuration.h index 10a8125fea..67e3dda25c 100644 --- a/launcher/src/client/configuration.h +++ b/launcher/src/client/configuration.h @@ -14,7 +14,7 @@ // State struct State { - explicit State(const launcher_cache &cache); + State(); // Methods void update(bool save); bool operator==(const State &other) const; @@ -34,7 +34,6 @@ private: void draw_main(); void draw_advanced() const; static void draw_category(FlagNode &category); - const State default_state; const State original_state; State &state; bool &save_settings; diff --git a/launcher/src/client/ui.cpp b/launcher/src/client/ui.cpp index e947180e26..a56231327a 100644 --- a/launcher/src/client/ui.cpp +++ b/launcher/src/client/ui.cpp @@ -1,7 +1,6 @@ #include #include "configuration.h" -#include "cache.h" #include @@ -17,7 +16,6 @@ static std::vector render_distances = { static constexpr int size = 400; ConfigurationUI::ConfigurationUI(State &state_, bool &save_settings_): Frame("Launcher", size, size), - default_state(empty_cache), original_state(state_), state(state_), save_settings(save_settings_) { @@ -59,9 +57,10 @@ int ConfigurationUI::render() { // Bottom Row int ConfigurationUI::draw_bottom() { // Reset All Settings + const State default_state; std::vector> reset_options = { {"Revert", "Last Saved", &original_state}, - {"Reset", "Default", &default_state} + {"Reset", "Default", &default_state}, }; for (const std::tuple &option : reset_options) { const State &new_state = *std::get<2>(option);