+ 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;
+
+ hashtable_destroy(hash);
+}
+
+char *getHashEntry(SetupFileHash *hash, char *token)
+{
+ if (hash == NULL)
+ return NULL;
+
+ return search_hash_entry(hash, token);
+}
+
+void setHashEntry(SetupFileHash *hash, char *token, char *value)
+{
+ char *value_copy;
+
+ if (hash == NULL)
+ return;
+
+ value_copy = getStringCopy(value);
+
+ // change value; if it does not exist, insert it as new
+ if (!change_hash_entry(hash, token, value_copy))
+ if (!insert_hash_entry(hash, getStringCopy(token), value_copy))
+ Fail("cannot insert into hash -- aborting");
+}
+
+void removeHashEntry(SetupFileHash *hash, char *token)
+{
+ if (hash == NULL)
+ return;
+
+ remove_hash_entry(hash, token);
+}
+
+#if ENABLE_UNUSED_CODE
+#if DEBUG
+static void printSetupFileHash(SetupFileHash *hash)
+{
+ BEGIN_HASH_ITERATION(hash, itr)
+ {
+ Debug("setup:printSetupFileHash", "token: '%s'", HASH_ITERATION_TOKEN(itr));
+ Debug("setup:printSetupFileHash", "value: '%s'", HASH_ITERATION_VALUE(itr));
+ }
+ END_HASH_ITERATION(hash, itr)
+}
+#endif
+#endif
+
+#define ALLOW_TOKEN_VALUE_SEPARATOR_BEING_WHITESPACE 1
+#define CHECK_TOKEN_VALUE_SEPARATOR__WARN_IF_MISSING 0
+#define CHECK_TOKEN__WARN_IF_ALREADY_EXISTS_IN_HASH 0
+
+static boolean token_value_separator_found = FALSE;
+#if CHECK_TOKEN_VALUE_SEPARATOR__WARN_IF_MISSING
+static boolean token_value_separator_warning = FALSE;
+#endif
+#if CHECK_TOKEN__WARN_IF_ALREADY_EXISTS_IN_HASH
+static boolean token_already_exists_warning = FALSE;
+#endif
+
+static boolean getTokenValueFromSetupLineExt(char *line,
+ char **token_ptr, char **value_ptr,
+ char *filename, char *line_raw,
+ int line_nr,
+ boolean separator_required)
+{
+ static char line_copy[MAX_LINE_LEN + 1], line_raw_copy[MAX_LINE_LEN + 1];
+ char *token, *value, *line_ptr;
+
+ // when externally invoked via ReadTokenValueFromLine(), copy line buffers
+ if (line_raw == NULL)
+ {
+ strncpy(line_copy, line, MAX_LINE_LEN);
+ line_copy[MAX_LINE_LEN] = '\0';
+ line = line_copy;
+
+ strcpy(line_raw_copy, line_copy);
+ line_raw = line_raw_copy;
+ }
+
+ // cut trailing comment from input line
+ for (line_ptr = line; *line_ptr; line_ptr++)
+ {
+ if (*line_ptr == '#')
+ {
+ *line_ptr = '\0';
+ break;
+ }
+ }
+
+ // cut trailing whitespaces from input line
+ for (line_ptr = &line[strlen(line)]; line_ptr >= line; line_ptr--)
+ if ((*line_ptr == ' ' || *line_ptr == '\t') && *(line_ptr + 1) == '\0')
+ *line_ptr = '\0';
+
+ // ignore empty lines
+ if (*line == '\0')
+ return FALSE;
+
+ // cut leading whitespaces from token
+ for (token = line; *token; token++)
+ if (*token != ' ' && *token != '\t')
+ break;
+
+ // start with empty value as reliable default
+ value = "";
+
+ token_value_separator_found = FALSE;
+
+ // find end of token to determine start of value
+ for (line_ptr = token; *line_ptr; line_ptr++)
+ {
+ // first look for an explicit token/value separator, like ':' or '='
+ if (*line_ptr == ':' || *line_ptr == '=')
+ {
+ *line_ptr = '\0'; // terminate token string
+ value = line_ptr + 1; // set beginning of value
+
+ token_value_separator_found = TRUE;
+
+ break;
+ }
+ }
+
+#if ALLOW_TOKEN_VALUE_SEPARATOR_BEING_WHITESPACE
+ // fallback: if no token/value separator found, also allow whitespaces
+ if (!token_value_separator_found && !separator_required)
+ {
+ for (line_ptr = token; *line_ptr; line_ptr++)
+ {
+ if (*line_ptr == ' ' || *line_ptr == '\t')
+ {
+ *line_ptr = '\0'; // terminate token string
+ value = line_ptr + 1; // set beginning of value
+
+ token_value_separator_found = TRUE;
+
+ break;
+ }
+ }
+
+#if CHECK_TOKEN_VALUE_SEPARATOR__WARN_IF_MISSING
+ if (token_value_separator_found)
+ {
+ if (!token_value_separator_warning)
+ {
+ Debug("setup", "---");
+
+ if (filename != NULL)
+ {
+ Debug("setup", "missing token/value separator(s) in config file:");
+ Debug("setup", "- config file: '%s'", filename);
+ }
+ else
+ {
+ Debug("setup", "missing token/value separator(s):");
+ }
+
+ token_value_separator_warning = TRUE;
+ }
+
+ if (filename != NULL)
+ Debug("setup", "- line %d: '%s'", line_nr, line_raw);
+ else
+ Debug("setup", "- line: '%s'", line_raw);
+ }
+#endif
+ }
+#endif
+
+ // cut trailing whitespaces from token
+ for (line_ptr = &token[strlen(token)]; line_ptr >= token; line_ptr--)
+ if ((*line_ptr == ' ' || *line_ptr == '\t') && *(line_ptr + 1) == '\0')
+ *line_ptr = '\0';
+
+ // cut leading whitespaces from value
+ for (; *value; value++)
+ if (*value != ' ' && *value != '\t')
+ break;
+
+ *token_ptr = token;
+ *value_ptr = value;
+
+ return TRUE;
+}
+
+boolean getTokenValueFromSetupLine(char *line, char **token, char **value)
+{
+ // while the internal (old) interface does not require a token/value
+ // separator (for downwards compatibility with existing files which
+ // don't use them), it is mandatory for the external (new) interface
+
+ return getTokenValueFromSetupLineExt(line, token, value, NULL, NULL, 0, TRUE);
+}
+
+static boolean loadSetupFileData(void *setup_file_data, char *filename,
+ boolean top_recursion_level, boolean is_hash)
+{
+ static SetupFileHash *include_filename_hash = NULL;
+ char line[MAX_LINE_LEN], line_raw[MAX_LINE_LEN], previous_line[MAX_LINE_LEN];
+ char *token, *value, *line_ptr;
+ void *insert_ptr = NULL;
+ boolean read_continued_line = FALSE;
+ File *file;
+ int line_nr = 0, token_count = 0, include_count = 0;
+
+#if CHECK_TOKEN_VALUE_SEPARATOR__WARN_IF_MISSING
+ token_value_separator_warning = FALSE;
+#endif
+
+#if CHECK_TOKEN__WARN_IF_ALREADY_EXISTS_IN_HASH
+ token_already_exists_warning = FALSE;
+#endif
+
+ if (!(file = openFile(filename, MODE_READ)))
+ {
+#if DEBUG_NO_CONFIG_FILE
+ Debug("setup", "cannot open configuration file '%s'", filename);
+#endif
+
+ return FALSE;
+ }
+
+ // use "insert pointer" to store list end for constant insertion complexity
+ if (!is_hash)
+ insert_ptr = setup_file_data;
+
+ // on top invocation, create hash to mark included files (to prevent loops)
+ if (top_recursion_level)
+ include_filename_hash = newSetupFileHash();
+
+ // mark this file as already included (to prevent including it again)
+ setHashEntry(include_filename_hash, getBaseNamePtr(filename), "true");
+
+ while (!checkEndOfFile(file))
+ {
+ // read next line of input file
+ if (!getStringFromFile(file, line, MAX_LINE_LEN))
+ break;
+
+ // check if line was completely read and is terminated by line break
+ if (strlen(line) > 0 && line[strlen(line) - 1] == '\n')
+ line_nr++;
+
+ // cut trailing line break (this can be newline and/or carriage return)
+ for (line_ptr = &line[strlen(line)]; line_ptr >= line; line_ptr--)
+ if ((*line_ptr == '\n' || *line_ptr == '\r') && *(line_ptr + 1) == '\0')
+ *line_ptr = '\0';
+
+ // copy raw input line for later use (mainly debugging output)
+ strcpy(line_raw, line);
+
+ if (read_continued_line)
+ {
+ // append new line to existing line, if there is enough space
+ if (strlen(previous_line) + strlen(line_ptr) < MAX_LINE_LEN)
+ strcat(previous_line, line_ptr);
+
+ strcpy(line, previous_line); // copy storage buffer to line
+
+ read_continued_line = FALSE;
+ }
+
+ // if the last character is '\', continue at next line
+ if (strlen(line) > 0 && line[strlen(line) - 1] == '\\')
+ {
+ line[strlen(line) - 1] = '\0'; // cut off trailing backslash
+ strcpy(previous_line, line); // copy line to storage buffer
+
+ read_continued_line = TRUE;
+
+ continue;
+ }
+
+ if (!getTokenValueFromSetupLineExt(line, &token, &value, filename,
+ line_raw, line_nr, FALSE))
+ continue;
+
+ if (*token)
+ {
+ if (strEqual(token, "include"))
+ {
+ if (getHashEntry(include_filename_hash, value) == NULL)
+ {
+ char *basepath = getBasePath(filename);
+ char *basename = getBaseName(value);
+ char *filename_include = getPath2(basepath, basename);
+
+ loadSetupFileData(setup_file_data, filename_include, FALSE, is_hash);
+
+ free(basepath);
+ free(basename);
+ free(filename_include);
+
+ include_count++;
+ }
+ else
+ {
+ Warn("ignoring already processed file '%s'", value);
+ }
+ }
+ else
+ {
+ if (is_hash)
+ {
+#if CHECK_TOKEN__WARN_IF_ALREADY_EXISTS_IN_HASH
+ char *old_value =
+ getHashEntry((SetupFileHash *)setup_file_data, token);
+
+ if (old_value != NULL)
+ {
+ if (!token_already_exists_warning)
+ {
+ Debug("setup", "---");
+ Debug("setup", "duplicate token(s) found in config file:");
+ Debug("setup", "- config file: '%s'", filename);
+
+ token_already_exists_warning = TRUE;
+ }
+
+ Debug("setup", "- token: '%s' (in line %d)", token, line_nr);
+ Debug("setup", " old value: '%s'", old_value);
+ Debug("setup", " new value: '%s'", value);
+ }
+#endif
+
+ setHashEntry((SetupFileHash *)setup_file_data, token, value);
+ }
+ else
+ {
+ insert_ptr = addListEntry((SetupFileList *)insert_ptr, token, value);
+ }
+
+ token_count++;
+ }
+ }
+ }
+
+ closeFile(file);
+
+#if CHECK_TOKEN_VALUE_SEPARATOR__WARN_IF_MISSING
+ if (token_value_separator_warning)
+ Debug("setup", "---");
+#endif
+
+#if CHECK_TOKEN__WARN_IF_ALREADY_EXISTS_IN_HASH
+ if (token_already_exists_warning)
+ Debug("setup", "---");
+#endif
+
+ if (token_count == 0 && include_count == 0)
+ Warn("configuration file '%s' is empty", filename);
+
+ if (top_recursion_level)
+ freeSetupFileHash(include_filename_hash);
+
+ return TRUE;
+}
+
+static int compareSetupFileData(const void *object1, const void *object2)
+{
+ const struct ConfigInfo *entry1 = (struct ConfigInfo *)object1;
+ const struct ConfigInfo *entry2 = (struct ConfigInfo *)object2;
+
+ return strcmp(entry1->token, entry2->token);
+}
+
+static void saveSetupFileHash(SetupFileHash *hash, char *filename)
+{
+ int item_count = hashtable_count(hash);
+ int item_size = sizeof(struct ConfigInfo);
+ struct ConfigInfo *sort_array = checked_malloc(item_count * item_size);
+ FILE *file;
+ int i = 0;
+
+ // copy string pointers from hash to array
+ BEGIN_HASH_ITERATION(hash, itr)
+ {
+ sort_array[i].token = HASH_ITERATION_TOKEN(itr);
+ sort_array[i].value = HASH_ITERATION_VALUE(itr);
+
+ i++;
+
+ if (i > item_count) // should never happen
+ break;
+ }
+ END_HASH_ITERATION(hash, itr)
+
+ // sort string pointers from hash in array
+ qsort(sort_array, item_count, item_size, compareSetupFileData);
+
+ if (!(file = fopen(filename, MODE_WRITE)))
+ {
+ Warn("cannot write configuration file '%s'", filename);
+
+ return;
+ }
+
+ fprintf(file, "%s\n\n", getFormattedSetupEntry("program.version",
+ program.version_string));
+ for (i = 0; i < item_count; i++)
+ fprintf(file, "%s\n", getFormattedSetupEntry(sort_array[i].token,
+ sort_array[i].value));
+ fclose(file);
+
+ checked_free(sort_array);
+}
+
+SetupFileList *loadSetupFileList(char *filename)
+{
+ SetupFileList *setup_file_list = newSetupFileList("", "");
+ SetupFileList *first_valid_list_entry;
+
+ if (!loadSetupFileData(setup_file_list, filename, TRUE, FALSE))
+ {
+ freeSetupFileList(setup_file_list);
+
+ return NULL;
+ }
+
+ first_valid_list_entry = setup_file_list->next;
+
+ // free empty list header
+ setup_file_list->next = NULL;
+ freeSetupFileList(setup_file_list);
+
+ return first_valid_list_entry;
+}
+
+SetupFileHash *loadSetupFileHash(char *filename)
+{
+ SetupFileHash *setup_file_hash = newSetupFileHash();
+
+ if (!loadSetupFileData(setup_file_hash, filename, TRUE, TRUE))
+ {
+ freeSetupFileHash(setup_file_hash);
+
+ return NULL;
+ }
+
+ return setup_file_hash;
+}
+
+
+// ============================================================================
+// setup file stuff
+// ============================================================================
+
+#define TOKEN_STR_LAST_LEVEL_SERIES "last_level_series"
+#define TOKEN_STR_LAST_PLAYED_MENU_USED "last_played_menu_used"
+#define TOKEN_STR_LAST_PLAYED_LEVEL "last_played_level"
+#define TOKEN_STR_HANDICAP_LEVEL "handicap_level"
+#define TOKEN_STR_LAST_USER "last_user"
+
+// level directory info
+#define LEVELINFO_TOKEN_IDENTIFIER 0
+#define LEVELINFO_TOKEN_NAME 1
+#define LEVELINFO_TOKEN_NAME_SORTING 2
+#define LEVELINFO_TOKEN_AUTHOR 3
+#define LEVELINFO_TOKEN_YEAR 4
+#define LEVELINFO_TOKEN_PROGRAM_TITLE 5
+#define LEVELINFO_TOKEN_PROGRAM_COPYRIGHT 6
+#define LEVELINFO_TOKEN_PROGRAM_COMPANY 7
+#define LEVELINFO_TOKEN_IMPORTED_FROM 8
+#define LEVELINFO_TOKEN_IMPORTED_BY 9
+#define LEVELINFO_TOKEN_TESTED_BY 10
+#define LEVELINFO_TOKEN_LEVELS 11
+#define LEVELINFO_TOKEN_FIRST_LEVEL 12
+#define LEVELINFO_TOKEN_SORT_PRIORITY 13
+#define LEVELINFO_TOKEN_LATEST_ENGINE 14
+#define LEVELINFO_TOKEN_LEVEL_GROUP 15
+#define LEVELINFO_TOKEN_READONLY 16
+#define LEVELINFO_TOKEN_GRAPHICS_SET_ECS 17
+#define LEVELINFO_TOKEN_GRAPHICS_SET_AGA 18
+#define LEVELINFO_TOKEN_GRAPHICS_SET 19
+#define LEVELINFO_TOKEN_SOUNDS_SET_DEFAULT 20
+#define LEVELINFO_TOKEN_SOUNDS_SET_LOWPASS 21
+#define LEVELINFO_TOKEN_SOUNDS_SET 22
+#define LEVELINFO_TOKEN_MUSIC_SET 23
+#define LEVELINFO_TOKEN_FILENAME 24
+#define LEVELINFO_TOKEN_FILETYPE 25
+#define LEVELINFO_TOKEN_SPECIAL_FLAGS 26
+#define LEVELINFO_TOKEN_EMPTY_LEVEL_NAME 27
+#define LEVELINFO_TOKEN_FORCE_LEVEL_NAME 28
+#define LEVELINFO_TOKEN_HANDICAP 29
+#define LEVELINFO_TOKEN_TIME_LIMIT 30
+#define LEVELINFO_TOKEN_SKIP_LEVELS 31
+#define LEVELINFO_TOKEN_USE_EMC_TILES 32
+#define LEVELINFO_TOKEN_INFO_SCREENS_FROM_MAIN 33
+
+#define NUM_LEVELINFO_TOKENS 34
+
+static LevelDirTree ldi;
+
+static struct TokenInfo levelinfo_tokens[] =
+{
+ // level directory info
+ { TYPE_STRING, &ldi.identifier, "identifier" },
+ { TYPE_STRING, &ldi.name, "name" },
+ { TYPE_STRING, &ldi.name_sorting, "name_sorting" },
+ { TYPE_STRING, &ldi.author, "author" },
+ { TYPE_STRING, &ldi.year, "year" },
+ { TYPE_STRING, &ldi.program_title, "program_title" },
+ { TYPE_STRING, &ldi.program_copyright, "program_copyright" },
+ { TYPE_STRING, &ldi.program_company, "program_company" },
+ { TYPE_STRING, &ldi.imported_from, "imported_from" },
+ { TYPE_STRING, &ldi.imported_by, "imported_by" },
+ { TYPE_STRING, &ldi.tested_by, "tested_by" },
+ { TYPE_INTEGER, &ldi.levels, "levels" },
+ { TYPE_INTEGER, &ldi.first_level, "first_level" },
+ { TYPE_INTEGER, &ldi.sort_priority, "sort_priority" },
+ { TYPE_BOOLEAN, &ldi.latest_engine, "latest_engine" },
+ { TYPE_BOOLEAN, &ldi.level_group, "level_group" },
+ { TYPE_BOOLEAN, &ldi.readonly, "readonly" },
+ { TYPE_STRING, &ldi.graphics_set_ecs, "graphics_set.ecs" },
+ { TYPE_STRING, &ldi.graphics_set_aga, "graphics_set.aga" },
+ { TYPE_STRING, &ldi.graphics_set, "graphics_set" },
+ { TYPE_STRING, &ldi.sounds_set_default,"sounds_set.default" },
+ { TYPE_STRING, &ldi.sounds_set_lowpass,"sounds_set.lowpass" },
+ { TYPE_STRING, &ldi.sounds_set, "sounds_set" },
+ { TYPE_STRING, &ldi.music_set, "music_set" },
+ { TYPE_STRING, &ldi.level_filename, "filename" },
+ { TYPE_STRING, &ldi.level_filetype, "filetype" },
+ { TYPE_STRING, &ldi.special_flags, "special_flags" },
+ { TYPE_STRING, &ldi.empty_level_name, "empty_level_name" },
+ { TYPE_BOOLEAN, &ldi.force_level_name, "force_level_name" },
+ { TYPE_BOOLEAN, &ldi.handicap, "handicap" },
+ { TYPE_BOOLEAN, &ldi.time_limit, "time_limit" },
+ { TYPE_BOOLEAN, &ldi.skip_levels, "skip_levels" },
+ { TYPE_BOOLEAN, &ldi.use_emc_tiles, "use_emc_tiles" },
+ { TYPE_BOOLEAN, &ldi.info_screens_from_main, "info_screens_from_main" }
+};
+
+static struct TokenInfo artworkinfo_tokens[] =
+{
+ // artwork directory info
+ { TYPE_STRING, &ldi.identifier, "identifier" },
+ { TYPE_STRING, &ldi.subdir, "subdir" },
+ { TYPE_STRING, &ldi.name, "name" },
+ { TYPE_STRING, &ldi.name_sorting, "name_sorting" },
+ { TYPE_STRING, &ldi.author, "author" },
+ { TYPE_STRING, &ldi.program_title, "program_title" },
+ { TYPE_STRING, &ldi.program_copyright, "program_copyright" },
+ { TYPE_STRING, &ldi.program_company, "program_company" },
+ { TYPE_INTEGER, &ldi.sort_priority, "sort_priority" },
+ { TYPE_STRING, &ldi.basepath, "basepath" },
+ { TYPE_STRING, &ldi.fullpath, "fullpath" },
+ { TYPE_BOOLEAN, &ldi.in_user_dir, "in_user_dir" },
+ { TYPE_STRING, &ldi.class_desc, "class_desc" },
+
+ { -1, NULL, NULL },
+};
+
+static char *optional_tokens[] =
+{
+ "program_title",
+ "program_copyright",
+ "program_company",
+
+ NULL
+};
+
+static void setTreeInfoToDefaults(TreeInfo *ti, int type)
+{
+ ti->type = type;
+
+ ti->node_top = (ti->type == TREE_TYPE_LEVEL_DIR ? &leveldir_first :
+ ti->type == TREE_TYPE_GRAPHICS_DIR ? &artwork.gfx_first :
+ ti->type == TREE_TYPE_SOUNDS_DIR ? &artwork.snd_first :
+ ti->type == TREE_TYPE_MUSIC_DIR ? &artwork.mus_first :
+ NULL);
+
+ ti->node_parent = NULL;
+ ti->node_group = NULL;
+ ti->next = NULL;
+
+ ti->cl_first = -1;
+ ti->cl_cursor = -1;
+
+ ti->subdir = NULL;
+ ti->fullpath = NULL;
+ ti->basepath = NULL;
+ ti->identifier = NULL;
+ ti->name = getStringCopy(ANONYMOUS_NAME);
+ ti->name_sorting = NULL;
+ ti->author = getStringCopy(ANONYMOUS_NAME);
+ ti->year = NULL;
+
+ ti->program_title = NULL;
+ ti->program_copyright = NULL;
+ ti->program_company = NULL;
+
+ ti->sort_priority = LEVELCLASS_UNDEFINED; // default: least priority
+ ti->latest_engine = FALSE; // default: get from level
+ ti->parent_link = FALSE;
+ ti->is_copy = FALSE;
+ ti->in_user_dir = FALSE;
+ ti->user_defined = FALSE;
+ ti->color = 0;
+ ti->class_desc = NULL;
+
+ ti->infotext = getStringCopy(TREE_INFOTEXT(ti->type));
+
+ if (ti->type == TREE_TYPE_LEVEL_DIR)
+ {
+ ti->imported_from = NULL;
+ ti->imported_by = NULL;
+ ti->tested_by = NULL;
+
+ ti->graphics_set_ecs = NULL;
+ ti->graphics_set_aga = NULL;
+ ti->graphics_set = NULL;
+ ti->sounds_set_default = NULL;
+ ti->sounds_set_lowpass = NULL;
+ ti->sounds_set = NULL;
+ ti->music_set = NULL;
+ ti->graphics_path = getStringCopy(UNDEFINED_FILENAME);
+ ti->sounds_path = getStringCopy(UNDEFINED_FILENAME);
+ ti->music_path = getStringCopy(UNDEFINED_FILENAME);
+
+ ti->level_filename = NULL;
+ ti->level_filetype = NULL;
+
+ ti->special_flags = NULL;
+
+ ti->empty_level_name = NULL;
+ ti->force_level_name = FALSE;
+
+ ti->levels = 0;
+ ti->first_level = 0;
+ ti->last_level = 0;
+ ti->level_group = FALSE;
+ ti->handicap_level = 0;
+ ti->readonly = TRUE;
+ ti->handicap = TRUE;
+ ti->time_limit = TRUE;
+ ti->skip_levels = FALSE;
+
+ ti->use_emc_tiles = FALSE;
+ ti->info_screens_from_main = FALSE;
+ }
+}
+
+static void setTreeInfoToDefaultsFromParent(TreeInfo *ti, TreeInfo *parent)
+{
+ if (parent == NULL)
+ {
+ Warn("setTreeInfoToDefaultsFromParent(): parent == NULL");
+
+ setTreeInfoToDefaults(ti, TREE_TYPE_UNDEFINED);
+
+ return;
+ }
+
+ // copy all values from the parent structure
+
+ ti->type = parent->type;
+
+ ti->node_top = parent->node_top;
+ ti->node_parent = parent;
+ ti->node_group = NULL;
+ ti->next = NULL;
+
+ ti->cl_first = -1;
+ ti->cl_cursor = -1;
+
+ ti->subdir = NULL;
+ ti->fullpath = NULL;
+ ti->basepath = NULL;
+ ti->identifier = NULL;
+ ti->name = getStringCopy(ANONYMOUS_NAME);
+ ti->name_sorting = NULL;
+ ti->author = getStringCopy(parent->author);
+ ti->year = getStringCopy(parent->year);
+
+ ti->program_title = getStringCopy(parent->program_title);
+ ti->program_copyright = getStringCopy(parent->program_copyright);
+ ti->program_company = getStringCopy(parent->program_company);
+
+ ti->sort_priority = parent->sort_priority;
+ ti->latest_engine = parent->latest_engine;
+ ti->parent_link = FALSE;
+ ti->is_copy = FALSE;
+ ti->in_user_dir = parent->in_user_dir;
+ ti->user_defined = parent->user_defined;
+ ti->color = parent->color;
+ ti->class_desc = getStringCopy(parent->class_desc);
+
+ ti->infotext = getStringCopy(parent->infotext);
+
+ if (ti->type == TREE_TYPE_LEVEL_DIR)
+ {
+ ti->imported_from = getStringCopy(parent->imported_from);
+ ti->imported_by = getStringCopy(parent->imported_by);
+ ti->tested_by = getStringCopy(parent->tested_by);
+
+ ti->graphics_set_ecs = getStringCopy(parent->graphics_set_ecs);
+ ti->graphics_set_aga = getStringCopy(parent->graphics_set_aga);
+ ti->graphics_set = getStringCopy(parent->graphics_set);
+ ti->sounds_set_default = getStringCopy(parent->sounds_set_default);
+ ti->sounds_set_lowpass = getStringCopy(parent->sounds_set_lowpass);
+ ti->sounds_set = getStringCopy(parent->sounds_set);
+ ti->music_set = getStringCopy(parent->music_set);
+ ti->graphics_path = getStringCopy(UNDEFINED_FILENAME);
+ ti->sounds_path = getStringCopy(UNDEFINED_FILENAME);
+ ti->music_path = getStringCopy(UNDEFINED_FILENAME);
+
+ ti->level_filename = getStringCopy(parent->level_filename);
+ ti->level_filetype = getStringCopy(parent->level_filetype);
+
+ ti->special_flags = getStringCopy(parent->special_flags);
+
+ ti->empty_level_name = getStringCopy(parent->empty_level_name);
+ ti->force_level_name = parent->force_level_name;
+
+ ti->levels = parent->levels;
+ ti->first_level = parent->first_level;
+ ti->last_level = parent->last_level;
+ ti->level_group = FALSE;
+ ti->handicap_level = parent->handicap_level;
+ ti->readonly = parent->readonly;
+ ti->handicap = parent->handicap;
+ ti->time_limit = parent->time_limit;
+ ti->skip_levels = parent->skip_levels;
+
+ ti->use_emc_tiles = parent->use_emc_tiles;
+ ti->info_screens_from_main = parent->info_screens_from_main;
+ }
+}
+
+static TreeInfo *getTreeInfoCopy(TreeInfo *ti)
+{
+ TreeInfo *ti_copy = newTreeInfo();
+
+ // copy all values from the original structure
+
+ ti_copy->type = ti->type;
+
+ ti_copy->node_top = ti->node_top;
+ ti_copy->node_parent = ti->node_parent;
+ ti_copy->node_group = ti->node_group;
+ ti_copy->next = ti->next;
+
+ ti_copy->cl_first = ti->cl_first;
+ ti_copy->cl_cursor = ti->cl_cursor;
+
+ ti_copy->subdir = getStringCopy(ti->subdir);
+ ti_copy->fullpath = getStringCopy(ti->fullpath);
+ ti_copy->basepath = getStringCopy(ti->basepath);
+ ti_copy->identifier = getStringCopy(ti->identifier);
+ ti_copy->name = getStringCopy(ti->name);
+ ti_copy->name_sorting = getStringCopy(ti->name_sorting);
+ ti_copy->author = getStringCopy(ti->author);
+ ti_copy->year = getStringCopy(ti->year);
+
+ ti_copy->program_title = getStringCopy(ti->program_title);
+ ti_copy->program_copyright = getStringCopy(ti->program_copyright);
+ ti_copy->program_company = getStringCopy(ti->program_company);
+
+ ti_copy->imported_from = getStringCopy(ti->imported_from);
+ ti_copy->imported_by = getStringCopy(ti->imported_by);
+ ti_copy->tested_by = getStringCopy(ti->tested_by);
+
+ ti_copy->graphics_set_ecs = getStringCopy(ti->graphics_set_ecs);
+ ti_copy->graphics_set_aga = getStringCopy(ti->graphics_set_aga);
+ ti_copy->graphics_set = getStringCopy(ti->graphics_set);
+ ti_copy->sounds_set_default = getStringCopy(ti->sounds_set_default);
+ ti_copy->sounds_set_lowpass = getStringCopy(ti->sounds_set_lowpass);
+ ti_copy->sounds_set = getStringCopy(ti->sounds_set);
+ ti_copy->music_set = getStringCopy(ti->music_set);
+ ti_copy->graphics_path = getStringCopy(ti->graphics_path);
+ ti_copy->sounds_path = getStringCopy(ti->sounds_path);
+ ti_copy->music_path = getStringCopy(ti->music_path);
+
+ ti_copy->level_filename = getStringCopy(ti->level_filename);
+ ti_copy->level_filetype = getStringCopy(ti->level_filetype);
+
+ ti_copy->special_flags = getStringCopy(ti->special_flags);
+
+ ti_copy->empty_level_name = getStringCopy(ti->empty_level_name);
+ ti_copy->force_level_name = ti->force_level_name;
+
+ ti_copy->levels = ti->levels;
+ ti_copy->first_level = ti->first_level;
+ ti_copy->last_level = ti->last_level;
+ ti_copy->sort_priority = ti->sort_priority;
+
+ ti_copy->latest_engine = ti->latest_engine;
+
+ ti_copy->level_group = ti->level_group;
+ ti_copy->parent_link = ti->parent_link;
+ ti_copy->is_copy = ti->is_copy;
+ ti_copy->in_user_dir = ti->in_user_dir;
+ ti_copy->user_defined = ti->user_defined;
+ ti_copy->readonly = ti->readonly;
+ ti_copy->handicap = ti->handicap;
+ ti_copy->time_limit = ti->time_limit;
+ ti_copy->skip_levels = ti->skip_levels;
+
+ ti_copy->use_emc_tiles = ti->use_emc_tiles;
+ ti_copy->info_screens_from_main = ti->info_screens_from_main;
+
+ ti_copy->color = ti->color;
+ ti_copy->class_desc = getStringCopy(ti->class_desc);
+ ti_copy->handicap_level = ti->handicap_level;
+
+ ti_copy->infotext = getStringCopy(ti->infotext);
+
+ return ti_copy;
+}
+
+void freeTreeInfo(TreeInfo *ti)
+{
+ if (ti == NULL)
+ return;
+
+ checked_free(ti->subdir);
+ checked_free(ti->fullpath);
+ checked_free(ti->basepath);
+ checked_free(ti->identifier);
+
+ checked_free(ti->name);
+ checked_free(ti->name_sorting);
+ checked_free(ti->author);
+ checked_free(ti->year);
+
+ checked_free(ti->program_title);
+ checked_free(ti->program_copyright);
+ checked_free(ti->program_company);
+
+ checked_free(ti->class_desc);
+
+ checked_free(ti->infotext);
+
+ if (ti->type == TREE_TYPE_LEVEL_DIR)
+ {
+ checked_free(ti->imported_from);
+ checked_free(ti->imported_by);
+ checked_free(ti->tested_by);
+
+ checked_free(ti->graphics_set_ecs);
+ checked_free(ti->graphics_set_aga);
+ checked_free(ti->graphics_set);
+ checked_free(ti->sounds_set_default);
+ checked_free(ti->sounds_set_lowpass);
+ checked_free(ti->sounds_set);
+ checked_free(ti->music_set);
+
+ checked_free(ti->graphics_path);
+ checked_free(ti->sounds_path);
+ checked_free(ti->music_path);
+
+ checked_free(ti->level_filename);
+ checked_free(ti->level_filetype);
+
+ checked_free(ti->special_flags);
+ }
+
+ // recursively free child node
+ if (ti->node_group)
+ freeTreeInfo(ti->node_group);
+
+ // recursively free next node
+ if (ti->next)
+ freeTreeInfo(ti->next);
+
+ checked_free(ti);
+}
+
+void setSetupInfo(struct TokenInfo *token_info,
+ int token_nr, char *token_value)
+{
+ int token_type = token_info[token_nr].type;
+ void *setup_value = token_info[token_nr].value;
+
+ if (token_value == NULL)
+ return;
+
+ // set setup field to corresponding token value
+ switch (token_type)
+ {
+ case TYPE_BOOLEAN:
+ case TYPE_SWITCH:
+ *(boolean *)setup_value = get_boolean_from_string(token_value);
+ break;
+
+ case TYPE_SWITCH3:
+ *(int *)setup_value = get_switch3_from_string(token_value);
+ break;
+
+ case TYPE_KEY:
+ *(Key *)setup_value = getKeyFromKeyName(token_value);
+ break;
+
+ case TYPE_KEY_X11:
+ *(Key *)setup_value = getKeyFromX11KeyName(token_value);
+ break;
+
+ case TYPE_INTEGER:
+ *(int *)setup_value = get_integer_from_string(token_value);
+ break;
+
+ case TYPE_STRING:
+ checked_free(*(char **)setup_value);
+ *(char **)setup_value = getStringCopy(token_value);
+ break;
+
+ case TYPE_PLAYER:
+ *(int *)setup_value = get_player_nr_from_string(token_value);
+ break;
+
+ default:
+ break;
+ }
+}