take nlohmann::json by const reference instead of copying (#6918)

This commit is contained in:
Philip Dubé
2026-07-13 02:49:01 +00:00
committed by GitHub
parent c9491183f7
commit 29948f27a1
17 changed files with 46 additions and 53 deletions
+11 -19
View File
@@ -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<std::string, nlohmann::json> map, CustomMessage message) {
std::vector<std::string> 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);
@@ -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<RandomizerCheck, ItemOverride> overrides = {};
std::vector<std::vector<RandomizerCheck>> playthroughLocations = {};
std::vector<RandomizerCheck> everyPossibleLocation = {};
+2 -2
View File
@@ -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();
+1 -1
View File
@@ -106,7 +106,7 @@ class Dungeons {
/// @return
std::array<DungeonInfo*, 12> GetDungeonList();
size_t GetDungeonListSize() const;
void ParseJson(nlohmann::json spoilerFileJson);
void ParseJson(const nlohmann::json& spoilerFileJson);
private:
std::array<DungeonInfo, 12> dungeonList;
+2 -2
View File
@@ -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;
+1 -1
View File
@@ -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);
@@ -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() {
@@ -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
@@ -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
@@ -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:
@@ -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") };
}
}
}
@@ -39,7 +39,7 @@ static std::vector<const char*> 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;
+6 -6
View File
@@ -3067,10 +3067,10 @@ void Context::FinalizeSettings(const std::set<RandomizerCheck>& excludedLocation
}
}
void Settings::ParseJson(nlohmann::json spoilerFileJson) {
mContext->SetSeedString(spoilerFileJson["seed"].get<std::string>());
mContext->SetSeed(spoilerFileJson["finalSeed"].get<uint32_t>());
nlohmann::json settingsJson = spoilerFileJson["settings"];
void Settings::ParseJson(const nlohmann::json& spoilerFileJson) {
mContext->SetSeedString(spoilerFileJson.at("seed").get<std::string>());
mContext->SetSeed(spoilerFileJson.at("finalSeed").get<uint32_t>());
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);
+1 -1
View File
@@ -115,7 +115,7 @@ class Settings {
*
* @param spoilerFileJson
*/
void ParseJson(nlohmann::json spoilerFileJson);
void ParseJson(const nlohmann::json& spoilerFileJson);
std::map<RandomizerArea, std::vector<RandomizerTrick>> mTricksByArea = {};
/**
@@ -220,7 +220,7 @@ StaticData::PopulateTranslationMap(const std::unordered_map<uint32_t, CustomMess
std::unordered_map<std::string, uint32_t> output = {};
for (const auto& [key, message] : input) {
std::vector<std::string> 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<uint32_t, Randomizer
std::unordered_map<std::string, uint32_t> output = {};
for (const auto& [key, text] : input) {
std::vector<std::string> 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?
+2 -2
View File
@@ -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();
+1 -1
View File
@@ -35,7 +35,7 @@ class Trials {
void RequireAll();
std::vector<TrialInfo*> GetTrialList();
size_t GetTrialListSize() const;
void ParseJson(nlohmann::json spoilerFileJson);
void ParseJson(const nlohmann::json& spoilerFileJson);
std::unordered_map<uint32_t, RandomizerHintTextKey> GetAllTrialHintHeys() const;
private: