X-Git-Url: https://git.artsoft.org/?p=rocksndiamonds.git;a=blobdiff_plain;f=src%2Flibgame%2Fsetup.c;h=7f080d9c6c0f92fbdb4f2c2a860c7975f6a3ba54;hp=1ae27900c05b0e6e183268632491e9e0f7788c17;hb=b2fbe97bd09c91e5efb580b35b910a3709015421;hpb=c3e0f340f3ba490911b5e1dce7d9902b01afc207 diff --git a/src/libgame/setup.c b/src/libgame/setup.c index 1ae27900..7f080d9c 100644 --- a/src/libgame/setup.c +++ b/src/libgame/setup.c @@ -178,7 +178,7 @@ static char *getNetworkDir(void) return network_dir; } -static char *getLevelDirFromTreeInfo(TreeInfo *node) +char *getLevelDirFromTreeInfo(TreeInfo *node) { static char *level_dir = NULL; @@ -340,7 +340,7 @@ static char *getClassicArtworkDir(int type) getDefaultMusicDir(MUS_CLASSIC_SUBDIR) : ""); } -static char *getUserGraphicsDir(void) +char *getUserGraphicsDir(void) { static char *usergraphics_dir = NULL; @@ -350,7 +350,7 @@ static char *getUserGraphicsDir(void) return usergraphics_dir; } -static char *getUserSoundsDir(void) +char *getUserSoundsDir(void) { static char *usersounds_dir = NULL; @@ -360,7 +360,7 @@ static char *getUserSoundsDir(void) return usersounds_dir; } -static char *getUserMusicDir(void) +char *getUserMusicDir(void) { static char *usermusic_dir = NULL; @@ -457,7 +457,7 @@ char *getProgramMainDataPath(char *command_filename, char *base_path) { // check if the program's main data base directory is configured if (!strEqual(base_path, ".")) - return base_path; + return getStringCopy(base_path); /* if the program is configured to start from current directory (default), determine program package directory from program binary (some versions @@ -494,22 +494,41 @@ char *getProgramMainDataPath(char *command_filename, char *base_path) char *getProgramConfigFilename(char *command_filename) { - char *command_filename_1 = getStringCopy(command_filename); + static char *config_filename_1 = NULL; + static char *config_filename_2 = NULL; + static char *config_filename_3 = NULL; + static boolean initialized = FALSE; - // strip trailing executable suffix from command filename - if (strSuffix(command_filename_1, ".exe")) - command_filename_1[strlen(command_filename_1) - 4] = '\0'; + if (!initialized) + { + char *command_filename_1 = getStringCopy(command_filename); + + // strip trailing executable suffix from command filename + if (strSuffix(command_filename_1, ".exe")) + command_filename_1[strlen(command_filename_1) - 4] = '\0'; + + char *ro_base_path = getProgramMainDataPath(command_filename, RO_BASE_PATH); + char *conf_directory = getPath2(ro_base_path, CONF_DIRECTORY); + + char *command_basepath = getBasePath(command_filename); + char *command_basename = getBaseNameNoSuffix(command_filename); + char *command_filename_2 = getPath2(command_basepath, command_basename); - char *ro_base_path = getProgramMainDataPath(command_filename, RO_BASE_PATH); - char *conf_directory = getPath2(ro_base_path, CONF_DIRECTORY); + config_filename_1 = getStringCat2(command_filename_1, ".conf"); + config_filename_2 = getStringCat2(command_filename_2, ".conf"); + config_filename_3 = getPath2(conf_directory, SETUP_FILENAME); - char *command_basepath = getBasePath(command_filename); - char *command_basename = getBaseNameNoSuffix(command_filename); - char *command_filename_2 = getPath2(command_basepath, command_basename); + checked_free(ro_base_path); + checked_free(conf_directory); - char *config_filename_1 = getStringCat2(command_filename_1, ".conf"); - char *config_filename_2 = getStringCat2(command_filename_2, ".conf"); - char *config_filename_3 = getPath2(conf_directory, SETUP_FILENAME); + 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)) @@ -1074,7 +1093,8 @@ void InitUserLevelDirectory(char *level_subdir) createDirectory(getUserLevelDir(NULL), "main user level", PERMS_PRIVATE); createDirectory(getUserLevelDir(level_subdir), "user level", PERMS_PRIVATE); - SaveUserLevelInfo(); + if (setup.internal.create_user_levelset) + SaveUserLevelInfo(); } } @@ -2982,6 +3002,73 @@ static void setArtworkInfoCacheEntry(TreeInfo *artwork_info, // functions for loading level info and custom artwork info // ---------------------------------------------------------------------------- +int GetZipFileTreeType(char *zip_filename) +{ + static char *top_dir_path = NULL; + static char *top_dir_conf_filename[NUM_BASE_TREE_TYPES] = { NULL }; + static char *conf_basename[NUM_BASE_TREE_TYPES] = + { + GRAPHICSINFO_FILENAME, + SOUNDSINFO_FILENAME, + MUSICINFO_FILENAME, + LEVELINFO_FILENAME + }; + int j; + + checked_free(top_dir_path); + top_dir_path = NULL; + + for (j = 0; j < NUM_BASE_TREE_TYPES; j++) + { + checked_free(top_dir_conf_filename[j]); + top_dir_conf_filename[j] = NULL; + } + + char **zip_entries = zip_list(zip_filename); + + // check if zip file successfully opened + if (zip_entries == NULL || zip_entries[0] == NULL) + return TREE_TYPE_UNDEFINED; + + // first zip file entry is expected to be top level directory + char *top_dir = zip_entries[0]; + + // check if valid top level directory found in zip file + if (!strSuffix(top_dir, "/")) + return TREE_TYPE_UNDEFINED; + + // get filenames of valid configuration files in top level directory + for (j = 0; j < NUM_BASE_TREE_TYPES; j++) + top_dir_conf_filename[j] = getStringCat2(top_dir, conf_basename[j]); + + int tree_type = TREE_TYPE_UNDEFINED; + int e = 0; + + while (zip_entries[e] != NULL) + { + // check if every zip file entry is below top level directory + if (!strPrefix(zip_entries[e], top_dir)) + return TREE_TYPE_UNDEFINED; + + // check if this zip file entry is a valid configuration filename + for (j = 0; j < NUM_BASE_TREE_TYPES; j++) + { + if (strEqual(zip_entries[e], top_dir_conf_filename[j])) + { + // only exactly one valid configuration file allowed + if (tree_type != TREE_TYPE_UNDEFINED) + return TREE_TYPE_UNDEFINED; + + tree_type = j; + } + } + + e++; + } + + return tree_type; +} + static boolean CheckZipFileForDirectory(char *zip_filename, char *directory, int tree_type) { @@ -3051,28 +3138,37 @@ static boolean CheckZipFileForDirectory(char *zip_filename, char *directory, return TRUE; } -static boolean ExtractZipFileIntoDirectory(char *zip_filename, char *directory, - int tree_type) +char *ExtractZipFileIntoDirectory(char *zip_filename, char *directory, + int tree_type) { boolean zip_file_valid = CheckZipFileForDirectory(zip_filename, directory, tree_type); - Error(ERR_DEBUG, "zip file '%s': %s", zip_filename, - (zip_file_valid ? "EXTRACT" : "REJECT")); - if (!zip_file_valid) - return FALSE; + { + Error(ERR_WARN, "zip file '%s' rejected!", zip_filename); + + return NULL; + } char **zip_entries = zip_extract(zip_filename, directory); - boolean zip_file_extracted = (zip_entries != NULL); + if (zip_entries == NULL) + { + Error(ERR_WARN, "zip file '%s' could not be extracted!", zip_filename); - if (zip_file_extracted) - Error(ERR_DEBUG, "zip file successfully extracted!"); - else - Error(ERR_DEBUG, "zip file could not be extracted!"); + return NULL; + } + + Error(ERR_INFO, "zip file '%s' successfully extracted!", zip_filename); + + // first zip file entry contains top level directory + char *top_dir = zip_entries[0]; - return zip_file_extracted; + // remove trailing directory separator from top level directory + top_dir[strlen(top_dir) - 1] = '\0'; + + return top_dir; } static void ProcessZipFilesInDirectory(char *directory, int tree_type) @@ -3104,10 +3200,9 @@ static void ProcessZipFilesInDirectory(char *directory, int tree_type) if (!fileExists(zip_filename_extracted) && !fileExists(zip_filename_rejected)) { - boolean zip_file_extracted = ExtractZipFileIntoDirectory(zip_filename, - directory, - tree_type); - char *marker_filename = (zip_file_extracted ? zip_filename_extracted : + char *top_dir = ExtractZipFileIntoDirectory(zip_filename, directory, + tree_type); + char *marker_filename = (top_dir != NULL ? zip_filename_extracted : zip_filename_rejected); FILE *marker_file; @@ -3785,13 +3880,29 @@ void LoadLevelArtworkInfo(void) print_timestamp_done("LoadLevelArtworkInfo"); } -static boolean AddUserLevelSetToLevelInfoExt(char *level_subdir_new) +static boolean AddTreeSetToTreeInfoExt(TreeInfo *tree_node_old, char *tree_dir, + char *tree_subdir_new, int type) { - // get level info tree node of first (original) user level set - char *level_subdir_old = getLoginName(); - LevelDirTree *leveldir_old = getTreeInfoFromIdentifier(leveldir_first, - level_subdir_old); - if (leveldir_old == NULL) // should not happen + if (tree_node_old == NULL) + { + if (type == TREE_TYPE_LEVEL_DIR) + { + // get level info tree node of personal user level set + tree_node_old = getTreeInfoFromIdentifier(leveldir_first, getLoginName()); + } + else + { + // get artwork info tree node of first artwork set + tree_node_old = ARTWORK_FIRST_NODE(artwork, type); + } + } + + if (tree_dir == NULL) + tree_dir = TREE_USERDIR(type); + + if (tree_node_old == NULL || + tree_dir == NULL || + tree_subdir_new == NULL) // should not happen return FALSE; int draw_deactivation_mask = GetDrawDeactivationMask(); @@ -3799,33 +3910,54 @@ static boolean AddUserLevelSetToLevelInfoExt(char *level_subdir_new) // override draw deactivation mask (temporarily disable drawing) SetDrawDeactivationMask(REDRAW_ALL); - // load new level set config and add it next to first user level set - LoadLevelInfoFromLevelConf(&leveldir_old->next, NULL, - leveldir_old->basepath, level_subdir_new); + if (type == TREE_TYPE_LEVEL_DIR) + { + // load new level set config and add it next to first user level set + LoadLevelInfoFromLevelConf(&tree_node_old->next, + tree_node_old->node_parent, + tree_dir, tree_subdir_new); + } + else + { + // load new artwork set config and add it next to first artwork set + LoadArtworkInfoFromArtworkConf(&tree_node_old->next, + tree_node_old->node_parent, + tree_dir, tree_subdir_new, type); + } // set draw deactivation mask to previous value SetDrawDeactivationMask(draw_deactivation_mask); - // get level info tree node of newly added user level set - LevelDirTree *leveldir_new = getTreeInfoFromIdentifier(leveldir_first, - level_subdir_new); - if (leveldir_new == NULL) // should not happen + // get first node of level or artwork info tree + TreeInfo **tree_node_first = TREE_FIRST_NODE_PTR(type); + + // get tree info node of newly added level or artwork set + TreeInfo *tree_node_new = getTreeInfoFromIdentifier(*tree_node_first, + tree_subdir_new); + + if (tree_node_new == NULL) // should not happen return FALSE; // correct top link and parent node link of newly created tree node - leveldir_new->node_top = leveldir_old->node_top; - leveldir_new->node_parent = leveldir_old->node_parent; + tree_node_new->node_top = tree_node_old->node_top; + tree_node_new->node_parent = tree_node_old->node_parent; - // sort level info tree to adjust position of newly added level set - sortTreeInfo(&leveldir_first); + // sort tree info to adjust position of newly added tree set + sortTreeInfo(tree_node_first); return TRUE; } +void AddTreeSetToTreeInfo(TreeInfo *tree_node, char *tree_dir, + char *tree_subdir_new, int type) +{ + if (!AddTreeSetToTreeInfoExt(tree_node, tree_dir, tree_subdir_new, type)) + Error(ERR_EXIT, "internal tree info structure corrupted -- aborting"); +} + void AddUserLevelSetToLevelInfo(char *level_subdir_new) { - if (!AddUserLevelSetToLevelInfoExt(level_subdir_new)) - Error(ERR_EXIT, "internal level set structure corrupted -- aborting"); + AddTreeSetToTreeInfo(NULL, NULL, level_subdir_new, TREE_TYPE_LEVEL_DIR); } char *getArtworkIdentifierForUserLevelSet(int type) @@ -3855,8 +3987,17 @@ TreeInfo *getArtworkTreeInfoForUserLevelSet(int type) { char *artwork_set = getArtworkIdentifierForUserLevelSet(type); TreeInfo *artwork_first_node = ARTWORK_FIRST_NODE(artwork, type); + TreeInfo *ti = getTreeInfoFromIdentifier(artwork_first_node, artwork_set); + + if (ti == NULL) + { + ti = getTreeInfoFromIdentifier(artwork_first_node, + ARTWORK_DEFAULT_SUBDIR(type)); + if (ti == NULL) + Error(ERR_EXIT, "cannot find default graphics -- should not happen"); + } - return getTreeInfoFromIdentifier(artwork_first_node, artwork_set); + return ti; } boolean checkIfCustomArtworkExistsForCurrentLevelSet(void) @@ -4224,6 +4365,11 @@ static void checkSeriesInfo(void) { static char *level_directory = NULL; Directory *dir; +#if 0 + DirectoryEntry *dir_entry; +#endif + + checked_free(level_directory); // check for more levels besides the 'levels' field of 'levelinfo.conf' @@ -4239,6 +4385,35 @@ static void checkSeriesInfo(void) return; } +#if 0 + while ((dir_entry = readDirectory(dir)) != NULL) // loop all entries + { + if (strlen(dir_entry->basename) > 4 && + dir_entry->basename[3] == '.' && + strEqual(&dir_entry->basename[4], LEVELFILE_EXTENSION)) + { + char levelnum_str[4]; + int levelnum_value; + + strncpy(levelnum_str, dir_entry->basename, 3); + levelnum_str[3] = '\0'; + + levelnum_value = atoi(levelnum_str); + + if (levelnum_value < leveldir_current->first_level) + { + Error(ERR_WARN, "additional level %d found", levelnum_value); + leveldir_current->first_level = levelnum_value; + } + else if (levelnum_value > leveldir_current->last_level) + { + Error(ERR_WARN, "additional level %d found", levelnum_value); + leveldir_current->last_level = levelnum_value; + } + } + } +#endif + closeDirectory(dir); }