+ checked_free(command_basepath);
+ checked_free(command_basename);
+
+ checked_free(command_filename_1);
+ checked_free(command_filename_2);
+
+ initialized = TRUE;
+ }
+
+ // 1st try: look for config file that exactly matches the binary filename
+ if (fileExists(config_filename_1))
+ return config_filename_1;
+
+ // 2nd try: look for config file that matches binary filename without suffix
+ if (fileExists(config_filename_2))
+ return config_filename_2;
+
+ // 3rd try: return setup config filename in global program config directory
+ return config_filename_3;
+}
+
+static char *getPlatformConfigFilename(char *config_filename)
+{
+ static char *platform_config_filename = NULL;
+ static boolean initialized = FALSE;
+
+ if (!initialized)
+ {
+ char *config_basepath = getBasePath(config_filename);
+ char *config_basename = getBaseNameNoSuffix(config_filename);
+ char *config_filename_prefix = getPath2(config_basepath, config_basename);
+ char *platform_string_lower = getStringToLower(PLATFORM_STRING);
+ char *platform_suffix = getStringCat2("-", platform_string_lower);
+
+ platform_config_filename = getStringCat3(config_filename_prefix,
+ platform_suffix, ".conf");
+
+ checked_free(config_basepath);
+ checked_free(config_basename);
+ checked_free(config_filename_prefix);
+ checked_free(platform_string_lower);
+ checked_free(platform_suffix);
+
+ initialized = TRUE;
+ }
+
+ return platform_config_filename;
+}
+
+char *getTapeFilename(int nr)
+{
+ static char *filename = NULL;
+ char basename[MAX_FILENAME_LEN];
+
+ checked_free(filename);
+
+ sprintf(basename, "%03d.%s", nr, TAPEFILE_EXTENSION);
+ filename = getPath2(getTapeDir(leveldir_current->subdir), basename);
+
+ return filename;
+}
+
+char *getTemporaryTapeFilename(void)
+{
+ static char *filename = NULL;
+ char basename[MAX_FILENAME_LEN];
+
+ checked_free(filename);
+
+ sprintf(basename, "tmp.%s", TAPEFILE_EXTENSION);
+ filename = getPath2(getTapeDir(NULL), basename);
+
+ return filename;
+}
+
+char *getDefaultSolutionTapeFilename(int nr)
+{
+ static char *filename = NULL;
+ char basename[MAX_FILENAME_LEN];
+
+ checked_free(filename);
+
+ sprintf(basename, "%03d.%s", nr, TAPEFILE_EXTENSION);
+ filename = getPath2(getSolutionTapeDir(), basename);
+
+ return filename;
+}
+
+char *getSokobanSolutionTapeFilename(int nr)
+{
+ static char *filename = NULL;
+ char basename[MAX_FILENAME_LEN];
+
+ checked_free(filename);
+
+ sprintf(basename, "%03d.sln", nr);
+ filename = getPath2(getSolutionTapeDir(), basename);
+
+ return filename;
+}
+
+char *getSolutionTapeFilename(int nr)
+{
+ char *filename = getDefaultSolutionTapeFilename(nr);
+
+ if (!fileExists(filename))
+ {
+ char *filename2 = getSokobanSolutionTapeFilename(nr);
+
+ if (fileExists(filename2))
+ return filename2;
+ }
+
+ return filename;
+}
+
+char *getScoreFilename(int nr)
+{
+ static char *filename = NULL;
+ char basename[MAX_FILENAME_LEN];
+
+ checked_free(filename);
+
+ sprintf(basename, "%03d.%s", nr, SCOREFILE_EXTENSION);
+
+ // used instead of "leveldir_current->subdir" (for network games)
+ filename = getPath2(getScoreDir(levelset.identifier), basename);
+
+ return filename;
+}
+
+char *getScoreCacheFilename(int nr)
+{
+ static char *filename = NULL;
+ char basename[MAX_FILENAME_LEN];
+
+ checked_free(filename);
+
+ sprintf(basename, "%03d.%s", nr, SCOREFILE_EXTENSION);
+
+ // used instead of "leveldir_current->subdir" (for network games)
+ filename = getPath2(getScoreCacheDir(levelset.identifier), basename);
+
+ return filename;
+}
+
+char *getScoreTapeBasename(char *name)
+{
+ static char basename[MAX_FILENAME_LEN];
+ char basename_raw[MAX_FILENAME_LEN];
+ char timestamp[20];
+
+ sprintf(timestamp, "%s", getCurrentTimestamp());
+ sprintf(basename_raw, "%s-%s", timestamp, name);
+ sprintf(basename, "%s-%08x", timestamp, get_hash_from_string(basename_raw));
+
+ return basename;
+}
+
+char *getScoreTapeFilename(char *basename_no_ext, int nr)
+{
+ static char *filename = NULL;
+ char basename[MAX_FILENAME_LEN];
+
+ checked_free(filename);
+
+ sprintf(basename, "%s.%s", basename_no_ext, TAPEFILE_EXTENSION);
+
+ // used instead of "leveldir_current->subdir" (for network games)
+ filename = getPath2(getScoreTapeDir(levelset.identifier, nr), basename);
+
+ return filename;
+}
+
+char *getScoreCacheTapeFilename(char *basename_no_ext, int nr)
+{
+ static char *filename = NULL;
+ char basename[MAX_FILENAME_LEN];
+
+ checked_free(filename);
+
+ sprintf(basename, "%s.%s", basename_no_ext, TAPEFILE_EXTENSION);
+
+ // used instead of "leveldir_current->subdir" (for network games)
+ filename = getPath2(getScoreCacheTapeDir(levelset.identifier, nr), basename);
+
+ return filename;
+}
+
+char *getSetupFilename(void)
+{
+ static char *filename = NULL;
+
+ checked_free(filename);
+
+ filename = getPath2(getSetupDir(), SETUP_FILENAME);
+
+ return filename;
+}
+
+char *getDefaultSetupFilename(void)
+{
+ return program.config_filename;
+}
+
+char *getPlatformSetupFilename(void)
+{
+ return getPlatformConfigFilename(program.config_filename);
+}
+
+char *getEditorSetupFilename(void)
+{
+ static char *filename = NULL;
+
+ checked_free(filename);
+ filename = getPath2(getCurrentLevelDir(), EDITORSETUP_FILENAME);
+
+ if (fileExists(filename))
+ return filename;
+
+ checked_free(filename);
+ filename = getPath2(getSetupDir(), EDITORSETUP_FILENAME);
+
+ return filename;
+}
+
+char *getHelpAnimFilename(void)
+{
+ static char *filename = NULL;
+
+ checked_free(filename);
+
+ filename = getPath2(getCurrentLevelDir(), HELPANIM_FILENAME);
+
+ return filename;
+}
+
+char *getHelpTextFilename(void)
+{
+ static char *filename = NULL;
+
+ checked_free(filename);
+
+ filename = getPath2(getCurrentLevelDir(), HELPTEXT_FILENAME);
+
+ return filename;
+}
+
+static char *getLevelSetInfoBasename(int nr)
+{
+ static char basename[32];
+
+ sprintf(basename, "levelset_%d.txt", nr + 1);
+
+ return basename;
+}
+
+char *getLevelSetInfoFilename(int nr)
+{
+ char *basename = getLevelSetInfoBasename(nr);
+ static char *info_subdir = NULL;
+ static char *filename = NULL;
+
+ if (info_subdir == NULL)
+ info_subdir = getPath2(DOCS_DIRECTORY, LEVELSET_INFO_DIRECTORY);
+
+ checked_free(filename);
+
+ // look for level set info file the current level set directory
+ filename = getPath3(getCurrentLevelDir(), info_subdir, basename);
+ if (fileExists(filename))
+ return filename;
+
+ if (nr > 0)
+ return 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;
+}
+
+static 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 *getCreditsBasename(int nr)
+{
+ static char basename[32];
+
+ sprintf(basename, "credits_%d.txt", nr + 1);
+
+ return basename;
+}
+
+char *getCreditsFilename(int nr, boolean global)
+{
+ char *basename = getCreditsBasename(nr);
+ char *basepath = NULL;
+ static char *credits_subdir = NULL;
+ static char *filename = NULL;
+
+ if (credits_subdir == NULL)
+ credits_subdir = getPath2(DOCS_DIRECTORY, CREDITS_DIRECTORY);
+
+ checked_free(filename);
+
+ // look for credits file in the game's base or current level set directory
+ basepath = (global ? options.base_directory : getCurrentLevelDir());
+
+ filename = getPath3(basepath, credits_subdir, basename);
+ if (fileExists(filename))
+ return filename;
+
+ return NULL; // cannot find credits file
+}
+
+static char *getProgramInfoBasename(int nr)
+{
+ static char basename[32];
+
+ sprintf(basename, "program_%d.txt", nr + 1);
+
+ return basename;
+}
+
+char *getProgramInfoFilename(int nr)
+{
+ char *basename = getProgramInfoBasename(nr);
+ static char *info_subdir = NULL;
+ static char *filename = NULL;
+
+ if (info_subdir == NULL)
+ info_subdir = getPath2(DOCS_DIRECTORY, PROGRAM_INFO_DIRECTORY);
+
+ checked_free(filename);
+
+ // look for program info file in the game's base directory
+ filename = getPath3(options.base_directory, info_subdir, basename);
+ if (fileExists(filename))
+ return filename;
+
+ return NULL; // cannot find program info file
+}
+
+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);
+
+ WarnUsingFallback(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);
+
+ WarnUsingFallback(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);
+
+ WarnUsingFallback(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;
+}
+
+static boolean directoryExists_CheckMusic(char *directory, boolean check_music)
+{
+ if (!directoryExists(directory))
+ return FALSE;
+
+ if (!check_music)
+ return TRUE;
+
+ Directory *dir;
+ DirectoryEntry *dir_entry;
+ int num_music = getMusicListSize();
+ boolean music_found = FALSE;
+
+ if ((dir = openDirectory(directory)) == NULL)
+ return FALSE;
+
+ while ((dir_entry = readDirectory(dir)) != NULL) // loop all entries
+ {
+ char *basename = dir_entry->basename;
+ boolean music_already_used = FALSE;
+ int i;
+
+ // skip all music files that are configured in music config file
+ for (i = 0; i < num_music; i++)
+ {
+ struct FileInfo *music = getMusicListEntry(i);
+
+ if (strEqual(basename, music->filename))
+ {
+ music_already_used = TRUE;
+
+ break;
+ }
+ }
+
+ if (music_already_used)
+ continue;
+
+ if (FileIsMusic(dir_entry->filename))
+ {
+ music_found = TRUE;
+
+ break;
+ }
+ }
+
+ closeDirectory(dir);
+
+ return music_found;
+}
+
+static char *getCustomMusicDirectoryExt(boolean check_music)
+{
+ 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_CheckMusic(directory, check_music))
+ 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));
+
+ // directory also valid if no unconfigured music found (no game music)
+ if (directoryExists_CheckMusic(directory, FALSE))
+ 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));
+
+ // directory also valid if no unconfigured music found (no game music)
+ if (directoryExists_CheckMusic(directory, FALSE))
+ return directory;
+
+ free(directory);
+ }
+
+ // 4th try: look for default artwork in new default artwork directory
+ directory = getStringCopy(getDefaultMusicDir(MUS_DEFAULT_SUBDIR));
+ if (directoryExists_CheckMusic(directory, check_music))
+ return directory;
+
+ free(directory);
+
+ // 5th try: look for default artwork in old default artwork directory
+ directory = getStringCopy(options.music_directory);
+ if (directoryExists_CheckMusic(directory, check_music))
+ return directory;
+
+ return NULL; // cannot find specified artwork file anywhere
+}
+
+char *getCustomMusicDirectory(void)
+{
+ return getCustomMusicDirectoryExt(FALSE);
+}
+
+char *getCustomMusicDirectory_NoConf(void)
+{
+ return getCustomMusicDirectoryExt(TRUE);
+}
+
+void MarkTapeDirectoryUploadsAsComplete(char *level_subdir)
+{
+ char *filename = getPath2(getTapeDir(level_subdir), UPLOADED_FILENAME);
+
+ touchFile(filename);
+
+ checked_free(filename);
+}
+
+void MarkTapeDirectoryUploadsAsIncomplete(char *level_subdir)
+{
+ char *filename = getPath2(getTapeDir(level_subdir), UPLOADED_FILENAME);
+
+ unlink(filename);
+
+ checked_free(filename);
+}
+
+boolean CheckTapeDirectoryUploadsComplete(char *level_subdir)
+{
+ char *filename = getPath2(getTapeDir(level_subdir), UPLOADED_FILENAME);
+ boolean success = fileExists(filename);
+
+ checked_free(filename);
+
+ return success;
+}
+
+void InitMissingFileHash(void)
+{
+ if (missing_file_hash == NULL)
+ freeSetupFileHash(missing_file_hash);
+
+ missing_file_hash = newSetupFileHash();
+}
+
+void InitTapeDirectory(char *level_subdir)
+{
+ boolean new_tape_dir = !directoryExists(getTapeDir(level_subdir));
+
+ createDirectory(getUserGameDataDir(), "user data");
+ createDirectory(getTapeDir(NULL), "main tape");
+ createDirectory(getTapeDir(level_subdir), "level tape");
+
+ if (new_tape_dir)
+ MarkTapeDirectoryUploadsAsComplete(level_subdir);
+}
+
+void InitScoreDirectory(char *level_subdir)
+{
+ createDirectory(getMainUserGameDataDir(), "main user data");
+ createDirectory(getScoreDir(NULL), "main score");
+ createDirectory(getScoreDir(level_subdir), "level score");
+}
+
+void InitScoreCacheDirectory(char *level_subdir)
+{
+ createDirectory(getMainUserGameDataDir(), "main user data");
+ createDirectory(getCacheDir(), "cache data");
+ createDirectory(getScoreCacheDir(NULL), "main score");
+ createDirectory(getScoreCacheDir(level_subdir), "level score");
+}
+
+void InitScoreTapeDirectory(char *level_subdir, int nr)
+{
+ InitScoreDirectory(level_subdir);
+
+ createDirectory(getScoreTapeDir(level_subdir, nr), "score tape");
+}
+
+void InitScoreCacheTapeDirectory(char *level_subdir, int nr)
+{
+ InitScoreCacheDirectory(level_subdir);
+
+ createDirectory(getScoreCacheTapeDir(level_subdir, nr), "score tape");
+}
+
+static void SaveUserLevelInfo(void);
+
+void InitUserLevelDirectory(char *level_subdir)
+{
+ if (!directoryExists(getUserLevelDir(level_subdir)))
+ {
+ createDirectory(getMainUserGameDataDir(), "main user data");
+ createDirectory(getUserLevelDir(NULL), "main user level");
+
+ if (setup.internal.create_user_levelset)
+ {
+ createDirectory(getUserLevelDir(level_subdir), "user level");
+
+ SaveUserLevelInfo();
+ }
+ }
+}
+
+void InitNetworkLevelDirectory(char *level_subdir)
+{
+ if (!directoryExists(getNetworkLevelDir(level_subdir)))
+ {
+ createDirectory(getMainUserGameDataDir(), "main user data");
+ createDirectory(getNetworkDir(), "network data");
+ createDirectory(getNetworkLevelDir(NULL), "main network level");
+ createDirectory(getNetworkLevelDir(level_subdir), "network level");
+ }
+}
+
+void InitLevelSetupDirectory(char *level_subdir)
+{
+ createDirectory(getUserGameDataDir(), "user data");
+ createDirectory(getLevelSetupDir(NULL), "main level setup");
+ createDirectory(getLevelSetupDir(level_subdir), "level setup");
+}
+
+static void InitCacheDirectory(void)
+{
+ createDirectory(getMainUserGameDataDir(), "main user data");
+ createDirectory(getCacheDir(), "cache data");
+}
+
+
+// ----------------------------------------------------------------------------
+// some functions to handle lists of level and artwork directories
+// ----------------------------------------------------------------------------
+
+TreeInfo *newTreeInfo(void)
+{
+ 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;
+}
+
+void removeTreeInfo(TreeInfo **node_first)
+{
+ TreeInfo *node_old = *node_first;
+
+ *node_first = node_old->next;
+ node_old->next = NULL;
+
+ freeTreeInfo(node_old);
+}
+
+int numTreeInfo(TreeInfo *node)
+{
+ int num = 0;
+
+ while (node)
+ {
+ num++;
+ node = node->next;
+ }
+
+ return num;
+}
+
+boolean validLevelSeries(TreeInfo *node)
+{
+ // in a number of cases, tree node is no valid level set
+ if (node == NULL || node->node_group || node->parent_link || node->is_copy)
+ return FALSE;
+
+ return TRUE;
+}
+
+TreeInfo *getValidLevelSeries(TreeInfo *node, TreeInfo *default_node)
+{
+ if (validLevelSeries(node))
+ return node;
+ else if (node->is_copy)
+ return getTreeInfoFromIdentifier(leveldir_first, node->identifier);
+ else
+ return getFirstValidTreeInfoEntry(default_node);
+}
+
+static TreeInfo *getValidTreeInfoEntryExt(TreeInfo *node, boolean get_next_node)
+{
+ if (node == NULL)
+ return NULL;
+
+ if (node->node_group) // enter node group (step down into tree)
+ return getFirstValidTreeInfoEntry(node->node_group);
+
+ if (node->parent_link) // skip first node (back link) of node group
+ get_next_node = TRUE;
+
+ if (!get_next_node) // get current regular tree node
+ return node;
+
+ // get next regular tree node, or step up until one is found
+ while (node->next == NULL && node->node_parent != NULL)
+ node = node->node_parent;
+
+ return getFirstValidTreeInfoEntry(node->next);
+}
+
+TreeInfo *getFirstValidTreeInfoEntry(TreeInfo *node)
+{
+ return getValidTreeInfoEntryExt(node, FALSE);
+}
+
+TreeInfo *getNextValidTreeInfoEntry(TreeInfo *node)
+{
+ return getValidTreeInfoEntryExt(node, TRUE);
+}
+
+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 getPosFromTreeInfo(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;
+}
+
+static TreeInfo *getTreeInfoFromIdentifierExt(TreeInfo *node, char *identifier,
+ int node_type_wanted)
+{
+ if (identifier == NULL)
+ return NULL;
+
+ while (node)
+ {
+ if (TREE_NODE_TYPE(node) == node_type_wanted &&
+ strEqual(identifier, node->identifier))
+ return node;
+
+ if (node->node_group)
+ {
+ TreeInfo *node_group = getTreeInfoFromIdentifierExt(node->node_group,
+ identifier,
+ node_type_wanted);
+ if (node_group)
+ return node_group;
+ }
+
+ node = node->next;
+ }
+
+ return NULL;
+}
+
+TreeInfo *getTreeInfoFromIdentifier(TreeInfo *node, char *identifier)
+{
+ return getTreeInfoFromIdentifierExt(node, identifier, TREE_NODE_TYPE_DEFAULT);
+}
+
+static 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;
+}
+
+static 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 adjustTreeArtworkForEMC(char **artwork_set_1,
+ char **artwork_set_2,
+ char **artwork_set, boolean prefer_2)
+{
+ // do nothing if neither special artwork set 1 nor 2 are defined
+ if (!*artwork_set_1 && !*artwork_set_2)
+ return FALSE;
+
+ boolean want_1 = (prefer_2 == FALSE);
+ boolean want_2 = (prefer_2 == TRUE);
+ boolean has_only_1 = (!*artwork_set && !*artwork_set_2);
+ boolean has_only_2 = (!*artwork_set && !*artwork_set_1);
+ char *artwork_set_new = NULL;
+
+ // replace missing special artwork 1 or 2 with (optional) standard artwork
+
+ if (!*artwork_set_1)
+ setString(artwork_set_1, *artwork_set);
+
+ if (!*artwork_set_2)
+ setString(artwork_set_2, *artwork_set);
+
+ // set standard artwork to either special artwork 1 or 2, as requested
+
+ if (*artwork_set_1 && (want_1 || has_only_1))
+ artwork_set_new = *artwork_set_1;
+
+ if (*artwork_set_2 && (want_2 || has_only_2))
+ artwork_set_new = *artwork_set_2;
+
+ if (artwork_set_new && !strEqual(*artwork_set, artwork_set_new))
+ {
+ setString(artwork_set, artwork_set_new);
+
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static boolean adjustTreeGraphicsForEMC(TreeInfo *node)
+{
+ boolean settings_changed = FALSE;
+
+ while (node)
+ {
+ settings_changed |= adjustTreeArtworkForEMC(&node->graphics_set_ecs,
+ &node->graphics_set_aga,
+ &node->graphics_set,
+ setup.prefer_aga_graphics);
+ if (node->node_group != NULL)
+ settings_changed |= adjustTreeGraphicsForEMC(node->node_group);
+
+ node = node->next;
+ }
+
+ return settings_changed;
+}
+
+static boolean adjustTreeSoundsForEMC(TreeInfo *node)
+{
+ boolean settings_changed = FALSE;
+
+ while (node)
+ {
+ settings_changed |= adjustTreeArtworkForEMC(&node->sounds_set_default,
+ &node->sounds_set_lowpass,
+ &node->sounds_set,
+ setup.prefer_lowpass_sounds);
+ if (node->node_group != NULL)
+ settings_changed |= adjustTreeSoundsForEMC(node->node_group);
+
+ node = node->next;
+ }
+
+ return settings_changed;
+}
+
+int dumpTreeInfo(TreeInfo *node, int depth)
+{
+ char bullet_list[] = { '-', '*', 'o' };
+ int num_leaf_nodes = 0;
+ int i;
+
+ if (depth == 0)
+ Debug("tree", "Dumping TreeInfo:");
+
+ while (node)
+ {
+ char bullet = bullet_list[depth % ARRAY_SIZE(bullet_list)];
+
+ for (i = 0; i < depth * 2; i++)
+ DebugContinued("", " ");
+
+ DebugContinued("tree", "%c '%s' ['%s] [PARENT: '%s'] %s\n",
+ bullet, node->name, node->identifier,
+ (node->node_parent ? node->node_parent->identifier : "-"),
+ (node->node_group ? "[GROUP]" :
+ node->is_copy ? "[COPY]" : ""));
+
+ if (!node->node_group && !node->parent_link)
+ num_leaf_nodes++;
+
+ /*
+ // use for dumping artwork info tree
+ Debug("tree", "subdir == '%s' ['%s', '%s'] [%d])",
+ node->subdir, node->fullpath, node->basepath, node->in_user_dir);
+ */
+
+ if (node->node_group != NULL)
+ num_leaf_nodes += dumpTreeInfo(node->node_group, depth + 1);
+
+ node = node->next;
+ }
+
+ if (depth == 0)
+ Debug("tree", "Summary: %d leaf nodes found", num_leaf_nodes);
+
+ return num_leaf_nodes;
+}
+
+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