From: Holger Schemel Date: Sun, 15 Oct 2017 19:01:21 +0000 (+0200) Subject: added functions for creating and updating user level sets X-Git-Tag: 4.1.0.0~99 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;ds=inline;h=d0c3964b020fd888d52b219b75d9c436ee5c5213;hp=81da35db369b73242e6ff3902657b3703efb4fa4;p=rocksndiamonds.git added functions for creating and updating user level sets --- diff --git a/src/libgame/setup.c b/src/libgame/setup.c index 4e81beb6..54e5eeee 100644 --- a/src/libgame/setup.c +++ b/src/libgame/setup.c @@ -3494,20 +3494,76 @@ void LoadLevelArtworkInfo() print_timestamp_done("LoadLevelArtworkInfo"); } -static void SaveUserLevelInfo() +boolean UpdateUserLevelSet(char *level_subdir, + char *level_name, char *level_author, + int num_levels, int first_level_nr) +{ + char *filename = getPath2(getUserLevelDir(level_subdir), LEVELINFO_FILENAME); + char *filename_tmp = getStringCat2(filename, ".tmp"); + FILE *file = NULL; + FILE *file_tmp = NULL; + char line[MAX_LINE_LEN]; + boolean update_num_levels_only = FALSE; + boolean success = FALSE; + + if (level_name == NULL || level_author == NULL || first_level_nr == -1) + update_num_levels_only = TRUE; + + if ((file = fopen(filename, MODE_READ)) && + (file_tmp = fopen(filename_tmp, MODE_WRITE))) + { + while (fgets(line, MAX_LINE_LEN, file)) + { + if (strPrefix(line, "name:") && !update_num_levels_only) + fprintf(file_tmp, "%-32s%s\n", "name:", level_name); + else if (strPrefix(line, "author:") && !update_num_levels_only) + fprintf(file_tmp, "%-32s%s\n", "author:", level_author); + else if (strPrefix(line, "levels:")) + fprintf(file_tmp, "%-32s%d\n", "levels:", num_levels); + else if (strPrefix(line, "first_level:") && !update_num_levels_only) + fprintf(file_tmp, "%-32s%d\n", "first_level:", first_level_nr); + else + fputs(line, file_tmp); + } + + success = TRUE; + } + + if (file) + fclose(file); + + if (file_tmp) + fclose(file_tmp); + + if (success) + success = (rename(filename_tmp, filename) == 0); + + free(filename); + free(filename_tmp); + + return success; +} + +boolean CreateUserLevelSet(char *level_subdir, + char *level_name, char *level_author, + int num_levels, int first_level_nr) { LevelDirTree *level_info; char *filename; FILE *file; int i; - filename = getPath2(getUserLevelDir(getLoginName()), LEVELINFO_FILENAME); + // create user level sub-directory, if needed + createDirectory(getUserLevelDir(level_subdir), "user level", PERMS_PRIVATE); + + filename = getPath2(getUserLevelDir(level_subdir), LEVELINFO_FILENAME); if (!(file = fopen(filename, MODE_WRITE))) { Error(ERR_WARN, "cannot write level info file '%s'", filename); free(filename); - return; + + return FALSE; } level_info = newTreeInfo(); @@ -3515,10 +3571,10 @@ static void SaveUserLevelInfo() /* always start with reliable default values */ setTreeInfoToDefaults(level_info, TREE_TYPE_LEVEL_DIR); - setString(&level_info->name, getLoginName()); - setString(&level_info->author, getRealName()); - level_info->levels = 100; - level_info->first_level = 1; + setString(&level_info->name, level_name); + setString(&level_info->author, level_author); + level_info->levels = num_levels; + level_info->first_level = first_level_nr; level_info->sort_priority = LEVELCLASS_PRIVATE_START; level_info->readonly = FALSE; @@ -3550,6 +3606,13 @@ static void SaveUserLevelInfo() freeTreeInfo(level_info); free(filename); + + return TRUE; +} + +static void SaveUserLevelInfo() +{ + CreateUserLevelSet(getLoginName(), getLoginName(), getRealName(), 100, 1); } char *getSetupValue(int type, void *value) diff --git a/src/libgame/setup.h b/src/libgame/setup.h index 1e837a5b..8dbd1694 100644 --- a/src/libgame/setup.h +++ b/src/libgame/setup.h @@ -310,6 +310,9 @@ void LoadLevelInfo(void); void LoadArtworkInfo(void); void LoadLevelArtworkInfo(void); +boolean UpdateUserLevelSet(char *, char *, char *, int, int); +boolean CreateUserLevelSet(char *, char *, char *, int, int); + void LoadLevelSetup_LastSeries(void); void SaveLevelSetup_LastSeries(void); void SaveLevelSetup_LastSeries_Deactivate();