Skip to content

Commit

Permalink
LUS Cleanup: Removes GameSettings class. Moves code to Imgui. (Harbou…
Browse files Browse the repository at this point in the history
…rMasters#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
  • Loading branch information
Kenix3 authored Aug 5, 2022
1 parent c23457d commit 93d0d74
Show file tree
Hide file tree
Showing 16 changed files with 189 additions and 247 deletions.
7 changes: 3 additions & 4 deletions ZAPDTR/ZAPDUtils/Utils/Directory.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once

#include <iostream>
#include <string>
#include <vector>
#include "StringHelper.h"
#include <iostream>

#if __has_include(<filesystem>)
#include <filesystem>
Expand All @@ -12,16 +13,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().string(); }
#endif

static bool Exists(const fs::path& path) { return fs::exists(path); }
Expand Down
125 changes: 125 additions & 0 deletions libultraship/libultraship/Cvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
31 changes: 17 additions & 14 deletions libultraship/libultraship/Cvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,23 @@ 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);
void CVar_SetS32(const char* name, int32_t value);
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);
void CVar_SetRGBA(const char* name, Color_RGBA8 value);

void CVar_RegisterS32(const char* name, int32_t defaultValue);
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);
float CVar_GetFloat(const char* name, float defaultValue);
void CVar_SetFloat(const char* name, float value);
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);
void CVar_SetRGBA(const char* name, Color_RGBA8 value);

void CVar_RegisterS32(const char* name, int32_t defaultValue);
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
};
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
3 changes: 1 addition & 2 deletions libultraship/libultraship/Lib/Fast3D/gfx_dxgi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)) {
Expand Down
Loading

0 comments on commit 93d0d74

Please sign in to comment.