OpenTTD Source  14.0-beta1
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 static bool _game_saveload_is_random;
29 
30 static const SaveLoad _game_script_desc[] = {
31  SLEG_SSTR("name", _game_saveload_name, SLE_STR),
32  SLEG_SSTR("settings", _game_saveload_settings, SLE_STR),
33  SLEG_VAR("version", _game_saveload_version, SLE_UINT32),
34  SLEG_VAR("is_random", _game_saveload_is_random, SLE_BOOL),
35 };
36 
37 static void SaveReal_GSDT(int *)
38 {
40 
41  if (config->HasScript()) {
42  _game_saveload_name = config->GetName();
43  _game_saveload_version = config->GetVersion();
44  } else {
45  /* No GameScript is configured for this so store an empty string as name. */
46  _game_saveload_name.clear();
47  _game_saveload_version = -1;
48  }
49 
50  _game_saveload_is_random = config->IsRandom();
51  _game_saveload_settings = config->SettingsToString();
52 
53  SlObject(nullptr, _game_script_desc);
54  Game::Save();
55 }
56 
58  GSDTChunkHandler() : ChunkHandler('GSDT', CH_TABLE) {}
59 
60  void Load() const override
61  {
62  const std::vector<SaveLoad> slt = SlCompatTableHeader(_game_script_desc, _game_script_sl_compat);
63 
64  /* Free all current data */
66 
67  if (SlIterateArray() == -1) return;
68 
69  _game_saveload_version = -1;
70  SlObject(nullptr, slt);
71 
72  if (_game_mode == GM_MENU || (_networking && !_network_server)) {
74  if (SlIterateArray() != -1) SlErrorCorrupt("Too many GameScript configs");
75  return;
76  }
77 
79  if (!_game_saveload_name.empty()) {
80  config->Change(_game_saveload_name, _game_saveload_version, false, _game_saveload_is_random);
81  if (!config->HasScript()) {
82  /* No version of the GameScript available that can load the data. Try to load the
83  * latest version of the GameScript instead. */
84  config->Change(_game_saveload_name, -1, false, _game_saveload_is_random);
85  if (!config->HasScript()) {
86  if (_game_saveload_name.compare("%_dummy") != 0) {
87  Debug(script, 0, "The savegame has an GameScript by the name '{}', version {} which is no longer available.", _game_saveload_name, _game_saveload_version);
88  Debug(script, 0, "This game will continue to run without GameScript.");
89  } else {
90  Debug(script, 0, "The savegame had no GameScript available at the time of saving.");
91  Debug(script, 0, "This game will continue to run without GameScript.");
92  }
93  } else {
94  Debug(script, 0, "The savegame has an GameScript by the name '{}', version {} which is no longer available.", _game_saveload_name, _game_saveload_version);
95  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.");
96  }
97  /* Make sure the GameScript doesn't get the saveload data, as it was not the
98  * writer of the saveload data in the first place */
99  _game_saveload_version = -1;
100  }
101  }
102 
103  config->StringToSettings(_game_saveload_settings);
104 
105  /* Load the GameScript saved data */
106  config->SetToLoadData(GameInstance::Load(_game_saveload_version));
107 
108  if (SlIterateArray() != -1) SlErrorCorrupt("Too many GameScript configs");
109  }
110 
111  void Save() const override
112  {
113  SlTableHeader(_game_script_desc);
114  SlSetArrayIndex(0);
115  SlAutolength((AutolengthProc *)SaveReal_GSDT, nullptr);
116  }
117 };
118 
119 extern GameStrings *_current_data;
120 
121 static std::string _game_saveload_string;
122 static uint32_t _game_saveload_strings;
123 
124 class SlGameLanguageString : public DefaultSaveLoadHandler<SlGameLanguageString, LanguageStrings> {
125 public:
126  inline static const SaveLoad description[] = {
127  SLEG_SSTR("string", _game_saveload_string, SLE_STR | SLF_ALLOW_CONTROL),
128  };
129  inline const static SaveLoadCompatTable compat_description = _game_language_string_sl_compat;
130 
131  void Save(LanguageStrings *ls) const override
132  {
133  SlSetStructListLength(ls->lines.size());
134 
135  for (const auto &string : ls->lines) {
136  _game_saveload_string = string;
137  SlObject(nullptr, this->GetDescription());
138  }
139  }
140 
141  void Load(LanguageStrings *ls) const override
142  {
143  uint32_t length = IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? _game_saveload_strings : (uint32_t)SlGetStructListLength(UINT32_MAX);
144 
145  for (uint32_t i = 0; i < length; i++) {
146  SlObject(nullptr, this->GetLoadDescription());
147  ls->lines.emplace_back(_game_saveload_string);
148  }
149  }
150 };
151 
152 static const SaveLoad _game_language_desc[] = {
153  SLE_SSTR(LanguageStrings, language, SLE_STR),
154  SLEG_CONDVAR("count", _game_saveload_strings, SLE_UINT32, SL_MIN_VERSION, SLV_SAVELOAD_LIST_LENGTH),
156 };
157 
159  GSTRChunkHandler() : ChunkHandler('GSTR', CH_TABLE) {}
160 
161  void Load() const override
162  {
163  const std::vector<SaveLoad> slt = SlCompatTableHeader(_game_language_desc, _game_language_sl_compat);
164 
165  delete _current_data;
166  _current_data = new GameStrings();
167 
168  while (SlIterateArray() != -1) {
169  LanguageStrings ls;
170  SlObject(&ls, slt);
171  _current_data->raw_strings.push_back(std::move(ls));
172  }
173 
174  /* If there were no strings in the savegame, set GameStrings to nullptr */
175  if (_current_data->raw_strings.empty()) {
176  delete _current_data;
177  _current_data = nullptr;
178  return;
179  }
180 
183  }
184 
185  void Save() const override
186  {
187  SlTableHeader(_game_language_desc);
188 
189  if (_current_data == nullptr) return;
190 
191  for (uint i = 0; i < _current_data->raw_strings.size(); i++) {
192  SlSetArrayIndex(i);
193  SlObject(&_current_data->raw_strings[i], _game_language_desc);
194  }
195  }
196 };
197 
198 static const GSTRChunkHandler GSTR;
199 static const GSDTChunkHandler GSDT;
200 static const ChunkHandlerRef game_chunk_handlers[] = {
201  GSTR,
202  GSDT,
203 };
204 
205 extern const ChunkHandlerTable _game_chunk_handlers(game_chunk_handlers);
DefaultSaveLoadHandler
Default handler for saving/loading an object to/from disk.
Definition: saveload.h:560
ReconsiderGameScriptLanguage
void ReconsiderGameScriptLanguage()
Reconsider the game script language, so we use the right one.
Definition: game_text.cpp:385
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:488
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:31
_network_server
bool _network_server
network-server is active
Definition: network.cpp:60
SlErrorCorrupt
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:362
SaveLoadHandler::GetLoadDescription
SaveLoadTable GetLoadDescription() const
Get the description for how to load the chunk.
Definition: saveload.cpp:3246
saveload.h
SaveLoadCompatTable
std::span< const struct SaveLoadCompat > SaveLoadCompatTable
A table of SaveLoadCompat entries.
Definition: saveload.h:497
GSDTChunkHandler::Load
void Load() const override
Load the chunk.
Definition: game_sl.cpp:60
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:442
Debug
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
SLF_ALLOW_CONTROL
@ SLF_ALLOW_CONTROL
Allow control codes in the strings.
Definition: saveload.h:667
game_sl_compat.h
IsSavegameVersionBefore
bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1203
GameConfig
Definition: game_config.hpp:15
ScriptInstance::Load
static ScriptData * Load(int version)
Load data from a savegame.
Definition: script_instance.cpp:673
SLEG_VAR
#define SLEG_VAR(name, variable, type)
Storage of a global variable in every savegame version.
Definition: saveload.h:1124
ScriptConfig::IsRandom
bool IsRandom() const
Is the current Script a randomly chosen Script?
Definition: script_config.cpp:142
_current_data
GameStrings * _current_data
The currently loaded game strings.
Definition: game_text.cpp:313
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
_game_language_string_sl_compat
const SaveLoadCompat _game_language_string_sl_compat[]
Original field order for SlGameLanguageString.
Definition: game_sl_compat.h:24
ScriptConfig::StringToSettings
void StringToSettings(const std::string &value)
Convert a string which is stored in the config file or savegames to custom settings of this Script.
Definition: script_config.cpp:157
GSTRChunkHandler::Save
void Save() const override
Save the chunk.
Definition: game_sl.cpp:185
SLEG_SSTR
#define SLEG_SSTR(name, variable, type)
Storage of a global std::string in every savegame version.
Definition: saveload.h:1148
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:59
GameStrings::raw_strings
std::vector< LanguageStrings > raw_strings
The raw strings per language, first must be English/the master language!.
Definition: game_text.hpp:54
ScriptInstance::LoadEmpty
static void LoadEmpty()
Load and discard data from a savegame.
Definition: script_instance.cpp:664
SlAutolength
void SlAutolength(AutolengthProc *proc, void *arg)
Do something of which I have no idea what it is :P.
Definition: saveload.cpp:1907
LanguageStrings
Container for the raw (unencoded) language strings of a language.
Definition: game_text.hpp:37
ScriptConfig::GetVersion
int GetVersion() const
Get the version of the Script.
Definition: script_config.cpp:152
SlGetStructListLength
size_t SlGetStructListLength(size_t limit)
Get the length of this list; if it exceeds the limit, error out.
Definition: saveload.cpp:1639
ScriptConfig::Change
void Change(std::optional< const std::string > name, int version=-1, bool force_exact_match=false, bool is_random=false)
Set another Script to be loaded in this slot.
Definition: script_config.cpp:21
GSDTChunkHandler::Save
void Save() const override
Save the chunk.
Definition: game_sl.cpp:111
GSDTChunkHandler
Definition: game_sl.cpp:57
SLEG_CONDVAR
#define SLEG_CONDVAR(name, variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition: saveload.h:1047
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:179
DefaultSaveLoadHandler< SlGameLanguageString, LanguageStrings >::GetDescription
SaveLoadTable GetDescription() const override
Definition: saveload.h:562
GameConfig::GetConfig
static GameConfig * GetConfig(ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition: game_config.cpp:18
_game_script_sl_compat
const SaveLoadCompat _game_script_sl_compat[]
Original field order for _game_script_desc.
Definition: game_sl_compat.h:16
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:491
SlGameLanguageString
Definition: game_sl.cpp:124
SLE_SSTR
#define SLE_SSTR(base, variable, type)
Storage of a std::string in every savegame version.
Definition: saveload.h:992
GSTRChunkHandler::Load
void Load() const override
Load the chunk.
Definition: game_sl.cpp:161
Game::Save
static void Save()
Save data from a GameScript to a savegame.
Definition: game_core.cpp:212
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:1843
LanguageStrings::lines
StringList lines
The lines of the file to pass into the parser/encoder.
Definition: game_text.hpp:39
GameStrings::Compile
void Compile()
Compile the language.
Definition: game_text.cpp:286
_game_language_sl_compat
const SaveLoadCompat _game_language_sl_compat[]
Original field order for _game_language_desc.
Definition: game_sl_compat.h:29
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1652
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1705
SlSetStructListLength
void SlSetStructListLength(size_t length)
Set the length of this list.
Definition: saveload.cpp:1623
SaveLoad
SaveLoad type struct.
Definition: saveload.h:694
ScriptConfig::SSS_FORCE_GAME
@ SSS_FORCE_GAME
Get the Script config from the current game.
Definition: script_config.hpp:99
GSTRChunkHandler
Definition: game_sl.cpp:158
GameStrings
Container for all the game strings.
Definition: game_text.hpp:50
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:137
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:647
ScriptConfig::GetName
const std::string & GetName() const
Get the name of the Script.
Definition: script_config.cpp:147
SLEG_STRUCTLIST
#define SLEG_STRUCTLIST(name, handler)
Storage of a list of structs in every savegame version.
Definition: saveload.h:1178