+ if (!setup.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_CLASSIC_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;
+
+ 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 (!setup.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_CLASSIC_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;
+
+ 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 (!setup.override_level_music)
+ {
+ /* 1st try: look for special artwork in current level series directory */
+ directory = getPath2(getCurrentLevelDir(), MUSIC_DIRECTORY);
+ if (fileExists(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 (fileExists(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 (fileExists(directory))
+ return directory;
+
+ free(directory);
+ }
+
+ /* 4th try: look for default artwork in new default artwork directory */
+ directory = getStringCopy(getDefaultMusicDir(MUS_CLASSIC_SUBDIR));
+ if (fileExists(directory))
+ return directory;
+
+ free(directory);
+
+ /* 5th try: look for default artwork in old default artwork directory */
+ directory = getStringCopy(options.music_directory);
+ if (fileExists(directory))
+ return directory;
+
+ return NULL; /* cannot find specified artwork file anywhere */
+}
+
+void InitTapeDirectory(char *level_subdir)
+{
+ createDirectory(getUserDataDir(), "user data", PERMS_PRIVATE);
+ createDirectory(getTapeDir(NULL), "main tape", PERMS_PRIVATE);
+ createDirectory(getTapeDir(level_subdir), "level tape", PERMS_PRIVATE);
+}
+
+void InitScoreDirectory(char *level_subdir)
+{
+ createDirectory(getCommonDataDir(), "common data", PERMS_PUBLIC);
+ createDirectory(getScoreDir(NULL), "main score", PERMS_PUBLIC);
+ createDirectory(getScoreDir(level_subdir), "level score", PERMS_PUBLIC);
+}
+
+static void SaveUserLevelInfo();
+
+void InitUserLevelDirectory(char *level_subdir)
+{
+ if (access(getUserLevelDir(level_subdir), F_OK) != 0)
+ {
+ createDirectory(getUserDataDir(), "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(getUserDataDir(), "user data", PERMS_PRIVATE);
+ createDirectory(getLevelSetupDir(NULL), "main level setup", PERMS_PRIVATE);
+ createDirectory(getLevelSetupDir(level_subdir), "level setup",PERMS_PRIVATE);
+}
+
+
+/* ------------------------------------------------------------------------- */
+/* some functions to handle lists of level directories */
+/* ------------------------------------------------------------------------- */
+
+TreeInfo *newTreeInfo()
+{
+ return checked_calloc(sizeof(TreeInfo));
+}
+
+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 (strcmp(identifier, node->identifier) == 0)
+ return node;
+ }
+
+ node = node->next;
+ }
+
+ return NULL;
+}
+
+void dumpTreeInfo(TreeInfo *node, int depth)
+{
+ int i;
+
+ printf("Dumping TreeInfo:\n");
+
+ while (node)
+ {
+ for (i = 0; i < (depth + 1) * 3; i++)
+ printf(" ");
+
+#if 1
+ printf("subdir == '%s' ['%s', '%s'] [%d])\n",
+ node->subdir, node->fullpath, node->basepath, node->in_user_dir);
+#else
+ printf("subdir == '%s' (%s) [%s] (%d)\n",
+ node->subdir, node->name, node->identifier, node->sort_priority);
+#endif
+
+ if (node->node_group != NULL)
+ dumpTreeInfo(node->node_group, depth + 1);
+
+ node = node->next;
+ }
+}
+
+void sortTreeInfo(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 */