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
8 /** @file strings_sl.cpp Code handling saving and loading of strings */
9
10 #include "../stdafx.h"
11 #include "../string_func.h"
12 #include "../strings_func.h"
13 #include "saveload_internal.h"
14 #include <sstream>
15
16 #include "table/strings.h"
17
18 #include "../safeguards.h"
19
20 static const int NUM_OLD_STRINGS = 512; ///< The number of custom strings stored in old savegames.
21 static const int LEN_OLD_STRINGS = 32; ///< The number of characters per string.
22 static const int LEN_OLD_STRINGS_TTO = 24; ///< The number of characters per string in TTO savegames.
23
24 /**
25 * Remap a string ID from the old format to the new format
26 * @param s StringID that requires remapping
27 * @return translated ID
28 */
RemapOldStringID(StringID s)29 StringID RemapOldStringID(StringID s)
30 {
31 switch (s) {
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
50 /** Location to load the old names to. */
51 char *_old_name_array = nullptr;
52
53 /**
54 * Copy and convert old custom names to UTF-8.
55 * They were all stored in a 512 by 32 (200 by 24 for TTO) long string array
56 * and are now stored with stations, waypoints and other places with names.
57 * @param id the StringID of the custom name to clone.
58 * @return the clones custom name.
59 */
CopyFromOldName(StringID id)60 std::string CopyFromOldName(StringID id)
61 {
62 /* Is this name an (old) custom name? */
63 if (GetStringTab(id) != TEXT_TAB_OLD_CUSTOM) return std::string();
64
65 if (IsSavegameVersionBefore(SLV_37)) {
66 uint offs = _savegame_type == SGT_TTO ? LEN_OLD_STRINGS_TTO * GB(id, 0, 8) : LEN_OLD_STRINGS * GB(id, 0, 9);
67 const char *strfrom = &_old_name_array[offs];
68
69 std::ostringstream tmp;
70 std::ostreambuf_iterator<char> strto(tmp);
71 for (; *strfrom != '\0'; strfrom++) {
72 WChar c = (byte)*strfrom;
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 Utf8Encode(strto, c);
88 }
89
90 return tmp.str();
91 } else {
92 /* Name will already be in UTF-8. */
93 return std::string(&_old_name_array[LEN_OLD_STRINGS * GB(id, 0, 9)]);
94 }
95 }
96
97 /**
98 * Free the memory of the old names array.
99 * Should be called once the old names have all been converted.
100 */
ResetOldNames()101 void ResetOldNames()
102 {
103 free(_old_name_array);
104 _old_name_array = nullptr;
105 }
106
107 /**
108 * Initialize the old names table memory.
109 */
InitializeOldNames()110 void InitializeOldNames()
111 {
112 free(_old_name_array);
113 _old_name_array = CallocT<char>(NUM_OLD_STRINGS * LEN_OLD_STRINGS); // 200 * 24 would be enough for TTO savegames
114 }
115
116 struct NAMEChunkHandler : ChunkHandler {
NAMEChunkHandlerNAMEChunkHandler117 NAMEChunkHandler() : ChunkHandler('NAME', CH_READONLY) {}
118
LoadNAMEChunkHandler119 void Load() const override
120 {
121 int index;
122
123 while ((index = SlIterateArray()) != -1) {
124 if (index >= NUM_OLD_STRINGS) SlErrorCorrupt("Invalid old name index");
125 if (SlGetFieldLength() > (uint)LEN_OLD_STRINGS) SlErrorCorrupt("Invalid old name length");
126
127 SlCopy(&_old_name_array[LEN_OLD_STRINGS * index], SlGetFieldLength(), SLE_UINT8);
128 /* Make sure the old name is null terminated */
129 _old_name_array[LEN_OLD_STRINGS * index + LEN_OLD_STRINGS - 1] = '\0';
130 }
131 }
132 };
133
134 static const NAMEChunkHandler NAME;
135 static const ChunkHandlerRef name_chunk_handlers[] = {
136 NAME,
137 };
138
139 extern const ChunkHandlerTable _name_chunk_handlers(name_chunk_handlers);
140