diff --git a/src/cvwrapper.cpp b/src/cvwrapper.cpp index 50c42572..b70cd9f9 100644 --- a/src/cvwrapper.cpp +++ b/src/cvwrapper.cpp @@ -12,6 +12,74 @@ int CatVar::last_id { 0 }; +int rebased_count { 0 }; + +int GetRebasedCatVarCount() { + return rebased_count; +} + +static CatCommand cfg_rebase("cfg_setbase", "Rebase config", []() { + for (auto& cv : CatVarList()) { + std::string value(cv->GetString()); + if (value != cv->defaults) { + cv->current_base = value; + rebased_count++; + } + } + logging::Info("Successfully rebased %d variables", rebased_count); +}); + +static CatCommand cfg_resetbase("cfg_resetbase", "Reset config base", []() { + for (auto& cv : CatVarList()) { + cv->current_base = cv->defaults; + } + rebased_count = 0; +}); + +static CatCommand save_settings("save", "Save settings (optional filename)", [](const CCommand& args) { + std::string filename("lastcfg"); + if (args.ArgC() > 1) { + filename = std::string(args.Arg(1)); + } + std::string path = format("tf/cfg/cat_", filename, ".cfg"); + logging::Info("Saving settings to %s", path.c_str()); + if (GetRebasedCatVarCount()) { + logging::Info("[Warning] %d CatVars are rebased!", GetRebasedCatVarCount()); + } + std::ofstream file(path, std::ios::out); + if (file.bad()) { + logging::Info("Couldn't open the file!"); + return; + } + for (const auto& i : CatVarList()) { + if (i->GetBase() != std::string(i->GetString())) { + file << CON_PREFIX << i->name << " \"" << i->GetString() << "\"\n"; + } + } + file.close(); +}); + +static CatCommand save_settings_complete("save_complete", "Save all settings (optional filename)", [](const CCommand& args) { + std::string filename("lastcfg"); + if (args.ArgC() > 1) { + filename = std::string(args.Arg(1)); + } + std::string path = format("tf/cfg/cat_", filename, ".cfg"); + logging::Info("Saving settings to %s", path.c_str()); + if (GetRebasedCatVarCount()) { + logging::Info("[Warning] %d CatVars are rebased!", GetRebasedCatVarCount()); + } + std::ofstream file(path, std::ios::out); + if (file.bad()) { + logging::Info("Couldn't open the file!"); + return; + } + for (const auto& i : CatVarList()) { + file << CON_PREFIX << i->name << " \"" << i->GetString() << "\"\n"; + } + file.close(); +}); + // Prevent initialization errors. std::vector& registrationArray() { static std::vector vector; @@ -94,6 +162,7 @@ void CatVar::Register() { id = last_id++; convar = CreateConVar(CON_PREFIX + name, defaults, desc_short); convar_parent = convar->m_pParent; + current_base = defaults; while (!callbacks.empty()) { callbacks.back()(this); callbacks.pop_back(); diff --git a/src/cvwrapper.h b/src/cvwrapper.h index 19fcd262..aa0602d3 100644 --- a/src/cvwrapper.h +++ b/src/cvwrapper.h @@ -94,6 +94,10 @@ public: return g_IInputSystem->IsButtonDown(static_cast(static_cast(*this))); } + inline const std::string& GetBase() const { + return current_base; + } + public: const CatVar_t type; const std::string name; @@ -102,6 +106,8 @@ public: const std::string desc_long { "" }; const CatEnum* const enum_type { nullptr }; + std::string current_base { "0" }; + bool restricted { false }; float min { 0.0f }; float max { 0.0f }; @@ -121,5 +127,6 @@ std::vector& commandRegistrationArray(); std::vector& CatVarList(); void RegisterCatVars(); +int GetRebasedCatVarCount(); #endif /* CVWRAPPER_H_ */ diff --git a/src/hacks/Misc.cpp b/src/hacks/Misc.cpp index f8adb551..9ce8d713 100644 --- a/src/hacks/Misc.cpp +++ b/src/hacks/Misc.cpp @@ -485,27 +485,6 @@ CatCommand name("name_set", "Immediate name change", [](const CCommand& args) { ch->SendNetMsg(setname, false); } }); -CatCommand save_settings("save", "Save settings (optional filename)", [](const CCommand& args) { - std::string filename("lastcfg"); - if (args.ArgC() > 1) { - filename = std::string(args.Arg(1)); - } - std::string path = format("tf/cfg/cat_", filename, ".cfg"); - logging::Info("Saving settings to %s", path.c_str()); - std::ofstream file(path, std::ios::out); - if (file.bad()) { - logging::Info("Couldn't open the file!"); - return; - } - for (const auto& i : RegisteredVarsList()) { - if (i) { - if (strcmp(i->GetString(), i->GetDefault())) { - file << i->GetName() << " \"" << i->GetString() << "\"\n"; - } - } - } - file.close(); -}); CatCommand say_lines("say_lines", "Say with newlines (\\n)", [](const CCommand& args) { std::string message(args.ArgS()); ReplaceString(message, "\\n", "\n");