Rework the trip history window layout.
[openttd-joker.git] / src / saveload / game_sl.cpp
blob411b91222ba325622b1646517511397c416d2af4
1 /* $Id: game_sl.cpp 25817 2013-10-06 12:10:33Z frosch $ */
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 */
10 /** @file game_sl.cpp Handles the saveload part of the GameScripts */
12 #include "../stdafx.h"
13 #include "../debug.h"
14 #include "saveload.h"
15 #include "../string_func.h"
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"
23 #include "../safeguards.h"
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;
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()
38 static void SaveReal_GSDT(int *index_ptr)
40 GameConfig *config = GameConfig::GetConfig();
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;
51 _game_saveload_is_random = config->IsRandom();
52 _game_saveload_settings[0] = '\0';
53 config->SettingsToString(_game_saveload_settings, lastof(_game_saveload_settings));
55 SlObject(NULL, _game_script);
56 Game::Save();
59 static void Load_GSDT()
61 /* Free all current data */
62 GameConfig::GetConfig(GameConfig::SSS_FORCE_GAME)->Change(NULL);
64 if ((CompanyID)SlIterateArray() == (CompanyID)-1) return;
66 _game_saveload_version = -1;
67 SlObject(NULL, _game_script);
69 if (_networking && !_network_server) {
70 GameInstance::LoadEmpty();
71 if ((CompanyID)SlIterateArray() != (CompanyID)-1) SlErrorCorrupt("Too many GameScript configs");
72 return;
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.");
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.");
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;
101 config->StringToSettings(_game_saveload_settings);
103 /* Start the GameScript directly if it was active in the savegame */
104 Game::StartNew();
105 Game::Load(_game_saveload_version);
107 if ((CompanyID)SlIterateArray() != (CompanyID)-1) SlErrorCorrupt("Too many GameScript configs");
110 static void Save_GSDT()
112 SlSetArrayIndex(0);
113 SlAutolength((AutolengthProc *)SaveReal_GSDT, NULL);
116 extern GameStrings *_current_data;
118 static const char *_game_saveload_string;
119 static uint _game_saveload_strings;
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()
127 static const SaveLoad _game_language_string[] = {
128 SLEG_STR(_game_saveload_string, SLE_STR | SLF_ALLOW_CONTROL),
129 SLE_END()
132 static void SaveReal_GSTR(LanguageStrings *ls)
134 _game_saveload_string = ls->language;
135 _game_saveload_strings = ls->lines.Length();
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);
144 static void Load_GSTR()
146 delete _current_data;
147 _current_data = new GameStrings();
149 while (SlIterateArray() != -1) {
150 _game_saveload_string = NULL;
151 SlObject(NULL, _game_language_header);
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 : "");
159 *_current_data->raw_strings.Append() = ls;
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;
169 _current_data->Compile();
170 ReconsiderGameScriptLanguage();
173 static void Save_GSTR()
175 if (_current_data == NULL) return;
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]);
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},