LUS Cleanup: Removes GameSettings class. Moves code to Imgui. (#1036)

* LUS Cleanup: Removes GameSettings class. Moves code to Imgui.

* Fixes more strdup problems and finalized removal of GameSetting.

* Reverts changes to Directory.h

* Update Directory.h

* Fixes PR.

* Update Directory.h

* Update rando_main.cpp
This commit is contained in:
Kenix3 2022-08-05 01:03:11 -04:00 committed by GitHub
parent c23457d666
commit 93d0d7443a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 188 additions and 246 deletions

View File

@ -1,8 +1,9 @@
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include "StringHelper.h"
#include <iostream>
#if __has_include(<filesystem>)
#include <filesystem>
@ -12,8 +13,6 @@ namespace fs = std::filesystem;
namespace fs = std::experimental::filesystem;
#endif
#include "StringHelper.h"
#undef GetCurrentDirectory
#undef CreateDirectory
@ -21,7 +20,7 @@ class Directory
{
public:
#ifndef PATH_HACK
static std::string GetCurrentDirectory() { return fs::current_path().u8string().c_str(); }
static std::string GetCurrentDirectory() { return fs::current_path().string(); }
#endif
static bool Exists(const fs::path& path) { return fs::exists(path); }

View File

@ -4,6 +4,8 @@
#include <functional>
#include <memory>
#include <utility>
#include <Utils/File.h>
#include "GlobalCtx2.h"
std::map<std::string, std::unique_ptr<CVar>, std::less<>> cvars;
@ -135,3 +137,126 @@ extern "C" void CVar_RegisterString(const char* name, const char* defaultValue)
if (!CVar_Get(name))
CVar_SetString(name, defaultValue);
}
template <typename Numeric> bool is_number(const std::string& s) {
Numeric n;
return ((std::istringstream(s) >> n >> std::ws).eof());
}
void CVar_LoadLegacy() {
auto cvarsConfig = Ship::GlobalCtx2::GetPathRelativeToAppDirectory("cvars.cfg");
if (File::Exists(cvarsConfig)) {
const auto lines = File::ReadAllLines(cvarsConfig);
for (const std::string& line : lines) {
std::vector<std::string> cfg = StringHelper::Split(line, " = ");
if (line.empty()) continue;
if (cfg.size() < 2) continue;
if (cfg[1].find("\"") == std::string::npos && (cfg[1].find("#") != std::string::npos))
{
std::string value(cfg[1]);
value.erase(std::remove_if(value.begin(), value.end(), [](char c) { return c == '#'; }), value.end());
auto splitTest = StringHelper::Split(value, "\r")[0];
uint32_t val = std::stoul(splitTest, nullptr, 16);
Color_RGBA8 clr;
clr.r = val >> 24;
clr.g = val >> 16;
clr.b = val >> 8;
clr.a = val & 0xFF;
CVar_SetRGBA(cfg[0].c_str(), clr);
}
if (cfg[1].find("\"") != std::string::npos) {
std::string value(cfg[1]);
value.erase(std::remove(value.begin(), value.end(), '\"'), value.end());
#ifdef _MSC_VER
CVar_SetString(cfg[0].c_str(), _strdup(value.c_str()));
#else
CVar_SetString(cfg[0].c_str(), strdup(value.c_str()));
#endif
}
if (is_number<float>(cfg[1])) {
CVar_SetFloat(cfg[0].c_str(), std::stof(cfg[1]));
}
if (is_number<int>(cfg[1])) {
CVar_SetS32(cfg[0].c_str(), std::stoi(cfg[1]));
}
}
fs::remove(cvarsConfig);
}
}
extern "C" void CVar_Load() {
std::shared_ptr<Mercury> pConf = Ship::GlobalCtx2::GetInstance()->GetConfig();
pConf->reload();
for (const auto& item : pConf->rjson["CVars"].items()) {
auto value = item.value();
switch (value.type()) {
case nlohmann::detail::value_t::array:
break;
case nlohmann::detail::value_t::object:
if (value["Type"].get<std::string>() == mercuryRGBAObjectType) {
Color_RGBA8 clr;
clr.r = value["R"].get<uint8_t>();
clr.g = value["G"].get<uint8_t>();
clr.b = value["B"].get<uint8_t>();
clr.a = value["A"].get<uint8_t>();
CVar_SetRGBA(item.key().c_str(), clr);
}
break;
case nlohmann::detail::value_t::string:
CVar_SetString(item.key().c_str(), value.get<std::string>().c_str());
break;
case nlohmann::detail::value_t::boolean:
CVar_SetS32(item.key().c_str(), value.get<bool>());
break;
case nlohmann::detail::value_t::number_unsigned:
case nlohmann::detail::value_t::number_integer:
CVar_SetS32(item.key().c_str(), value.get<int>());
break;
case nlohmann::detail::value_t::number_float:
CVar_SetFloat(item.key().c_str(), value.get<float>());
break;
default:;
}
if (item.key() == "gOpenMenuBar") {
int bp = 0;
}
}
CVar_LoadLegacy();
}
extern "C" void CVar_Save()
{
std::shared_ptr<Mercury> pConf = Ship::GlobalCtx2::GetInstance()->GetConfig();
for (const auto& cvar : cvars) {
const std::string key = StringHelper::Sprintf("CVars.%s", cvar.first.c_str());
if (cvar.second->type == CVarType::String && cvar.second->value.valueStr != nullptr)
pConf->setString(key, std::string(cvar.second->value.valueStr));
else if (cvar.second->type == CVarType::S32)
pConf->setInt(key, cvar.second->value.valueS32);
else if (cvar.second->type == CVarType::Float)
pConf->setFloat(key, cvar.second->value.valueFloat);
else if (cvar.second->type == CVarType::RGBA)
{
auto keyStr = key.c_str();
Color_RGBA8 clr = cvar.second->value.valueRGBA;
pConf->setUInt(StringHelper::Sprintf("%s.R", keyStr), clr.r);
pConf->setUInt(StringHelper::Sprintf("%s.G", keyStr), clr.r);
pConf->setUInt(StringHelper::Sprintf("%s.B", keyStr), clr.r);
pConf->setUInt(StringHelper::Sprintf("%s.A", keyStr), clr.r);
pConf->setString(StringHelper::Sprintf("%s.Type", keyStr), mercuryRGBAObjectType);
}
}
pConf->save();
}

View File

@ -33,11 +33,11 @@ extern "C"
{
#endif
int32_t CVar_GetS32(const char* name, int32_t defaultValue);
float CVar_GetFloat(const char* name, float defaultValue);
void CVar_SetFloat(const char* name, float value);
const char* CVar_GetString(const char* name, const char* defaultValue);
int32_t CVar_GetS32(const char* name, int32_t defaultValue);
void CVar_SetS32(const char* name, int32_t value);
const char* CVar_GetString(const char* name, const char* defaultValue);
void CVar_SetString(const char* name, const char* value);
Color_RGB8 CVar_GetRGB(const char* name, Color_RGB8 defaultValue);
Color_RGBA8 CVar_GetRGBA(const char* name, Color_RGBA8 defaultValue);
@ -48,6 +48,9 @@ void CVar_RegisterFloat(const char* name, float defaultValue);
void CVar_RegisterString(const char* name, const char* defaultValue);
void CVar_RegisterRGBA(const char* name, Color_RGBA8 defaultValue);
void CVar_Load();
void CVar_Save();
#ifdef __cplusplus
};
#endif

View File

@ -1,56 +0,0 @@
#include "GameSettings.h"
// Audio
#include <cstddef>
#include <PR/ultra64/types.h>
#include <PR/ultra64/sptask.h>
#include <PR/ultra64/pi.h>
#include <PR/ultra64/message.h>
#include "Cvar.h"
#include "GlobalCtx2.h"
#include "ImGuiImpl.h"
#include "../../soh/include/z64audio.h"
#include "Hooks.h"
#include "../../soh/soh/Enhancements/debugconsole.h"
#include "Window.h"
#include "Lib/Fast3D/gfx_rendering_api.h"
#define ABS(var) var < 0 ? -(var) : var
using namespace Ship;
namespace Game {
bool DeSyncAudio = false;
void UpdateAudio() {
Audio_SetGameVolume(SEQ_BGM_MAIN, CVar_GetFloat("gMainMusicVolume", 1));
Audio_SetGameVolume(SEQ_BGM_SUB, CVar_GetFloat("gSubMusicVolume", 1));
Audio_SetGameVolume(SEQ_FANFARE, CVar_GetFloat("gSFXMusicVolume", 1));
Audio_SetGameVolume(SEQ_SFX, CVar_GetFloat("gFanfareVolume", 1));
}
void LoadSettings() {
DebugConsole_LoadCVars();
}
void SaveSettings() {
DebugConsole_SaveCVars();
}
void InitSettings() {
Ship::RegisterHook<Ship::AudioInit>(UpdateAudio);
Ship::RegisterHook<Ship::GfxInit>([] {
gfx_get_current_rendering_api()->set_texture_filter((FilteringMode) CVar_GetS32("gTextureFilter", FILTER_THREE_POINT));
SohImGui::console->opened = CVar_GetS32("gConsoleEnabled", 0);
SohImGui::controller->Opened = CVar_GetS32("gControllerConfigurationEnabled", 0);
UpdateAudio();
});
}
void SetSeqPlayerVolume(SeqPlayers playerId, float volume) {
Audio_SetGameVolume(playerId, volume);
}
}

View File

@ -1,17 +0,0 @@
#pragma once
enum SeqPlayers {
/* 0 */ SEQ_BGM_MAIN,
/* 1 */ SEQ_FANFARE,
/* 2 */ SEQ_SFX,
/* 3 */ SEQ_BGM_SUB,
/* 4 */ SEQ_MAX
};
namespace Game {
void InitSettings();
void LoadSettings();
void LoadPadSettings();
void SaveSettings();
void SetSeqPlayerVolume(SeqPlayers playerId, float volume);
}

View File

@ -7,8 +7,13 @@
#include <algorithm>
#include <vector>
#include <cstddef>
#include <PR/ultra64/types.h>
#include <PR/ultra64/sptask.h>
#include <PR/ultra64/pi.h>
#include <PR/ultra64/message.h>
#include "../../soh/include/z64audio.h"
#include "Archive.h"
#include "GameSettings.h"
#include "Console.h"
#include "Hooks.h"
#define IMGUI_DEFINE_MATH_OPERATORS
@ -68,6 +73,14 @@ std::vector<std::string> emptyArgs;
bool isBetaQuestEnabled = false;
enum SeqPlayers {
/* 0 */ SEQ_BGM_MAIN,
/* 1 */ SEQ_FANFARE,
/* 2 */ SEQ_SFX,
/* 3 */ SEQ_BGM_SUB,
/* 4 */ SEQ_MAX
};
extern "C" {
void enableBetaQuest() { isBetaQuestEnabled = true; }
void disableBetaQuest() { isBetaQuestEnabled = false; }
@ -115,6 +128,23 @@ namespace SohImGui {
std::map<std::string, std::vector<std::string>> windowCategories;
std::map<std::string, CustomWindow> customWindows;
void UpdateAudio() {
Audio_SetGameVolume(SEQ_BGM_MAIN, CVar_GetFloat("gMainMusicVolume", 1));
Audio_SetGameVolume(SEQ_BGM_SUB, CVar_GetFloat("gSubMusicVolume", 1));
Audio_SetGameVolume(SEQ_FANFARE, CVar_GetFloat("gSFXMusicVolume", 1));
Audio_SetGameVolume(SEQ_SFX, CVar_GetFloat("gFanfareVolume", 1));
}
void InitSettings() {
Ship::RegisterHook<Ship::AudioInit>(UpdateAudio);
Ship::RegisterHook<Ship::GfxInit>([] {
gfx_get_current_rendering_api()->set_texture_filter((FilteringMode)CVar_GetS32("gTextureFilter", FILTER_THREE_POINT));
SohImGui::console->opened = CVar_GetS32("gConsoleEnabled", 0);
SohImGui::controller->Opened = CVar_GetS32("gControllerConfigurationEnabled", 0);
UpdateAudio();
});
}
int GetBackendID(std::shared_ptr<Mercury> cfg) {
std::string backend = cfg->getString("Window.GfxBackend");
if (backend.empty()) {
@ -347,7 +377,7 @@ namespace SohImGui {
}
void Init(WindowImpl window_impl) {
Game::LoadSettings();
CVar_Load();
impl = window_impl;
ImGuiContext* ctx = ImGui::CreateContext();
ImGui::SetCurrentContext(ctx);
@ -405,7 +435,7 @@ namespace SohImGui {
pads = cont_pad;
});
Game::InitSettings();
InitSettings();
CVar_SetS32("gRandoGenerating", 0);
CVar_SetS32("gNewSeedGenerated", 0);
@ -419,7 +449,7 @@ namespace SohImGui {
void Update(EventImpl event) {
if (needs_save) {
Game::SaveSettings();
CVar_Save();
needs_save = false;
}
ImGuiProcessEvent(event);
@ -436,11 +466,10 @@ namespace SohImGui {
const float volume = floorf(value * 100) / 100;
CVar_SetFloat(key, volume);
needs_save = true;
Game::SetSeqPlayerVolume(playerId, volume);
Audio_SetGameVolume(playerId, volume);
}
}
void EnhancementCombobox(const char* name, const char* ComboArray[], size_t arraySize, uint8_t FirstTimeValue = 0) {
if (FirstTimeValue <= 0) {
FirstTimeValue = 0;

View File

@ -32,7 +32,6 @@
#define DECLARE_GFX_DXGI_FUNCTIONS
#include "gfx_dxgi.h"
#include "../../GameSettings.h"
#define WINCLASS_NAME L"N64GAME"
#define GFX_API_NAME "DirectX"
@ -274,7 +273,7 @@ static LRESULT CALLBACK gfx_dxgi_wnd_proc(HWND h_wnd, UINT message, WPARAM w_par
DragQueryFileA((HDROP)w_param, 0, fileName, 256);
CVar_SetString("gDroppedFile", fileName);
CVar_SetS32("gNewFileDropped", 1);
Game::SaveSettings();
CVar_Save();
break;
case WM_SYSKEYDOWN:
if ((w_param == VK_RETURN) && ((l_param & 1 << 30) == 0)) {

View File

@ -35,8 +35,6 @@
#ifdef _WIN32
#include <WTypesbase.h>
#endif
#include <time.h>
#include "../../GameSettings.h"
#define GFX_API_NAME "SDL2 - OpenGL"
@ -306,7 +304,7 @@ static void gfx_sdl_handle_events(void) {
case SDL_DROPFILE:
CVar_SetString("gDroppedFile", event.drop.file);
CVar_SetS32("gNewFileDropped", 1);
Game::SaveSettings();
CVar_Save();
break;
case SDL_QUIT:
Ship::ExecuteHooks<Ship::ExitGame>();

View File

@ -9,7 +9,6 @@
#include <spdlog/sinks/base_sink.h>
#include <spdlog/details/synchronous_factory.h>
#include "ImGuiImpl.h"
#include "GameSettings.h"
#include "Cvar.h"
#include <chrono>
#include <mutex>

View File

@ -261,7 +261,6 @@
<ClCompile Include="Factories\AudioFactory.cpp" />
<ClCompile Include="InputEditor.cpp" />
<ClCompile Include="GameOverlay.cpp" />
<ClCompile Include="GameSettings.cpp" />
<ClCompile Include="Lib\ImGui\backends\imgui_impl_dx11.cpp" />
<ClCompile Include="Lib\ImGui\backends\imgui_impl_win32.cpp" />
<ClCompile Include="Lib\Mercury\Mercury.cpp" />
@ -353,7 +352,6 @@
<ClInclude Include="Factories\AudioFactory.h" />
<ClInclude Include="InputEditor.h" />
<ClInclude Include="GameOverlay.h" />
<ClInclude Include="GameSettings.h" />
<ClInclude Include="GameVersions.h" />
<ClInclude Include="Lib\dr_libs\mp3.h" />
<ClInclude Include="Lib\dr_libs\wav.h" />

View File

@ -318,9 +318,6 @@
<ClCompile Include="luslog.cpp">
<Filter>Source Files\Logging</Filter>
</ClCompile>
<ClCompile Include="GameSettings.cpp">
<Filter>Source Files\CustomImpl</Filter>
</ClCompile>
<ClCompile Include="Audio.cpp">
<Filter>Source Files\Resources\Files</Filter>
</ClCompile>
@ -608,9 +605,6 @@
<ClInclude Include="luslog.h">
<Filter>Source Files\Logging</Filter>
</ClInclude>
<ClInclude Include="GameSettings.h">
<Filter>Source Files\CustomImpl</Filter>
</ClInclude>
<ClInclude Include="GameVersions.h">
<Filter>Source Files\Resources</Filter>
</ClInclude>

View File

@ -1,11 +1,6 @@
#include "CosmeticsEditor.h"
#include "../../util.h"
#include "../libultraship/ImGuiImpl.h"
#include "GameSettings.h"
#include <array>
#include <bit>
#include <map>
#include <string>
#include <Cvar.h>
#include <PR/ultra64/types.h>

View File

@ -14,7 +14,6 @@
#define Path _Path
#define PATH_HACK
#include <Utils/StringHelper.h>
#include <Utils/File.h>
#include "Window.h"
#include "Lib/ImGui/imgui_internal.h"
@ -426,7 +425,7 @@ static bool SetCVarHandler(const std::vector<std::string>& args) {
else
CVar_SetS32(args[1].c_str(), std::stoi(args[2]));
DebugConsole_SaveCVars();
CVar_Save();
//INFO("[SOH] Updated player position to [ %.2f, %.2f, %.2f ]", pos.x, pos.y, pos.z);
return CMD_SUCCESS;
@ -507,123 +506,5 @@ void DebugConsole_Init(void) {
Ship::ArgumentType::NUMBER,
}
} });
DebugConsole_LoadCVars();
}
template <typename Numeric> bool is_number(const std::string& s) {
Numeric n;
return ((std::istringstream(s) >> n >> std::ws).eof());
}
void DebugConsole_LoadLegacyCVars() {
auto cvarsConfig = Ship::GlobalCtx2::GetPathRelativeToAppDirectory("cvars.cfg");
if (File::Exists(cvarsConfig)) {
const auto lines = File::ReadAllLines(cvarsConfig);
for (const std::string& line : lines) {
std::vector<std::string> cfg = StringHelper::Split(line, " = ");
if (line.empty()) continue;
if (cfg.size() < 2) continue;
if (cfg[1].find("\"") == std::string::npos && (cfg[1].find("#") != std::string::npos))
{
std::string value(cfg[1]);
value.erase(std::remove_if(value.begin(), value.end(), [](char c) { return c == '#'; }), value.end());
auto splitTest = StringHelper::Split(value, "\r")[0];
uint32_t val = std::stoul(splitTest, nullptr, 16);
Color_RGBA8 clr;
clr.r = val >> 24;
clr.g = val >> 16;
clr.b = val >> 8;
clr.a = val & 0xFF;
CVar_SetRGBA(cfg[0].c_str(), clr);
}
if (cfg[1].find("\"") != std::string::npos) {
std::string value(cfg[1]);
value.erase(std::remove(value.begin(), value.end(), '\"'), value.end());
CVar_SetString(cfg[0].c_str(), ImStrdup(value.c_str()));
}
if (is_number<float>(cfg[1])) {
CVar_SetFloat(cfg[0].c_str(), std::stof(cfg[1]));
}
if (is_number<int>(cfg[1])) {
CVar_SetS32(cfg[0].c_str(), std::stoi(cfg[1]));
}
}
fs::remove(cvarsConfig);
}
}
void DebugConsole_LoadCVars() {
std::shared_ptr<Mercury> pConf = Ship::GlobalCtx2::GetInstance()->GetConfig();
pConf->reload();
for (const auto& item : pConf->rjson["CVars"].items()) {
auto value = item.value();
switch (value.type()) {
case nlohmann::detail::value_t::array:
break;
case nlohmann::detail::value_t::object:
if (value["Type"].get<std::string>() == mercuryRGBAObjectType) {
Color_RGBA8 clr;
clr.r = value["R"].get<uint8_t>();
clr.g = value["G"].get<uint8_t>();
clr.b = value["B"].get<uint8_t>();
clr.a = value["A"].get<uint8_t>();
}
break;
case nlohmann::detail::value_t::string:
CVar_SetString(item.key().c_str(), value.get<std::string>().c_str());
break;
case nlohmann::detail::value_t::boolean:
CVar_SetS32(item.key().c_str(), value.get<bool>());
break;
case nlohmann::detail::value_t::number_unsigned:
case nlohmann::detail::value_t::number_integer:
CVar_SetS32(item.key().c_str(), value.get<int>());
break;
case nlohmann::detail::value_t::number_float:
CVar_SetFloat(item.key().c_str(), value.get<float>());
break;
default: ;
}
if (item.key() == "gOpenMenuBar") {
int bp = 0;
}
}
DebugConsole_LoadLegacyCVars();
}
void DebugConsole_SaveCVars()
{
std::shared_ptr<Mercury> pConf = Ship::GlobalCtx2::GetInstance()->GetConfig();
for (const auto &cvar : cvars) {
const std::string key = StringHelper::Sprintf("CVars.%s", cvar.first.c_str());
if (cvar.second->type == CVarType::String && cvar.second->value.valueStr != nullptr)
pConf->setString(key, std::string(cvar.second->value.valueStr));
else if (cvar.second->type == CVarType::S32)
pConf->setInt(key, cvar.second->value.valueS32);
else if (cvar.second->type == CVarType::Float)
pConf->setFloat(key, cvar.second->value.valueFloat);
else if (cvar.second->type == CVarType::RGBA)
{
auto keyStr = key.c_str();
Color_RGBA8 clr = cvar.second->value.valueRGBA;
pConf->setUInt(StringHelper::Sprintf("%s.R", keyStr), clr.r);
pConf->setUInt(StringHelper::Sprintf("%s.G", keyStr), clr.r);
pConf->setUInt(StringHelper::Sprintf("%s.B", keyStr), clr.r);
pConf->setUInt(StringHelper::Sprintf("%s.A", keyStr), clr.r);
pConf->setString(StringHelper::Sprintf("%s.Type", keyStr), mercuryRGBAObjectType);
}
}
pConf->save();
CVar_Load();
}

View File

@ -1,5 +1,3 @@
#pragma once
void DebugConsole_Init(void);
void DebugConsole_LoadCVars();
void DebugConsole_SaveCVars();

View File

@ -6,7 +6,6 @@
#include "rando_main.hpp"
// #include <soh/Enhancements/randomizer.h>
#include <Cvar.h>
#include <GameSettings.h>
#define NOGDI
#define WIN32_LEAN_AND_MEAN
#include <GlobalCtx2.h>
@ -25,7 +24,7 @@ void RandoMain::GenerateRando(std::unordered_map<RandomizerSettingKey, u8> cvarS
std::string fileName = Ship::GlobalCtx2::GetPathRelativeToAppDirectory(GenerateRandomizer(cvarSettings).c_str());
CVar_SetString("gSpoilerLog", fileName.c_str());
Game::SaveSettings();
Game::LoadSettings();
CVar_Save();
CVar_Load();
CVar_SetS32("gNewSeedGenerated", 1);
}

View File

@ -8,7 +8,6 @@
#include <Cvar.h>
#include <textures/icon_item_static/icon_item_static.h>
#include <textures/icon_item_24_static/icon_item_24_static.h>
#include <GameSettings.h>
#include "../libultraship/ImGuiImpl.h"
#include <thread>
#include "3drando/rando_main.hpp"
@ -3192,7 +3191,7 @@ std::thread randoThread;
void GenerateRandomizerImgui() {
CVar_SetS32("gRandoGenerating", 1);
Game::SaveSettings();
CVar_Save();
std::unordered_map<RandomizerSettingKey, u8> cvarSettings;
cvarSettings[RSK_FOREST] = CVar_GetS32("gRandomizeForest", 1);
@ -3258,9 +3257,8 @@ void GenerateRandomizerImgui() {
RandoMain::GenerateRando(cvarSettings);
CVar_SetS32("gRandoGenerating", 0);
Game::SaveSettings();
Game::LoadSettings();
CVar_Save();
CVar_Load();
generated = 1;
}