1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * (c) 1995-98 Artsoft Entertainment *
8 * phone: ++49 +521 290471 *
9 * email: aeglos@valinor.owl.de *
10 *----------------------------------------------------------*
12 ***********************************************************/
25 #define MAX_FILENAME_LEN 256 /* maximal filename length */
26 #define MAX_LINE_LEN 1000 /* maximal input line length */
27 #define CHUNK_ID_LEN 4 /* IFF style chunk id length */
28 #define LEVEL_HEADER_SIZE 80 /* size of level file header */
29 #define LEVEL_HEADER_UNUSED 18 /* unused level header bytes */
30 #define TAPE_HEADER_SIZE 20 /* size of tape file header */
31 #define TAPE_HEADER_UNUSED 7 /* unused tape header bytes */
32 #define FILE_VERSION_1_0 10 /* old 1.0 file version */
33 #define FILE_VERSION_1_2 12 /* actual file version */
35 /* file identifier strings */
36 #define LEVEL_COOKIE "ROCKSNDIAMONDS_LEVEL_FILE_VERSION_1.2"
37 #define SCORE_COOKIE "ROCKSNDIAMONDS_SCORE_FILE_VERSION_1.2"
38 #define TAPE_COOKIE "ROCKSNDIAMONDS_TAPE_FILE_VERSION_1.2"
39 #define SETUP_COOKIE "ROCKSNDIAMONDS_SETUP_FILE_VERSION_1.2"
40 #define LEVELSETUP_COOKIE "ROCKSNDIAMONDS_LEVELSETUP_FILE_VERSION_1.2"
41 #define LEVELINFO_COOKIE "ROCKSNDIAMONDS_LEVELINFO_FILE_VERSION_1.2"
42 /* old file identifiers for backward compatibility */
43 #define LEVEL_COOKIE_10 "ROCKSNDIAMONDS_LEVEL_FILE_VERSION_1.0"
44 #define TAPE_COOKIE_10 "ROCKSNDIAMONDS_LEVELREC_FILE_VERSION_1.0"
46 /* file names and filename extensions */
48 #define USERDATA_DIRECTORY ".rocksndiamonds"
49 #define SETUP_FILENAME "setup.conf"
50 #define LEVELSETUP_FILENAME "levelsetup.conf"
51 #define LEVELINFO_FILENAME "levelinfo.conf"
52 #define LEVELFILE_EXTENSION "level"
53 #define TAPEFILE_EXTENSION "tape"
54 #define SCOREFILE_EXTENSION "score"
56 #define USERDATA_DIRECTORY "userdata"
57 #define SETUP_FILENAME "setup.cnf"
58 #define LEVELSETUP_FILENAME "lvlsetup.cnf"
59 #define LEVELINFO_FILENAME "lvlinfo.cnf"
60 #define LEVELFILE_EXTENSION "lvl"
61 #define TAPEFILE_EXTENSION "tap"
62 #define SCOREFILE_EXTENSION "sco"
63 #define ERROR_FILENAME "error.out"
66 /* file permissions for newly written files */
67 #define MODE_R_ALL (S_IRUSR | S_IRGRP | S_IROTH)
68 #define MODE_W_ALL (S_IWUSR | S_IWGRP | S_IWOTH)
69 #define MODE_X_ALL (S_IXUSR | S_IXGRP | S_IXOTH)
70 #define USERDATA_DIR_MODE (MODE_R_ALL | MODE_X_ALL | S_IWUSR)
71 #define LEVEL_PERMS (MODE_R_ALL | MODE_W_ALL)
72 #define SCORE_PERMS LEVEL_PERMS
73 #define TAPE_PERMS LEVEL_PERMS
74 #define SETUP_PERMS LEVEL_PERMS
76 static void SaveUserLevelInfo(); /* for 'InitUserLevelDir()' */
77 static char *getSetupLine(char *, int); /* for 'SaveUserLevelInfo()' */
79 static char *getGlobalDataDir()
84 char *getUserDataDir()
86 static char *userdata_dir = NULL;
90 char *home_dir = getHomeDir();
91 char *data_dir = USERDATA_DIRECTORY;
93 userdata_dir = getPath2(home_dir, data_dir);
99 static char *getSetupDir()
101 return getUserDataDir();
104 static char *getUserLevelDir(char *level_subdir)
106 static char *userlevel_dir = NULL;
107 char *data_dir = getUserDataDir();
108 char *userlevel_subdir = LEVELS_DIRECTORY;
113 if (strlen(level_subdir) > 0)
114 userlevel_dir = getPath3(data_dir, userlevel_subdir, level_subdir);
116 userlevel_dir = getPath2(data_dir, userlevel_subdir);
118 return userlevel_dir;
121 static char *getTapeDir(char *level_subdir)
123 static char *tape_dir = NULL;
124 char *data_dir = getUserDataDir();
125 char *tape_subdir = TAPES_DIRECTORY;
130 if (strlen(level_subdir) > 0)
131 tape_dir = getPath3(data_dir, tape_subdir, level_subdir);
133 tape_dir = getPath2(data_dir, tape_subdir);
138 static char *getScoreDir(char *level_subdir)
140 static char *score_dir = NULL;
141 char *data_dir = getGlobalDataDir();
142 char *score_subdir = SCORES_DIRECTORY;
147 if (strlen(level_subdir) > 0)
148 score_dir = getPath3(data_dir, score_subdir, level_subdir);
150 score_dir = getPath2(data_dir, score_subdir);
155 static char *getLevelFilename(int nr)
157 static char *filename = NULL;
158 char basename[MAX_FILENAME_LEN];
160 if (filename != NULL)
163 sprintf(basename, "%03d.%s", nr, LEVELFILE_EXTENSION);
164 filename = getPath3((leveldir[leveldir_nr].user_defined ?
165 getUserLevelDir("") :
166 options.level_directory),
167 leveldir[leveldir_nr].filename,
173 static char *getTapeFilename(int nr)
175 static char *filename = NULL;
176 char basename[MAX_FILENAME_LEN];
178 if (filename != NULL)
181 sprintf(basename, "%03d.%s", nr, TAPEFILE_EXTENSION);
182 filename = getPath2(getTapeDir(leveldir[leveldir_nr].filename), basename);
187 static char *getScoreFilename(int nr)
189 static char *filename = NULL;
190 char basename[MAX_FILENAME_LEN];
192 if (filename != NULL)
195 sprintf(basename, "%03d.%s", nr, SCOREFILE_EXTENSION);
196 filename = getPath2(getScoreDir(leveldir[leveldir_nr].filename), basename);
201 static void createDirectory(char *dir, char *text)
203 if (access(dir, F_OK) != 0)
204 if (mkdir(dir, USERDATA_DIR_MODE) != 0)
205 Error(ERR_WARN, "cannot create %s directory '%s'", text, dir);
208 static void InitUserDataDirectory()
210 createDirectory(getUserDataDir(), "user data");
213 static void InitTapeDirectory(char *level_subdir)
215 createDirectory(getUserDataDir(), "user data");
216 createDirectory(getTapeDir(""), "main tape");
217 createDirectory(getTapeDir(level_subdir), "level tape");
220 static void InitScoreDirectory(char *level_subdir)
222 createDirectory(getScoreDir(""), "main score");
223 createDirectory(getScoreDir(level_subdir), "level score");
226 static void InitUserLevelDirectory(char *level_subdir)
228 if (access(getUserLevelDir(level_subdir), F_OK) != 0)
230 createDirectory(getUserDataDir(), "user data");
231 createDirectory(getUserLevelDir(""), "main user level");
232 createDirectory(getUserLevelDir(level_subdir), "user level");
238 static void setLevelInfoToDefaults()
242 lev_fieldx = level.fieldx = STD_LEV_FIELDX;
243 lev_fieldy = level.fieldy = STD_LEV_FIELDY;
245 for(x=0; x<MAX_LEV_FIELDX; x++)
246 for(y=0; y<MAX_LEV_FIELDY; y++)
247 Feld[x][y] = Ur[x][y] = EL_ERDREICH;
250 level.edelsteine = 0;
251 level.tempo_amoebe = 10;
252 level.dauer_sieb = 10;
253 level.dauer_ablenk = 10;
254 level.amoebe_inhalt = EL_DIAMANT;
256 strcpy(level.name, "Nameless Level");
258 for(i=0; i<LEVEL_SCORE_ELEMENTS; i++)
265 level.mampfer_inhalt[i][x][y] = EL_FELSBROCKEN;
267 Feld[0][0] = Ur[0][0] = EL_SPIELFIGUR;
268 Feld[STD_LEV_FIELDX-1][STD_LEV_FIELDY-1] =
269 Ur[STD_LEV_FIELDX-1][STD_LEV_FIELDY-1] = EL_AUSGANG_ZU;
272 void LoadLevel(int level_nr)
275 char *filename = getLevelFilename(level_nr);
276 char cookie[MAX_LINE_LEN];
277 char chunk[CHUNK_ID_LEN + 1];
278 int file_version = FILE_VERSION_1_2; /* last version of level files */
282 /* always start with reliable default values */
283 setLevelInfoToDefaults();
285 if (!(file = fopen(filename, "r")))
287 Error(ERR_WARN, "cannot read level '%s' - creating new level", filename);
291 /* check file identifier */
292 fgets(cookie, MAX_LINE_LEN, file);
293 if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
294 cookie[strlen(cookie) - 1] = '\0';
296 if (strcmp(cookie, LEVEL_COOKIE_10) == 0) /* old 1.0 level format */
297 file_version = FILE_VERSION_1_0;
298 else if (strcmp(cookie, LEVEL_COOKIE) != 0) /* unknown level format */
300 Error(ERR_WARN, "wrong file identifier of level file '%s'", filename);
305 /* read chunk "HEAD" */
306 if (file_version >= FILE_VERSION_1_2)
308 /* first check header chunk identifier and chunk length */
309 fgets(chunk, CHUNK_ID_LEN + 1, file);
311 (fgetc(file)<<24) | (fgetc(file)<<16) | (fgetc(file)<<8) | fgetc(file);
312 if (strcmp(chunk, "HEAD") || chunk_length != LEVEL_HEADER_SIZE)
314 Error(ERR_WARN, "wrong 'HEAD' chunk of level file '%s'", filename);
320 lev_fieldx = level.fieldx = fgetc(file);
321 lev_fieldy = level.fieldy = fgetc(file);
323 level.time = (fgetc(file)<<8) | fgetc(file);
324 level.edelsteine = (fgetc(file)<<8) | fgetc(file);
326 for(i=0; i<MAX_LEVNAMLEN; i++)
327 level.name[i] = fgetc(file);
328 level.name[MAX_LEVNAMLEN - 1] = 0;
330 for(i=0; i<LEVEL_SCORE_ELEMENTS; i++)
331 level.score[i] = fgetc(file);
341 level.mampfer_inhalt[i][x][y] = fgetc(file);
343 level.mampfer_inhalt[i][x][y] = EL_LEERRAUM;
348 level.tempo_amoebe = fgetc(file);
349 level.dauer_sieb = fgetc(file);
350 level.dauer_ablenk = fgetc(file);
351 level.amoebe_inhalt = fgetc(file);
353 for(i=0; i<LEVEL_HEADER_UNUSED; i++) /* skip unused header bytes */
356 /* read chunk "BODY" */
357 if (file_version >= FILE_VERSION_1_2)
359 fgets(chunk, CHUNK_ID_LEN + 1, file);
361 (fgetc(file)<<24) | (fgetc(file)<<16) | (fgetc(file)<<8) | fgetc(file);
363 /* look for optional content chunk */
364 if (strcmp(chunk, "CONT") == 0 && chunk_length == 4 + 8 * 3 * 3)
367 MampferMax = fgetc(file);
374 level.mampfer_inhalt[i][x][y] = fgetc(file);
376 fgets(chunk, CHUNK_ID_LEN + 1, file);
378 (fgetc(file)<<24) | (fgetc(file)<<16) | (fgetc(file)<<8) | fgetc(file);
381 /* next check body chunk identifier and chunk length */
382 if (strcmp(chunk, "BODY") || chunk_length != lev_fieldx * lev_fieldy)
384 Error(ERR_WARN, "wrong 'BODY' chunk of level file '%s'", filename);
390 for(y=0; y<lev_fieldy; y++)
391 for(x=0; x<lev_fieldx; x++)
392 Feld[x][y] = Ur[x][y] = fgetc(file);
396 if (level.time <= 10) /* minimum playing time of each level */
400 void SaveLevel(int level_nr)
403 char *filename = getLevelFilename(level_nr);
407 if (!(file = fopen(filename, "w")))
409 Error(ERR_WARN, "cannot save level file '%s'", filename);
413 fputs(LEVEL_COOKIE, file); /* file identifier */
416 fputs("HEAD", file); /* chunk identifier for file header */
418 chunk_length = LEVEL_HEADER_SIZE;
420 fputc((chunk_length >> 24) & 0xff, file);
421 fputc((chunk_length >> 16) & 0xff, file);
422 fputc((chunk_length >> 8) & 0xff, file);
423 fputc((chunk_length >> 0) & 0xff, file);
425 fputc(level.fieldx, file);
426 fputc(level.fieldy, file);
427 fputc(level.time / 256, file);
428 fputc(level.time % 256, file);
429 fputc(level.edelsteine / 256, file);
430 fputc(level.edelsteine % 256, file);
432 for(i=0; i<MAX_LEVNAMLEN; i++)
433 fputc(level.name[i], file);
434 for(i=0; i<LEVEL_SCORE_ELEMENTS; i++)
435 fputc(level.score[i], file);
439 fputc(level.mampfer_inhalt[i][x][y], file);
440 fputc(level.tempo_amoebe, file);
441 fputc(level.dauer_sieb, file);
442 fputc(level.dauer_ablenk, file);
443 fputc(level.amoebe_inhalt, file);
445 for(i=0; i<LEVEL_HEADER_UNUSED; i++) /* set unused header bytes to zero */
448 fputs("CONT", file); /* chunk identifier for contents */
450 chunk_length = 4 + 8 * 3 * 3;
452 fputc((chunk_length >> 24) & 0xff, file);
453 fputc((chunk_length >> 16) & 0xff, file);
454 fputc((chunk_length >> 8) & 0xff, file);
455 fputc((chunk_length >> 0) & 0xff, file);
457 fputc(EL_MAMPFER, file);
458 fputc(MampferMax, file);
465 fputc(level.mampfer_inhalt[i][x][y], file);
467 fputs("BODY", file); /* chunk identifier for file body */
468 chunk_length = lev_fieldx * lev_fieldy;
470 fputc((chunk_length >> 24) & 0xff, file);
471 fputc((chunk_length >> 16) & 0xff, file);
472 fputc((chunk_length >> 8) & 0xff, file);
473 fputc((chunk_length >> 0) & 0xff, file);
475 for(y=0; y<lev_fieldy; y++)
476 for(x=0; x<lev_fieldx; x++)
477 fputc(Ur[x][y], file);
481 chmod(filename, LEVEL_PERMS);
484 void LoadTape(int level_nr)
487 char *filename = getTapeFilename(level_nr);
488 char cookie[MAX_LINE_LEN];
489 char chunk[CHUNK_ID_LEN + 1];
491 int num_participating_players;
492 int file_version = FILE_VERSION_1_2; /* last version of tape files */
495 /* always start with reliable default values (empty tape) */
498 /* default values (also for pre-1.2 tapes) with only the first player */
499 tape.player_participates[0] = TRUE;
500 for(i=1; i<MAX_PLAYERS; i++)
501 tape.player_participates[i] = FALSE;
503 /* at least one (default: the first) player participates in every tape */
504 num_participating_players = 1;
506 if (!(file = fopen(filename, "r")))
509 /* check file identifier */
510 fgets(cookie, MAX_LINE_LEN, file);
511 if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
512 cookie[strlen(cookie) - 1] = '\0';
514 if (strcmp(cookie, TAPE_COOKIE_10) == 0) /* old 1.0 tape format */
515 file_version = FILE_VERSION_1_0;
516 else if (strcmp(cookie, TAPE_COOKIE) != 0) /* unknown tape format */
518 Error(ERR_WARN, "wrong file identifier of tape file '%s'", filename);
523 /* read chunk "HEAD" */
524 if (file_version >= FILE_VERSION_1_2)
526 /* first check header chunk identifier and chunk length */
527 fgets(chunk, CHUNK_ID_LEN + 1, file);
529 (fgetc(file)<<24) | (fgetc(file)<<16) | (fgetc(file)<<8) | fgetc(file);
531 if (strcmp(chunk, "HEAD") || chunk_length != TAPE_HEADER_SIZE)
533 Error(ERR_WARN, "wrong 'HEAD' chunk of tape file '%s'", filename);
540 (fgetc(file)<<24) | (fgetc(file)<<16) | (fgetc(file)<<8) | fgetc(file);
542 (fgetc(file)<<24) | (fgetc(file)<<16) | (fgetc(file)<<8) | fgetc(file);
544 (fgetc(file)<<24) | (fgetc(file)<<16) | (fgetc(file)<<8) | fgetc(file);
546 /* read header fields that are new since version 1.2 */
547 if (file_version >= FILE_VERSION_1_2)
549 byte store_participating_players = fgetc(file);
551 for(i=0; i<TAPE_HEADER_UNUSED; i++) /* skip unused header bytes */
554 /* since version 1.2, tapes store which players participate in the tape */
555 num_participating_players = 0;
556 for(i=0; i<MAX_PLAYERS; i++)
558 tape.player_participates[i] = FALSE;
560 if (store_participating_players & (1 << i))
562 tape.player_participates[i] = TRUE;
563 num_participating_players++;
568 tape.level_nr = level_nr;
570 tape.changed = FALSE;
572 tape.recording = FALSE;
573 tape.playing = FALSE;
574 tape.pausing = FALSE;
576 /* read chunk "BODY" */
577 if (file_version >= FILE_VERSION_1_2)
579 /* next check body chunk identifier and chunk length */
580 fgets(chunk, CHUNK_ID_LEN + 1, file);
582 (fgetc(file)<<24) | (fgetc(file)<<16) | (fgetc(file)<<8) | fgetc(file);
583 if (strcmp(chunk, "BODY") ||
584 chunk_length != (num_participating_players + 1) * tape.length)
586 Error(ERR_WARN, "wrong 'BODY' chunk of tape file '%s'", filename);
592 for(i=0; i<tape.length; i++)
594 if (i >= MAX_TAPELEN)
597 for(j=0; j<MAX_PLAYERS; j++)
599 tape.pos[i].action[j] = MV_NO_MOVING;
601 if (tape.player_participates[j])
602 tape.pos[i].action[j] = fgetc(file);
605 tape.pos[i].delay = fgetc(file);
607 if (file_version == FILE_VERSION_1_0)
609 /* eliminate possible diagonal moves in old tapes */
610 /* this is only for backward compatibility */
612 byte joy_dir[4] = { JOY_LEFT, JOY_RIGHT, JOY_UP, JOY_DOWN };
613 byte action = tape.pos[i].action[0];
614 int k, num_moves = 0;
618 if (action & joy_dir[k])
620 tape.pos[i + num_moves].action[0] = joy_dir[k];
622 tape.pos[i + num_moves].delay = 0;
631 tape.length += num_moves;
641 if (i != tape.length)
642 Error(ERR_WARN, "level recording file '%s' corrupted", filename);
644 tape.length_seconds = GetTapeLength();
647 void SaveTape(int level_nr)
650 char *filename = getTapeFilename(level_nr);
652 boolean new_tape = TRUE;
653 byte store_participating_players;
654 int num_participating_players;
657 InitTapeDirectory(leveldir[leveldir_nr].filename);
659 /* if a tape still exists, ask to overwrite it */
660 if (access(filename, F_OK) == 0)
663 if (!Request("Replace old tape ?", REQ_ASK))
667 /* count number of players and set corresponding bits for compact storage */
668 store_participating_players = 0;
669 num_participating_players = 0;
670 for(i=0; i<MAX_PLAYERS; i++)
672 if (tape.player_participates[i])
674 num_participating_players++;
675 store_participating_players |= (1 << i);
679 if (!(file = fopen(filename, "w")))
681 Error(ERR_WARN, "cannot save level recording file '%s'", filename);
685 fputs(TAPE_COOKIE, file); /* file identifier */
688 fputs("HEAD", file); /* chunk identifier for file header */
690 chunk_length = TAPE_HEADER_SIZE;
692 fputc((chunk_length >> 24) & 0xff, file);
693 fputc((chunk_length >> 16) & 0xff, file);
694 fputc((chunk_length >> 8) & 0xff, file);
695 fputc((chunk_length >> 0) & 0xff, file);
697 fputc((tape.random_seed >> 24) & 0xff, file);
698 fputc((tape.random_seed >> 16) & 0xff, file);
699 fputc((tape.random_seed >> 8) & 0xff, file);
700 fputc((tape.random_seed >> 0) & 0xff, file);
702 fputc((tape.date >> 24) & 0xff, file);
703 fputc((tape.date >> 16) & 0xff, file);
704 fputc((tape.date >> 8) & 0xff, file);
705 fputc((tape.date >> 0) & 0xff, file);
707 fputc((tape.length >> 24) & 0xff, file);
708 fputc((tape.length >> 16) & 0xff, file);
709 fputc((tape.length >> 8) & 0xff, file);
710 fputc((tape.length >> 0) & 0xff, file);
712 fputc(store_participating_players, file);
714 for(i=0; i<TAPE_HEADER_UNUSED; i++) /* set unused header bytes to zero */
717 fputs("BODY", file); /* chunk identifier for file body */
718 chunk_length = (num_participating_players + 1) * tape.length;
720 fputc((chunk_length >> 24) & 0xff, file);
721 fputc((chunk_length >> 16) & 0xff, file);
722 fputc((chunk_length >> 8) & 0xff, file);
723 fputc((chunk_length >> 0) & 0xff, file);
725 for(i=0; i<tape.length; i++)
729 for(j=0; j<MAX_PLAYERS; j++)
730 if (tape.player_participates[j])
731 fputc(tape.pos[i].action[j], file);
733 fputc(tape.pos[i].delay, file);
738 chmod(filename, TAPE_PERMS);
740 tape.changed = FALSE;
743 Request("tape saved !", REQ_CONFIRM);
746 void LoadScore(int level_nr)
749 char *filename = getScoreFilename(level_nr);
750 char cookie[MAX_LINE_LEN];
751 char line[MAX_LINE_LEN];
755 /* always start with reliable default values */
756 for(i=0; i<MAX_SCORE_ENTRIES; i++)
758 strcpy(highscore[i].Name, EMPTY_PLAYER_NAME);
759 highscore[i].Score = 0;
762 if (!(file = fopen(filename, "r")))
765 /* check file identifier */
766 fgets(cookie, MAX_LINE_LEN, file);
767 if (strlen(cookie) > 0 && cookie[strlen(cookie) - 1] == '\n')
768 cookie[strlen(cookie) - 1] = '\0';
770 if (strcmp(cookie, SCORE_COOKIE) != 0)
772 Error(ERR_WARN, "wrong file identifier of score file '%s'", filename);
777 for(i=0; i<MAX_SCORE_ENTRIES; i++)
779 fscanf(file, "%d", &highscore[i].Score);
780 fgets(line, MAX_LINE_LEN, file);
782 if (line[strlen(line)-1] == '\n')
783 line[strlen(line)-1] = '\0';
785 for (line_ptr = line; *line_ptr; line_ptr++)
787 if (*line_ptr != ' ' && *line_ptr != '\t' && *line_ptr != '\0')
789 strncpy(highscore[i].Name, line_ptr, MAX_NAMELEN - 1);
790 highscore[i].Name[MAX_NAMELEN - 1] = '\0';
799 void SaveScore(int level_nr)
802 char *filename = getScoreFilename(level_nr);
805 InitScoreDirectory(leveldir[leveldir_nr].filename);
807 if (!(file = fopen(filename, "w")))
809 Error(ERR_WARN, "cannot save score for level %d", level_nr);
813 fprintf(file, "%s\n\n", SCORE_COOKIE);
815 for(i=0; i<MAX_SCORE_ENTRIES; i++)
816 fprintf(file, "%d %s\n", highscore[i].Score, highscore[i].Name);
820 chmod(filename, SCORE_PERMS);
823 #define TOKEN_STR_FILE_IDENTIFIER "file_identifier"
824 #define TOKEN_STR_LAST_LEVEL_SERIES "last_level_series"
825 #define TOKEN_STR_PLAYER_PREFIX "player_"
827 #define TOKEN_VALUE_POSITION 30
830 #define SETUP_TOKEN_PLAYER_NAME 0
831 #define SETUP_TOKEN_SOUND 1
832 #define SETUP_TOKEN_SOUND_LOOPS 2
833 #define SETUP_TOKEN_SOUND_MUSIC 3
834 #define SETUP_TOKEN_SOUND_SIMPLE 4
835 #define SETUP_TOKEN_TOONS 5
836 #define SETUP_TOKEN_DOUBLE_BUFFERING 6
837 #define SETUP_TOKEN_SCROLL_DELAY 7
838 #define SETUP_TOKEN_SOFT_SCROLLING 8
839 #define SETUP_TOKEN_FADING 9
840 #define SETUP_TOKEN_AUTORECORD 10
841 #define SETUP_TOKEN_QUICK_DOORS 11
842 #define SETUP_TOKEN_TEAM_MODE 12
845 #define SETUP_TOKEN_USE_JOYSTICK 13
846 #define SETUP_TOKEN_JOY_DEVICE_NAME 14
847 #define SETUP_TOKEN_JOY_XLEFT 15
848 #define SETUP_TOKEN_JOY_XMIDDLE 16
849 #define SETUP_TOKEN_JOY_XRIGHT 17
850 #define SETUP_TOKEN_JOY_YUPPER 18
851 #define SETUP_TOKEN_JOY_YMIDDLE 19
852 #define SETUP_TOKEN_JOY_YLOWER 20
853 #define SETUP_TOKEN_JOY_SNAP 21
854 #define SETUP_TOKEN_JOY_BOMB 22
855 #define SETUP_TOKEN_KEY_LEFT 23
856 #define SETUP_TOKEN_KEY_RIGHT 24
857 #define SETUP_TOKEN_KEY_UP 25
858 #define SETUP_TOKEN_KEY_DOWN 26
859 #define SETUP_TOKEN_KEY_SNAP 27
860 #define SETUP_TOKEN_KEY_BOMB 28
862 /* level directory info */
863 #define LEVELINFO_TOKEN_NAME 29
864 #define LEVELINFO_TOKEN_LEVELS 30
865 #define LEVELINFO_TOKEN_SORT_PRIORITY 31
866 #define LEVELINFO_TOKEN_READONLY 32
868 #define FIRST_GLOBAL_SETUP_TOKEN SETUP_TOKEN_PLAYER_NAME
869 #define LAST_GLOBAL_SETUP_TOKEN SETUP_TOKEN_TEAM_MODE
871 #define FIRST_PLAYER_SETUP_TOKEN SETUP_TOKEN_USE_JOYSTICK
872 #define LAST_PLAYER_SETUP_TOKEN SETUP_TOKEN_KEY_BOMB
874 #define FIRST_LEVELINFO_TOKEN LEVELINFO_TOKEN_NAME
875 #define LAST_LEVELINFO_TOKEN LEVELINFO_TOKEN_READONLY
877 #define TYPE_BOOLEAN 1
878 #define TYPE_SWITCH 2
879 #define TYPE_KEYSYM 3
880 #define TYPE_INTEGER 4
881 #define TYPE_STRING 5
883 static struct SetupInfo si;
884 static struct SetupInputInfo sii;
885 static struct LevelDirInfo ldi;
894 { TYPE_STRING, &si.player_name, "player_name" },
895 { TYPE_SWITCH, &si.sound, "sound" },
896 { TYPE_SWITCH, &si.sound_loops, "repeating_sound_loops" },
897 { TYPE_SWITCH, &si.sound_music, "background_music" },
898 { TYPE_SWITCH, &si.sound_simple, "simple_sound_effects" },
899 { TYPE_SWITCH, &si.toons, "toons" },
900 { TYPE_SWITCH, &si.double_buffering, "double_buffering" },
901 { TYPE_SWITCH, &si.scroll_delay, "scroll_delay" },
902 { TYPE_SWITCH, &si.soft_scrolling, "soft_scrolling" },
903 { TYPE_SWITCH, &si.fading, "screen_fading" },
904 { TYPE_SWITCH, &si.autorecord, "automatic_tape_recording" },
905 { TYPE_SWITCH, &si.quick_doors, "quick_doors" },
906 { TYPE_SWITCH, &si.team_mode, "team_mode" },
909 { TYPE_BOOLEAN, &sii.use_joystick, ".use_joystick" },
910 { TYPE_STRING, &sii.joy.device_name, ".joy.device_name" },
911 { TYPE_INTEGER, &sii.joy.xleft, ".joy.xleft" },
912 { TYPE_INTEGER, &sii.joy.xmiddle, ".joy.xmiddle" },
913 { TYPE_INTEGER, &sii.joy.xright, ".joy.xright" },
914 { TYPE_INTEGER, &sii.joy.yupper, ".joy.yupper" },
915 { TYPE_INTEGER, &sii.joy.ymiddle, ".joy.ymiddle" },
916 { TYPE_INTEGER, &sii.joy.ylower, ".joy.ylower" },
917 { TYPE_INTEGER, &sii.joy.snap, ".joy.snap_field" },
918 { TYPE_INTEGER, &sii.joy.bomb, ".joy.place_bomb" },
919 { TYPE_KEYSYM, &sii.key.left, ".key.move_left" },
920 { TYPE_KEYSYM, &sii.key.right, ".key.move_right" },
921 { TYPE_KEYSYM, &sii.key.up, ".key.move_up" },
922 { TYPE_KEYSYM, &sii.key.down, ".key.move_down" },
923 { TYPE_KEYSYM, &sii.key.snap, ".key.snap_field" },
924 { TYPE_KEYSYM, &sii.key.bomb, ".key.place_bomb" },
926 /* level directory info */
927 { TYPE_STRING, &ldi.name, "name" },
928 { TYPE_INTEGER, &ldi.levels, "levels" },
929 { TYPE_INTEGER, &ldi.sort_priority, "sort_priority" },
930 { TYPE_BOOLEAN, &ldi.readonly, "readonly" }
933 static char *string_tolower(char *s)
935 static char s_lower[100];
938 if (strlen(s) >= 100)
943 for (i=0; i<strlen(s_lower); i++)
944 s_lower[i] = tolower(s_lower[i]);
949 static int get_string_integer_value(char *s)
951 static char *number_text[][3] =
953 { "0", "zero", "null", },
954 { "1", "one", "first" },
955 { "2", "two", "second" },
956 { "3", "three", "third" },
957 { "4", "four", "fourth" },
958 { "5", "five", "fifth" },
959 { "6", "six", "sixth" },
960 { "7", "seven", "seventh" },
961 { "8", "eight", "eighth" },
962 { "9", "nine", "ninth" },
963 { "10", "ten", "tenth" },
964 { "11", "eleven", "eleventh" },
965 { "12", "twelve", "twelfth" },
972 if (strcmp(string_tolower(s), number_text[i][j]) == 0)
978 static boolean get_string_boolean_value(char *s)
980 if (strcmp(string_tolower(s), "true") == 0 ||
981 strcmp(string_tolower(s), "yes") == 0 ||
982 strcmp(string_tolower(s), "on") == 0 ||
983 get_string_integer_value(s) == 1)
989 static char *getFormattedSetupEntry(char *token, char *value)
992 static char entry[MAX_LINE_LEN];
994 sprintf(entry, "%s:", token);
995 for (i=strlen(entry); i<TOKEN_VALUE_POSITION; i++)
999 strcat(entry, value);
1004 static void freeSetupFileList(struct SetupFileList *setup_file_list)
1006 if (!setup_file_list)
1009 if (setup_file_list->token)
1010 free(setup_file_list->token);
1011 if (setup_file_list->value)
1012 free(setup_file_list->value);
1013 if (setup_file_list->next)
1014 freeSetupFileList(setup_file_list->next);
1015 free(setup_file_list);
1018 static struct SetupFileList *newSetupFileList(char *token, char *value)
1020 struct SetupFileList *new = checked_malloc(sizeof(struct SetupFileList));
1022 new->token = checked_malloc(strlen(token) + 1);
1023 strcpy(new->token, token);
1025 new->value = checked_malloc(strlen(value) + 1);
1026 strcpy(new->value, value);
1033 static char *getTokenValue(struct SetupFileList *setup_file_list,
1036 if (!setup_file_list)
1039 if (strcmp(setup_file_list->token, token) == 0)
1040 return setup_file_list->value;
1042 return getTokenValue(setup_file_list->next, token);
1045 static void setTokenValue(struct SetupFileList *setup_file_list,
1046 char *token, char *value)
1048 if (!setup_file_list)
1051 if (strcmp(setup_file_list->token, token) == 0)
1053 free(setup_file_list->value);
1054 setup_file_list->value = checked_malloc(strlen(value) + 1);
1055 strcpy(setup_file_list->value, value);
1057 else if (setup_file_list->next == NULL)
1058 setup_file_list->next = newSetupFileList(token, value);
1060 setTokenValue(setup_file_list->next, token, value);
1064 static void printSetupFileList(struct SetupFileList *setup_file_list)
1066 if (!setup_file_list)
1069 printf("token: '%s'\n", setup_file_list->token);
1070 printf("value: '%s'\n", setup_file_list->value);
1072 printSetupFileList(setup_file_list->next);
1076 static struct SetupFileList *loadSetupFileList(char *filename)
1079 char line[MAX_LINE_LEN];
1080 char *token, *value, *line_ptr;
1081 struct SetupFileList *setup_file_list = newSetupFileList("", "");
1082 struct SetupFileList *first_valid_list_entry;
1086 if (!(file = fopen(filename, "r")))
1088 Error(ERR_WARN, "cannot open configuration file '%s'", filename);
1094 /* read next line of input file */
1095 if (!fgets(line, MAX_LINE_LEN, file))
1098 /* cut trailing comment or whitespace from input line */
1099 for (line_ptr = line; *line_ptr; line_ptr++)
1101 if (*line_ptr == '#' || *line_ptr == '\n')
1108 /* cut trailing whitespaces from input line */
1109 for (line_ptr = &line[strlen(line)]; line_ptr > line; line_ptr--)
1110 if ((*line_ptr == ' ' || *line_ptr == '\t') && line_ptr[1] == '\0')
1113 /* ignore empty lines */
1117 line_len = strlen(line);
1119 /* cut leading whitespaces from token */
1120 for (token = line; *token; token++)
1121 if (*token != ' ' && *token != '\t')
1124 /* find end of token */
1125 for (line_ptr = token; *line_ptr; line_ptr++)
1127 if (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == ':')
1134 if (line_ptr < line + line_len)
1135 value = line_ptr + 1;
1139 /* cut leading whitespaces from value */
1140 for (; *value; value++)
1141 if (*value != ' ' && *value != '\t')
1144 if (*token && *value)
1145 setTokenValue(setup_file_list, token, value);
1150 first_valid_list_entry = setup_file_list->next;
1152 /* free empty list header */
1153 setup_file_list->next = NULL;
1154 freeSetupFileList(setup_file_list);
1156 if (first_valid_list_entry == NULL)
1157 Error(ERR_WARN, "configuration file '%s' is empty", filename);
1159 return first_valid_list_entry;
1162 static void checkSetupFileListIdentifier(struct SetupFileList *setup_file_list,
1165 if (!setup_file_list)
1168 if (strcmp(setup_file_list->token, TOKEN_STR_FILE_IDENTIFIER) == 0)
1170 if (strcmp(setup_file_list->value, identifier) != 0)
1172 Error(ERR_WARN, "configuration file has wrong version");
1179 if (setup_file_list->next)
1180 checkSetupFileListIdentifier(setup_file_list->next, identifier);
1183 Error(ERR_WARN, "configuration file has no version information");
1188 static void setLevelDirInfoToDefaults(struct LevelDirInfo *ldi)
1190 ldi->name = getStringCopy("non-existing");
1192 ldi->sort_priority = 999; /* default: least priority */
1193 ldi->readonly = TRUE;
1196 static void setSetupInfoToDefaults(struct SetupInfo *si)
1200 si->player_name = getStringCopy(getLoginName());
1203 si->sound_loops = TRUE;
1204 si->sound_music = TRUE;
1205 si->sound_simple = TRUE;
1207 si->double_buffering = TRUE;
1208 si->direct_draw = !si->double_buffering;
1209 si->scroll_delay = TRUE;
1210 si->soft_scrolling = TRUE;
1212 si->autorecord = TRUE;
1213 si->quick_doors = FALSE;
1215 for (i=0; i<MAX_PLAYERS; i++)
1217 si->input[i].use_joystick = FALSE;
1218 si->input[i].joy.device_name = getStringCopy(joystick_device_name[i]);
1219 si->input[i].joy.xleft = JOYSTICK_XLEFT;
1220 si->input[i].joy.xmiddle = JOYSTICK_XMIDDLE;
1221 si->input[i].joy.xright = JOYSTICK_XRIGHT;
1222 si->input[i].joy.yupper = JOYSTICK_YUPPER;
1223 si->input[i].joy.ymiddle = JOYSTICK_YMIDDLE;
1224 si->input[i].joy.ylower = JOYSTICK_YLOWER;
1225 si->input[i].joy.snap = (i == 0 ? JOY_BUTTON_1 : 0);
1226 si->input[i].joy.bomb = (i == 0 ? JOY_BUTTON_2 : 0);
1227 si->input[i].key.left = (i == 0 ? DEFAULT_KEY_LEFT : KEY_UNDEFINDED);
1228 si->input[i].key.right = (i == 0 ? DEFAULT_KEY_RIGHT : KEY_UNDEFINDED);
1229 si->input[i].key.up = (i == 0 ? DEFAULT_KEY_UP : KEY_UNDEFINDED);
1230 si->input[i].key.down = (i == 0 ? DEFAULT_KEY_DOWN : KEY_UNDEFINDED);
1231 si->input[i].key.snap = (i == 0 ? DEFAULT_KEY_SNAP : KEY_UNDEFINDED);
1232 si->input[i].key.bomb = (i == 0 ? DEFAULT_KEY_BOMB : KEY_UNDEFINDED);
1236 static void setSetupInfo(int token_nr, char *token_value)
1238 int token_type = token_info[token_nr].type;
1239 void *setup_value = token_info[token_nr].value;
1241 if (token_value == NULL)
1244 /* set setup field to corresponding token value */
1249 *(boolean *)setup_value = get_string_boolean_value(token_value);
1253 *(KeySym *)setup_value = getKeySymFromX11KeyName(token_value);
1257 *(int *)setup_value = get_string_integer_value(token_value);
1261 if (*(char **)setup_value != NULL)
1262 free(*(char **)setup_value);
1263 *(char **)setup_value = getStringCopy(token_value);
1271 static void decodeSetupFileList(struct SetupFileList *setup_file_list)
1275 if (!setup_file_list)
1278 /* handle global setup values */
1280 for (i=FIRST_GLOBAL_SETUP_TOKEN; i<=LAST_GLOBAL_SETUP_TOKEN; i++)
1281 setSetupInfo(i, getTokenValue(setup_file_list, token_info[i].text));
1284 /* handle player specific setup values */
1285 for (pnr=0; pnr<MAX_PLAYERS; pnr++)
1289 sprintf(prefix, "%s%d", TOKEN_STR_PLAYER_PREFIX, pnr + 1);
1291 sii = setup.input[pnr];
1292 for (i=FIRST_PLAYER_SETUP_TOKEN; i<=LAST_PLAYER_SETUP_TOKEN; i++)
1294 char full_token[100];
1296 sprintf(full_token, "%s%s", prefix, token_info[i].text);
1297 setSetupInfo(i, getTokenValue(setup_file_list, full_token));
1299 setup.input[pnr] = sii;
1303 int getLevelSeriesNrFromLevelSeriesName(char *level_series_name)
1307 if (!level_series_name)
1310 for (i=0; i<num_leveldirs; i++)
1311 if (strcmp(level_series_name, leveldir[i].filename) == 0)
1317 int getLastPlayedLevelOfLevelSeries(char *level_series_name)
1320 int level_series_nr = getLevelSeriesNrFromLevelSeriesName(level_series_name);
1321 int last_level_nr = 0;
1323 if (!level_series_name)
1326 token_value = getTokenValue(level_setup_list, level_series_name);
1330 int highest_level_nr = leveldir[level_series_nr].levels - 1;
1332 last_level_nr = atoi(token_value);
1334 if (last_level_nr < 0)
1336 if (last_level_nr > highest_level_nr)
1337 last_level_nr = highest_level_nr;
1340 return last_level_nr;
1343 static int compareLevelDirInfoEntries(const void *object1, const void *object2)
1345 const struct LevelDirInfo *entry1 = object1;
1346 const struct LevelDirInfo *entry2 = object2;
1349 if (entry1->sort_priority != entry2->sort_priority)
1350 compare_result = entry1->sort_priority - entry2->sort_priority;
1353 char *name1 = getStringToLower(entry1->name);
1354 char *name2 = getStringToLower(entry2->name);
1356 compare_result = strcmp(name1, name2);
1362 return compare_result;
1365 static int LoadLevelInfoFromLevelDir(char *level_directory, int start_entry)
1368 struct stat file_status;
1369 char *directory = NULL;
1370 char *filename = NULL;
1371 struct SetupFileList *setup_file_list = NULL;
1372 struct dirent *dir_entry;
1373 int i, current_entry = start_entry;
1375 if ((dir = opendir(level_directory)) == NULL)
1377 Error(ERR_WARN, "cannot read level directory '%s'", level_directory);
1378 return current_entry;
1381 while (current_entry < MAX_LEVDIR_ENTRIES)
1383 if ((dir_entry = readdir(dir)) == NULL) /* last directory entry */
1386 /* skip entries for current and parent directory */
1387 if (strcmp(dir_entry->d_name, ".") == 0 ||
1388 strcmp(dir_entry->d_name, "..") == 0)
1391 /* find out if directory entry is itself a directory */
1392 directory = getPath2(level_directory, dir_entry->d_name);
1393 if (stat(directory, &file_status) != 0 || /* cannot stat file */
1394 (file_status.st_mode & S_IFMT) != S_IFDIR) /* not a directory */
1400 filename = getPath2(directory, LEVELINFO_FILENAME);
1401 setup_file_list = loadSetupFileList(filename);
1403 if (setup_file_list)
1405 checkSetupFileListIdentifier(setup_file_list, LEVELINFO_COOKIE);
1406 setLevelDirInfoToDefaults(&leveldir[current_entry]);
1408 ldi = leveldir[current_entry];
1409 for (i=FIRST_LEVELINFO_TOKEN; i<=LAST_LEVELINFO_TOKEN; i++)
1410 setSetupInfo(i, getTokenValue(setup_file_list, token_info[i].text));
1411 leveldir[current_entry] = ldi;
1413 leveldir[current_entry].filename = getStringCopy(dir_entry->d_name);
1414 leveldir[current_entry].user_defined =
1415 (level_directory == options.level_directory ? FALSE : TRUE);
1417 freeSetupFileList(setup_file_list);
1421 Error(ERR_WARN, "ignoring level directory '%s'", directory);
1427 if (current_entry == MAX_LEVDIR_ENTRIES)
1428 Error(ERR_WARN, "using %d level directories -- ignoring the rest",
1433 if (current_entry == start_entry)
1434 Error(ERR_WARN, "cannot find any valid level series in directory '%s'",
1437 return current_entry;
1440 void LoadLevelInfo()
1442 InitUserLevelDirectory(getLoginName());
1447 num_leveldirs = LoadLevelInfoFromLevelDir(options.level_directory,
1449 num_leveldirs = LoadLevelInfoFromLevelDir(getUserLevelDir(""),
1452 if (num_leveldirs == 0)
1453 Error(ERR_EXIT, "cannot find any valid level series in any directory");
1455 if (num_leveldirs > 1)
1456 qsort(leveldir, num_leveldirs, sizeof(struct LevelDirInfo),
1457 compareLevelDirInfoEntries);
1460 static void SaveUserLevelInfo()
1466 filename = getPath2(getUserLevelDir(getLoginName()), LEVELINFO_FILENAME);
1468 if (!(file = fopen(filename, "w")))
1470 Error(ERR_WARN, "cannot write level info file '%s'", filename);
1475 ldi.name = getLoginName();
1477 ldi.sort_priority = 300;
1478 ldi.readonly = FALSE;
1480 fprintf(file, "%s\n\n",
1481 getFormattedSetupEntry(TOKEN_STR_FILE_IDENTIFIER, LEVELINFO_COOKIE));
1483 for (i=FIRST_LEVELINFO_TOKEN; i<=LAST_LEVELINFO_TOKEN; i++)
1484 fprintf(file, "%s\n", getSetupLine("", i));
1489 chmod(filename, SETUP_PERMS);
1495 struct SetupFileList *setup_file_list = NULL;
1497 /* always start with reliable default values */
1498 setSetupInfoToDefaults(&setup);
1500 filename = getPath2(getSetupDir(), SETUP_FILENAME);
1502 setup_file_list = loadSetupFileList(filename);
1504 if (setup_file_list)
1506 checkSetupFileListIdentifier(setup_file_list, SETUP_COOKIE);
1507 decodeSetupFileList(setup_file_list);
1509 setup.direct_draw = !setup.double_buffering;
1511 freeSetupFileList(setup_file_list);
1513 /* needed to work around problems with fixed length strings */
1514 if (strlen(setup.player_name) >= MAX_NAMELEN)
1515 setup.player_name[MAX_NAMELEN - 1] = '\0';
1516 else if (strlen(setup.player_name) < MAX_NAMELEN - 1)
1518 char *new_name = checked_malloc(MAX_NAMELEN);
1520 strcpy(new_name, setup.player_name);
1521 free(setup.player_name);
1522 setup.player_name = new_name;
1526 Error(ERR_WARN, "using default setup values");
1531 static char *getSetupLine(char *prefix, int token_nr)
1534 static char entry[MAX_LINE_LEN];
1535 int token_type = token_info[token_nr].type;
1536 void *setup_value = token_info[token_nr].value;
1537 char *token_text = token_info[token_nr].text;
1539 /* start with the prefix, token and some spaces to format output line */
1540 sprintf(entry, "%s%s:", prefix, token_text);
1541 for (i=strlen(entry); i<TOKEN_VALUE_POSITION; i++)
1544 /* continue with the token's value (which can have different types) */
1548 strcat(entry, (*(boolean *)setup_value ? "true" : "false"));
1552 strcat(entry, (*(boolean *)setup_value ? "on" : "off"));
1557 KeySym keysym = *(KeySym *)setup_value;
1558 char *keyname = getKeyNameFromKeySym(keysym);
1560 strcat(entry, getX11KeyNameFromKeySym(keysym));
1561 for (i=strlen(entry); i<50; i++)
1564 /* add comment, if useful */
1565 if (strcmp(keyname, "(undefined)") != 0 &&
1566 strcmp(keyname, "(unknown)") != 0)
1568 strcat(entry, "# ");
1569 strcat(entry, keyname);
1576 char buffer[MAX_LINE_LEN];
1578 sprintf(buffer, "%d", *(int *)setup_value);
1579 strcat(entry, buffer);
1584 strcat(entry, *(char **)setup_value);
1600 InitUserDataDirectory();
1602 filename = getPath2(getSetupDir(), SETUP_FILENAME);
1604 if (!(file = fopen(filename, "w")))
1606 Error(ERR_WARN, "cannot write setup file '%s'", filename);
1611 fprintf(file, "%s\n",
1612 getFormattedSetupEntry(TOKEN_STR_FILE_IDENTIFIER, SETUP_COOKIE));
1613 fprintf(file, "\n");
1615 /* handle global setup values */
1617 for (i=FIRST_GLOBAL_SETUP_TOKEN; i<=LAST_GLOBAL_SETUP_TOKEN; i++)
1619 fprintf(file, "%s\n", getSetupLine("", i));
1621 /* just to make things nicer :) */
1622 if (i == SETUP_TOKEN_PLAYER_NAME)
1623 fprintf(file, "\n");
1626 /* handle player specific setup values */
1627 for (pnr=0; pnr<MAX_PLAYERS; pnr++)
1631 sprintf(prefix, "%s%d", TOKEN_STR_PLAYER_PREFIX, pnr + 1);
1632 fprintf(file, "\n");
1634 sii = setup.input[pnr];
1635 for (i=FIRST_PLAYER_SETUP_TOKEN; i<=LAST_PLAYER_SETUP_TOKEN; i++)
1636 fprintf(file, "%s\n", getSetupLine(prefix, i));
1642 chmod(filename, SETUP_PERMS);
1645 void LoadLevelSetup()
1649 /* always start with reliable default values */
1653 filename = getPath2(getSetupDir(), LEVELSETUP_FILENAME);
1655 if (level_setup_list)
1656 freeSetupFileList(level_setup_list);
1658 level_setup_list = loadSetupFileList(filename);
1660 if (level_setup_list)
1662 char *last_level_series =
1663 getTokenValue(level_setup_list, TOKEN_STR_LAST_LEVEL_SERIES);
1665 leveldir_nr = getLevelSeriesNrFromLevelSeriesName(last_level_series);
1666 level_nr = getLastPlayedLevelOfLevelSeries(last_level_series);
1668 checkSetupFileListIdentifier(level_setup_list, LEVELSETUP_COOKIE);
1672 level_setup_list = newSetupFileList(TOKEN_STR_FILE_IDENTIFIER,
1674 Error(ERR_WARN, "using default setup values");
1680 void SaveLevelSetup()
1683 struct SetupFileList *list_entry = level_setup_list;
1686 InitUserDataDirectory();
1688 setTokenValue(level_setup_list,
1689 TOKEN_STR_LAST_LEVEL_SERIES, leveldir[leveldir_nr].filename);
1691 setTokenValue(level_setup_list,
1692 leveldir[leveldir_nr].filename, int2str(level_nr, 0));
1694 filename = getPath2(getSetupDir(), LEVELSETUP_FILENAME);
1696 if (!(file = fopen(filename, "w")))
1698 Error(ERR_WARN, "cannot write setup file '%s'", filename);
1703 fprintf(file, "%s\n\n", getFormattedSetupEntry(TOKEN_STR_FILE_IDENTIFIER,
1704 LEVELSETUP_COOKIE));
1707 if (strcmp(list_entry->token, TOKEN_STR_FILE_IDENTIFIER) != 0)
1708 fprintf(file, "%s\n",
1709 getFormattedSetupEntry(list_entry->token, list_entry->value));
1711 /* just to make things nicer :) */
1712 if (strcmp(list_entry->token, TOKEN_STR_LAST_LEVEL_SERIES) == 0)
1713 fprintf(file, "\n");
1715 list_entry = list_entry->next;
1721 chmod(filename, SETUP_PERMS);
1725 static boolean initErrorFile()
1730 InitUserDataDirectory();
1732 filename = getPath2(getUserDataDir(), ERROR_FILENAME);
1733 error_file = fopen(filename, "w");
1736 if (error_file == NULL)
1744 FILE *openErrorFile()
1746 static boolean first_access = TRUE;
1752 if (!initErrorFile())
1755 first_access = FALSE;
1758 filename = getPath2(getUserDataDir(), ERROR_FILENAME);
1759 error_file = fopen(filename, "a");
1765 void dumpErrorFile()
1770 filename = getPath2(getUserDataDir(), ERROR_FILENAME);
1771 error_file = fopen(filename, "r");
1774 if (error_file != NULL)
1776 while (!feof(error_file))
1777 fputc(fgetc(error_file), stderr);