Freeciv-3.2
Loading...
Searching...
No Matches
savemain.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18/* utility */
19#include "log.h"
20#include "mem.h"
21#include "registry.h"
22
23/* common */
24#include "ai.h"
25#include "capability.h"
26#include "game.h"
27#include "research.h"
28
29/* server */
30#include "console.h"
31#include "notify.h"
32#include "plrhand.h"
33#include "ruleset.h"
34
35/* server/savegame */
36#include "savegame2.h"
37#include "savegame3.h"
38
39#include "savemain.h"
40
42
43/************************************************************************/
46void savegame_load(struct section_file *sfile)
47{
48 const char *savefile_options;
49
50 fc_assert_ret(sfile != NULL);
51
52#ifdef DEBUG_TIMERS
53 struct timer *loadtimer = timer_new(TIMER_CPU, TIMER_DEBUG, "load");
55#endif /* DEBUG_TIMERS */
56
57 savefile_options = secfile_lookup_str(sfile, "savefile.options");
58
59 if (!savefile_options) {
60 log_error("Missing savefile options. Can not load the savegame.");
62 return;
63 }
64
65 if (has_capabilities("+version3", savefile_options)) {
66 /* Load new format (freeciv 3.0.x and newer) */
67 log_verbose("loading savefile in 3.0+ format ...");
68 savegame3_load(sfile);
69 } else if (has_capabilities("+version2", savefile_options)) {
70 /* Load old format (freeciv 2.3 - 2.6) */
71 log_verbose("loading savefile in 2.3 - 2.6 format ...");
72 savegame2_load(sfile);
73 } else {
74 log_error("Too old savegame. Format not supported any more.");
76 return;
77 }
78
79 players_iterate(pplayer) {
80 unit_list_iterate(pplayer->units, punit) {
81 CALL_FUNC_EACH_AI(unit_created, punit);
82 CALL_PLR_AI_FUNC(unit_got, pplayer, punit);
84
85 city_list_iterate(pplayer->cities, pcity) {
86 CALL_FUNC_EACH_AI(city_created, pcity);
87 CALL_PLR_AI_FUNC(city_got, pplayer, pplayer, pcity);
90
91 /* Capital information was not saved in savegames,
92 * so we have no capital setup at all at the moment.
93 * Best we can do is to do recalculation now. It might be
94 * a bit off compared to the situation before the game was saved,
95 * but definitely better than not setting capitals at all. */
96 players_iterate_alive(pplayer) {
97 update_capital(pplayer);
99
100#ifdef DEBUG_TIMERS
102 log_debug("Loading secfile in %.3f seconds.", timer_read_seconds(loadtimer));
104#endif /* DEBUG_TIMERS */
105}
106
107/************************************************************************/
110void savegame_save(struct section_file *sfile, const char *save_reason,
111 bool scenario)
112{
113 savegame3_save(sfile, save_reason, scenario);
114}
115
123
124/************************************************************************/
128{
129 secfile_destroy(stdata->sfile);
130 free(stdata);
131}
132
133/************************************************************************/
136static void save_thread_run(void *arg)
137{
138 struct save_thread_data *stdata = (struct save_thread_data *)arg;
139
140 if (!secfile_save(stdata->sfile, stdata->filepath, stdata->save_compress_level,
141 stdata->save_compress_type)) {
142 con_write(C_FAIL, _("Failed saving game as %s"), stdata->filepath);
143 log_error("Game saving failed: %s", secfile_error());
144 notify_conn(NULL, NULL, E_LOG_ERROR, ftc_warning, _("Failed saving game."));
145 } else {
146 con_write(C_OK, _("Game saved as %s"), stdata->filepath);
147 }
148
150}
151
152/************************************************************************/
156void save_game(const char *orig_filename, const char *save_reason,
157 bool scenario)
158{
159 char *dot, *filename;
160 struct timer *timer_cpu, *timer_user;
161 struct save_thread_data *stdata;
162
163 stdata = fc_malloc(sizeof(*stdata));
164
165 stdata->save_compress_type = game.server.save_compress_type;
166 stdata->save_compress_level = game.server.save_compress_level;
167
168 if (!orig_filename) {
169 stdata->filepath[0] = '\0';
170 filename = stdata->filepath;
171 } else {
172 sz_strlcpy(stdata->filepath, orig_filename);
173 if ((filename = strrchr(stdata->filepath, '/'))) {
174 filename++;
175 } else {
176 filename = stdata->filepath;
177 }
178
179 /* Ignores the dot at the start of the filename. */
180 for (dot = filename; '.' == *dot; dot++) {
181 /* Nothing. */
182 }
183 if ('\0' == *dot) {
184 /* Only dots in this file name, consider it as empty. */
185 filename[0] = '\0';
186 } else {
187 char *end_dot;
188 char *strip_extensions[] = {
189 ".sav", ".gz", ".bz2", ".xz", ".zst", NULL };
190 bool stripped = TRUE;
191
192 while ((end_dot = strrchr(dot, '.')) && stripped) {
193 int i;
194
195 stripped = FALSE;
196
197 for (i = 0; strip_extensions[i] != NULL && !stripped; i++) {
199 *end_dot = '\0';
200 stripped = TRUE;
201 }
202 }
203 }
204 }
205 }
206
207 /* If orig_filename is NULL or empty, use a generated default name. */
208 if (filename[0] == '\0') {
209 /* manual save */
211 sizeof(stdata->filepath) + stdata->filepath - filename, "manual");
212 }
213
218
219 /* Allowing duplicates shouldn't be allowed. However, it takes very too
220 * long time for huge game saving... */
221 stdata->sfile = secfile_new(TRUE);
222 savegame_save(stdata->sfile, save_reason, scenario);
223
224 /* We have consistent game state in stdata->sfile now, so
225 * we could pass it to the saving thread already. We want to
226 * handle below notify_conn() and directory creation in
227 * main thread, though. */
228
229 /* Append ".sav" to filename. */
230 sz_strlcat(stdata->filepath, ".sav");
231
232 if (stdata->save_compress_level > 0) {
233 switch (stdata->save_compress_type) {
234#ifdef FREECIV_HAVE_LIBZ
235 case FZ_ZLIB:
236 /* Append ".gz" to filename. */
237 sz_strlcat(stdata->filepath, ".gz");
238 break;
239#endif
240#ifdef FREECIV_HAVE_LIBLZMA
241 case FZ_XZ:
242 /* Append ".xz" to filename. */
243 sz_strlcat(stdata->filepath, ".xz");
244 break;
245#endif
246#ifdef FREECIV_HAVE_LIBZSTD
247 case FZ_ZSTD:
248 /* Append ".zstd" to filename. */
249 sz_strlcat(stdata->filepath, ".zst");
250 break;
251#endif /* FREECIV_HAVE_LIBZSTD */
252 case FZ_PLAIN:
253 break;
254 default:
255 log_error(_("Unsupported compression type %d."),
256 stdata->save_compress_type);
258 _("Unsupported compression type %d."),
259 stdata->save_compress_type);
260 break;
261 }
262 }
263
264 if (!path_is_absolute(stdata->filepath)) {
265 char tmpname[600];
266
267 if (!scenario) {
268 /* Ensure the saves directory exists. */
269 if (srvarg.saves_pathname[0] != '\0'
271 log_error(_("Can't create saves directory %s!"),
273 /* Don't tell server paths to clients */
275 _("Can't create saves directory!"));
276
280
281 return;
282 }
283
285 } else {
286 /* Make sure scenario directory exist */
287 if (srvarg.scenarios_pathname[0] != '\0'
289 log_error(_("Can't create scenario saves directory %s!"),
291 /* Don't tell server paths to clients */
293 _("Can't create scenario saves directory!"));
294
298
299 return;
300 }
301
303 }
304
305 if (tmpname[0] != '\0') {
306 sz_strlcat(tmpname, "/");
307 }
308 sz_strlcat(tmpname, stdata->filepath);
309 sz_strlcpy(stdata->filepath, tmpname);
310 }
311
312 if (save_thread != NULL) {
313 /* Previously started thread */
316 /* Setting has changed since the last save */
319 }
320 } else if (game.server.threaded_save) {
322 }
323
324 if (save_thread != NULL) {
326 } else {
328 }
329
330#ifdef LOG_TIMERS
331 log_verbose("Save time: %g seconds (%g apparent)",
333#endif
334
337}
338
339/************************************************************************/
343{
344 if (save_thread != NULL) {
348 }
349}
350
351/************************************************************************/
355{
356 /* Try to get the server back to a vaguely sane state */
360
364}
#define CALL_FUNC_EACH_AI(_func,...)
Definition ai.h:387
#define CALL_PLR_AI_FUNC(_func, _player,...)
Definition ai.h:377
bool has_capabilities(const char *us, const char *them)
Definition capability.c:86
#define city_list_iterate(citylist, pcity)
Definition city.h:508
#define city_list_iterate_end
Definition city.h:510
char * incite_cost
Definition comments.c:75
void con_write(enum rfc_status rfc_status, const char *message,...)
Definition console.c:203
@ C_FAIL
Definition console.h:45
@ C_OK
Definition console.h:41
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:74
#define _(String)
Definition fcintl.h:67
int fc_thread_start(fc_thread *thread, void(*function)(void *arg), void *arg)
void fc_thread_wait(fc_thread *thread)
const struct ft_color ftc_warning
struct civ_game game
Definition game.c:62
int generate_save_name(const char *format, char *buf, int buflen, const char *reason)
Definition game.c:791
fz_method
Definition ioz.h:36
@ FZ_PLAIN
Definition ioz.h:37
#define fc_assert_ret(condition)
Definition log.h:191
#define log_verbose(message,...)
Definition log.h:109
#define log_debug(message,...)
Definition log.h:115
#define log_error(message,...)
Definition log.h:103
#define fc_malloc(sz)
Definition mem.h:34
void notify_conn(struct conn_list *dest, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:238
#define players_iterate_end
Definition player.h:537
#define players_iterate(_pplayer)
Definition player.h:532
#define players_iterate_alive_end
Definition player.h:547
#define players_iterate_alive(_pplayer)
Definition player.h:542
void update_capital(struct player *pplayer)
Definition plrhand.c:731
struct section_file * secfile_new(bool allow_duplicates)
const char * secfile_error(void)
void secfile_destroy(struct section_file *secfile)
const char * secfile_lookup_str(const struct section_file *secfile, const char *path,...)
bool secfile_save(const struct section_file *secfile, const char *filename, int compression_level, enum fz_method compression_method)
int recalculate_techs_researched(const struct research *presearch)
Definition research.c:1345
#define researches_iterate(_presearch)
Definition research.h:155
#define researches_iterate_end
Definition research.h:158
bool load_rulesets(const char *restore, const char *alt, bool compat_mode, rs_conversion_logger logger, bool act, bool buffer_script, bool load_luadata)
Definition ruleset.c:9367
void savegame2_load(struct section_file *file)
Definition savegame2.c:405
void savegame3_save(struct section_file *sfile, const char *save_reason, bool scenario)
Definition savegame3.c:432
void savegame3_load(struct section_file *file)
Definition savegame3.c:459
void savegame_load(struct section_file *sfile)
Definition savemain.c:46
void savegame_save(struct section_file *sfile, const char *save_reason, bool scenario)
Definition savemain.c:110
void save_system_close(void)
Definition savemain.c:342
static void save_thread_run(void *arg)
Definition savemain.c:136
static void save_thread_data_free(struct save_thread_data *stdata)
Definition savemain.c:127
void save_game(const char *orig_filename, const char *save_reason, bool scenario)
Definition savemain.c:156
void save_restore_sane_state(void)
Definition savemain.c:354
static fc_thread * save_thread
Definition savemain.c:41
bool make_dir(const char *pathname)
Definition shared.c:1782
bool path_is_absolute(const char *filename)
Definition shared.c:1883
void server_game_init(bool keep_ruleset_value)
Definition srv_main.c:3464
struct server_arguments srvarg
Definition srv_main.c:176
void server_game_free(void)
Definition srv_main.c:3488
bool threaded_save
Definition game.h:184
int save_compress_level
Definition game.h:185
char save_name[MAX_LEN_NAME]
Definition game.h:224
enum fz_method save_compress_type
Definition game.h:186
struct civ_game::@31::@35 server
int save_compress_level
Definition savemain.c:120
enum fz_method save_compress_type
Definition savemain.c:121
struct section_file * sfile
Definition savemain.c:118
char filepath[600]
Definition savemain.c:119
char * scenarios_pathname
Definition srv_main.h:47
char * saves_pathname
Definition srv_main.h:46
Definition timing.c:81
#define sz_strlcpy(dest, src)
Definition support.h:195
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
#define sz_strlcat(dest, src)
Definition support.h:196
void timer_destroy(struct timer *t)
Definition timing.c:208
void timer_start(struct timer *t)
Definition timing.c:264
void timer_stop(struct timer *t)
Definition timing.c:308
struct timer * timer_new(enum timer_timetype type, enum timer_use use, const char *name)
Definition timing.c:160
double timer_read_seconds(struct timer *t)
Definition timing.c:384
@ TIMER_ACTIVE
Definition timing.h:46
#define TIMER_DEBUG
Definition timing.h:61
@ TIMER_CPU
Definition timing.h:41
@ TIMER_USER
Definition timing.h:42
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_end
Definition unitlist.h:33