OpenTTD
game_sl.cpp
Go to the documentation of this file.
1 /* $Id: game_sl.cpp 27003 2014-10-12 18:41:53Z rubidium $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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  */
9 
12 #include "../stdafx.h"
13 #include "../debug.h"
14 #include "saveload.h"
15 #include "../string_func.h"
16 
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 char _game_saveload_name[64];
26 static int _game_saveload_version;
27 static char _game_saveload_settings[1024];
28 static bool _game_saveload_is_random;
29 
30 static const SaveLoad _game_script[] = {
31  SLEG_STR(_game_saveload_name, SLE_STRB),
32  SLEG_STR(_game_saveload_settings, SLE_STRB),
33  SLEG_VAR(_game_saveload_version, SLE_UINT32),
34  SLEG_VAR(_game_saveload_is_random, SLE_BOOL),
35  SLE_END()
36 };
37 
38 static void SaveReal_GSDT(int *index_ptr)
39 {
41 
42  if (config->HasScript()) {
43  strecpy(_game_saveload_name, config->GetName(), lastof(_game_saveload_name));
44  _game_saveload_version = config->GetVersion();
45  } else {
46  /* No GameScript is configured for this so store an empty string as name. */
47  _game_saveload_name[0] = '\0';
48  _game_saveload_version = -1;
49  }
50 
51  _game_saveload_is_random = config->IsRandom();
52  _game_saveload_settings[0] = '\0';
53  config->SettingsToString(_game_saveload_settings, lastof(_game_saveload_settings));
54 
55  SlObject(NULL, _game_script);
56  Game::Save();
57 }
58 
59 static void Load_GSDT()
60 {
61  /* Free all current data */
63 
64  if ((CompanyID)SlIterateArray() == (CompanyID)-1) return;
65 
66  _game_saveload_version = -1;
67  SlObject(NULL, _game_script);
68 
69  if (_networking && !_network_server) {
71  if ((CompanyID)SlIterateArray() != (CompanyID)-1) SlErrorCorrupt("Too many GameScript configs");
72  return;
73  }
74 
76  if (StrEmpty(_game_saveload_name)) {
77  } else {
78  config->Change(_game_saveload_name, _game_saveload_version, false, _game_saveload_is_random);
79  if (!config->HasScript()) {
80  /* No version of the GameScript available that can load the data. Try to load the
81  * latest version of the GameScript instead. */
82  config->Change(_game_saveload_name, -1, false, _game_saveload_is_random);
83  if (!config->HasScript()) {
84  if (strcmp(_game_saveload_name, "%_dummy") != 0) {
85  DEBUG(script, 0, "The savegame has an GameScript by the name '%s', version %d which is no longer available.", _game_saveload_name, _game_saveload_version);
86  DEBUG(script, 0, "This game will continue to run without GameScript.");
87  } else {
88  DEBUG(script, 0, "The savegame had no GameScript available at the time of saving.");
89  DEBUG(script, 0, "This game will continue to run without GameScript.");
90  }
91  } else {
92  DEBUG(script, 0, "The savegame has an GameScript by the name '%s', version %d which is no longer available.", _game_saveload_name, _game_saveload_version);
93  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  }
95  /* Make sure the GameScript doesn't get the saveload data, as he was not the
96  * writer of the saveload data in the first place */
97  _game_saveload_version = -1;
98  }
99  }
100 
101  config->StringToSettings(_game_saveload_settings);
102 
103  /* Start the GameScript directly if it was active in the savegame */
104  Game::StartNew();
105  Game::Load(_game_saveload_version);
106 
107  if ((CompanyID)SlIterateArray() != (CompanyID)-1) SlErrorCorrupt("Too many GameScript configs");
108 }
109 
110 static void Save_GSDT()
111 {
112  SlSetArrayIndex(0);
113  SlAutolength((AutolengthProc *)SaveReal_GSDT, NULL);
114 }
115 
116 extern GameStrings *_current_data;
117 
118 static const char *_game_saveload_string;
119 static uint _game_saveload_strings;
120 
121 static const SaveLoad _game_language_header[] = {
122  SLEG_STR(_game_saveload_string, SLE_STR),
123  SLEG_VAR(_game_saveload_strings, SLE_UINT32),
124  SLE_END()
125 };
126 
127 static const SaveLoad _game_language_string[] = {
128  SLEG_STR(_game_saveload_string, SLE_STR | SLF_ALLOW_CONTROL),
129  SLE_END()
130 };
131 
132 static void SaveReal_GSTR(LanguageStrings *ls)
133 {
134  _game_saveload_string = ls->language;
135  _game_saveload_strings = ls->lines.Length();
136 
137  SlObject(NULL, _game_language_header);
138  for (uint i = 0; i < _game_saveload_strings; i++) {
139  _game_saveload_string = ls->lines[i];
140  SlObject(NULL, _game_language_string);
141  }
142 }
143 
144 static void Load_GSTR()
145 {
146  delete _current_data;
147  _current_data = new GameStrings();
148 
149  while (SlIterateArray() != -1) {
150  _game_saveload_string = NULL;
151  SlObject(NULL, _game_language_header);
152 
153  LanguageStrings *ls = new LanguageStrings(_game_saveload_string != NULL ? _game_saveload_string : "");
154  for (uint i = 0; i < _game_saveload_strings; i++) {
155  SlObject(NULL, _game_language_string);
156  *ls->lines.Append() = stredup(_game_saveload_string != NULL ? _game_saveload_string : "");
157  }
158 
159  *_current_data->raw_strings.Append() = ls;
160  }
161 
162  /* If there were no strings in the savegame, set GameStrings to NULL */
163  if (_current_data->raw_strings.Length() == 0) {
164  delete _current_data;
165  _current_data = NULL;
166  return;
167  }
168 
169  _current_data->Compile();
171 }
172 
173 static void Save_GSTR()
174 {
175  if (_current_data == NULL) return;
176 
177  for (uint i = 0; i < _current_data->raw_strings.Length(); i++) {
178  SlSetArrayIndex(i);
179  SlAutolength((AutolengthProc *)SaveReal_GSTR, _current_data->raw_strings[i]);
180  }
181 }
182 
183 extern const ChunkHandler _game_chunk_handlers[] = {
184  { 'GSTR', Save_GSTR, Load_GSTR, NULL, NULL, CH_ARRAY },
185  { 'GSDT', Save_GSDT, Load_GSDT, NULL, NULL, CH_ARRAY | CH_LAST},
186 };
bool _networking
are we in networking mode?
Definition: network.cpp:56
#define SLEG_STR(variable, type)
Storage of a global string in every savegame version.
Definition: saveload.h:439
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:561
Container for all the game strings.
Definition: game_text.hpp:31
AutoDeleteSmallVector< LanguageStrings *, 4 > raw_strings
The raw strings per language, first must be English/the master language!.
Definition: game_text.hpp:35
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.
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
static GameConfig * GetConfig(ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition: game_config.cpp:20
#define SLEG_VAR(variable, type)
Storage of a global variable in every savegame version.
Definition: saveload.h:418
const char * GetName() const
Get the name of the Script.
T * Append(uint to_add=1)
Append an item and return it.
static void Save()
Save data from a GameScript to a savegame.
Definition: game_core.cpp:198
bool IsRandom() const
Is the current Script a randomly chosen Script?
Functions/types related to saving and loading games.
uint Length() const
Get the number of items in the list.
allow control codes in the strings
Definition: saveload.h:184
bool HasScript() const
Is this config attached to an Script? In other words, is there a Script that is assigned to this slot...
void ReconsiderGameScriptLanguage()
Reconsider the game script language, so we use the right one.
Definition: game_text.cpp:377
void Compile()
Compile the language.
Definition: game_text.cpp:309
Container for the raw (unencoded) language strings of a language.
Definition: game_text.hpp:22
StringList lines
The lines of the file to pass into the parser/encoder.
Definition: game_text.hpp:24
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:126
static void StartNew()
Start up a new GameScript.
Definition: game_core.cpp:65
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:828
Handlers and description of chunk.
Definition: saveload.h:66
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:39
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:353
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:68
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
bool _network_server
network-server is active
Definition: network.cpp:57
void SlAutolength(AutolengthProc *proc, void *arg)
Do something of which I have no idea what it is :P.
Definition: saveload.cpp:1640
static void Load(int version)
Load data for a GameScript from a savegame.
Definition: game_core.cpp:209
SaveLoad type struct.
Definition: saveload.h:208
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:322
const char * language
Name of the language (base filename).
Definition: game_text.hpp:23
Owner
Enum for all companies/owners.
Definition: company_type.h:20
static void LoadEmpty()
Load and discard data from a savegame.
void StringToSettings(const char *value)
Convert a string which is stored in the config file or savegames to custom settings of this Script...
void SettingsToString(char *string, const char *last) const
Convert the custom settings to a string that can be stored in the config file or savegames.
Get the Script config from the current game.
Last chunk in this array.
Definition: saveload.h:104
GameStrings * _current_data
The currently loaded game strings.
Definition: game_text.cpp:334
int GetVersion() const
Get the version of the Script.