OpenTTD Source  12.0-beta2
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 *index_ptr)
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 (_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.c_str(), _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.c_str(), -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  /* Start the GameScript directly if it was active in the savegame */
106  Game::StartNew();
107  Game::Load(_game_saveload_version);
108 
109  if (SlIterateArray() != -1) SlErrorCorrupt("Too many GameScript configs");
110  }
111 
112  void Save() const override
113  {
114  SlTableHeader(_game_script_desc);
115  SlSetArrayIndex(0);
116  SlAutolength((AutolengthProc *)SaveReal_GSDT, nullptr);
117  }
118 };
119 
120 extern GameStrings *_current_data;
121 
122 static std::string _game_saveload_string;
123 static uint32 _game_saveload_strings;
124 
125 class SlGameLanguageString : public DefaultSaveLoadHandler<SlGameLanguageString, LanguageStrings> {
126 public:
127  inline static const SaveLoad description[] = {
128  SLEG_SSTR("string", _game_saveload_string, SLE_STR | SLF_ALLOW_CONTROL),
129  };
130  inline const static SaveLoadCompatTable compat_description = _game_language_string_sl_compat;
131 
132  void Save(LanguageStrings *ls) const override
133  {
134  SlSetStructListLength(ls->lines.size());
135 
136  for (const auto &string : ls->lines) {
137  _game_saveload_string = string;
138  SlObject(nullptr, this->GetDescription());
139  }
140  }
141 
142  void Load(LanguageStrings *ls) const override
143  {
144  uint32 length = IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? _game_saveload_strings : (uint32)SlGetStructListLength(UINT32_MAX);
145 
146  for (uint32 i = 0; i < length; i++) {
147  SlObject(nullptr, this->GetLoadDescription());
148  ls->lines.emplace_back(_game_saveload_string);
149  }
150  }
151 };
152 
153 static const SaveLoad _game_language_desc[] = {
154  SLE_SSTR(LanguageStrings, language, SLE_STR),
155  SLEG_CONDVAR("count", _game_saveload_strings, SLE_UINT32, SL_MIN_VERSION, SLV_SAVELOAD_LIST_LENGTH),
157 };
158 
160  GSTRChunkHandler() : ChunkHandler('GSTR', CH_TABLE) {}
161 
162  void Load() const override
163  {
164  const std::vector<SaveLoad> slt = SlCompatTableHeader(_game_language_desc, _game_language_sl_compat);
165 
166  delete _current_data;
167  _current_data = new GameStrings();
168 
169  while (SlIterateArray() != -1) {
170  LanguageStrings ls;
171  SlObject(&ls, slt);
172  _current_data->raw_strings.push_back(std::move(ls));
173  }
174 
175  /* If there were no strings in the savegame, set GameStrings to nullptr */
176  if (_current_data->raw_strings.size() == 0) {
177  delete _current_data;
178  _current_data = nullptr;
179  return;
180  }
181 
184  }
185 
186  void Save() const override
187  {
188  SlTableHeader(_game_language_desc);
189 
190  if (_current_data == nullptr) return;
191 
192  for (uint i = 0; i < _current_data->raw_strings.size(); i++) {
193  SlSetArrayIndex(i);
194  SlObject(&_current_data->raw_strings[i], _game_language_desc);
195  }
196  }
197 };
198 
199 static const GSTRChunkHandler GSTR;
200 static const GSDTChunkHandler GSDT;
201 static const ChunkHandlerRef game_chunk_handlers[] = {
202  GSTR,
203  GSDT,
204 };
205 
206 extern const ChunkHandlerTable _game_chunk_handlers(game_chunk_handlers);
DefaultSaveLoadHandler
Default handler for saving/loading an object to/from disk.
Definition: saveload.h:515
ReconsiderGameScriptLanguage
void ReconsiderGameScriptLanguage()
Reconsider the game script language, so we use the right one.
Definition: game_text.cpp:345
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:35
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:443
_network_server
bool _network_server
network-server is active
Definition: network.cpp:57
Game::Load
static void Load(int version)
Load data for a GameScript from a savegame.
Definition: game_core.cpp:216
SLE_STR
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:798
SaveLoadHandler::GetLoadDescription
SaveLoadTable GetLoadDescription() const
Get the description for how to load the chunk.
Definition: saveload.cpp:3436
saveload.h
GSDTChunkHandler::Load
void Load() const override
Load the chunk.
Definition: game_sl.cpp:60
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:406
SLF_ALLOW_CONTROL
@ SLF_ALLOW_CONTROL
Allow control codes in the strings.
Definition: saveload.h:625
span
A trimmed down version of what std::span will be in C++20.
Definition: span_type.hpp:60
game_sl_compat.h
GameConfig
Definition: game_config.hpp:15
ScriptConfig::GetName
const char * GetName() const
Get the name of the Script.
Definition: script_config.cpp:172
SLEG_VAR
#define SLEG_VAR(name, variable, type)
Storage of a global variable in every savegame version.
Definition: saveload.h:937
ScriptConfig::IsRandom
bool IsRandom() const
Is the current Script a randomly chosen Script?
Definition: script_config.cpp:167
_current_data
GameStrings * _current_data
The currently loaded game strings.
Definition: game_text.cpp:301
_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:182
GSTRChunkHandler::Save
void Save() const override
Save the chunk.
Definition: game_sl.cpp:186
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1024
SLEG_SSTR
#define SLEG_SSTR(name, variable, type)
Storage of a global std::string in every savegame version.
Definition: saveload.h:969
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:56
GameStrings::raw_strings
std::vector< LanguageStrings > raw_strings
The raw strings per language, first must be English/the master language!.
Definition: game_text.hpp:37
ScriptInstance::LoadEmpty
static void LoadEmpty()
Load and discard data from a savegame.
Definition: script_instance.cpp:620
SlAutolength
void SlAutolength(AutolengthProc *proc, void *arg)
Do something of which I have no idea what it is :P.
Definition: saveload.cpp:2093
LanguageStrings
Container for the raw (unencoded) language strings of a language.
Definition: game_text.hpp:20
ScriptConfig::GetVersion
int GetVersion() const
Get the version of the Script.
Definition: script_config.cpp:177
SlGetStructListLength
size_t SlGetStructListLength(size_t limit)
Get the length of this list; if it exceeds the limit, error out.
Definition: saveload.cpp:1825
GSDTChunkHandler::Save
void Save() const override
Save the chunk.
Definition: game_sl.cpp:112
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:849
SlErrorCorrupt
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:364
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:208
DefaultSaveLoadHandler< SlGameLanguageString, LanguageStrings >::GetDescription
SaveLoadTable GetDescription() const override
Definition: saveload.h:517
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
SlGameLanguageString
Definition: game_sl.cpp:125
SLE_SSTR
#define SLE_SSTR(base, variable, type)
Storage of a std::string in every savegame version.
Definition: saveload.h:806
GSTRChunkHandler::Load
void Load() const override
Load the chunk.
Definition: game_sl.cpp:162
Game::Save
static void Save()
Save data from a GameScript to a savegame.
Definition: game_core.cpp:205
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
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:335
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:2029
ScriptConfig::Change
void Change(const char *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:19
LanguageStrings::lines
StringList lines
The lines of the file to pass into the parser/encoder.
Definition: game_text.hpp:22
GameStrings::Compile
void Compile()
Compile the language.
Definition: game_text.cpp:276
_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:1838
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1891
SlSetStructListLength
void SlSetStructListLength(size_t length)
Set the length of this list.
Definition: saveload.cpp:1809
SaveLoad
SaveLoad type struct.
Definition: saveload.h:653
ScriptConfig::SSS_FORCE_GAME
@ SSS_FORCE_GAME
Get the Script config from the current game.
Definition: script_config.hpp:105
GSTRChunkHandler
Definition: game_sl.cpp:159
Game::StartNew
static void StartNew()
Start up a new GameScript.
Definition: game_core.cpp:72
GameStrings
Container for all the game strings.
Definition: game_text.hpp:33
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:162
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:670
SLEG_STRUCTLIST
#define SLEG_STRUCTLIST(name, handler)
Storage of a list of structs in every savegame version.
Definition: saveload.h:999