Consolidate FormatStringIDLegacy, FormatStringToUpper into Formatting

This commit is contained in:
Aaron van Geffen 2024-07-13 13:48:49 +02:00
parent 3d5c19de08
commit 896eb4f488
36 changed files with 87 additions and 149 deletions

View file

@ -18,7 +18,6 @@
# include <openrct2/Context.h>
# include <openrct2/localisation/Formatter.h>
# include <openrct2/localisation/Formatting.h>
# include <openrct2/localisation/Localisation.h>
# include <openrct2/util/Util.h>
using namespace OpenRCT2::Scripting;

View file

@ -25,7 +25,6 @@
# include <openrct2/interface/Window.h>
# include <openrct2/localisation/Formatter.h>
# include <openrct2/localisation/Language.h>
# include <openrct2/localisation/Localisation.h>
# include <openrct2/scripting/Plugin.h>
# include <openrct2/sprites.h>
# include <optional>

View file

@ -17,8 +17,7 @@
#include <openrct2/drawing/Drawing.h>
#include <openrct2/drawing/Font.h>
#include <openrct2/interface/Screenshot.h>
#include <openrct2/localisation/Formatter.h>
#include <openrct2/localisation/Localisation.h>
#include <openrct2/localisation/Formatting.h>
namespace OpenRCT2::Ui::Windows
{

View file

@ -26,9 +26,8 @@
#include <openrct2/actions/ParkSetNameAction.h>
#include <openrct2/config/Config.h>
#include <openrct2/localisation/Currency.h>
#include <openrct2/localisation/Formatter.h>
#include <openrct2/localisation/Formatting.h>
#include <openrct2/localisation/Localisation.Date.h>
#include <openrct2/localisation/Localisation.h>
#include <openrct2/management/Award.h>
#include <openrct2/peep/PeepAnimationData.h>
#include <openrct2/ride/RideData.h>

View file

@ -13,7 +13,7 @@
#include <openrct2/Context.h>
#include <openrct2/drawing/Drawing.h>
#include <openrct2/drawing/Text.h>
#include <openrct2/localisation/Localisation.h>
#include <openrct2/localisation/Formatting.h>
#include <openrct2/localisation/StringIds.h>
#include <openrct2/sprites.h>
#include <random>

View file

@ -21,7 +21,6 @@
# include <openrct2/drawing/Text.h>
# include <openrct2/interface/Colour.h>
# include <openrct2/localisation/Formatter.h>
# include <openrct2/localisation/Localisation.h>
# include <openrct2/network/ServerList.h>
# include <openrct2/network/network.h>
# include <openrct2/platform/Platform.h>

View file

@ -17,7 +17,6 @@
# include <openrct2/ParkImporter.h>
# include <openrct2/config/Config.h>
# include <openrct2/interface/Chat.h>
# include <openrct2/localisation/Localisation.h>
# include <openrct2/network/network.h>
# include <openrct2/util/Util.h>
# include <openrct2/windows/Intent.h>

View file

@ -16,7 +16,7 @@
#include <openrct2/core/String.hpp>
#include <openrct2/core/UTF8.h>
#include <openrct2/drawing/Drawing.h>
#include <openrct2/localisation/Localisation.h>
#include <openrct2/localisation/Formatting.h>
#include <openrct2/localisation/StringIds.h>
#include <openrct2/util/Util.h>

View file

@ -16,7 +16,6 @@
#include <openrct2/drawing/Drawing.h>
#include <openrct2/localisation/Formatter.h>
#include <openrct2/localisation/Formatting.h>
#include <openrct2/localisation/Localisation.h>
namespace OpenRCT2::Ui::Windows
{

View file

@ -17,8 +17,7 @@
#include <openrct2/config/Config.h>
#include <openrct2/core/String.hpp>
#include <openrct2/drawing/IDrawingEngine.h>
#include <openrct2/localisation/Formatter.h>
#include <openrct2/localisation/Localisation.h>
#include <openrct2/localisation/Formatting.h>
#include <openrct2/ride/RideConstruction.h>
#include <openrct2/ride/RideData.h>
#include <openrct2/ride/TrackDesign.h>

View file

@ -1,9 +1,11 @@
#include "GameActionResult.h"
#include "../localisation/Localisation.h"
#include "../localisation/Formatting.h"
#include <algorithm>
using namespace OpenRCT2;
namespace GameActions
{
Result::Result(GameActions::Status error, StringId title, StringId message, uint8_t* args /*= nullptr*/)

View file

@ -22,8 +22,8 @@
#include "../drawing/IDrawingEngine.h"
#include "../interface/Window.h"
#include "../localisation/Currency.h"
#include "../localisation/Formatting.h"
#include "../localisation/Language.h"
#include "../localisation/Localisation.h"
#include "../localisation/StringIds.h"
#include "../network/network.h"
#include "../paint/VirtualFloor.h"

View file

@ -14,7 +14,6 @@
#include "../interface/Colour.h"
#include "../localisation/Formatter.h"
#include "../localisation/Formatting.h"
#include "../localisation/Localisation.h"
#include "../localisation/LocalisationService.h"
#include "../paint/Paint.h"
#include "../sprites.h"

View file

@ -12,7 +12,6 @@
#include "../core/UTF8.h"
#include "../localisation/Formatter.h"
#include "../localisation/Formatting.h"
#include "../localisation/Localisation.h"
#include "Drawing.h"
class StaticLayout
@ -153,7 +152,7 @@ int32_t DrawTextWrapped(
{
const void* args = ft.Data();
StaticLayout layout(FormatStringIDLegacy(format, args), textPaint, width);
StaticLayout layout(OpenRCT2::FormatStringIDLegacy(format, args), textPaint, width);
if (textPaint.Alignment == TextAlignment::CENTRE)
{

View file

@ -29,7 +29,6 @@
#include "../interface/Window_internal.h"
#include "../localisation/Formatter.h"
#include "../localisation/Formatting.h"
#include "../localisation/Localisation.h"
#include "../management/Finance.h"
#include "../management/Marketing.h"
#include "../management/NewsItem.h"

View file

@ -291,7 +291,6 @@
<ClInclude Include="localisation\Language.h" />
<ClInclude Include="localisation\LanguagePack.h" />
<ClInclude Include="localisation\Localisation.Date.h" />
<ClInclude Include="localisation\Localisation.h" />
<ClInclude Include="localisation\LocalisationService.h" />
<ClInclude Include="localisation\StringIds.h" />
<ClInclude Include="management\Award.h" />
@ -821,7 +820,6 @@
<ClCompile Include="localisation\Formatting.cpp" />
<ClCompile Include="localisation\Language.cpp" />
<ClCompile Include="localisation\LanguagePack.cpp" />
<ClCompile Include="localisation\Localisation.cpp" />
<ClCompile Include="localisation\Localisation.Date.cpp" />
<ClCompile Include="localisation\LocalisationService.cpp" />
<ClCompile Include="management\Award.cpp" />

View file

@ -9,6 +9,7 @@
#include "Formatting.h"
#include "../Diagnostic.h"
#include "../config/Config.h"
#include "../peep/RealNames.h"
#include "../util/Util.h"
@ -813,15 +814,6 @@ namespace OpenRCT2
}
}
size_t FormatStringLegacy(char* buffer, size_t bufferLen, StringId id, const void* args)
{
thread_local std::vector<FormatArg_t> anyArgs;
anyArgs.clear();
auto fmt = GetFmtStringById(id);
BuildAnyArgListFromLegacyArgBuffer(fmt, anyArgs, args);
return FormatStringAny(buffer, bufferLen, fmt, anyArgs);
}
static void FormatMonthYear(FormatBuffer& ss, int32_t month, int32_t year)
{
thread_local std::vector<FormatArg_t> tempArgs;
@ -837,4 +829,67 @@ namespace OpenRCT2
FormatStringAny(ss, fmt, tempArgs, argIndex);
}
size_t FormatStringLegacy(char* buffer, size_t bufferLen, StringId id, const void* args)
{
thread_local std::vector<FormatArg_t> anyArgs;
anyArgs.clear();
auto fmt = GetFmtStringById(id);
BuildAnyArgListFromLegacyArgBuffer(fmt, anyArgs, args);
return FormatStringAny(buffer, bufferLen, fmt, anyArgs);
}
std::string FormatStringIDLegacy(StringId format, const void* args)
{
std::string buffer(256, 0);
size_t len{};
for (;;)
{
FormatStringLegacy(buffer.data(), buffer.size(), format, args);
len = buffer.find('\0');
if (len == std::string::npos)
{
len = buffer.size();
}
if (len >= buffer.size() - 1)
{
// Null terminator to close to end of buffer, grow buffer and try again
buffer.resize(buffer.size() * 2);
}
else
{
buffer.resize(len);
break;
}
}
return buffer;
}
/**
* Writes a formatted string to a buffer and converts it to upper case.
* rct2: 0x006C2538
* dest (edi)
* format (ax)
* args (ecx)
*/
void FormatStringToUpper(utf8* dest, size_t size, StringId format, const void* args)
{
if (size == 0)
{
return;
}
FormatStringLegacy(dest, size, format, args);
std::string upperString = String::ToUpper(dest);
if (upperString.size() + 1 >= size)
{
upperString.resize(size - 1);
dest[size - 1] = '\0';
LOG_WARNING("Truncating formatted string \"%s\" to %d bytes.", dest, size);
}
upperString.copy(dest, upperString.size());
dest[upperString.size()] = '\0';
}
} // namespace OpenRCT2

View file

@ -23,6 +23,9 @@
namespace OpenRCT2
{
// TODO: find a better spot for this (RCT12.h?)
constexpr size_t kUserStringMaxLength = 32;
template<typename T, size_t StackSize = 256, typename TTraits = std::char_traits<T>> class FormatBufferBase
{
T _storage[StackSize];
@ -304,5 +307,9 @@ namespace OpenRCT2
std::string FormatStringAny(const FmtString& fmt, const std::vector<FormatArg_t>& args);
size_t FormatStringAny(char* buffer, size_t bufferLen, const FmtString& fmt, const std::vector<FormatArg_t>& args);
// TODO: the following three functions should not be used in new code.
size_t FormatStringLegacy(char* buffer, size_t bufferLen, StringId id, const void* args);
std::string FormatStringIDLegacy(StringId format, const void* args);
void FormatStringToUpper(char* dest, size_t size, StringId format, const void* args);
} // namespace OpenRCT2

View file

@ -1,81 +0,0 @@
/*****************************************************************************
* Copyright (c) 2014-2024 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "Localisation.h"
#include "../Diagnostic.h"
#include "../Game.h"
#include "../config/Config.h"
#include "../core/Guard.hpp"
#include "../core/String.hpp"
#include "../ride/Ride.h"
#include "../util/Util.h"
#include "Currency.h"
#include "FormatCodes.h"
#include "Formatting.h"
#include "Localisation.Date.h"
#include <cstring>
using namespace OpenRCT2;
std::string FormatStringIDLegacy(StringId format, const void* args)
{
std::string buffer(256, 0);
size_t len{};
for (;;)
{
OpenRCT2::FormatStringLegacy(buffer.data(), buffer.size(), format, args);
len = buffer.find('\0');
if (len == std::string::npos)
{
len = buffer.size();
}
if (len >= buffer.size() - 1)
{
// Null terminator to close to end of buffer, grow buffer and try again
buffer.resize(buffer.size() * 2);
}
else
{
buffer.resize(len);
break;
}
}
return buffer;
}
/**
* Writes a formatted string to a buffer and converts it to upper case.
* rct2: 0x006C2538
* dest (edi)
* format (ax)
* args (ecx)
*/
void FormatStringToUpper(utf8* dest, size_t size, StringId format, const void* args)
{
if (size == 0)
{
return;
}
OpenRCT2::FormatStringLegacy(dest, size, format, args);
std::string upperString = String::ToUpper(dest);
if (upperString.size() + 1 >= size)
{
upperString.resize(size - 1);
dest[size - 1] = '\0';
LOG_WARNING("Truncating formatted string \"%s\" to %d bytes.", dest, size);
}
upperString.copy(dest, upperString.size());
dest[upperString.size()] = '\0';
}

View file

@ -1,19 +0,0 @@
/*****************************************************************************
* Copyright (c) 2014-2024 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#pragma once
#include "StringIdType.h"
#include <string>
std::string FormatStringIDLegacy(StringId format, const void* args);
void FormatStringToUpper(char* dest, size_t size, StringId format, const void* args);
constexpr size_t kUserStringMaxLength = 32;

View file

@ -79,7 +79,6 @@ static constexpr uint32_t MaxPacketsPerUpdate = 100;
# include "../interface/Chat.h"
# include "../interface/Window.h"
# include "../localisation/Localisation.Date.h"
# include "../localisation/Localisation.h"
# include "../object/ObjectManager.h"
# include "../object/ObjectRepository.h"
# include "../scenario/Scenario.h"

View file

@ -13,7 +13,6 @@
# include "../core/String.hpp"
# include "../localisation/Formatting.h"
# include "../localisation/Localisation.h"
# include "../platform/Platform.h"
# include "Socket.h"
# include "network.h"

View file

@ -12,7 +12,6 @@
# include "NetworkPlayer.h"
# include "../interface/Window.h"
# include "../localisation/Localisation.h"
# include "NetworkPacket.h"
void NetworkPlayer::SetName(std::string_view name)

View file

@ -15,7 +15,6 @@
#include "../../interface/Viewport.h"
#include "../../localisation/Formatter.h"
#include "../../localisation/Formatting.h"
#include "../../localisation/Localisation.h"
#include "../../localisation/StringIds.h"
#include "../../object/BannerObject.h"
#include "../../object/ObjectEntryManager.h"

View file

@ -17,7 +17,6 @@
#include "../../interface/Viewport.h"
#include "../../localisation/Formatter.h"
#include "../../localisation/Formatting.h"
#include "../../localisation/Localisation.h"
#include "../../object/EntranceObject.h"
#include "../../object/ObjectManager.h"
#include "../../object/StationObject.h"

View file

@ -17,7 +17,6 @@
#include "../../core/UTF8.h"
#include "../../interface/Viewport.h"
#include "../../localisation/Formatting.h"
#include "../../localisation/Localisation.h"
#include "../../localisation/StringIds.h"
#include "../../object/LargeSceneryObject.h"
#include "../../profiling/Profiling.h"

View file

@ -18,7 +18,6 @@
#include "../../interface/Viewport.h"
#include "../../localisation/Formatter.h"
#include "../../localisation/Formatting.h"
#include "../../localisation/Localisation.h"
#include "../../localisation/StringIds.h"
#include "../../object/FootpathObject.h"
#include "../../object/FootpathRailingsObject.h"

View file

@ -16,7 +16,6 @@
#include "../../interface/Colour.h"
#include "../../interface/Viewport.h"
#include "../../localisation/Formatting.h"
#include "../../localisation/Localisation.h"
#include "../../localisation/StringIds.h"
#include "../../object/WallSceneryEntry.h"
#include "../../profiling/Profiling.h"

View file

@ -37,8 +37,8 @@
#include "../entity/Peep.h"
#include "../entity/Staff.h"
#include "../interface/Window.h"
#include "../localisation/Formatting.h"
#include "../localisation/Localisation.Date.h"
#include "../localisation/Localisation.h"
#include "../management/Award.h"
#include "../management/Finance.h"
#include "../management/Marketing.h"

View file

@ -35,8 +35,8 @@
#include "../entity/PatrolArea.h"
#include "../entity/Staff.h"
#include "../interface/Viewport.h"
#include "../localisation/Formatting.h"
#include "../localisation/Localisation.Date.h"
#include "../localisation/Localisation.h"
#include "../management/Award.h"
#include "../management/Finance.h"
#include "../management/Marketing.h"

View file

@ -35,7 +35,6 @@
#include "../localisation/Formatter.h"
#include "../localisation/Formatting.h"
#include "../localisation/Localisation.Date.h"
#include "../localisation/Localisation.h"
#include "../management/Finance.h"
#include "../management/Marketing.h"
#include "../management/NewsItem.h"

View file

@ -11,7 +11,7 @@
#include "../../../Identifiers.h"
#include "../../../core/String.hpp"
#include "../../../localisation/Localisation.h"
#include "../../../localisation/Formatting.h"
#include <cstdint>

View file

@ -23,7 +23,6 @@
#include "../../interface/Screenshot.h"
#include "../../interface/Viewport.h"
#include "../../interface/Window.h"
#include "../../localisation/Localisation.h"
#include "../../network/NetworkBase.h"
#include "../../network/network.h"
#include "../../scenario/Scenario.h"

View file

@ -13,7 +13,7 @@
# include "../../../GameState.h"
# include "../../../entity/Guest.h"
# include "../../../localisation/Localisation.h"
# include "../../../localisation/Formatting.h"
# include "../../../peep/PeepAnimationData.h"
# include "../../../ride/RideEntry.h"

View file

@ -16,9 +16,8 @@
#include "../core/Memory.hpp"
#include "../core/String.hpp"
#include "../interface/Window.h"
#include "../localisation/FormatCodes.h"
#include "../localisation/Formatter.h"
#include "../localisation/Localisation.h"
#include "../localisation/Formatting.h"
#include "../management/Finance.h"
#include "../network/network.h"
#include "../object/BannerSceneryEntry.h"

View file

@ -7,8 +7,6 @@
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "openrct2/localisation/Formatting.h"
#include <gtest/gtest.h>
#include <memory>
#include <openrct2/Context.h>
@ -16,7 +14,7 @@
#include <openrct2/config/Config.h>
#include <openrct2/core/String.hpp>
#include <openrct2/localisation/Formatter.h>
#include <openrct2/localisation/Localisation.h>
#include <openrct2/localisation/Formatting.h>
#include <openrct2/localisation/StringIds.h>
#include <openrct2/ride/RideStringIds.h>
#include <sstream>