From e895f57e50817eeb57230a74392a70f510d22c12 Mon Sep 17 00:00:00 2001 From: fed <58637860+fedddddd@users.noreply.github.com> Date: Mon, 12 Feb 2024 17:11:48 +0100 Subject: [PATCH] Add option to switch branch --- data/cdata/ui_scripts/settings/settings.lua | 25 ++++++++ data/zonetool/localizedstrings/english.json | 3 + src/client/component/config.cpp | 2 + src/client/component/ui_scripting.cpp | 14 +++++ src/client/component/updater.cpp | 64 ++++++++++++++++++++- src/client/component/updater.hpp | 12 ++++ 6 files changed, 119 insertions(+), 1 deletion(-) diff --git a/data/cdata/ui_scripts/settings/settings.lua b/data/cdata/ui_scripts/settings/settings.lua index de8788bb..845f2e44 100644 --- a/data/cdata/ui_scripts/settings/settings.lua +++ b/data/cdata/ui_scripts/settings/settings.lua @@ -61,6 +61,31 @@ LUI.MenuBuilder.m_types_build["settings_menu"] = function(a1) }) end + if (Engine.InFrontend()) then + local dev = "@LUA_MENU_SWITCH_TO_DEVELOP" + local main = "@LUA_MENU_SWITCH_TO_MAIN" + + local text = nil + local branch = updater.getcurrentbranch() + + local oppositebranch = nil + + if (branch == updater.develop) then + text = main + oppositebranch = updater.main + else + text = dev + oppositebranch = updater.develop + end + + menu:AddButton(text, function() + updater.setbranch(oppositebranch) + LUI.tryupdating(false) + end, nil, true, nil, { + desc_text = Engine.Localize("@LUA_MENU_SWITCH_BRANCH_DESC") + }) + end + createdivider(menu, "@LUA_MENU_DRAWING") LUI.Options.CreateOptionButton( diff --git a/data/zonetool/localizedstrings/english.json b/data/zonetool/localizedstrings/english.json index 665174ef..312c8950 100644 --- a/data/zonetool/localizedstrings/english.json +++ b/data/zonetool/localizedstrings/english.json @@ -65,6 +65,9 @@ "LUA_MENU_MODE3": "Debug shader", "LUA_MENU_INTRO": "Intro movie", "LUA_MENU_INTRO_DESC": "Show or skip intro movie with companies' logos on startup.", + "LUA_MENU_SWITCH_TO_DEVELOP": "Switch to the ^3Develop^7 branch", + "LUA_MENU_SWITCH_TO_MAIN": "Switch to the ^3Main^7 branch", + "LUA_MENU_SWITCH_BRANCH_DESC": "Switch the h2-mod branch (develop: latest changes, main: latest stable release).", "MENU_MUSIC_VOLUME": "Music Volume", "MENU_MUSIC_VOLUME_DESC": "Move the slider to adjust the volume of the music.", diff --git a/src/client/component/config.cpp b/src/client/component/config.cpp index bfe54b65..ca5c85d2 100644 --- a/src/client/component/config.cpp +++ b/src/client/component/config.cpp @@ -4,6 +4,7 @@ #include "config.hpp" #include "console.hpp" #include "language.hpp" +#include "updater.hpp" #include #include @@ -35,6 +36,7 @@ namespace config {define_field("disable_custom_fonts", field_type::boolean, false)}, {define_field("language", field_type::string, language::get_default_language(), language::is_valid_language)}, {define_field("motd_last_seen", field_type::number_unsigned, 0)}, + {define_field("branch", field_type::string, updater::get_git_branch(), updater::is_valid_git_branch)}, }; std::string get_config_file_path() diff --git a/src/client/component/ui_scripting.cpp b/src/client/component/ui_scripting.cpp index f6fa713f..e95414d8 100644 --- a/src/client/component/ui_scripting.cpp +++ b/src/client/component/ui_scripting.cpp @@ -446,6 +446,20 @@ namespace ui_scripting updater_table["getlasterror"] = updater::get_last_error; updater_table["getcurrentfile"] = updater::get_current_file; + updater_table["getcurrentbranch"] = []() + { + const auto branch = updater::get_current_branch(); + return static_cast(branch); + }; + + updater_table["setbranch"] = [](const std::uint32_t branch) + { + updater::set_branch(static_cast(branch)); + }; + + updater_table["develop"] = static_cast(updater::branch_develop); + updater_table["main"] = static_cast(updater::branch_main); + auto mods_table = table(); lua["mods"] = mods_table; diff --git a/src/client/component/updater.cpp b/src/client/component/updater.cpp index b2853b51..da66b769 100644 --- a/src/client/component/updater.cpp +++ b/src/client/component/updater.cpp @@ -7,6 +7,7 @@ #include "console.hpp" #include "command.hpp" #include "database.hpp" +#include "config.hpp" #include "version.h" @@ -72,11 +73,33 @@ namespace updater utils::concurrency::container update_data; + std::unordered_map git_branches = + { + {"develop", branch_develop}, + {"main", branch_main}, + }; + + std::string get_branch_name(const git_branch branch) + { + for (const auto& [name, b] : git_branches) + { + if (branch == b) + { + return name; + } + } + + throw std::runtime_error("invalid branch"); + } + std::string select(const std::string& main, const std::string& develop) { - if (GIT_BRANCH == "develop"s) + switch (updater::get_current_branch()) { + case branch_develop: return develop; + case branch_main: + return main; } return main; @@ -579,6 +602,45 @@ namespace updater return !utils::io::directory_exists(folder) || utils::io::directory_is_empty(folder); } + bool is_valid_git_branch(const std::string& branch) + { + return git_branches.contains(branch); + } + + std::string get_git_branch() + { + return GIT_BRANCH; + } + + git_branch get_current_branch() + { + const auto get_branch_name = []() + -> std::string + { + const auto branch_opt = config::get("branch"); + if (!branch_opt.has_value()) + { + return GIT_BRANCH; + } + + return branch_opt.value(); + }; + + const auto branch_name = get_branch_name(); + return git_branches.at(branch_name); + } + + void set_branch(const git_branch branch) + { + if (branch >= branch_count) + { + return; + } + + const auto name = get_branch_name(branch); + config::set("branch", name); + } + class component final : public component_interface { public: diff --git a/src/client/component/updater.hpp b/src/client/component/updater.hpp index a72e34b4..d8caf989 100644 --- a/src/client/component/updater.hpp +++ b/src/client/component/updater.hpp @@ -4,6 +4,13 @@ namespace updater { + enum git_branch + { + branch_develop, + branch_main, + branch_count + }; + std::optional get_server_file(const std::string& endpoint); void relaunch(); @@ -29,4 +36,9 @@ namespace updater void cancel_update(); bool should_force_update(); + + bool is_valid_git_branch(const std::string& branch); + std::string get_git_branch(); + git_branch get_current_branch(); + void set_branch(const git_branch branch); } \ No newline at end of file