OpenTTD Source: src/saveload/strings_sl.cpp Source File

OpenTTD Source 14.1

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 <http://www.gnu.org/licenses/>.
6 */
7
10 #include "../stdafx.h"
11 #include "../core/alloc_func.hpp"
12 #include "../string_func.h"
13 #include "../strings_func.h"
14 #include "saveload_internal.h"
15 #include <sstream>
16
17 #include "table/strings.h"
18
19 #include "../safeguards.h"
20
21 static const int NUM_OLD_STRINGS = 512;
22 static const int LEN_OLD_STRINGS = 32;
23 static const int LEN_OLD_STRINGS_TTO = 24;
24
30 StringID RemapOldStringID(StringID s)
31 {
32 switch (s) {
33 case 0x0006: return STR_SV_EMPTY;
34 case 0x7000: return STR_SV_UNNAMED;
35 case 0x70E4: return SPECSTR_COMPANY_NAME_START;
36 case 0x70E9: return SPECSTR_COMPANY_NAME_START;
37 case 0x8864: return STR_SV_TRAIN_NAME;
38 case 0x902B: return STR_SV_ROAD_VEHICLE_NAME;
39 case 0x9830: return STR_SV_SHIP_NAME;
40 case 0xA02F: return STR_SV_AIRCRAFT_NAME;
41
42 default:
43 if (IsInsideMM(s, 0x300F, 0x3030)) {
44 return s - 0x300F + STR_SV_STNAME;
45 } else {
46 return s;
47 }
48 }
49 }
50
52 char *_old_name_array = nullptr;
53
61 std::string CopyFromOldName(StringID id)
62 {
63 /* Is this name an (old) custom name? */
64 if (GetStringTab(id) != TEXT_TAB_OLD_CUSTOM) return std::string();
65
66 if (IsSavegameVersionBefore(SLV_37)) {
67 uint offs = _savegame_type == SGT_TTO ? LEN_OLD_STRINGS_TTO * GB(id, 0, 8) : LEN_OLD_STRINGS * GB(id, 0, 9);
68 const char *strfrom = &_old_name_array[offs];
69
70 std::ostringstream tmp;
71 std::ostreambuf_iterator<char> strto(tmp);
72 for (; *strfrom != '\0'; strfrom++) {
73 char32_t c = (byte)*strfrom;
74
75 /* Map from non-ISO8859-15 characters to UTF-8. */
76 switch (c) {
77 case 0xA4: c = 0x20AC; break; // Euro
78 case 0xA6: c = 0x0160; break; // S with caron
79 case 0xA8: c = 0x0161; break; // s with caron
80 case 0xB4: c = 0x017D; break; // Z with caron
81 case 0xB8: c = 0x017E; break; // z with caron
82 case 0xBC: c = 0x0152; break; // OE ligature
83 case 0xBD: c = 0x0153; break; // oe ligature
84 case 0xBE: c = 0x0178; break; // Y with diaeresis
85 default: break;
86 }
87
88 Utf8Encode(strto, c);
89 }
90
91 return tmp.str();
92 } else {
93 /* Name will already be in UTF-8. */
94 return std::string(&_old_name_array[LEN_OLD_STRINGS * GB(id, 0, 9)]);
95 }
96 }
97
102 void ResetOldNames()
103 {
104 free(_old_name_array);
105 _old_name_array = nullptr;
106 }
107
111 void InitializeOldNames()
112 {
113 free(_old_name_array);
114 _old_name_array = CallocT<char>(NUM_OLD_STRINGS * LEN_OLD_STRINGS); // 200 * 24 would be enough for TTO savegames
115 }
116
117 struct NAMEChunkHandler : ChunkHandler {
118 NAMEChunkHandler() : ChunkHandler('NAME', CH_READONLY) {}
119
120 void Load() const override
121 {
122 int index;
123
124 while ((index = SlIterateArray()) != -1) {
125 if (index >= NUM_OLD_STRINGS) SlErrorCorrupt("Invalid old name index");
126 if (SlGetFieldLength() > (uint)LEN_OLD_STRINGS) SlErrorCorrupt("Invalid old name length");
127
128 SlCopy(&_old_name_array[LEN_OLD_STRINGS * index], SlGetFieldLength(), SLE_UINT8);
129 /* Make sure the old name is null terminated */
130 _old_name_array[LEN_OLD_STRINGS * index + LEN_OLD_STRINGS - 1] = '\0';
131 }
132 }
133 };
134
135 static const NAMEChunkHandler NAME;
136 static const ChunkHandlerRef name_chunk_handlers[] = {
137 NAME,
138 };
139
140 extern const ChunkHandlerTable _name_chunk_handlers(name_chunk_handlers);
IsInsideMM
constexpr bool IsInsideMM(const T x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
Definition: math_func.hpp:268
NAMEChunkHandler::Load
void Load() const override
Load the chunk.
Definition: strings_sl.cpp:120
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:491
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
SlCopy
void SlCopy(void *object, size_t length, VarType conv)
Copy a list of SL_VARs to/from a savegame.
Definition: saveload.cpp:1027
GB
constexpr static debug_inline uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
SlErrorCorrupt
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:364
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:445
SLV_37
@ SLV_37
37 7182
Definition: saveload.h:87
CopyFromOldName
std::string CopyFromOldName(StringID id)
Copy and convert old custom names to UTF-8.
Definition: strings_sl.cpp:61
_savegame_type
SavegameType _savegame_type
type of savegame we are loading
Definition: saveload.cpp:59
InitializeOldNames
void InitializeOldNames()
Initialize the old names table memory.
Definition: strings_sl.cpp:111
CH_READONLY
@ CH_READONLY
Chunk is never saved.
Definition: saveload.h:441
SGT_TTO
@ SGT_TTO
TTO savegame.
Definition: saveload.h:411
IsSavegameVersionBefore
bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1215
free
void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:382
SlGetFieldLength
size_t SlGetFieldLength()
Get the length of the current object.
Definition: saveload.cpp:779
_old_name_array
char * _old_name_array
Location to load the old names to.
Definition: strings_sl.cpp:52
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:494
NUM_OLD_STRINGS
static const int NUM_OLD_STRINGS
The number of custom strings stored in old savegames.
Definition: strings_sl.cpp:21
saveload_internal.h
RemapOldStringID
StringID RemapOldStringID(StringID s)
Remap a string ID from the old format to the new format.
Definition: strings_sl.cpp:30
Utf8Encode
size_t Utf8Encode(T buf, char32_t c)
Encode a unicode character and place it in the buffer.
Definition: string.cpp:479
LEN_OLD_STRINGS_TTO
static const int LEN_OLD_STRINGS_TTO
The number of characters per string in TTO savegames.
Definition: strings_sl.cpp:23
GetStringTab
StringTab GetStringTab(StringID str)
Extract the StringTab from a StringID.
Definition: strings_func.h:25
NAMEChunkHandler
Definition: strings_sl.cpp:117
LEN_OLD_STRINGS
static const int LEN_OLD_STRINGS
The number of characters per string.
Definition: strings_sl.cpp:22
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:656
ResetOldNames
void ResetOldNames()
Free the memory of the old names array.
Definition: strings_sl.cpp:102