From 263b73cb3170c1ccfc41d33e779010c3d8700f15 Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Sun, 27 Oct 2024 19:23:12 +0100 Subject: [PATCH] Use ScenarioTextObject for indexing scenarios --- src/openrct2/object/ScenarioTextObject.cpp | 2 +- src/openrct2/object/StringTable.cpp | 4 ++ src/openrct2/rct1/S4Importer.cpp | 56 ++++++++++++---------- src/openrct2/rct2/S6Importer.cpp | 34 +++++++------ 4 files changed, 56 insertions(+), 40 deletions(-) diff --git a/src/openrct2/object/ScenarioTextObject.cpp b/src/openrct2/object/ScenarioTextObject.cpp index b82e46070d..93c5c1aca1 100644 --- a/src/openrct2/object/ScenarioTextObject.cpp +++ b/src/openrct2/object/ScenarioTextObject.cpp @@ -32,7 +32,7 @@ void ScenarioTextObject::ReadJson(IReadObjectContext* context, json_t& root) std::string ScenarioTextObject::GetScenarioName() { - return GetStringTable().GetString(ObjectStringID::SCENARIO_NAME); + return GetStringTable().GetString(ObjectStringID::NAME); // SCENARIO_NAME } std::string ScenarioTextObject::GetParkName() diff --git a/src/openrct2/object/StringTable.cpp b/src/openrct2/object/StringTable.cpp index db3c6310d5..d507b4430c 100644 --- a/src/openrct2/object/StringTable.cpp +++ b/src/openrct2/object/StringTable.cpp @@ -88,6 +88,10 @@ ObjectStringID StringTable::ParseStringId(const std::string& s) return ObjectStringID::NAME; if (s == "description") return ObjectStringID::DESCRIPTION; + if (s == "park_name") + return ObjectStringID::PARK_NAME; + if (s == "details") + return ObjectStringID::SCENARIO_DETAILS; if (s == "capacity") return ObjectStringID::CAPACITY; if (s == "vehicleName") diff --git a/src/openrct2/rct1/S4Importer.cpp b/src/openrct2/rct1/S4Importer.cpp index 548241ceb6..17b373cde1 100644 --- a/src/openrct2/rct1/S4Importer.cpp +++ b/src/openrct2/rct1/S4Importer.cpp @@ -48,6 +48,7 @@ #include "../object/ObjectList.h" #include "../object/ObjectManager.h" #include "../object/ObjectRepository.h" +#include "../object/ScenarioTextObject.h" #include "../park/Legacy.h" #include "../peep/PeepAnimationData.h" #include "../peep/RideUseSystem.h" @@ -244,7 +245,7 @@ namespace OpenRCT2::RCT1 dst->ObjectiveArg3 = GetBuildTheBestRideId(); } - auto name = RCT2StringToUTF8(_s4.ScenarioName, RCT2LanguageId::EnglishUK); + std::string name = RCT2StringToUTF8(_s4.ScenarioName, RCT2LanguageId::EnglishUK); std::string details; // TryGetById won't set this property if the scenario is not recognised, @@ -256,16 +257,20 @@ namespace OpenRCT2::RCT1 String::Set(dst->InternalName, sizeof(dst->InternalName), desc.title); - StringId localisedStringIds[3]; - if (LanguageGetLocalisedScenarioStrings(desc.title, localisedStringIds)) + if (desc.textObjectId != nullptr) { - if (localisedStringIds[0] != STR_NONE) + auto& objManager = GetContext()->GetObjectManager(); + + // Unload loaded scenario text object, if any. + if (auto* obj = objManager.GetLoadedObject(ObjectType::ScenarioText, 0); obj != nullptr) + objManager.UnloadObjects({ obj->GetDescriptor() }); + + // Load the one specified + if (auto* obj = objManager.LoadObject(desc.textObjectId); obj != nullptr) { - name = String::ToStd(LanguageGetString(localisedStringIds[0])); - } - if (localisedStringIds[2] != STR_NONE) - { - details = String::ToStd(LanguageGetString(localisedStringIds[2])); + auto* textObject = reinterpret_cast(obj); + name = textObject->GetScenarioName(); + details = textObject->GetScenarioDetails(); } } @@ -2324,24 +2329,25 @@ namespace OpenRCT2::RCT1 int32_t scNumber = _s4.ScenarioSlotIndex; if (scNumber != -1) { - SourceDescriptor sourceDesc; - if (ScenarioSources::TryGetById(scNumber, &sourceDesc)) + SourceDescriptor desc; + if (ScenarioSources::TryGetById(scNumber, &desc) && desc.textObjectId != nullptr) { - StringId localisedStringIds[3]; - if (LanguageGetLocalisedScenarioStrings(sourceDesc.title, localisedStringIds)) + auto& objManager = GetContext()->GetObjectManager(); + + // Ensure only one thread talks to the object manager at a time + std::lock_guard lock(mtx); + + // Unload loaded scenario text object, if any. + if (auto* obj = objManager.GetLoadedObject(ObjectType::ScenarioText, 0); obj != nullptr) + objManager.UnloadObjects({ obj->GetDescriptor() }); + + // Load the one specified + if (auto* obj = objManager.LoadObject(desc.textObjectId); obj != nullptr) { - if (localisedStringIds[0] != STR_NONE) - { - name = String::ToStd(LanguageGetString(localisedStringIds[0])); - } - if (localisedStringIds[1] != STR_NONE) - { - parkName = String::ToStd(LanguageGetString(localisedStringIds[1])); - } - if (localisedStringIds[2] != STR_NONE) - { - details = String::ToStd(LanguageGetString(localisedStringIds[2])); - } + auto* textObject = reinterpret_cast(obj); + name = textObject->GetScenarioName(); + parkName = textObject->GetParkName(); + details = textObject->GetScenarioDetails(); } } } diff --git a/src/openrct2/rct2/S6Importer.cpp b/src/openrct2/rct2/S6Importer.cpp index d8050f5a84..e70d48d58c 100644 --- a/src/openrct2/rct2/S6Importer.cpp +++ b/src/openrct2/rct2/S6Importer.cpp @@ -49,6 +49,7 @@ #include "../object/ObjectList.h" #include "../object/ObjectManager.h" #include "../object/ObjectRepository.h" +#include "../object/ScenarioTextObject.h" #include "../object/WallSceneryEntry.h" #include "../park/Legacy.h" #include "../peep/RideUseSystem.h" @@ -263,11 +264,6 @@ namespace OpenRCT2::RCT2 String::Set(dst->Name, sizeof(dst->Name), normalisedName.c_str()); } - // dst->name will be translated later so keep the untranslated name here - String::Set(dst->InternalName, sizeof(dst->InternalName), dst->Name); - - String::Set(dst->Details, sizeof(dst->Details), _s6.Info.Details); - // Look up and store information regarding the origins of this scenario. SourceDescriptor desc; if (ScenarioSources::TryGetByName(dst->Name, &desc)) @@ -291,17 +287,27 @@ namespace OpenRCT2::RCT2 } } - // Localise the park name and description - StringId localisedStringIds[3]; - if (LanguageGetLocalisedScenarioStrings(dst->Name, localisedStringIds)) + // dst->name will be translated later so keep the untranslated name here + String::Set(dst->InternalName, sizeof(dst->InternalName), dst->Name); + String::Set(dst->Details, sizeof(dst->Details), _s6.Info.Details); + + if (desc.textObjectId != nullptr) { - if (localisedStringIds[0] != STR_NONE) + auto& objManager = GetContext()->GetObjectManager(); + + // Unload loaded scenario text object, if any. + if (auto* obj = objManager.GetLoadedObject(ObjectType::ScenarioText, 0); obj != nullptr) + objManager.UnloadObjects({ obj->GetDescriptor() }); + + // Load the one specified + if (auto* obj = objManager.LoadObject(desc.textObjectId); obj != nullptr) { - String::Set(dst->Name, sizeof(dst->Name), LanguageGetString(localisedStringIds[0])); - } - if (localisedStringIds[2] != STR_NONE) - { - String::Set(dst->Details, sizeof(dst->Details), LanguageGetString(localisedStringIds[2])); + auto* textObject = reinterpret_cast(obj); + auto name = textObject->GetScenarioName(); + auto details = textObject->GetScenarioDetails(); + + String::Set(dst->Name, sizeof(dst->Name), name.c_str()); + String::Set(dst->Details, sizeof(dst->Details), details.c_str()); } }