Skip to content

Commit

Permalink
LUS Cleanup: Removes GameSettings class. Moves code to Imgui.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenix3 committed Aug 4, 2022
1 parent 97adc4a commit 2f5bd9a
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 224 deletions.
5 changes: 1 addition & 4 deletions ZAPDTR/ZAPDUtils/Utils/Directory.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include <iostream>
#include <string>
#include <vector>

Expand All @@ -12,16 +11,14 @@ namespace fs = std::filesystem;
namespace fs = std::experimental::filesystem;
#endif

#include "StringHelper.h"

#undef GetCurrentDirectory
#undef CreateDirectory

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().generic_string(); }
#endif

static bool Exists(const fs::path& path) { return fs::exists(path); }
Expand Down
119 changes: 119 additions & 0 deletions libultraship/libultraship/Cvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <functional>
#include <memory>
#include <utility>
#include <Utils/File.h>

std::map<std::string, std::unique_ptr<CVar>, std::less<>> cvars;

Expand Down Expand Up @@ -135,3 +136,121 @@ 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());
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);
}
}


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>();
}

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();
}
3 changes: 3 additions & 0 deletions libultraship/libultraship/Cvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 0 additions & 56 deletions libultraship/libultraship/GameSettings.cpp

This file was deleted.

17 changes: 0 additions & 17 deletions libultraship/libultraship/GameSettings.h

This file was deleted.

41 changes: 35 additions & 6 deletions libultraship/libultraship/ImGuiImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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; }
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -405,7 +435,7 @@ namespace SohImGui {
pads = cont_pad;
});

Game::InitSettings();
InitSettings();

CVar_SetS32("gRandoGenerating", 0);
CVar_SetS32("gNewSeedGenerated", 0);
Expand All @@ -419,7 +449,7 @@ namespace SohImGui {

void Update(EventImpl event) {
if (needs_save) {
Game::SaveSettings();
CVar_Save();
needs_save = false;
}
ImGuiProcessEvent(event);
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion libultraship/libultraship/Lib/Fast3D/gfx_dxgi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,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)) {
Expand Down
2 changes: 1 addition & 1 deletion libultraship/libultraship/Lib/Fast3D/gfx_sdl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,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>();
Expand Down
2 changes: 0 additions & 2 deletions libultraship/libultraship/libultraship.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down Expand Up @@ -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" />
Expand Down
6 changes: 0 additions & 6 deletions libultraship/libultraship/libultraship.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
Loading

0 comments on commit 2f5bd9a

Please sign in to comment.