+static char *getLevelDirFromTreeInfo(TreeInfo *node)
+{
+ static char *level_dir = NULL;
+
+ if (node == NULL)
+ return options.level_directory;
+
+ if (level_dir)
+ free(level_dir);
+
+ level_dir = getPath2((node->user_defined ? getUserLevelDir(NULL) :
+ options.level_directory), node->fullpath);
+
+ return level_dir;
+}
+
+static char *getCurrentLevelDir()
+{
+ return getLevelDirFromTreeInfo(leveldir_current);
+}
+
+static char *getDefaultGraphicsDir(char *graphics_subdir)
+{
+ static char *graphics_dir = NULL;
+
+ if (graphics_subdir == NULL)
+ return options.graphics_directory;
+
+ if (graphics_dir)
+ free(graphics_dir);
+
+ graphics_dir = getPath2(options.graphics_directory, graphics_subdir);
+
+ return graphics_dir;
+}
+
+static char *getDefaultSoundsDir(char *sounds_subdir)
+{
+ static char *sounds_dir = NULL;
+
+ if (sounds_subdir == NULL)
+ return options.sounds_directory;
+
+ if (sounds_dir)
+ free(sounds_dir);
+
+ sounds_dir = getPath2(options.sounds_directory, sounds_subdir);
+
+ return sounds_dir;
+}
+
+static char *getDefaultMusicDir(char *music_subdir)
+{
+ static char *music_dir = NULL;
+
+ if (music_subdir == NULL)
+ return options.music_directory;
+
+ if (music_dir)
+ free(music_dir);
+
+ music_dir = getPath2(options.music_directory, music_subdir);
+
+ return music_dir;
+}
+
+static char *getDefaultArtworkSet(int type)
+{
+ return (type == TREE_TYPE_GRAPHICS_DIR ? GFX_CLASSIC_SUBDIR :
+ type == TREE_TYPE_SOUNDS_DIR ? SND_CLASSIC_SUBDIR :
+ type == TREE_TYPE_MUSIC_DIR ? MUS_CLASSIC_SUBDIR : "");
+}
+
+static char *getDefaultArtworkDir(int type)
+{
+ return (type == TREE_TYPE_GRAPHICS_DIR ?
+ getDefaultGraphicsDir(GFX_CLASSIC_SUBDIR) :
+ type == TREE_TYPE_SOUNDS_DIR ?
+ getDefaultSoundsDir(SND_CLASSIC_SUBDIR) :
+ type == TREE_TYPE_MUSIC_DIR ?
+ getDefaultMusicDir(MUS_CLASSIC_SUBDIR) : "");
+}
+
+static char *getUserGraphicsDir()
+{
+ static char *usergraphics_dir = NULL;
+
+ if (usergraphics_dir == NULL)
+ usergraphics_dir = getPath2(getUserDataDir(), GRAPHICS_DIRECTORY);
+
+ return usergraphics_dir;
+}
+
+static char *getUserSoundsDir()
+{
+ static char *usersounds_dir = NULL;
+
+ if (usersounds_dir == NULL)
+ usersounds_dir = getPath2(getUserDataDir(), SOUNDS_DIRECTORY);
+
+ return usersounds_dir;
+}
+
+static char *getUserMusicDir()
+{
+ static char *usermusic_dir = NULL;
+
+ if (usermusic_dir == NULL)
+ usermusic_dir = getPath2(getUserDataDir(), MUSIC_DIRECTORY);
+
+ return usermusic_dir;
+}
+
+static char *getSetupArtworkDir(TreeInfo *ti)
+{
+ static char *artwork_dir = NULL;
+
+ if (artwork_dir != NULL)
+ free(artwork_dir);
+
+ artwork_dir = getPath2(ti->basepath, ti->fullpath);
+
+ return artwork_dir;
+}
+
+char *setLevelArtworkDir(TreeInfo *ti)
+{
+ char **artwork_path_ptr, **artwork_set_ptr;
+ TreeInfo *level_artwork;
+
+ if (ti == NULL || leveldir_current == NULL)
+ return NULL;
+
+ artwork_path_ptr = &(LEVELDIR_ARTWORK_PATH(leveldir_current, ti->type));
+ artwork_set_ptr = &(LEVELDIR_ARTWORK_SET( leveldir_current, ti->type));
+
+ if (*artwork_path_ptr != NULL)
+ free(*artwork_path_ptr);
+
+ if ((level_artwork = getTreeInfoFromIdentifier(ti, *artwork_set_ptr)))
+ *artwork_path_ptr = getStringCopy(getSetupArtworkDir(level_artwork));
+ else
+ {
+ /* No (or non-existing) artwork configured in "levelinfo.conf". This would
+ normally result in using the artwork configured in the setup menu. But
+ if an artwork subdirectory exists (which might contain custom artwork
+ or an artwork configuration file), this level artwork must be treated
+ as relative to the default "classic" artwork, not to the artwork that
+ is currently configured in the setup menu. */
+
+ char *dir = getPath2(getCurrentLevelDir(), ARTWORK_DIRECTORY(ti->type));
+
+ if (*artwork_set_ptr != NULL)
+ free(*artwork_set_ptr);
+
+ if (fileExists(dir))
+ {
+ *artwork_path_ptr = getStringCopy(getDefaultArtworkDir(ti->type));
+ *artwork_set_ptr = getStringCopy(getDefaultArtworkSet(ti->type));
+ }
+ else
+ {
+ *artwork_path_ptr = getStringCopy(UNDEFINED_FILENAME);
+ *artwork_set_ptr = NULL;
+ }
+
+ free(dir);
+ }
+
+ return *artwork_set_ptr;
+}
+
+inline static char *getLevelArtworkSet(int type)
+{
+ if (leveldir_current == NULL)
+ return NULL;
+
+ return LEVELDIR_ARTWORK_SET(leveldir_current, type);
+}
+
+inline static char *getLevelArtworkDir(int type)
+{
+ if (leveldir_current == NULL)
+ return UNDEFINED_FILENAME;
+
+ return LEVELDIR_ARTWORK_PATH(leveldir_current, type);
+}
+