Simplify Cache Loading
This commit is contained in:
parent
a740814354
commit
09c8af0396
@ -3,7 +3,6 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <libreborn/log.h>
|
#include <libreborn/log.h>
|
||||||
@ -18,25 +17,18 @@ static std::string get_cache_path() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Load
|
// Load
|
||||||
launcher_cache empty_cache = {
|
State load_cache() {
|
||||||
.username = DEFAULT_USERNAME,
|
|
||||||
.render_distance = DEFAULT_RENDER_DISTANCE,
|
|
||||||
.feature_flags = {}
|
|
||||||
};
|
|
||||||
launcher_cache load_cache() {
|
|
||||||
// Log
|
// Log
|
||||||
DEBUG("Loading Launcher Cache...");
|
DEBUG("Loading Launcher Cache...");
|
||||||
|
|
||||||
// Return Value
|
// Return Value
|
||||||
launcher_cache ret = empty_cache;
|
State ret;
|
||||||
|
|
||||||
// Open File
|
// Open File
|
||||||
std::ifstream stream(get_cache_path(), std::ios::in | std::ios::binary);
|
std::ifstream stream(get_cache_path(), std::ios::in | std::ios::binary);
|
||||||
if (!stream) {
|
if (!stream) {
|
||||||
// Fail
|
|
||||||
struct stat s = {};
|
|
||||||
// No Warning If File Doesn't Exist
|
// 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");
|
WARN("Unable To Open Launcher Cache For Loading");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -54,28 +46,29 @@ launcher_cache load_cache() {
|
|||||||
WARN("Invalid Launcher Cache Version (Expected: %i, Actual: %i)", CACHE_VERSION, (int) cache_version);
|
WARN("Invalid Launcher Cache Version (Expected: %i, Actual: %i)", CACHE_VERSION, (int) cache_version);
|
||||||
} else {
|
} else {
|
||||||
// Load Username And Render Distance
|
// Load Username And Render Distance
|
||||||
launcher_cache cache;
|
State state;
|
||||||
std::getline(stream, cache.username, '\0');
|
std::getline(stream, state.username, '\0');
|
||||||
std::getline(stream, cache.render_distance, '\0');
|
std::getline(stream, state.render_distance, '\0');
|
||||||
|
|
||||||
// Load Feature Flags
|
// Load Feature Flags
|
||||||
|
std::unordered_map<std::string, bool> flags;
|
||||||
std::string flag;
|
std::string flag;
|
||||||
while (!stream.eof() && std::getline(stream, flag, '\0')) {
|
while (!stream.eof() && std::getline(stream, flag, '\0')) {
|
||||||
if (!flag.empty()) {
|
if (!flag.empty()) {
|
||||||
bool is_enabled = false;
|
bool is_enabled = false;
|
||||||
stream.read((char *) &is_enabled, sizeof(bool));
|
stream.read((char *) &is_enabled, sizeof(bool));
|
||||||
cache.feature_flags[flag] = is_enabled;
|
flags[flag] = is_enabled;
|
||||||
}
|
}
|
||||||
stream.peek();
|
stream.peek();
|
||||||
}
|
}
|
||||||
|
state.flags.from_cache(flags);
|
||||||
|
|
||||||
// Finish
|
// Check For Error
|
||||||
if (!stream) {
|
if (!stream) {
|
||||||
// Fail
|
|
||||||
WARN("Failure While Loading Launcher Cache");
|
WARN("Failure While Loading Launcher Cache");
|
||||||
} else {
|
} else {
|
||||||
// Success
|
// Success
|
||||||
ret = cache;
|
ret = state;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,22 +1,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <unordered_map>
|
|
||||||
|
|
||||||
// Cache Version
|
// Cache Version
|
||||||
#define CACHE_VERSION 0
|
#define CACHE_VERSION 0
|
||||||
|
|
||||||
// Load Cache
|
// Load Cache
|
||||||
struct launcher_cache {
|
struct State;
|
||||||
std::string username;
|
State load_cache();
|
||||||
std::string render_distance;
|
|
||||||
std::unordered_map<std::string, bool> feature_flags;
|
|
||||||
};
|
|
||||||
extern launcher_cache empty_cache;
|
|
||||||
launcher_cache load_cache();
|
|
||||||
|
|
||||||
// Save Cache
|
// Save Cache
|
||||||
struct State;
|
|
||||||
void save_cache(const State &state);
|
void save_cache(const State &state);
|
||||||
|
|
||||||
// Wipe Cache
|
// Wipe Cache
|
||||||
|
@ -5,11 +5,10 @@
|
|||||||
#include "cache.h"
|
#include "cache.h"
|
||||||
|
|
||||||
// State
|
// State
|
||||||
State::State(const launcher_cache &cache): flags("") {
|
State::State(): flags("") {
|
||||||
username = cache.username;
|
username = DEFAULT_USERNAME;
|
||||||
render_distance = cache.render_distance;
|
render_distance = DEFAULT_RENDER_DISTANCE;
|
||||||
flags = Flags::get();
|
flags = Flags::get();
|
||||||
flags.from_cache(cache.feature_flags);
|
|
||||||
}
|
}
|
||||||
template <typename T>
|
template <typename T>
|
||||||
static void update_from_env(const char *env, T &value, const bool save) {
|
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
|
// Configure Client Options
|
||||||
void configure_client(const options_t &options) {
|
void configure_client(const options_t &options) {
|
||||||
// Load Cache
|
// Load Cache
|
||||||
const launcher_cache cache = options.no_cache ? empty_cache : load_cache();
|
State state;
|
||||||
|
if (!options.no_cache) {
|
||||||
|
state = load_cache();
|
||||||
|
}
|
||||||
|
|
||||||
// Setup State
|
// Read From Environment
|
||||||
State state(cache);
|
|
||||||
state.update(false);
|
state.update(false);
|
||||||
|
|
||||||
// --default
|
// --default
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
// State
|
// State
|
||||||
struct State {
|
struct State {
|
||||||
explicit State(const launcher_cache &cache);
|
State();
|
||||||
// Methods
|
// Methods
|
||||||
void update(bool save);
|
void update(bool save);
|
||||||
bool operator==(const State &other) const;
|
bool operator==(const State &other) const;
|
||||||
@ -34,7 +34,6 @@ private:
|
|||||||
void draw_main();
|
void draw_main();
|
||||||
void draw_advanced() const;
|
void draw_advanced() const;
|
||||||
static void draw_category(FlagNode &category);
|
static void draw_category(FlagNode &category);
|
||||||
const State default_state;
|
|
||||||
const State original_state;
|
const State original_state;
|
||||||
State &state;
|
State &state;
|
||||||
bool &save_settings;
|
bool &save_settings;
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "configuration.h"
|
#include "configuration.h"
|
||||||
#include "cache.h"
|
|
||||||
|
|
||||||
#include <imgui_stdlib.h>
|
#include <imgui_stdlib.h>
|
||||||
|
|
||||||
@ -17,7 +16,6 @@ static std::vector render_distances = {
|
|||||||
static constexpr int size = 400;
|
static constexpr int size = 400;
|
||||||
ConfigurationUI::ConfigurationUI(State &state_, bool &save_settings_):
|
ConfigurationUI::ConfigurationUI(State &state_, bool &save_settings_):
|
||||||
Frame("Launcher", size, size),
|
Frame("Launcher", size, size),
|
||||||
default_state(empty_cache),
|
|
||||||
original_state(state_),
|
original_state(state_),
|
||||||
state(state_),
|
state(state_),
|
||||||
save_settings(save_settings_) {
|
save_settings(save_settings_) {
|
||||||
@ -59,9 +57,10 @@ int ConfigurationUI::render() {
|
|||||||
// Bottom Row
|
// Bottom Row
|
||||||
int ConfigurationUI::draw_bottom() {
|
int ConfigurationUI::draw_bottom() {
|
||||||
// Reset All Settings
|
// Reset All Settings
|
||||||
|
const State default_state;
|
||||||
std::vector<std::tuple<const char *, const char *, const State *>> reset_options = {
|
std::vector<std::tuple<const char *, const char *, const State *>> reset_options = {
|
||||||
{"Revert", "Last Saved", &original_state},
|
{"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) {
|
for (const std::tuple<const char *, const char *, const State *> &option : reset_options) {
|
||||||
const State &new_state = *std::get<2>(option);
|
const State &new_state = *std::get<2>(option);
|
||||||
|
Loading…
Reference in New Issue
Block a user