X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Ffiles.c;h=cb1199d340bb58212659431caadeb900800719ca;hb=05651fcbc154b1d8321d6e4e9374cfcdd274feda;hp=46671d4631c18ca86423f370eb81c7a3a01dbc70;hpb=dbdefdcdfa0fa58296160ffc6b72fea58bea5c75;p=rocksndiamonds.git diff --git a/src/files.c b/src/files.c index 46671d46..cb1199d3 100644 --- a/src/files.c +++ b/src/files.c @@ -22,7 +22,14 @@ #include "tape.h" #include "joystick.h" -#define MAX_LINE_LEN 1000 +#define MAX_LINE_LEN 1000 /* file input line length */ +#define CHUNK_ID_LEN 4 /* IFF style chunk id length */ +#define LEVEL_HEADER_SIZE 80 /* size of level file header */ +#define LEVEL_HEADER_UNUSED 18 /* unused level header bytes */ +#define TAPE_HEADER_SIZE 20 /* size of tape file header */ +#define TAPE_HEADER_UNUSED 7 /* unused tape header bytes */ +#define FILE_VERSION_1_0 10 /* old 1.0 file version */ +#define FILE_VERSION_1_2 12 /* actual file version */ static void SaveUserLevelInfo(); /* for 'InitUserLevelDir()' */ static char *getSetupLine(char *, int); /* for 'SaveUserLevelInfo()' */ @@ -139,13 +146,52 @@ static void InitUserLevelDirectory(char *level_subdir) } } +static void setLevelInfoToDefaults() +{ + int i, x, y; + + lev_fieldx = level.fieldx = STD_LEV_FIELDX; + lev_fieldy = level.fieldy = STD_LEV_FIELDY; + + for(x=0; x 0 && cookie[strlen(cookie) - 1] == '\n') + cookie[strlen(cookie) - 1] = '\0'; + + if (strcmp(cookie, LEVEL_COOKIE_10) == 0) /* old 1.0 level format */ + file_version = FILE_VERSION_1_0; + else if (strcmp(cookie, LEVEL_COOKIE) != 0) /* unknown level format */ { - fgets(cookie, LEVEL_COOKIE_LEN, file); - fgetc(file); + Error(ERR_WARN, "wrong file identifier of level file '%s'", filename); + fclose(file); + return; + } - if (strcmp(cookie,LEVEL_COOKIE)) + /* read chunk "HEAD" */ + if (file_version >= FILE_VERSION_1_2) + { + /* first check header chunk identifier and chunk length */ + fgets(chunk, CHUNK_ID_LEN + 1, file); + chunk_length = + (fgetc(file)<<24) | (fgetc(file)<<16) | (fgetc(file)<<8) | fgetc(file); + if (strcmp(chunk, "HEAD") || chunk_length != LEVEL_HEADER_SIZE) { - Error(ERR_WARN, "wrong format of level file '%s'", filename); + Error(ERR_WARN, "wrong 'HEAD' chunk of level file '%s'", filename); fclose(file); - file = NULL; + return; } } - if (file) - { - lev_fieldx = level.fieldx = fgetc(file); - lev_fieldy = level.fieldy = fgetc(file); - - level.time = (fgetc(file)<<8) | fgetc(file); - level.edelsteine = (fgetc(file)<<8) | fgetc(file); - for(i=0; i= FILE_VERSION_1_2) { - lev_fieldx = level.fieldx = STD_LEV_FIELDX; - lev_fieldy = level.fieldy = STD_LEV_FIELDY; - - level.time = 100; - level.edelsteine = 0; - strcpy(level.name, "Nameless Level"); - for(i=0; i> 24) & 0xff, file); + fputc((chunk_length >> 16) & 0xff, file); + fputc((chunk_length >> 8) & 0xff, file); + fputc((chunk_length >> 0) & 0xff, file); fputc(level.fieldx, file); fputc(level.fieldy, file); @@ -275,9 +337,17 @@ void SaveLevel(int level_nr) fputc(level.dauer_ablenk, file); fputc(level.amoebe_inhalt, file); - for(i=0; i> 24) & 0xff, file); + fputc((chunk_length >> 16) & 0xff, file); + fputc((chunk_length >> 8) & 0xff, file); + fputc((chunk_length >> 0) & 0xff, file); + for(y=0; y 0 && cookie[strlen(cookie) - 1] == '\n') + cookie[strlen(cookie) - 1] = '\0'; + + if (strcmp(cookie, TAPE_COOKIE_10) == 0) /* old 1.0 tape format */ + file_version = FILE_VERSION_1_0; + else if (strcmp(cookie, TAPE_COOKIE) != 0) /* unknown tape format */ { - fgets(cookie, LEVELREC_COOKIE_LEN, file); - fgetc(file); - if (!strcmp(cookie, LEVELREC_COOKIE_10)) /* old 1.0 tape format */ - levelrec_10 = TRUE; - else if (strcmp(cookie, LEVELREC_COOKIE)) /* unknown tape format */ + Error(ERR_WARN, "wrong file identifier of tape file '%s'", filename); + fclose(file); + return; + } + + /* read chunk "HEAD" */ + if (file_version >= FILE_VERSION_1_2) + { + /* first check header chunk identifier and chunk length */ + fgets(chunk, CHUNK_ID_LEN + 1, file); + chunk_length = + (fgetc(file)<<24) | (fgetc(file)<<16) | (fgetc(file)<<8) | fgetc(file); + + if (strcmp(chunk, "HEAD") || chunk_length != TAPE_HEADER_SIZE) { - Error(ERR_WARN, "wrong format of level recording file '%s'", filename); + Error(ERR_WARN, "wrong 'HEAD' chunk of tape file '%s'", filename); fclose(file); - file = NULL; + return; } } - if (!file) - return; - tape.random_seed = (fgetc(file)<<24) | (fgetc(file)<<16) | (fgetc(file)<<8) | fgetc(file); tape.date = @@ -323,6 +413,28 @@ void LoadTape(int level_nr) tape.length = (fgetc(file)<<24) | (fgetc(file)<<16) | (fgetc(file)<<8) | fgetc(file); + /* read header fields that are new since version 1.2 */ + if (file_version >= FILE_VERSION_1_2) + { + byte store_participating_players = fgetc(file); + + for(i=0; i= FILE_VERSION_1_2) { - int j; + /* next check body chunk identifier and chunk length */ + fgets(chunk, CHUNK_ID_LEN + 1, file); + chunk_length = + (fgetc(file)<<24) | (fgetc(file)<<16) | (fgetc(file)<<8) | fgetc(file); + if (strcmp(chunk, "BODY") || + chunk_length != (num_participating_players + 1) * tape.length) + { + Error(ERR_WARN, "wrong 'BODY' chunk of tape file '%s'", filename); + fclose(file); + return; + } + } + for(i=0; i= MAX_TAPELEN) break; for(j=0; j 0) - { - tape.pos[i].action[j] = MV_NO_MOVING; + tape.pos[i].action[j] = MV_NO_MOVING; + + /* pre-1.2 tapes store data for only one player */ + if (file_version == FILE_VERSION_1_0 && j > 0) continue; - } - tape.pos[i].action[j] = fgetc(file); + + if (player_participates[j]) + tape.pos[i].action[j] = fgetc(file); } tape.pos[i].delay = fgetc(file); - if (levelrec_10) + if (file_version == FILE_VERSION_1_0) { /* eliminate possible diagonal moves in old tapes */ /* this is only for backward compatibility */ @@ -392,10 +520,14 @@ void LoadTape(int level_nr) void SaveTape(int level_nr) { - int i; + int i, j; char filename[MAX_FILENAME_LEN]; FILE *file; boolean new_tape = TRUE; + boolean player_participates[MAX_PLAYERS]; + byte store_participating_players; + int num_participating_players; + int chunk_length; InitTapeDirectory(leveldir[leveldir_nr].filename); @@ -413,36 +545,80 @@ void SaveTape(int level_nr) return; } + for(i=0; i> 24) & 0xff,file); - fputc((tape.random_seed >> 16) & 0xff,file); - fputc((tape.random_seed >> 8) & 0xff,file); - fputc((tape.random_seed >> 0) & 0xff,file); + fputc((chunk_length >> 24) & 0xff, file); + fputc((chunk_length >> 16) & 0xff, file); + fputc((chunk_length >> 8) & 0xff, file); + fputc((chunk_length >> 0) & 0xff, file); - fputc((tape.date >> 24) & 0xff,file); - fputc((tape.date >> 16) & 0xff,file); - fputc((tape.date >> 8) & 0xff,file); - fputc((tape.date >> 0) & 0xff,file); + fputc((tape.random_seed >> 24) & 0xff, file); + fputc((tape.random_seed >> 16) & 0xff, file); + fputc((tape.random_seed >> 8) & 0xff, file); + fputc((tape.random_seed >> 0) & 0xff, file); - fputc((tape.length >> 24) & 0xff,file); - fputc((tape.length >> 16) & 0xff,file); - fputc((tape.length >> 8) & 0xff,file); - fputc((tape.length >> 0) & 0xff,file); + fputc((tape.date >> 24) & 0xff, file); + fputc((tape.date >> 16) & 0xff, file); + fputc((tape.date >> 8) & 0xff, file); + fputc((tape.date >> 0) & 0xff, file); + + fputc((tape.length >> 24) & 0xff, file); + fputc((tape.length >> 16) & 0xff, file); + fputc((tape.length >> 8) & 0xff, file); + fputc((tape.length >> 0) & 0xff, file); + + fputc(store_participating_players, file); + + for(i=0; i> 24) & 0xff, file); + fputc((chunk_length >> 16) & 0xff, file); + fputc((chunk_length >> 8) & 0xff, file); + fputc((chunk_length >> 0) & 0xff, file); for(i=0; i