added check if loading tape file fails when auto-processing tapes
[rocksndiamonds.git] / src / tape.c
index 14bf45bcb383c975baa0780776272284cb42e107..82792fe116b922a4cd1f30062c38129dedfe1547 100644 (file)
@@ -312,7 +312,8 @@ static void DrawVideoDisplay_DateTime(unsigned int state, unsigned int value)
       char s[MAX_DATETIME_STRING_SIZE];
       int year2 = value / 10000;
       int year4 = (year2 < 70 ? 2000 + year2 : 1900 + year2);
-      int month_index = (value / 100) % 100;
+      int month_index_raw = (value / 100) % 100;
+      int month_index = month_index_raw % 12;  // prevent invalid index
       int month = month_index + 1;
       int day = value % 100;
 
@@ -512,6 +513,27 @@ static void PrintTapeReplayProgress(boolean replay_finished)
   }
 }
 
+static FILE *tape_log_file;
+
+static void OpenTapeLogfile(void)
+{
+  if (!(tape_log_file = fopen(options.tape_log_filename, MODE_WRITE)))
+    Warn("cannot write tape logfile '%s'", options.tape_log_filename);
+}
+
+static void WriteTapeLogfile(byte action[MAX_TAPE_ACTIONS])
+{
+  int i;
+
+  for (i = 0; i < MAX_TAPE_ACTIONS; i++)
+    putFile8Bit(tape_log_file, action[i]);
+}
+
+static void CloseTapeLogfile(void)
+{
+  fclose(tape_log_file);
+}
+
 
 // ============================================================================
 // tape control functions
@@ -936,6 +958,9 @@ byte *TapePlayAction(void)
   if (tape.auto_play)
     PrintTapeReplayProgress(FALSE);
 
+  if (options.tape_log_filename != NULL)
+    WriteTapeLogfile(action);
+
   return action;
 }
 
@@ -1313,6 +1338,9 @@ void AutoPlayTapes(void)
     // just finished auto-playing tape
     PrintTapeReplayProgress(TRUE);
 
+    if (options.tape_log_filename != NULL)
+      CloseTapeLogfile();
+
     if (global.autoplay_mode == AUTOPLAY_MODE_SAVE &&
        tape.auto_play_level_solved)
     {
@@ -1345,7 +1373,10 @@ void AutoPlayTapes(void)
 
       server_scores.uploaded = FALSE;
 
-      // temporarily save score tape (as the tape filename is unknown here)
+      // save score tape to upload to server; may be required for some reasons:
+      // * level set identifier in solution tapes may differ from level set
+      // * solution tape may have native format (like Supaplex solution files)
+
       SaveScoreTape(level_nr);
       SaveServerScore(level_nr);
 
@@ -1394,6 +1425,14 @@ void AutoPlayTapes(void)
 
       LoadTapeFromFilename(tape_filename);
 
+      if (tape.no_valid_file)
+      {
+       if (!fileExists(tape_filename))
+         Fail("tape file '%s' does not exist", tape_filename);
+       else
+         Fail("cannot load tape file '%s'", tape_filename);
+      }
+
       global.autoplay_leveldir = tape.level_identifier;
 
       if (tape.level_nr >= 0 && tape.level_nr < MAX_TAPES_PER_SET)
@@ -1549,6 +1588,9 @@ void AutoPlayTapes(void)
 
     InitCounter();
 
+    if (options.tape_log_filename != NULL)
+      OpenTapeLogfile();
+
     TapeStartGamePlaying();
     TapeStartWarpForward(global.autoplay_mode);