X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Ffiles.c;h=17bc45e5986691d8b773e6ad12dad24c7a29df79;hb=c4b26cf489dcc65a00bfcc05f7898700d2f0c9e4;hp=e1a4b80c42c4720fa256b2bffacbf186c29d4a9e;hpb=e5c5bf5c4a76a04f9bf64e92227bf2ef969fd25c;p=rocksndiamonds.git diff --git a/src/files.c b/src/files.c index e1a4b80c..17bc45e5 100644 --- a/src/files.c +++ b/src/files.c @@ -11,6 +11,8 @@ * files.h * ***********************************************************/ +#include + #include "files.h" #include "tools.h" #include "misc.h" @@ -51,6 +53,10 @@ boolean CreateNewScoreFile() return(TRUE); } + + +#if 0 + boolean CreateNewNamesFile(int mode) { char filename[MAX_FILENAME_LEN]; @@ -72,6 +78,9 @@ boolean CreateNewNamesFile(int mode) return(TRUE); } +#endif + + boolean LoadLevelInfo() { int i; @@ -83,14 +92,14 @@ boolean LoadLevelInfo() if (!(file=fopen(filename,"r"))) { - Error(ERR_RETURN, "cannot load level info '%s'", filename); + Error(ERR_WARN, "cannot read level info '%s'", filename); return(FALSE); } fscanf(file,"%s\n",cookie); if (strcmp(cookie,LEVELDIR_COOKIE)) /* ungültiges Format? */ { - Error(ERR_RETURN, "wrong format of level info file"); + Error(ERR_WARN, "wrong format of level info file"); fclose(file); return(FALSE); } @@ -111,7 +120,7 @@ boolean LoadLevelInfo() if (!num_leveldirs) { - Error(ERR_RETURN, "empty level info '%s'", filename); + Error(ERR_WARN, "empty level info '%s'", filename); return(FALSE); } @@ -129,7 +138,7 @@ void LoadLevel(int level_nr) level_directory,leveldir[leveldir_nr].filename,level_nr); if (!(file = fopen(filename,"r"))) - Error(ERR_RETURN, "cannot load level '%s' - creating new level", filename); + Error(ERR_WARN, "cannot read level '%s' - creating new level", filename); else { fgets(cookie,LEVEL_COOKIE_LEN,file); @@ -137,7 +146,7 @@ void LoadLevel(int level_nr) if (strcmp(cookie,LEVEL_COOKIE)) /* ungültiges Format? */ { - Error(ERR_RETURN, "wrong format of level file '%s'", filename); + Error(ERR_WARN, "wrong format of level file '%s'", filename); fclose(file); file = NULL; } @@ -232,7 +241,7 @@ void LoadLevelTape(int level_nr) levelrec_10 = TRUE; else if (strcmp(cookie,LEVELREC_COOKIE)) /* unknown tape format */ { - Error(ERR_RETURN, "wrong format of level recording file '%s'", filename); + Error(ERR_WARN, "wrong format of level recording file '%s'", filename); fclose(file); file = NULL; } @@ -282,7 +291,7 @@ void LoadLevelTape(int level_nr) fclose(file); if (i != tape.length) - Error(ERR_RETURN, "level recording file '%s' corrupted", filename); + Error(ERR_WARN, "level recording file '%s' corrupted", filename); tape.length_seconds = GetTapeLength(); } @@ -300,9 +309,9 @@ void LoadScore(int level_nr) if (!(file = fopen(filename,"r"))) { if (!CreateNewScoreFile()) - Error(ERR_RETURN, "cannot create score file '%s'", filename); + Error(ERR_WARN, "cannot create score file '%s'", filename); else if (!(file = fopen(filename,"r"))) - Error(ERR_RETURN, "cannot load score for level %d", level_nr); + Error(ERR_WARN, "cannot read score for level %d", level_nr); } if (file) @@ -310,7 +319,7 @@ void LoadScore(int level_nr) fgets(cookie,SCORE_COOKIE_LEN,file); if (strcmp(cookie,SCORE_COOKIE)) /* ungültiges Format? */ { - Error(ERR_RETURN, "wrong format of score file '%s'", filename); + Error(ERR_WARN, "wrong format of score file '%s'", filename); fclose(file); file = NULL; } @@ -339,6 +348,10 @@ void LoadScore(int level_nr) } } + + +#if 0 + void LoadPlayerInfo(int mode) { int i; @@ -349,6 +362,15 @@ void LoadPlayerInfo(int mode) struct PlayerInfo default_player, new_player; int version_10_file = FALSE; + + + if (mode == PLAYER_SETUP) + LoadSetup(); + else if (mode == PLAYER_LEVEL) + LoadLevelSetup(); + + + if (mode==PLAYER_LEVEL) sprintf(filename,"%s/%s/%s", level_directory,leveldir[leveldir_nr].filename,NAMES_FILENAME); @@ -369,9 +391,9 @@ void LoadPlayerInfo(int mode) if (!(file = fopen(filename,"r"))) { if (!CreateNewNamesFile(mode)) - Error(ERR_RETURN, "cannot create names file '%s'", filename); + Error(ERR_WARN, "cannot create names file '%s'", filename); else if (!(file = fopen(filename,"r"))) - Error(ERR_RETURN, "cannot load player information file '%s'", filename); + Error(ERR_WARN, "cannot read player information file '%s'", filename); } if (file) @@ -381,7 +403,7 @@ void LoadPlayerInfo(int mode) version_10_file = TRUE; else if (strcmp(cookie,NAMES_COOKIE)) /* ungültiges Format? */ { - Error(ERR_RETURN, "wrong format of names file '%s'", filename); + Error(ERR_WARN, "wrong format of names file '%s'", filename); fclose(file); file = NULL; } @@ -418,7 +440,7 @@ void LoadPlayerInfo(int mode) fclose(file); if (!(file = fopen(filename,"a"))) - Error(ERR_RETURN, "cannot append new player to names file '%s'", + Error(ERR_WARN, "cannot append new player to names file '%s'", filename); else { @@ -464,6 +486,10 @@ void LoadPlayerInfo(int mode) fclose(file); } +#endif + + + void SaveLevel(int level_nr) { int i,x,y; @@ -475,7 +501,7 @@ void SaveLevel(int level_nr) if (!(file=fopen(filename,"w"))) { - Error(ERR_RETURN, "cannot save level file '%s'", filename); + Error(ERR_WARN, "cannot save level file '%s'", filename); return; } @@ -541,7 +567,7 @@ void SaveLevelTape(int level_nr) if (!(file=fopen(filename,"w"))) { - Error(ERR_RETURN, "cannot save level recording file '%s'", filename); + Error(ERR_WARN, "cannot save level recording file '%s'", filename); return; } @@ -594,7 +620,7 @@ void SaveScore(int level_nr) if (!(file=fopen(filename,"r+"))) { - Error(ERR_RETURN, "cannot save score for level %d", level_nr); + Error(ERR_WARN, "cannot save score for level %d", level_nr); return; } @@ -611,6 +637,10 @@ void SaveScore(int level_nr) fclose(file); } + + +#if 0 + void SavePlayerInfo(int mode) { int i; @@ -620,7 +650,16 @@ void SavePlayerInfo(int mode) struct PlayerInfo default_player; int version_10_file = FALSE; - if (mode==PLAYER_LEVEL) + + + if (mode == PLAYER_SETUP) + SaveSetup(); + else if (mode == PLAYER_LEVEL) + SaveLevelSetup(); + + + + if (mode == PLAYER_LEVEL) sprintf(filename,"%s/%s/%s", level_directory,leveldir[leveldir_nr].filename,NAMES_FILENAME); else @@ -628,7 +667,7 @@ void SavePlayerInfo(int mode) if (!(file = fopen(filename,"r+"))) { - Error(ERR_RETURN, "cannot save player information to file '%s'", filename); + Error(ERR_WARN, "cannot save player information to file '%s'", filename); return; } @@ -637,7 +676,7 @@ void SavePlayerInfo(int mode) version_10_file = TRUE; else if (strcmp(cookie,NAMES_COOKIE)) /* ungültiges Format? */ { - Error(ERR_RETURN, "wrong format of names file '%s'", filename); + Error(ERR_WARN, "wrong format of names file '%s'", filename); fclose(file); return; } @@ -691,6 +730,10 @@ void SavePlayerInfo(int mode) fclose(file); } +#endif + + + void LoadJoystickData() { int i; @@ -707,7 +750,7 @@ void LoadJoystickData() fscanf(file,"%s",cookie); if (strcmp(cookie,JOYSTICK_COOKIE)) /* ungültiges Format? */ { - Error(ERR_RETURN, "wrong format of joystick file '%s'", JOYDAT_FILE); + Error(ERR_WARN, "wrong format of joystick file '%s'", JOYDAT_FILE); fclose(file); return; } @@ -741,7 +784,7 @@ void SaveJoystickData() if (!(file=fopen(JOYDAT_FILE,"w"))) { - Error(ERR_RETURN, "cannot save joystick calibration data to file '%s'", + Error(ERR_WARN, "cannot save joystick calibration data to file '%s'", JOYDAT_FILE); return; } @@ -763,3 +806,823 @@ void SaveJoystickData() #endif } + +/* ------------------------------------------------------------------------- */ +/* new setup functions */ +/* ------------------------------------------------------------------------- */ + +#define TOKEN_STR_FILE_IDENTIFIER "file_identifier" +#define TOKEN_STR_LAST_LEVEL_SERIES "last_level_series" +#define TOKEN_STR_ALIAS_NAME "alias_name" + +#define TOKEN_STR_PLAYER_PREFIX "player_" +#define TOKEN_VALUE_POSITION 30 +#define TOKEN_INVALID -1 +#define TOKEN_IGNORE -99 + +#define SETUP_TOKEN_ALIAS_NAME 100 + +#define SETUP_TOKEN_SOUND 0 +#define SETUP_TOKEN_SOUND_LOOPS 1 +#define SETUP_TOKEN_SOUND_MUSIC 2 +#define SETUP_TOKEN_SOUND_SIMPLE 3 +#define SETUP_TOKEN_TOONS 4 +#define SETUP_TOKEN_DIRECT_DRAW 5 +#define SETUP_TOKEN_SCROLL_DELAY 6 +#define SETUP_TOKEN_SOFT_SCROLLING 7 +#define SETUP_TOKEN_FADING 8 +#define SETUP_TOKEN_AUTORECORD 9 +#define SETUP_TOKEN_QUICK_DOORS 10 +#define SETUP_TOKEN_USE_JOYSTICK 11 +#define SETUP_TOKEN_JOYSTICK_NR 12 +#define SETUP_TOKEN_JOY_SNAP 13 +#define SETUP_TOKEN_JOY_BOMB 14 +#define SETUP_TOKEN_KEY_LEFT 15 +#define SETUP_TOKEN_KEY_RIGHT 16 +#define SETUP_TOKEN_KEY_UP 17 +#define SETUP_TOKEN_KEY_DOWN 18 +#define SETUP_TOKEN_KEY_SNAP 19 +#define SETUP_TOKEN_KEY_BOMB 20 + +#define NUM_SETUP_TOKENS 21 + +static struct +{ + char *token, *value_true, *value_false; +} setup_info[] = +{ + { "sound", "on", "off" }, + { "repeating_sound_loops", "on", "off" }, + { "background_music", "on", "off" }, + { "simple_sound_effects", "on", "off" }, + { "toons", "on", "off" }, + { "double_buffering", "off", "on" }, + { "scroll_delay", "on", "off" }, + { "soft_scrolling", "on", "off" }, + { "screen_fading", "on", "off" }, + { "automatic_tape_recording", "on", "off" }, + { "quick_doors", "on", "off" }, + + /* for each player: */ + { ".use_joystick", "true", "false" }, + { ".joystick_device", "second", "first" }, + { ".joy.snap_field", "", "" }, + { ".joy.place_bomb", "", "" }, + { ".key.move_left", "", "" }, + { ".key.move_right", "", "" }, + { ".key.move_up", "", "" }, + { ".key.move_down", "", "" }, + { ".key.snap_field", "", "" }, + { ".key.place_bomb", "", "" } +}; + +static char *string_tolower(char *s) +{ + static char s_lower[100]; + int i; + + if (strlen(s) >= 100) + return s; + + strcpy(s_lower, s); + + for (i=0; itoken) + free(setup_file_list->token); + if (setup_file_list->value) + free(setup_file_list->value); + if (setup_file_list->next) + freeSetupFileList(setup_file_list->next); + free(setup_file_list); +} + +static struct SetupFileList *newSetupFileList(char *token, char *value) +{ + struct SetupFileList *new = checked_malloc(sizeof(struct SetupFileList)); + + new->token = checked_malloc(strlen(token) + 1); + strcpy(new->token, token); + + new->value = checked_malloc(strlen(value) + 1); + strcpy(new->value, value); + + new->next = NULL; + + return new; +} + +static char *getSetupFileListEntry(struct SetupFileList *setup_file_list, + char *token) +{ + if (!setup_file_list) + return NULL; + + if (strcmp(setup_file_list->token, token) == 0) + return setup_file_list->value; + else + return getSetupFileListEntry(setup_file_list->next, token); +} + +boolean setSetupFileListEntry(struct SetupFileList *setup_file_list, + char *token, char *value) +{ + if (!setup_file_list) + return FALSE; + + if (strcmp(setup_file_list->token, token) == 0) + { + free(setup_file_list->value); + setup_file_list->value = checked_malloc(strlen(value) + 1); + strcpy(setup_file_list->value, value); + + return TRUE; + } + else + return setSetupFileListEntry(setup_file_list->next, token, value); +} + +void updateSetupFileListEntry(struct SetupFileList *setup_file_list, + char *token, char *value) +{ + if (!setup_file_list) + return; + + if (getSetupFileListEntry(setup_file_list, token) != NULL) + setSetupFileListEntry(setup_file_list, token, value); + else + { + struct SetupFileList *list_entry = setup_file_list; + + while (list_entry->next) + list_entry = list_entry->next; + + list_entry->next = newSetupFileList(token, value); + } +} + +#ifdef DEBUG +static void printSetupFileList(struct SetupFileList *setup_file_list) +{ + if (!setup_file_list) + return; + + printf("token: '%s'\n", setup_file_list->token); + printf("value: '%s'\n", setup_file_list->value); + + printSetupFileList(setup_file_list->next); +} +#endif + +static struct SetupFileList *loadSetupFileList(char *filename) +{ + int line_len; + char line[MAX_LINE_LEN]; + char *token, *value, *line_ptr; + struct SetupFileList *setup_file_list = newSetupFileList("", ""); + struct SetupFileList *first_valid_list_entry; + + + + /* + struct SetupFileList **next_entry = &setup_file_list; + */ + + + FILE *file; + + if (!(file = fopen(filename, "r"))) + { + Error(ERR_WARN, "cannot open setup file '%s'", filename); + return NULL; + } + + 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 */ + for (line_ptr = line; *line_ptr; line_ptr++) + { + if (*line_ptr == '#' || *line_ptr == '\n') + { + *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') + continue; + + line_len = strlen(line); + + /* cut leading whitespaces from token */ + for (token = line; *token; token++) + if (*token != ' ' && *token != '\t') + break; + + /* find end of token */ + for (line_ptr = token; *line_ptr; line_ptr++) + { + if (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == ':') + { + *line_ptr = '\0'; + 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) + updateSetupFileListEntry(setup_file_list, token, value); + + +#if 0 + { + /* allocate new token/value pair */ + + *next_entry = newSetupFileList(token, value); + next_entry = &((*next_entry)->next); + } +#endif + + + + } + + fclose(file); + + first_valid_list_entry = setup_file_list->next; + + /* free empty list header */ + setup_file_list->next = NULL; + freeSetupFileList(setup_file_list); + + if (!first_valid_list_entry) + Error(ERR_WARN, "setup file is empty"); + + return first_valid_list_entry; +} + +static void checkSetupFileListIdentifier(struct SetupFileList *setup_file_list, + char *identifier) +{ + if (!setup_file_list) + return; + + if (strcmp(setup_file_list->token, TOKEN_STR_FILE_IDENTIFIER) == 0) + { + if (strcmp(setup_file_list->value, identifier) != 0) + { + Error(ERR_WARN, "setup file has wrong version"); + return; + } + else + return; + } + + if (setup_file_list->next) + checkSetupFileListIdentifier(setup_file_list->next, identifier); + else + { + Error(ERR_WARN, "setup file has no version information"); + return; + } +} + +static void decodeSetupFileList(struct SetupFileList *setup_file_list) +{ + int i; + int token_nr = TOKEN_INVALID; + int player_nr = 0; + char *token; + char *token_value; + int token_integer_value; + boolean token_boolean_value; + int token_player_prefix_len; + + if (!setup_file_list) + return; + + token = setup_file_list->token; + token_value = setup_file_list->value; + token_integer_value = get_string_integer_value(token_value); + token_boolean_value = get_string_boolean_value(token_value); + + token_player_prefix_len = strlen(TOKEN_STR_PLAYER_PREFIX); + + if (strncmp(token, TOKEN_STR_PLAYER_PREFIX, + token_player_prefix_len) == 0) + { + token += token_player_prefix_len; + + if (*token >= '0' && *token <= '9') + { + player_nr = ((int)(*token - '0') - 1 + MAX_PLAYERS) % MAX_PLAYERS; + token++; + } + } + + for (i=0; i 1) + token_integer_value = 1; + setup.input[player_nr].joystick_nr = token_integer_value - 1; + break; + case SETUP_TOKEN_JOY_SNAP: + setup.input[player_nr].joy.snap = getJoySymbolFromJoyName(token_value); + break; + case SETUP_TOKEN_JOY_BOMB : + setup.input[player_nr].joy.bomb = getJoySymbolFromJoyName(token_value); + break; + case SETUP_TOKEN_KEY_LEFT: + setup.input[player_nr].key.left = getKeySymFromX11KeyName(token_value); + break; + case SETUP_TOKEN_KEY_RIGHT: + setup.input[player_nr].key.right = getKeySymFromX11KeyName(token_value); + break; + case SETUP_TOKEN_KEY_UP: + setup.input[player_nr].key.up = getKeySymFromX11KeyName(token_value); + break; + case SETUP_TOKEN_KEY_DOWN: + setup.input[player_nr].key.down = getKeySymFromX11KeyName(token_value); + break; + case SETUP_TOKEN_KEY_SNAP: + setup.input[player_nr].key.snap = getKeySymFromX11KeyName(token_value); + break; + case SETUP_TOKEN_KEY_BOMB: + setup.input[player_nr].key.bomb = getKeySymFromX11KeyName(token_value); + break; + + case SETUP_TOKEN_ALIAS_NAME: + strncpy(local_player->alias_name, token_value, MAX_NAMELEN-1); + local_player->alias_name[MAX_NAMELEN-1] = '\0'; + break; + + case TOKEN_INVALID: + Error(ERR_WARN, "unknown token '%s' not recognized", token); + break; + + case TOKEN_IGNORE: + default: + break; + } + + decodeSetupFileList(setup_file_list->next); +} + +void LoadSetup() +{ + int i; + char filename[MAX_FILENAME_LEN]; + struct SetupFileList *setup_file_list = NULL; + + /* always start with reliable default setup values */ + + strncpy(local_player->login_name, GetLoginName(), MAX_NAMELEN-1); + local_player->login_name[MAX_NAMELEN-1] = '\0'; + + strncpy(local_player->alias_name, GetLoginName(), MAX_NAMELEN-1); + local_player->alias_name[MAX_NAMELEN-1] = '\0'; + + setup.sound_on = TRUE; + setup.sound_loops_on = FALSE; + setup.sound_music_on = FALSE; + setup.sound_simple_on = FALSE; + setup.toons_on = TRUE; + setup.direct_draw_on = FALSE; + setup.scroll_delay_on = FALSE; + setup.soft_scrolling_on = TRUE; + setup.fading_on = FALSE; + setup.autorecord_on = FALSE; + setup.quick_doors = FALSE; + + for (i=0; i leveldir[leveldir_nr].levels - 1) + level_nr = leveldir[leveldir_nr].levels - 1; + } + + break; + } + } + } + + +#if 0 + printSetupFileList(level_setup_list); +#endif + + checkSetupFileListIdentifier(level_setup_list, LEVELSETUP_COOKIE); + } + else + Error(ERR_RETURN, "using default setup values"); +} + +void SaveSetup() +{ + int i; + char filename[MAX_FILENAME_LEN]; + FILE *file; + + sprintf(filename, "%s/%s", SETUP_PATH, SETUP_FILENAME); + + if (!(file = fopen(filename, "w"))) + { + Error(ERR_WARN, "cannot write setup file '%s'", filename); + return; + } + + fprintf(file, "%s: %s\n", + TOKEN_STR_FILE_IDENTIFIER, SETUP_COOKIE); + + fprintf(file, "\n"); + + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SOUND, + setup.sound_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SOUND_LOOPS, + setup.sound_loops_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SOUND_MUSIC, + setup.sound_music_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SOUND_SIMPLE, + setup.sound_simple_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_TOONS, + setup.toons_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_DIRECT_DRAW, + setup.direct_draw_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SCROLL_DELAY, + setup.scroll_delay_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_SOFT_SCROLLING, + setup.soft_scrolling_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_FADING, + setup.fading_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_AUTORECORD, + setup.autorecord_on)); + fprintf(file, "%s\n", + getSetupEntry("", SETUP_TOKEN_QUICK_DOORS, + setup.quick_doors)); + + fprintf(file, "\n"); + + fprintf(file, "%s\n", + getFormattedSetupEntry(TOKEN_STR_ALIAS_NAME, + local_player->alias_name)); + + for (i=0; itoken, TOKEN_STR_FILE_IDENTIFIER) != 0) + fprintf(file, "%s\n", + getFormattedSetupEntry(list_entry->token, list_entry->value)); + + /* just to make things nicer :) */ + if (strcmp(list_entry->token, TOKEN_STR_LAST_LEVEL_SERIES) == 0) + fprintf(file, "\n"); + + list_entry = list_entry->next; + } + + fclose(file); + + chmod(filename, SETUP_PERMS); +}