From 5aae95fd37b5e9d680a7ee44c61fd1012eabefac Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Fri, 23 Sep 2022 00:31:42 -0400 Subject: [PATCH] Add --wipe-cache --- docs/COMMAND_LINE.md | 29 +++++++---- launcher/src/client/cache.cpp | 12 +++++ launcher/src/client/cache.h | 3 ++ launcher/src/client/launcher.cpp | 89 ++++++++++++++++++-------------- 4 files changed, 83 insertions(+), 50 deletions(-) diff --git a/docs/COMMAND_LINE.md b/docs/COMMAND_LINE.md index 8e208286..7e86ef64 100644 --- a/docs/COMMAND_LINE.md +++ b/docs/COMMAND_LINE.md @@ -5,8 +5,10 @@ ### ``--version`` (Or ``-v``) If you run MCPI-Reborn with ``--version`` it will print its version to ``stdout``. -### ``--print-available-feature-flags`` (Client Mode Only) -If you run MCPI-Reborn with ``--print-available-feature-flags``, it will print the available feature flags to ``stdout`` and then immediately exit. +### Client Mode Only + +#### ``--print-available-feature-flags`` +This print the available feature flags (and their default values) to ``stdout`` and then immediately exit. The feature flags are printed in the following format: ``` @@ -14,19 +16,24 @@ TRUE This Flag Is On By Default FALSE This Flag Is Off By Default ``` -### ``--default`` (Client Mode Only) -If you run MCPI-Reborn with ``--default``, it will skip the startup configuration dialogs and just use the default values. +#### ``--default`` +This will skip the startup configuration dialogs and just use the default values. This will use the cached configuration unless ``--no-cache`` is used. -### ``--only-generate`` (Server Mode Only) -If you run MCPI-Reborn with ``--only-generate``, it will immediately exit once world generation has completed. This is mainly used for automatically testing MCPI-Reborn. - -### ``--benchmark`` (Client Mode Only) -If you run MCPI-Reborn with ``--benchmark``, it will enter a simple benchmark mode. This means automatically loading a newly generated world, then rotating the camera for a period of time. When it has finished, it will then exit and print the average FPS while the world was loaded. In this mode, all user input is blocked. However you can still modify rendering settings by changing feature flags. +#### ``--benchmark`` +This will make MCPI-Reborn enter a simple benchmark mode. This means automatically loading a newly generated world, then rotating the camera for a period of time. When it has finished, it will then exit and print the average FPS while the world was loaded. In this mode, all user input is blocked. However you can still modify rendering settings by changing feature flags. The world used will always be re-created on start and uses a hard-coded seed. -### ``--no-cache`` (Client Mode Only) -If you run MCPI-Reborn with ``--no-cache``, it will skip loading and saving the cached launcher configuration. +#### ``--no-cache`` +This will skip loading and saving the cached launcher configuration. + +#### ``--wipe-cache`` +This will wipe the cached launcher configuration. + +### Server Mode Only + +#### ``--only-generate`` +This will make MCPI-Reborn immediately exit once world generation has completed. This is mainly used for automatically testing MCPI-Reborn. ## Environmental Variables diff --git a/launcher/src/client/cache.cpp b/launcher/src/client/cache.cpp index 9af5764a..0c9075b0 100644 --- a/launcher/src/client/cache.cpp +++ b/launcher/src/client/cache.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -139,3 +140,14 @@ void save_cache() { WARN("Failure While Saving Launcher Cache"); } } + +// Wipe Cache +void wipe_cache() { + // Log + INFO("Wiping Launcher Cache..."); + + // Unlink File + if (unlink(get_cache_path().c_str()) != 0) { + WARN("Failure While Wiping Cache: %s", strerror(errno)); + } +} diff --git a/launcher/src/client/cache.h b/launcher/src/client/cache.h index ba80c47c..3e05c164 100644 --- a/launcher/src/client/cache.h +++ b/launcher/src/client/cache.h @@ -17,3 +17,6 @@ launcher_cache load_cache(); // Save Cache void save_cache(); + +// Wipe Cache +void wipe_cache(); diff --git a/launcher/src/client/launcher.cpp b/launcher/src/client/launcher.cpp index 1807aec4..73447d6c 100644 --- a/launcher/src/client/launcher.cpp +++ b/launcher/src/client/launcher.cpp @@ -149,9 +149,6 @@ int main(int argc, char *argv[]) { ERR("$HOME Isn't Set"); } - // Pre-Bootstrap - pre_bootstrap(argc, argv); - // Print Features for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "--print-available-feature-flags") == 0) { @@ -164,33 +161,22 @@ int main(int argc, char *argv[]) { } } - // --default + // Pre-Bootstrap + pre_bootstrap(argc, argv); + + // Create ~/.minecraft-pi If Needed + { + char *minecraft_folder = NULL; + safe_asprintf(&minecraft_folder, "%s" HOME_SUBDIRECTORY_FOR_GAME_DATA, getenv("HOME")); + const char *const command[] = {"mkdir", "-p", minecraft_folder, NULL}; + run_simple_command(command, "Unable To Create Data Directory"); + free(minecraft_folder); + } + + // --wipe-cache for (int i = 1; i < argc; i++) { - if (strcmp(argv[i], "--default") == 0) { - // Use Default Feature Flags - set_env_if_unset("MCPI_FEATURE_FLAGS", []() { - std::string feature_flags = ""; - load_available_feature_flags([&feature_flags](std::string flag) { - bool default_value; - // Strip Default Value - std::string stripped_flag = strip_feature_flag_default(flag, &default_value); - // Specify Default Value - if (default_value) { - // Enabled By Default - feature_flags += stripped_flag + '|'; - } - }); - if (feature_flags.length() > 0 && feature_flags[feature_flags.length() - 1] == '|') { - feature_flags.pop_back(); - } - return feature_flags; - }); - set_env_if_unset("MCPI_RENDER_DISTANCE", []() { - return DEFAULT_RENDER_DISTANCE; - }); - set_env_if_unset("MCPI_USERNAME", []() { - return DEFAULT_USERNAME; - }); + if (strcmp(argv[i], "--wipe-cache") == 0) { + wipe_cache(); break; } } @@ -203,19 +189,44 @@ int main(int argc, char *argv[]) { break; } } - - // Create ~/.minecraft-pi If Needed - { - char *minecraft_folder = NULL; - safe_asprintf(&minecraft_folder, "%s" HOME_SUBDIRECTORY_FOR_GAME_DATA, getenv("HOME")); - const char *const command[] = {"mkdir", "-p", minecraft_folder, NULL}; - run_simple_command(command, "Unable To Create Data Directory"); - free(minecraft_folder); - } - // Load Cache launcher_cache cache = no_cache ? empty_cache : load_cache(); + // --default + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--default") == 0) { + // Use Default Feature Flags + set_env_if_unset("MCPI_FEATURE_FLAGS", [&cache]() { + std::string feature_flags = ""; + load_available_feature_flags([&feature_flags, &cache](std::string flag) { + bool value; + // Strip Default Value + std::string stripped_flag = strip_feature_flag_default(flag, &value); + // Use Cache + if (cache.feature_flags.count(stripped_flag) > 0) { + value = cache.feature_flags[stripped_flag]; + } + // Specify Default Value + if (value) { + // Enabled By Default + feature_flags += stripped_flag + '|'; + } + }); + if (feature_flags.length() > 0 && feature_flags[feature_flags.length() - 1] == '|') { + feature_flags.pop_back(); + } + return feature_flags; + }); + set_env_if_unset("MCPI_RENDER_DISTANCE", [&cache]() { + return cache.render_distance; + }); + set_env_if_unset("MCPI_USERNAME", [&cache]() { + return cache.username; + }); + break; + } + } + // Setup MCPI_FEATURE_FLAGS { std::vector command;