cleanup of BD style game elements in level editor
[rocksndiamonds.git] / src / libgame / setup.c
1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
5 //                  Holger Schemel
6 //                  info@artsoft.org
7 //                  https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
9 // setup.c
10 // ============================================================================
11
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <dirent.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <errno.h>
18
19 #include "platform.h"
20
21 #include "setup.h"
22 #include "sound.h"
23 #include "joystick.h"
24 #include "text.h"
25 #include "misc.h"
26 #include "hash.h"
27 #include "zip/miniunz.h"
28
29
30 #define ENABLE_UNUSED_CODE      FALSE   // for currently unused functions
31 #define DEBUG_NO_CONFIG_FILE    FALSE   // for extra-verbose debug output
32
33 #define NUM_LEVELCLASS_DESC     8
34
35 static char *levelclass_desc[NUM_LEVELCLASS_DESC] =
36 {
37   "Tutorial Levels",
38   "Classic Originals",
39   "Contributions",
40   "Private Levels",
41   "Boulderdash",
42   "Emerald Mine",
43   "Supaplex",
44   "DX Boulderdash"
45 };
46
47 #define TOKEN_VALUE_POSITION_SHORT              32
48 #define TOKEN_VALUE_POSITION_DEFAULT            40
49 #define TOKEN_COMMENT_POSITION_DEFAULT          60
50
51 #define TREE_NODE_TYPE_DEFAULT                  0
52 #define TREE_NODE_TYPE_PARENT                   1
53 #define TREE_NODE_TYPE_GROUP                    2
54 #define TREE_NODE_TYPE_COPY                     3
55
56 #define TREE_NODE_TYPE(ti)      (ti->node_group  ? TREE_NODE_TYPE_GROUP  : \
57                                  ti->parent_link ? TREE_NODE_TYPE_PARENT : \
58                                  ti->is_copy     ? TREE_NODE_TYPE_COPY   : \
59                                  TREE_NODE_TYPE_DEFAULT)
60
61
62 static void setTreeInfoToDefaults(TreeInfo *, int);
63 static TreeInfo *getTreeInfoCopy(TreeInfo *ti);
64 static int compareTreeInfoEntries(const void *, const void *);
65
66 static int token_value_position   = TOKEN_VALUE_POSITION_DEFAULT;
67 static int token_comment_position = TOKEN_COMMENT_POSITION_DEFAULT;
68
69 static SetupFileHash *artworkinfo_cache_old = NULL;
70 static SetupFileHash *artworkinfo_cache_new = NULL;
71 static SetupFileHash *optional_tokens_hash = NULL;
72 static SetupFileHash *missing_file_hash = NULL;
73 static boolean use_artworkinfo_cache = TRUE;
74 static boolean update_artworkinfo_cache = FALSE;
75
76
77 // ----------------------------------------------------------------------------
78 // file functions
79 // ----------------------------------------------------------------------------
80
81 static void WarnUsingFallback(char *filename)
82 {
83   if (getHashEntry(missing_file_hash, filename) == NULL)
84   {
85     setHashEntry(missing_file_hash, filename, "");
86
87     Debug("setup", "cannot find artwork file '%s' (using fallback)", filename);
88   }
89 }
90
91 static char *getLevelClassDescription(TreeInfo *ti)
92 {
93   int position = ti->sort_priority / 100;
94
95   if (position >= 0 && position < NUM_LEVELCLASS_DESC)
96     return levelclass_desc[position];
97   else
98     return "Unknown Level Class";
99 }
100
101 static char *getCacheDir(void)
102 {
103   static char *cache_dir = NULL;
104
105   if (cache_dir == NULL)
106     cache_dir = getPath2(getMainUserGameDataDir(), CACHE_DIRECTORY);
107
108   return cache_dir;
109 }
110
111 static char *getScoreDir(char *level_subdir)
112 {
113   static char *score_dir = NULL;
114   static char *score_level_dir = NULL;
115   char *score_subdir = SCORES_DIRECTORY;
116
117   if (score_dir == NULL)
118     score_dir = getPath2(getMainUserGameDataDir(), score_subdir);
119
120   if (level_subdir != NULL)
121   {
122     checked_free(score_level_dir);
123
124     score_level_dir = getPath2(score_dir, level_subdir);
125
126     return score_level_dir;
127   }
128
129   return score_dir;
130 }
131
132 static char *getScoreCacheDir(char *level_subdir)
133 {
134   static char *score_dir = NULL;
135   static char *score_level_dir = NULL;
136   char *score_subdir = SCORES_DIRECTORY;
137
138   if (score_dir == NULL)
139     score_dir = getPath2(getCacheDir(), score_subdir);
140
141   if (level_subdir != NULL)
142   {
143     checked_free(score_level_dir);
144
145     score_level_dir = getPath2(score_dir, level_subdir);
146
147     return score_level_dir;
148   }
149
150   return score_dir;
151 }
152
153 static char *getScoreTapeDir(char *level_subdir, int nr)
154 {
155   static char *score_tape_dir = NULL;
156   char tape_subdir[MAX_FILENAME_LEN];
157
158   checked_free(score_tape_dir);
159
160   sprintf(tape_subdir, "%03d", nr);
161   score_tape_dir = getPath2(getScoreDir(level_subdir), tape_subdir);
162
163   return score_tape_dir;
164 }
165
166 static char *getScoreCacheTapeDir(char *level_subdir, int nr)
167 {
168   static char *score_cache_tape_dir = NULL;
169   char tape_subdir[MAX_FILENAME_LEN];
170
171   checked_free(score_cache_tape_dir);
172
173   sprintf(tape_subdir, "%03d", nr);
174   score_cache_tape_dir = getPath2(getScoreCacheDir(level_subdir), tape_subdir);
175
176   return score_cache_tape_dir;
177 }
178
179 static char *getUserSubdir(int nr)
180 {
181   static char user_subdir[16] = { 0 };
182
183   sprintf(user_subdir, "%03d", nr);
184
185   return user_subdir;
186 }
187
188 static char *getUserDir(int nr)
189 {
190   static char *user_dir = NULL;
191   char *main_data_dir = getMainUserGameDataDir();
192   char *users_subdir = USERS_DIRECTORY;
193   char *user_subdir = getUserSubdir(nr);
194
195   checked_free(user_dir);
196
197   if (nr != -1)
198     user_dir = getPath3(main_data_dir, users_subdir, user_subdir);
199   else
200     user_dir = getPath2(main_data_dir, users_subdir);
201
202   return user_dir;
203 }
204
205 static char *getLevelSetupDir(char *level_subdir)
206 {
207   static char *levelsetup_dir = NULL;
208   char *data_dir = getUserGameDataDir();
209   char *levelsetup_subdir = LEVELSETUP_DIRECTORY;
210
211   checked_free(levelsetup_dir);
212
213   if (level_subdir != NULL)
214     levelsetup_dir = getPath3(data_dir, levelsetup_subdir, level_subdir);
215   else
216     levelsetup_dir = getPath2(data_dir, levelsetup_subdir);
217
218   return levelsetup_dir;
219 }
220
221 static char *getNetworkDir(void)
222 {
223   static char *network_dir = NULL;
224
225   if (network_dir == NULL)
226     network_dir = getPath2(getMainUserGameDataDir(), NETWORK_DIRECTORY);
227
228   return network_dir;
229 }
230
231 char *getLevelDirFromTreeInfo(TreeInfo *node)
232 {
233   static char *level_dir = NULL;
234
235   if (node == NULL)
236     return options.level_directory;
237
238   checked_free(level_dir);
239
240   level_dir = getPath2((node->in_user_dir ? getUserLevelDir(NULL) :
241                         options.level_directory), node->fullpath);
242
243   return level_dir;
244 }
245
246 char *getUserLevelDir(char *level_subdir)
247 {
248   static char *userlevel_dir = NULL;
249   char *data_dir = getMainUserGameDataDir();
250   char *userlevel_subdir = LEVELS_DIRECTORY;
251
252   checked_free(userlevel_dir);
253
254   if (level_subdir != NULL)
255     userlevel_dir = getPath3(data_dir, userlevel_subdir, level_subdir);
256   else
257     userlevel_dir = getPath2(data_dir, userlevel_subdir);
258
259   return userlevel_dir;
260 }
261
262 char *getNetworkLevelDir(char *level_subdir)
263 {
264   static char *network_level_dir = NULL;
265   char *data_dir = getNetworkDir();
266   char *networklevel_subdir = LEVELS_DIRECTORY;
267
268   checked_free(network_level_dir);
269
270   if (level_subdir != NULL)
271     network_level_dir = getPath3(data_dir, networklevel_subdir, level_subdir);
272   else
273     network_level_dir = getPath2(data_dir, networklevel_subdir);
274
275   return network_level_dir;
276 }
277
278 char *getCurrentLevelDir(void)
279 {
280   return getLevelDirFromTreeInfo(leveldir_current);
281 }
282
283 char *getNewUserLevelSubdir(void)
284 {
285   static char *new_level_subdir = NULL;
286   char *subdir_prefix = getLoginName();
287   char subdir_suffix[10];
288   int max_suffix_number = 1000;
289   int i = 0;
290
291   while (++i < max_suffix_number)
292   {
293     sprintf(subdir_suffix, "_%d", i);
294
295     checked_free(new_level_subdir);
296     new_level_subdir = getStringCat2(subdir_prefix, subdir_suffix);
297
298     if (!directoryExists(getUserLevelDir(new_level_subdir)))
299       break;
300   }
301
302   return new_level_subdir;
303 }
304
305 char *getTapeDir(char *level_subdir)
306 {
307   static char *tape_dir = NULL;
308   char *data_dir = getUserGameDataDir();
309   char *tape_subdir = TAPES_DIRECTORY;
310
311   checked_free(tape_dir);
312
313   if (level_subdir != NULL)
314     tape_dir = getPath3(data_dir, tape_subdir, level_subdir);
315   else
316     tape_dir = getPath2(data_dir, tape_subdir);
317
318   return tape_dir;
319 }
320
321 static char *getSolutionTapeDir(void)
322 {
323   static char *tape_dir = NULL;
324   char *data_dir = getCurrentLevelDir();
325   char *tape_subdir = TAPES_DIRECTORY;
326
327   checked_free(tape_dir);
328
329   tape_dir = getPath2(data_dir, tape_subdir);
330
331   return tape_dir;
332 }
333
334 static char *getDefaultGraphicsDir(char *graphics_subdir)
335 {
336   static char *graphics_dir = NULL;
337
338   if (graphics_subdir == NULL)
339     return options.graphics_directory;
340
341   checked_free(graphics_dir);
342
343   graphics_dir = getPath2(options.graphics_directory, graphics_subdir);
344
345   return graphics_dir;
346 }
347
348 static char *getDefaultSoundsDir(char *sounds_subdir)
349 {
350   static char *sounds_dir = NULL;
351
352   if (sounds_subdir == NULL)
353     return options.sounds_directory;
354
355   checked_free(sounds_dir);
356
357   sounds_dir = getPath2(options.sounds_directory, sounds_subdir);
358
359   return sounds_dir;
360 }
361
362 static char *getDefaultMusicDir(char *music_subdir)
363 {
364   static char *music_dir = NULL;
365
366   if (music_subdir == NULL)
367     return options.music_directory;
368
369   checked_free(music_dir);
370
371   music_dir = getPath2(options.music_directory, music_subdir);
372
373   return music_dir;
374 }
375
376 static char *getClassicArtworkSet(int type)
377 {
378   return (type == TREE_TYPE_GRAPHICS_DIR ? GFX_CLASSIC_SUBDIR :
379           type == TREE_TYPE_SOUNDS_DIR   ? SND_CLASSIC_SUBDIR :
380           type == TREE_TYPE_MUSIC_DIR    ? MUS_CLASSIC_SUBDIR : "");
381 }
382
383 static char *getClassicArtworkDir(int type)
384 {
385   return (type == TREE_TYPE_GRAPHICS_DIR ?
386           getDefaultGraphicsDir(GFX_CLASSIC_SUBDIR) :
387           type == TREE_TYPE_SOUNDS_DIR ?
388           getDefaultSoundsDir(SND_CLASSIC_SUBDIR) :
389           type == TREE_TYPE_MUSIC_DIR ?
390           getDefaultMusicDir(MUS_CLASSIC_SUBDIR) : "");
391 }
392
393 char *getUserGraphicsDir(void)
394 {
395   static char *usergraphics_dir = NULL;
396
397   if (usergraphics_dir == NULL)
398     usergraphics_dir = getPath2(getMainUserGameDataDir(), GRAPHICS_DIRECTORY);
399
400   return usergraphics_dir;
401 }
402
403 char *getUserSoundsDir(void)
404 {
405   static char *usersounds_dir = NULL;
406
407   if (usersounds_dir == NULL)
408     usersounds_dir = getPath2(getMainUserGameDataDir(), SOUNDS_DIRECTORY);
409
410   return usersounds_dir;
411 }
412
413 char *getUserMusicDir(void)
414 {
415   static char *usermusic_dir = NULL;
416
417   if (usermusic_dir == NULL)
418     usermusic_dir = getPath2(getMainUserGameDataDir(), MUSIC_DIRECTORY);
419
420   return usermusic_dir;
421 }
422
423 static char *getSetupArtworkDir(TreeInfo *ti)
424 {
425   static char *artwork_dir = NULL;
426
427   if (ti == NULL)
428     return NULL;
429
430   checked_free(artwork_dir);
431
432   artwork_dir = getPath2(ti->basepath, ti->fullpath);
433
434   return artwork_dir;
435 }
436
437 char *setLevelArtworkDir(TreeInfo *ti)
438 {
439   char **artwork_path_ptr, **artwork_set_ptr;
440   TreeInfo *level_artwork;
441
442   if (ti == NULL || leveldir_current == NULL)
443     return NULL;
444
445   artwork_path_ptr = LEVELDIR_ARTWORK_PATH_PTR(leveldir_current, ti->type);
446   artwork_set_ptr  = LEVELDIR_ARTWORK_SET_PTR( leveldir_current, ti->type);
447
448   checked_free(*artwork_path_ptr);
449
450   if ((level_artwork = getTreeInfoFromIdentifier(ti, *artwork_set_ptr)))
451   {
452     *artwork_path_ptr = getStringCopy(getSetupArtworkDir(level_artwork));
453   }
454   else
455   {
456     /*
457       No (or non-existing) artwork configured in "levelinfo.conf". This would
458       normally result in using the artwork configured in the setup menu. But
459       if an artwork subdirectory exists (which might contain custom artwork
460       or an artwork configuration file), this level artwork must be treated
461       as relative to the default "classic" artwork, not to the artwork that
462       is currently configured in the setup menu.
463
464       Update: For "special" versions of R'n'D (like "R'n'D jue"), do not use
465       the "default" artwork (which would be "jue0" for "R'n'D jue"), but use
466       the real "classic" artwork from the original R'n'D (like "gfx_classic").
467     */
468
469     char *dir = getPath2(getCurrentLevelDir(), ARTWORK_DIRECTORY(ti->type));
470
471     checked_free(*artwork_set_ptr);
472
473     if (directoryExists(dir))
474     {
475       *artwork_path_ptr = getStringCopy(getClassicArtworkDir(ti->type));
476       *artwork_set_ptr = getStringCopy(getClassicArtworkSet(ti->type));
477     }
478     else
479     {
480       *artwork_path_ptr = getStringCopy(UNDEFINED_FILENAME);
481       *artwork_set_ptr = NULL;
482     }
483
484     free(dir);
485   }
486
487   return *artwork_set_ptr;
488 }
489
490 static char *getLevelArtworkSet(int type)
491 {
492   if (leveldir_current == NULL)
493     return NULL;
494
495   return LEVELDIR_ARTWORK_SET(leveldir_current, type);
496 }
497
498 static char *getLevelArtworkDir(int type)
499 {
500   if (leveldir_current == NULL)
501     return UNDEFINED_FILENAME;
502
503   return LEVELDIR_ARTWORK_PATH(leveldir_current, type);
504 }
505
506 char *getProgramMainDataPath(char *command_filename, char *base_path)
507 {
508   // check if the program's main data base directory is configured
509   if (!strEqual(base_path, "."))
510     return getStringCopy(base_path);
511
512   /* if the program is configured to start from current directory (default),
513      determine program package directory from program binary (some versions
514      of KDE/Konqueror and Mac OS X (especially "Mavericks") apparently do not
515      set the current working directory to the program package directory) */
516   char *main_data_path = getBasePath(command_filename);
517
518 #if defined(PLATFORM_MAC)
519   if (strSuffix(main_data_path, MAC_APP_BINARY_SUBDIR))
520   {
521     char *main_data_path_old = main_data_path;
522
523     // cut relative path to Mac OS X application binary directory from path
524     main_data_path[strlen(main_data_path) -
525                    strlen(MAC_APP_BINARY_SUBDIR)] = '\0';
526
527     // cut trailing path separator from path (but not if path is root directory)
528     if (strSuffix(main_data_path, "/") && !strEqual(main_data_path, "/"))
529       main_data_path[strlen(main_data_path) - 1] = '\0';
530
531     // replace empty path with current directory
532     if (strEqual(main_data_path, ""))
533       main_data_path = ".";
534
535     // add relative path to Mac OS X application resources directory to path
536     main_data_path = getPath2(main_data_path, MAC_APP_FILES_SUBDIR);
537
538     free(main_data_path_old);
539   }
540 #endif
541
542   return main_data_path;
543 }
544
545 char *getProgramConfigFilename(char *command_filename)
546 {
547   static char *config_filename_1 = NULL;
548   static char *config_filename_2 = NULL;
549   static char *config_filename_3 = NULL;
550   static boolean initialized = FALSE;
551
552   if (!initialized)
553   {
554     char *command_filename_1 = getStringCopy(command_filename);
555
556     // strip trailing executable suffix from command filename
557     if (strSuffix(command_filename_1, ".exe"))
558       command_filename_1[strlen(command_filename_1) - 4] = '\0';
559
560     char *base_path = getProgramMainDataPath(command_filename, BASE_PATH);
561     char *conf_directory = getPath2(base_path, CONF_DIRECTORY);
562
563     char *command_basepath = getBasePath(command_filename);
564     char *command_basename = getBaseNameNoSuffix(command_filename);
565     char *command_filename_2 = getPath2(command_basepath, command_basename);
566
567     config_filename_1 = getStringCat2(command_filename_1, ".conf");
568     config_filename_2 = getStringCat2(command_filename_2, ".conf");
569     config_filename_3 = getPath2(conf_directory, SETUP_FILENAME);
570
571     checked_free(base_path);
572     checked_free(conf_directory);
573
574     checked_free(command_basepath);
575     checked_free(command_basename);
576
577     checked_free(command_filename_1);
578     checked_free(command_filename_2);
579
580     initialized = TRUE;
581   }
582
583   // 1st try: look for config file that exactly matches the binary filename
584   if (fileExists(config_filename_1))
585     return config_filename_1;
586
587   // 2nd try: look for config file that matches binary filename without suffix
588   if (fileExists(config_filename_2))
589     return config_filename_2;
590
591   // 3rd try: return setup config filename in global program config directory
592   return config_filename_3;
593 }
594
595 static char *getPlatformConfigFilename(char *config_filename)
596 {
597   static char *platform_config_filename = NULL;
598   static boolean initialized = FALSE;
599
600   if (!initialized)
601   {
602     char *config_basepath = getBasePath(config_filename);
603     char *config_basename = getBaseNameNoSuffix(config_filename);
604     char *config_filename_prefix = getPath2(config_basepath, config_basename);
605     char *platform_string_lower = getStringToLower(PLATFORM_STRING);
606     char *platform_suffix = getStringCat2("-", platform_string_lower);
607
608     platform_config_filename = getStringCat3(config_filename_prefix,
609                                              platform_suffix, ".conf");
610
611     checked_free(config_basepath);
612     checked_free(config_basename);
613     checked_free(config_filename_prefix);
614     checked_free(platform_string_lower);
615     checked_free(platform_suffix);
616
617     initialized = TRUE;
618   }
619
620   return platform_config_filename;
621 }
622
623 char *getTapeFilename(int nr)
624 {
625   static char *filename = NULL;
626   char basename[MAX_FILENAME_LEN];
627
628   checked_free(filename);
629
630   sprintf(basename, "%03d.%s", nr, TAPEFILE_EXTENSION);
631   filename = getPath2(getTapeDir(leveldir_current->subdir), basename);
632
633   return filename;
634 }
635
636 char *getTemporaryTapeFilename(void)
637 {
638   static char *filename = NULL;
639   char basename[MAX_FILENAME_LEN];
640
641   checked_free(filename);
642
643   sprintf(basename, "tmp.%s", TAPEFILE_EXTENSION);
644   filename = getPath2(getTapeDir(NULL), basename);
645
646   return filename;
647 }
648
649 char *getDefaultSolutionTapeFilename(int nr)
650 {
651   static char *filename = NULL;
652   char basename[MAX_FILENAME_LEN];
653
654   checked_free(filename);
655
656   sprintf(basename, "%03d.%s", nr, TAPEFILE_EXTENSION);
657   filename = getPath2(getSolutionTapeDir(), basename);
658
659   return filename;
660 }
661
662 char *getSokobanSolutionTapeFilename(int nr)
663 {
664   static char *filename = NULL;
665   char basename[MAX_FILENAME_LEN];
666
667   checked_free(filename);
668
669   sprintf(basename, "%03d.sln", nr);
670   filename = getPath2(getSolutionTapeDir(), basename);
671
672   return filename;
673 }
674
675 char *getSolutionTapeFilename(int nr)
676 {
677   char *filename = getDefaultSolutionTapeFilename(nr);
678
679   if (!fileExists(filename))
680   {
681     char *filename2 = getSokobanSolutionTapeFilename(nr);
682
683     if (fileExists(filename2))
684       return filename2;
685   }
686
687   return filename;
688 }
689
690 char *getScoreFilename(int nr)
691 {
692   static char *filename = NULL;
693   char basename[MAX_FILENAME_LEN];
694
695   checked_free(filename);
696
697   sprintf(basename, "%03d.%s", nr, SCOREFILE_EXTENSION);
698
699   // used instead of "leveldir_current->subdir" (for network games)
700   filename = getPath2(getScoreDir(levelset.identifier), basename);
701
702   return filename;
703 }
704
705 char *getScoreCacheFilename(int nr)
706 {
707   static char *filename = NULL;
708   char basename[MAX_FILENAME_LEN];
709
710   checked_free(filename);
711
712   sprintf(basename, "%03d.%s", nr, SCOREFILE_EXTENSION);
713
714   // used instead of "leveldir_current->subdir" (for network games)
715   filename = getPath2(getScoreCacheDir(levelset.identifier), basename);
716
717   return filename;
718 }
719
720 char *getScoreTapeBasename(char *name)
721 {
722   static char basename[MAX_FILENAME_LEN];
723   char basename_raw[MAX_FILENAME_LEN];
724   char timestamp[20];
725
726   sprintf(timestamp, "%s", getCurrentTimestamp());
727   sprintf(basename_raw, "%s-%s", timestamp, name);
728   sprintf(basename, "%s-%08x", timestamp, get_hash_from_string(basename_raw));
729
730   return basename;
731 }
732
733 char *getScoreTapeFilename(char *basename_no_ext, int nr)
734 {
735   static char *filename = NULL;
736   char basename[MAX_FILENAME_LEN];
737
738   checked_free(filename);
739
740   sprintf(basename, "%s.%s", basename_no_ext, TAPEFILE_EXTENSION);
741
742   // used instead of "leveldir_current->subdir" (for network games)
743   filename = getPath2(getScoreTapeDir(levelset.identifier, nr), basename);
744
745   return filename;
746 }
747
748 char *getScoreCacheTapeFilename(char *basename_no_ext, int nr)
749 {
750   static char *filename = NULL;
751   char basename[MAX_FILENAME_LEN];
752
753   checked_free(filename);
754
755   sprintf(basename, "%s.%s", basename_no_ext, TAPEFILE_EXTENSION);
756
757   // used instead of "leveldir_current->subdir" (for network games)
758   filename = getPath2(getScoreCacheTapeDir(levelset.identifier, nr), basename);
759
760   return filename;
761 }
762
763 char *getSetupFilename(void)
764 {
765   static char *filename = NULL;
766
767   checked_free(filename);
768
769   filename = getPath2(getSetupDir(), SETUP_FILENAME);
770
771   return filename;
772 }
773
774 char *getDefaultSetupFilename(void)
775 {
776   return program.config_filename;
777 }
778
779 char *getPlatformSetupFilename(void)
780 {
781   return getPlatformConfigFilename(program.config_filename);
782 }
783
784 char *getEditorSetupFilename(void)
785 {
786   static char *filename = NULL;
787
788   checked_free(filename);
789   filename = getPath2(getCurrentLevelDir(), EDITORSETUP_FILENAME);
790
791   if (fileExists(filename))
792     return filename;
793
794   checked_free(filename);
795   filename = getPath2(getSetupDir(), EDITORSETUP_FILENAME);
796
797   return filename;
798 }
799
800 char *getHelpAnimFilename(void)
801 {
802   static char *filename = NULL;
803
804   checked_free(filename);
805
806   filename = getPath2(getCurrentLevelDir(), HELPANIM_FILENAME);
807
808   return filename;
809 }
810
811 char *getHelpTextFilename(void)
812 {
813   static char *filename = NULL;
814
815   checked_free(filename);
816
817   filename = getPath2(getCurrentLevelDir(), HELPTEXT_FILENAME);
818
819   return filename;
820 }
821
822 static char *getLevelSetInfoBasename(int nr)
823 {
824   static char basename[32];
825
826   sprintf(basename, "levelset_%d.txt", nr + 1);
827
828   return basename;
829 }
830
831 char *getLevelSetInfoFilename(int nr)
832 {
833   char *basename = getLevelSetInfoBasename(nr);
834   static char *info_subdir = NULL;
835   static char *filename = NULL;
836
837   if (info_subdir == NULL)
838     info_subdir = getPath2(DOCS_DIRECTORY, LEVELSET_INFO_DIRECTORY);
839
840   checked_free(filename);
841
842   // look for level set info file the current level set directory
843   filename = getPath3(getCurrentLevelDir(), info_subdir, basename);
844   if (fileExists(filename))
845     return filename;
846
847   if (nr > 0)
848     return NULL;
849
850   char *basenames[] =
851   {
852     "README",
853     "README.TXT",
854     "README.txt",
855     "Readme",
856     "Readme.txt",
857     "readme",
858     "readme.txt",
859
860     NULL
861   };
862   int i;
863
864   for (i = 0; basenames[i] != NULL; i++)
865   {
866     checked_free(filename);
867     filename = getPath2(getCurrentLevelDir(), basenames[i]);
868
869     if (fileExists(filename))
870       return filename;
871   }
872
873   return NULL;
874 }
875
876 static char *getLevelSetTitleMessageBasename(int nr, boolean initial)
877 {
878   static char basename[32];
879
880   sprintf(basename, "%s_%d.txt",
881           (initial ? "titlemessage_initial" : "titlemessage"), nr + 1);
882
883   return basename;
884 }
885
886 char *getLevelSetTitleMessageFilename(int nr, boolean initial)
887 {
888   static char *filename = NULL;
889   char *basename;
890   boolean skip_setup_artwork = FALSE;
891
892   checked_free(filename);
893
894   basename = getLevelSetTitleMessageBasename(nr, initial);
895
896   if (!gfx.override_level_graphics)
897   {
898     // 1st try: look for special artwork in current level series directory
899     filename = getPath3(getCurrentLevelDir(), GRAPHICS_DIRECTORY, basename);
900     if (fileExists(filename))
901       return filename;
902
903     free(filename);
904
905     // 2nd try: look for message file in current level set directory
906     filename = getPath2(getCurrentLevelDir(), basename);
907     if (fileExists(filename))
908       return filename;
909
910     free(filename);
911
912     // check if there is special artwork configured in level series config
913     if (getLevelArtworkSet(ARTWORK_TYPE_GRAPHICS) != NULL)
914     {
915       // 3rd try: look for special artwork configured in level series config
916       filename = getPath2(getLevelArtworkDir(ARTWORK_TYPE_GRAPHICS), basename);
917       if (fileExists(filename))
918         return filename;
919
920       free(filename);
921
922       // take missing artwork configured in level set config from default
923       skip_setup_artwork = TRUE;
924     }
925   }
926
927   if (!skip_setup_artwork)
928   {
929     // 4th try: look for special artwork in configured artwork directory
930     filename = getPath2(getSetupArtworkDir(artwork.gfx_current), basename);
931     if (fileExists(filename))
932       return filename;
933
934     free(filename);
935   }
936
937   // 5th try: look for default artwork in new default artwork directory
938   filename = getPath2(getDefaultGraphicsDir(GFX_DEFAULT_SUBDIR), basename);
939   if (fileExists(filename))
940     return filename;
941
942   free(filename);
943
944   // 6th try: look for default artwork in old default artwork directory
945   filename = getPath2(options.graphics_directory, basename);
946   if (fileExists(filename))
947     return filename;
948
949   return NULL;          // cannot find specified artwork file anywhere
950 }
951
952 static char *getCreditsBasename(int nr)
953 {
954   static char basename[32];
955
956   sprintf(basename, "credits_%d.txt", nr + 1);
957
958   return basename;
959 }
960
961 char *getCreditsFilename(int nr, boolean global)
962 {
963   char *basename = getCreditsBasename(nr);
964   char *basepath = NULL;
965   static char *credits_subdir = NULL;
966   static char *filename = NULL;
967
968   if (credits_subdir == NULL)
969     credits_subdir = getPath2(DOCS_DIRECTORY, CREDITS_DIRECTORY);
970
971   checked_free(filename);
972
973   // look for credits file in the game's base or current level set directory
974   basepath = (global ? options.base_directory : getCurrentLevelDir());
975
976   filename = getPath3(basepath, credits_subdir, basename);
977   if (fileExists(filename))
978     return filename;
979
980   return NULL;          // cannot find credits file
981 }
982
983 static char *getProgramInfoBasename(int nr)
984 {
985   static char basename[32];
986
987   sprintf(basename, "program_%d.txt", nr + 1);
988
989   return basename;
990 }
991
992 char *getProgramInfoFilename(int nr)
993 {
994   char *basename = getProgramInfoBasename(nr);
995   static char *info_subdir = NULL;
996   static char *filename = NULL;
997
998   if (info_subdir == NULL)
999     info_subdir = getPath2(DOCS_DIRECTORY, PROGRAM_INFO_DIRECTORY);
1000
1001   checked_free(filename);
1002
1003   // look for program info file in the game's base directory
1004   filename = getPath3(options.base_directory, info_subdir, basename);
1005   if (fileExists(filename))
1006     return filename;
1007
1008   return NULL;          // cannot find program info file
1009 }
1010
1011 static char *getCorrectedArtworkBasename(char *basename)
1012 {
1013   return basename;
1014 }
1015
1016 char *getCustomImageFilename(char *basename)
1017 {
1018   static char *filename = NULL;
1019   boolean skip_setup_artwork = FALSE;
1020
1021   checked_free(filename);
1022
1023   basename = getCorrectedArtworkBasename(basename);
1024
1025   if (!gfx.override_level_graphics)
1026   {
1027     // 1st try: look for special artwork in current level series directory
1028     filename = getImg3(getCurrentLevelDir(), GRAPHICS_DIRECTORY, basename);
1029     if (fileExists(filename))
1030       return filename;
1031
1032     free(filename);
1033
1034     // check if there is special artwork configured in level series config
1035     if (getLevelArtworkSet(ARTWORK_TYPE_GRAPHICS) != NULL)
1036     {
1037       // 2nd try: look for special artwork configured in level series config
1038       filename = getImg2(getLevelArtworkDir(ARTWORK_TYPE_GRAPHICS), basename);
1039       if (fileExists(filename))
1040         return filename;
1041
1042       free(filename);
1043
1044       // take missing artwork configured in level set config from default
1045       skip_setup_artwork = TRUE;
1046     }
1047   }
1048
1049   if (!skip_setup_artwork)
1050   {
1051     // 3rd try: look for special artwork in configured artwork directory
1052     filename = getImg2(getSetupArtworkDir(artwork.gfx_current), basename);
1053     if (fileExists(filename))
1054       return filename;
1055
1056     free(filename);
1057   }
1058
1059   // 4th try: look for default artwork in new default artwork directory
1060   filename = getImg2(getDefaultGraphicsDir(GFX_DEFAULT_SUBDIR), basename);
1061   if (fileExists(filename))
1062     return filename;
1063
1064   free(filename);
1065
1066   // 5th try: look for default artwork in old default artwork directory
1067   filename = getImg2(options.graphics_directory, basename);
1068   if (fileExists(filename))
1069     return filename;
1070
1071   if (!strEqual(GFX_FALLBACK_FILENAME, UNDEFINED_FILENAME))
1072   {
1073     free(filename);
1074
1075     WarnUsingFallback(basename);
1076
1077     // 6th try: look for fallback artwork in old default artwork directory
1078     // (needed to prevent errors when trying to access unused artwork files)
1079     filename = getImg2(options.graphics_directory, GFX_FALLBACK_FILENAME);
1080     if (fileExists(filename))
1081       return filename;
1082   }
1083
1084   return NULL;          // cannot find specified artwork file anywhere
1085 }
1086
1087 char *getCustomSoundFilename(char *basename)
1088 {
1089   static char *filename = NULL;
1090   boolean skip_setup_artwork = FALSE;
1091
1092   checked_free(filename);
1093
1094   basename = getCorrectedArtworkBasename(basename);
1095
1096   if (!gfx.override_level_sounds)
1097   {
1098     // 1st try: look for special artwork in current level series directory
1099     filename = getPath3(getCurrentLevelDir(), SOUNDS_DIRECTORY, basename);
1100     if (fileExists(filename))
1101       return filename;
1102
1103     free(filename);
1104
1105     // check if there is special artwork configured in level series config
1106     if (getLevelArtworkSet(ARTWORK_TYPE_SOUNDS) != NULL)
1107     {
1108       // 2nd try: look for special artwork configured in level series config
1109       filename = getPath2(getLevelArtworkDir(TREE_TYPE_SOUNDS_DIR), basename);
1110       if (fileExists(filename))
1111         return filename;
1112
1113       free(filename);
1114
1115       // take missing artwork configured in level set config from default
1116       skip_setup_artwork = TRUE;
1117     }
1118   }
1119
1120   if (!skip_setup_artwork)
1121   {
1122     // 3rd try: look for special artwork in configured artwork directory
1123     filename = getPath2(getSetupArtworkDir(artwork.snd_current), basename);
1124     if (fileExists(filename))
1125       return filename;
1126
1127     free(filename);
1128   }
1129
1130   // 4th try: look for default artwork in new default artwork directory
1131   filename = getPath2(getDefaultSoundsDir(SND_DEFAULT_SUBDIR), basename);
1132   if (fileExists(filename))
1133     return filename;
1134
1135   free(filename);
1136
1137   // 5th try: look for default artwork in old default artwork directory
1138   filename = getPath2(options.sounds_directory, basename);
1139   if (fileExists(filename))
1140     return filename;
1141
1142   if (!strEqual(SND_FALLBACK_FILENAME, UNDEFINED_FILENAME))
1143   {
1144     free(filename);
1145
1146     WarnUsingFallback(basename);
1147
1148     // 6th try: look for fallback artwork in old default artwork directory
1149     // (needed to prevent errors when trying to access unused artwork files)
1150     filename = getPath2(options.sounds_directory, SND_FALLBACK_FILENAME);
1151     if (fileExists(filename))
1152       return filename;
1153   }
1154
1155   return NULL;          // cannot find specified artwork file anywhere
1156 }
1157
1158 char *getCustomMusicFilename(char *basename)
1159 {
1160   static char *filename = NULL;
1161   boolean skip_setup_artwork = FALSE;
1162
1163   checked_free(filename);
1164
1165   basename = getCorrectedArtworkBasename(basename);
1166
1167   if (!gfx.override_level_music)
1168   {
1169     // 1st try: look for special artwork in current level series directory
1170     filename = getPath3(getCurrentLevelDir(), MUSIC_DIRECTORY, basename);
1171     if (fileExists(filename))
1172       return filename;
1173
1174     free(filename);
1175
1176     // check if there is special artwork configured in level series config
1177     if (getLevelArtworkSet(ARTWORK_TYPE_MUSIC) != NULL)
1178     {
1179       // 2nd try: look for special artwork configured in level series config
1180       filename = getPath2(getLevelArtworkDir(TREE_TYPE_MUSIC_DIR), basename);
1181       if (fileExists(filename))
1182         return filename;
1183
1184       free(filename);
1185
1186       // take missing artwork configured in level set config from default
1187       skip_setup_artwork = TRUE;
1188     }
1189   }
1190
1191   if (!skip_setup_artwork)
1192   {
1193     // 3rd try: look for special artwork in configured artwork directory
1194     filename = getPath2(getSetupArtworkDir(artwork.mus_current), basename);
1195     if (fileExists(filename))
1196       return filename;
1197
1198     free(filename);
1199   }
1200
1201   // 4th try: look for default artwork in new default artwork directory
1202   filename = getPath2(getDefaultMusicDir(MUS_DEFAULT_SUBDIR), basename);
1203   if (fileExists(filename))
1204     return filename;
1205
1206   free(filename);
1207
1208   // 5th try: look for default artwork in old default artwork directory
1209   filename = getPath2(options.music_directory, basename);
1210   if (fileExists(filename))
1211     return filename;
1212
1213   if (!strEqual(MUS_FALLBACK_FILENAME, UNDEFINED_FILENAME))
1214   {
1215     free(filename);
1216
1217     WarnUsingFallback(basename);
1218
1219     // 6th try: look for fallback artwork in old default artwork directory
1220     // (needed to prevent errors when trying to access unused artwork files)
1221     filename = getPath2(options.music_directory, MUS_FALLBACK_FILENAME);
1222     if (fileExists(filename))
1223       return filename;
1224   }
1225
1226   return NULL;          // cannot find specified artwork file anywhere
1227 }
1228
1229 char *getCustomArtworkFilename(char *basename, int type)
1230 {
1231   if (type == ARTWORK_TYPE_GRAPHICS)
1232     return getCustomImageFilename(basename);
1233   else if (type == ARTWORK_TYPE_SOUNDS)
1234     return getCustomSoundFilename(basename);
1235   else if (type == ARTWORK_TYPE_MUSIC)
1236     return getCustomMusicFilename(basename);
1237   else
1238     return UNDEFINED_FILENAME;
1239 }
1240
1241 char *getCustomArtworkConfigFilename(int type)
1242 {
1243   return getCustomArtworkFilename(ARTWORKINFO_FILENAME(type), type);
1244 }
1245
1246 char *getCustomArtworkLevelConfigFilename(int type)
1247 {
1248   static char *filename = NULL;
1249
1250   checked_free(filename);
1251
1252   filename = getPath2(getLevelArtworkDir(type), ARTWORKINFO_FILENAME(type));
1253
1254   return filename;
1255 }
1256
1257 static boolean directoryExists_CheckMusic(char *directory, boolean check_music)
1258 {
1259   if (!directoryExists(directory))
1260     return FALSE;
1261
1262   if (!check_music)
1263     return TRUE;
1264
1265   Directory *dir;
1266   DirectoryEntry *dir_entry;
1267   int num_music = getMusicListSize();
1268   boolean music_found = FALSE;
1269
1270   if ((dir = openDirectory(directory)) == NULL)
1271     return FALSE;
1272
1273   while ((dir_entry = readDirectory(dir)) != NULL)      // loop all entries
1274   {
1275     char *basename = dir_entry->basename;
1276     boolean music_already_used = FALSE;
1277     int i;
1278
1279     // skip all music files that are configured in music config file
1280     for (i = 0; i < num_music; i++)
1281     {
1282       struct FileInfo *music = getMusicListEntry(i);
1283
1284       if (strEqual(basename, music->filename))
1285       {
1286         music_already_used = TRUE;
1287
1288         break;
1289       }
1290     }
1291
1292     if (music_already_used)
1293       continue;
1294
1295     if (FileIsMusic(dir_entry->filename))
1296     {
1297       music_found = TRUE;
1298
1299       break;
1300     }
1301   }
1302
1303   closeDirectory(dir);
1304
1305   return music_found;
1306 }
1307
1308 static char *getCustomMusicDirectoryExt(boolean check_music)
1309 {
1310   static char *directory = NULL;
1311   boolean skip_setup_artwork = FALSE;
1312
1313   checked_free(directory);
1314
1315   if (!gfx.override_level_music)
1316   {
1317     // 1st try: look for special artwork in current level series directory
1318     directory = getPath2(getCurrentLevelDir(), MUSIC_DIRECTORY);
1319     if (directoryExists_CheckMusic(directory, check_music))
1320       return directory;
1321
1322     free(directory);
1323
1324     // check if there is special artwork configured in level series config
1325     if (getLevelArtworkSet(ARTWORK_TYPE_MUSIC) != NULL)
1326     {
1327       // 2nd try: look for special artwork configured in level series config
1328       directory = getStringCopy(getLevelArtworkDir(TREE_TYPE_MUSIC_DIR));
1329
1330       // directory also valid if no unconfigured music found (no game music)
1331       if (directoryExists_CheckMusic(directory, FALSE))
1332         return directory;
1333
1334       free(directory);
1335
1336       // take missing artwork configured in level set config from default
1337       skip_setup_artwork = TRUE;
1338     }
1339   }
1340
1341   if (!skip_setup_artwork)
1342   {
1343     // 3rd try: look for special artwork in configured artwork directory
1344     directory = getStringCopy(getSetupArtworkDir(artwork.mus_current));
1345
1346     // directory also valid if no unconfigured music found (no game music)
1347     if (directoryExists_CheckMusic(directory, FALSE))
1348       return directory;
1349
1350     free(directory);
1351   }
1352
1353   // 4th try: look for default artwork in new default artwork directory
1354   directory = getStringCopy(getDefaultMusicDir(MUS_DEFAULT_SUBDIR));
1355   if (directoryExists_CheckMusic(directory, check_music))
1356     return directory;
1357
1358   free(directory);
1359
1360   // 5th try: look for default artwork in old default artwork directory
1361   directory = getStringCopy(options.music_directory);
1362   if (directoryExists_CheckMusic(directory, check_music))
1363     return directory;
1364
1365   return NULL;          // cannot find specified artwork file anywhere
1366 }
1367
1368 char *getCustomMusicDirectory(void)
1369 {
1370   return getCustomMusicDirectoryExt(FALSE);
1371 }
1372
1373 char *getCustomMusicDirectory_NoConf(void)
1374 {
1375   return getCustomMusicDirectoryExt(TRUE);
1376 }
1377
1378 void MarkTapeDirectoryUploadsAsComplete(char *level_subdir)
1379 {
1380   char *filename = getPath2(getTapeDir(level_subdir), UPLOADED_FILENAME);
1381
1382   touchFile(filename);
1383
1384   checked_free(filename);
1385 }
1386
1387 void MarkTapeDirectoryUploadsAsIncomplete(char *level_subdir)
1388 {
1389   char *filename = getPath2(getTapeDir(level_subdir), UPLOADED_FILENAME);
1390
1391   unlink(filename);
1392
1393   checked_free(filename);
1394 }
1395
1396 boolean CheckTapeDirectoryUploadsComplete(char *level_subdir)
1397 {
1398   char *filename = getPath2(getTapeDir(level_subdir), UPLOADED_FILENAME);
1399   boolean success = fileExists(filename);
1400
1401   checked_free(filename);
1402
1403   return success;
1404 }
1405
1406 void InitMissingFileHash(void)
1407 {
1408   if (missing_file_hash == NULL)
1409     freeSetupFileHash(missing_file_hash);
1410
1411   missing_file_hash = newSetupFileHash();
1412 }
1413
1414 void InitTapeDirectory(char *level_subdir)
1415 {
1416   boolean new_tape_dir = !directoryExists(getTapeDir(level_subdir));
1417
1418   createDirectory(getUserGameDataDir(), "user data");
1419   createDirectory(getTapeDir(NULL), "main tape");
1420   createDirectory(getTapeDir(level_subdir), "level tape");
1421
1422   if (new_tape_dir)
1423     MarkTapeDirectoryUploadsAsComplete(level_subdir);
1424 }
1425
1426 void InitScoreDirectory(char *level_subdir)
1427 {
1428   createDirectory(getMainUserGameDataDir(), "main user data");
1429   createDirectory(getScoreDir(NULL), "main score");
1430   createDirectory(getScoreDir(level_subdir), "level score");
1431 }
1432
1433 void InitScoreCacheDirectory(char *level_subdir)
1434 {
1435   createDirectory(getMainUserGameDataDir(), "main user data");
1436   createDirectory(getCacheDir(), "cache data");
1437   createDirectory(getScoreCacheDir(NULL), "main score");
1438   createDirectory(getScoreCacheDir(level_subdir), "level score");
1439 }
1440
1441 void InitScoreTapeDirectory(char *level_subdir, int nr)
1442 {
1443   InitScoreDirectory(level_subdir);
1444
1445   createDirectory(getScoreTapeDir(level_subdir, nr), "score tape");
1446 }
1447
1448 void InitScoreCacheTapeDirectory(char *level_subdir, int nr)
1449 {
1450   InitScoreCacheDirectory(level_subdir);
1451
1452   createDirectory(getScoreCacheTapeDir(level_subdir, nr), "score tape");
1453 }
1454
1455 static void SaveUserLevelInfo(void);
1456
1457 void InitUserLevelDirectory(char *level_subdir)
1458 {
1459   if (!directoryExists(getUserLevelDir(level_subdir)))
1460   {
1461     createDirectory(getMainUserGameDataDir(), "main user data");
1462     createDirectory(getUserLevelDir(NULL), "main user level");
1463
1464     if (setup.internal.create_user_levelset)
1465     {
1466       createDirectory(getUserLevelDir(level_subdir), "user level");
1467
1468       SaveUserLevelInfo();
1469     }
1470   }
1471 }
1472
1473 void InitNetworkLevelDirectory(char *level_subdir)
1474 {
1475   if (!directoryExists(getNetworkLevelDir(level_subdir)))
1476   {
1477     createDirectory(getMainUserGameDataDir(), "main user data");
1478     createDirectory(getNetworkDir(), "network data");
1479     createDirectory(getNetworkLevelDir(NULL), "main network level");
1480     createDirectory(getNetworkLevelDir(level_subdir), "network level");
1481   }
1482 }
1483
1484 void InitLevelSetupDirectory(char *level_subdir)
1485 {
1486   createDirectory(getUserGameDataDir(), "user data");
1487   createDirectory(getLevelSetupDir(NULL), "main level setup");
1488   createDirectory(getLevelSetupDir(level_subdir), "level setup");
1489 }
1490
1491 static void InitCacheDirectory(void)
1492 {
1493   createDirectory(getMainUserGameDataDir(), "main user data");
1494   createDirectory(getCacheDir(), "cache data");
1495 }
1496
1497
1498 // ----------------------------------------------------------------------------
1499 // some functions to handle lists of level and artwork directories
1500 // ----------------------------------------------------------------------------
1501
1502 TreeInfo *newTreeInfo(void)
1503 {
1504   return checked_calloc(sizeof(TreeInfo));
1505 }
1506
1507 TreeInfo *newTreeInfo_setDefaults(int type)
1508 {
1509   TreeInfo *ti = newTreeInfo();
1510
1511   setTreeInfoToDefaults(ti, type);
1512
1513   return ti;
1514 }
1515
1516 void pushTreeInfo(TreeInfo **node_first, TreeInfo *node_new)
1517 {
1518   node_new->next = *node_first;
1519   *node_first = node_new;
1520 }
1521
1522 void removeTreeInfo(TreeInfo **node_first)
1523 {
1524   TreeInfo *node_old = *node_first;
1525
1526   *node_first = node_old->next;
1527   node_old->next = NULL;
1528
1529   freeTreeInfo(node_old);
1530 }
1531
1532 int numTreeInfo(TreeInfo *node)
1533 {
1534   int num = 0;
1535
1536   while (node)
1537   {
1538     num++;
1539     node = node->next;
1540   }
1541
1542   return num;
1543 }
1544
1545 boolean validLevelSeries(TreeInfo *node)
1546 {
1547   // in a number of cases, tree node is no valid level set
1548   if (node == NULL || node->node_group || node->parent_link || node->is_copy)
1549     return FALSE;
1550
1551   return TRUE;
1552 }
1553
1554 TreeInfo *getValidLevelSeries(TreeInfo *node, TreeInfo *default_node)
1555 {
1556   if (validLevelSeries(node))
1557     return node;
1558   else if (node->is_copy)
1559     return getTreeInfoFromIdentifier(leveldir_first, node->identifier);
1560   else
1561     return getFirstValidTreeInfoEntry(default_node);
1562 }
1563
1564 static TreeInfo *getValidTreeInfoEntryExt(TreeInfo *node, boolean get_next_node)
1565 {
1566   if (node == NULL)
1567     return NULL;
1568
1569   if (node->node_group)         // enter node group (step down into tree)
1570     return getFirstValidTreeInfoEntry(node->node_group);
1571
1572   if (node->parent_link)        // skip first node (back link) of node group
1573     get_next_node = TRUE;
1574
1575   if (!get_next_node)           // get current regular tree node
1576     return node;
1577
1578   // get next regular tree node, or step up until one is found
1579   while (node->next == NULL && node->node_parent != NULL)
1580     node = node->node_parent;
1581
1582   return getFirstValidTreeInfoEntry(node->next);
1583 }
1584
1585 TreeInfo *getFirstValidTreeInfoEntry(TreeInfo *node)
1586 {
1587   return getValidTreeInfoEntryExt(node, FALSE);
1588 }
1589
1590 TreeInfo *getNextValidTreeInfoEntry(TreeInfo *node)
1591 {
1592   return getValidTreeInfoEntryExt(node, TRUE);
1593 }
1594
1595 TreeInfo *getTreeInfoFirstGroupEntry(TreeInfo *node)
1596 {
1597   if (node == NULL)
1598     return NULL;
1599
1600   if (node->node_parent == NULL)                // top level group
1601     return *node->node_top;
1602   else                                          // sub level group
1603     return node->node_parent->node_group;
1604 }
1605
1606 int numTreeInfoInGroup(TreeInfo *node)
1607 {
1608   return numTreeInfo(getTreeInfoFirstGroupEntry(node));
1609 }
1610
1611 int getPosFromTreeInfo(TreeInfo *node)
1612 {
1613   TreeInfo *node_cmp = getTreeInfoFirstGroupEntry(node);
1614   int pos = 0;
1615
1616   while (node_cmp)
1617   {
1618     if (node_cmp == node)
1619       return pos;
1620
1621     pos++;
1622     node_cmp = node_cmp->next;
1623   }
1624
1625   return 0;
1626 }
1627
1628 TreeInfo *getTreeInfoFromPos(TreeInfo *node, int pos)
1629 {
1630   TreeInfo *node_default = node;
1631   int pos_cmp = 0;
1632
1633   while (node)
1634   {
1635     if (pos_cmp == pos)
1636       return node;
1637
1638     pos_cmp++;
1639     node = node->next;
1640   }
1641
1642   return node_default;
1643 }
1644
1645 static TreeInfo *getTreeInfoFromIdentifierExt(TreeInfo *node, char *identifier,
1646                                               int node_type_wanted)
1647 {
1648   if (identifier == NULL)
1649     return NULL;
1650
1651   while (node)
1652   {
1653     if (TREE_NODE_TYPE(node) == node_type_wanted &&
1654         strEqual(identifier, node->identifier))
1655       return node;
1656
1657     if (node->node_group)
1658     {
1659       TreeInfo *node_group = getTreeInfoFromIdentifierExt(node->node_group,
1660                                                           identifier,
1661                                                           node_type_wanted);
1662       if (node_group)
1663         return node_group;
1664     }
1665
1666     node = node->next;
1667   }
1668
1669   return NULL;
1670 }
1671
1672 TreeInfo *getTreeInfoFromIdentifier(TreeInfo *node, char *identifier)
1673 {
1674   return getTreeInfoFromIdentifierExt(node, identifier, TREE_NODE_TYPE_DEFAULT);
1675 }
1676
1677 static TreeInfo *cloneTreeNode(TreeInfo **node_top, TreeInfo *node_parent,
1678                                TreeInfo *node, boolean skip_sets_without_levels)
1679 {
1680   TreeInfo *node_new;
1681
1682   if (node == NULL)
1683     return NULL;
1684
1685   if (!node->parent_link && !node->level_group &&
1686       skip_sets_without_levels && node->levels == 0)
1687     return cloneTreeNode(node_top, node_parent, node->next,
1688                          skip_sets_without_levels);
1689
1690   node_new = getTreeInfoCopy(node);             // copy complete node
1691
1692   node_new->node_top = node_top;                // correct top node link
1693   node_new->node_parent = node_parent;          // correct parent node link
1694
1695   if (node->level_group)
1696     node_new->node_group = cloneTreeNode(node_top, node_new, node->node_group,
1697                                          skip_sets_without_levels);
1698
1699   node_new->next = cloneTreeNode(node_top, node_parent, node->next,
1700                                  skip_sets_without_levels);
1701   
1702   return node_new;
1703 }
1704
1705 static void cloneTree(TreeInfo **ti_new, TreeInfo *ti, boolean skip_empty_sets)
1706 {
1707   TreeInfo *ti_cloned = cloneTreeNode(ti_new, NULL, ti, skip_empty_sets);
1708
1709   *ti_new = ti_cloned;
1710 }
1711
1712 static boolean adjustTreeArtworkForEMC(char **artwork_set_1,
1713                                        char **artwork_set_2,
1714                                        char **artwork_set, boolean prefer_2)
1715 {
1716   // do nothing if neither special artwork set 1 nor 2 are defined
1717   if (!*artwork_set_1 && !*artwork_set_2)
1718     return FALSE;
1719
1720   boolean want_1 = (prefer_2 == FALSE);
1721   boolean want_2 = (prefer_2 == TRUE);
1722   boolean has_only_1 = (!*artwork_set && !*artwork_set_2);
1723   boolean has_only_2 = (!*artwork_set && !*artwork_set_1);
1724   char *artwork_set_new = NULL;
1725
1726   // replace missing special artwork 1 or 2 with (optional) standard artwork
1727
1728   if (!*artwork_set_1)
1729     setString(artwork_set_1, *artwork_set);
1730
1731   if (!*artwork_set_2)
1732     setString(artwork_set_2, *artwork_set);
1733
1734   // set standard artwork to either special artwork 1 or 2, as requested
1735
1736   if (*artwork_set_1 && (want_1 || has_only_1))
1737     artwork_set_new = *artwork_set_1;
1738
1739   if (*artwork_set_2 && (want_2 || has_only_2))
1740     artwork_set_new = *artwork_set_2;
1741
1742   if (artwork_set_new && !strEqual(*artwork_set, artwork_set_new))
1743   {
1744     setString(artwork_set, artwork_set_new);
1745
1746     return TRUE;
1747   }
1748
1749   return FALSE;
1750 }
1751
1752 static boolean adjustTreeGraphicsForEMC(TreeInfo *node)
1753 {
1754   boolean settings_changed = FALSE;
1755
1756   while (node)
1757   {
1758     settings_changed |= adjustTreeArtworkForEMC(&node->graphics_set_ecs,
1759                                                 &node->graphics_set_aga,
1760                                                 &node->graphics_set,
1761                                                 setup.prefer_aga_graphics);
1762     if (node->node_group != NULL)
1763       settings_changed |= adjustTreeGraphicsForEMC(node->node_group);
1764
1765     node = node->next;
1766   }
1767
1768   return settings_changed;
1769 }
1770
1771 static boolean adjustTreeSoundsForEMC(TreeInfo *node)
1772 {
1773   boolean settings_changed = FALSE;
1774
1775   while (node)
1776   {
1777     settings_changed |= adjustTreeArtworkForEMC(&node->sounds_set_default,
1778                                                 &node->sounds_set_lowpass,
1779                                                 &node->sounds_set,
1780                                                 setup.prefer_lowpass_sounds);
1781     if (node->node_group != NULL)
1782       settings_changed |= adjustTreeSoundsForEMC(node->node_group);
1783
1784     node = node->next;
1785   }
1786
1787   return settings_changed;
1788 }
1789
1790 int dumpTreeInfo(TreeInfo *node, int depth)
1791 {
1792   char bullet_list[] = { '-', '*', 'o' };
1793   int num_leaf_nodes = 0;
1794   int i;
1795
1796   if (depth == 0)
1797     Debug("tree", "Dumping TreeInfo:");
1798
1799   while (node)
1800   {
1801     char bullet = bullet_list[depth % ARRAY_SIZE(bullet_list)];
1802
1803     for (i = 0; i < depth * 2; i++)
1804       DebugContinued("", " ");
1805
1806     DebugContinued("tree", "%c '%s' ['%s] [PARENT: '%s'] %s\n",
1807                    bullet, node->name, node->identifier,
1808                    (node->node_parent ? node->node_parent->identifier : "-"),
1809                    (node->node_group ? "[GROUP]" :
1810                     node->is_copy ? "[COPY]" : ""));
1811
1812     if (!node->node_group && !node->parent_link)
1813       num_leaf_nodes++;
1814
1815     /*
1816     // use for dumping artwork info tree
1817     Debug("tree", "subdir == '%s' ['%s', '%s'] [%d])",
1818           node->subdir, node->fullpath, node->basepath, node->in_user_dir);
1819     */
1820
1821     if (node->node_group != NULL)
1822       num_leaf_nodes += dumpTreeInfo(node->node_group, depth + 1);
1823
1824     node = node->next;
1825   }
1826
1827   if (depth == 0)
1828     Debug("tree", "Summary: %d leaf nodes found", num_leaf_nodes);
1829
1830   return num_leaf_nodes;
1831 }
1832
1833 void sortTreeInfoBySortFunction(TreeInfo **node_first,
1834                                 int (*compare_function)(const void *,
1835                                                         const void *))
1836 {
1837   int num_nodes = numTreeInfo(*node_first);
1838   TreeInfo **sort_array;
1839   TreeInfo *node = *node_first;
1840   int i = 0;
1841
1842   if (num_nodes == 0)
1843     return;
1844
1845   // allocate array for sorting structure pointers
1846   sort_array = checked_calloc(num_nodes * sizeof(TreeInfo *));
1847
1848   // writing structure pointers to sorting array
1849   while (i < num_nodes && node)         // double boundary check...
1850   {
1851     sort_array[i] = node;
1852
1853     i++;
1854     node = node->next;
1855   }
1856
1857   // sorting the structure pointers in the sorting array
1858   qsort(sort_array, num_nodes, sizeof(TreeInfo *),
1859         compare_function);
1860
1861   // update the linkage of list elements with the sorted node array
1862   for (i = 0; i < num_nodes - 1; i++)
1863     sort_array[i]->next = sort_array[i + 1];
1864   sort_array[num_nodes - 1]->next = NULL;
1865
1866   // update the linkage of the main list anchor pointer
1867   *node_first = sort_array[0];
1868
1869   free(sort_array);
1870
1871   // now recursively sort the level group structures
1872   node = *node_first;
1873   while (node)
1874   {
1875     if (node->node_group != NULL)
1876       sortTreeInfoBySortFunction(&node->node_group, compare_function);
1877
1878     node = node->next;
1879   }
1880 }
1881
1882 void sortTreeInfo(TreeInfo **node_first)
1883 {
1884   sortTreeInfoBySortFunction(node_first, compareTreeInfoEntries);
1885 }
1886
1887
1888 // ============================================================================
1889 // some stuff from "files.c"
1890 // ============================================================================
1891
1892 #if defined(PLATFORM_WINDOWS)
1893 #ifndef S_IRGRP
1894 #define S_IRGRP S_IRUSR
1895 #endif
1896 #ifndef S_IROTH
1897 #define S_IROTH S_IRUSR
1898 #endif
1899 #ifndef S_IWGRP
1900 #define S_IWGRP S_IWUSR
1901 #endif
1902 #ifndef S_IWOTH
1903 #define S_IWOTH S_IWUSR
1904 #endif
1905 #ifndef S_IXGRP
1906 #define S_IXGRP S_IXUSR
1907 #endif
1908 #ifndef S_IXOTH
1909 #define S_IXOTH S_IXUSR
1910 #endif
1911 #ifndef S_IRWXG
1912 #define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
1913 #endif
1914 #ifndef S_ISGID
1915 #define S_ISGID 0
1916 #endif
1917 #endif  // PLATFORM_WINDOWS
1918
1919 // file permissions for newly written files
1920 #define MODE_R_ALL              (S_IRUSR | S_IRGRP | S_IROTH)
1921 #define MODE_W_ALL              (S_IWUSR | S_IWGRP | S_IWOTH)
1922 #define MODE_X_ALL              (S_IXUSR | S_IXGRP | S_IXOTH)
1923
1924 #define MODE_W_PRIVATE          (S_IWUSR)
1925 #define MODE_W_PUBLIC_FILE      (S_IWUSR | S_IWGRP)
1926 #define MODE_W_PUBLIC_DIR       (S_IWUSR | S_IWGRP | S_ISGID)
1927
1928 #define DIR_PERMS_PRIVATE       (MODE_R_ALL | MODE_X_ALL | MODE_W_PRIVATE)
1929 #define DIR_PERMS_PUBLIC        (MODE_R_ALL | MODE_X_ALL | MODE_W_PUBLIC_DIR)
1930 #define DIR_PERMS_PUBLIC_ALL    (MODE_R_ALL | MODE_X_ALL | MODE_W_ALL)
1931
1932 #define FILE_PERMS_PRIVATE      (MODE_R_ALL | MODE_W_PRIVATE)
1933 #define FILE_PERMS_PUBLIC       (MODE_R_ALL | MODE_W_PUBLIC_FILE)
1934 #define FILE_PERMS_PUBLIC_ALL   (MODE_R_ALL | MODE_W_ALL)
1935
1936
1937 char *getHomeDir(void)
1938 {
1939   static char *dir = NULL;
1940
1941 #if defined(PLATFORM_WINDOWS)
1942   if (dir == NULL)
1943   {
1944     dir = checked_malloc(MAX_PATH + 1);
1945
1946     if (!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, dir)))
1947       strcpy(dir, ".");
1948   }
1949 #elif defined(PLATFORM_EMSCRIPTEN)
1950   dir = PERSISTENT_DIRECTORY;
1951 #elif defined(PLATFORM_UNIX)
1952   if (dir == NULL)
1953   {
1954     if ((dir = getenv("HOME")) == NULL)
1955     {
1956       dir = getUnixHomeDir();
1957
1958       if (dir != NULL)
1959         dir = getStringCopy(dir);
1960       else
1961         dir = ".";
1962     }
1963   }
1964 #else
1965   dir = ".";
1966 #endif
1967
1968   return dir;
1969 }
1970
1971 char *getPersonalDataDir(void)
1972 {
1973   static char *personal_data_dir = NULL;
1974
1975 #if defined(PLATFORM_MAC)
1976   if (personal_data_dir == NULL)
1977     personal_data_dir = getPath2(getHomeDir(), "Documents");
1978 #else
1979   if (personal_data_dir == NULL)
1980     personal_data_dir = getHomeDir();
1981 #endif
1982
1983   return personal_data_dir;
1984 }
1985
1986 char *getMainUserGameDataDir(void)
1987 {
1988   static char *main_user_data_dir = NULL;
1989
1990 #if defined(PLATFORM_ANDROID)
1991   if (main_user_data_dir == NULL)
1992     main_user_data_dir = (char *)(SDL_AndroidGetExternalStorageState() &
1993                                   SDL_ANDROID_EXTERNAL_STORAGE_WRITE ?
1994                                   SDL_AndroidGetExternalStoragePath() :
1995                                   SDL_AndroidGetInternalStoragePath());
1996 #else
1997   if (main_user_data_dir == NULL)
1998     main_user_data_dir = getPath2(getPersonalDataDir(),
1999                                   program.userdata_subdir);
2000 #endif
2001
2002   return main_user_data_dir;
2003 }
2004
2005 char *getUserGameDataDir(void)
2006 {
2007   if (user.nr == 0)
2008     return getMainUserGameDataDir();
2009   else
2010     return getUserDir(user.nr);
2011 }
2012
2013 char *getSetupDir(void)
2014 {
2015   return getUserGameDataDir();
2016 }
2017
2018 static mode_t posix_umask(mode_t mask)
2019 {
2020 #if defined(PLATFORM_UNIX)
2021   return umask(mask);
2022 #else
2023   return 0;
2024 #endif
2025 }
2026
2027 static int posix_mkdir(const char *pathname, mode_t mode)
2028 {
2029 #if defined(PLATFORM_WINDOWS)
2030   return mkdir(pathname);
2031 #else
2032   return mkdir(pathname, mode);
2033 #endif
2034 }
2035
2036 static boolean posix_process_running_setgid(void)
2037 {
2038 #if defined(PLATFORM_UNIX)
2039   return (getgid() != getegid());
2040 #else
2041   return FALSE;
2042 #endif
2043 }
2044
2045 void createDirectory(char *dir, char *text)
2046 {
2047   if (directoryExists(dir))
2048     return;
2049
2050   // leave "other" permissions in umask untouched, but ensure group parts
2051   // of USERDATA_DIR_MODE are not masked
2052   int permission_class = PERMS_PRIVATE;
2053   mode_t dir_mode = (permission_class == PERMS_PRIVATE ?
2054                      DIR_PERMS_PRIVATE : DIR_PERMS_PUBLIC);
2055   mode_t last_umask = posix_umask(0);
2056   mode_t group_umask = ~(dir_mode & S_IRWXG);
2057   int running_setgid = posix_process_running_setgid();
2058
2059   if (permission_class == PERMS_PUBLIC)
2060   {
2061     // if we're setgid, protect files against "other"
2062     // else keep umask(0) to make the dir world-writable
2063
2064     if (running_setgid)
2065       posix_umask(last_umask & group_umask);
2066     else
2067       dir_mode = DIR_PERMS_PUBLIC_ALL;
2068   }
2069
2070   if (posix_mkdir(dir, dir_mode) != 0)
2071     Warn("cannot create %s directory '%s': %s", text, dir, strerror(errno));
2072
2073   if (permission_class == PERMS_PUBLIC && !running_setgid)
2074     chmod(dir, dir_mode);
2075
2076   posix_umask(last_umask);              // restore previous umask
2077 }
2078
2079 void InitMainUserDataDirectory(void)
2080 {
2081   createDirectory(getMainUserGameDataDir(), "main user data");
2082 }
2083
2084 void InitUserDataDirectory(void)
2085 {
2086   createDirectory(getMainUserGameDataDir(), "main user data");
2087
2088   if (user.nr != 0)
2089   {
2090     createDirectory(getUserDir(-1), "users");
2091     createDirectory(getUserDir(user.nr), "user data");
2092   }
2093 }
2094
2095 void SetFilePermissions(char *filename, int permission_class)
2096 {
2097   int running_setgid = posix_process_running_setgid();
2098   int perms = (permission_class == PERMS_PRIVATE ?
2099                FILE_PERMS_PRIVATE : FILE_PERMS_PUBLIC);
2100
2101   if (permission_class == PERMS_PUBLIC && !running_setgid)
2102     perms = FILE_PERMS_PUBLIC_ALL;
2103
2104   chmod(filename, perms);
2105 }
2106
2107 void fprintFileHeader(FILE *file, char *basename)
2108 {
2109   char *prefix = "# ";
2110   char *sep1 = "=";
2111
2112   fprintf_line_with_prefix(file, prefix, sep1, 77);
2113   fprintf(file, "%s%s\n", prefix, basename);
2114   fprintf_line_with_prefix(file, prefix, sep1, 77);
2115   fprintf(file, "\n");
2116 }
2117
2118 int getFileVersionFromCookieString(const char *cookie)
2119 {
2120   const char *ptr_cookie1, *ptr_cookie2;
2121   const char *pattern1 = "_FILE_VERSION_";
2122   const char *pattern2 = "?.?";
2123   const int len_cookie = strlen(cookie);
2124   const int len_pattern1 = strlen(pattern1);
2125   const int len_pattern2 = strlen(pattern2);
2126   const int len_pattern = len_pattern1 + len_pattern2;
2127   int version_super, version_major;
2128
2129   if (len_cookie <= len_pattern)
2130     return -1;
2131
2132   ptr_cookie1 = &cookie[len_cookie - len_pattern];
2133   ptr_cookie2 = &cookie[len_cookie - len_pattern2];
2134
2135   if (strncmp(ptr_cookie1, pattern1, len_pattern1) != 0)
2136     return -1;
2137
2138   if (ptr_cookie2[0] < '0' || ptr_cookie2[0] > '9' ||
2139       ptr_cookie2[1] != '.' ||
2140       ptr_cookie2[2] < '0' || ptr_cookie2[2] > '9')
2141     return -1;
2142
2143   version_super = ptr_cookie2[0] - '0';
2144   version_major = ptr_cookie2[2] - '0';
2145
2146   return VERSION_IDENT(version_super, version_major, 0, 0);
2147 }
2148
2149 boolean checkCookieString(const char *cookie, const char *template)
2150 {
2151   const char *pattern = "_FILE_VERSION_?.?";
2152   const int len_cookie = strlen(cookie);
2153   const int len_template = strlen(template);
2154   const int len_pattern = strlen(pattern);
2155
2156   if (len_cookie != len_template)
2157     return FALSE;
2158
2159   if (strncmp(cookie, template, len_cookie - len_pattern) != 0)
2160     return FALSE;
2161
2162   return TRUE;
2163 }
2164
2165
2166 // ----------------------------------------------------------------------------
2167 // setup file list and hash handling functions
2168 // ----------------------------------------------------------------------------
2169
2170 char *getFormattedSetupEntry(char *token, char *value)
2171 {
2172   int i;
2173   static char entry[MAX_LINE_LEN];
2174
2175   // if value is an empty string, just return token without value
2176   if (*value == '\0')
2177     return token;
2178
2179   // start with the token and some spaces to format output line
2180   sprintf(entry, "%s:", token);
2181   for (i = strlen(entry); i < token_value_position; i++)
2182     strcat(entry, " ");
2183
2184   // continue with the token's value
2185   strcat(entry, value);
2186
2187   return entry;
2188 }
2189
2190 SetupFileList *newSetupFileList(char *token, char *value)
2191 {
2192   SetupFileList *new = checked_malloc(sizeof(SetupFileList));
2193
2194   new->token = getStringCopy(token);
2195   new->value = getStringCopy(value);
2196
2197   new->next = NULL;
2198
2199   return new;
2200 }
2201
2202 void freeSetupFileList(SetupFileList *list)
2203 {
2204   if (list == NULL)
2205     return;
2206
2207   checked_free(list->token);
2208   checked_free(list->value);
2209
2210   if (list->next)
2211     freeSetupFileList(list->next);
2212
2213   free(list);
2214 }
2215
2216 char *getListEntry(SetupFileList *list, char *token)
2217 {
2218   if (list == NULL)
2219     return NULL;
2220
2221   if (strEqual(list->token, token))
2222     return list->value;
2223   else
2224     return getListEntry(list->next, token);
2225 }
2226
2227 SetupFileList *setListEntry(SetupFileList *list, char *token, char *value)
2228 {
2229   if (list == NULL)
2230     return NULL;
2231
2232   if (strEqual(list->token, token))
2233   {
2234     checked_free(list->value);
2235
2236     list->value = getStringCopy(value);
2237
2238     return list;
2239   }
2240   else if (list->next == NULL)
2241     return (list->next = newSetupFileList(token, value));
2242   else
2243     return setListEntry(list->next, token, value);
2244 }
2245
2246 SetupFileList *addListEntry(SetupFileList *list, char *token, char *value)
2247 {
2248   if (list == NULL)
2249     return NULL;
2250
2251   if (list->next == NULL)
2252     return (list->next = newSetupFileList(token, value));
2253   else
2254     return addListEntry(list->next, token, value);
2255 }
2256
2257 #if ENABLE_UNUSED_CODE
2258 #ifdef DEBUG
2259 static void printSetupFileList(SetupFileList *list)
2260 {
2261   if (!list)
2262     return;
2263
2264   Debug("setup:printSetupFileList", "token: '%s'", list->token);
2265   Debug("setup:printSetupFileList", "value: '%s'", list->value);
2266
2267   printSetupFileList(list->next);
2268 }
2269 #endif
2270 #endif
2271
2272 #ifdef DEBUG
2273 DEFINE_HASHTABLE_INSERT(insert_hash_entry, char, char);
2274 DEFINE_HASHTABLE_SEARCH(search_hash_entry, char, char);
2275 DEFINE_HASHTABLE_CHANGE(change_hash_entry, char, char);
2276 DEFINE_HASHTABLE_REMOVE(remove_hash_entry, char, char);
2277 #else
2278 #define insert_hash_entry hashtable_insert
2279 #define search_hash_entry hashtable_search
2280 #define change_hash_entry hashtable_change
2281 #define remove_hash_entry hashtable_remove
2282 #endif
2283
2284 unsigned int get_hash_from_string(void *key)
2285 {
2286   /*
2287     djb2
2288
2289     This algorithm (k=33) was first reported by Dan Bernstein many years ago in
2290     'comp.lang.c'. Another version of this algorithm (now favored by Bernstein)
2291     uses XOR: hash(i) = hash(i - 1) * 33 ^ str[i]; the magic of number 33 (why
2292     it works better than many other constants, prime or not) has never been
2293     adequately explained.
2294
2295     If you just want to have a good hash function, and cannot wait, djb2
2296     is one of the best string hash functions i know. It has excellent
2297     distribution and speed on many different sets of keys and table sizes.
2298     You are not likely to do better with one of the "well known" functions
2299     such as PJW, K&R, etc.
2300
2301     Ozan (oz) Yigit [http://www.cs.yorku.ca/~oz/hash.html]
2302   */
2303
2304   char *str = (char *)key;
2305   unsigned int hash = 5381;
2306   int c;
2307
2308   while ((c = *str++))
2309     hash = ((hash << 5) + hash) + c;    // hash * 33 + c
2310
2311   return hash;
2312 }
2313
2314 unsigned int get_hash_from_integer(void *key)
2315 {
2316   unsigned int hash = PTR_TO_UINT(key);
2317
2318   return hash;
2319 }
2320
2321 int hash_key_strings_are_equal(void *key1, void *key2)
2322 {
2323   return (strEqual((char *)key1, (char *)key2));
2324 }
2325
2326 int hash_key_integers_are_equal(void *key1, void *key2)
2327 {
2328   return (key1 == key2);
2329 }
2330
2331 SetupFileHash *newSetupFileHash(void)
2332 {
2333   SetupFileHash *new_hash =
2334     create_hashtable(get_hash_from_string, hash_key_strings_are_equal, free, free);
2335
2336   if (new_hash == NULL)
2337     Fail("create_hashtable() failed -- out of memory");
2338
2339   return new_hash;
2340 }
2341
2342 void freeSetupFileHash(SetupFileHash *hash)
2343 {
2344   if (hash == NULL)
2345     return;
2346
2347   hashtable_destroy(hash);
2348 }
2349
2350 char *getHashEntry(SetupFileHash *hash, char *token)
2351 {
2352   if (hash == NULL)
2353     return NULL;
2354
2355   return search_hash_entry(hash, token);
2356 }
2357
2358 void setHashEntry(SetupFileHash *hash, char *token, char *value)
2359 {
2360   char *value_copy;
2361
2362   if (hash == NULL)
2363     return;
2364
2365   value_copy = getStringCopy(value);
2366
2367   // change value; if it does not exist, insert it as new
2368   if (!change_hash_entry(hash, token, value_copy))
2369     if (!insert_hash_entry(hash, getStringCopy(token), value_copy))
2370       Fail("cannot insert into hash -- aborting");
2371 }
2372
2373 void removeHashEntry(SetupFileHash *hash, char *token)
2374 {
2375   if (hash == NULL)
2376     return;
2377
2378   remove_hash_entry(hash, token);
2379 }
2380
2381 #if ENABLE_UNUSED_CODE
2382 #if DEBUG
2383 static void printSetupFileHash(SetupFileHash *hash)
2384 {
2385   BEGIN_HASH_ITERATION(hash, itr)
2386   {
2387     Debug("setup:printSetupFileHash", "token: '%s'", HASH_ITERATION_TOKEN(itr));
2388     Debug("setup:printSetupFileHash", "value: '%s'", HASH_ITERATION_VALUE(itr));
2389   }
2390   END_HASH_ITERATION(hash, itr)
2391 }
2392 #endif
2393 #endif
2394
2395 #define ALLOW_TOKEN_VALUE_SEPARATOR_BEING_WHITESPACE            1
2396 #define CHECK_TOKEN_VALUE_SEPARATOR__WARN_IF_MISSING            0
2397 #define CHECK_TOKEN__WARN_IF_ALREADY_EXISTS_IN_HASH             0
2398
2399 static boolean token_value_separator_found = FALSE;
2400 #if CHECK_TOKEN_VALUE_SEPARATOR__WARN_IF_MISSING
2401 static boolean token_value_separator_warning = FALSE;
2402 #endif
2403 #if CHECK_TOKEN__WARN_IF_ALREADY_EXISTS_IN_HASH
2404 static boolean token_already_exists_warning = FALSE;
2405 #endif
2406
2407 static boolean getTokenValueFromSetupLineExt(char *line,
2408                                              char **token_ptr, char **value_ptr,
2409                                              char *filename, char *line_raw,
2410                                              int line_nr,
2411                                              boolean separator_required)
2412 {
2413   static char line_copy[MAX_LINE_LEN + 1], line_raw_copy[MAX_LINE_LEN + 1];
2414   char *token, *value, *line_ptr;
2415
2416   // when externally invoked via ReadTokenValueFromLine(), copy line buffers
2417   if (line_raw == NULL)
2418   {
2419     strncpy(line_copy, line, MAX_LINE_LEN);
2420     line_copy[MAX_LINE_LEN] = '\0';
2421     line = line_copy;
2422
2423     strcpy(line_raw_copy, line_copy);
2424     line_raw = line_raw_copy;
2425   }
2426
2427   // cut trailing comment from input line
2428   for (line_ptr = line; *line_ptr; line_ptr++)
2429   {
2430     if (*line_ptr == '#')
2431     {
2432       *line_ptr = '\0';
2433       break;
2434     }
2435   }
2436
2437   // cut trailing whitespaces from input line
2438   for (line_ptr = &line[strlen(line)]; line_ptr >= line; line_ptr--)
2439     if ((*line_ptr == ' ' || *line_ptr == '\t') && *(line_ptr + 1) == '\0')
2440       *line_ptr = '\0';
2441
2442   // ignore empty lines
2443   if (*line == '\0')
2444     return FALSE;
2445
2446   // cut leading whitespaces from token
2447   for (token = line; *token; token++)
2448     if (*token != ' ' && *token != '\t')
2449       break;
2450
2451   // start with empty value as reliable default
2452   value = "";
2453
2454   token_value_separator_found = FALSE;
2455
2456   // find end of token to determine start of value
2457   for (line_ptr = token; *line_ptr; line_ptr++)
2458   {
2459     // first look for an explicit token/value separator, like ':' or '='
2460     if (*line_ptr == ':' || *line_ptr == '=')
2461     {
2462       *line_ptr = '\0';                 // terminate token string
2463       value = line_ptr + 1;             // set beginning of value
2464
2465       token_value_separator_found = TRUE;
2466
2467       break;
2468     }
2469   }
2470
2471 #if ALLOW_TOKEN_VALUE_SEPARATOR_BEING_WHITESPACE
2472   // fallback: if no token/value separator found, also allow whitespaces
2473   if (!token_value_separator_found && !separator_required)
2474   {
2475     for (line_ptr = token; *line_ptr; line_ptr++)
2476     {
2477       if (*line_ptr == ' ' || *line_ptr == '\t')
2478       {
2479         *line_ptr = '\0';               // terminate token string
2480         value = line_ptr + 1;           // set beginning of value
2481
2482         token_value_separator_found = TRUE;
2483
2484         break;
2485       }
2486     }
2487
2488 #if CHECK_TOKEN_VALUE_SEPARATOR__WARN_IF_MISSING
2489     if (token_value_separator_found)
2490     {
2491       if (!token_value_separator_warning)
2492       {
2493         Debug("setup", "---");
2494
2495         if (filename != NULL)
2496         {
2497           Debug("setup", "missing token/value separator(s) in config file:");
2498           Debug("setup", "- config file: '%s'", filename);
2499         }
2500         else
2501         {
2502           Debug("setup", "missing token/value separator(s):");
2503         }
2504
2505         token_value_separator_warning = TRUE;
2506       }
2507
2508       if (filename != NULL)
2509         Debug("setup", "- line %d: '%s'", line_nr, line_raw);
2510       else
2511         Debug("setup", "- line: '%s'", line_raw);
2512     }
2513 #endif
2514   }
2515 #endif
2516
2517   // cut trailing whitespaces from token
2518   for (line_ptr = &token[strlen(token)]; line_ptr >= token; line_ptr--)
2519     if ((*line_ptr == ' ' || *line_ptr == '\t') && *(line_ptr + 1) == '\0')
2520       *line_ptr = '\0';
2521
2522   // cut leading whitespaces from value
2523   for (; *value; value++)
2524     if (*value != ' ' && *value != '\t')
2525       break;
2526
2527   *token_ptr = token;
2528   *value_ptr = value;
2529
2530   return TRUE;
2531 }
2532
2533 boolean getTokenValueFromSetupLine(char *line, char **token, char **value)
2534 {
2535   // while the internal (old) interface does not require a token/value
2536   // separator (for downwards compatibility with existing files which
2537   // don't use them), it is mandatory for the external (new) interface
2538
2539   return getTokenValueFromSetupLineExt(line, token, value, NULL, NULL, 0, TRUE);
2540 }
2541
2542 static boolean loadSetupFileData(void *setup_file_data, char *filename,
2543                                  boolean top_recursion_level, boolean is_hash)
2544 {
2545   static SetupFileHash *include_filename_hash = NULL;
2546   char line[MAX_LINE_LEN], line_raw[MAX_LINE_LEN], previous_line[MAX_LINE_LEN];
2547   char *token, *value, *line_ptr;
2548   void *insert_ptr = NULL;
2549   boolean read_continued_line = FALSE;
2550   File *file;
2551   int line_nr = 0, token_count = 0, include_count = 0;
2552
2553 #if CHECK_TOKEN_VALUE_SEPARATOR__WARN_IF_MISSING
2554   token_value_separator_warning = FALSE;
2555 #endif
2556
2557 #if CHECK_TOKEN__WARN_IF_ALREADY_EXISTS_IN_HASH
2558   token_already_exists_warning = FALSE;
2559 #endif
2560
2561   if (!(file = openFile(filename, MODE_READ)))
2562   {
2563 #if DEBUG_NO_CONFIG_FILE
2564     Debug("setup", "cannot open configuration file '%s'", filename);
2565 #endif
2566
2567     return FALSE;
2568   }
2569
2570   // use "insert pointer" to store list end for constant insertion complexity
2571   if (!is_hash)
2572     insert_ptr = setup_file_data;
2573
2574   // on top invocation, create hash to mark included files (to prevent loops)
2575   if (top_recursion_level)
2576     include_filename_hash = newSetupFileHash();
2577
2578   // mark this file as already included (to prevent including it again)
2579   setHashEntry(include_filename_hash, getBaseNamePtr(filename), "true");
2580
2581   while (!checkEndOfFile(file))
2582   {
2583     // read next line of input file
2584     if (!getStringFromFile(file, line, MAX_LINE_LEN))
2585       break;
2586
2587     // check if line was completely read and is terminated by line break
2588     if (strlen(line) > 0 && line[strlen(line) - 1] == '\n')
2589       line_nr++;
2590
2591     // cut trailing line break (this can be newline and/or carriage return)
2592     for (line_ptr = &line[strlen(line)]; line_ptr >= line; line_ptr--)
2593       if ((*line_ptr == '\n' || *line_ptr == '\r') && *(line_ptr + 1) == '\0')
2594         *line_ptr = '\0';
2595
2596     // copy raw input line for later use (mainly debugging output)
2597     strcpy(line_raw, line);
2598
2599     if (read_continued_line)
2600     {
2601       // append new line to existing line, if there is enough space
2602       if (strlen(previous_line) + strlen(line_ptr) < MAX_LINE_LEN)
2603         strcat(previous_line, line_ptr);
2604
2605       strcpy(line, previous_line);      // copy storage buffer to line
2606
2607       read_continued_line = FALSE;
2608     }
2609
2610     // if the last character is '\', continue at next line
2611     if (strlen(line) > 0 && line[strlen(line) - 1] == '\\')
2612     {
2613       line[strlen(line) - 1] = '\0';    // cut off trailing backslash
2614       strcpy(previous_line, line);      // copy line to storage buffer
2615
2616       read_continued_line = TRUE;
2617
2618       continue;
2619     }
2620
2621     if (!getTokenValueFromSetupLineExt(line, &token, &value, filename,
2622                                        line_raw, line_nr, FALSE))
2623       continue;
2624
2625     if (*token)
2626     {
2627       if (strEqual(token, "include"))
2628       {
2629         if (getHashEntry(include_filename_hash, value) == NULL)
2630         {
2631           char *basepath = getBasePath(filename);
2632           char *basename = getBaseName(value);
2633           char *filename_include = getPath2(basepath, basename);
2634
2635           loadSetupFileData(setup_file_data, filename_include, FALSE, is_hash);
2636
2637           free(basepath);
2638           free(basename);
2639           free(filename_include);
2640
2641           include_count++;
2642         }
2643         else
2644         {
2645           Warn("ignoring already processed file '%s'", value);
2646         }
2647       }
2648       else
2649       {
2650         if (is_hash)
2651         {
2652 #if CHECK_TOKEN__WARN_IF_ALREADY_EXISTS_IN_HASH
2653           char *old_value =
2654             getHashEntry((SetupFileHash *)setup_file_data, token);
2655
2656           if (old_value != NULL)
2657           {
2658             if (!token_already_exists_warning)
2659             {
2660               Debug("setup", "---");
2661               Debug("setup", "duplicate token(s) found in config file:");
2662               Debug("setup", "- config file: '%s'", filename);
2663
2664               token_already_exists_warning = TRUE;
2665             }
2666
2667             Debug("setup", "- token: '%s' (in line %d)", token, line_nr);
2668             Debug("setup", "  old value: '%s'", old_value);
2669             Debug("setup", "  new value: '%s'", value);
2670           }
2671 #endif
2672
2673           setHashEntry((SetupFileHash *)setup_file_data, token, value);
2674         }
2675         else
2676         {
2677           insert_ptr = addListEntry((SetupFileList *)insert_ptr, token, value);
2678         }
2679
2680         token_count++;
2681       }
2682     }
2683   }
2684
2685   closeFile(file);
2686
2687 #if CHECK_TOKEN_VALUE_SEPARATOR__WARN_IF_MISSING
2688   if (token_value_separator_warning)
2689     Debug("setup", "---");
2690 #endif
2691
2692 #if CHECK_TOKEN__WARN_IF_ALREADY_EXISTS_IN_HASH
2693   if (token_already_exists_warning)
2694     Debug("setup", "---");
2695 #endif
2696
2697   if (token_count == 0 && include_count == 0)
2698     Warn("configuration file '%s' is empty", filename);
2699
2700   if (top_recursion_level)
2701     freeSetupFileHash(include_filename_hash);
2702
2703   return TRUE;
2704 }
2705
2706 static int compareSetupFileData(const void *object1, const void *object2)
2707 {
2708   const struct ConfigInfo *entry1 = (struct ConfigInfo *)object1;
2709   const struct ConfigInfo *entry2 = (struct ConfigInfo *)object2;
2710
2711   return strcmp(entry1->token, entry2->token);
2712 }
2713
2714 static void saveSetupFileHash(SetupFileHash *hash, char *filename)
2715 {
2716   int item_count = hashtable_count(hash);
2717   int item_size = sizeof(struct ConfigInfo);
2718   struct ConfigInfo *sort_array = checked_malloc(item_count * item_size);
2719   FILE *file;
2720   int i = 0;
2721
2722   // copy string pointers from hash to array
2723   BEGIN_HASH_ITERATION(hash, itr)
2724   {
2725     sort_array[i].token = HASH_ITERATION_TOKEN(itr);
2726     sort_array[i].value = HASH_ITERATION_VALUE(itr);
2727
2728     i++;
2729
2730     if (i > item_count)         // should never happen
2731       break;
2732   }
2733   END_HASH_ITERATION(hash, itr)
2734
2735   // sort string pointers from hash in array
2736   qsort(sort_array, item_count, item_size, compareSetupFileData);
2737
2738   if (!(file = fopen(filename, MODE_WRITE)))
2739   {
2740     Warn("cannot write configuration file '%s'", filename);
2741
2742     return;
2743   }
2744
2745   fprintf(file, "%s\n\n", getFormattedSetupEntry("program.version",
2746                                                  program.version_string));
2747   for (i = 0; i < item_count; i++)
2748     fprintf(file, "%s\n", getFormattedSetupEntry(sort_array[i].token,
2749                                                  sort_array[i].value));
2750   fclose(file);
2751
2752   checked_free(sort_array);
2753 }
2754
2755 SetupFileList *loadSetupFileList(char *filename)
2756 {
2757   SetupFileList *setup_file_list = newSetupFileList("", "");
2758   SetupFileList *first_valid_list_entry;
2759
2760   if (!loadSetupFileData(setup_file_list, filename, TRUE, FALSE))
2761   {
2762     freeSetupFileList(setup_file_list);
2763
2764     return NULL;
2765   }
2766
2767   first_valid_list_entry = setup_file_list->next;
2768
2769   // free empty list header
2770   setup_file_list->next = NULL;
2771   freeSetupFileList(setup_file_list);
2772
2773   return first_valid_list_entry;
2774 }
2775
2776 SetupFileHash *loadSetupFileHash(char *filename)
2777 {
2778   SetupFileHash *setup_file_hash = newSetupFileHash();
2779
2780   if (!loadSetupFileData(setup_file_hash, filename, TRUE, TRUE))
2781   {
2782     freeSetupFileHash(setup_file_hash);
2783
2784     return NULL;
2785   }
2786
2787   return setup_file_hash;
2788 }
2789
2790
2791 // ============================================================================
2792 // setup file stuff
2793 // ============================================================================
2794
2795 #define TOKEN_STR_LAST_LEVEL_SERIES             "last_level_series"
2796 #define TOKEN_STR_LAST_PLAYED_MENU_USED         "last_played_menu_used"
2797 #define TOKEN_STR_LAST_PLAYED_LEVEL             "last_played_level"
2798 #define TOKEN_STR_HANDICAP_LEVEL                "handicap_level"
2799 #define TOKEN_STR_LAST_USER                     "last_user"
2800
2801 // level directory info
2802 #define LEVELINFO_TOKEN_IDENTIFIER              0
2803 #define LEVELINFO_TOKEN_NAME                    1
2804 #define LEVELINFO_TOKEN_NAME_SORTING            2
2805 #define LEVELINFO_TOKEN_AUTHOR                  3
2806 #define LEVELINFO_TOKEN_YEAR                    4
2807 #define LEVELINFO_TOKEN_PROGRAM_TITLE           5
2808 #define LEVELINFO_TOKEN_PROGRAM_COPYRIGHT       6
2809 #define LEVELINFO_TOKEN_PROGRAM_COMPANY         7
2810 #define LEVELINFO_TOKEN_IMPORTED_FROM           8
2811 #define LEVELINFO_TOKEN_IMPORTED_BY             9
2812 #define LEVELINFO_TOKEN_TESTED_BY               10
2813 #define LEVELINFO_TOKEN_LEVELS                  11
2814 #define LEVELINFO_TOKEN_FIRST_LEVEL             12
2815 #define LEVELINFO_TOKEN_SORT_PRIORITY           13
2816 #define LEVELINFO_TOKEN_LATEST_ENGINE           14
2817 #define LEVELINFO_TOKEN_LEVEL_GROUP             15
2818 #define LEVELINFO_TOKEN_READONLY                16
2819 #define LEVELINFO_TOKEN_GRAPHICS_SET_ECS        17
2820 #define LEVELINFO_TOKEN_GRAPHICS_SET_AGA        18
2821 #define LEVELINFO_TOKEN_GRAPHICS_SET            19
2822 #define LEVELINFO_TOKEN_SOUNDS_SET_DEFAULT      20
2823 #define LEVELINFO_TOKEN_SOUNDS_SET_LOWPASS      21
2824 #define LEVELINFO_TOKEN_SOUNDS_SET              22
2825 #define LEVELINFO_TOKEN_MUSIC_SET               23
2826 #define LEVELINFO_TOKEN_FILENAME                24
2827 #define LEVELINFO_TOKEN_FILETYPE                25
2828 #define LEVELINFO_TOKEN_SPECIAL_FLAGS           26
2829 #define LEVELINFO_TOKEN_EMPTY_LEVEL_NAME        27
2830 #define LEVELINFO_TOKEN_FORCE_LEVEL_NAME        28
2831 #define LEVELINFO_TOKEN_HANDICAP                29
2832 #define LEVELINFO_TOKEN_TIME_LIMIT              30
2833 #define LEVELINFO_TOKEN_SKIP_LEVELS             31
2834 #define LEVELINFO_TOKEN_USE_EMC_TILES           32
2835 #define LEVELINFO_TOKEN_INFO_SCREENS_FROM_MAIN  33
2836
2837 #define NUM_LEVELINFO_TOKENS                    34
2838
2839 static LevelDirTree ldi;
2840
2841 static struct TokenInfo levelinfo_tokens[] =
2842 {
2843   // level directory info
2844   { TYPE_STRING,        &ldi.identifier,        "identifier"            },
2845   { TYPE_STRING,        &ldi.name,              "name"                  },
2846   { TYPE_STRING,        &ldi.name_sorting,      "name_sorting"          },
2847   { TYPE_STRING,        &ldi.author,            "author"                },
2848   { TYPE_STRING,        &ldi.year,              "year"                  },
2849   { TYPE_STRING,        &ldi.program_title,     "program_title"         },
2850   { TYPE_STRING,        &ldi.program_copyright, "program_copyright"     },
2851   { TYPE_STRING,        &ldi.program_company,   "program_company"       },
2852   { TYPE_STRING,        &ldi.imported_from,     "imported_from"         },
2853   { TYPE_STRING,        &ldi.imported_by,       "imported_by"           },
2854   { TYPE_STRING,        &ldi.tested_by,         "tested_by"             },
2855   { TYPE_INTEGER,       &ldi.levels,            "levels"                },
2856   { TYPE_INTEGER,       &ldi.first_level,       "first_level"           },
2857   { TYPE_INTEGER,       &ldi.sort_priority,     "sort_priority"         },
2858   { TYPE_BOOLEAN,       &ldi.latest_engine,     "latest_engine"         },
2859   { TYPE_BOOLEAN,       &ldi.level_group,       "level_group"           },
2860   { TYPE_BOOLEAN,       &ldi.readonly,          "readonly"              },
2861   { TYPE_STRING,        &ldi.graphics_set_ecs,  "graphics_set.ecs"      },
2862   { TYPE_STRING,        &ldi.graphics_set_aga,  "graphics_set.aga"      },
2863   { TYPE_STRING,        &ldi.graphics_set,      "graphics_set"          },
2864   { TYPE_STRING,        &ldi.sounds_set_default,"sounds_set.default"    },
2865   { TYPE_STRING,        &ldi.sounds_set_lowpass,"sounds_set.lowpass"    },
2866   { TYPE_STRING,        &ldi.sounds_set,        "sounds_set"            },
2867   { TYPE_STRING,        &ldi.music_set,         "music_set"             },
2868   { TYPE_STRING,        &ldi.level_filename,    "filename"              },
2869   { TYPE_STRING,        &ldi.level_filetype,    "filetype"              },
2870   { TYPE_STRING,        &ldi.special_flags,     "special_flags"         },
2871   { TYPE_STRING,        &ldi.empty_level_name,  "empty_level_name"      },
2872   { TYPE_BOOLEAN,       &ldi.force_level_name,  "force_level_name"      },
2873   { TYPE_BOOLEAN,       &ldi.handicap,          "handicap"              },
2874   { TYPE_BOOLEAN,       &ldi.time_limit,        "time_limit"            },
2875   { TYPE_BOOLEAN,       &ldi.skip_levels,       "skip_levels"           },
2876   { TYPE_BOOLEAN,       &ldi.use_emc_tiles,     "use_emc_tiles"         },
2877   { TYPE_BOOLEAN,       &ldi.info_screens_from_main, "info_screens_from_main" }
2878 };
2879
2880 static struct TokenInfo artworkinfo_tokens[] =
2881 {
2882   // artwork directory info
2883   { TYPE_STRING,        &ldi.identifier,        "identifier"            },
2884   { TYPE_STRING,        &ldi.subdir,            "subdir"                },
2885   { TYPE_STRING,        &ldi.name,              "name"                  },
2886   { TYPE_STRING,        &ldi.name_sorting,      "name_sorting"          },
2887   { TYPE_STRING,        &ldi.author,            "author"                },
2888   { TYPE_STRING,        &ldi.program_title,     "program_title"         },
2889   { TYPE_STRING,        &ldi.program_copyright, "program_copyright"     },
2890   { TYPE_STRING,        &ldi.program_company,   "program_company"       },
2891   { TYPE_INTEGER,       &ldi.sort_priority,     "sort_priority"         },
2892   { TYPE_STRING,        &ldi.basepath,          "basepath"              },
2893   { TYPE_STRING,        &ldi.fullpath,          "fullpath"              },
2894   { TYPE_BOOLEAN,       &ldi.in_user_dir,       "in_user_dir"           },
2895   { TYPE_STRING,        &ldi.class_desc,        "class_desc"            },
2896
2897   { -1,                 NULL,                   NULL                    },
2898 };
2899
2900 static char *optional_tokens[] =
2901 {
2902   "program_title",
2903   "program_copyright",
2904   "program_company",
2905
2906   NULL
2907 };
2908
2909 static void setTreeInfoToDefaults(TreeInfo *ti, int type)
2910 {
2911   ti->type = type;
2912
2913   ti->node_top = (ti->type == TREE_TYPE_LEVEL_DIR    ? &leveldir_first :
2914                   ti->type == TREE_TYPE_GRAPHICS_DIR ? &artwork.gfx_first :
2915                   ti->type == TREE_TYPE_SOUNDS_DIR   ? &artwork.snd_first :
2916                   ti->type == TREE_TYPE_MUSIC_DIR    ? &artwork.mus_first :
2917                   NULL);
2918
2919   ti->node_parent = NULL;
2920   ti->node_group = NULL;
2921   ti->next = NULL;
2922
2923   ti->cl_first = -1;
2924   ti->cl_cursor = -1;
2925
2926   ti->subdir = NULL;
2927   ti->fullpath = NULL;
2928   ti->basepath = NULL;
2929   ti->identifier = NULL;
2930   ti->name = getStringCopy(ANONYMOUS_NAME);
2931   ti->name_sorting = NULL;
2932   ti->author = getStringCopy(ANONYMOUS_NAME);
2933   ti->year = NULL;
2934
2935   ti->program_title = NULL;
2936   ti->program_copyright = NULL;
2937   ti->program_company = NULL;
2938
2939   ti->sort_priority = LEVELCLASS_UNDEFINED;     // default: least priority
2940   ti->latest_engine = FALSE;                    // default: get from level
2941   ti->parent_link = FALSE;
2942   ti->is_copy = FALSE;
2943   ti->in_user_dir = FALSE;
2944   ti->user_defined = FALSE;
2945   ti->color = 0;
2946   ti->class_desc = NULL;
2947
2948   ti->infotext = getStringCopy(TREE_INFOTEXT(ti->type));
2949
2950   if (ti->type == TREE_TYPE_LEVEL_DIR)
2951   {
2952     ti->imported_from = NULL;
2953     ti->imported_by = NULL;
2954     ti->tested_by = NULL;
2955
2956     ti->graphics_set_ecs = NULL;
2957     ti->graphics_set_aga = NULL;
2958     ti->graphics_set = NULL;
2959     ti->sounds_set_default = NULL;
2960     ti->sounds_set_lowpass = NULL;
2961     ti->sounds_set = NULL;
2962     ti->music_set = NULL;
2963     ti->graphics_path = getStringCopy(UNDEFINED_FILENAME);
2964     ti->sounds_path = getStringCopy(UNDEFINED_FILENAME);
2965     ti->music_path = getStringCopy(UNDEFINED_FILENAME);
2966
2967     ti->level_filename = NULL;
2968     ti->level_filetype = NULL;
2969
2970     ti->special_flags = NULL;
2971
2972     ti->empty_level_name = NULL;
2973     ti->force_level_name = FALSE;
2974
2975     ti->levels = 0;
2976     ti->first_level = 0;
2977     ti->last_level = 0;
2978     ti->level_group = FALSE;
2979     ti->handicap_level = 0;
2980     ti->readonly = TRUE;
2981     ti->handicap = TRUE;
2982     ti->time_limit = TRUE;
2983     ti->skip_levels = FALSE;
2984
2985     ti->use_emc_tiles = FALSE;
2986     ti->info_screens_from_main = FALSE;
2987   }
2988 }
2989
2990 static void setTreeInfoToDefaultsFromParent(TreeInfo *ti, TreeInfo *parent)
2991 {
2992   if (parent == NULL)
2993   {
2994     Warn("setTreeInfoToDefaultsFromParent(): parent == NULL");
2995
2996     setTreeInfoToDefaults(ti, TREE_TYPE_UNDEFINED);
2997
2998     return;
2999   }
3000
3001   // copy all values from the parent structure
3002
3003   ti->type = parent->type;
3004
3005   ti->node_top = parent->node_top;
3006   ti->node_parent = parent;
3007   ti->node_group = NULL;
3008   ti->next = NULL;
3009
3010   ti->cl_first = -1;
3011   ti->cl_cursor = -1;
3012
3013   ti->subdir = NULL;
3014   ti->fullpath = NULL;
3015   ti->basepath = NULL;
3016   ti->identifier = NULL;
3017   ti->name = getStringCopy(ANONYMOUS_NAME);
3018   ti->name_sorting = NULL;
3019   ti->author = getStringCopy(parent->author);
3020   ti->year = getStringCopy(parent->year);
3021
3022   ti->program_title = getStringCopy(parent->program_title);
3023   ti->program_copyright = getStringCopy(parent->program_copyright);
3024   ti->program_company = getStringCopy(parent->program_company);
3025
3026   ti->sort_priority = parent->sort_priority;
3027   ti->latest_engine = parent->latest_engine;
3028   ti->parent_link = FALSE;
3029   ti->is_copy = FALSE;
3030   ti->in_user_dir = parent->in_user_dir;
3031   ti->user_defined = parent->user_defined;
3032   ti->color = parent->color;
3033   ti->class_desc = getStringCopy(parent->class_desc);
3034
3035   ti->infotext = getStringCopy(parent->infotext);
3036
3037   if (ti->type == TREE_TYPE_LEVEL_DIR)
3038   {
3039     ti->imported_from = getStringCopy(parent->imported_from);
3040     ti->imported_by = getStringCopy(parent->imported_by);
3041     ti->tested_by = getStringCopy(parent->tested_by);
3042
3043     ti->graphics_set_ecs = getStringCopy(parent->graphics_set_ecs);
3044     ti->graphics_set_aga = getStringCopy(parent->graphics_set_aga);
3045     ti->graphics_set = getStringCopy(parent->graphics_set);
3046     ti->sounds_set_default = getStringCopy(parent->sounds_set_default);
3047     ti->sounds_set_lowpass = getStringCopy(parent->sounds_set_lowpass);
3048     ti->sounds_set = getStringCopy(parent->sounds_set);
3049     ti->music_set = getStringCopy(parent->music_set);
3050     ti->graphics_path = getStringCopy(UNDEFINED_FILENAME);
3051     ti->sounds_path = getStringCopy(UNDEFINED_FILENAME);
3052     ti->music_path = getStringCopy(UNDEFINED_FILENAME);
3053
3054     ti->level_filename = getStringCopy(parent->level_filename);
3055     ti->level_filetype = getStringCopy(parent->level_filetype);
3056
3057     ti->special_flags = getStringCopy(parent->special_flags);
3058
3059     ti->empty_level_name = getStringCopy(parent->empty_level_name);
3060     ti->force_level_name = parent->force_level_name;
3061
3062     ti->levels = parent->levels;
3063     ti->first_level = parent->first_level;
3064     ti->last_level = parent->last_level;
3065     ti->level_group = FALSE;
3066     ti->handicap_level = parent->handicap_level;
3067     ti->readonly = parent->readonly;
3068     ti->handicap = parent->handicap;
3069     ti->time_limit = parent->time_limit;
3070     ti->skip_levels = parent->skip_levels;
3071
3072     ti->use_emc_tiles = parent->use_emc_tiles;
3073     ti->info_screens_from_main = parent->info_screens_from_main;
3074   }
3075 }
3076
3077 static TreeInfo *getTreeInfoCopy(TreeInfo *ti)
3078 {
3079   TreeInfo *ti_copy = newTreeInfo();
3080
3081   // copy all values from the original structure
3082
3083   ti_copy->type                 = ti->type;
3084
3085   ti_copy->node_top             = ti->node_top;
3086   ti_copy->node_parent          = ti->node_parent;
3087   ti_copy->node_group           = ti->node_group;
3088   ti_copy->next                 = ti->next;
3089
3090   ti_copy->cl_first             = ti->cl_first;
3091   ti_copy->cl_cursor            = ti->cl_cursor;
3092
3093   ti_copy->subdir               = getStringCopy(ti->subdir);
3094   ti_copy->fullpath             = getStringCopy(ti->fullpath);
3095   ti_copy->basepath             = getStringCopy(ti->basepath);
3096   ti_copy->identifier           = getStringCopy(ti->identifier);
3097   ti_copy->name                 = getStringCopy(ti->name);
3098   ti_copy->name_sorting         = getStringCopy(ti->name_sorting);
3099   ti_copy->author               = getStringCopy(ti->author);
3100   ti_copy->year                 = getStringCopy(ti->year);
3101
3102   ti_copy->program_title        = getStringCopy(ti->program_title);
3103   ti_copy->program_copyright    = getStringCopy(ti->program_copyright);
3104   ti_copy->program_company      = getStringCopy(ti->program_company);
3105
3106   ti_copy->imported_from        = getStringCopy(ti->imported_from);
3107   ti_copy->imported_by          = getStringCopy(ti->imported_by);
3108   ti_copy->tested_by            = getStringCopy(ti->tested_by);
3109
3110   ti_copy->graphics_set_ecs     = getStringCopy(ti->graphics_set_ecs);
3111   ti_copy->graphics_set_aga     = getStringCopy(ti->graphics_set_aga);
3112   ti_copy->graphics_set         = getStringCopy(ti->graphics_set);
3113   ti_copy->sounds_set_default   = getStringCopy(ti->sounds_set_default);
3114   ti_copy->sounds_set_lowpass   = getStringCopy(ti->sounds_set_lowpass);
3115   ti_copy->sounds_set           = getStringCopy(ti->sounds_set);
3116   ti_copy->music_set            = getStringCopy(ti->music_set);
3117   ti_copy->graphics_path        = getStringCopy(ti->graphics_path);
3118   ti_copy->sounds_path          = getStringCopy(ti->sounds_path);
3119   ti_copy->music_path           = getStringCopy(ti->music_path);
3120
3121   ti_copy->level_filename       = getStringCopy(ti->level_filename);
3122   ti_copy->level_filetype       = getStringCopy(ti->level_filetype);
3123
3124   ti_copy->special_flags        = getStringCopy(ti->special_flags);
3125
3126   ti_copy->empty_level_name     = getStringCopy(ti->empty_level_name);
3127   ti_copy->force_level_name     = ti->force_level_name;
3128
3129   ti_copy->levels               = ti->levels;
3130   ti_copy->first_level          = ti->first_level;
3131   ti_copy->last_level           = ti->last_level;
3132   ti_copy->sort_priority        = ti->sort_priority;
3133
3134   ti_copy->latest_engine        = ti->latest_engine;
3135
3136   ti_copy->level_group          = ti->level_group;
3137   ti_copy->parent_link          = ti->parent_link;
3138   ti_copy->is_copy              = ti->is_copy;
3139   ti_copy->in_user_dir          = ti->in_user_dir;
3140   ti_copy->user_defined         = ti->user_defined;
3141   ti_copy->readonly             = ti->readonly;
3142   ti_copy->handicap             = ti->handicap;
3143   ti_copy->time_limit           = ti->time_limit;
3144   ti_copy->skip_levels          = ti->skip_levels;
3145
3146   ti_copy->use_emc_tiles        = ti->use_emc_tiles;
3147   ti_copy->info_screens_from_main = ti->info_screens_from_main;
3148
3149   ti_copy->color                = ti->color;
3150   ti_copy->class_desc           = getStringCopy(ti->class_desc);
3151   ti_copy->handicap_level       = ti->handicap_level;
3152
3153   ti_copy->infotext             = getStringCopy(ti->infotext);
3154
3155   return ti_copy;
3156 }
3157
3158 void freeTreeInfo(TreeInfo *ti)
3159 {
3160   if (ti == NULL)
3161     return;
3162
3163   checked_free(ti->subdir);
3164   checked_free(ti->fullpath);
3165   checked_free(ti->basepath);
3166   checked_free(ti->identifier);
3167
3168   checked_free(ti->name);
3169   checked_free(ti->name_sorting);
3170   checked_free(ti->author);
3171   checked_free(ti->year);
3172
3173   checked_free(ti->program_title);
3174   checked_free(ti->program_copyright);
3175   checked_free(ti->program_company);
3176
3177   checked_free(ti->class_desc);
3178
3179   checked_free(ti->infotext);
3180
3181   if (ti->type == TREE_TYPE_LEVEL_DIR)
3182   {
3183     checked_free(ti->imported_from);
3184     checked_free(ti->imported_by);
3185     checked_free(ti->tested_by);
3186
3187     checked_free(ti->graphics_set_ecs);
3188     checked_free(ti->graphics_set_aga);
3189     checked_free(ti->graphics_set);
3190     checked_free(ti->sounds_set_default);
3191     checked_free(ti->sounds_set_lowpass);
3192     checked_free(ti->sounds_set);
3193     checked_free(ti->music_set);
3194
3195     checked_free(ti->graphics_path);
3196     checked_free(ti->sounds_path);
3197     checked_free(ti->music_path);
3198
3199     checked_free(ti->level_filename);
3200     checked_free(ti->level_filetype);
3201
3202     checked_free(ti->special_flags);
3203   }
3204
3205   // recursively free child node
3206   if (ti->node_group)
3207     freeTreeInfo(ti->node_group);
3208
3209   // recursively free next node
3210   if (ti->next)
3211     freeTreeInfo(ti->next);
3212
3213   checked_free(ti);
3214 }
3215
3216 void setSetupInfo(struct TokenInfo *token_info,
3217                   int token_nr, char *token_value)
3218 {
3219   int token_type = token_info[token_nr].type;
3220   void *setup_value = token_info[token_nr].value;
3221
3222   if (token_value == NULL)
3223     return;
3224
3225   // set setup field to corresponding token value
3226   switch (token_type)
3227   {
3228     case TYPE_BOOLEAN:
3229     case TYPE_SWITCH:
3230       *(boolean *)setup_value = get_boolean_from_string(token_value);
3231       break;
3232
3233     case TYPE_SWITCH3:
3234       *(int *)setup_value = get_switch3_from_string(token_value);
3235       break;
3236
3237     case TYPE_KEY:
3238       *(Key *)setup_value = getKeyFromKeyName(token_value);
3239       break;
3240
3241     case TYPE_KEY_X11:
3242       *(Key *)setup_value = getKeyFromX11KeyName(token_value);
3243       break;
3244
3245     case TYPE_INTEGER:
3246       *(int *)setup_value = get_integer_from_string(token_value);
3247       break;
3248
3249     case TYPE_STRING:
3250       checked_free(*(char **)setup_value);
3251       *(char **)setup_value = getStringCopy(token_value);
3252       break;
3253
3254     case TYPE_PLAYER:
3255       *(int *)setup_value = get_player_nr_from_string(token_value);
3256       break;
3257
3258     default:
3259       break;
3260   }
3261 }
3262
3263 static int compareTreeInfoEntries(const void *object1, const void *object2)
3264 {
3265   const TreeInfo *entry1 = *((TreeInfo **)object1);
3266   const TreeInfo *entry2 = *((TreeInfo **)object2);
3267   int tree_sorting1 = TREE_SORTING(entry1);
3268   int tree_sorting2 = TREE_SORTING(entry2);
3269
3270   if (tree_sorting1 != tree_sorting2)
3271     return (tree_sorting1 - tree_sorting2);
3272   else
3273     return strcasecmp(entry1->name_sorting, entry2->name_sorting);
3274 }
3275
3276 static TreeInfo *createParentTreeInfoNode(TreeInfo *node_parent)
3277 {
3278   TreeInfo *ti_new;
3279
3280   if (node_parent == NULL)
3281     return NULL;
3282
3283   ti_new = newTreeInfo();
3284   setTreeInfoToDefaults(ti_new, node_parent->type);
3285
3286   ti_new->node_parent = node_parent;
3287   ti_new->parent_link = TRUE;
3288
3289   setString(&ti_new->identifier, node_parent->identifier);
3290   setString(&ti_new->name, BACKLINK_TEXT_PARENT);
3291   setString(&ti_new->name_sorting, ti_new->name);
3292
3293   setString(&ti_new->subdir, STRING_PARENT_DIRECTORY);
3294   setString(&ti_new->fullpath, node_parent->fullpath);
3295
3296   ti_new->sort_priority = LEVELCLASS_PARENT;
3297   ti_new->latest_engine = node_parent->latest_engine;
3298
3299   setString(&ti_new->class_desc, getLevelClassDescription(ti_new));
3300
3301   pushTreeInfo(&node_parent->node_group, ti_new);
3302
3303   return ti_new;
3304 }
3305
3306 static TreeInfo *createTopTreeInfoNode(TreeInfo *node_first)
3307 {
3308   if (node_first == NULL)
3309     return NULL;
3310
3311   TreeInfo *ti_new = newTreeInfo();
3312   int type = node_first->type;
3313
3314   setTreeInfoToDefaults(ti_new, type);
3315
3316   ti_new->node_parent = NULL;
3317   ti_new->parent_link = FALSE;
3318
3319   setString(&ti_new->identifier, "top_tree_node");
3320   setString(&ti_new->name, TREE_INFOTEXT(type));
3321   setString(&ti_new->name_sorting, ti_new->name);
3322
3323   setString(&ti_new->subdir, STRING_TOP_DIRECTORY);
3324   setString(&ti_new->fullpath, ".");
3325
3326   ti_new->sort_priority = LEVELCLASS_TOP;
3327   ti_new->latest_engine = node_first->latest_engine;
3328
3329   setString(&ti_new->class_desc, TREE_INFOTEXT(type));
3330
3331   ti_new->node_group = node_first;
3332   ti_new->level_group = TRUE;
3333
3334   TreeInfo *ti_new2 = createParentTreeInfoNode(ti_new);
3335
3336   setString(&ti_new2->name, TREE_BACKLINK_TEXT(type));
3337   setString(&ti_new2->name_sorting, ti_new2->name);
3338
3339   return ti_new;
3340 }
3341
3342 static void setTreeInfoParentNodes(TreeInfo *node, TreeInfo *node_parent)
3343 {
3344   while (node)
3345   {
3346     if (node->node_group)
3347       setTreeInfoParentNodes(node->node_group, node);
3348
3349     node->node_parent = node_parent;
3350
3351     node = node->next;
3352   }
3353 }
3354
3355 TreeInfo *addTopTreeInfoNode(TreeInfo *node_first)
3356 {
3357   // add top tree node with back link node in previous tree
3358   node_first = createTopTreeInfoNode(node_first);
3359
3360   // set all parent links (back links) in complete tree
3361   setTreeInfoParentNodes(node_first, NULL);
3362
3363   return node_first;
3364 }
3365
3366
3367 // ----------------------------------------------------------------------------
3368 // functions for handling level and custom artwork info cache
3369 // ----------------------------------------------------------------------------
3370
3371 static void LoadArtworkInfoCache(void)
3372 {
3373   InitCacheDirectory();
3374
3375   if (artworkinfo_cache_old == NULL)
3376   {
3377     char *filename = getPath2(getCacheDir(), ARTWORKINFO_CACHE_FILE);
3378
3379     // try to load artwork info hash from already existing cache file
3380     artworkinfo_cache_old = loadSetupFileHash(filename);
3381
3382     // try to get program version that artwork info cache was written with
3383     char *version = getHashEntry(artworkinfo_cache_old, "program.version");
3384
3385     // check program version of artwork info cache against current version
3386     if (!strEqual(version, program.version_string))
3387     {
3388       freeSetupFileHash(artworkinfo_cache_old);
3389
3390       artworkinfo_cache_old = NULL;
3391     }
3392
3393     // if no artwork info cache file was found, start with empty hash
3394     if (artworkinfo_cache_old == NULL)
3395       artworkinfo_cache_old = newSetupFileHash();
3396
3397     free(filename);
3398   }
3399
3400   if (artworkinfo_cache_new == NULL)
3401     artworkinfo_cache_new = newSetupFileHash();
3402
3403   update_artworkinfo_cache = FALSE;
3404 }
3405
3406 static void SaveArtworkInfoCache(void)
3407 {
3408   if (!update_artworkinfo_cache)
3409     return;
3410
3411   char *filename = getPath2(getCacheDir(), ARTWORKINFO_CACHE_FILE);
3412
3413   InitCacheDirectory();
3414
3415   saveSetupFileHash(artworkinfo_cache_new, filename);
3416
3417   free(filename);
3418 }
3419
3420 static char *getCacheTokenPrefix(char *prefix1, char *prefix2)
3421 {
3422   static char *prefix = NULL;
3423
3424   checked_free(prefix);
3425
3426   prefix = getStringCat2WithSeparator(prefix1, prefix2, ".");
3427
3428   return prefix;
3429 }
3430
3431 // (identical to above function, but separate string buffer needed -- nasty)
3432 static char *getCacheToken(char *prefix, char *suffix)
3433 {
3434   static char *token = NULL;
3435
3436   checked_free(token);
3437
3438   token = getStringCat2WithSeparator(prefix, suffix, ".");
3439
3440   return token;
3441 }
3442
3443 static char *getFileTimestampString(char *filename)
3444 {
3445   return getStringCopy(i_to_a(getFileTimestampEpochSeconds(filename)));
3446 }
3447
3448 static boolean modifiedFileTimestamp(char *filename, char *timestamp_string)
3449 {
3450   struct stat file_status;
3451
3452   if (timestamp_string == NULL)
3453     return TRUE;
3454
3455   if (!fileExists(filename))                    // file does not exist
3456     return (atoi(timestamp_string) != 0);
3457
3458   if (stat(filename, &file_status) != 0)        // cannot stat file
3459     return TRUE;
3460
3461   return (file_status.st_mtime != atoi(timestamp_string));
3462 }
3463
3464 static TreeInfo *getArtworkInfoCacheEntry(LevelDirTree *level_node, int type)
3465 {
3466   char *identifier = level_node->subdir;
3467   char *type_string = ARTWORK_DIRECTORY(type);
3468   char *token_prefix = getCacheTokenPrefix(type_string, identifier);
3469   char *token_main = getCacheToken(token_prefix, "CACHED");
3470   char *cache_entry = getHashEntry(artworkinfo_cache_old, token_main);
3471   boolean cached = (cache_entry != NULL && strEqual(cache_entry, "true"));
3472   TreeInfo *artwork_info = NULL;
3473
3474   if (!use_artworkinfo_cache)
3475     return NULL;
3476
3477   if (optional_tokens_hash == NULL)
3478   {
3479     int i;
3480
3481     // create hash from list of optional tokens (for quick access)
3482     optional_tokens_hash = newSetupFileHash();
3483     for (i = 0; optional_tokens[i] != NULL; i++)
3484       setHashEntry(optional_tokens_hash, optional_tokens[i], "");
3485   }
3486
3487   if (cached)
3488   {
3489     int i;
3490
3491     artwork_info = newTreeInfo();
3492     setTreeInfoToDefaults(artwork_info, type);
3493
3494     // set all structure fields according to the token/value pairs
3495     ldi = *artwork_info;
3496     for (i = 0; artworkinfo_tokens[i].type != -1; i++)
3497     {
3498       char *token_suffix = artworkinfo_tokens[i].text;
3499       char *token = getCacheToken(token_prefix, token_suffix);
3500       char *value = getHashEntry(artworkinfo_cache_old, token);
3501       boolean optional =
3502         (getHashEntry(optional_tokens_hash, token_suffix) != NULL);
3503
3504       setSetupInfo(artworkinfo_tokens, i, value);
3505
3506       // check if cache entry for this item is mandatory, but missing
3507       if (value == NULL && !optional)
3508       {
3509         Warn("missing cache entry '%s'", token);
3510
3511         cached = FALSE;
3512       }
3513     }
3514
3515     *artwork_info = ldi;
3516   }
3517
3518   if (cached)
3519   {
3520     char *filename_levelinfo = getPath2(getLevelDirFromTreeInfo(level_node),
3521                                         LEVELINFO_FILENAME);
3522     char *filename_artworkinfo = getPath2(getSetupArtworkDir(artwork_info),
3523                                           ARTWORKINFO_FILENAME(type));
3524
3525     // check if corresponding "levelinfo.conf" file has changed
3526     token_main = getCacheToken(token_prefix, "TIMESTAMP_LEVELINFO");
3527     cache_entry = getHashEntry(artworkinfo_cache_old, token_main);
3528
3529     if (modifiedFileTimestamp(filename_levelinfo, cache_entry))
3530       cached = FALSE;
3531
3532     // check if corresponding "<artworkinfo>.conf" file has changed
3533     token_main = getCacheToken(token_prefix, "TIMESTAMP_ARTWORKINFO");
3534     cache_entry = getHashEntry(artworkinfo_cache_old, token_main);
3535
3536     if (modifiedFileTimestamp(filename_artworkinfo, cache_entry))
3537       cached = FALSE;
3538
3539     checked_free(filename_levelinfo);
3540     checked_free(filename_artworkinfo);
3541   }
3542
3543   if (!cached && artwork_info != NULL)
3544   {
3545     freeTreeInfo(artwork_info);
3546
3547     return NULL;
3548   }
3549
3550   return artwork_info;
3551 }
3552
3553 static void setArtworkInfoCacheEntry(TreeInfo *artwork_info,
3554                                      LevelDirTree *level_node, int type)
3555 {
3556   char *identifier = level_node->subdir;
3557   char *type_string = ARTWORK_DIRECTORY(type);
3558   char *token_prefix = getCacheTokenPrefix(type_string, identifier);
3559   char *token_main = getCacheToken(token_prefix, "CACHED");
3560   boolean set_cache_timestamps = TRUE;
3561   int i;
3562
3563   setHashEntry(artworkinfo_cache_new, token_main, "true");
3564
3565   if (set_cache_timestamps)
3566   {
3567     char *filename_levelinfo = getPath2(getLevelDirFromTreeInfo(level_node),
3568                                         LEVELINFO_FILENAME);
3569     char *filename_artworkinfo = getPath2(getSetupArtworkDir(artwork_info),
3570                                           ARTWORKINFO_FILENAME(type));
3571     char *timestamp_levelinfo = getFileTimestampString(filename_levelinfo);
3572     char *timestamp_artworkinfo = getFileTimestampString(filename_artworkinfo);
3573
3574     token_main = getCacheToken(token_prefix, "TIMESTAMP_LEVELINFO");
3575     setHashEntry(artworkinfo_cache_new, token_main, timestamp_levelinfo);
3576
3577     token_main = getCacheToken(token_prefix, "TIMESTAMP_ARTWORKINFO");
3578     setHashEntry(artworkinfo_cache_new, token_main, timestamp_artworkinfo);
3579
3580     checked_free(filename_levelinfo);
3581     checked_free(filename_artworkinfo);
3582     checked_free(timestamp_levelinfo);
3583     checked_free(timestamp_artworkinfo);
3584   }
3585
3586   ldi = *artwork_info;
3587   for (i = 0; artworkinfo_tokens[i].type != -1; i++)
3588   {
3589     char *token = getCacheToken(token_prefix, artworkinfo_tokens[i].text);
3590     char *value = getSetupValue(artworkinfo_tokens[i].type,
3591                                 artworkinfo_tokens[i].value);
3592     if (value != NULL)
3593       setHashEntry(artworkinfo_cache_new, token, value);
3594   }
3595 }
3596
3597
3598 // ----------------------------------------------------------------------------
3599 // functions for loading level info and custom artwork info
3600 // ----------------------------------------------------------------------------
3601
3602 int GetZipFileTreeType(char *zip_filename)
3603 {
3604   static char *top_dir_path = NULL;
3605   static char *top_dir_conf_filename[NUM_BASE_TREE_TYPES] = { NULL };
3606   static char *conf_basename[NUM_BASE_TREE_TYPES] =
3607   {
3608     GRAPHICSINFO_FILENAME,
3609     SOUNDSINFO_FILENAME,
3610     MUSICINFO_FILENAME,
3611     LEVELINFO_FILENAME
3612   };
3613   int j;
3614
3615   checked_free(top_dir_path);
3616   top_dir_path = NULL;
3617
3618   for (j = 0; j < NUM_BASE_TREE_TYPES; j++)
3619   {
3620     checked_free(top_dir_conf_filename[j]);
3621     top_dir_conf_filename[j] = NULL;
3622   }
3623
3624   char **zip_entries = zip_list(zip_filename);
3625
3626   // check if zip file successfully opened
3627   if (zip_entries == NULL || zip_entries[0] == NULL)
3628     return TREE_TYPE_UNDEFINED;
3629
3630   // first zip file entry is expected to be top level directory
3631   char *top_dir = zip_entries[0];
3632
3633   // check if valid top level directory found in zip file
3634   if (!strSuffix(top_dir, "/"))
3635     return TREE_TYPE_UNDEFINED;
3636
3637   // get filenames of valid configuration files in top level directory
3638   for (j = 0; j < NUM_BASE_TREE_TYPES; j++)
3639     top_dir_conf_filename[j] = getStringCat2(top_dir, conf_basename[j]);
3640
3641   int tree_type = TREE_TYPE_UNDEFINED;
3642   int e = 0;
3643
3644   while (zip_entries[e] != NULL)
3645   {
3646     // check if every zip file entry is below top level directory
3647     if (!strPrefix(zip_entries[e], top_dir))
3648       return TREE_TYPE_UNDEFINED;
3649
3650     // check if this zip file entry is a valid configuration filename
3651     for (j = 0; j < NUM_BASE_TREE_TYPES; j++)
3652     {
3653       if (strEqual(zip_entries[e], top_dir_conf_filename[j]))
3654       {
3655         // only exactly one valid configuration file allowed
3656         if (tree_type != TREE_TYPE_UNDEFINED)
3657           return TREE_TYPE_UNDEFINED;
3658
3659         tree_type = j;
3660       }
3661     }
3662
3663     e++;
3664   }
3665
3666   return tree_type;
3667 }
3668
3669 static boolean CheckZipFileForDirectory(char *zip_filename, char *directory,
3670                                         int tree_type)
3671 {
3672   static char *top_dir_path = NULL;
3673   static char *top_dir_conf_filename = NULL;
3674
3675   checked_free(top_dir_path);
3676   checked_free(top_dir_conf_filename);
3677
3678   top_dir_path = NULL;
3679   top_dir_conf_filename = NULL;
3680
3681   char *conf_basename = (tree_type == TREE_TYPE_LEVEL_DIR ? LEVELINFO_FILENAME :
3682                          ARTWORKINFO_FILENAME(tree_type));
3683
3684   // check if valid configuration filename determined
3685   if (conf_basename == NULL || strEqual(conf_basename, ""))
3686     return FALSE;
3687
3688   char **zip_entries = zip_list(zip_filename);
3689
3690   // check if zip file successfully opened
3691   if (zip_entries == NULL || zip_entries[0] == NULL)
3692     return FALSE;
3693
3694   // first zip file entry is expected to be top level directory
3695   char *top_dir = zip_entries[0];
3696
3697   // check if valid top level directory found in zip file
3698   if (!strSuffix(top_dir, "/"))
3699     return FALSE;
3700
3701   // get path of extracted top level directory
3702   top_dir_path = getPath2(directory, top_dir);
3703
3704   // remove trailing directory separator from top level directory path
3705   // (required to be able to check for file and directory in next step)
3706   top_dir_path[strlen(top_dir_path) - 1] = '\0';
3707
3708   // check if zip file's top level directory already exists in target directory
3709   if (fileExists(top_dir_path))         // (checks for file and directory)
3710     return FALSE;
3711
3712   // get filename of configuration file in top level directory
3713   top_dir_conf_filename = getStringCat2(top_dir, conf_basename);
3714
3715   boolean found_top_dir_conf_filename = FALSE;
3716   int i = 0;
3717
3718   while (zip_entries[i] != NULL)
3719   {
3720     // check if every zip file entry is below top level directory
3721     if (!strPrefix(zip_entries[i], top_dir))
3722       return FALSE;
3723
3724     // check if this zip file entry is the configuration filename
3725     if (strEqual(zip_entries[i], top_dir_conf_filename))
3726       found_top_dir_conf_filename = TRUE;
3727
3728     i++;
3729   }
3730
3731   // check if valid configuration filename was found in zip file
3732   if (!found_top_dir_conf_filename)
3733     return FALSE;
3734
3735   return TRUE;
3736 }
3737
3738 char *ExtractZipFileIntoDirectory(char *zip_filename, char *directory,
3739                                   int tree_type)
3740 {
3741   boolean zip_file_valid = CheckZipFileForDirectory(zip_filename, directory,
3742                                                     tree_type);
3743
3744   if (!zip_file_valid)
3745   {
3746     Warn("zip file '%s' rejected!", zip_filename);
3747
3748     return NULL;
3749   }
3750
3751   char **zip_entries = zip_extract(zip_filename, directory);
3752
3753   if (zip_entries == NULL)
3754   {
3755     Warn("zip file '%s' could not be extracted!", zip_filename);
3756
3757     return NULL;
3758   }
3759
3760   Info("zip file '%s' successfully extracted!", zip_filename);
3761
3762   // first zip file entry contains top level directory
3763   char *top_dir = zip_entries[0];
3764
3765   // remove trailing directory separator from top level directory
3766   top_dir[strlen(top_dir) - 1] = '\0';
3767
3768   return top_dir;
3769 }
3770
3771 static void ProcessZipFilesInDirectory(char *directory, int tree_type)
3772 {
3773   Directory *dir;
3774   DirectoryEntry *dir_entry;
3775
3776   if ((dir = openDirectory(directory)) == NULL)
3777   {
3778     // display error if directory is main "options.graphics_directory" etc.
3779     if (tree_type == TREE_TYPE_LEVEL_DIR ||
3780         directory == OPTIONS_ARTWORK_DIRECTORY(tree_type))
3781       Warn("cannot read directory '%s'", directory);
3782
3783     return;
3784   }
3785
3786   while ((dir_entry = readDirectory(dir)) != NULL)      // loop all entries
3787   {
3788     // skip non-zip files (and also directories with zip extension)
3789     if (!strSuffixLower(dir_entry->basename, ".zip") || dir_entry->is_directory)
3790       continue;
3791
3792     char *zip_filename = getPath2(directory, dir_entry->basename);
3793     char *zip_filename_extracted = getStringCat2(zip_filename, ".extracted");
3794     char *zip_filename_rejected  = getStringCat2(zip_filename, ".rejected");
3795
3796     // check if zip file hasn't already been extracted or rejected
3797     if (!fileExists(zip_filename_extracted) &&
3798         !fileExists(zip_filename_rejected))
3799     {
3800       char *top_dir = ExtractZipFileIntoDirectory(zip_filename, directory,
3801                                                   tree_type);
3802       char *marker_filename = (top_dir != NULL ? zip_filename_extracted :
3803                                zip_filename_rejected);
3804       FILE *marker_file;
3805
3806       // create empty file to mark zip file as extracted or rejected
3807       if ((marker_file = fopen(marker_filename, MODE_WRITE)))
3808         fclose(marker_file);
3809
3810       free(zip_filename);
3811       free(zip_filename_extracted);
3812       free(zip_filename_rejected);
3813     }
3814   }
3815
3816   closeDirectory(dir);
3817 }
3818
3819 // forward declaration for recursive call by "LoadLevelInfoFromLevelDir()"
3820 static void LoadLevelInfoFromLevelDir(TreeInfo **, TreeInfo *, char *);
3821
3822 static boolean LoadLevelInfoFromLevelConf(TreeInfo **node_first,
3823                                           TreeInfo *node_parent,
3824                                           char *level_directory,
3825                                           char *directory_name)
3826 {
3827   char *directory_path = getPath2(level_directory, directory_name);
3828   char *filename = getPath2(directory_path, LEVELINFO_FILENAME);
3829   SetupFileHash *setup_file_hash;
3830   LevelDirTree *leveldir_new = NULL;
3831   int i;
3832
3833   // unless debugging, silently ignore directories without "levelinfo.conf"
3834   if (!options.debug && !fileExists(filename))
3835   {
3836     free(directory_path);
3837     free(filename);
3838
3839     return FALSE;
3840   }
3841
3842   setup_file_hash = loadSetupFileHash(filename);
3843
3844   if (setup_file_hash == NULL)
3845   {
3846 #if DEBUG_NO_CONFIG_FILE
3847     Debug("setup", "ignoring level directory '%s'", directory_path);
3848 #endif
3849
3850     free(directory_path);
3851     free(filename);
3852
3853     return FALSE;
3854   }
3855
3856   leveldir_new = newTreeInfo();
3857
3858   if (node_parent)
3859     setTreeInfoToDefaultsFromParent(leveldir_new, node_parent);
3860   else
3861     setTreeInfoToDefaults(leveldir_new, TREE_TYPE_LEVEL_DIR);
3862
3863   leveldir_new->subdir = getStringCopy(directory_name);
3864
3865   // set all structure fields according to the token/value pairs
3866   ldi = *leveldir_new;
3867   for (i = 0; i < NUM_LEVELINFO_TOKENS; i++)
3868     setSetupInfo(levelinfo_tokens, i,
3869                  getHashEntry(setup_file_hash, levelinfo_tokens[i].text));
3870   *leveldir_new = ldi;
3871
3872   if (strEqual(leveldir_new->name, ANONYMOUS_NAME))
3873     setString(&leveldir_new->name, leveldir_new->subdir);
3874
3875   if (leveldir_new->identifier == NULL)
3876     leveldir_new->identifier = getStringCopy(leveldir_new->subdir);
3877
3878   if (leveldir_new->name_sorting == NULL)
3879     leveldir_new->name_sorting = getStringCopy(leveldir_new->name);
3880
3881   if (node_parent == NULL)              // top level group
3882   {
3883     leveldir_new->basepath = getStringCopy(level_directory);
3884     leveldir_new->fullpath = getStringCopy(leveldir_new->subdir);
3885   }
3886   else                                  // sub level group
3887   {
3888     leveldir_new->basepath = getStringCopy(node_parent->basepath);
3889     leveldir_new->fullpath = getPath2(node_parent->fullpath, directory_name);
3890   }
3891
3892   leveldir_new->last_level =
3893     leveldir_new->first_level + leveldir_new->levels - 1;
3894
3895   leveldir_new->in_user_dir =
3896     (!strEqual(leveldir_new->basepath, options.level_directory));
3897
3898   // adjust some settings if user's private level directory was detected
3899   if (leveldir_new->sort_priority == LEVELCLASS_UNDEFINED &&
3900       leveldir_new->in_user_dir &&
3901       (strEqual(leveldir_new->subdir, getLoginName()) ||
3902        strEqual(leveldir_new->name,   getLoginName()) ||
3903        strEqual(leveldir_new->author, getRealName())))
3904   {
3905     leveldir_new->sort_priority = LEVELCLASS_PRIVATE_START;
3906     leveldir_new->readonly = FALSE;
3907   }
3908
3909   leveldir_new->user_defined =
3910     (leveldir_new->in_user_dir && IS_LEVELCLASS_PRIVATE(leveldir_new));
3911
3912   setString(&leveldir_new->class_desc, getLevelClassDescription(leveldir_new));
3913
3914   leveldir_new->handicap_level =        // set handicap to default value
3915     (leveldir_new->user_defined || !leveldir_new->handicap ?
3916      leveldir_new->last_level : leveldir_new->first_level);
3917
3918   DrawInitTextItem(leveldir_new->name);
3919
3920   pushTreeInfo(node_first, leveldir_new);
3921
3922   freeSetupFileHash(setup_file_hash);
3923
3924   if (leveldir_new->level_group)
3925   {
3926     // create node to link back to current level directory
3927     createParentTreeInfoNode(leveldir_new);
3928
3929     // recursively step into sub-directory and look for more level series
3930     LoadLevelInfoFromLevelDir(&leveldir_new->node_group,
3931                               leveldir_new, directory_path);
3932   }
3933
3934   free(directory_path);
3935   free(filename);
3936
3937   return TRUE;
3938 }
3939
3940 static void LoadLevelInfoFromLevelDir(TreeInfo **node_first,
3941                                       TreeInfo *node_parent,
3942                                       char *level_directory)
3943 {
3944   // ---------- 1st stage: process any level set zip files ----------
3945
3946   ProcessZipFilesInDirectory(level_directory, TREE_TYPE_LEVEL_DIR);
3947
3948   // ---------- 2nd stage: check for level set directories ----------
3949
3950   Directory *dir;
3951   DirectoryEntry *dir_entry;
3952   boolean valid_entry_found = FALSE;
3953
3954   if ((dir = openDirectory(level_directory)) == NULL)
3955   {
3956     Warn("cannot read level directory '%s'", level_directory);
3957
3958     return;
3959   }
3960
3961   while ((dir_entry = readDirectory(dir)) != NULL)      // loop all entries
3962   {
3963     char *directory_name = dir_entry->basename;
3964     char *directory_path = getPath2(level_directory, directory_name);
3965
3966     // skip entries for current and parent directory
3967     if (strEqual(directory_name, ".") ||
3968         strEqual(directory_name, ".."))
3969     {
3970       free(directory_path);
3971
3972       continue;
3973     }
3974
3975     // find out if directory entry is itself a directory
3976     if (!dir_entry->is_directory)                       // not a directory
3977     {
3978       free(directory_path);
3979
3980       continue;
3981     }
3982
3983     free(directory_path);
3984
3985     if (strEqual(directory_name, GRAPHICS_DIRECTORY) ||
3986         strEqual(directory_name, SOUNDS_DIRECTORY) ||
3987         strEqual(directory_name, MUSIC_DIRECTORY))
3988       continue;
3989
3990     valid_entry_found |= LoadLevelInfoFromLevelConf(node_first, node_parent,
3991                                                     level_directory,
3992                                                     directory_name);
3993   }
3994
3995   closeDirectory(dir);
3996
3997   // special case: top level directory may directly contain "levelinfo.conf"
3998   if (node_parent == NULL && !valid_entry_found)
3999   {
4000     // check if this directory directly contains a file "levelinfo.conf"
4001     valid_entry_found |= LoadLevelInfoFromLevelConf(node_first, node_parent,
4002                                                     level_directory, ".");
4003   }
4004
4005   boolean valid_entry_expected =
4006     (strEqual(level_directory, options.level_directory) ||
4007      setup.internal.create_user_levelset);
4008
4009   if (valid_entry_expected && !valid_entry_found)
4010     Warn("cannot find any valid level series in directory '%s'",
4011          level_directory);
4012 }
4013
4014 boolean AdjustGraphicsForEMC(void)
4015 {
4016   boolean settings_changed = FALSE;
4017
4018   settings_changed |= adjustTreeGraphicsForEMC(leveldir_first_all);
4019   settings_changed |= adjustTreeGraphicsForEMC(leveldir_first);
4020
4021   return settings_changed;
4022 }
4023
4024 boolean AdjustSoundsForEMC(void)
4025 {
4026   boolean settings_changed = FALSE;
4027
4028   settings_changed |= adjustTreeSoundsForEMC(leveldir_first_all);
4029   settings_changed |= adjustTreeSoundsForEMC(leveldir_first);
4030
4031   return settings_changed;
4032 }
4033
4034 void LoadLevelInfo(void)
4035 {
4036   InitUserLevelDirectory(getLoginName());
4037
4038   DrawInitTextHead("Loading level series");
4039
4040   LoadLevelInfoFromLevelDir(&leveldir_first, NULL, options.level_directory);
4041   LoadLevelInfoFromLevelDir(&leveldir_first, NULL, getUserLevelDir(NULL));
4042
4043   leveldir_first = createTopTreeInfoNode(leveldir_first);
4044
4045   /* after loading all level set information, clone the level directory tree
4046      and remove all level sets without levels (these may still contain artwork
4047      to be offered in the setup menu as "custom artwork", and are therefore
4048      checked for existing artwork in the function "LoadLevelArtworkInfo()") */
4049   leveldir_first_all = leveldir_first;
4050   cloneTree(&leveldir_first, leveldir_first_all, TRUE);
4051
4052   AdjustGraphicsForEMC();
4053   AdjustSoundsForEMC();
4054
4055   // before sorting, the first entries will be from the user directory
4056   leveldir_current = getFirstValidTreeInfoEntry(leveldir_first);
4057
4058   if (leveldir_first == NULL)
4059     Fail("cannot find any valid level series in any directory");
4060
4061   sortTreeInfo(&leveldir_first);
4062
4063 #if ENABLE_UNUSED_CODE
4064   dumpTreeInfo(leveldir_first, 0);
4065 #endif
4066 }
4067
4068 static boolean LoadArtworkInfoFromArtworkConf(TreeInfo **node_first,
4069                                               TreeInfo *node_parent,
4070                                               char *base_directory,
4071                                               char *directory_name, int type)
4072 {
4073   char *directory_path = getPath2(base_directory, directory_name);
4074   char *filename = getPath2(directory_path, ARTWORKINFO_FILENAME(type));
4075   SetupFileHash *setup_file_hash = NULL;
4076   TreeInfo *artwork_new = NULL;
4077   int i;
4078
4079   if (fileExists(filename))
4080     setup_file_hash = loadSetupFileHash(filename);
4081
4082   if (setup_file_hash == NULL)  // no config file -- look for artwork files
4083   {
4084     Directory *dir;
4085     DirectoryEntry *dir_entry;
4086     boolean valid_file_found = FALSE;
4087
4088     if ((dir = openDirectory(directory_path)) != NULL)
4089     {
4090       while ((dir_entry = readDirectory(dir)) != NULL)
4091       {
4092         if (FileIsArtworkType(dir_entry->filename, type))
4093         {
4094           valid_file_found = TRUE;
4095
4096           break;
4097         }
4098       }
4099
4100       closeDirectory(dir);
4101     }
4102
4103     if (!valid_file_found)
4104     {
4105 #if DEBUG_NO_CONFIG_FILE
4106       if (!strEqual(directory_name, "."))
4107         Debug("setup", "ignoring artwork directory '%s'", directory_path);
4108 #endif
4109
4110       free(directory_path);
4111       free(filename);
4112
4113       return FALSE;
4114     }
4115   }
4116
4117   artwork_new = newTreeInfo();
4118
4119   if (node_parent)
4120     setTreeInfoToDefaultsFromParent(artwork_new, node_parent);
4121   else
4122     setTreeInfoToDefaults(artwork_new, type);
4123
4124   artwork_new->subdir = getStringCopy(directory_name);
4125
4126   if (setup_file_hash)  // (before defining ".color" and ".class_desc")
4127   {
4128     // set all structure fields according to the token/value pairs
4129     ldi = *artwork_new;
4130     for (i = 0; i < NUM_LEVELINFO_TOKENS; i++)
4131       setSetupInfo(levelinfo_tokens, i,
4132                    getHashEntry(setup_file_hash, levelinfo_tokens[i].text));
4133     *artwork_new = ldi;
4134
4135     if (strEqual(artwork_new->name, ANONYMOUS_NAME))
4136       setString(&artwork_new->name, artwork_new->subdir);
4137
4138     if (artwork_new->identifier == NULL)
4139       artwork_new->identifier = getStringCopy(artwork_new->subdir);
4140
4141     if (artwork_new->name_sorting == NULL)
4142       artwork_new->name_sorting = getStringCopy(artwork_new->name);
4143   }
4144
4145   if (node_parent == NULL)              // top level group
4146   {
4147     artwork_new->basepath = getStringCopy(base_directory);
4148     artwork_new->fullpath = getStringCopy(artwork_new->subdir);
4149   }
4150   else                                  // sub level group
4151   {
4152     artwork_new->basepath = getStringCopy(node_parent->basepath);
4153     artwork_new->fullpath = getPath2(node_parent->fullpath, directory_name);
4154   }
4155
4156   artwork_new->in_user_dir =
4157     (!strEqual(artwork_new->basepath, OPTIONS_ARTWORK_DIRECTORY(type)));
4158
4159   setString(&artwork_new->class_desc, getLevelClassDescription(artwork_new));
4160
4161   if (setup_file_hash == NULL)  // (after determining ".user_defined")
4162   {
4163     if (strEqual(artwork_new->subdir, "."))
4164     {
4165       if (artwork_new->user_defined)
4166       {
4167         setString(&artwork_new->identifier, "private");
4168         artwork_new->sort_priority = ARTWORKCLASS_PRIVATE;
4169       }
4170       else
4171       {
4172         setString(&artwork_new->identifier, "classic");
4173         artwork_new->sort_priority = ARTWORKCLASS_CLASSICS;
4174       }
4175
4176       setString(&artwork_new->class_desc,
4177                 getLevelClassDescription(artwork_new));
4178     }
4179     else
4180     {
4181       setString(&artwork_new->identifier, artwork_new->subdir);
4182     }
4183
4184     setString(&artwork_new->name, artwork_new->identifier);
4185     setString(&artwork_new->name_sorting, artwork_new->name);
4186   }
4187
4188   pushTreeInfo(node_first, artwork_new);
4189
4190   freeSetupFileHash(setup_file_hash);
4191
4192   free(directory_path);
4193   free(filename);
4194
4195   return TRUE;
4196 }
4197
4198 static void LoadArtworkInfoFromArtworkDir(TreeInfo **node_first,
4199                                           TreeInfo *node_parent,
4200                                           char *base_directory, int type)
4201 {
4202   // ---------- 1st stage: process any artwork set zip files ----------
4203
4204   ProcessZipFilesInDirectory(base_directory, type);
4205
4206   // ---------- 2nd stage: check for artwork set directories ----------
4207
4208   Directory *dir;
4209   DirectoryEntry *dir_entry;
4210   boolean valid_entry_found = FALSE;
4211
4212   if ((dir = openDirectory(base_directory)) == NULL)
4213   {
4214     // display error if directory is main "options.graphics_directory" etc.
4215     if (base_directory == OPTIONS_ARTWORK_DIRECTORY(type))
4216       Warn("cannot read directory '%s'", base_directory);
4217
4218     return;
4219   }
4220
4221   while ((dir_entry = readDirectory(dir)) != NULL)      // loop all entries
4222   {
4223     char *directory_name = dir_entry->basename;
4224     char *directory_path = getPath2(base_directory, directory_name);
4225
4226     // skip directory entries for current and parent directory
4227     if (strEqual(directory_name, ".") ||
4228         strEqual(directory_name, ".."))
4229     {
4230       free(directory_path);
4231
4232       continue;
4233     }
4234
4235     // skip directory entries which are not a directory
4236     if (!dir_entry->is_directory)                       // not a directory
4237     {
4238       free(directory_path);
4239
4240       continue;
4241     }
4242
4243     free(directory_path);
4244
4245     // check if this directory contains artwork with or without config file
4246     valid_entry_found |= LoadArtworkInfoFromArtworkConf(node_first, node_parent,
4247                                                         base_directory,
4248                                                         directory_name, type);
4249   }
4250
4251   closeDirectory(dir);
4252
4253   // check if this directory directly contains artwork itself
4254   valid_entry_found |= LoadArtworkInfoFromArtworkConf(node_first, node_parent,
4255                                                       base_directory, ".",
4256                                                       type);
4257   if (!valid_entry_found)
4258     Warn("cannot find any valid artwork in directory '%s'", base_directory);
4259 }
4260
4261 static TreeInfo *getDummyArtworkInfo(int type)
4262 {
4263   // this is only needed when there is completely no artwork available
4264   TreeInfo *artwork_new = newTreeInfo();
4265
4266   setTreeInfoToDefaults(artwork_new, type);
4267
4268   setString(&artwork_new->subdir,   UNDEFINED_FILENAME);
4269   setString(&artwork_new->fullpath, UNDEFINED_FILENAME);
4270   setString(&artwork_new->basepath, UNDEFINED_FILENAME);
4271
4272   setString(&artwork_new->identifier,   UNDEFINED_FILENAME);
4273   setString(&artwork_new->name,         UNDEFINED_FILENAME);
4274   setString(&artwork_new->name_sorting, UNDEFINED_FILENAME);
4275
4276   return artwork_new;
4277 }
4278
4279 void SetCurrentArtwork(int type)
4280 {
4281   ArtworkDirTree **current_ptr = ARTWORK_CURRENT_PTR(artwork, type);
4282   ArtworkDirTree *first_node = ARTWORK_FIRST_NODE(artwork, type);
4283   char *setup_set = SETUP_ARTWORK_SET(setup, type);
4284   char *default_subdir = ARTWORK_DEFAULT_SUBDIR(type);
4285
4286   // set current artwork to artwork configured in setup menu
4287   *current_ptr = getTreeInfoFromIdentifier(first_node, setup_set);
4288
4289   // if not found, set current artwork to default artwork
4290   if (*current_ptr == NULL)
4291     *current_ptr = getTreeInfoFromIdentifier(first_node, default_subdir);
4292
4293   // if not found, set current artwork to first artwork in tree
4294   if (*current_ptr == NULL)
4295     *current_ptr = getFirstValidTreeInfoEntry(first_node);
4296 }
4297
4298 void ChangeCurrentArtworkIfNeeded(int type)
4299 {
4300   char *current_identifier = ARTWORK_CURRENT_IDENTIFIER(artwork, type);
4301   char *setup_set = SETUP_ARTWORK_SET(setup, type);
4302
4303   if (!strEqual(current_identifier, setup_set))
4304     SetCurrentArtwork(type);
4305 }
4306
4307 void LoadArtworkInfo(void)
4308 {
4309   LoadArtworkInfoCache();
4310
4311   DrawInitTextHead("Looking for custom artwork");
4312
4313   LoadArtworkInfoFromArtworkDir(&artwork.gfx_first, NULL,
4314                                 options.graphics_directory,
4315                                 TREE_TYPE_GRAPHICS_DIR);
4316   LoadArtworkInfoFromArtworkDir(&artwork.gfx_first, NULL,
4317                                 getUserGraphicsDir(),
4318                                 TREE_TYPE_GRAPHICS_DIR);
4319
4320   LoadArtworkInfoFromArtworkDir(&artwork.snd_first, NULL,
4321                                 options.sounds_directory,
4322                                 TREE_TYPE_SOUNDS_DIR);
4323   LoadArtworkInfoFromArtworkDir(&artwork.snd_first, NULL,
4324                                 getUserSoundsDir(),
4325                                 TREE_TYPE_SOUNDS_DIR);
4326
4327   LoadArtworkInfoFromArtworkDir(&artwork.mus_first, NULL,
4328                                 options.music_directory,
4329                                 TREE_TYPE_MUSIC_DIR);
4330   LoadArtworkInfoFromArtworkDir(&artwork.mus_first, NULL,
4331                                 getUserMusicDir(),
4332                                 TREE_TYPE_MUSIC_DIR);
4333
4334   if (artwork.gfx_first == NULL)
4335     artwork.gfx_first = getDummyArtworkInfo(TREE_TYPE_GRAPHICS_DIR);
4336   if (artwork.snd_first == NULL)
4337     artwork.snd_first = getDummyArtworkInfo(TREE_TYPE_SOUNDS_DIR);
4338   if (artwork.mus_first == NULL)
4339     artwork.mus_first = getDummyArtworkInfo(TREE_TYPE_MUSIC_DIR);
4340
4341   // before sorting, the first entries will be from the user directory
4342   SetCurrentArtwork(ARTWORK_TYPE_GRAPHICS);
4343   SetCurrentArtwork(ARTWORK_TYPE_SOUNDS);
4344   SetCurrentArtwork(ARTWORK_TYPE_MUSIC);
4345
4346   artwork.gfx_current_identifier = artwork.gfx_current->identifier;
4347   artwork.snd_current_identifier = artwork.snd_current->identifier;
4348   artwork.mus_current_identifier = artwork.mus_current->identifier;
4349
4350 #if ENABLE_UNUSED_CODE
4351   Debug("setup:LoadArtworkInfo", "graphics set == %s",
4352         artwork.gfx_current_identifier);
4353   Debug("setup:LoadArtworkInfo", "sounds set == %s",
4354         artwork.snd_current_identifier);
4355   Debug("setup:LoadArtworkInfo", "music set == %s",
4356         artwork.mus_current_identifier);
4357 #endif
4358
4359   sortTreeInfo(&artwork.gfx_first);
4360   sortTreeInfo(&artwork.snd_first);
4361   sortTreeInfo(&artwork.mus_first);
4362
4363 #if ENABLE_UNUSED_CODE
4364   dumpTreeInfo(artwork.gfx_first, 0);
4365   dumpTreeInfo(artwork.snd_first, 0);
4366   dumpTreeInfo(artwork.mus_first, 0);
4367 #endif
4368 }
4369
4370 static void MoveArtworkInfoIntoSubTree(ArtworkDirTree **artwork_node)
4371 {
4372   ArtworkDirTree *artwork_new = newTreeInfo();
4373   char *top_node_name = "standalone artwork";
4374
4375   setTreeInfoToDefaults(artwork_new, (*artwork_node)->type);
4376
4377   artwork_new->level_group = TRUE;
4378
4379   setString(&artwork_new->identifier,   top_node_name);
4380   setString(&artwork_new->name,         top_node_name);
4381   setString(&artwork_new->name_sorting, top_node_name);
4382
4383   // create node to link back to current custom artwork directory
4384   createParentTreeInfoNode(artwork_new);
4385
4386   // move existing custom artwork tree into newly created sub-tree
4387   artwork_new->node_group->next = *artwork_node;
4388
4389   // change custom artwork tree to contain only newly created node
4390   *artwork_node = artwork_new;
4391 }
4392
4393 static void LoadArtworkInfoFromLevelInfoExt(ArtworkDirTree **artwork_node,
4394                                             ArtworkDirTree *node_parent,
4395                                             LevelDirTree *level_node,
4396                                             boolean empty_level_set_mode)
4397 {
4398   int type = (*artwork_node)->type;
4399
4400   // recursively check all level directories for artwork sub-directories
4401
4402   while (level_node)
4403   {
4404     boolean empty_level_set = (level_node->levels == 0);
4405
4406     // check all tree entries for artwork, but skip parent link entries
4407     if (!level_node->parent_link && empty_level_set == empty_level_set_mode)
4408     {
4409       TreeInfo *artwork_new = getArtworkInfoCacheEntry(level_node, type);
4410       boolean cached = (artwork_new != NULL);
4411
4412       if (cached)
4413       {
4414         pushTreeInfo(artwork_node, artwork_new);
4415       }
4416       else
4417       {
4418         TreeInfo *topnode_last = *artwork_node;
4419         char *path = getPath2(getLevelDirFromTreeInfo(level_node),
4420                               ARTWORK_DIRECTORY(type));
4421
4422         LoadArtworkInfoFromArtworkDir(artwork_node, NULL, path, type);
4423
4424         if (topnode_last != *artwork_node)      // check for newly added node
4425         {
4426           artwork_new = *artwork_node;
4427
4428           setString(&artwork_new->identifier,   level_node->subdir);
4429           setString(&artwork_new->name,         level_node->name);
4430           setString(&artwork_new->name_sorting, level_node->name_sorting);
4431
4432           artwork_new->sort_priority = level_node->sort_priority;
4433           artwork_new->in_user_dir = level_node->in_user_dir;
4434
4435           update_artworkinfo_cache = TRUE;
4436         }
4437
4438         free(path);
4439       }
4440
4441       // insert artwork info (from old cache or filesystem) into new cache
4442       if (artwork_new != NULL)
4443         setArtworkInfoCacheEntry(artwork_new, level_node, type);
4444     }
4445
4446     DrawInitTextItem(level_node->name);
4447
4448     if (level_node->node_group != NULL)
4449     {
4450       TreeInfo *artwork_new = newTreeInfo();
4451
4452       if (node_parent)
4453         setTreeInfoToDefaultsFromParent(artwork_new, node_parent);
4454       else
4455         setTreeInfoToDefaults(artwork_new, type);
4456
4457       artwork_new->level_group = TRUE;
4458
4459       setString(&artwork_new->identifier,   level_node->subdir);
4460
4461       if (node_parent == NULL)          // check for top tree node
4462       {
4463         char *top_node_name = (empty_level_set_mode ?
4464                                "artwork for certain level sets" :
4465                                "artwork included in level sets");
4466
4467         setString(&artwork_new->name,         top_node_name);
4468         setString(&artwork_new->name_sorting, top_node_name);
4469       }
4470       else
4471       {
4472         setString(&artwork_new->name,         level_node->name);
4473         setString(&artwork_new->name_sorting, level_node->name_sorting);
4474       }
4475
4476       pushTreeInfo(artwork_node, artwork_new);
4477
4478       // create node to link back to current custom artwork directory
4479       createParentTreeInfoNode(artwork_new);
4480
4481       // recursively step into sub-directory and look for more custom artwork
4482       LoadArtworkInfoFromLevelInfoExt(&artwork_new->node_group, artwork_new,
4483                                       level_node->node_group,
4484                                       empty_level_set_mode);
4485
4486       // if sub-tree has no custom artwork at all, remove it
4487       if (artwork_new->node_group->next == NULL)
4488         removeTreeInfo(artwork_node);
4489     }
4490
4491     level_node = level_node->next;
4492   }
4493 }
4494
4495 static void LoadArtworkInfoFromLevelInfo(ArtworkDirTree **artwork_node)
4496 {
4497   // move peviously loaded artwork tree into separate sub-tree
4498   MoveArtworkInfoIntoSubTree(artwork_node);
4499
4500   // load artwork from level sets into separate sub-trees
4501   LoadArtworkInfoFromLevelInfoExt(artwork_node, NULL, leveldir_first_all, TRUE);
4502   LoadArtworkInfoFromLevelInfoExt(artwork_node, NULL, leveldir_first_all, FALSE);
4503
4504   // add top tree node over all sub-trees and set parent links
4505   *artwork_node = addTopTreeInfoNode(*artwork_node);
4506 }
4507
4508 void LoadLevelArtworkInfo(void)
4509 {
4510   print_timestamp_init("LoadLevelArtworkInfo");
4511
4512   DrawInitTextHead("Looking for custom level artwork");
4513
4514   print_timestamp_time("DrawTimeText");
4515
4516   LoadArtworkInfoFromLevelInfo(&artwork.gfx_first);
4517   print_timestamp_time("LoadArtworkInfoFromLevelInfo (gfx)");
4518   LoadArtworkInfoFromLevelInfo(&artwork.snd_first);
4519   print_timestamp_time("LoadArtworkInfoFromLevelInfo (snd)");
4520   LoadArtworkInfoFromLevelInfo(&artwork.mus_first);
4521   print_timestamp_time("LoadArtworkInfoFromLevelInfo (mus)");
4522
4523   SaveArtworkInfoCache();
4524
4525   print_timestamp_time("SaveArtworkInfoCache");
4526
4527   // needed for reloading level artwork not known at ealier stage
4528   ChangeCurrentArtworkIfNeeded(ARTWORK_TYPE_GRAPHICS);
4529   ChangeCurrentArtworkIfNeeded(ARTWORK_TYPE_SOUNDS);
4530   ChangeCurrentArtworkIfNeeded(ARTWORK_TYPE_MUSIC);
4531
4532   print_timestamp_time("getTreeInfoFromIdentifier");
4533
4534   sortTreeInfo(&artwork.gfx_first);
4535   sortTreeInfo(&artwork.snd_first);
4536   sortTreeInfo(&artwork.mus_first);
4537
4538   print_timestamp_time("sortTreeInfo");
4539
4540 #if ENABLE_UNUSED_CODE
4541   dumpTreeInfo(artwork.gfx_first, 0);
4542   dumpTreeInfo(artwork.snd_first, 0);
4543   dumpTreeInfo(artwork.mus_first, 0);
4544 #endif
4545
4546   print_timestamp_done("LoadLevelArtworkInfo");
4547 }
4548
4549 static boolean AddTreeSetToTreeInfoExt(TreeInfo *tree_node_old, char *tree_dir,
4550                                        char *tree_subdir_new, int type)
4551 {
4552   if (tree_node_old == NULL)
4553   {
4554     if (type == TREE_TYPE_LEVEL_DIR)
4555     {
4556       // get level info tree node of personal user level set
4557       tree_node_old = getTreeInfoFromIdentifier(leveldir_first, getLoginName());
4558
4559       // this may happen if "setup.internal.create_user_levelset" is FALSE
4560       // or if file "levelinfo.conf" is missing in personal user level set
4561       if (tree_node_old == NULL)
4562         tree_node_old = leveldir_first->node_group;
4563     }
4564     else
4565     {
4566       // get artwork info tree node of first artwork set
4567       tree_node_old = ARTWORK_FIRST_NODE(artwork, type);
4568     }
4569   }
4570
4571   if (tree_dir == NULL)
4572     tree_dir = TREE_USERDIR(type);
4573
4574   if (tree_node_old   == NULL ||
4575       tree_dir        == NULL ||
4576       tree_subdir_new == NULL)          // should not happen
4577     return FALSE;
4578
4579   int draw_deactivation_mask = GetDrawDeactivationMask();
4580
4581   // override draw deactivation mask (temporarily disable drawing)
4582   SetDrawDeactivationMask(REDRAW_ALL);
4583
4584   if (type == TREE_TYPE_LEVEL_DIR)
4585   {
4586     // load new level set config and add it next to first user level set
4587     LoadLevelInfoFromLevelConf(&tree_node_old->next,
4588                                tree_node_old->node_parent,
4589                                tree_dir, tree_subdir_new);
4590   }
4591   else
4592   {
4593     // load new artwork set config and add it next to first artwork set
4594     LoadArtworkInfoFromArtworkConf(&tree_node_old->next,
4595                                    tree_node_old->node_parent,
4596                                    tree_dir, tree_subdir_new, type);
4597   }
4598
4599   // set draw deactivation mask to previous value
4600   SetDrawDeactivationMask(draw_deactivation_mask);
4601
4602   // get first node of level or artwork info tree
4603   TreeInfo **tree_node_first = TREE_FIRST_NODE_PTR(type);
4604
4605   // get tree info node of newly added level or artwork set
4606   TreeInfo *tree_node_new = getTreeInfoFromIdentifier(*tree_node_first,
4607                                                       tree_subdir_new);
4608
4609   // if not found, check if added node is level group or artwork group
4610   if (tree_node_new == NULL)
4611     tree_node_new = getTreeInfoFromIdentifierExt(*tree_node_first,
4612                                                  tree_subdir_new,
4613                                                  TREE_NODE_TYPE_GROUP);
4614
4615   if (tree_node_new == NULL)            // should not happen
4616     return FALSE;
4617
4618   // correct top link and parent node link of newly created tree node
4619   tree_node_new->node_top    = tree_node_old->node_top;
4620   tree_node_new->node_parent = tree_node_old->node_parent;
4621
4622   // sort tree info to adjust position of newly added tree set
4623   sortTreeInfo(tree_node_first);
4624
4625   return TRUE;
4626 }
4627
4628 void AddTreeSetToTreeInfo(TreeInfo *tree_node, char *tree_dir,
4629                           char *tree_subdir_new, int type)
4630 {
4631   if (!AddTreeSetToTreeInfoExt(tree_node, tree_dir, tree_subdir_new, type))
4632     Fail("internal tree info structure corrupted -- aborting");
4633 }
4634
4635 void AddUserLevelSetToLevelInfo(char *level_subdir_new)
4636 {
4637   AddTreeSetToTreeInfo(NULL, NULL, level_subdir_new, TREE_TYPE_LEVEL_DIR);
4638 }
4639
4640 char *getArtworkIdentifierForUserLevelSet(int type)
4641 {
4642   char *classic_artwork_set = getClassicArtworkSet(type);
4643
4644   // check for custom artwork configured in "levelinfo.conf"
4645   char *leveldir_artwork_set =
4646     *LEVELDIR_ARTWORK_SET_PTR(leveldir_current, type);
4647   boolean has_leveldir_artwork_set =
4648     (leveldir_artwork_set != NULL && !strEqual(leveldir_artwork_set,
4649                                                classic_artwork_set));
4650
4651   // check for custom artwork in sub-directory "graphics" etc.
4652   TreeInfo *artwork_first_node = ARTWORK_FIRST_NODE(artwork, type);
4653   char *leveldir_identifier = leveldir_current->identifier;
4654   boolean has_artwork_subdir =
4655     (getTreeInfoFromIdentifier(artwork_first_node,
4656                                leveldir_identifier) != NULL);
4657
4658   return (has_leveldir_artwork_set ? leveldir_artwork_set :
4659           has_artwork_subdir       ? leveldir_identifier :
4660           classic_artwork_set);
4661 }
4662
4663 TreeInfo *getArtworkTreeInfoForUserLevelSet(int type)
4664 {
4665   char *artwork_set = getArtworkIdentifierForUserLevelSet(type);
4666   TreeInfo *artwork_first_node = ARTWORK_FIRST_NODE(artwork, type);
4667   TreeInfo *ti = getTreeInfoFromIdentifier(artwork_first_node, artwork_set);
4668
4669   if (ti == NULL)
4670   {
4671     ti = getTreeInfoFromIdentifier(artwork_first_node,
4672                                    ARTWORK_DEFAULT_SUBDIR(type));
4673     if (ti == NULL)
4674       Fail("cannot find default graphics -- should not happen");
4675   }
4676
4677   return ti;
4678 }
4679
4680 boolean checkIfCustomArtworkExistsForCurrentLevelSet(void)
4681 {
4682   char *graphics_set =
4683     getArtworkIdentifierForUserLevelSet(ARTWORK_TYPE_GRAPHICS);
4684   char *sounds_set =
4685     getArtworkIdentifierForUserLevelSet(ARTWORK_TYPE_SOUNDS);
4686   char *music_set =
4687     getArtworkIdentifierForUserLevelSet(ARTWORK_TYPE_MUSIC);
4688
4689   return (!strEqual(graphics_set, GFX_CLASSIC_SUBDIR) ||
4690           !strEqual(sounds_set,   SND_CLASSIC_SUBDIR) ||
4691           !strEqual(music_set,    MUS_CLASSIC_SUBDIR));
4692 }
4693
4694 boolean UpdateUserLevelSet(char *level_subdir, char *level_name,
4695                            char *level_author, int num_levels)
4696 {
4697   char *filename = getPath2(getUserLevelDir(level_subdir), LEVELINFO_FILENAME);
4698   char *filename_tmp = getStringCat2(filename, ".tmp");
4699   FILE *file = NULL;
4700   FILE *file_tmp = NULL;
4701   char line[MAX_LINE_LEN];
4702   boolean success = FALSE;
4703   LevelDirTree *leveldir = getTreeInfoFromIdentifier(leveldir_first,
4704                                                      level_subdir);
4705   // update values in level directory tree
4706
4707   if (level_name != NULL)
4708     setString(&leveldir->name, level_name);
4709
4710   if (level_author != NULL)
4711     setString(&leveldir->author, level_author);
4712
4713   if (num_levels != -1)
4714     leveldir->levels = num_levels;
4715
4716   // update values that depend on other values
4717
4718   setString(&leveldir->name_sorting, leveldir->name);
4719
4720   leveldir->last_level = leveldir->first_level + leveldir->levels - 1;
4721
4722   // sort order of level sets may have changed
4723   sortTreeInfo(&leveldir_first);
4724
4725   if ((file     = fopen(filename,     MODE_READ)) &&
4726       (file_tmp = fopen(filename_tmp, MODE_WRITE)))
4727   {
4728     while (fgets(line, MAX_LINE_LEN, file))
4729     {
4730       if (strPrefix(line, "name:") && level_name != NULL)
4731         fprintf(file_tmp, "%-32s%s\n", "name:", level_name);
4732       else if (strPrefix(line, "author:") && level_author != NULL)
4733         fprintf(file_tmp, "%-32s%s\n", "author:", level_author);
4734       else if (strPrefix(line, "levels:") && num_levels != -1)
4735         fprintf(file_tmp, "%-32s%d\n", "levels:", num_levels);
4736       else
4737         fputs(line, file_tmp);
4738     }
4739
4740     success = TRUE;
4741   }
4742
4743   if (file)
4744     fclose(file);
4745
4746   if (file_tmp)
4747     fclose(file_tmp);
4748
4749   if (success)
4750     success = (rename(filename_tmp, filename) == 0);
4751
4752   free(filename);
4753   free(filename_tmp);
4754
4755   return success;
4756 }
4757
4758 boolean CreateUserLevelSet(char *level_subdir, char *level_name,
4759                            char *level_author, int num_levels,
4760                            boolean use_artwork_set)
4761 {
4762   LevelDirTree *level_info;
4763   char *filename;
4764   FILE *file;
4765   int i;
4766
4767   // create user level sub-directory, if needed
4768   createDirectory(getUserLevelDir(level_subdir), "user level");
4769
4770   filename = getPath2(getUserLevelDir(level_subdir), LEVELINFO_FILENAME);
4771
4772   if (!(file = fopen(filename, MODE_WRITE)))
4773   {
4774     Warn("cannot write level info file '%s'", filename);
4775
4776     free(filename);
4777
4778     return FALSE;
4779   }
4780
4781   level_info = newTreeInfo();
4782
4783   // always start with reliable default values
4784   setTreeInfoToDefaults(level_info, TREE_TYPE_LEVEL_DIR);
4785
4786   setString(&level_info->name, level_name);
4787   setString(&level_info->author, level_author);
4788   level_info->levels = num_levels;
4789   level_info->first_level = 1;
4790   level_info->sort_priority = LEVELCLASS_PRIVATE_START;
4791   level_info->readonly = FALSE;
4792
4793   if (use_artwork_set)
4794   {
4795     level_info->graphics_set =
4796       getStringCopy(getArtworkIdentifierForUserLevelSet(ARTWORK_TYPE_GRAPHICS));
4797     level_info->sounds_set =
4798       getStringCopy(getArtworkIdentifierForUserLevelSet(ARTWORK_TYPE_SOUNDS));
4799     level_info->music_set =
4800       getStringCopy(getArtworkIdentifierForUserLevelSet(ARTWORK_TYPE_MUSIC));
4801   }
4802
4803   token_value_position = TOKEN_VALUE_POSITION_SHORT;
4804
4805   fprintFileHeader(file, LEVELINFO_FILENAME);
4806
4807   ldi = *level_info;
4808   for (i = 0; i < NUM_LEVELINFO_TOKENS; i++)
4809   {
4810     if (i == LEVELINFO_TOKEN_NAME ||
4811         i == LEVELINFO_TOKEN_AUTHOR ||
4812         i == LEVELINFO_TOKEN_LEVELS ||
4813         i == LEVELINFO_TOKEN_FIRST_LEVEL ||
4814         i == LEVELINFO_TOKEN_SORT_PRIORITY ||
4815         i == LEVELINFO_TOKEN_READONLY ||
4816         (use_artwork_set && (i == LEVELINFO_TOKEN_GRAPHICS_SET ||
4817                              i == LEVELINFO_TOKEN_SOUNDS_SET ||
4818                              i == LEVELINFO_TOKEN_MUSIC_SET)))
4819       fprintf(file, "%s\n", getSetupLine(levelinfo_tokens, "", i));
4820
4821     // just to make things nicer :)
4822     if (i == LEVELINFO_TOKEN_AUTHOR ||
4823         i == LEVELINFO_TOKEN_FIRST_LEVEL ||
4824         (use_artwork_set && i == LEVELINFO_TOKEN_READONLY))
4825       fprintf(file, "\n");      
4826   }
4827
4828   token_value_position = TOKEN_VALUE_POSITION_DEFAULT;
4829
4830   fclose(file);
4831
4832   SetFilePermissions(filename, PERMS_PRIVATE);
4833
4834   freeTreeInfo(level_info);
4835   free(filename);
4836
4837   return TRUE;
4838 }
4839
4840 static void SaveUserLevelInfo(void)
4841 {
4842   CreateUserLevelSet(getLoginName(), getLoginName(), getRealName(), 100, FALSE);
4843 }
4844
4845 char *getSetupValue(int type, void *value)
4846 {
4847   static char value_string[MAX_LINE_LEN];
4848
4849   if (value == NULL)
4850     return NULL;
4851
4852   switch (type)
4853   {
4854     case TYPE_BOOLEAN:
4855       strcpy(value_string, (*(boolean *)value ? "true" : "false"));
4856       break;
4857
4858     case TYPE_SWITCH:
4859       strcpy(value_string, (*(boolean *)value ? "on" : "off"));
4860       break;
4861
4862     case TYPE_SWITCH3:
4863       strcpy(value_string, (*(int *)value == AUTO  ? "auto" :
4864                             *(int *)value == FALSE ? "off" : "on"));
4865       break;
4866
4867     case TYPE_YES_NO:
4868       strcpy(value_string, (*(boolean *)value ? "yes" : "no"));
4869       break;
4870
4871     case TYPE_YES_NO_AUTO:
4872       strcpy(value_string, (*(int *)value == AUTO  ? "auto" :
4873                             *(int *)value == FALSE ? "no" : "yes"));
4874       break;
4875
4876     case TYPE_ECS_AGA:
4877       strcpy(value_string, (*(boolean *)value ? "AGA" : "ECS"));
4878       break;
4879
4880     case TYPE_KEY:
4881       strcpy(value_string, getKeyNameFromKey(*(Key *)value));
4882       break;
4883
4884     case TYPE_KEY_X11:
4885       strcpy(value_string, getX11KeyNameFromKey(*(Key *)value));
4886       break;
4887
4888     case TYPE_INTEGER:
4889       sprintf(value_string, "%d", *(int *)value);
4890       break;
4891
4892     case TYPE_STRING:
4893       if (*(char **)value == NULL)
4894         return NULL;
4895
4896       strcpy(value_string, *(char **)value);
4897       break;
4898
4899     case TYPE_PLAYER:
4900       sprintf(value_string, "player_%d", *(int *)value + 1);
4901       break;
4902
4903     default:
4904       value_string[0] = '\0';
4905       break;
4906   }
4907
4908   if (type & TYPE_GHOSTED)
4909     strcpy(value_string, "n/a");
4910
4911   return value_string;
4912 }
4913
4914 char *getSetupLine(struct TokenInfo *token_info, char *prefix, int token_nr)
4915 {
4916   int i;
4917   char *line;
4918   static char token_string[MAX_LINE_LEN];
4919   int token_type = token_info[token_nr].type;
4920   void *setup_value = token_info[token_nr].value;
4921   char *token_text = token_info[token_nr].text;
4922   char *value_string = getSetupValue(token_type, setup_value);
4923
4924   // build complete token string
4925   sprintf(token_string, "%s%s", prefix, token_text);
4926
4927   // build setup entry line
4928   line = getFormattedSetupEntry(token_string, value_string);
4929
4930   if (token_type == TYPE_KEY_X11)
4931   {
4932     Key key = *(Key *)setup_value;
4933     char *keyname = getKeyNameFromKey(key);
4934
4935     // add comment, if useful
4936     if (!strEqual(keyname, "(undefined)") &&
4937         !strEqual(keyname, "(unknown)"))
4938     {
4939       // add at least one whitespace
4940       strcat(line, " ");
4941       for (i = strlen(line); i < token_comment_position; i++)
4942         strcat(line, " ");
4943
4944       strcat(line, "# ");
4945       strcat(line, keyname);
4946     }
4947   }
4948
4949   return line;
4950 }
4951
4952 static void InitLastPlayedLevels_ParentNode(void)
4953 {
4954   LevelDirTree **leveldir_top = &leveldir_first->node_group->next;
4955   LevelDirTree *leveldir_new = NULL;
4956
4957   // check if parent node for last played levels already exists
4958   if (strEqual((*leveldir_top)->identifier, TOKEN_STR_LAST_LEVEL_SERIES))
4959     return;
4960
4961   leveldir_new = newTreeInfo();
4962
4963   setTreeInfoToDefaultsFromParent(leveldir_new, leveldir_first);
4964
4965   leveldir_new->level_group = TRUE;
4966   leveldir_new->sort_priority = LEVELCLASS_LAST_PLAYED_LEVEL;
4967
4968   setString(&leveldir_new->identifier, TOKEN_STR_LAST_LEVEL_SERIES);
4969   setString(&leveldir_new->name, "<< (last played level sets)");
4970   setString(&leveldir_new->name_sorting, leveldir_new->name);
4971
4972   pushTreeInfo(leveldir_top, leveldir_new);
4973
4974   // create node to link back to current level directory
4975   createParentTreeInfoNode(leveldir_new);
4976 }
4977
4978 void UpdateLastPlayedLevels_TreeInfo(void)
4979 {
4980   char **last_level_series = setup.level_setup.last_level_series;
4981   LevelDirTree *leveldir_last;
4982   TreeInfo **node_new = NULL;
4983   int i;
4984
4985   if (last_level_series[0] == NULL)
4986     return;
4987
4988   InitLastPlayedLevels_ParentNode();
4989
4990   leveldir_last = getTreeInfoFromIdentifierExt(leveldir_first,
4991                                                TOKEN_STR_LAST_LEVEL_SERIES,
4992                                                TREE_NODE_TYPE_GROUP);
4993   if (leveldir_last == NULL)
4994     return;
4995
4996   node_new = &leveldir_last->node_group->next;
4997
4998   freeTreeInfo(*node_new);
4999
5000   *node_new = NULL;
5001
5002   for (i = 0; last_level_series[i] != NULL; i++)
5003   {
5004     LevelDirTree *node_last = getTreeInfoFromIdentifier(leveldir_first,
5005                                                         last_level_series[i]);
5006     if (node_last == NULL)
5007       continue;
5008
5009     *node_new = getTreeInfoCopy(node_last);     // copy complete node
5010
5011     (*node_new)->node_top = &leveldir_first;    // correct top node link
5012     (*node_new)->node_parent = leveldir_last;   // correct parent node link
5013
5014     (*node_new)->is_copy = TRUE;                // mark entry as node copy
5015
5016     (*node_new)->node_group = NULL;
5017     (*node_new)->next = NULL;
5018
5019     (*node_new)->cl_first = -1;                 // force setting tree cursor
5020
5021     node_new = &((*node_new)->next);
5022   }
5023 }
5024
5025 static void UpdateLastPlayedLevels_List(void)
5026 {
5027   char **last_level_series = setup.level_setup.last_level_series;
5028   int pos = MAX_LEVELDIR_HISTORY - 1;
5029   int i;
5030
5031   // search for potentially already existing entry in list of level sets
5032   for (i = 0; last_level_series[i] != NULL; i++)
5033     if (strEqual(last_level_series[i], leveldir_current->identifier))
5034       pos = i;
5035
5036   // move list of level sets one entry down (using potentially free entry)
5037   for (i = pos; i > 0; i--)
5038     setString(&last_level_series[i], last_level_series[i - 1]);
5039
5040   // put last played level set at top position
5041   setString(&last_level_series[0], leveldir_current->identifier);
5042 }
5043
5044 #define LAST_PLAYED_MODE_SET                    1
5045 #define LAST_PLAYED_MODE_SET_FORCED             2
5046 #define LAST_PLAYED_MODE_GET                    3
5047
5048 static TreeInfo *StoreOrRestoreLastPlayedLevels(TreeInfo *node, int mode)
5049 {
5050   static char *identifier = NULL;
5051
5052   if (mode == LAST_PLAYED_MODE_SET)
5053   {
5054     setString(&identifier, (node && node->is_copy ? node->identifier : NULL));
5055   }
5056   else if (mode == LAST_PLAYED_MODE_SET_FORCED)
5057   {
5058     setString(&identifier, (node ? node->identifier : NULL));
5059   }
5060   else if (mode == LAST_PLAYED_MODE_GET)
5061   {
5062     TreeInfo *node_new = getTreeInfoFromIdentifierExt(leveldir_first,
5063                                                       identifier,
5064                                                       TREE_NODE_TYPE_COPY);
5065     return (node_new != NULL ? node_new : node);
5066   }
5067
5068   return NULL;          // not used
5069 }
5070
5071 void StoreLastPlayedLevels(TreeInfo *node)
5072 {
5073   StoreOrRestoreLastPlayedLevels(node, LAST_PLAYED_MODE_SET);
5074 }
5075
5076 void ForcedStoreLastPlayedLevels(TreeInfo *node)
5077 {
5078   StoreOrRestoreLastPlayedLevels(node, LAST_PLAYED_MODE_SET_FORCED);
5079 }
5080
5081 void RestoreLastPlayedLevels(TreeInfo **node)
5082 {
5083   *node = StoreOrRestoreLastPlayedLevels(*node, LAST_PLAYED_MODE_GET);
5084 }
5085
5086 boolean CheckLastPlayedLevels(void)
5087 {
5088   return (StoreOrRestoreLastPlayedLevels(NULL, LAST_PLAYED_MODE_GET) != NULL);
5089 }
5090
5091 void LoadLevelSetup_LastSeries(void)
5092 {
5093   // --------------------------------------------------------------------------
5094   // ~/.<program>/levelsetup.conf
5095   // --------------------------------------------------------------------------
5096
5097   char *filename = getPath2(getSetupDir(), LEVELSETUP_FILENAME);
5098   SetupFileHash *level_setup_hash = NULL;
5099   int pos = 0;
5100   int i;
5101
5102   // always start with reliable default values
5103   leveldir_current = getFirstValidTreeInfoEntry(leveldir_first);
5104
5105   // start with empty history of last played level sets
5106   setString(&setup.level_setup.last_level_series[0], NULL);
5107
5108   if (!strEqual(DEFAULT_LEVELSET, UNDEFINED_LEVELSET))
5109   {
5110     leveldir_current = getTreeInfoFromIdentifier(leveldir_first,
5111                                                  DEFAULT_LEVELSET);
5112     if (leveldir_current == NULL)
5113       leveldir_current = getFirstValidTreeInfoEntry(leveldir_first);
5114   }
5115
5116   if ((level_setup_hash = loadSetupFileHash(filename)))
5117   {
5118     char *last_level_series =
5119       getHashEntry(level_setup_hash, TOKEN_STR_LAST_LEVEL_SERIES);
5120
5121     leveldir_current = getTreeInfoFromIdentifier(leveldir_first,
5122                                                  last_level_series);
5123     if (leveldir_current == NULL)
5124       leveldir_current = getFirstValidTreeInfoEntry(leveldir_first);
5125
5126     char *last_played_menu_used =
5127       getHashEntry(level_setup_hash, TOKEN_STR_LAST_PLAYED_MENU_USED);
5128
5129     // store if last level set was selected from "last played" menu
5130     if (strEqual(last_played_menu_used, "true"))
5131       ForcedStoreLastPlayedLevels(leveldir_current);
5132
5133     for (i = 0; i < MAX_LEVELDIR_HISTORY; i++)
5134     {
5135       char token[strlen(TOKEN_STR_LAST_LEVEL_SERIES) + 10];
5136       LevelDirTree *leveldir_last;
5137
5138       sprintf(token, "%s.%03d", TOKEN_STR_LAST_LEVEL_SERIES, i);
5139
5140       last_level_series = getHashEntry(level_setup_hash, token);
5141
5142       leveldir_last = getTreeInfoFromIdentifier(leveldir_first,
5143                                                 last_level_series);
5144       if (leveldir_last != NULL)
5145         setString(&setup.level_setup.last_level_series[pos++],
5146                   last_level_series);
5147     }
5148
5149     setString(&setup.level_setup.last_level_series[pos], NULL);
5150
5151     freeSetupFileHash(level_setup_hash);
5152   }
5153   else
5154   {
5155     Debug("setup", "using default setup values");
5156   }
5157
5158   free(filename);
5159 }
5160
5161 static void SaveLevelSetup_LastSeries_Ext(boolean deactivate_last_level_series)
5162 {
5163   // --------------------------------------------------------------------------
5164   // ~/.<program>/levelsetup.conf
5165   // --------------------------------------------------------------------------
5166
5167   // check if the current level directory structure is available at this point
5168   if (leveldir_current == NULL)
5169     return;
5170
5171   char **last_level_series = setup.level_setup.last_level_series;
5172   char *filename = getPath2(getSetupDir(), LEVELSETUP_FILENAME);
5173   FILE *file;
5174   int i;
5175
5176   InitUserDataDirectory();
5177
5178   UpdateLastPlayedLevels_List();
5179
5180   if (!(file = fopen(filename, MODE_WRITE)))
5181   {
5182     Warn("cannot write setup file '%s'", filename);
5183
5184     free(filename);
5185
5186     return;
5187   }
5188
5189   fprintFileHeader(file, LEVELSETUP_FILENAME);
5190
5191   if (deactivate_last_level_series)
5192     fprintf(file, "# %s\n# ", "the following level set may have caused a problem and was deactivated");
5193
5194   fprintf(file, "%s\n\n", getFormattedSetupEntry(TOKEN_STR_LAST_LEVEL_SERIES,
5195                                                  leveldir_current->identifier));
5196
5197   // store if last level set was selected from "last played" menu
5198   boolean last_played_menu_used = CheckLastPlayedLevels();
5199   char *setup_value = getSetupValue(TYPE_BOOLEAN, &last_played_menu_used);
5200
5201   fprintf(file, "%s\n\n", getFormattedSetupEntry(TOKEN_STR_LAST_PLAYED_MENU_USED,
5202                                                  setup_value));
5203
5204   for (i = 0; last_level_series[i] != NULL; i++)
5205   {
5206     char token[strlen(TOKEN_STR_LAST_LEVEL_SERIES) + 1 + 10 + 1];
5207
5208     sprintf(token, "%s.%03d", TOKEN_STR_LAST_LEVEL_SERIES, i);
5209
5210     fprintf(file, "%s\n", getFormattedSetupEntry(token, last_level_series[i]));
5211   }
5212
5213   fclose(file);
5214
5215   SetFilePermissions(filename, PERMS_PRIVATE);
5216
5217   free(filename);
5218 }
5219
5220 void SaveLevelSetup_LastSeries(void)
5221 {
5222   SaveLevelSetup_LastSeries_Ext(FALSE);
5223 }
5224
5225 void SaveLevelSetup_LastSeries_Deactivate(void)
5226 {
5227   SaveLevelSetup_LastSeries_Ext(TRUE);
5228 }
5229
5230 static void checkSeriesInfo(void)
5231 {
5232   static char *level_directory = NULL;
5233   Directory *dir;
5234 #if 0
5235   DirectoryEntry *dir_entry;
5236 #endif
5237
5238   checked_free(level_directory);
5239
5240   // check for more levels besides the 'levels' field of 'levelinfo.conf'
5241
5242   level_directory = getPath2((leveldir_current->in_user_dir ?
5243                               getUserLevelDir(NULL) :
5244                               options.level_directory),
5245                              leveldir_current->fullpath);
5246
5247   if ((dir = openDirectory(level_directory)) == NULL)
5248   {
5249     Warn("cannot read level directory '%s'", level_directory);
5250
5251     return;
5252   }
5253
5254 #if 0
5255   while ((dir_entry = readDirectory(dir)) != NULL)      // loop all entries
5256   {
5257     if (strlen(dir_entry->basename) > 4 &&
5258         dir_entry->basename[3] == '.' &&
5259         strEqual(&dir_entry->basename[4], LEVELFILE_EXTENSION))
5260     {
5261       char levelnum_str[4];
5262       int levelnum_value;
5263
5264       strncpy(levelnum_str, dir_entry->basename, 3);
5265       levelnum_str[3] = '\0';
5266
5267       levelnum_value = atoi(levelnum_str);
5268
5269       if (levelnum_value < leveldir_current->first_level)
5270       {
5271         Warn("additional level %d found", levelnum_value);
5272
5273         leveldir_current->first_level = levelnum_value;
5274       }
5275       else if (levelnum_value > leveldir_current->last_level)
5276       {
5277         Warn("additional level %d found", levelnum_value);
5278
5279         leveldir_current->last_level = levelnum_value;
5280       }
5281     }
5282   }
5283 #endif
5284
5285   closeDirectory(dir);
5286 }
5287
5288 void LoadLevelSetup_SeriesInfo(void)
5289 {
5290   char *filename;
5291   SetupFileHash *level_setup_hash = NULL;
5292   char *level_subdir = leveldir_current->subdir;
5293   int i;
5294
5295   // always start with reliable default values
5296   level_nr = leveldir_current->first_level;
5297
5298   for (i = 0; i < MAX_LEVELS; i++)
5299   {
5300     LevelStats_setPlayed(i, 0);
5301     LevelStats_setSolved(i, 0);
5302   }
5303
5304   checkSeriesInfo();
5305
5306   // --------------------------------------------------------------------------
5307   // ~/.<program>/levelsetup/<level series>/levelsetup.conf
5308   // --------------------------------------------------------------------------
5309
5310   level_subdir = leveldir_current->subdir;
5311
5312   filename = getPath2(getLevelSetupDir(level_subdir), LEVELSETUP_FILENAME);
5313
5314   if ((level_setup_hash = loadSetupFileHash(filename)))
5315   {
5316     char *token_value;
5317
5318     // get last played level in this level set
5319
5320     token_value = getHashEntry(level_setup_hash, TOKEN_STR_LAST_PLAYED_LEVEL);
5321
5322     if (token_value)
5323     {
5324       level_nr = atoi(token_value);
5325
5326       if (level_nr < leveldir_current->first_level)
5327         level_nr = leveldir_current->first_level;
5328       if (level_nr > leveldir_current->last_level)
5329         level_nr = leveldir_current->last_level;
5330     }
5331
5332     // get handicap level in this level set
5333
5334     token_value = getHashEntry(level_setup_hash, TOKEN_STR_HANDICAP_LEVEL);
5335
5336     if (token_value)
5337     {
5338       int level_nr = atoi(token_value);
5339
5340       if (level_nr < leveldir_current->first_level)
5341         level_nr = leveldir_current->first_level;
5342       if (level_nr > leveldir_current->last_level + 1)
5343         level_nr = leveldir_current->last_level;
5344
5345       if (leveldir_current->user_defined || !leveldir_current->handicap)
5346         level_nr = leveldir_current->last_level;
5347
5348       leveldir_current->handicap_level = level_nr;
5349     }
5350
5351     // get number of played and solved levels in this level set
5352
5353     BEGIN_HASH_ITERATION(level_setup_hash, itr)
5354     {
5355       char *token = HASH_ITERATION_TOKEN(itr);
5356       char *value = HASH_ITERATION_VALUE(itr);
5357
5358       if (strlen(token) == 3 &&
5359           token[0] >= '0' && token[0] <= '9' &&
5360           token[1] >= '0' && token[1] <= '9' &&
5361           token[2] >= '0' && token[2] <= '9')
5362       {
5363         int level_nr = atoi(token);
5364
5365         if (value != NULL)
5366           LevelStats_setPlayed(level_nr, atoi(value));  // read 1st column
5367
5368         value = strchr(value, ' ');
5369
5370         if (value != NULL)
5371           LevelStats_setSolved(level_nr, atoi(value));  // read 2nd column
5372       }
5373     }
5374     END_HASH_ITERATION(hash, itr)
5375
5376     freeSetupFileHash(level_setup_hash);
5377   }
5378   else
5379   {
5380     Debug("setup", "using default setup values");
5381   }
5382
5383   free(filename);
5384 }
5385
5386 void SaveLevelSetup_SeriesInfo(void)
5387 {
5388   char *filename;
5389   char *level_subdir = leveldir_current->subdir;
5390   char *level_nr_str = int2str(level_nr, 0);
5391   char *handicap_level_str = int2str(leveldir_current->handicap_level, 0);
5392   FILE *file;
5393   int i;
5394
5395   // --------------------------------------------------------------------------
5396   // ~/.<program>/levelsetup/<level series>/levelsetup.conf
5397   // --------------------------------------------------------------------------
5398
5399   InitLevelSetupDirectory(level_subdir);
5400
5401   filename = getPath2(getLevelSetupDir(level_subdir), LEVELSETUP_FILENAME);
5402
5403   if (!(file = fopen(filename, MODE_WRITE)))
5404   {
5405     Warn("cannot write setup file '%s'", filename);
5406
5407     free(filename);
5408
5409     return;
5410   }
5411
5412   fprintFileHeader(file, LEVELSETUP_FILENAME);
5413
5414   fprintf(file, "%s\n", getFormattedSetupEntry(TOKEN_STR_LAST_PLAYED_LEVEL,
5415                                                level_nr_str));
5416   fprintf(file, "%s\n\n", getFormattedSetupEntry(TOKEN_STR_HANDICAP_LEVEL,
5417                                                  handicap_level_str));
5418
5419   for (i = leveldir_current->first_level; i <= leveldir_current->last_level;
5420        i++)
5421   {
5422     if (LevelStats_getPlayed(i) > 0 ||
5423         LevelStats_getSolved(i) > 0)
5424     {
5425       char token[16];
5426       char value[16];
5427
5428       sprintf(token, "%03d", i);
5429       sprintf(value, "%d %d", LevelStats_getPlayed(i), LevelStats_getSolved(i));
5430
5431       fprintf(file, "%s\n", getFormattedSetupEntry(token, value));
5432     }
5433   }
5434
5435   fclose(file);
5436
5437   SetFilePermissions(filename, PERMS_PRIVATE);
5438
5439   free(filename);
5440 }
5441
5442 int LevelStats_getPlayed(int nr)
5443 {
5444   return (nr >= 0 && nr < MAX_LEVELS ? level_stats[nr].played : 0);
5445 }
5446
5447 int LevelStats_getSolved(int nr)
5448 {
5449   return (nr >= 0 && nr < MAX_LEVELS ? level_stats[nr].solved : 0);
5450 }
5451
5452 void LevelStats_setPlayed(int nr, int value)
5453 {
5454   if (nr >= 0 && nr < MAX_LEVELS)
5455     level_stats[nr].played = value;
5456 }
5457
5458 void LevelStats_setSolved(int nr, int value)
5459 {
5460   if (nr >= 0 && nr < MAX_LEVELS)
5461     level_stats[nr].solved = value;
5462 }
5463
5464 void LevelStats_incPlayed(int nr)
5465 {
5466   if (nr >= 0 && nr < MAX_LEVELS)
5467     level_stats[nr].played++;
5468 }
5469
5470 void LevelStats_incSolved(int nr)
5471 {
5472   if (nr >= 0 && nr < MAX_LEVELS)
5473     level_stats[nr].solved++;
5474 }
5475
5476 void LoadUserSetup(void)
5477 {
5478   // --------------------------------------------------------------------------
5479   // ~/.<program>/usersetup.conf
5480   // --------------------------------------------------------------------------
5481
5482   char *filename = getPath2(getMainUserGameDataDir(), USERSETUP_FILENAME);
5483   SetupFileHash *user_setup_hash = NULL;
5484
5485   // always start with reliable default values
5486   user.nr = 0;
5487
5488   if ((user_setup_hash = loadSetupFileHash(filename)))
5489   {
5490     char *token_value;
5491
5492     // get last selected user number
5493     token_value = getHashEntry(user_setup_hash, TOKEN_STR_LAST_USER);
5494
5495     if (token_value)
5496       user.nr = MIN(MAX(0, atoi(token_value)), MAX_PLAYER_NAMES - 1);
5497
5498     freeSetupFileHash(user_setup_hash);
5499   }
5500   else
5501   {
5502     Debug("setup", "using default setup values");
5503   }
5504
5505   free(filename);
5506 }
5507
5508 void SaveUserSetup(void)
5509 {
5510   // --------------------------------------------------------------------------
5511   // ~/.<program>/usersetup.conf
5512   // --------------------------------------------------------------------------
5513
5514   char *filename = getPath2(getMainUserGameDataDir(), USERSETUP_FILENAME);
5515   FILE *file;
5516
5517   InitMainUserDataDirectory();
5518
5519   if (!(file = fopen(filename, MODE_WRITE)))
5520   {
5521     Warn("cannot write setup file '%s'", filename);
5522
5523     free(filename);
5524
5525     return;
5526   }
5527
5528   fprintFileHeader(file, USERSETUP_FILENAME);
5529
5530   fprintf(file, "%s\n", getFormattedSetupEntry(TOKEN_STR_LAST_USER,
5531                                                i_to_a(user.nr)));
5532   fclose(file);
5533
5534   SetFilePermissions(filename, PERMS_PRIVATE);
5535
5536   free(filename);
5537 }