OpenTTD Source 20261112-master-gbcbec5e63f
strings_sl.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../stdafx.h"
11#include "../string_func.h"
12#include "../strings_func.h"
14#include "saveload_error.hpp"
15#include "saveload_internal.h"
16
17#include "table/strings.h"
18
19#include "../safeguards.h"
20
21static const int NUM_OLD_STRINGS = 512;
22static const size_t LEN_OLD_STRINGS = 32;
23
30{
31 switch (s.base()) {
32 case 0x0006: return STR_SV_EMPTY;
33 case 0x7000: return STR_SV_UNNAMED;
34 case 0x70E4: return SPECSTR_COMPANY_NAME_START;
35 case 0x70E9: return SPECSTR_COMPANY_NAME_START;
36 case 0x8864: return STR_SV_TRAIN_NAME;
37 case 0x902B: return STR_SV_ROAD_VEHICLE_NAME;
38 case 0x9830: return STR_SV_SHIP_NAME;
39 case 0xA02F: return STR_SV_AIRCRAFT_NAME;
40
41 default:
42 if (IsInsideMM(s, 0x300F, 0x3030)) {
43 return s - 0x300F + STR_SV_STNAME;
44 } else {
45 return s;
46 }
47 }
48}
49
51std::unique_ptr<std::string[]> _old_name_array;
52
61{
62 /* Is this name an (old) custom name? */
63 if (GetStringTab(id) != TEXT_TAB_OLD_CUSTOM) return std::string();
64
66 const std::string &strfrom = _old_name_array[GB(id.base(), 0, 9)];
67
68 std::string result;
69 StringBuilder builder(result);
70 for (char s : strfrom) {
71 if (s == '\0') break;
72 char32_t c = static_cast<uint8_t>(s); // cast to unsigned before integer promotion
73
74 /* Map from non-ISO8859-15 characters to UTF-8. */
75 switch (c) {
76 case 0xA4: c = 0x20AC; break; // Euro
77 case 0xA6: c = 0x0160; break; // S with caron
78 case 0xA8: c = 0x0161; break; // s with caron
79 case 0xB4: c = 0x017D; break; // Z with caron
80 case 0xB8: c = 0x017E; break; // z with caron
81 case 0xBC: c = 0x0152; break; // OE ligature
82 case 0xBD: c = 0x0153; break; // oe ligature
83 case 0xBE: c = 0x0178; break; // Y with diaeresis
84 default: break;
85 }
86
87 if (IsPrintable(c)) builder.PutUtf8(c);
88 }
89
90 return result;
91 } else {
92 /* Name will already be in UTF-8. */
93 return StrMakeValid(_old_name_array[GB(id.base(), 0, 9)]);
94 }
95}
96
102{
103 _old_name_array = nullptr;
104}
105
110{
111 _old_name_array = std::make_unique<std::string[]>(NUM_OLD_STRINGS); // 200 would be enough for TTO savegames
112}
113
114struct NAMEChunkHandler : ChunkHandler {
115 NAMEChunkHandler() : ChunkHandler("NAME", ChunkType::ReadOnly) {}
116
117 void Load() const override
118 {
119 int index;
120
121 while ((index = SlIterateArray()) != -1) {
122 if (index >= NUM_OLD_STRINGS) SlErrorCorrupt("Invalid old name index");
123 size_t length = SlGetFieldLength();
124 if (length > LEN_OLD_STRINGS) SlErrorCorrupt("Invalid old name length");
125
126 SlReadString(_old_name_array[index], length);
127 }
128 }
129};
130
131static const NAMEChunkHandler NAME;
132static const ChunkHandlerRef name_chunk_handlers[] = {
133 NAME,
134};
135
136extern const ChunkHandlerTable _name_chunk_handlers(name_chunk_handlers);
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
void PutUtf8(char32_t c)
Append UTF-8 char.
Compose data into a growing std::string.
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
std::unique_ptr< std::string[]> _old_name_array
Location to load the old names to.
A number of safeguards to prevent using unsafe methods.
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition saveload.cpp:748
size_t SlGetFieldLength()
Get the length of the current object.
Definition saveload.cpp:874
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition saveload.cpp:372
void SlReadString(std::string &str, size_t length)
Read the given amount of bytes from the buffer into the string.
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:512
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:515
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1225
@ Utf8
Saveload version: 37, SVN revision: 7182 UTF-8 strings.
Definition saveload.h:93
@ ReadOnly
Chunk is never saved.
Definition saveload.h:453
Functions/types related to errors from savegames.
Declaration of functions used in more save/load files.
Definition of base types and functions in a cross-platform compatible way.
static void StrMakeValid(Builder &builder, StringConsumer &consumer, StringValidationSettings settings)
Copies the valid (UTF-8) characters from consumer to the builder.
Definition string.cpp:118
Compose strings from textual and binary data.
Functions related to low-level strings.
Functions related to OTTD's strings.
StringTab GetStringTab(StringID str)
Extract the StringTab from a StringID.
std::unique_ptr< std::string[]> _old_name_array
Location to load the old names to.
void ResetOldNames()
Free the memory of the old names array.
StringID RemapOldStringID(StringID s)
Remap a string ID from the old format to the new format.
static const int NUM_OLD_STRINGS
The number of custom strings stored in old savegames.
static const size_t LEN_OLD_STRINGS
The number of characters per string.
void InitializeOldNames()
Initialize the old names table memory.
std::string CopyFromOldName(StringID id)
Copy and convert old custom names to UTF-8.
static constexpr StringID SPECSTR_COMPANY_NAME_START
Special strings for company names on the form "TownName transport".
StrongType::Typedef< uint32_t, struct StringIDTag, StrongType::Compare, StrongType::Integer > StringID
Numeric value that represents a string, independent of the selected language.
ChunkHandler(ChunkId id, ChunkType type)
Create this ChunkHandler.
Definition saveload.h:469
void Load() const override
Load the chunk.