Simplify Cache Loading
This commit is contained in:
parent
a740814354
commit
09c8af0396
@ -3,7 +3,6 @@
|
||||
#include <fstream>
|
||||
#include <unordered_map>
|
||||
#include <sstream>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <libreborn/log.h>
|
||||
@ -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<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));
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,22 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
// Cache Version
|
||||
#define CACHE_VERSION 0
|
||||
|
||||
// Load Cache
|
||||
struct launcher_cache {
|
||||
std::string username;
|
||||
std::string render_distance;
|
||||
std::unordered_map<std::string, bool> 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
|
||||
|
@ -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 <typename T>
|
||||
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
|
||||
|
@ -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;
|
||||
|
@ -1,7 +1,6 @@
|
||||
#include <vector>
|
||||
|
||||
#include "configuration.h"
|
||||
#include "cache.h"
|
||||
|
||||
#include <imgui_stdlib.h>
|
||||
|
||||
@ -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<std::tuple<const char *, const char *, const State *>> reset_options = {
|
||||
{"Revert", "Last Saved", &original_state},
|
||||
{"Reset", "Default", &default_state}
|
||||
{"Reset", "Default", &default_state},
|
||||
};
|
||||
for (const std::tuple<const char *, const char *, const State *> &option : reset_options) {
|
||||
const State &new_state = *std::get<2>(option);
|
||||
|
Loading…
Reference in New Issue
Block a user