X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fsetup.c;h=b1e3b4439756a3307269cd94a56b6362775c294c;hb=2f5368f25e34c02cb5ff7a012aa96198442231cb;hp=2c7dcfd7c8742db7418d6da057c59b644dc1ba83;hpb=a2c9a458aaa4ac568f07ecd25b19fbca3f766e6b;p=rocksndiamonds.git diff --git a/src/libgame/setup.c b/src/libgame/setup.c index 2c7dcfd7..b1e3b443 100644 --- a/src/libgame/setup.c +++ b/src/libgame/setup.c @@ -108,23 +108,6 @@ static char *getUserLevelDir(char *level_subdir) return userlevel_dir; } -static char *getTapeDir(char *level_subdir) -{ - static char *tape_dir = NULL; - char *data_dir = getUserDataDir(); - char *tape_subdir = TAPES_DIRECTORY; - - if (tape_dir) - free(tape_dir); - - if (level_subdir != NULL) - tape_dir = getPath3(data_dir, tape_subdir, level_subdir); - else - tape_dir = getPath2(data_dir, tape_subdir); - - return tape_dir; -} - static char *getScoreDir(char *level_subdir) { static char *score_dir = NULL; @@ -180,6 +163,37 @@ static char *getCurrentLevelDir() return getLevelDirFromTreeInfo(leveldir_current); } +static char *getTapeDir(char *level_subdir) +{ + static char *tape_dir = NULL; + char *data_dir = getUserDataDir(); + char *tape_subdir = TAPES_DIRECTORY; + + if (tape_dir) + free(tape_dir); + + if (level_subdir != NULL) + tape_dir = getPath3(data_dir, tape_subdir, level_subdir); + else + tape_dir = getPath2(data_dir, tape_subdir); + + return tape_dir; +} + +static char *getSolutionTapeDir() +{ + static char *tape_dir = NULL; + char *data_dir = getCurrentLevelDir(); + char *tape_subdir = TAPES_DIRECTORY; + + if (tape_dir) + free(tape_dir); + + tape_dir = getPath2(data_dir, tape_subdir); + + return tape_dir; +} + static char *getDefaultGraphicsDir(char *graphics_subdir) { static char *graphics_dir = NULL; @@ -379,6 +393,20 @@ char *getTapeFilename(int nr) return filename; } +char *getSolutionTapeFilename(int nr) +{ + static char *filename = NULL; + char basename[MAX_FILENAME_LEN]; + + if (filename != NULL) + free(filename); + + sprintf(basename, "%03d.%s", nr, TAPEFILE_EXTENSION); + filename = getPath2(getSolutionTapeDir(), basename); + + return filename; +} + char *getScoreFilename(int nr) { static char *filename = NULL; @@ -405,6 +433,70 @@ char *getSetupFilename() return filename; } +char *getEditorSetupFilename() +{ + static char *filename = NULL; + + if (filename != NULL) + free(filename); + + filename = getPath2(getSetupDir(), EDITORSETUP_FILENAME); + + return filename; +} + +char *getHelpAnimFilename() +{ + static char *filename = NULL; + + if (filename != NULL) + free(filename); + + filename = getPath2(getCurrentLevelDir(), HELPANIM_FILENAME); + + return filename; +} + +char *getHelpTextFilename() +{ + static char *filename = NULL; + + if (filename != NULL) + free(filename); + + filename = getPath2(getCurrentLevelDir(), HELPTEXT_FILENAME); + + return filename; +} + +char *getLevelSetInfoFilename() +{ + static char *filename = NULL; + char *basenames[] = + { + "readme", + "readme.txt", + "README", + "README.txt", + "README.TXT", + + NULL + }; + int i; + + for (i = 0; basenames[i] != NULL; i++) + { + if (filename != NULL) + free(filename); + + filename = getPath2(getCurrentLevelDir(), basenames[i]); + if (fileExists(filename)) + return filename; + } + + return NULL; +} + static char *getCorrectedArtworkBasename(char *basename) { char *basename_corrected = basename; @@ -556,12 +648,73 @@ char *getCustomSoundFilename(char *basename) return NULL; /* cannot find specified artwork file anywhere */ } +char *getCustomMusicFilename(char *basename) +{ + static char *filename = NULL; + boolean skip_setup_artwork = FALSE; + + if (filename != NULL) + free(filename); + + basename = getCorrectedArtworkBasename(basename); + + if (!setup.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_CLASSIC_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; + + 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; } @@ -813,7 +966,7 @@ void dumpTreeInfo(TreeInfo *node, int depth) while (node) { - for (i=0; i<(depth + 1) * 3; i++) + for (i = 0; i < (depth + 1) * 3; i++) printf(" "); #if 1 @@ -859,7 +1012,7 @@ void sortTreeInfo(TreeInfo **node_first, compare_function); /* update the linkage of list elements with the sorted node array */ - for (i=0; inext = sort_array[i + 1]; sort_array[num_nodes - 1]->next = NULL; @@ -1053,7 +1206,7 @@ int getFileVersionFromCookieString(const char *cookie) version_major = ptr_cookie2[0] - '0'; version_minor = ptr_cookie2[2] - '0'; - return VERSION_IDENT(version_major, version_minor, 0); + return VERSION_IDENT(version_major, version_minor, 0, 0); } boolean checkCookieString(const char *cookie, const char *template) @@ -1081,9 +1234,13 @@ 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); inext, 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); +} + #ifdef DEBUG static void printSetupFileList(SetupFileList *list) { @@ -1214,6 +1382,9 @@ SetupFileHash *newSetupFileHash() SetupFileHash *new_hash = create_hashtable(16, 0.75, get_hash_from_key, keys_are_equal); + if (new_hash == NULL) + Error(ERR_EXIT, "create_hashtable() failed -- out of memory"); + return new_hash; } @@ -1248,6 +1419,14 @@ void setHashEntry(SetupFileHash *hash, char *token, char *value) Error(ERR_EXIT, "cannot insert into hash -- aborting"); } +char *removeHashEntry(SetupFileHash *hash, char *token) +{ + if (hash == NULL) + return NULL; + + return remove_hash_entry(hash, token); +} + #if 0 #ifdef DEBUG static void printSetupFileHash(SetupFileHash *hash) @@ -1264,10 +1443,10 @@ static void printSetupFileHash(SetupFileHash *hash) static void *loadSetupFileData(char *filename, boolean use_hash) { - int line_len; - char line[MAX_LINE_LEN]; + char line[MAX_LINE_LEN], previous_line[MAX_LINE_LEN]; char *token, *value, *line_ptr; - void *setup_file_data, *insert_ptr; + void *setup_file_data, *insert_ptr = NULL; + boolean read_continued_line = FALSE; FILE *file; if (use_hash) @@ -1281,16 +1460,48 @@ static void *loadSetupFileData(char *filename, boolean use_hash) return NULL; } - while(!feof(file)) + while (!feof(file)) { /* read next line of input file */ if (!fgets(line, MAX_LINE_LEN, file)) break; - /* cut trailing comment or whitespace from input line */ + /* cut trailing newline 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'; + + if (read_continued_line) + { + /* cut leading whitespaces from input line */ + for (line_ptr = line; *line_ptr; line_ptr++) + if (*line_ptr != ' ' && *line_ptr != '\t') + break; + + /* 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; + } + + /* cut trailing comment from input line */ for (line_ptr = line; *line_ptr; line_ptr++) { - if (*line_ptr == '#' || *line_ptr == '\n' || *line_ptr == '\r') + if (*line_ptr == '#') { *line_ptr = '\0'; break; @@ -1298,47 +1509,50 @@ static void *loadSetupFileData(char *filename, boolean use_hash) } /* 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') + 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') continue; - line_len = strlen(line); - /* cut leading whitespaces from token */ for (token = line; *token; token++) if (*token != ' ' && *token != '\t') break; - /* find end of token */ + /* start with empty value as reliable default */ + value = ""; + + /* find end of token to determine start of value */ for (line_ptr = token; *line_ptr; line_ptr++) { if (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == ':') { - *line_ptr = '\0'; + *line_ptr = '\0'; /* terminate token string */ + value = line_ptr + 1; /* set beginning of value */ + break; } } - if (line_ptr < line + line_len) - value = line_ptr + 1; - else - value = "\0"; - /* cut leading whitespaces from value */ for (; *value; value++) if (*value != ' ' && *value != '\t') break; - if (*token && *value) +#if 0 + if (*value == '\0') + value = "true"; /* treat tokens without value as "true" */ +#endif + + if (*token) { if (use_hash) setHashEntry((SetupFileHash *)setup_file_data, token, value); else - insert_ptr = setListEntry((SetupFileList *)insert_ptr, token, value); + insert_ptr = addListEntry((SetupFileList *)insert_ptr, token, value); } } @@ -1783,7 +1997,7 @@ static boolean LoadLevelInfoFromLevelConf(TreeInfo **node_first, /* set all structure fields according to the token/value pairs */ ldi = *leveldir_new; - for (i=0; i/levelsetup.conf */ /* ----------------------------------------------------------------------- */ - filename = getPath2(getSetupDir(), LEVELSETUP_FILENAME); + char *filename = getPath2(getSetupDir(), LEVELSETUP_FILENAME); + SetupFileHash *level_setup_hash = NULL; + + /* always start with reliable default values */ + leveldir_current = getFirstValidTreeInfoEntry(leveldir_first); if ((level_setup_hash = loadSetupFileHash(filename))) { @@ -2591,17 +2803,15 @@ void LoadLevelSetup_LastSeries() void SaveLevelSetup_LastSeries() { - char *filename; - char *level_subdir = leveldir_current->filename; - FILE *file; - /* ----------------------------------------------------------------------- */ /* ~/./levelsetup.conf */ /* ----------------------------------------------------------------------- */ - InitUserDataDirectory(); + char *filename = getPath2(getSetupDir(), LEVELSETUP_FILENAME); + char *level_subdir = leveldir_current->filename; + FILE *file; - filename = getPath2(getSetupDir(), LEVELSETUP_FILENAME); + InitUserDataDirectory(); if (!(file = fopen(filename, MODE_WRITE))) {