/
load.c
530 lines (444 loc) · 12.4 KB
/
load.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
* See Licensing and Copyright notice in naev.h
*/
/**
* @file load.c
*
* @brief Contains stuff to load a pilot or look up information about it.
*/
#include "load.h"
#include "naev.h"
#include "nxml.h"
#include "log.h"
#include "player.h"
#include "nfile.h"
#include "array.h"
#include "space.h"
#include "toolkit.h"
#include "menu.h"
#include "dialogue.h"
#include "event.h"
#include "mission.h"
#include "faction.h"
#include "gui.h"
#include "unidiff.h"
#include "nlua_var.h"
#include "land.h"
#include "hook.h"
#define LOAD_WIDTH 600 /**< Load window width. */
#define LOAD_HEIGHT 500 /**< Load window height. */
#define BUTTON_WIDTH 80 /**< Button width. */
#define BUTTON_HEIGHT 30 /**< Button height. */
static nsave_t *load_saves = NULL; /**< Array of save.s */
extern int save_loaded; /**< From save.c */
/*
* Prototypes.
*/
/* externs */
/* player.c */
extern Planet* player_load( xmlNodePtr parent ); /**< Loads player related stuff. */
/* mission.c */
extern int missions_loadActive( xmlNodePtr parent ); /**< Loads active missions. */
/* event.c */
extern int events_loadActive( xmlNodePtr parent );
/* nlua_var.c */
extern int var_load( xmlNodePtr parent ); /**< Loads mission variables. */
/* faction.c */
extern int pfaction_load( xmlNodePtr parent ); /**< Loads faction data. */
/* hook.c */
extern int hook_load( xmlNodePtr parent ); /**< Loads hooks. */
/* space.c */
extern int space_sysLoad( xmlNodePtr parent ); /**< Loads the space stuff. */
/* unidiff.c */
extern int diff_load( xmlNodePtr parent ); /**< Loads the universe diffs. */
/* static */
static void load_menu_update( unsigned int wid, char *str );
static void load_menu_close( unsigned int wdw, char *str );
static void load_menu_load( unsigned int wdw, char *str );
static void load_menu_delete( unsigned int wdw, char *str );
static int load_load( nsave_t *save, const char *path );
/**
* @brief Loads an individual save.
*/
static int load_load( nsave_t *save, const char *path )
{
xmlDocPtr doc;
xmlNodePtr root, parent, node, cur;
int scu, stp, stu;
memset( save, 0, sizeof(nsave_t) );
/* Load the XML. */
doc = xmlParseFile(path);
if (doc == NULL) {
WARN("Unable to parse save path '%s'.", path);
return -1;
}
root = doc->xmlChildrenNode; /* base node */
if (root == NULL) {
WARN("Unable to get child node of save '%s'.",path);
xmlFreeDoc(doc);
return -1;
}
/* Save path. */
save->path = strdup(path);
/* Iterate inside the naev_save. */
parent = root->xmlChildrenNode;
do {
xml_onlyNodes(parent);
/* Info. */
if (xml_isNode(parent,"version")) {
node = parent->xmlChildrenNode;
do {
xmlr_strd(node,"naev",save->version);
xmlr_strd(node,"data",save->data);
} while (xml_nextNode(node));
continue;
}
if (xml_isNode(parent,"player")) {
/* Get name. */
xmlr_attr(parent,"name",save->name);
/* Parse rest. */
node = parent->xmlChildrenNode;
do {
xml_onlyNodes(node);
/* Player info. */
xmlr_strd(node,"location",save->planet);
xmlr_ulong(node,"credits",save->credits);
/* Time. */
if (xml_isNode(node,"time")) {
cur = node->xmlChildrenNode;
scu = stp = stu = 0;
do {
xmlr_int(cur,"SCU",scu);
xmlr_int(cur,"STP",stp);
xmlr_int(cur,"STU",stu);
} while (xml_nextNode(cur));
save->date = ntime_create( scu, stp, stu );
continue;
}
/* Ship info. */
if (xml_isNode(node,"ship")) {
xmlr_attr(node,"name",save->shipname);
xmlr_attr(node,"model",save->shipmodel);
continue;
}
} while (xml_nextNode(node));
continue;
}
} while (xml_nextNode(parent));
/* Clean up. */
xmlFreeDoc(doc);
return 0;
}
/**
* @brief Loads or refreshes saved games.
*/
int load_refresh (void)
{
char **files, buf[PATH_MAX];
int nfiles, i, len;
int ok;
nsave_t *ns;
if (load_saves != NULL)
load_free();
load_saves = array_create( nsave_t );
/* load the saves */
files = nfile_readDir( &nfiles, "%ssaves", nfile_basePath() );
for (i=0; i<nfiles; i++) {
len = strlen(files[i]);
/* no save extension */
if ((len < 5) || strcmp(&files[i][len-3],".ns")) {
free(files[i]);
memmove( &files[i], &files[i+1], sizeof(char*) * (nfiles-i-1) );
nfiles--;
i--;
}
}
/* Make sure files are none. */
if (files == NULL)
return 0;
/* Allocate and parse. */
ok = 0;
ns = NULL;
for (i=0; i<nfiles; i++) {
if (!ok)
ns = &array_grow( &load_saves );
snprintf( buf, sizeof(buf), "%ssaves/%s", nfile_basePath(), files[i] );
ok = load_load( ns, buf );
}
/* Clean up memory. */
for (i=0; i<nfiles; i++)
free(files[i]);
free(files);
return 0;
}
/**
* @brief Frees loaded save stuff.
*/
void load_free (void)
{
int i;
nsave_t *ns;
if (load_saves != NULL) {
for (i=0; i<array_size(load_saves); i++) {
ns = &load_saves[i];
free(ns->path);
if (ns->name != NULL)
free(ns->name);
if (ns->version != NULL)
free(ns->version);
if (ns->data != NULL)
free(ns->data);
if (ns->planet != NULL)
free(ns->planet);
if (ns->shipname != NULL)
free(ns->shipname);
if (ns->shipmodel != NULL)
free(ns->shipmodel);
}
array_free( load_saves );
}
load_saves = NULL;
}
/**
* @brief Gets the list of loaded saves.
*/
nsave_t *load_getList( int *n )
{
if (load_saves == NULL) {
*n = 0;
return NULL;
}
*n = array_size( load_saves );
return load_saves;
}
/**
* @brief Opens the load game menu.
*/
void load_loadGameMenu (void)
{
unsigned int wid;
char **names;
nsave_t *nslist, *ns;
int i, n;
/* window */
wid = window_create( "Load Game", -1, -1, LOAD_WIDTH, LOAD_HEIGHT );
window_setAccept( wid, load_menu_load );
window_setCancel( wid, load_menu_close );
/* Load loads. */
load_refresh();
/* load the saves */
nslist = load_getList( &n );
if (n > 0) {
names = malloc( sizeof(char*)*n );
for (i=0; i<n; i++) {
ns = &nslist[i];
names[i] = strdup( ns->name );
}
}
/* case there are no files */
else {
names = malloc(sizeof(char*));
names[0] = strdup("None");
n = 1;
}
/* Player text. */
window_addText( wid, -20, -40, 200, LOAD_HEIGHT-40-20-2*(BUTTON_HEIGHT+20),
0, "txtPilot", NULL, &cBlack, NULL );
window_addList( wid, 20, -50,
LOAD_WIDTH-200-60, LOAD_HEIGHT-110,
"lstSaves", names, n, 0, load_menu_update );
/* Buttons */
window_addButton( wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT,
"btnBack", "Back", load_menu_close );
window_addButton( wid, -20, 20 + BUTTON_HEIGHT+20, BUTTON_WIDTH, BUTTON_HEIGHT,
"btnLoad", "Load", load_menu_load );
window_addButton( wid, 20, 20, BUTTON_WIDTH, BUTTON_HEIGHT,
"btnDelete", "Del", load_menu_delete );
}
/**
* @brief Closes the load game menu.
* @param wdw Window triggering function.
* @param str Unused.
*/
static void load_menu_close( unsigned int wdw, char *str )
{
(void)str;
window_destroy( wdw );
}
/**
* @brief Updates the load menu.
* @param wdw Window triggering function.
* @param str Unused.
*/
static void load_menu_update( unsigned int wid, char *str )
{
(void) str;
int pos;
nsave_t *ns;
int n;
char *save;
char buf[256], credits[ECON_CRED_STRLEN], date[64];
/* Make sure list is ok. */
save = toolkit_getList( wid, "lstSaves" );
if (strcmp(save,"None") == 0)
return;
/* Get position. */
pos = toolkit_getListPos( wid, "lstSaves" );
ns = load_getList( &n );
ns = &ns[pos];
/* Display text. */
credits2str( credits, ns->credits, 2 );
ntime_prettyBuf( date, sizeof(date), ns->date, 2 );
snprintf( buf, sizeof(buf),
"\eDName:\n"
"\e0 %s\n"
"\eDVersion:\n"
"\e0 %s\n"
"\eDDate:\n"
"\e0 %s\n"
"\eDPlanet:\n"
"\e0 %s\n"
"\eDCredits:\n"
"\e0 %s\n"
"\eDShip Name:\n"
"\e0 %s\n"
"\eDShip Model:\n"
"\e0 %s",
ns->name, ns->version, date, ns->planet,
credits, ns->shipname, ns->shipmodel );
window_modifyText( wid, "txtPilot", buf );
}
/**
* @brief Loads a new game.
* @param wdw Window triggering function.
* @param str Unused.
*/
static void load_menu_load( unsigned int wdw, char *str )
{
(void)str;
char *save;
int wid, pos;
nsave_t *ns;
int n;
int version[3];
int diff;
wid = window_get( "Load Game" );
save = toolkit_getList( wid, "lstSaves" );
if (strcmp(save,"None") == 0)
return;
pos = toolkit_getListPos( wid, "lstSaves" );
ns = load_getList( &n );
/* Check version. */
if (ns->version != NULL) {
naev_versionParse( version, ns[pos].version, strlen(ns[pos].version) );
diff = naev_versionCompare( version );
if (ABS(diff) >= 2) {
if (!dialogue_YesNo( "Save game version mismatch",
"Save game '%s' version does not match Naev version:\n"
" Save version: \er%s\e0\n"
" Naev version: \eD%s\e0\n"
"Are you sure you want to load the game? It may have loss of data.",
save, ns[pos].version, naev_version(0) ))
return;
}
}
/* Close menus before loading for proper rendering. */
load_menu_close(wdw, NULL);
/* Close the main menu. */
menu_main_close();
/* Try to load the game. */
if (load_game( ns[pos].path )) {
/* Failed so reopen both. */
menu_main();
load_loadGameMenu();
}
}
/**
* @brief Deletes an old game.
* @param wdw Window to delete.
* @param str Unused.
*/
static void load_menu_delete( unsigned int wdw, char *str )
{
(void)str;
char *save;
int wid, pos;
nsave_t *ns;
int n;
wid = window_get( "Load Game" );
save = toolkit_getList( wid, "lstSaves" );
if (strcmp(save,"None") == 0)
return;
if (dialogue_YesNo( "Permanently Delete?",
"Are you sure you want to permanently delete '%s'?", save) == 0)
return;
/* Remove it. */
pos = toolkit_getListPos( wid, "lstSaves" );
ns = load_getList( &n );
remove( ns[pos].path ); /* remove is portable and will call unlink on linux. */
/* need to reload the menu */
load_menu_close(wdw, NULL);
load_loadGameMenu();
}
/**
* @brief Actually loads a new game based on file.
*
* @param file File that contains the new game.
* @return 0 on success.
*/
int load_game( const char* file )
{
xmlNodePtr node;
xmlDocPtr doc;
Planet *pnt;
/* Make sure it exists. */
if (!nfile_fileExists(file)) {
dialogue_alert("Savegame file seems to have been deleted.");
return -1;
}
/* Load the XML. */
doc = xmlParseFile(file);
if (doc == NULL)
goto err;
node = doc->xmlChildrenNode; /* base node */
if (node == NULL)
goto err_doc;
/* Clean up possible stuff that should be cleaned. */
player_cleanup();
/* Welcome message - must be before space_init. */
player_message( "\egWelcome to "APPNAME"!" );
player_message( "\eg v%s", naev_version(0) );
/* Now begin to load. */
diff_load(node); /* Must load first to work properly. */
pfaction_load(node); /* Must be loaded before player so the messages show up properly. */
pnt = player_load(node);
var_load(node);
missions_loadActive(node);
events_loadActive(node);
hook_load(node);
space_sysLoad(node);
/* Initialize the economy. */
economy_init();
/* Check sanity. */
event_checkSanity();
/* Run the load event trigger. */
events_trigger( EVENT_TRIGGER_LOAD );
/* Land the player. */
land( pnt, 1 );
/* Load the GUI. */
if (gui_load( gui_pick() )) {
if (player.p->ship->gui != NULL)
gui_load( player.p->ship->gui );
}
/* Sanitize the GUI. */
gui_setCargo();
xmlFreeDoc(doc);
/* Set loaded. */
save_loaded = 1;
return 0;
err_doc:
xmlFreeDoc(doc);
err:
WARN("Savegame '%s' invalid!", file);
return -1;
}