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/>.
8 /** @file game_sl.cpp Handles the saveload part of the GameScripts */
10 #include "../stdafx.h"
14 #include "compat/game_sl_compat.h"
16 #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 std::string _game_saveload_name
;
26 static int _game_saveload_version
;
27 static std::string _game_saveload_settings
;
29 static const SaveLoad _game_script_desc
[] = {
30 SLEG_SSTR("name", _game_saveload_name
, SLE_STR
),
31 SLEG_SSTR("settings", _game_saveload_settings
, SLE_STR
),
32 SLEG_VAR("version", _game_saveload_version
, SLE_UINT32
),
35 static void SaveReal_GSDT(int)
37 GameConfig
*config
= GameConfig::GetConfig();
39 if (config
->HasScript()) {
40 _game_saveload_name
= config
->GetName();
41 _game_saveload_version
= config
->GetVersion();
43 /* No GameScript is configured for this so store an empty string as name. */
44 _game_saveload_name
.clear();
45 _game_saveload_version
= -1;
48 _game_saveload_settings
= config
->SettingsToString();
50 SlObject(nullptr, _game_script_desc
);
54 struct GSDTChunkHandler
: ChunkHandler
{
55 GSDTChunkHandler() : ChunkHandler('GSDT', CH_TABLE
) {}
57 void Load() const override
59 const std::vector
<SaveLoad
> slt
= SlCompatTableHeader(_game_script_desc
, _game_script_sl_compat
);
61 /* Free all current data */
62 GameConfig::GetConfig(GameConfig::SSS_FORCE_GAME
)->Change(std::nullopt
);
64 if (SlIterateArray() == -1) return;
66 _game_saveload_version
= -1;
67 SlObject(nullptr, slt
);
69 if (_game_mode
== GM_MENU
|| (_networking
&& !_network_server
)) {
70 GameInstance::LoadEmpty();
71 if (SlIterateArray() != -1) SlErrorCorrupt("Too many GameScript configs");
75 GameConfig
*config
= GameConfig::GetConfig(GameConfig::SSS_FORCE_GAME
);
76 if (!_game_saveload_name
.empty()) {
77 config
->Change(_game_saveload_name
, _game_saveload_version
, false);
78 if (!config
->HasScript()) {
79 /* No version of the GameScript available that can load the data. Try to load the
80 * latest version of the GameScript instead. */
81 config
->Change(_game_saveload_name
, -1, false);
82 if (!config
->HasScript()) {
83 if (_game_saveload_name
.compare("%_dummy") != 0) {
84 Debug(script
, 0, "The savegame has an GameScript by the name '{}', version {} which is no longer available.", _game_saveload_name
, _game_saveload_version
);
85 Debug(script
, 0, "This game will continue to run without GameScript.");
87 Debug(script
, 0, "The savegame had no GameScript available at the time of saving.");
88 Debug(script
, 0, "This game will continue to run without GameScript.");
91 Debug(script
, 0, "The savegame has an GameScript by the name '{}', version {} which is no longer available.", _game_saveload_name
, _game_saveload_version
);
92 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 /* Make sure the GameScript doesn't get the saveload data, as it was not the
95 * writer of the saveload data in the first place */
96 _game_saveload_version
= -1;
100 config
->StringToSettings(_game_saveload_settings
);
102 /* Load the GameScript saved data */
103 config
->SetToLoadData(GameInstance::Load(_game_saveload_version
));
105 if (SlIterateArray() != -1) SlErrorCorrupt("Too many GameScript configs");
108 void Save() const override
110 SlTableHeader(_game_script_desc
);
112 SlAutolength(SaveReal_GSDT
, 0);
116 extern GameStrings
*_current_data
;
118 static std::string _game_saveload_string
;
119 static uint32_t _game_saveload_strings
;
121 class SlGameLanguageString
: public DefaultSaveLoadHandler
<SlGameLanguageString
, LanguageStrings
> {
123 inline static const SaveLoad description
[] = {
124 SLEG_SSTR("string", _game_saveload_string
, SLE_STR
| SLF_ALLOW_CONTROL
),
126 inline const static SaveLoadCompatTable compat_description
= _game_language_string_sl_compat
;
128 void Save(LanguageStrings
*ls
) const override
130 SlSetStructListLength(ls
->lines
.size());
132 for (const auto &string
: ls
->lines
) {
133 _game_saveload_string
= string
;
134 SlObject(nullptr, this->GetDescription());
138 void Load(LanguageStrings
*ls
) const override
140 uint32_t length
= IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH
) ? _game_saveload_strings
: (uint32_t)SlGetStructListLength(UINT32_MAX
);
142 for (uint32_t i
= 0; i
< length
; i
++) {
143 SlObject(nullptr, this->GetLoadDescription());
144 ls
->lines
.emplace_back(_game_saveload_string
);
149 static const SaveLoad _game_language_desc
[] = {
150 SLE_SSTR(LanguageStrings
, language
, SLE_STR
),
151 SLEG_CONDVAR("count", _game_saveload_strings
, SLE_UINT32
, SL_MIN_VERSION
, SLV_SAVELOAD_LIST_LENGTH
),
152 SLEG_STRUCTLIST("strings", SlGameLanguageString
),
155 struct GSTRChunkHandler
: ChunkHandler
{
156 GSTRChunkHandler() : ChunkHandler('GSTR', CH_TABLE
) {}
158 void Load() const override
160 const std::vector
<SaveLoad
> slt
= SlCompatTableHeader(_game_language_desc
, _game_language_sl_compat
);
162 delete _current_data
;
163 _current_data
= new GameStrings();
165 while (SlIterateArray() != -1) {
168 _current_data
->raw_strings
.push_back(std::move(ls
));
171 /* If there were no strings in the savegame, set GameStrings to nullptr */
172 if (_current_data
->raw_strings
.empty()) {
173 delete _current_data
;
174 _current_data
= nullptr;
178 _current_data
->Compile();
179 ReconsiderGameScriptLanguage();
182 void Save() const override
184 SlTableHeader(_game_language_desc
);
186 if (_current_data
== nullptr) return;
188 for (uint i
= 0; i
< _current_data
->raw_strings
.size(); i
++) {
190 SlObject(&_current_data
->raw_strings
[i
], _game_language_desc
);
195 static const GSTRChunkHandler GSTR
;
196 static const GSDTChunkHandler GSDT
;
197 static const ChunkHandlerRef game_chunk_handlers
[] = {
202 extern const ChunkHandlerTable
_game_chunk_handlers(game_chunk_handlers
);