+ {
+ static struct
+ {
+ char *name;
+ int size;
+ int (*loader)(FILE *, int, struct TapeInfo *);
+ }
+ chunk_info[] =
+ {
+ { "VERS", FILE_VERS_CHUNK_SIZE, LoadTape_VERS },
+ { "HEAD", TAPE_HEADER_SIZE, LoadTape_HEAD },
+ { "INFO", -1, LoadTape_INFO },
+ { "BODY", -1, LoadTape_BODY },
+ { NULL, 0, NULL }
+ };
+
+ while (getFileChunkBE(file, chunk_name, &chunk_size))
+ {
+ int i = 0;
+
+ while (chunk_info[i].name != NULL &&
+ strcmp(chunk_name, chunk_info[i].name) != 0)
+ i++;
+
+ if (chunk_info[i].name == NULL)
+ {
+ Error(ERR_WARN, "unknown chunk '%s' in tape file '%s'",
+ chunk_name, filename);
+ ReadUnusedBytesFromFile(file, chunk_size);
+ }
+ else if (chunk_info[i].size != -1 &&
+ chunk_info[i].size != chunk_size)
+ {
+ Error(ERR_WARN, "wrong size (%d) of chunk '%s' in tape file '%s'",
+ chunk_size, chunk_name, filename);
+ ReadUnusedBytesFromFile(file, chunk_size);
+ }
+ else
+ {
+ /* call function to load this tape chunk */
+ int chunk_size_expected =
+ (chunk_info[i].loader)(file, chunk_size, &tape);
+
+ /* the size of some chunks cannot be checked before reading other
+ chunks first (like "HEAD" and "BODY") that contain some header
+ information, so check them here */
+ if (chunk_size_expected != chunk_size)
+ {
+ Error(ERR_WARN, "wrong size (%d) of chunk '%s' in tape file '%s'",
+ chunk_size, chunk_name, filename);
+ }
+ }
+ }
+ }
+
+ fclose(file);
+
+ tape.length_seconds = GetTapeLength();
+
+#if 0
+ printf("tape game version: %d\n", tape.game_version);
+ printf("tape engine version: %d\n", tape.engine_version);
+#endif
+}
+
+void LoadTape(int level_nr)
+{
+ char *filename = getTapeFilename(level_nr);
+
+ LoadTapeFromFilename(filename);
+}
+
+static void SaveTape_VERS(FILE *file, struct TapeInfo *tape)
+{
+ putFileVersion(file, tape->file_version);
+ putFileVersion(file, tape->game_version);
+}
+
+static void SaveTape_HEAD(FILE *file, struct TapeInfo *tape)
+{
+ int i;
+ byte store_participating_players = 0;
+
+ /* set bits for participating players for compact storage */
+ for(i=0; i<MAX_PLAYERS; i++)
+ if (tape->player_participates[i])
+ store_participating_players |= (1 << i);
+
+ putFile32BitBE(file, tape->random_seed);
+ putFile32BitBE(file, tape->date);
+ putFile32BitBE(file, tape->length);
+
+ putFile8Bit(file, store_participating_players);
+
+ /* unused bytes not at the end here for 4-byte alignment of engine_version */
+ WriteUnusedBytesToFile(file, TAPE_HEADER_UNUSED);
+
+ putFileVersion(file, tape->engine_version);
+}
+
+static void SaveTape_INFO(FILE *file, struct TapeInfo *tape)
+{
+ int level_identifier_size = strlen(tape->level_identifier) + 1;
+ int i;
+
+ putFile16BitBE(file, level_identifier_size);
+
+ for(i=0; i < level_identifier_size; i++)
+ putFile8Bit(file, tape->level_identifier[i]);
+
+ putFile16BitBE(file, tape->level_nr);
+}
+
+static void SaveTape_BODY(FILE *file, struct TapeInfo *tape)
+{
+ int i, j;
+
+ for(i=0; i<tape->length; i++)
+ {
+ for(j=0; j<MAX_PLAYERS; j++)
+ if (tape->player_participates[j])
+ putFile8Bit(file, tape->pos[i].action[j]);
+
+ putFile8Bit(file, tape->pos[i].delay);
+ }