Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] CVar rework #2552

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions soh/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ set(Header_Files__include
"include/z64player.h"
"include/z64save.h"
"include/z64scene.h"
"include/z64settings.h"
"include/z64transition.h"
)
source_group("Header Files\\include" FILES ${Header_Files__include})
Expand Down Expand Up @@ -301,6 +302,8 @@ set(Source_Files__soh
"soh/UIWidgets.hpp"
"soh/UIWidgets.cpp"
"soh/CrashHandlerExt.cpp"
"soh/GlobalSettings.h"
"soh/GlobalSettings.cpp"
)
source_group("Source Files\\soh" FILES ${Source_Files__soh})

Expand Down
1 change: 1 addition & 0 deletions soh/include/variables.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ extern "C"
extern u64 gJpegUCodeData[];

extern SaveContext gSaveContext;
extern GlobalSettingsStruct gGlobalSettings;
extern GameInfo* gGameInfo;
extern u16 D_8015FCC0;
extern u16 D_8015FCC2;
Expand Down
1 change: 1 addition & 0 deletions soh/include/z64.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "z64skin.h"
#include "z64transition.h"
#include "z64interface.h"
#include "z64settings.h"
#include "alignment.h"
#include "sequence.h"
#include "sfx.h"
Expand Down
3 changes: 0 additions & 3 deletions soh/include/z64save.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,7 @@ typedef struct {
/* 0x1404 */ u16 minigameState;
/* 0x1406 */ u16 minigameScore; // "yabusame_total"
/* 0x1408 */ char unk_1408[0x0001];
/* 0x1409 */ u8 language; // NTSC 0: Japanese; 1: English | PAL 0: English; 1: German; 2: French
/* 0x140A */ u8 audioSetting;
/* 0x140B */ char unk_140B[0x0001];
/* 0x140C */ u8 zTargetSetting; // 0: Switch; 1: Hold
/* 0x140E */ u16 forcedSeqId; // immediately start playing the sequence if set
/* 0x1410 */ u8 cutsceneTransitionControl; // context dependent usage: can either trigger a delayed fade or control fill alpha
/* 0x1411 */ char unk_1411[0x0001];
Expand Down
12 changes: 12 additions & 0 deletions soh/include/z64settings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef Z64SETTINGS_H
#define Z64SETTINGS_H

#include <libultraship/libultra.h>

typedef struct {
s32 language; // NTSC 0: Japanese; 1: English | PAL 0: English; 1: German; 2: French
s32 zTargetSetting;
s32 audioSetting;
} GlobalSettingsStruct;

#endif
16 changes: 8 additions & 8 deletions soh/soh/Enhancements/debugger/debugSaveEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ void DrawInfoTab() {
UIWidgets::InsertHelpHoverText("Time, in seconds");

const char* audioName;
switch (gSaveContext.audioSetting) {
switch (gGlobalSettings.audioSetting) {
case 0:
audioName = "Stereo";
break;
Expand All @@ -475,16 +475,16 @@ void DrawInfoTab() {
}
if (ImGui::BeginCombo("Audio", audioName)) {
if (ImGui::Selectable("Stereo")) {
gSaveContext.audioSetting = 0;
gGlobalSettings.audioSetting = 0;
}
if (ImGui::Selectable("Mono")) {
gSaveContext.audioSetting = 1;
gGlobalSettings.audioSetting = 1;
}
if (ImGui::Selectable("Headset")) {
gSaveContext.audioSetting = 2;
gGlobalSettings.audioSetting = 2;
}
if (ImGui::Selectable("Surround")) {
gSaveContext.audioSetting = 3;
gGlobalSettings.audioSetting = 3;
}

ImGui::EndCombo();
Expand All @@ -497,12 +497,12 @@ void DrawInfoTab() {
}
UIWidgets::InsertHelpHoverText("WARNING! If you save, your file may be locked! Use caution!");

if (ImGui::BeginCombo("Z Target Mode", gSaveContext.zTargetSetting ? "Hold" : "Switch")) {
if (ImGui::BeginCombo("Z Target Mode", gGlobalSettings.zTargetSetting ? "Hold" : "Switch")) {
if (ImGui::Selectable("Switch")) {
gSaveContext.zTargetSetting = 0;
gGlobalSettings.zTargetSetting = 0;
}
if (ImGui::Selectable("Hold")) {
gSaveContext.zTargetSetting = 1;
gGlobalSettings.zTargetSetting = 1;
}
ImGui::EndCombo();
}
Expand Down
6 changes: 3 additions & 3 deletions soh/soh/Enhancements/randomizer/3drando/spoiler_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ static void WriteLocation(
ItemLocation* location = Location(locationKey);

// auto node = parentNode->InsertNewChildElement("location");
switch (gSaveContext.language) {
switch (gGlobalSettings.language) {
case LANGUAGE_ENG:
default:
jsonData["playthrough"][sphere][location->GetName()] = location->GetPlacedItemName().GetEnglish();
Expand Down Expand Up @@ -335,7 +335,7 @@ static void WriteShuffledEntrance(std::string sphereString, Entrance* entrance)
jsonData["entrances"].push_back(reverseEntranceJson);
}

switch (gSaveContext.language) {
switch (gGlobalSettings.language) {
case LANGUAGE_ENG:
case LANGUAGE_FRA:
default:
Expand Down Expand Up @@ -538,7 +538,7 @@ static void WriteRequiredTrials() {
for (const auto& trial : Trial::trialList) {
if (trial->IsRequired()) {
std::string trialName;
switch (gSaveContext.language) {
switch (gGlobalSettings.language) {
case LANGUAGE_FRA:
trialName = trial->GetName().GetFrench();
break;
Expand Down
14 changes: 8 additions & 6 deletions soh/soh/Enhancements/randomizer/randomizer_check_tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -845,10 +845,11 @@ void DrawLocation(RandomizerCheckObject rcObj, RandomizerCheckShow* thisCheckSta
case RCSHOW_SCUMMED:
if (gSaveContext.n64ddFlag)
txt = OTRGlobals::Instance->gRandomizer
->EnumToSpoilerfileGetName[gSaveContext.itemLocations[rcObj.rc].get.rgID][gSaveContext.language];
else if (gSaveContext.language == LANGUAGE_ENG)
->EnumToSpoilerfileGetName[gSaveContext.itemLocations[rcObj.rc].get.rgID]
[gGlobalSettings.language];
else if (gGlobalSettings.language == LANGUAGE_ENG)
txt = ItemFromGIID(rcObj.ogItemId).GetName().english;
else if (gSaveContext.language == LANGUAGE_FRA)
else if (gGlobalSettings.language == LANGUAGE_FRA)
txt = ItemFromGIID(rcObj.ogItemId).GetName().french;
break;
case RCSHOW_SKIPPED:
Expand All @@ -857,10 +858,11 @@ void DrawLocation(RandomizerCheckObject rcObj, RandomizerCheckShow* thisCheckSta
case RCSHOW_SEEN:
if (gSaveContext.n64ddFlag)
txt = OTRGlobals::Instance->gRandomizer
->EnumToSpoilerfileGetName[gSaveContext.itemLocations[rcObj.rc].get.fakeRgID][gSaveContext.language];
else if (gSaveContext.language == LANGUAGE_ENG)
->EnumToSpoilerfileGetName[gSaveContext.itemLocations[rcObj.rc].get.fakeRgID]
[gGlobalSettings.language];
else if (gGlobalSettings.language == LANGUAGE_ENG)
txt = ItemFromGIID(rcObj.ogItemId).GetName().english;
else if (gSaveContext.language == LANGUAGE_FRA)
else if (gGlobalSettings.language == LANGUAGE_FRA)
txt = ItemFromGIID(rcObj.ogItemId).GetName().french;
break;
case RCSHOW_HINTED:
Expand Down
25 changes: 25 additions & 0 deletions soh/soh/GlobalSettings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "GlobalSettings.h"
#include "z64.h"
#include "variables.h"
#include "core/bridge/consolevariablebridge.h"
#include "core/Window.h"

extern "C" GlobalSettingsStruct gGlobalSettings;

namespace GlobalSettings {

void Init() {
gGlobalSettings.language = LANGUAGE_ENG;
gGlobalSettings.zTargetSetting = 0;
gGlobalSettings.audioSetting = 0;
}

void RegisterCVars() {
std::shared_ptr<Ship::ConsoleVariable> cvar = Ship::Window::GetInstance()->GetConsoleVariables();

cvar->RegisterManaged("gLanguages", gGlobalSettings.language);
cvar->RegisterManaged("gZTargetSetting", gGlobalSettings.zTargetSetting);
cvar->RegisterManaged("gAudioSetting", gGlobalSettings.audioSetting);
}

} // namespace GlobalSettings
11 changes: 11 additions & 0 deletions soh/soh/GlobalSettings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "z64settings.h"

namespace GlobalSettings {

void Init();

void RegisterCVars();

}
13 changes: 10 additions & 3 deletions soh/soh/OTRGlobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <Utils/StringHelper.h>
#include <Hooks.h>
#include "Enhancements/custom-message/CustomMessageManager.h"
#include "GlobalSettings.h"

#include <Fast3D/gfx_pc.h>
#include <Fast3D/gfx_rendering_api.h>
Expand Down Expand Up @@ -230,6 +231,10 @@ OTRGlobals::OTRGlobals() {
OOT_PAL_GC_DBG1,
OOT_PAL_GC_DBG2
};

GlobalSettings::Init();
Ship::RegisterHook<Ship::CVarInit>(GlobalSettings::RegisterCVars);

context = Ship::Window::CreateInstance("Ship of Harkinian", OTRFiles);

context->GetResourceManager()->GetResourceLoader()->RegisterResourceFactory(Ship::ResourceType::SOH_Animation, std::make_shared<Ship::AnimationFactory>());
Expand Down Expand Up @@ -565,6 +570,8 @@ extern "C" void InitOTR() {
SohImGui::AddSetupHooksDelegate(GameMenuBar::SetupHooks);
SohImGui::RegisterMenuDrawMethod(GameMenuBar::Draw);

//GlobalSettings_Init();

OTRGlobals::Instance = new OTRGlobals();
SaveManager::Instance = new SaveManager();
CustomMessageManager::Instance = new CustomMessageManager();
Expand Down Expand Up @@ -1414,11 +1421,11 @@ extern "C" void* getN64WeirdFrame(s32 i) {
extern "C" int GetEquipNowMessage(char* buffer, char* src, const int maxBufferSize) {
std::string postfix;

if (gSaveContext.language == LANGUAGE_FRA) {
if (gGlobalSettings.language == LANGUAGE_FRA) {
postfix = "\x04\x1A\x08" "D\x96sirez-vous l'\x96quiper maintenant?" "\x09&&"
"\x1B%g" "Oui" "&"
"Non" "%w\x02";
} else if (gSaveContext.language == LANGUAGE_GER) {
} else if (gGlobalSettings.language == LANGUAGE_GER) {
postfix = "\x04\x1A\x08" "M""\x9A""chtest Du es jetzt ausr\x9Esten?" "\x09&&"
"\x1B%g" "Ja!" "&"
"Nein!" "%w\x02";
Expand Down Expand Up @@ -1687,7 +1694,7 @@ extern "C" int CustomMessage_RetrieveIfExists(PlayState* play) {
}
if (messageEntry.textBoxType != -1) {
font->charTexBuf[0] = (messageEntry.textBoxType << 4) | messageEntry.textBoxPos;
switch (gSaveContext.language) {
switch (gGlobalSettings.language) {
case LANGUAGE_FRA:
return msgCtx->msgLength = font->msgLength =
CopyStringToCharBuffer(messageEntry.french, buffer, maxBufferSize);
Expand Down
52 changes: 15 additions & 37 deletions soh/soh/SaveManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <array>

extern "C" SaveContext gSaveContext;
extern "C" GlobalSettingsStruct gGlobalSettings;

std::filesystem::path SaveManager::GetFileName(int fileNum) {
const std::filesystem::path sSavePath(Ship::Window::GetPathRelativeToAppDirectory("Save"));
Expand Down Expand Up @@ -333,34 +334,22 @@ void SaveManager::Init() {
nlohmann::json globalBlock;
input >> globalBlock;

if (!globalBlock.contains("version")) {
SPDLOG_WARN("Global save does not contain a version. We are reconstructing it.");
CreateDefaultGlobal();
return;
if (globalBlock.contains("version") && (globalBlock["version"].get<int>() == 1)) {
currentJsonContext = &globalBlock;
LoadData("audioSetting", gGlobalSettings.audioSetting);
LoadData("zTargetSetting", gGlobalSettings.zTargetSetting);
LoadData("language", gGlobalSettings.language);
}

switch (globalBlock["version"].get<int>()) {
case 1:
currentJsonContext = &globalBlock;
LoadData("audioSetting", gSaveContext.audioSetting);
LoadData("zTargetSetting", gSaveContext.zTargetSetting);
LoadData("language", gSaveContext.language);
break;
default:
SPDLOG_WARN("Global save has a unrecognized version. We are reconstructing it.");
CreateDefaultGlobal();
break;
}
} else {
CreateDefaultGlobal();
input.close();
std::filesystem::remove(sGlobalPath);
}

// Load files to initialize metadata
for (int fileNum = 0; fileNum < MaxFiles; fileNum++) {
if (std::filesystem::exists(GetFileName(fileNum))) {
LoadFile(fileNum);
}

}
}

Expand Down Expand Up @@ -701,10 +690,7 @@ void SaveManager::SaveFile(int fileNum) {

void SaveManager::SaveGlobal() {
nlohmann::json globalBlock;
globalBlock["version"] = 1;
globalBlock["audioSetting"] = gSaveContext.audioSetting;
globalBlock["zTargetSetting"] = gSaveContext.zTargetSetting;
globalBlock["language"] = gSaveContext.language;
globalBlock["version"] = 2;

const std::filesystem::path sSavePath(Ship::Window::GetPathRelativeToAppDirectory("Save"));
const std::filesystem::path sGlobalPath = sSavePath / std::string("global.sav");
Expand Down Expand Up @@ -811,14 +797,6 @@ void SaveManager::AddPostFunction(const std::string& name, PostFunc func) {
postHandlers[name] = func;
}

void SaveManager::CreateDefaultGlobal() {
gSaveContext.audioSetting = 0;
gSaveContext.zTargetSetting = 0;
gSaveContext.language = CVarGetInteger("gLanguages", LANGUAGE_ENG);

SaveGlobal();
}

void SaveManager::LoadBaseVersion1() {
SaveManager::Instance->LoadData("entranceIndex", gSaveContext.entranceIndex);
SaveManager::Instance->LoadData("linkAge", gSaveContext.linkAge);
Expand Down Expand Up @@ -2003,16 +1981,16 @@ void SaveManager::ConvertFromUnversioned() {

for (size_t i = 0; i < ARRAY_COUNT(sZeldaMagic) - 3; i++) {
if (sZeldaMagic[i + SRAM_HEADER_MAGIC] != data[i + SRAM_HEADER_MAGIC]) {
CreateDefaultGlobal();
SPDLOG_WARN("Save file does not contain the magic bytes, we can not convert it.");
return;
}
}

gSaveContext.audioSetting = data[SRAM_HEADER_SOUND] & 3;
gSaveContext.zTargetSetting = data[SRAM_HEADER_ZTARGET] & 1;
gSaveContext.language = data[SRAM_HEADER_LANGUAGE];
if (gSaveContext.language >= LANGUAGE_MAX) {
gSaveContext.language = CVarGetInteger("gLanguages", LANGUAGE_ENG);
gGlobalSettings.audioSetting = data[SRAM_HEADER_SOUND] & 3;
gGlobalSettings.zTargetSetting = data[SRAM_HEADER_ZTARGET] & 1;
gGlobalSettings.language = data[SRAM_HEADER_LANGUAGE];
if (gGlobalSettings.language >= LANGUAGE_MAX) {
gGlobalSettings.language = LANGUAGE_ENG;
}
SaveGlobal();

Expand Down
1 change: 0 additions & 1 deletion soh/soh/SaveManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ class SaveManager {
std::filesystem::path GetFileName(int fileNum);

void ConvertFromUnversioned();
void CreateDefaultGlobal();

void InitMeta(int slotNum);
static void InitFileImpl(bool isDebug);
Expand Down
2 changes: 0 additions & 2 deletions soh/src/code/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,6 @@ void GameState_Update(GameState* gameState) {
}
}

gSaveContext.language = CVarGetInteger("gLanguages", LANGUAGE_ENG);

gameState->frames++;
}

Expand Down
Loading