OpenTTD Source  20241121-master-g67a0fccfad
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 <http://www.gnu.org/licenses/>.
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 
25 static std::string _game_saveload_name;
26 static int _game_saveload_version;
27 static std::string _game_saveload_settings;
28 
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),
33 };
34 
35 static void SaveReal_GSDT(int)
36 {
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 
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 */
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)) {
71  if (SlIterateArray() != -1) SlErrorCorrupt("Too many GameScript configs");
72  return;
73  }
74 
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.");
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 
116 extern GameStrings *_current_data;
117 
118 static std::string _game_saveload_string;
119 static uint32_t _game_saveload_strings;
120 
121 class SlGameLanguageString : public DefaultSaveLoadHandler<SlGameLanguageString, LanguageStrings> {
122 public:
123  inline static const SaveLoad description[] = {
124  SLEG_SSTR("string", _game_saveload_string, SLE_STR | SLF_ALLOW_CONTROL),
125  };
126  inline const static 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 
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),
153 };
154 
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  delete _current_data;
163  _current_data = new GameStrings();
164 
165  while (SlIterateArray() != -1) {
166  LanguageStrings ls;
167  SlObject(&ls, slt);
168  _current_data->raw_strings.push_back(std::move(ls));
169  }
170 
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;
175  return;
176  }
177 
180  }
181 
182  void Save() const override
183  {
184  SlTableHeader(_game_language_desc);
185 
186  if (_current_data == nullptr) return;
187 
188  for (uint i = 0; i < _current_data->raw_strings.size(); i++) {
189  SlSetArrayIndex(i);
190  SlObject(&_current_data->raw_strings[i], _game_language_desc);
191  }
192  }
193 };
194 
195 static const GSTRChunkHandler GSTR;
196 static const GSDTChunkHandler GSDT;
197 static const ChunkHandlerRef game_chunk_handlers[] = {
198  GSTR,
199  GSDT,
200 };
201 
202 extern const ChunkHandlerTable _game_chunk_handlers(game_chunk_handlers);
Default handler for saving/loading an object to/from disk.
Definition: saveload.h:579
static GameConfig * GetConfig(ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition: game_config.cpp:18
static void Save()
Save data from a GameScript to a savegame.
Definition: game_core.cpp:206
SaveLoadTable GetLoadDescription() const
Get the description for how to load the chunk.
Definition: saveload.cpp:3293
@ SSS_FORCE_GAME
Get the Script config from the current game.
bool HasScript() const
Is this config attached to an Script? In other words, is there a Script that is assigned to this slot...
void Change(std::optional< const std::string > name, int version=-1, bool force_exact_match=false)
Set another Script to be loaded in this slot.
const std::string & GetName() const
Get the name of the Script.
int GetVersion() const
Get the version of the Script.
void StringToSettings(const std::string &value)
Convert a string which is stored in the config file or savegames to custom settings of this Script.
std::string SettingsToString() const
Convert the custom settings to a string that can be stored in the config file or savegames.
static ScriptData * Load(int version)
Load data from a savegame.
static void LoadEmpty()
Load and discard data from a savegame.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
GameStrings * _current_data
The currently loaded game strings.
Definition: game_text.cpp:311
Loading for game chunks before table headers were added.
const SaveLoadCompat _game_language_string_sl_compat[]
Original field order for SlGameLanguageString.
const SaveLoadCompat _game_language_sl_compat[]
Original field order for _game_language_desc.
const SaveLoadCompat _game_script_sl_compat[]
Original field order for _game_script_desc.
void ReconsiderGameScriptLanguage()
Reconsider the game script language, so we use the right one.
Definition: game_text.cpp:383
bool _networking
are we in networking mode?
Definition: network.cpp:65
bool _network_server
network-server is active
Definition: network.cpp:66
size_t SlGetStructListLength(size_t limit)
Get the length of this list; if it exceeds the limit, error out.
Definition: saveload.cpp:1689
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:658
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:351
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:1893
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1755
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1702
void SlAutolength(AutolengthProc *proc, int arg)
Do something of which I have no idea what it is :P.
Definition: saveload.cpp:1957
void SlSetStructListLength(size_t length)
Set the length of this list.
Definition: saveload.cpp:1673
Functions/types related to saving and loading games.
#define SLEG_STRUCTLIST(name, handler)
Storage of a list of structs in every savegame version.
Definition: saveload.h:1217
@ SLF_ALLOW_CONTROL
Allow control codes in the strings.
Definition: saveload.h:686
#define SLEG_CONDVAR(name, variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition: saveload.h:1086
#define SLEG_SSTR(name, variable, type)
Storage of a global std::string in every savegame version.
Definition: saveload.h:1187
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:507
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:510
#define SLEG_VAR(name, variable, type)
Storage of a global variable in every savegame version.
Definition: saveload.h:1163
std::span< const struct SaveLoadCompat > SaveLoadCompatTable
A table of SaveLoadCompat entries.
Definition: saveload.h:516
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1239
#define SLE_SSTR(base, variable, type)
Storage of a std::string in every savegame version.
Definition: saveload.h:1031
@ 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
First savegame version.
Definition: saveload.h:31
Handlers and description of chunk.
Definition: saveload.h:461
void Save() const override
Save the chunk.
Definition: game_sl.cpp:108
void Load() const override
Load the chunk.
Definition: game_sl.cpp:57
void Save() const override
Save the chunk.
Definition: game_sl.cpp:182
void Load() const override
Load the chunk.
Definition: game_sl.cpp:158
Container for all the game strings.
Definition: game_text.hpp:50
std::vector< LanguageStrings > raw_strings
The raw strings per language, first must be English/the master language!.
Definition: game_text.hpp:54
void Compile()
Compile the language.
Definition: game_text.cpp:284
Container for the raw (unencoded) language strings of a language.
Definition: game_text.hpp:37
StringList lines
The lines of the file to pass into the parser/encoder.
Definition: game_text.hpp:39
SaveLoad type struct.
Definition: saveload.h:713