OpenTTD: src/saveload/game_sl.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

game_sl.cpp
Go to the documentation of this file.
1 /* $Id: game_sl.cpp 27003 2014-10-12 18:41:53Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #include "../stdafx.h"
13 #include "../debug.h"
14 #include "saveload.h"
15 #include "../string_func.h"
16
17 #include "../game/game.hpp"
18 #include "../game/game_config.hpp"
19 #include "../network/network.h"
20 #include "../game/game_instance.hpp"
21 #include "../game/game_text.hpp"
22
23 #include "../safeguards.h"
24
25 static char _game_saveload_name[64];
26 static int _game_saveload_version;
27 static char _game_saveload_settings[1024];
28 static bool _game_saveload_is_random;
29
30 static const SaveLoad _game_script[] = {
31 SLEG_STR(_game_saveload_name, SLE_STRB),
32 SLEG_STR(_game_saveload_settings, SLE_STRB),
33 SLEG_VAR(_game_saveload_version, SLE_UINT32),
34 SLEG_VAR(_game_saveload_is_random, SLE_BOOL),
35 SLE_END()
36 };
37
38 static void SaveReal_GSDT(int *index_ptr)
39 {
40 GameConfig *config = GameConfig::GetConfig();
41
42 if (config->HasScript()) {
43 strecpy(_game_saveload_name, config->GetName(), lastof(_game_saveload_name));
44 _game_saveload_version = config->GetVersion();
45 } else {
46 /* No GameScript is configured for this so store an empty string as name. */
47 _game_saveload_name[0] = '\0';
48 _game_saveload_version = -1;
49 }
50
51 _game_saveload_is_random = config->IsRandom();
52 _game_saveload_settings[0] = '\0';
53 config->SettingsToString(_game_saveload_settings, lastof(_game_saveload_settings));
54
55 SlObject(NULL, _game_script);
56 Game::Save();
57 }
58
59 static void Load_GSDT()
60 {
61 /* Free all current data */
62 GameConfig::GetConfig(GameConfig::SSS_FORCE_GAME)->Change(NULL);
63
64 if ((CompanyID)SlIterateArray() == (CompanyID)-1) return;
65
66 _game_saveload_version = -1;
67 SlObject(NULL, _game_script);
68
69 if (_networking && !_network_server) {
70 GameInstance::LoadEmpty();
71 if ((CompanyID)SlIterateArray() != (CompanyID)-1) SlErrorCorrupt("Too many GameScript configs");
72 return;
73 }
74
75 GameConfig *config = GameConfig::GetConfig(GameConfig::SSS_FORCE_GAME);
76 if (StrEmpty(_game_saveload_name)) {
77 } else {
78 config->Change(_game_saveload_name, _game_saveload_version, false, _game_saveload_is_random);
79 if (!config->HasScript()) {
80 /* No version of the GameScript available that can load the data. Try to load the
81 * latest version of the GameScript instead. */
82 config->Change(_game_saveload_name, -1, false, _game_saveload_is_random);
83 if (!config->HasScript()) {
84 if (strcmp(_game_saveload_name, "%_dummy") != 0) {
85 DEBUG(script, 0, "The savegame has an GameScript by the name '%s', version %d which is no longer available.", _game_saveload_name, _game_saveload_version);
86 DEBUG(script, 0, "This game will continue to run without GameScript.");
87 } else {
88 DEBUG(script, 0, "The savegame had no GameScript available at the time of saving.");
89 DEBUG(script, 0, "This game will continue to run without GameScript.");
90 }
91 } else {
92 DEBUG(script, 0, "The savegame has an GameScript by the name '%s', version %d which is no longer available.", _game_saveload_name, _game_saveload_version);
93 DEBUG(script, 0, "The latest version of that GameScript has been loaded instead, but it'll not get the savegame data as it's incompatible.");
94 }
95 /* Make sure the GameScript doesn't get the saveload data, as he was not the
96 * writer of the saveload data in the first place */
97 _game_saveload_version = -1;
98 }
99 }
100
101 config->StringToSettings(_game_saveload_settings);
102
103 /* Start the GameScript directly if it was active in the savegame */
104 Game::StartNew();
105 Game::Load(_game_saveload_version);
106
107 if ((CompanyID)SlIterateArray() != (CompanyID)-1) SlErrorCorrupt("Too many GameScript configs");
108 }
109
110 static void Save_GSDT()
111 {
112 SlSetArrayIndex(0);
113 SlAutolength((AutolengthProc *)SaveReal_GSDT, NULL);
114 }
115
116 extern GameStrings *_current_data;
117
118 static const char *_game_saveload_string;
119 static uint _game_saveload_strings;
120
121 static const SaveLoad _game_language_header[] = {
122 SLEG_STR(_game_saveload_string, SLE_STR),
123 SLEG_VAR(_game_saveload_strings, SLE_UINT32),
124 SLE_END()
125 };
126
127 static const SaveLoad _game_language_string[] = {
128 SLEG_STR(_game_saveload_string, SLE_STR | SLF_ALLOW_CONTROL),
129 SLE_END()
130 };
131
132 static void SaveReal_GSTR(LanguageStrings *ls)
133 {
134 _game_saveload_string = ls->language;
135 _game_saveload_strings = ls->lines.Length();
136
137 SlObject(NULL, _game_language_header);
138 for (uint i = 0; i < _game_saveload_strings; i++) {
139 _game_saveload_string = ls->lines[i];
140 SlObject(NULL, _game_language_string);
141 }
142 }
143
144 static void Load_GSTR()
145 {
146 delete _current_data;
147 _current_data = new GameStrings();
148
149 while (SlIterateArray() != -1) {
150 _game_saveload_string = NULL;
151 SlObject(NULL, _game_language_header);
152
153 LanguageStrings *ls = new LanguageStrings(_game_saveload_string != NULL ? _game_saveload_string : "");
154 for (uint i = 0; i < _game_saveload_strings; i++) {
155 SlObject(NULL, _game_language_string);
156 *ls->lines.Append() = stredup(_game_saveload_string != NULL ? _game_saveload_string : "");
157 }
158
159 *_current_data->raw_strings.Append() = ls;
160 }
161
162 /* If there were no strings in the savegame, set GameStrings to NULL */
163 if (_current_data->raw_strings.Length() == 0) {
164 delete _current_data;
165 _current_data = NULL;
166 return;
167 }
168
169 _current_data->Compile();
170 ReconsiderGameScriptLanguage();
171 }
172
173 static void Save_GSTR()
174 {
175 if (_current_data == NULL) return;
176
177 for (uint i = 0; i < _current_data->raw_strings.Length(); i++) {
178 SlSetArrayIndex(i);
179 SlAutolength((AutolengthProc *)SaveReal_GSTR, _current_data->raw_strings[i]);
180 }
181 }
182
183 extern const ChunkHandler _game_chunk_handlers[] = {
184 { 'GSTR', Save_GSTR, Load_GSTR, NULL, NULL, CH_ARRAY },
185 { 'GSDT', Save_GSDT, Load_GSDT, NULL, NULL, CH_ARRAY | CH_LAST},
186 };