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

OpenTTD Source 15.3

game_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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "../stdafx.h"
11#include "../debug.h"
12
13#include "saveload.h"
14#include "compat/game_sl_compat.h"
15
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"
22
23#include "../safeguards.h"
24
25static std::string _game_saveload_name;
26static int _game_saveload_version;
27static std::string _game_saveload_settings;
28
29static 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),
33};
34
35static void SaveReal_GSDT(int)
36{
37 GameConfig *config = GameConfig::GetConfig();
38
39 if (config->HasScript()) {
40 _game_saveload_name = config->GetName();
41 _game_saveload_version = config->GetVersion();
42 } else {
43 /* No GameScript is configured for this so store an empty string as name. */
44 _game_saveload_name.clear();
45 _game_saveload_version = -1;
46 }
47
48 _game_saveload_settings = config->SettingsToString();
49
50 SlObject(nullptr, _game_script_desc);
51 Game::Save();
52}
53
54 struct GSDTChunkHandler : ChunkHandler {
55 GSDTChunkHandler() : ChunkHandler('GSDT', CH_TABLE) {}
56
57 void Load() const override
58 {
59 const std::vector<SaveLoad> slt = SlCompatTableHeader(_game_script_desc, _game_script_sl_compat);
60
61 /* Free all current data */
62 GameConfig::GetConfig(GameConfig::SSS_FORCE_GAME)->Change(std::nullopt);
63
64 if (SlIterateArray() == -1) return;
65
66 _game_saveload_version = -1;
67 SlObject(nullptr, slt);
68
69 if (_game_mode == GM_MENU || (_networking && !_network_server)) {
70 GameInstance::LoadEmpty();
71 if (SlIterateArray() != -1) SlErrorCorrupt("Too many GameScript configs");
72 return;
73 }
74
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 != "%_dummy") {
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.");
86 } else {
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.");
89 }
90 } else {
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.");
93 }
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;
97 }
98 }
99
100 config->StringToSettings(_game_saveload_settings);
101
102 /* Load the GameScript saved data */
103 config->SetToLoadData(GameInstance::Load(_game_saveload_version));
104
105 if (SlIterateArray() != -1) SlErrorCorrupt("Too many GameScript configs");
106 }
107
108 void Save() const override
109 {
110 SlTableHeader(_game_script_desc);
111 SlSetArrayIndex(0);
112 SlAutolength(SaveReal_GSDT, 0);
113 }
114};
115
116extern std::shared_ptr<GameStrings> _current_gamestrings_data;
117
118static std::string _game_saveload_string;
119static uint32_t _game_saveload_strings;
120
121 class SlGameLanguageString : public DefaultSaveLoadHandler<SlGameLanguageString, LanguageStrings> {
122public:
123 static inline const SaveLoad description[] = {
124 SLEG_SSTR("string", _game_saveload_string, SLE_STR | SLF_ALLOW_CONTROL | SLF_REPLACE_TABCRLF),
125 };
126 static inline const SaveLoadCompatTable compat_description = _game_language_string_sl_compat;
127
128 void Save(LanguageStrings *ls) const override
129 {
130 SlSetStructListLength(ls->lines.size());
131
132 for (const auto &string : ls->lines) {
133 _game_saveload_string = string;
134 SlObject(nullptr, this->GetDescription());
135 }
136 }
137
138 void Load(LanguageStrings *ls) const override
139 {
140 uint32_t length = IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? _game_saveload_strings : (uint32_t)SlGetStructListLength(UINT32_MAX);
141
142 for (uint32_t i = 0; i < length; i++) {
143 SlObject(nullptr, this->GetLoadDescription());
144 ls->lines.emplace_back(_game_saveload_string);
145 }
146 }
147};
148
149static 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),
153};
154
155 struct GSTRChunkHandler : ChunkHandler {
156 GSTRChunkHandler() : ChunkHandler('GSTR', CH_TABLE) {}
157
158 void Load() const override
159 {
160 const std::vector<SaveLoad> slt = SlCompatTableHeader(_game_language_desc, _game_language_sl_compat);
161
162 _current_gamestrings_data = std::make_shared<GameStrings>();
163
164 while (SlIterateArray() != -1) {
165 LanguageStrings ls;
166 SlObject(&ls, slt);
167 _current_gamestrings_data->raw_strings.push_back(std::move(ls));
168 }
169
170 /* If there were no strings in the savegame, set GameStrings to nullptr */
171 if (_current_gamestrings_data->raw_strings.empty()) {
172 _current_gamestrings_data.reset();
173 return;
174 }
175
176 _current_gamestrings_data->Compile();
177 ReconsiderGameScriptLanguage();
178 }
179
180 void Save() const override
181 {
182 SlTableHeader(_game_language_desc);
183
184 if (_current_gamestrings_data == nullptr) return;
185
186 for (uint i = 0; i < _current_gamestrings_data->raw_strings.size(); i++) {
187 SlSetArrayIndex(i);
188 SlObject(&_current_gamestrings_data->raw_strings[i], _game_language_desc);
189 }
190 }
191};
192
193static const GSTRChunkHandler GSTR;
194static const GSDTChunkHandler GSDT;
195static const ChunkHandlerRef game_chunk_handlers[] = {
196 GSTR,
197 GSDT,
198};
199
200extern const ChunkHandlerTable _game_chunk_handlers(game_chunk_handlers);
DefaultSaveLoadHandler
Default handler for saving/loading an object to/from disk.
Definition saveload.h:596
DefaultSaveLoadHandler< SlGameLanguageString, LanguageStrings >::GetDescription
SaveLoadTable GetDescription() const override
Definition saveload.h:598
GameConfig
Definition game_config.hpp:15
GameConfig::GetConfig
static GameConfig * GetConfig(ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition game_config.cpp:18
Game::Save
static void Save()
Save data from a GameScript to a savegame.
Definition game_core.cpp:192
SaveLoadHandler::GetLoadDescription
SaveLoadTable GetLoadDescription() const
Get the description for how to load the chunk.
Definition saveload.cpp:3444
ScriptConfig::SSS_FORCE_GAME
@ SSS_FORCE_GAME
Get the Script config from the current game.
Definition script_config.hpp:100
ScriptConfig::HasScript
bool HasScript() const
Is this config attached to an Script? In other words, is there a Script that is assigned to this slot...
Definition script_config.cpp:127
ScriptConfig::Change
void Change(std::optional< std::string_view > name, int version=-1, bool force_exact_match=false)
Set another Script to be loaded in this slot.
Definition script_config.cpp:22
ScriptConfig::StringToSettings
void StringToSettings(std::string_view value)
Convert a string which is stored in the config file or savegames to custom settings of this Script.
Definition script_config.cpp:142
ScriptConfig::GetName
const std::string & GetName() const
Get the name of the Script.
Definition script_config.cpp:132
ScriptConfig::GetVersion
int GetVersion() const
Get the version of the Script.
Definition script_config.cpp:137
ScriptConfig::SettingsToString
std::string SettingsToString() const
Convert the custom settings to a string that can be stored in the config file or savegames.
Definition script_config.cpp:164
ScriptInstance::Load
static ScriptData * Load(int version)
Load data from a savegame.
Definition script_instance.cpp:732
ScriptInstance::LoadEmpty
static void LoadEmpty()
Load and discard data from a savegame.
Definition script_instance.cpp:723
SlGameLanguageString
Definition game_sl.cpp:121
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
_current_gamestrings_data
std::shared_ptr< GameStrings > _current_gamestrings_data
The currently loaded game strings.
Definition game_text.cpp:308
game_sl_compat.h
Loading for game chunks before table headers were added.
_game_language_string_sl_compat
const SaveLoadCompat _game_language_string_sl_compat[]
Original field order for SlGameLanguageString.
Definition game_sl_compat.h:24
_game_language_sl_compat
const SaveLoadCompat _game_language_sl_compat[]
Original field order for _game_language_desc.
Definition game_sl_compat.h:29
_game_script_sl_compat
const SaveLoadCompat _game_script_sl_compat[]
Original field order for _game_script_desc.
Definition game_sl_compat.h:16
ReconsiderGameScriptLanguage
void ReconsiderGameScriptLanguage()
Reconsider the game script language, so we use the right one.
Definition game_text.cpp:381
_networking
bool _networking
are we in networking mode?
Definition network.cpp:67
_network_server
bool _network_server
network-server is active
Definition network.cpp:68
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition saveload.cpp:2037
SlGetStructListLength
size_t SlGetStructListLength(size_t limit)
Get the length of this list; if it exceeds the limit, error out.
Definition saveload.cpp:1833
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition saveload.cpp:677
SlErrorCorrupt
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition saveload.cpp:369
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition saveload.cpp:1846
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition saveload.cpp:1899
SlAutolength
void SlAutolength(AutolengthProc *proc, int arg)
Do something of which I have no idea what it is :P.
Definition saveload.cpp:2101
SlSetStructListLength
void SlSetStructListLength(size_t length)
Set the length of this list.
Definition saveload.cpp:1817
saveload.h
Functions/types related to saving and loading games.
SLEG_STRUCTLIST
#define SLEG_STRUCTLIST(name, handler)
Storage of a list of structs in every savegame version.
Definition saveload.h:1256
SLEG_CONDVAR
#define SLEG_CONDVAR(name, variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition saveload.h:1124
SLF_REPLACE_TABCRLF
@ SLF_REPLACE_TABCRLF
Replace tabs, cr and lf in the string with spaces.
Definition saveload.h:704
SLF_ALLOW_CONTROL
@ SLF_ALLOW_CONTROL
Allow control codes in the strings.
Definition saveload.h:702
SLEG_SSTR
#define SLEG_SSTR(name, variable, type)
Storage of a global std::string in every savegame version.
Definition saveload.h:1226
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:524
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:527
SLEG_VAR
#define SLEG_VAR(name, variable, type)
Storage of a global variable in every savegame version.
Definition saveload.h:1201
SaveLoadCompatTable
std::span< const struct SaveLoadCompat > SaveLoadCompatTable
A table of SaveLoadCompat entries.
Definition saveload.h:533
IsSavegameVersionBefore
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1278
SLE_SSTR
#define SLE_SSTR(base, variable, type)
Storage of a std::string in every savegame version.
Definition saveload.h:1061
SLV_SAVELOAD_LIST_LENGTH
@ SLV_SAVELOAD_LIST_LENGTH
293 PR#9374 Consistency in list length with SL_STRUCT / SL_STRUCTLIST / SL_DEQUE / SL_REFLIST.
Definition saveload.h:331
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition saveload.h:31
ChunkHandler
Handlers and description of chunk.
Definition saveload.h:478
GSDTChunkHandler
Definition game_sl.cpp:54
GSDTChunkHandler::Save
void Save() const override
Save the chunk.
Definition game_sl.cpp:108
GSDTChunkHandler::Load
void Load() const override
Load the chunk.
Definition game_sl.cpp:57
GSTRChunkHandler
Definition game_sl.cpp:155
GSTRChunkHandler::Save
void Save() const override
Save the chunk.
Definition game_sl.cpp:180
GSTRChunkHandler::Load
void Load() const override
Load the chunk.
Definition game_sl.cpp:158
LanguageStrings
Container for the raw (unencoded) language strings of a language.
Definition game_text.hpp:40
LanguageStrings::lines
TypedIndexContainer< StringList, StringIndexInTab > lines
The lines of the file to pass into the parser/encoder.
Definition game_text.hpp:42
SaveLoad
SaveLoad type struct.
Definition saveload.h:732