From 29948f27a1439777f6fa17024e6ec44243a4aab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philip=20Dub=C3=A9?= <159546+serprex@users.noreply.github.com> Date: Mon, 13 Jul 2026 02:49:01 +0000 Subject: [PATCH] take nlohmann::json by const reference instead of copying (#6918) --- .../Enhancements/randomizer/SeedContext.cpp | 30 +++++++------------ soh/soh/Enhancements/randomizer/SeedContext.h | 8 ++--- soh/soh/Enhancements/randomizer/dungeon.cpp | 4 +-- soh/soh/Enhancements/randomizer/dungeon.h | 2 +- soh/soh/Enhancements/randomizer/entrance.cpp | 4 +-- soh/soh/Enhancements/randomizer/entrance.h | 2 +- .../randomizer/randomizer_check_tracker.cpp | 6 ++-- .../randomizer/randomizer_check_tracker.h | 2 +- .../randomizer_entrance_tracker.cpp | 6 ++-- .../randomizer/randomizer_entrance_tracker.h | 2 +- .../randomizer/randomizer_item_tracker.cpp | 7 +++-- .../randomizer/randomizer_item_tracker.h | 2 +- soh/soh/Enhancements/randomizer/settings.cpp | 12 ++++---- soh/soh/Enhancements/randomizer/settings.h | 2 +- .../Enhancements/randomizer/static_data.cpp | 4 +-- soh/soh/Enhancements/randomizer/trial.cpp | 4 +-- soh/soh/Enhancements/randomizer/trial.h | 2 +- 17 files changed, 46 insertions(+), 53 deletions(-) diff --git a/soh/soh/Enhancements/randomizer/SeedContext.cpp b/soh/soh/Enhancements/randomizer/SeedContext.cpp index 472782e347..abd36b42bd 100644 --- a/soh/soh/Enhancements/randomizer/SeedContext.cpp +++ b/soh/soh/Enhancements/randomizer/SeedContext.cpp @@ -417,8 +417,8 @@ void Context::ParseSpoiler(const char* spoilerFileName) { } catch (...) { LUSLOG_ERROR("Failed to load Spoiler File: %s", spoilerFileName); } } -void Context::ParseHashIconIndexesJson(nlohmann::json spoilerFileJson) { - nlohmann::json hashJson = spoilerFileJson["file_hash"]; +void Context::ParseHashIconIndexesJson(const nlohmann::json& spoilerFileJson) { + nlohmann::json hashJson = spoilerFileJson.value("file_hash", nlohmann::json()); int index = 0; for (auto it = hashJson.begin(); it != hashJson.end(); ++it) { hashIconIndexes[index] = gSeedTextures[it.value()].id; @@ -426,8 +426,8 @@ void Context::ParseHashIconIndexesJson(nlohmann::json spoilerFileJson) { } } -void Context::ParseItemLocationsJson(nlohmann::json spoilerFileJson) { - nlohmann::json locationsJson = spoilerFileJson["locations"]; +void Context::ParseItemLocationsJson(const nlohmann::json& spoilerFileJson) { + nlohmann::json locationsJson = spoilerFileJson.value("locations", nlohmann::json()); for (auto it = locationsJson.begin(); it != locationsJson.end(); ++it) { RandomizerCheck rc = StaticData::locationNameToEnum[it.key()]; if (it->is_structured()) { @@ -455,30 +455,22 @@ void Context::WriteHintJson(nlohmann::ordered_json& spoilerFileJson) { } } -nlohmann::json getValueForMessage(std::unordered_map map, CustomMessage message) { - std::vector strings = message.GetAllMessages(MF_CLEAN); - for (uint8_t language = 0; language < LANGUAGE_MAX; language++) { - if (map.contains(strings[language])) { - return strings[language]; - } - } - return {}; -} - -void Context::ParseHintJson(nlohmann::json spoilerFileJson) { - for (auto hintData : spoilerFileJson["Gossip Stone Hints"].items()) { +void Context::ParseHintJson(const nlohmann::json& spoilerFileJson) { + nlohmann::json gossipHintsJson = spoilerFileJson.value("Gossip Stone Hints", nlohmann::json()); + for (auto hintData : gossipHintsJson.items()) { RandomizerHint hint = (RandomizerHint)StaticData::hintNameToEnum[hintData.key()]; AddHint(hint, Hint(hint, hintData.value())); } - for (auto hintData : spoilerFileJson["Static Hints"].items()) { + nlohmann::json staticHintsJson = spoilerFileJson.value("Static Hints", nlohmann::json()); + for (auto hintData : staticHintsJson.items()) { RandomizerHint hint = (RandomizerHint)StaticData::hintNameToEnum[hintData.key()]; AddHint(hint, Hint(hint, hintData.value())); } CreateStaticHints(); } -void Context::ParseTricksJson(nlohmann::json spoilerFileJson) { - nlohmann::json enabledTricksJson = spoilerFileJson["enabledTricks"]; +void Context::ParseTricksJson(const nlohmann::json& spoilerFileJson) { + nlohmann::json enabledTricksJson = spoilerFileJson.value("enabledTricks", nlohmann::json()); const auto& settings = Rando::Settings::GetInstance(); for (auto it : enabledTricksJson) { int rt = settings->GetRandomizerTrickByName(it); diff --git a/soh/soh/Enhancements/randomizer/SeedContext.h b/soh/soh/Enhancements/randomizer/SeedContext.h index 08510eb036..cc87f66cfc 100644 --- a/soh/soh/Enhancements/randomizer/SeedContext.h +++ b/soh/soh/Enhancements/randomizer/SeedContext.h @@ -121,11 +121,11 @@ class Context { GetItemEntry GetFinalGIEntry(RandomizerCheck rc, bool checkObtainability = true, GetItemID ogItemId = GI_NONE); void ParseSpoiler(const char* spoilerFileName); - void ParseHashIconIndexesJson(nlohmann::json spoilerFileJson); - void ParseItemLocationsJson(nlohmann::json spoilerFileJson); + void ParseHashIconIndexesJson(const nlohmann::json& spoilerFileJson); + void ParseItemLocationsJson(const nlohmann::json& spoilerFileJson); void WriteHintJson(nlohmann::ordered_json& spoilerFileJson); - void ParseHintJson(nlohmann::json spoilerFileJson); - void ParseTricksJson(nlohmann::json spoilerFileJson); + void ParseHintJson(const nlohmann::json& spoilerFileJson); + void ParseTricksJson(const nlohmann::json& spoilerFileJson); std::map overrides = {}; std::vector> playthroughLocations = {}; std::vector everyPossibleLocation = {}; diff --git a/soh/soh/Enhancements/randomizer/dungeon.cpp b/soh/soh/Enhancements/randomizer/dungeon.cpp index 7a9287aff5..103f984618 100644 --- a/soh/soh/Enhancements/randomizer/dungeon.cpp +++ b/soh/soh/Enhancements/randomizer/dungeon.cpp @@ -325,8 +325,8 @@ size_t Dungeons::GetDungeonListSize() const { return dungeonList.size(); } -void Dungeons::ParseJson(nlohmann::json spoilerFileJson) { - nlohmann::json mqDungeonsJson = spoilerFileJson["masterQuestDungeons"]; +void Dungeons::ParseJson(const nlohmann::json& spoilerFileJson) { + nlohmann::json mqDungeonsJson = spoilerFileJson.value("masterQuestDungeons", nlohmann::json()); for (auto& dungeon : dungeonList) { dungeon.ClearMQ(); diff --git a/soh/soh/Enhancements/randomizer/dungeon.h b/soh/soh/Enhancements/randomizer/dungeon.h index e688b69cc3..a5c2d4869e 100644 --- a/soh/soh/Enhancements/randomizer/dungeon.h +++ b/soh/soh/Enhancements/randomizer/dungeon.h @@ -106,7 +106,7 @@ class Dungeons { /// @return std::array GetDungeonList(); size_t GetDungeonListSize() const; - void ParseJson(nlohmann::json spoilerFileJson); + void ParseJson(const nlohmann::json& spoilerFileJson); private: std::array dungeonList; diff --git a/soh/soh/Enhancements/randomizer/entrance.cpp b/soh/soh/Enhancements/randomizer/entrance.cpp index 35f92b4ca3..67d01e4b6e 100644 --- a/soh/soh/Enhancements/randomizer/entrance.cpp +++ b/soh/soh/Enhancements/randomizer/entrance.cpp @@ -1676,10 +1676,10 @@ void EntranceShuffler::UnshuffleAllEntrances() { } } -void EntranceShuffler::ParseJson(nlohmann::json spoilerFileJson) { +void EntranceShuffler::ParseJson(const nlohmann::json& spoilerFileJson) { UnshuffleAllEntrances(); try { - nlohmann::json entrancesJson = spoilerFileJson["entrances"]; + nlohmann::json entrancesJson = spoilerFileJson.value("entrances", nlohmann::json()); size_t i = 0; for (auto it = entrancesJson.begin(); it != entrancesJson.end() && i < entranceOverrides.size(); ++it, i++) { nlohmann::json entranceJson = *it; diff --git a/soh/soh/Enhancements/randomizer/entrance.h b/soh/soh/Enhancements/randomizer/entrance.h index 32fefbb132..ed2a0fc868 100644 --- a/soh/soh/Enhancements/randomizer/entrance.h +++ b/soh/soh/Enhancements/randomizer/entrance.h @@ -136,7 +136,7 @@ class EntranceShuffler { int ShuffleAllEntrances(); void CreateEntranceOverrides(); void UnshuffleAllEntrances(); - void ParseJson(nlohmann::json spoilerFileJson); + void ParseJson(const nlohmann::json& spoilerFileJson); void ApplyEntranceOverrides(); static const Entrance* GetEntranceByIndex(int16_t index); diff --git a/soh/soh/Enhancements/randomizer/randomizer_check_tracker.cpp b/soh/soh/Enhancements/randomizer/randomizer_check_tracker.cpp index f300430044..4991a861ce 100644 --- a/soh/soh/Enhancements/randomizer/randomizer_check_tracker.cpp +++ b/soh/soh/Enhancements/randomizer/randomizer_check_tracker.cpp @@ -2274,10 +2274,10 @@ void RecalculateAvailableChecks(RandomizerRegion startingRegion /* = RR_ROOT */, availableChecksStartingAgeTime = startingAgeTime; } -void LoadFromPreset(nlohmann::json info) { +void LoadFromPreset(const nlohmann::json& info) { presetLoaded = true; - presetPos = { info["pos"]["x"], info["pos"]["y"] }; - presetSize = { info["size"]["width"], info["size"]["height"] }; + presetPos = { info.at("pos").at("x"), info.at("pos").at("y") }; + presetSize = { info.at("size").at("width"), info.at("size").at("height") }; } void CheckTrackerWindow::Draw() { diff --git a/soh/soh/Enhancements/randomizer/randomizer_check_tracker.h b/soh/soh/Enhancements/randomizer/randomizer_check_tracker.h index 3c54a2b010..1edd0b0cea 100644 --- a/soh/soh/Enhancements/randomizer/randomizer_check_tracker.h +++ b/soh/soh/Enhancements/randomizer/randomizer_check_tracker.h @@ -60,5 +60,5 @@ void UpdateAllAreas(); void RecalculateAllAreaTotals(); void SpoilAreaFromCheck(RandomizerCheck rc); void RecalculateAvailableChecks(RandomizerRegion startingRegion = RR_ROOT, RandoAgeTime startingAgeTime = RAT_NONE); -void LoadFromPreset(nlohmann::json info); +void LoadFromPreset(const nlohmann::json& info); } // namespace CheckTracker diff --git a/soh/soh/Enhancements/randomizer/randomizer_entrance_tracker.cpp b/soh/soh/Enhancements/randomizer/randomizer_entrance_tracker.cpp index 03761053ac..5f118292cc 100644 --- a/soh/soh/Enhancements/randomizer/randomizer_entrance_tracker.cpp +++ b/soh/soh/Enhancements/randomizer/randomizer_entrance_tracker.cpp @@ -467,10 +467,10 @@ const EntranceData* GetEntranceData(s16 index) { return nullptr; } -void LoadFromPreset(nlohmann::json info) { +void LoadFromPreset(const nlohmann::json& info) { presetLoaded = true; - presetPos = { info["pos"]["x"], info["pos"]["y"] }; - presetSize = { info["size"]["width"], info["size"]["height"] }; + presetPos = { info.at("pos").at("x"), info.at("pos").at("y") }; + presetSize = { info.at("size").at("width"), info.at("size").at("height") }; } // Used for verifying the names on both sides of entrance pairs match. Keeping for ease of use for further name changes diff --git a/soh/soh/Enhancements/randomizer/randomizer_entrance_tracker.h b/soh/soh/Enhancements/randomizer/randomizer_entrance_tracker.h index 0dac2f9ed0..dfc3f010ef 100644 --- a/soh/soh/Enhancements/randomizer/randomizer_entrance_tracker.h +++ b/soh/soh/Enhancements/randomizer/randomizer_entrance_tracker.h @@ -114,7 +114,7 @@ void InitEntranceTrackingData(); s16 GetLastEntranceOverride(); s16 GetCurrentGrottoId(); const EntranceData* GetEntranceData(s16); -void LoadFromPreset(nlohmann::json info); +void LoadFromPreset(const nlohmann::json& info); class EntranceTrackerSettingsWindow final : public Ship::GuiWindow { public: diff --git a/soh/soh/Enhancements/randomizer/randomizer_item_tracker.cpp b/soh/soh/Enhancements/randomizer/randomizer_item_tracker.cpp index 2502e357bb..f4c3637060 100644 --- a/soh/soh/Enhancements/randomizer/randomizer_item_tracker.cpp +++ b/soh/soh/Enhancements/randomizer/randomizer_item_tracker.cpp @@ -488,12 +488,13 @@ bool HasEquipment(ItemTrackerItem item) { return GameInteractor::IsSaveLoaded() ? (item.data & gSaveContext.inventory.equipment) : false; } -void ItemTracker_LoadFromPreset(nlohmann::json trackerInfo) { +void ItemTracker_LoadFromPreset(const nlohmann::json& trackerInfo) { presetLoaded = true; for (auto window : itemTrackerWindowIDs) { if (trackerInfo.contains(window)) { - presetPos[window] = { trackerInfo[window]["pos"]["x"], trackerInfo[window]["pos"]["y"] }; - presetSize[window] = { trackerInfo[window]["size"]["width"], trackerInfo[window]["size"]["height"] }; + const nlohmann::json& windowInfo = trackerInfo.at(window); + presetPos[window] = { windowInfo.at("pos").at("x"), windowInfo.at("pos").at("y") }; + presetSize[window] = { windowInfo.at("size").at("width"), windowInfo.at("size").at("height") }; } } } diff --git a/soh/soh/Enhancements/randomizer/randomizer_item_tracker.h b/soh/soh/Enhancements/randomizer/randomizer_item_tracker.h index 26cf5aa03d..a33e1c5fa4 100644 --- a/soh/soh/Enhancements/randomizer/randomizer_item_tracker.h +++ b/soh/soh/Enhancements/randomizer/randomizer_item_tracker.h @@ -39,7 +39,7 @@ static std::vector itemTrackerWindowIDs = { "Item Tracker", "Fishing Pole Tracker", "Personal Notes", "Total Checks" }; -void ItemTracker_LoadFromPreset(nlohmann::json trackerInfo); +void ItemTracker_LoadFromPreset(const nlohmann::json& trackerInfo); typedef struct ItemTrackerDungeon { uint32_t id; diff --git a/soh/soh/Enhancements/randomizer/settings.cpp b/soh/soh/Enhancements/randomizer/settings.cpp index b15df87098..fb59b2c370 100644 --- a/soh/soh/Enhancements/randomizer/settings.cpp +++ b/soh/soh/Enhancements/randomizer/settings.cpp @@ -3067,10 +3067,10 @@ void Context::FinalizeSettings(const std::set& excludedLocation } } -void Settings::ParseJson(nlohmann::json spoilerFileJson) { - mContext->SetSeedString(spoilerFileJson["seed"].get()); - mContext->SetSeed(spoilerFileJson["finalSeed"].get()); - nlohmann::json settingsJson = spoilerFileJson["settings"]; +void Settings::ParseJson(const nlohmann::json& spoilerFileJson) { + mContext->SetSeedString(spoilerFileJson.at("seed").get()); + mContext->SetSeed(spoilerFileJson.at("finalSeed").get()); + nlohmann::json settingsJson = spoilerFileJson.value("settings", nlohmann::json()); for (auto it = settingsJson.begin(); it != settingsJson.end(); ++it) { // todo load into cvars for UI // RANDOTODO handle numeric value to options conversion better than brute force @@ -3080,7 +3080,7 @@ void Settings::ParseJson(nlohmann::json spoilerFileJson) { } } - nlohmann::json jsonExcludedLocations = spoilerFileJson["excludedLocations"]; + nlohmann::json jsonExcludedLocations = spoilerFileJson.value("excludedLocations", nlohmann::json()); const auto ctx = Context::GetInstance(); for (auto it = jsonExcludedLocations.begin(); it != jsonExcludedLocations.end(); ++it) { @@ -3088,7 +3088,7 @@ void Settings::ParseJson(nlohmann::json spoilerFileJson) { ctx->GetItemLocation(rc)->SetExcludedOption(RO_GENERIC_ON); } - nlohmann::json enabledTricksJson = spoilerFileJson["enabledTricks"]; + nlohmann::json enabledTricksJson = spoilerFileJson.value("enabledTricks", nlohmann::json()); for (auto it = enabledTricksJson.begin(); it != enabledTricksJson.end(); ++it) { const RandomizerTrick rt = mTrickNameToEnum[it.value()]; GetTrickSetting(rt).SetContextIndex(RO_GENERIC_ON); diff --git a/soh/soh/Enhancements/randomizer/settings.h b/soh/soh/Enhancements/randomizer/settings.h index c8a525afb3..6b63a0a390 100644 --- a/soh/soh/Enhancements/randomizer/settings.h +++ b/soh/soh/Enhancements/randomizer/settings.h @@ -115,7 +115,7 @@ class Settings { * * @param spoilerFileJson */ - void ParseJson(nlohmann::json spoilerFileJson); + void ParseJson(const nlohmann::json& spoilerFileJson); std::map> mTricksByArea = {}; /** diff --git a/soh/soh/Enhancements/randomizer/static_data.cpp b/soh/soh/Enhancements/randomizer/static_data.cpp index 2315e8caf3..31c5006f33 100644 --- a/soh/soh/Enhancements/randomizer/static_data.cpp +++ b/soh/soh/Enhancements/randomizer/static_data.cpp @@ -220,7 +220,7 @@ StaticData::PopulateTranslationMap(const std::unordered_map output = {}; for (const auto& [key, message] : input) { std::vector strings = message.GetAllMessages(MF_CLEAN); - for (std::string string : strings) { + for (const std::string& string : strings) { if (output.contains(string)) { if (output[string] != key) { // RANDOTODO should this cause an error of some kind? @@ -239,7 +239,7 @@ StaticData::PopulateTranslationMap(const std::unordered_map output = {}; for (const auto& [key, text] : input) { std::vector strings = hintTextTable[text].GetClear().GetAllMessages(MF_CLEAN); - for (std::string string : strings) { + for (const std::string& string : strings) { if (output.contains(string)) { if (output[string] != key) { // RANDOTODO should this cause an error of some kind? diff --git a/soh/soh/Enhancements/randomizer/trial.cpp b/soh/soh/Enhancements/randomizer/trial.cpp index 04f38b89eb..77a81e2219 100644 --- a/soh/soh/Enhancements/randomizer/trial.cpp +++ b/soh/soh/Enhancements/randomizer/trial.cpp @@ -71,8 +71,8 @@ size_t Trials::GetTrialListSize() const { return mTrials.size(); } -void Trials::ParseJson(nlohmann::json spoilerFileJson) { - nlohmann::json trialsJson = spoilerFileJson["requiredTrials"]; +void Trials::ParseJson(const nlohmann::json& spoilerFileJson) { + nlohmann::json trialsJson = spoilerFileJson.value("requiredTrials", nlohmann::json()); for (auto& trial : mTrials) { trial.SetAsSkipped(); diff --git a/soh/soh/Enhancements/randomizer/trial.h b/soh/soh/Enhancements/randomizer/trial.h index 5757dfc369..77de508d99 100644 --- a/soh/soh/Enhancements/randomizer/trial.h +++ b/soh/soh/Enhancements/randomizer/trial.h @@ -35,7 +35,7 @@ class Trials { void RequireAll(); std::vector GetTrialList(); size_t GetTrialListSize() const; - void ParseJson(nlohmann::json spoilerFileJson); + void ParseJson(const nlohmann::json& spoilerFileJson); std::unordered_map GetAllTrialHintHeys() const; private: