1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-24 23:34:37 +01:00

Merge pull request #23090 from Gymnasiast/refactor/version-info

Replace some old C string handling
This commit is contained in:
Michael Steenbeek
2024-10-30 19:02:28 +01:00
committed by GitHub
18 changed files with 53 additions and 85 deletions

View File

@@ -199,12 +199,10 @@ namespace OpenRCT2::Ui::Windows
ScreenCoordsXY logoCoords = windowPos
+ ScreenCoordsXY(widgets[WIDX_OPENRCT2_LOGO].left, widgets[WIDX_OPENRCT2_LOGO].top);
GfxDrawSprite(dpi, ImageId(SPR_G2_LOGO), logoCoords);
// Version info
utf8 buffer[256];
utf8* ch = buffer;
OpenRCT2WriteFullVersionInfo(ch, sizeof(buffer) - (ch - buffer));
u8string versionInfo = gVersionInfoFull;
auto ft = Formatter();
ft.Add<const char*>(buffer);
ft.Add<const char*>(versionInfo.c_str());
auto const& versionPlaceholder = widgets[WIDX_VERSION];
auto versionPlaceHolderWidth = versionPlaceholder.right - versionPlaceholder.left;

View File

@@ -1566,11 +1566,6 @@ bool ContextLoadParkFromStream(void* stream)
return GetContext()->LoadParkFromStream(static_cast<IStream*>(stream), "");
}
void OpenRCT2WriteFullVersionInfo(utf8* buffer, size_t bufferSize)
{
String::Set(buffer, bufferSize, gVersionInfoFull);
}
void OpenRCT2Finish()
{
GetContext()->Finish();
@@ -1738,22 +1733,6 @@ void ContextQuit()
GetContext()->Quit();
}
bool ContextOpenCommonFileDialog(utf8* outFilename, OpenRCT2::Ui::FileDialogDesc& desc, size_t outSize)
{
try
{
std::string result = GetContext()->GetUiContext()->ShowFileDialog(desc);
String::Set(outFilename, outSize, result.c_str());
return !result.empty();
}
catch (const std::exception& ex)
{
LOG_ERROR(ex.what());
outFilename[0] = '\0';
return false;
}
}
u8string ContextOpenCommonFileDialog(OpenRCT2::Ui::FileDialogDesc& desc)
{
try

View File

@@ -232,5 +232,4 @@ void ContextHandleInput();
void ContextInputHandleKeyboard(bool isTitle);
void ContextQuit();
bool ContextLoadParkFromStream(void* stream);
bool ContextOpenCommonFileDialog(utf8* outFilename, OpenRCT2::Ui::FileDialogDesc& desc, size_t outSize);
u8string ContextOpenCommonFileDialog(OpenRCT2::Ui::FileDialogDesc& desc);

View File

@@ -60,7 +60,6 @@ extern uint8_t gScreenFlags;
extern uint32_t gScreenAge;
extern PromptMode gSavePromptMode;
void OpenRCT2WriteFullVersionInfo(utf8* buffer, size_t bufferSize);
void OpenRCT2Finish();
int32_t CommandLineRun(const char** argv, int32_t argc);

View File

@@ -447,9 +447,8 @@ static void PrintAbout()
static void PrintVersion()
{
char buffer[256];
OpenRCT2WriteFullVersionInfo(buffer, sizeof(buffer));
Console::WriteLine(buffer);
u8string versionInfo = gVersionInfoFull;
Console::WriteLine(versionInfo.c_str());
Console::WriteFormat("%s (%s)", OPENRCT2_PLATFORM, OPENRCT2_ARCHITECTURE);
Console::WriteLine();
Console::WriteFormat("Network version: %s", NetworkGetVersion().c_str());
@@ -481,9 +480,8 @@ static void PrintLaunchInformation()
struct tm* tmInfo;
// Print name and version information
OpenRCT2WriteFullVersionInfo(buffer, sizeof(buffer));
Console::WriteFormat("%s", buffer);
Console::WriteLine();
u8string versionInfo = gVersionInfoFull;
Console::WriteLine(versionInfo.c_str());
Console::WriteFormat("%s (%s)", OPENRCT2_PLATFORM, OPENRCT2_ARCHITECTURE);
Console::WriteLine();
Console::WriteLine();

View File

@@ -749,7 +749,7 @@ namespace OpenRCT2::Config
return {};
}
static bool SelectGogInstaller(utf8* installerPath)
static u8string SelectGogInstaller()
{
FileDialogDesc desc{};
desc.Type = FileDialogType::Open;
@@ -760,7 +760,7 @@ namespace OpenRCT2::Config
const auto userHomePath = Platform::GetFolderPath(SPECIAL_FOLDER::USER_HOME);
desc.InitialDirectory = userHomePath;
return ContextOpenCommonFileDialog(installerPath, desc, 4096);
return ContextOpenCommonFileDialog(desc);
}
static bool ExtractGogInstaller(const u8string& installerPath, const u8string& targetPath)
@@ -888,8 +888,8 @@ namespace OpenRCT2::Config
while (true)
{
uiContext->ShowMessageBox(LanguageGetString(STR_PLEASE_SELECT_GOG_INSTALLER));
utf8 gogPath[4096];
if (!SelectGogInstaller(gogPath))
auto gogPath = SelectGogInstaller();
if (gogPath.empty())
{
// The user clicked "Cancel", so stop trying.
return false;

View File

@@ -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());

View File

@@ -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.

View File

@@ -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);

View File

@@ -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);

View File

@@ -155,9 +155,8 @@ public:
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;

View File

@@ -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

View File

@@ -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);
}

View File

@@ -9,6 +9,7 @@
#pragma once
#include "../core/StringTypes.h"
#include "../localisation/StringIdType.h"
#include <memory>
@@ -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);

View File

@@ -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

View File

@@ -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))

View File

@@ -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);
// Strip "RCT(1|2)?" prefix off scenario names.
if (nameLength >= 3 && (name[0] == 'R' && name[1] == 'C' && name[2] == 'T'))
{
if (nameLength >= 4 && (name[3] == '1' || name[3] == '2'))
{
LOG_VERBOSE("Stripping RCT/1/2 from name: %s", name);
String::Set(buffer, bufferSize, name + 4);
}
else
{
String::Set(buffer, bufferSize, name + 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)
{
if (String::Equals(alias.Alternative, name))
if (String::Equals(alias.Alternative, input))
{
LOG_VERBOSE("Found alias: %s; will treat as: %s", name, alias.Original);
String::Set(buffer, bufferSize, alias.Original);
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 (input.starts_with("RCT"))
{
LOG_VERBOSE("Stripping RCT/1/2 from name: %s", u8string(input).c_str());
if (input.length() >= 4 && (input[3] == '1' || input[3] == '2'))
{
normalisedName = input.substr(4);
}
else
{
normalisedName = input.substr(3);
}
}
else
{
normalisedName = input;
}
// Trim (for the sake of the above and WW / TT scenarios)
return String::TrimStart(normalisedName);
}
} // namespace OpenRCT2::ScenarioSources

View File

@@ -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