+ 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
+ *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_WINDOWS)
+#ifndef S_IRGRP
+#define S_IRGRP S_IRUSR
+#endif
+#ifndef S_IROTH
+#define S_IROTH S_IRUSR
+#endif
+#ifndef S_IWGRP
+#define S_IWGRP S_IWUSR
+#endif
+#ifndef S_IWOTH
+#define S_IWOTH S_IWUSR
+#endif
+#ifndef S_IXGRP
+#define S_IXGRP S_IXUSR
+#endif
+#ifndef S_IXOTH
+#define S_IXOTH S_IXUSR
+#endif
+#ifndef S_IRWXG
+#define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
+#endif
+#ifndef S_ISGID
+#define S_ISGID 0
+#endif
+#endif // PLATFORM_WINDOWS
+
+// file permissions for newly written files
+#define MODE_R_ALL (S_IRUSR | S_IRGRP | S_IROTH)
+#define MODE_W_ALL (S_IWUSR | S_IWGRP | S_IWOTH)
+#define MODE_X_ALL (S_IXUSR | S_IXGRP | S_IXOTH)
+
+#define MODE_W_PRIVATE (S_IWUSR)
+#define MODE_W_PUBLIC_FILE (S_IWUSR | S_IWGRP)
+#define MODE_W_PUBLIC_DIR (S_IWUSR | S_IWGRP | S_ISGID)
+
+#define DIR_PERMS_PRIVATE (MODE_R_ALL | MODE_X_ALL | MODE_W_PRIVATE)
+#define DIR_PERMS_PUBLIC (MODE_R_ALL | MODE_X_ALL | MODE_W_PUBLIC_DIR)
+#define DIR_PERMS_PUBLIC_ALL (MODE_R_ALL | MODE_X_ALL | MODE_W_ALL)
+
+#define FILE_PERMS_PRIVATE (MODE_R_ALL | MODE_W_PRIVATE)
+#define FILE_PERMS_PUBLIC (MODE_R_ALL | MODE_W_PUBLIC_FILE)
+#define FILE_PERMS_PUBLIC_ALL (MODE_R_ALL | MODE_W_ALL)
+
+
+char *getHomeDir(void)
+{
+ static char *dir = NULL;
+
+#if defined(PLATFORM_WINDOWS)
+ if (dir == NULL)
+ {
+ dir = checked_malloc(MAX_PATH + 1);
+
+ if (!SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, dir)))
+ strcpy(dir, ".");
+ }
+#elif defined(PLATFORM_EMSCRIPTEN)
+ dir = PERSISTENT_DIRECTORY;
+#elif defined(PLATFORM_UNIX)
+ if (dir == NULL)
+ {
+ if ((dir = getenv("HOME")) == NULL)
+ {
+ dir = getUnixHomeDir();
+
+ if (dir != NULL)
+ dir = getStringCopy(dir);
+ else
+ dir = ".";
+ }
+ }
+#else
+ dir = ".";
+#endif
+
+ return dir;
+}
+
+char *getPersonalDataDir(void)
+{
+ static char *personal_data_dir = NULL;
+
+#if defined(PLATFORM_MAC)
+ if (personal_data_dir == NULL)
+ personal_data_dir = getPath2(getHomeDir(), "Documents");
+#else
+ if (personal_data_dir == NULL)
+ personal_data_dir = getHomeDir();
+#endif
+
+ return personal_data_dir;
+}
+
+char *getMainUserGameDataDir(void)
+{
+ static char *main_user_data_dir = NULL;
+
+#if defined(PLATFORM_ANDROID)
+ if (main_user_data_dir == NULL)
+ main_user_data_dir = (char *)(SDL_AndroidGetExternalStorageState() &
+ SDL_ANDROID_EXTERNAL_STORAGE_WRITE ?
+ SDL_AndroidGetExternalStoragePath() :
+ SDL_AndroidGetInternalStoragePath());
+#else
+ if (main_user_data_dir == NULL)
+ main_user_data_dir = getPath2(getPersonalDataDir(),
+ program.userdata_subdir);
+#endif
+
+ return main_user_data_dir;
+}
+
+char *getUserGameDataDir(void)
+{
+ if (user.nr == 0)
+ return getMainUserGameDataDir();
+ else
+ return getUserDir(user.nr);
+}
+
+char *getSetupDir(void)
+{
+ return getUserGameDataDir();
+}
+
+static mode_t posix_umask(mode_t mask)
+{
+#if defined(PLATFORM_UNIX)
+ return umask(mask);
+#else
+ return 0;
+#endif
+}
+
+static int posix_mkdir(const char *pathname, mode_t mode)
+{
+#if defined(PLATFORM_WINDOWS)
+ return mkdir(pathname);
+#else
+ return mkdir(pathname, mode);
+#endif
+}
+
+static boolean posix_process_running_setgid(void)
+{
+#if defined(PLATFORM_UNIX)
+ return (getgid() != getegid());
+#else
+ return FALSE;
+#endif
+}
+
+void createDirectory(char *dir, char *text)
+{
+ if (directoryExists(dir))
+ return;
+
+ // leave "other" permissions in umask untouched, but ensure group parts
+ // of USERDATA_DIR_MODE are not masked
+ int permission_class = PERMS_PRIVATE;
+ mode_t dir_mode = (permission_class == PERMS_PRIVATE ?
+ DIR_PERMS_PRIVATE : DIR_PERMS_PUBLIC);
+ mode_t last_umask = posix_umask(0);
+ mode_t group_umask = ~(dir_mode & S_IRWXG);
+ int running_setgid = posix_process_running_setgid();
+
+ if (permission_class == PERMS_PUBLIC)
+ {
+ // if we're setgid, protect files against "other"
+ // else keep umask(0) to make the dir world-writable
+
+ if (running_setgid)
+ posix_umask(last_umask & group_umask);
+ else
+ dir_mode = DIR_PERMS_PUBLIC_ALL;
+ }
+
+ if (posix_mkdir(dir, dir_mode) != 0)
+ Warn("cannot create %s directory '%s': %s", text, dir, strerror(errno));
+
+ if (permission_class == PERMS_PUBLIC && !running_setgid)
+ chmod(dir, dir_mode);
+
+ posix_umask(last_umask); // restore previous umask
+}
+
+void InitMainUserDataDirectory(void)
+{
+ createDirectory(getMainUserGameDataDir(), "main user data");
+}
+
+void InitUserDataDirectory(void)
+{
+ createDirectory(getMainUserGameDataDir(), "main user data");
+
+ if (user.nr != 0)
+ {
+ createDirectory(getUserDir(-1), "users");
+ createDirectory(getUserDir(user.nr), "user data");
+ }
+}
+
+void SetFilePermissions(char *filename, int permission_class)
+{
+ int running_setgid = posix_process_running_setgid();
+ int perms = (permission_class == PERMS_PRIVATE ?
+ FILE_PERMS_PRIVATE : FILE_PERMS_PUBLIC);
+
+ if (permission_class == PERMS_PUBLIC && !running_setgid)
+ perms = FILE_PERMS_PUBLIC_ALL;
+
+ chmod(filename, perms);
+}
+
+void fprintFileHeader(FILE *file, char *basename)
+{
+ char *prefix = "# ";
+ char *sep1 = "=";
+
+ fprintf_line_with_prefix(file, prefix, sep1, 77);
+ fprintf(file, "%s%s\n", prefix, basename);
+ fprintf_line_with_prefix(file, prefix, sep1, 77);
+ fprintf(file, "\n");
+}
+
+int getFileVersionFromCookieString(const char *cookie)
+{
+ const char *ptr_cookie1, *ptr_cookie2;
+ const char *pattern1 = "_FILE_VERSION_";
+ const char *pattern2 = "?.?";
+ const int len_cookie = strlen(cookie);
+ const int len_pattern1 = strlen(pattern1);
+ const int len_pattern2 = strlen(pattern2);
+ const int len_pattern = len_pattern1 + len_pattern2;
+ int version_super, version_major;
+
+ if (len_cookie <= len_pattern)
+ return -1;
+
+ ptr_cookie1 = &cookie[len_cookie - len_pattern];
+ ptr_cookie2 = &cookie[len_cookie - len_pattern2];
+
+ if (strncmp(ptr_cookie1, pattern1, len_pattern1) != 0)
+ return -1;
+
+ if (ptr_cookie2[0] < '0' || ptr_cookie2[0] > '9' ||
+ ptr_cookie2[1] != '.' ||
+ ptr_cookie2[2] < '0' || ptr_cookie2[2] > '9')
+ return -1;
+
+ version_super = ptr_cookie2[0] - '0';
+ version_major = ptr_cookie2[2] - '0';
+
+ return VERSION_IDENT(version_super, version_major, 0, 0);
+}
+
+boolean checkCookieString(const char *cookie, const char *template)
+{
+ const char *pattern = "_FILE_VERSION_?.?";
+ const int len_cookie = strlen(cookie);
+ const int len_template = strlen(template);
+ const int len_pattern = strlen(pattern);
+
+ if (len_cookie != len_template)
+ return FALSE;
+
+ if (strncmp(cookie, template, len_cookie - len_pattern) != 0)
+ return FALSE;
+
+ return TRUE;
+}
+
+
+// ----------------------------------------------------------------------------
+// setup file list and hash handling functions
+// ----------------------------------------------------------------------------
+
+char *getFormattedSetupEntry(char *token, char *value)
+{
+ int i;
+ static char entry[MAX_LINE_LEN];
+
+ // if value is an empty string, just return token without value
+ if (*value == '\0')
+ return token;
+
+ // start with the token and some spaces to format output line
+ sprintf(entry, "%s:", token);
+ for (i = strlen(entry); i < token_value_position; i++)
+ strcat(entry, " ");
+
+ // continue with the token's value
+ strcat(entry, value);
+
+ return entry;
+}
+
+SetupFileList *newSetupFileList(char *token, char *value)
+{
+ SetupFileList *new = checked_malloc(sizeof(SetupFileList));
+
+ new->token = getStringCopy(token);
+ new->value = getStringCopy(value);
+
+ new->next = NULL;
+
+ return new;
+}
+
+void freeSetupFileList(SetupFileList *list)
+{
+ if (list == NULL)
+ return;
+
+ checked_free(list->token);
+ checked_free(list->value);
+
+ if (list->next)
+ freeSetupFileList(list->next);
+
+ free(list);
+}
+
+char *getListEntry(SetupFileList *list, char *token)
+{
+ if (list == NULL)
+ return NULL;
+
+ if (strEqual(list->token, token))
+ return list->value;
+ else
+ return getListEntry(list->next, token);
+}
+
+SetupFileList *setListEntry(SetupFileList *list, char *token, char *value)
+{
+ if (list == NULL)
+ return NULL;
+
+ if (strEqual(list->token, token))
+ {
+ checked_free(list->value);
+
+ list->value = getStringCopy(value);
+
+ return list;
+ }
+ else if (list->next == NULL)
+ return (list->next = newSetupFileList(token, value));
+ else
+ return setListEntry(list->next, token, value);
+}
+
+SetupFileList *addListEntry(SetupFileList *list, char *token, char *value)
+{
+ if (list == NULL)
+ return NULL;
+
+ if (list->next == NULL)
+ return (list->next = newSetupFileList(token, value));
+ else
+ return addListEntry(list->next, token, value);
+}
+
+#if ENABLE_UNUSED_CODE
+#ifdef DEBUG
+static void printSetupFileList(SetupFileList *list)
+{
+ if (!list)
+ return;
+
+ Debug("setup:printSetupFileList", "token: '%s'", list->token);
+ Debug("setup:printSetupFileList", "value: '%s'", list->value);
+
+ printSetupFileList(list->next);
+}
+#endif
+#endif
+
+#ifdef DEBUG
+DEFINE_HASHTABLE_INSERT(insert_hash_entry, char, char);
+DEFINE_HASHTABLE_SEARCH(search_hash_entry, char, char);
+DEFINE_HASHTABLE_CHANGE(change_hash_entry, char, char);
+DEFINE_HASHTABLE_REMOVE(remove_hash_entry, char, char);
+#else
+#define insert_hash_entry hashtable_insert
+#define search_hash_entry hashtable_search
+#define change_hash_entry hashtable_change
+#define remove_hash_entry hashtable_remove
+#endif
+
+unsigned int get_hash_from_string(void *key)
+{
+ /*
+ djb2
+
+ This algorithm (k=33) was first reported by Dan Bernstein many years ago in
+ 'comp.lang.c'. Another version of this algorithm (now favored by Bernstein)
+ uses XOR: hash(i) = hash(i - 1) * 33 ^ str[i]; the magic of number 33 (why
+ it works better than many other constants, prime or not) has never been
+ adequately explained.
+
+ If you just want to have a good hash function, and cannot wait, djb2
+ is one of the best string hash functions i know. It has excellent
+ distribution and speed on many different sets of keys and table sizes.
+ You are not likely to do better with one of the "well known" functions
+ such as PJW, K&R, etc.
+
+ Ozan (oz) Yigit [http://www.cs.yorku.ca/~oz/hash.html]
+ */
+
+ char *str = (char *)key;
+ unsigned int hash = 5381;
+ int c;
+
+ while ((c = *str++))
+ hash = ((hash << 5) + hash) + c; // hash * 33 + c
+
+ return hash;
+}
+
+unsigned int get_hash_from_integer(void *key)
+{
+ unsigned int hash = PTR_TO_UINT(key);
+
+ return hash;
+}
+
+int hash_key_strings_are_equal(void *key1, void *key2)
+{
+ return (strEqual((char *)key1, (char *)key2));
+}
+
+int hash_key_integers_are_equal(void *key1, void *key2)
+{
+ return (key1 == key2);
+}
+
+SetupFileHash *newSetupFileHash(void)
+{
+ SetupFileHash *new_hash =
+ create_hashtable(get_hash_from_string, hash_key_strings_are_equal, free, free);
+
+ if (new_hash == NULL)
+ Fail("create_hashtable() failed -- out of memory");
+
+ return new_hash;
+}
+
+void freeSetupFileHash(SetupFileHash *hash)
+{
+ if (hash == NULL)
+ return;