Skip to content

Commit

Permalink
Replace C string in scenario name handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Gymnasiast committed Oct 30, 2024
1 parent 95178be commit 749b331
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 43 deletions.
5 changes: 0 additions & 5 deletions src/openrct2/core/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,6 @@ namespace OpenRCT2::String
return ch;
}

utf8* TrimStart(utf8* buffer, size_t bufferSize, const utf8* src)
{
return String::Set(buffer, bufferSize, TrimStart(src));
}

std::string TrimStart(const std::string& s)
{
const utf8* trimmed = TrimStart(s.c_str());
Expand Down
5 changes: 2 additions & 3 deletions src/openrct2/core/String.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ namespace OpenRCT2::String
bool IsWhiteSpace(codepoint_t codepoint);
utf8* Trim(utf8* str);
const utf8* TrimStart(const utf8* str);
utf8* TrimStart(utf8* buffer, size_t bufferSize, const utf8* src);
std::string TrimStart(const std::string& s);
std::string Trim(const std::string& s);
[[nodiscard]] std::string TrimStart(const std::string& s);
[[nodiscard]] std::string Trim(const std::string& s);

/**
* Converts a multi-byte string from one code page to UTF-8.
Expand Down
2 changes: 1 addition & 1 deletion src/openrct2/localisation/Language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ bool LanguageOpen(int32_t id)
}
}

bool LanguageGetLocalisedScenarioStrings(const utf8* scenarioFilename, StringId* outStringIds)
bool LanguageGetLocalisedScenarioStrings(u8string_view scenarioFilename, StringId* outStringIds)
{
const auto& localisationService = OpenRCT2::GetContext()->GetLocalisationService();
auto result = localisationService.GetLocalisedScenarioStrings(scenarioFilename);
Expand Down
2 changes: 1 addition & 1 deletion src/openrct2/localisation/Language.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ uint8_t LanguageGetIDFromLocale(const char* locale);
const char* LanguageGetString(StringId id);
bool LanguageOpen(int32_t id);

bool LanguageGetLocalisedScenarioStrings(const utf8* scenarioFilename, StringId* outStringIds);
bool LanguageGetLocalisedScenarioStrings(u8string_view scenarioFilename, StringId* outStringIds);
void LanguageFreeObjectString(StringId stringId);
StringId LanguageAllocateObjectString(const std::string& target);
3 changes: 1 addition & 2 deletions src/openrct2/localisation/LanguagePack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,8 @@ class LanguagePack final : public ILanguagePack
return nullptr;
}

StringId GetScenarioOverrideStringId(const utf8* scenarioFilename, uint8_t index) override
StringId GetScenarioOverrideStringId(u8string_view scenarioFilename, uint8_t index) override
{
Guard::ArgumentNotNull(scenarioFilename);
Guard::Assert(index < ScenarioOverrideMaxStringCount);

int32_t ooIndex = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/openrct2/localisation/LanguagePack.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct ILanguagePack
virtual void RemoveString(StringId stringId) = 0;
virtual void SetString(StringId stringId, const std::string& str) = 0;
virtual const utf8* GetString(StringId stringId) const = 0;
virtual StringId GetScenarioOverrideStringId(const utf8* scenarioFilename, uint8_t index) = 0;
virtual StringId GetScenarioOverrideStringId(u8string_view scenarioFilename, uint8_t index) = 0;
};

namespace OpenRCT2::LanguagePackFactory
Expand Down
9 changes: 4 additions & 5 deletions src/openrct2/localisation/LocalisationService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,12 @@ void LocalisationService::CloseLanguages()
_currentLanguage = LANGUAGE_UNDEFINED;
}

std::tuple<StringId, StringId, StringId> LocalisationService::GetLocalisedScenarioStrings(
const std::string& scenarioFilename) const
std::tuple<StringId, StringId, StringId> LocalisationService::GetLocalisedScenarioStrings(u8string_view scenarioFilename) const
{
Guard::Assert(!_loadedLanguages.empty());
auto result0 = _loadedLanguages[0]->GetScenarioOverrideStringId(scenarioFilename.c_str(), 0);
auto result1 = _loadedLanguages[0]->GetScenarioOverrideStringId(scenarioFilename.c_str(), 1);
auto result2 = _loadedLanguages[0]->GetScenarioOverrideStringId(scenarioFilename.c_str(), 2);
auto result0 = _loadedLanguages[0]->GetScenarioOverrideStringId(scenarioFilename, 0);
auto result1 = _loadedLanguages[0]->GetScenarioOverrideStringId(scenarioFilename, 1);
auto result2 = _loadedLanguages[0]->GetScenarioOverrideStringId(scenarioFilename, 2);
return std::make_tuple(result0, result1, result2);
}

Expand Down
3 changes: 2 additions & 1 deletion src/openrct2/localisation/LocalisationService.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#pragma once

#include "../core/StringTypes.h"
#include "../localisation/StringIdType.h"

#include <memory>
Expand Down Expand Up @@ -58,7 +59,7 @@ namespace OpenRCT2::Localisation
~LocalisationService();

const char* GetString(StringId id) const;
std::tuple<StringId, StringId, StringId> GetLocalisedScenarioStrings(const std::string& scenarioFilename) const;
std::tuple<StringId, StringId, StringId> GetLocalisedScenarioStrings(u8string_view scenarioFilename) const;
std::string GetLanguagePath(uint32_t languageId) const;

void OpenLanguage(int32_t id);
Expand Down
4 changes: 2 additions & 2 deletions src/openrct2/rct2/S6Importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ namespace OpenRCT2::RCT2
}
else
{
String::Set(dst->Name, sizeof(dst->Name), _s6.Info.Name);
// Normalise the name to make the scenario as recognisable as possible.
ScenarioSources::NormaliseName(dst->Name, sizeof(dst->Name), dst->Name);
auto normalisedName = ScenarioSources::NormaliseName(_s6.Info.Name);
String::Set(dst->Name, sizeof(dst->Name), normalisedName.c_str());
}

// dst->name will be translated later so keep the untranslated name here
Expand Down
3 changes: 1 addition & 2 deletions src/openrct2/scenario/Scenario.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ void ScenarioReset(GameState_t& gameState)
gameState.Cash = gameState.InitialCash;

{
utf8 normalisedName[64];
ScenarioSources::NormaliseName(normalisedName, sizeof(normalisedName), gameState.ScenarioName.c_str());
auto normalisedName = ScenarioSources::NormaliseName(gameState.ScenarioName);

StringId localisedStringIds[3];
if (LanguageGetLocalisedScenarioStrings(normalisedName, localisedStringIds))
Expand Down
41 changes: 22 additions & 19 deletions src/openrct2/scenario/ScenarioSources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,36 +421,39 @@ namespace OpenRCT2::ScenarioSources
return false;
}

void NormaliseName(utf8* buffer, size_t bufferSize, const utf8* name)
u8string NormaliseName(u8string_view input)
{
size_t nameLength = String::LengthOf(name);
// American scenario titles should be converted to British name
// Don't worry, names will be translated using language packs later
for (const ScenarioAlias& alias : ScenarioAliases)
{
if (String::Equals(alias.Alternative, input))
{
LOG_VERBOSE("Found alias: %s; will treat as: %s", input, alias.Original);
return u8string(alias.Original);
}
}

u8string normalisedName;
// Strip "RCT(1|2)?" prefix off scenario names.
if (nameLength >= 3 && (name[0] == 'R' && name[1] == 'C' && name[2] == 'T'))
if (input.starts_with("RCT"))
{
if (nameLength >= 4 && (name[3] == '1' || name[3] == '2'))
LOG_VERBOSE("Stripping RCT/1/2 from name: %s", u8string(input).c_str());
if (input.length() >= 4 && (input[3] == '1' || input[3] == '2'))
{
LOG_VERBOSE("Stripping RCT/1/2 from name: %s", name);
String::Set(buffer, bufferSize, name + 4);
normalisedName = input.substr(4);
}
else
{
String::Set(buffer, bufferSize, name + 3);
normalisedName = input.substr(3);
}
}

// Trim (for the sake of the above and WW / TT scenarios
String::TrimStart(buffer, bufferSize, name);

// American scenario titles should be converted to British name
// Don't worry, names will be translated using language packs later
for (const ScenarioAlias& alias : ScenarioAliases)
else
{
if (String::Equals(alias.Alternative, name))
{
LOG_VERBOSE("Found alias: %s; will treat as: %s", name, alias.Original);
String::Set(buffer, bufferSize, alias.Original);
}
normalisedName = input;
}

// Trim (for the sake of the above and WW / TT scenarios)
return String::TrimStart(normalisedName);
}
} // namespace OpenRCT2::ScenarioSources
2 changes: 1 addition & 1 deletion src/openrct2/scenario/ScenarioSources.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace OpenRCT2::ScenarioSources
{
bool TryGetByName(const utf8* name, SourceDescriptor* outDesc);
bool TryGetById(uint8_t id, SourceDescriptor* outDesc);
void NormaliseName(utf8* buffer, size_t bufferSize, const utf8* name);
u8string NormaliseName(u8string_view input);
} // namespace OpenRCT2::ScenarioSources

// RCT1 scenario index map
Expand Down

0 comments on commit 749b331

Please sign in to comment.