+void LoadArtworkInfoFromLevelInfo(ArtworkDirTree **artwork_node,
+ LevelDirTree *level_node)
+{
+ int type = (*artwork_node)->type;
+
+ /* recursively check all level directories for artwork sub-directories */
+
+ while (level_node)
+ {
+ /* check all tree entries for artwork, but skip parent link entries */
+ if (!level_node->parent_link)
+ {
+ TreeInfo *artwork_new = getArtworkInfoCacheEntry(level_node, type);
+ boolean cached = (artwork_new != NULL);
+
+ if (cached)
+ {
+ pushTreeInfo(artwork_node, artwork_new);
+ }
+ else
+ {
+ TreeInfo *topnode_last = *artwork_node;
+ char *path = getPath2(getLevelDirFromTreeInfo(level_node),
+ ARTWORK_DIRECTORY(type));
+
+ LoadArtworkInfoFromArtworkDir(artwork_node, NULL, path, type);
+
+ if (topnode_last != *artwork_node) /* check for newly added node */
+ {
+ artwork_new = *artwork_node;
+
+ setString(&artwork_new->identifier, level_node->subdir);
+ setString(&artwork_new->name, level_node->name);
+ setString(&artwork_new->name_sorting, level_node->name_sorting);
+
+ artwork_new->sort_priority = level_node->sort_priority;
+ artwork_new->color = LEVELCOLOR(artwork_new);
+ }
+
+ free(path);
+ }
+
+ /* insert artwork info (from old cache or filesystem) into new cache */
+ if (artwork_new != NULL)
+ setArtworkInfoCacheEntry(artwork_new, level_node, type);
+ }
+
+ DrawInitText(level_node->name, 150, FC_YELLOW);
+
+ if (level_node->node_group != NULL)
+ LoadArtworkInfoFromLevelInfo(artwork_node, level_node->node_group);
+
+ level_node = level_node->next;
+ }
+}
+
+void LoadLevelArtworkInfo()
+{
+ print_timestamp_init("LoadLevelArtworkInfo");
+
+ DrawInitText("Looking for custom level artwork", 120, FC_GREEN);
+
+ print_timestamp_time("DrawTimeText");
+
+ LoadArtworkInfoFromLevelInfo(&artwork.gfx_first, leveldir_first_all);
+ print_timestamp_time("LoadArtworkInfoFromLevelInfo (gfx)");
+ LoadArtworkInfoFromLevelInfo(&artwork.snd_first, leveldir_first_all);
+ print_timestamp_time("LoadArtworkInfoFromLevelInfo (snd)");
+ LoadArtworkInfoFromLevelInfo(&artwork.mus_first, leveldir_first_all);
+ print_timestamp_time("LoadArtworkInfoFromLevelInfo (mus)");
+
+ SaveArtworkInfoCache();
+
+ print_timestamp_time("SaveArtworkInfoCache");
+
+ /* needed for reloading level artwork not known at ealier stage */
+
+ if (!strEqual(artwork.gfx_current_identifier, setup.graphics_set))
+ {
+ artwork.gfx_current =
+ getTreeInfoFromIdentifier(artwork.gfx_first, setup.graphics_set);
+ if (artwork.gfx_current == NULL)
+ artwork.gfx_current =
+ getTreeInfoFromIdentifier(artwork.gfx_first, GFX_DEFAULT_SUBDIR);
+ if (artwork.gfx_current == NULL)
+ artwork.gfx_current = getFirstValidTreeInfoEntry(artwork.gfx_first);
+ }
+
+ if (!strEqual(artwork.snd_current_identifier, setup.sounds_set))
+ {
+ artwork.snd_current =
+ getTreeInfoFromIdentifier(artwork.snd_first, setup.sounds_set);
+ if (artwork.snd_current == NULL)
+ artwork.snd_current =
+ getTreeInfoFromIdentifier(artwork.snd_first, SND_DEFAULT_SUBDIR);
+ if (artwork.snd_current == NULL)
+ artwork.snd_current = getFirstValidTreeInfoEntry(artwork.snd_first);
+ }
+
+ if (!strEqual(artwork.mus_current_identifier, setup.music_set))
+ {
+ artwork.mus_current =
+ getTreeInfoFromIdentifier(artwork.mus_first, setup.music_set);
+ if (artwork.mus_current == NULL)
+ artwork.mus_current =
+ getTreeInfoFromIdentifier(artwork.mus_first, MUS_DEFAULT_SUBDIR);
+ if (artwork.mus_current == NULL)
+ artwork.mus_current = getFirstValidTreeInfoEntry(artwork.mus_first);
+ }
+
+ print_timestamp_time("getTreeInfoFromIdentifier");
+
+ sortTreeInfo(&artwork.gfx_first);
+ sortTreeInfo(&artwork.snd_first);
+ sortTreeInfo(&artwork.mus_first);
+
+ print_timestamp_time("sortTreeInfo");
+
+#if ENABLE_UNUSED_CODE
+ dumpTreeInfo(artwork.gfx_first, 0);
+ dumpTreeInfo(artwork.snd_first, 0);
+ dumpTreeInfo(artwork.mus_first, 0);
+#endif
+
+ print_timestamp_done("LoadLevelArtworkInfo");
+}
+
+static boolean AddUserLevelSetToLevelInfoExt(char *level_subdir_new)
+{
+ // get level info tree node of first (original) user level set
+ char *level_subdir_old = getLoginName();
+ LevelDirTree *leveldir_old = getTreeInfoFromIdentifier(leveldir_first,
+ level_subdir_old);
+ if (leveldir_old == NULL) // should not happen
+ return FALSE;
+
+ int draw_deactivation_mask = GetDrawDeactivationMask();
+
+ // override draw deactivation mask (temporarily disable drawing)
+ SetDrawDeactivationMask(REDRAW_ALL);
+
+ // load new level set config and add it next to first user level set
+ LoadLevelInfoFromLevelConf(&leveldir_old->next, NULL,
+ leveldir_old->basepath, level_subdir_new);
+
+ // set draw deactivation mask to previous value
+ SetDrawDeactivationMask(draw_deactivation_mask);
+
+ // get level info tree node of newly added user level set
+ LevelDirTree *leveldir_new = getTreeInfoFromIdentifier(leveldir_first,
+ level_subdir_new);
+ if (leveldir_new == NULL) // should not happen
+ return FALSE;
+
+ // correct top link and parent node link of newly created tree node
+ leveldir_new->node_top = leveldir_old->node_top;
+ leveldir_new->node_parent = leveldir_old->node_parent;
+
+ // sort level info tree to adjust position of newly added level set
+ sortTreeInfo(&leveldir_first);
+
+ return TRUE;
+}
+
+void AddUserLevelSetToLevelInfo(char *level_subdir_new)
+{
+ if (!AddUserLevelSetToLevelInfoExt(level_subdir_new))
+ Error(ERR_EXIT, "internal level set structure corrupted -- aborting");
+}
+
+char *getArtworkIdentifierForUserLevelSet(int type)
+{
+ char *classic_artwork_set = getClassicArtworkSet(type);
+
+ /* check for custom artwork configured in "levelinfo.conf" */
+ char *leveldir_artwork_set =
+ *LEVELDIR_ARTWORK_SET_PTR(leveldir_current, type);
+ boolean has_leveldir_artwork_set =
+ (leveldir_artwork_set != NULL && !strEqual(leveldir_artwork_set,
+ classic_artwork_set));
+
+ /* check for custom artwork in sub-directory "graphics" etc. */
+ TreeInfo *artwork_first_node = ARTWORK_FIRST_NODE(artwork, type);
+ char *leveldir_identifier = leveldir_current->identifier;
+ boolean has_artwork_subdir =
+ (getTreeInfoFromIdentifier(artwork_first_node,
+ leveldir_identifier) != NULL);
+
+ return (has_leveldir_artwork_set ? leveldir_artwork_set :
+ has_artwork_subdir ? leveldir_identifier :
+ classic_artwork_set);
+}
+
+TreeInfo *getArtworkTreeInfoForUserLevelSet(int type)
+{
+ char *artwork_set = getArtworkIdentifierForUserLevelSet(type);
+ TreeInfo *artwork_first_node = ARTWORK_FIRST_NODE(artwork, type);
+
+ return getTreeInfoFromIdentifier(artwork_first_node, artwork_set);
+}
+
+boolean checkIfCustomArtworkExistsForCurrentLevelSet()
+{
+ char *graphics_set =
+ getArtworkIdentifierForUserLevelSet(ARTWORK_TYPE_GRAPHICS);
+ char *sounds_set =
+ getArtworkIdentifierForUserLevelSet(ARTWORK_TYPE_SOUNDS);
+ char *music_set =
+ getArtworkIdentifierForUserLevelSet(ARTWORK_TYPE_MUSIC);
+
+ return (!strEqual(graphics_set, GFX_CLASSIC_SUBDIR) ||
+ !strEqual(sounds_set, SND_CLASSIC_SUBDIR) ||
+ !strEqual(music_set, MUS_CLASSIC_SUBDIR));
+}
+
+boolean UpdateUserLevelSet(char *level_subdir, char *level_name,
+ char *level_author, int num_levels)
+{
+ char *filename = getPath2(getUserLevelDir(level_subdir), LEVELINFO_FILENAME);
+ char *filename_tmp = getStringCat2(filename, ".tmp");
+ FILE *file = NULL;
+ FILE *file_tmp = NULL;
+ char line[MAX_LINE_LEN];
+ boolean success = FALSE;
+ LevelDirTree *leveldir = getTreeInfoFromIdentifier(leveldir_first,
+ level_subdir);
+ // update values in level directory tree
+
+ if (level_name != NULL)
+ setString(&leveldir->name, level_name);
+
+ if (level_author != NULL)
+ setString(&leveldir->author, level_author);
+
+ if (num_levels != -1)
+ leveldir->levels = num_levels;
+
+ // update values that depend on other values
+
+ setString(&leveldir->name_sorting, leveldir->name);
+
+ leveldir->last_level = leveldir->first_level + leveldir->levels - 1;
+
+ // sort order of level sets may have changed
+ sortTreeInfo(&leveldir_first);
+
+ if ((file = fopen(filename, MODE_READ)) &&
+ (file_tmp = fopen(filename_tmp, MODE_WRITE)))
+ {
+ while (fgets(line, MAX_LINE_LEN, file))
+ {
+ if (strPrefix(line, "name:") && level_name != NULL)
+ fprintf(file_tmp, "%-32s%s\n", "name:", level_name);
+ else if (strPrefix(line, "author:") && level_author != NULL)
+ fprintf(file_tmp, "%-32s%s\n", "author:", level_author);
+ else if (strPrefix(line, "levels:") && num_levels != -1)
+ fprintf(file_tmp, "%-32s%d\n", "levels:", num_levels);
+ else
+ fputs(line, file_tmp);
+ }
+
+ success = TRUE;
+ }
+
+ if (file)
+ fclose(file);
+
+ if (file_tmp)
+ fclose(file_tmp);
+
+ if (success)
+ success = (rename(filename_tmp, filename) == 0);
+
+ free(filename);
+ free(filename_tmp);
+
+ return success;
+}
+
+boolean CreateUserLevelSet(char *level_subdir, char *level_name,
+ char *level_author, int num_levels,
+ boolean use_artwork_set)