+ if (success)
+ SDL_RWclose(file);
+ }
+#endif
+
+ return success;
+}
+
+boolean fileExists(char *filename)
+{
+ if (filename == NULL)
+ return FALSE;
+
+ boolean success = (access(filename, F_OK) == 0);
+
+#if defined(PLATFORM_ANDROID)
+ if (!success)
+ {
+ // this might be an asset file; check by trying to open it
+ SDL_RWops *file = SDL_RWFromFile(filename, MODE_READ);
+
+ success = (file != NULL);
+
+ if (success)
+ SDL_RWclose(file);
+ }
+#endif
+
+ return success;
+}
+
+#if 0
+static boolean fileHasPrefix(char *basename, char *prefix)
+{
+ static char *basename_lower = NULL;
+ int basename_length, prefix_length;
+
+ checked_free(basename_lower);
+
+ if (basename == NULL || prefix == NULL)
+ return FALSE;
+
+ basename_lower = getStringToLower(basename);
+ basename_length = strlen(basename_lower);
+ prefix_length = strlen(prefix);
+
+ if (basename_length > prefix_length + 1 &&
+ basename_lower[prefix_length] == '.' &&
+ strncmp(basename_lower, prefix, prefix_length) == 0)
+ return TRUE;
+
+ return FALSE;
+}
+#endif
+
+static boolean fileHasSuffix(char *basename, char *suffix)
+{
+ static char *basename_lower = NULL;
+ int basename_length, suffix_length;
+
+ checked_free(basename_lower);
+
+ if (basename == NULL || suffix == NULL)
+ return FALSE;
+
+ basename_lower = getStringToLower(basename);
+ basename_length = strlen(basename_lower);
+ suffix_length = strlen(suffix);
+
+ if (basename_length > suffix_length + 1 &&
+ basename_lower[basename_length - suffix_length - 1] == '.' &&
+ strEqual(&basename_lower[basename_length - suffix_length], suffix))
+ return TRUE;
+
+ return FALSE;
+}
+
+static boolean FileCouldBeArtwork(char *filename)
+{
+ char *basename = getBaseNamePtr(filename);
+
+ return (!strEqual(basename, ".") &&
+ !strEqual(basename, "..") &&
+ !fileHasSuffix(basename, "txt") &&
+ !fileHasSuffix(basename, "conf") &&
+ !directoryExists(filename));
+}
+
+boolean FileIsGraphic(char *filename)
+{
+ return FileCouldBeArtwork(filename);
+}
+
+boolean FileIsSound(char *filename)
+{
+ return FileCouldBeArtwork(filename);
+}
+
+boolean FileIsMusic(char *filename)
+{
+ return FileCouldBeArtwork(filename);
+}
+
+boolean FileIsArtworkType(char *filename, int type)
+{
+ if ((type == TREE_TYPE_GRAPHICS_DIR && FileIsGraphic(filename)) ||
+ (type == TREE_TYPE_SOUNDS_DIR && FileIsSound(filename)) ||
+ (type == TREE_TYPE_MUSIC_DIR && FileIsMusic(filename)))
+ return TRUE;
+
+ return FALSE;
+}
+
+// ----------------------------------------------------------------------------
+// functions for loading artwork configuration information
+// ----------------------------------------------------------------------------
+
+char *get_mapped_token(char *token)
+{
+ // !!! make this dynamically configurable (init.c:InitArtworkConfig) !!!
+ static char *map_token_prefix[][2] =
+ {
+ { "char_procent", "char_percent" },
+ { "bd_magic_wall_filling", "bd_magic_wall.filling" },
+ { "bd_magic_wall_emptying", "bd_magic_wall.emptying" },
+ { "bd_butterfly_left", "bd_butterfly.left" },
+ { "bd_butterfly_right", "bd_butterfly.right" },
+ { "bd_butterfly_up", "bd_butterfly.up" },
+ { "bd_butterfly_down", "bd_butterfly.down" },
+ { "bd_firefly_left", "bd_firefly.left" },
+ { "bd_firefly_right", "bd_firefly.right" },
+ { "bd_firefly_up", "bd_firefly.up" },
+ { "bd_firefly_down", "bd_firefly.down" },
+
+ { NULL, }
+ };
+ int i;
+
+ for (i = 0; map_token_prefix[i][0] != NULL; i++)
+ {
+ int len_token_prefix = strlen(map_token_prefix[i][0]);
+
+ if (strncmp(token, map_token_prefix[i][0], len_token_prefix) == 0)
+ return getStringCat2(map_token_prefix[i][1], &token[len_token_prefix]);
+ }
+
+ // change tokens containing ".gfx" by moving the "gfx" part to the very left
+ char *gfx_substring = ".gfx";
+ char *gfx_prefix = "gfx.";
+ if (strstr(token, gfx_substring) != NULL)
+ {
+ char *token_prefix = getStringCopy(token);
+ char *token_gfx_pos = strstr(token_prefix, gfx_substring);
+ char *token_suffix = &token_gfx_pos[strlen(gfx_substring)];
+ char *mapped_token;
+
+ // cut off token string at ".gfx" substring position
+ *token_gfx_pos = '\0';
+
+ // put together prefix "gfx." and token prefix and suffix without ".gfx"
+ mapped_token = getStringCat3(gfx_prefix, token_prefix, token_suffix);
+
+ free(token_prefix);
+
+ return mapped_token;
+ }
+
+ return NULL;
+}
+
+static char *get_special_base_token(struct ArtworkListInfo *artwork_info,
+ char *token)
+{
+ // !!! make this dynamically configurable (init.c:InitArtworkConfig) !!!
+ static struct ConfigTypeInfo prefix_list[] =
+ {
+ { "global.anim_1" },
+ { "global.anim_2" },
+ { "global.anim_3" },
+ { "global.anim_4" },
+ { "global.anim_5" },
+ { "global.anim_6" },
+ { "global.anim_7" },
+ { "global.anim_8" },
+ { "global.anim_9" },
+ { "global.anim_10" },
+ { "global.anim_11" },
+ { "global.anim_12" },
+ { "global.anim_13" },
+ { "global.anim_14" },
+ { "global.anim_15" },
+ { "global.anim_16" },
+ { "global.anim_17" },
+ { "global.anim_18" },
+ { "global.anim_19" },
+ { "global.anim_20" },
+ { "global.anim_21" },
+ { "global.anim_22" },
+ { "global.anim_23" },
+ { "global.anim_24" },
+ { "global.anim_25" },
+ { "global.anim_26" },
+ { "global.anim_27" },
+ { "global.anim_28" },
+ { "global.anim_29" },
+ { "global.anim_30" },
+ { "global.anim_31" },
+ { "global.anim_32" },
+
+ { NULL }
+ };
+ struct ConfigTypeInfo *suffix_list = artwork_info->suffix_list;
+ boolean prefix_found = FALSE;
+ int len_suffix = 0;
+ int i;
+
+ // search for prefix to check if base token has to be created
+ for (i = 0; prefix_list[i].token != NULL; i++)
+ if (strPrefix(token, prefix_list[i].token))
+ prefix_found = TRUE;
+
+ if (!prefix_found)
+ return NULL;
+
+ // search for suffix (parameter) to determine base token length
+ for (i = 0; suffix_list[i].token != NULL; i++)
+ if (strSuffix(token, suffix_list[i].token))
+ len_suffix = strlen(suffix_list[i].token);
+
+ return getStringCopyN(token, strlen(token) - len_suffix);
+}
+
+static void FreeCustomArtworkList(struct ArtworkListInfo *,
+ struct ListNodeInfo ***, int *);
+
+struct FileInfo *getFileListFromConfigList(struct ConfigInfo *config_list,
+ struct ConfigTypeInfo *suffix_list,
+ char **ignore_tokens,
+ int num_file_list_entries)
+{
+ SetupFileHash *ignore_tokens_hash;
+ struct FileInfo *file_list;
+ int num_file_list_entries_found = 0;
+ int num_suffix_list_entries = 0;
+ int list_pos;
+ int i, j;
+
+ // create hash from list of tokens to be ignored (for quick access)
+ ignore_tokens_hash = newSetupFileHash();
+ for (i = 0; ignore_tokens[i] != NULL; i++)
+ setHashEntry(ignore_tokens_hash, ignore_tokens[i], "");
+
+ file_list = checked_calloc(num_file_list_entries * sizeof(struct FileInfo));
+
+ for (i = 0; suffix_list[i].token != NULL; i++)
+ num_suffix_list_entries++;
+
+ // always start with reliable default values
+ for (i = 0; i < num_file_list_entries; i++)
+ {
+ file_list[i].token = NULL;
+
+ file_list[i].default_filename = NULL;
+ file_list[i].filename = NULL;
+
+ if (num_suffix_list_entries > 0)
+ {
+ int parameter_array_size = num_suffix_list_entries * sizeof(char *);
+
+ file_list[i].default_parameter = checked_calloc(parameter_array_size);
+ file_list[i].parameter = checked_calloc(parameter_array_size);
+
+ for (j = 0; j < num_suffix_list_entries; j++)
+ {
+ setString(&file_list[i].default_parameter[j], suffix_list[j].value);
+ setString(&file_list[i].parameter[j], suffix_list[j].value);
+ }
+
+ file_list[i].redefined = FALSE;
+ file_list[i].fallback_to_default = FALSE;
+ file_list[i].default_is_cloned = FALSE;
+ }
+ }
+
+ list_pos = 0;
+
+ for (i = 0; config_list[i].token != NULL; i++)
+ {
+ int len_config_token = strlen(config_list[i].token);
+ boolean is_file_entry = TRUE;
+
+ for (j = 0; suffix_list[j].token != NULL; j++)
+ {
+ int len_suffix = strlen(suffix_list[j].token);
+
+ if (len_suffix < len_config_token &&
+ strEqual(&config_list[i].token[len_config_token - len_suffix],
+ suffix_list[j].token))
+ {
+ setString(&file_list[list_pos].default_parameter[j],
+ config_list[i].value);
+
+ is_file_entry = FALSE;
+
+ break;
+ }
+ }
+
+ // the following tokens are no file definitions, but other config tokens
+ if (getHashEntry(ignore_tokens_hash, config_list[i].token) != NULL)
+ is_file_entry = FALSE;
+
+ if (is_file_entry)
+ {
+ if (i > 0)
+ list_pos++;
+
+ if (list_pos >= num_file_list_entries)
+ break;
+
+ file_list[list_pos].token = config_list[i].token;
+ file_list[list_pos].default_filename = config_list[i].value;
+ }
+
+ if (strSuffix(config_list[i].token, ".clone_from"))
+ file_list[list_pos].default_is_cloned = TRUE;
+ }
+
+ num_file_list_entries_found = list_pos + 1;
+ if (num_file_list_entries_found != num_file_list_entries)
+ {
+ Error(ERR_INFO_LINE, "-");
+ Error(ERR_INFO, "inconsistant config list information:");
+ Error(ERR_INFO, "- should be: %d (according to 'src/conf_xxx.h')",
+ num_file_list_entries);
+ Error(ERR_INFO, "- found to be: %d (according to 'src/conf_xxx.c')",
+ num_file_list_entries_found);
+ Error(ERR_EXIT, "please fix");
+ }
+
+ freeSetupFileHash(ignore_tokens_hash);
+
+ return file_list;
+}
+
+static boolean token_suffix_match(char *token, char *suffix, int start_pos)
+{
+ int len_token = strlen(token);
+ int len_suffix = strlen(suffix);
+
+ if (start_pos < 0) // compare suffix from end of string
+ start_pos += len_token;
+
+ if (start_pos < 0 || start_pos + len_suffix > len_token)
+ return FALSE;
+
+ if (strncmp(&token[start_pos], suffix, len_suffix) != 0)
+ return FALSE;
+
+ if (token[start_pos + len_suffix] == '\0')
+ return TRUE;
+
+ if (token[start_pos + len_suffix] == '.')
+ return TRUE;
+
+ return FALSE;
+}
+
+#define KNOWN_TOKEN_VALUE "[KNOWN_TOKEN_VALUE]"
+
+static void read_token_parameters(SetupFileHash *setup_file_hash,