+char *getLevelSetInfoFilename()
+{
+ static char *filename = NULL;
+ char *basenames[] =
+ {
+ "README",
+ "README.TXT",
+ "README.txt",
+ "Readme",
+ "Readme.txt",
+ "readme",
+ "readme.txt",
+
+ NULL
+ };
+ int i;
+
+ for (i = 0; basenames[i] != NULL; i++)
+ {
+ checked_free(filename);
+ filename = getPath2(getCurrentLevelDir(), basenames[i]);
+
+ if (fileExists(filename))
+ return filename;
+ }
+
+ return NULL;
+}
+
+char *getLevelSetTitleMessageBasename(int nr, boolean initial)
+{
+ static char basename[32];
+
+ sprintf(basename, "%s_%d.txt",
+ (initial ? "titlemessage_initial" : "titlemessage"), nr + 1);
+
+ return basename;
+}
+
+char *getLevelSetTitleMessageFilename(int nr, boolean initial)
+{
+ static char *filename = NULL;
+ char *basename;
+ boolean skip_setup_artwork = FALSE;
+
+ checked_free(filename);
+
+ basename = getLevelSetTitleMessageBasename(nr, initial);
+
+ if (!gfx.override_level_graphics)
+ {
+ /* 1st try: look for special artwork in current level series directory */
+ filename = getPath3(getCurrentLevelDir(), GRAPHICS_DIRECTORY, basename);
+ if (fileExists(filename))
+ return filename;
+
+ free(filename);
+
+ /* 2nd try: look for message file in current level set directory */
+ filename = getPath2(getCurrentLevelDir(), basename);
+ if (fileExists(filename))
+ return filename;
+
+ free(filename);
+
+ /* check if there is special artwork configured in level series config */
+ if (getLevelArtworkSet(ARTWORK_TYPE_GRAPHICS) != NULL)
+ {
+ /* 3rd try: look for special artwork configured in level series config */
+ filename = getPath2(getLevelArtworkDir(ARTWORK_TYPE_GRAPHICS), basename);
+ if (fileExists(filename))
+ return filename;
+
+ free(filename);
+
+ /* take missing artwork configured in level set config from default */
+ skip_setup_artwork = TRUE;
+ }
+ }
+
+ if (!skip_setup_artwork)
+ {
+ /* 4th try: look for special artwork in configured artwork directory */
+ filename = getPath2(getSetupArtworkDir(artwork.gfx_current), basename);
+ if (fileExists(filename))
+ return filename;
+
+ free(filename);
+ }
+
+ /* 5th try: look for default artwork in new default artwork directory */
+ filename = getPath2(getDefaultGraphicsDir(GFX_DEFAULT_SUBDIR), basename);
+ if (fileExists(filename))
+ return filename;
+
+ free(filename);
+
+ /* 6th try: look for default artwork in old default artwork directory */
+ filename = getPath2(options.graphics_directory, basename);
+ if (fileExists(filename))
+ return filename;
+
+ return NULL; /* cannot find specified artwork file anywhere */
+}
+
+static char *getCorrectedArtworkBasename(char *basename)
+{
+ return basename;
+}
+
+char *getCustomImageFilename(char *basename)
+{
+ static char *filename = NULL;
+ boolean skip_setup_artwork = FALSE;
+
+ checked_free(filename);
+
+ basename = getCorrectedArtworkBasename(basename);
+
+ if (!gfx.override_level_graphics)
+ {
+ /* 1st try: look for special artwork in current level series directory */
+ filename = getImg3(getCurrentLevelDir(), GRAPHICS_DIRECTORY, basename);
+ if (fileExists(filename))
+ return filename;
+
+ free(filename);
+
+ /* check if there is special artwork configured in level series config */
+ if (getLevelArtworkSet(ARTWORK_TYPE_GRAPHICS) != NULL)
+ {
+ /* 2nd try: look for special artwork configured in level series config */
+ filename = getImg2(getLevelArtworkDir(ARTWORK_TYPE_GRAPHICS), basename);
+ if (fileExists(filename))
+ return filename;
+
+ free(filename);
+
+ /* take missing artwork configured in level set config from default */
+ skip_setup_artwork = TRUE;
+ }
+ }
+
+ if (!skip_setup_artwork)
+ {
+ /* 3rd try: look for special artwork in configured artwork directory */
+ filename = getImg2(getSetupArtworkDir(artwork.gfx_current), basename);
+ if (fileExists(filename))
+ return filename;
+
+ free(filename);
+ }
+
+ /* 4th try: look for default artwork in new default artwork directory */
+ filename = getImg2(getDefaultGraphicsDir(GFX_DEFAULT_SUBDIR), basename);
+ if (fileExists(filename))
+ return filename;
+
+ free(filename);
+
+ /* 5th try: look for default artwork in old default artwork directory */
+ filename = getImg2(options.graphics_directory, basename);
+ if (fileExists(filename))
+ return filename;
+
+ if (!strEqual(GFX_FALLBACK_FILENAME, UNDEFINED_FILENAME))
+ {
+ free(filename);
+
+ if (options.debug)
+ Error(ERR_WARN, "cannot find artwork file '%s' (using fallback)",
+ basename);
+
+ /* 6th try: look for fallback artwork in old default artwork directory */
+ /* (needed to prevent errors when trying to access unused artwork files) */
+ filename = getImg2(options.graphics_directory, GFX_FALLBACK_FILENAME);
+ if (fileExists(filename))
+ return filename;
+ }
+
+ return NULL; /* cannot find specified artwork file anywhere */
+}
+
+char *getCustomSoundFilename(char *basename)
+{
+ static char *filename = NULL;
+ boolean skip_setup_artwork = FALSE;
+
+ checked_free(filename);
+
+ basename = getCorrectedArtworkBasename(basename);
+
+ if (!gfx.override_level_sounds)
+ {
+ /* 1st try: look for special artwork in current level series directory */
+ filename = getPath3(getCurrentLevelDir(), SOUNDS_DIRECTORY, basename);
+ if (fileExists(filename))
+ return filename;
+
+ free(filename);
+
+ /* check if there is special artwork configured in level series config */
+ if (getLevelArtworkSet(ARTWORK_TYPE_SOUNDS) != NULL)
+ {
+ /* 2nd try: look for special artwork configured in level series config */
+ filename = getPath2(getLevelArtworkDir(TREE_TYPE_SOUNDS_DIR), basename);
+ if (fileExists(filename))
+ return filename;
+
+ free(filename);
+
+ /* take missing artwork configured in level set config from default */
+ skip_setup_artwork = TRUE;
+ }
+ }
+
+ if (!skip_setup_artwork)
+ {
+ /* 3rd try: look for special artwork in configured artwork directory */
+ filename = getPath2(getSetupArtworkDir(artwork.snd_current), basename);
+ if (fileExists(filename))
+ return filename;
+
+ free(filename);
+ }
+
+ /* 4th try: look for default artwork in new default artwork directory */
+ filename = getPath2(getDefaultSoundsDir(SND_DEFAULT_SUBDIR), basename);
+ if (fileExists(filename))
+ return filename;
+
+ free(filename);
+
+ /* 5th try: look for default artwork in old default artwork directory */
+ filename = getPath2(options.sounds_directory, basename);
+ if (fileExists(filename))
+ return filename;
+
+ if (!strEqual(SND_FALLBACK_FILENAME, UNDEFINED_FILENAME))
+ {
+ free(filename);
+
+ if (options.debug)
+ Error(ERR_WARN, "cannot find artwork file '%s' (using fallback)",
+ basename);
+
+ /* 6th try: look for fallback artwork in old default artwork directory */
+ /* (needed to prevent errors when trying to access unused artwork files) */
+ filename = getPath2(options.sounds_directory, SND_FALLBACK_FILENAME);
+ if (fileExists(filename))
+ return filename;
+ }
+
+ return NULL; /* cannot find specified artwork file anywhere */
+}
+
+char *getCustomMusicFilename(char *basename)
+{
+ static char *filename = NULL;
+ boolean skip_setup_artwork = FALSE;
+
+ checked_free(filename);
+
+ basename = getCorrectedArtworkBasename(basename);
+
+ if (!gfx.override_level_music)
+ {
+ /* 1st try: look for special artwork in current level series directory */
+ filename = getPath3(getCurrentLevelDir(), MUSIC_DIRECTORY, basename);
+ if (fileExists(filename))
+ return filename;
+
+ free(filename);
+
+ /* check if there is special artwork configured in level series config */
+ if (getLevelArtworkSet(ARTWORK_TYPE_MUSIC) != NULL)
+ {
+ /* 2nd try: look for special artwork configured in level series config */
+ filename = getPath2(getLevelArtworkDir(TREE_TYPE_MUSIC_DIR), basename);
+ if (fileExists(filename))
+ return filename;
+
+ free(filename);
+
+ /* take missing artwork configured in level set config from default */
+ skip_setup_artwork = TRUE;
+ }
+ }
+
+ if (!skip_setup_artwork)
+ {
+ /* 3rd try: look for special artwork in configured artwork directory */
+ filename = getPath2(getSetupArtworkDir(artwork.mus_current), basename);
+ if (fileExists(filename))
+ return filename;
+
+ free(filename);
+ }
+
+ /* 4th try: look for default artwork in new default artwork directory */
+ filename = getPath2(getDefaultMusicDir(MUS_DEFAULT_SUBDIR), basename);
+ if (fileExists(filename))
+ return filename;
+
+ free(filename);
+
+ /* 5th try: look for default artwork in old default artwork directory */
+ filename = getPath2(options.music_directory, basename);
+ if (fileExists(filename))
+ return filename;
+
+ if (!strEqual(MUS_FALLBACK_FILENAME, UNDEFINED_FILENAME))
+ {
+ free(filename);
+
+ if (options.debug)
+ Error(ERR_WARN, "cannot find artwork file '%s' (using fallback)",
+ basename);
+
+ /* 6th try: look for fallback artwork in old default artwork directory */
+ /* (needed to prevent errors when trying to access unused artwork files) */
+ filename = getPath2(options.music_directory, MUS_FALLBACK_FILENAME);
+ if (fileExists(filename))
+ return filename;
+ }
+
+ return NULL; /* cannot find specified artwork file anywhere */
+}
+
+char *getCustomArtworkFilename(char *basename, int type)
+{
+ if (type == ARTWORK_TYPE_GRAPHICS)
+ return getCustomImageFilename(basename);
+ else if (type == ARTWORK_TYPE_SOUNDS)
+ return getCustomSoundFilename(basename);
+ else if (type == ARTWORK_TYPE_MUSIC)
+ return getCustomMusicFilename(basename);
+ else
+ return UNDEFINED_FILENAME;
+}
+
+char *getCustomArtworkConfigFilename(int type)
+{
+ return getCustomArtworkFilename(ARTWORKINFO_FILENAME(type), type);
+}
+
+char *getCustomArtworkLevelConfigFilename(int type)
+{
+ static char *filename = NULL;
+
+ checked_free(filename);
+
+ filename = getPath2(getLevelArtworkDir(type), ARTWORKINFO_FILENAME(type));
+
+ return filename;
+}
+
+char *getCustomMusicDirectory(void)
+{
+ static char *directory = NULL;
+ boolean skip_setup_artwork = FALSE;
+
+ checked_free(directory);
+
+ if (!gfx.override_level_music)
+ {
+ /* 1st try: look for special artwork in current level series directory */
+ directory = getPath2(getCurrentLevelDir(), MUSIC_DIRECTORY);
+ if (directoryExists(directory))
+ return directory;
+
+ free(directory);
+
+ /* check if there is special artwork configured in level series config */
+ if (getLevelArtworkSet(ARTWORK_TYPE_MUSIC) != NULL)
+ {
+ /* 2nd try: look for special artwork configured in level series config */
+ directory = getStringCopy(getLevelArtworkDir(TREE_TYPE_MUSIC_DIR));
+ if (directoryExists(directory))
+ return directory;
+
+ free(directory);
+
+ /* take missing artwork configured in level set config from default */
+ skip_setup_artwork = TRUE;
+ }
+ }
+
+ if (!skip_setup_artwork)
+ {
+ /* 3rd try: look for special artwork in configured artwork directory */
+ directory = getStringCopy(getSetupArtworkDir(artwork.mus_current));
+ if (directoryExists(directory))
+ return directory;
+
+ free(directory);
+ }
+
+ /* 4th try: look for default artwork in new default artwork directory */
+ directory = getStringCopy(getDefaultMusicDir(MUS_DEFAULT_SUBDIR));
+ if (directoryExists(directory))
+ return directory;
+
+ free(directory);
+
+ /* 5th try: look for default artwork in old default artwork directory */
+ directory = getStringCopy(options.music_directory);
+ if (directoryExists(directory))
+ return directory;
+
+ return NULL; /* cannot find specified artwork file anywhere */
+}
+
+void InitTapeDirectory(char *level_subdir)
+{
+ createDirectory(getUserGameDataDir(), "user data", PERMS_PRIVATE);
+ createDirectory(getTapeDir(NULL), "main tape", PERMS_PRIVATE);
+ createDirectory(getTapeDir(level_subdir), "level tape", PERMS_PRIVATE);
+}
+
+void InitScoreDirectory(char *level_subdir)
+{
+ int permissions = (program.global_scores ? PERMS_PUBLIC : PERMS_PRIVATE);
+
+ if (program.global_scores)
+ createDirectory(getCommonDataDir(), "common data", permissions);
+ else
+ createDirectory(getUserGameDataDir(), "user data", permissions);
+
+ createDirectory(getScoreDir(NULL), "main score", permissions);
+ createDirectory(getScoreDir(level_subdir), "level score", permissions);
+}
+
+static void SaveUserLevelInfo();
+
+void InitUserLevelDirectory(char *level_subdir)
+{
+ if (!directoryExists(getUserLevelDir(level_subdir)))
+ {
+ createDirectory(getUserGameDataDir(), "user data", PERMS_PRIVATE);
+ createDirectory(getUserLevelDir(NULL), "main user level", PERMS_PRIVATE);
+ createDirectory(getUserLevelDir(level_subdir), "user level", PERMS_PRIVATE);
+
+ SaveUserLevelInfo();
+ }
+}
+
+void InitLevelSetupDirectory(char *level_subdir)
+{
+ createDirectory(getUserGameDataDir(), "user data", PERMS_PRIVATE);
+ createDirectory(getLevelSetupDir(NULL), "main level setup", PERMS_PRIVATE);
+ createDirectory(getLevelSetupDir(level_subdir), "level setup", PERMS_PRIVATE);
+}
+
+void InitCacheDirectory()
+{
+ createDirectory(getUserGameDataDir(), "user data", PERMS_PRIVATE);
+ createDirectory(getCacheDir(), "cache data", PERMS_PRIVATE);
+}
+
+
+/* ------------------------------------------------------------------------- */
+/* some functions to handle lists of level and artwork directories */
+/* ------------------------------------------------------------------------- */
+
+TreeInfo *newTreeInfo()
+{
+ return checked_calloc(sizeof(TreeInfo));
+}
+
+TreeInfo *newTreeInfo_setDefaults(int type)
+{
+ TreeInfo *ti = newTreeInfo();
+
+ setTreeInfoToDefaults(ti, type);
+
+ return ti;
+}
+
+void pushTreeInfo(TreeInfo **node_first, TreeInfo *node_new)
+{
+ node_new->next = *node_first;
+ *node_first = node_new;
+}
+
+int numTreeInfo(TreeInfo *node)
+{
+ int num = 0;
+
+ while (node)
+ {
+ num++;
+ node = node->next;
+ }
+
+ return num;
+}
+
+boolean validLevelSeries(TreeInfo *node)
+{
+ return (node != NULL && !node->node_group && !node->parent_link);
+}
+
+TreeInfo *getFirstValidTreeInfoEntry(TreeInfo *node)
+{
+ if (node == NULL)
+ return NULL;
+
+ if (node->node_group) /* enter level group (step down into tree) */
+ return getFirstValidTreeInfoEntry(node->node_group);
+ else if (node->parent_link) /* skip start entry of level group */
+ {
+ if (node->next) /* get first real level series entry */
+ return getFirstValidTreeInfoEntry(node->next);
+ else /* leave empty level group and go on */
+ return getFirstValidTreeInfoEntry(node->node_parent->next);
+ }
+ else /* this seems to be a regular level series */
+ return node;
+}
+
+TreeInfo *getTreeInfoFirstGroupEntry(TreeInfo *node)
+{
+ if (node == NULL)
+ return NULL;
+
+ if (node->node_parent == NULL) /* top level group */
+ return *node->node_top;
+ else /* sub level group */
+ return node->node_parent->node_group;
+}
+
+int numTreeInfoInGroup(TreeInfo *node)
+{
+ return numTreeInfo(getTreeInfoFirstGroupEntry(node));
+}
+
+int posTreeInfo(TreeInfo *node)
+{
+ TreeInfo *node_cmp = getTreeInfoFirstGroupEntry(node);
+ int pos = 0;
+
+ while (node_cmp)
+ {
+ if (node_cmp == node)
+ return pos;
+
+ pos++;
+ node_cmp = node_cmp->next;
+ }
+
+ return 0;
+}
+
+TreeInfo *getTreeInfoFromPos(TreeInfo *node, int pos)
+{
+ TreeInfo *node_default = node;
+ int pos_cmp = 0;
+
+ while (node)
+ {
+ if (pos_cmp == pos)
+ return node;
+
+ pos_cmp++;
+ node = node->next;
+ }
+
+ return node_default;
+}
+
+TreeInfo *getTreeInfoFromIdentifier(TreeInfo *node, char *identifier)
+{
+ if (identifier == NULL)
+ return NULL;
+
+ while (node)
+ {
+ if (node->node_group)
+ {
+ TreeInfo *node_group;
+
+ node_group = getTreeInfoFromIdentifier(node->node_group, identifier);
+
+ if (node_group)
+ return node_group;
+ }
+ else if (!node->parent_link)
+ {
+ if (strEqual(identifier, node->identifier))
+ return node;
+ }
+
+ node = node->next;
+ }
+
+ return NULL;
+}
+
+TreeInfo *cloneTreeNode(TreeInfo **node_top, TreeInfo *node_parent,
+ TreeInfo *node, boolean skip_sets_without_levels)
+{
+ TreeInfo *node_new;
+
+ if (node == NULL)
+ return NULL;
+
+ if (!node->parent_link && !node->level_group &&
+ skip_sets_without_levels && node->levels == 0)
+ return cloneTreeNode(node_top, node_parent, node->next,
+ skip_sets_without_levels);
+
+ node_new = getTreeInfoCopy(node); /* copy complete node */
+
+ node_new->node_top = node_top; /* correct top node link */
+ node_new->node_parent = node_parent; /* correct parent node link */
+
+ if (node->level_group)
+ node_new->node_group = cloneTreeNode(node_top, node_new, node->node_group,
+ skip_sets_without_levels);
+
+ node_new->next = cloneTreeNode(node_top, node_parent, node->next,
+ skip_sets_without_levels);
+
+ return node_new;
+}
+
+void cloneTree(TreeInfo **ti_new, TreeInfo *ti, boolean skip_empty_sets)
+{
+ TreeInfo *ti_cloned = cloneTreeNode(ti_new, NULL, ti, skip_empty_sets);
+
+ *ti_new = ti_cloned;
+}
+
+static boolean adjustTreeGraphicsForEMC(TreeInfo *node)
+{
+ boolean settings_changed = FALSE;
+
+ while (node)
+ {
+ if (node->graphics_set_ecs && !setup.prefer_aga_graphics &&
+ !strEqual(node->graphics_set, node->graphics_set_ecs))
+ {
+ setString(&node->graphics_set, node->graphics_set_ecs);
+ settings_changed = TRUE;
+ }
+ else if (node->graphics_set_aga && setup.prefer_aga_graphics &&
+ !strEqual(node->graphics_set, node->graphics_set_aga))
+ {
+ setString(&node->graphics_set, node->graphics_set_aga);
+ settings_changed = TRUE;
+ }
+
+ if (node->node_group != NULL)
+ settings_changed |= adjustTreeGraphicsForEMC(node->node_group);
+
+ node = node->next;
+ }
+
+ return settings_changed;
+}
+
+void dumpTreeInfo(TreeInfo *node, int depth)
+{
+ int i;
+
+ printf("Dumping TreeInfo:\n");
+
+ while (node)
+ {
+ for (i = 0; i < (depth + 1) * 3; i++)
+ printf(" ");
+
+ printf("'%s' / '%s'\n", node->identifier, node->name);
+
+ /*
+ // use for dumping artwork info tree
+ printf("subdir == '%s' ['%s', '%s'] [%d])\n",
+ node->subdir, node->fullpath, node->basepath, node->in_user_dir);
+ */
+
+ if (node->node_group != NULL)
+ dumpTreeInfo(node->node_group, depth + 1);
+
+ node = node->next;
+ }
+}
+
+void sortTreeInfoBySortFunction(TreeInfo **node_first,
+ int (*compare_function)(const void *,
+ const void *))
+{
+ int num_nodes = numTreeInfo(*node_first);
+ TreeInfo **sort_array;
+ TreeInfo *node = *node_first;
+ int i = 0;
+
+ if (num_nodes == 0)
+ return;
+
+ /* allocate array for sorting structure pointers */
+ sort_array = checked_calloc(num_nodes * sizeof(TreeInfo *));
+
+ /* writing structure pointers to sorting array */
+ while (i < num_nodes && node) /* double boundary check... */
+ {
+ sort_array[i] = node;
+
+ i++;
+ node = node->next;
+ }
+
+ /* sorting the structure pointers in the sorting array */
+ qsort(sort_array, num_nodes, sizeof(TreeInfo *),
+ compare_function);
+
+ /* update the linkage of list elements with the sorted node array */
+ for (i = 0; i < num_nodes - 1; i++)
+ sort_array[i]->next = sort_array[i + 1];
+ sort_array[num_nodes - 1]->next = NULL;
+
+ /* update the linkage of the main list anchor pointer */
+ *node_first = sort_array[0];
+
+ free(sort_array);
+
+ /* now recursively sort the level group structures */
+ node = *node_first;
+ while (node)
+ {
+ if (node->node_group != NULL)
+ sortTreeInfoBySortFunction(&node->node_group, compare_function);
+
+ node = node->next;
+ }
+}
+
+void sortTreeInfo(TreeInfo **node_first)
+{
+ sortTreeInfoBySortFunction(node_first, compareTreeInfoEntries);
+}
+
+
+/* ========================================================================= */
+/* some stuff from "files.c" */
+/* ========================================================================= */
+
+#if defined(PLATFORM_WIN32)
+#ifndef S_IRGRP
+#define S_IRGRP S_IRUSR
+#endif