Change a "magic number" to a preprocessor macro. Also don't store and
[neverball] / ball / demo.c
index 66a6a79..51b2e6b 100644 (file)
 /*---------------------------------------------------------------------------*/
 
 #define MAGIC           0x52424EAF
-#define DEMO_VERSION    1
+#define DEMO_VERSION    2
 
-static FILE *demo_fp;
+#define DATELEN 20         
 
-static struct demo demos[MAXDEMO]; /* Array of scanned demos */
+static FILE *demo_fp;
 
-static int count; /* number of scanned demos */
+static struct demo demos[MAXDEMO]; /* Array of scanned demos  */
+static int         count;          /* Number of scanned demos */
 
 /*---------------------------------------------------------------------------*/
 
@@ -41,7 +42,6 @@ void demo_dump_info(const struct demo *d)
 {
     printf("Name:         %s\n"
            "File:         %s\n"
-           "NB Version:   %s\n"
            "Time:         %d\n"
            "Coins:        %d\n"
            "Mode:         %d\n"
@@ -59,19 +59,41 @@ void demo_dump_info(const struct demo *d)
            "Balls:        %d\n"
            "Total Time:   %d\n",
            d->name, d->filename,
-           d->nb_version,
            d->timer, d->coins, d->mode, d->state, ctime(&d->date),
            d->player,
            d->shot, d->file, d->back, d->grad, d->song,
            d->time, d->goal, d->score, d->balls, d->times);
 }
 
+static time_t make_time_from_utc(struct tm *tm)
+{
+    struct tm local, *utc;
+    time_t t;
+
+    t = mktime(tm);
+
+    local = *localtime(&t);
+    utc   =  gmtime(&t);
+
+    local.tm_year += local.tm_year - utc->tm_year;
+    local.tm_mon  += local.tm_mon  - utc->tm_mon ;
+    local.tm_mday += local.tm_mday - utc->tm_mday;
+    local.tm_hour += local.tm_hour - utc->tm_hour;
+    local.tm_min  += local.tm_min  - utc->tm_min ;
+    local.tm_sec  += local.tm_sec  - utc->tm_sec ;
+
+    return mktime(&local);
+}
+
 static int demo_header_read(FILE *fp, struct demo *d)
 {
     int magic;
     int version;
     int t;
 
+    struct tm date;
+    char datestr[DATELEN];
+
     get_index(fp, &magic);
     get_index(fp, &version);
 
@@ -81,10 +103,29 @@ static int demo_header_read(FILE *fp, struct demo *d)
     {
         d->timer = t;
 
-        get_index (fp, &d->coins);
-        get_index (fp, &d->state);
-        get_index (fp, &d->mode);
-        get_index (fp, (int *) &d->date);
+        get_index(fp, &d->coins);
+        get_index(fp, &d->state);
+        get_index(fp, &d->mode);
+
+#if 0
+        get_index(fp, (int *) &d->date);
+#endif
+        fread(datestr, 1, DATELEN, fp);
+        sscanf(datestr,
+               "%d-%d-%dT%d:%d:%d",
+               &date.tm_year,
+               &date.tm_mon,
+               &date.tm_mday,
+               &date.tm_hour,
+               &date.tm_min,
+               &date.tm_sec);
+
+        /* Convert certain values to valid structure member values. */
+
+        date.tm_year -= 1900;
+        date.tm_mon  -= 1;
+
+        d->date = make_time_from_utc(&date);
 
         fread(d->player, 1, MAXNAM, fp);
 
@@ -100,54 +141,77 @@ static int demo_header_read(FILE *fp, struct demo *d)
         get_index(fp, &d->balls);
         get_index(fp, &d->times);
 
-        fread(d->nb_version, 1, 20, fp);
-
         return 1;
     }
     return 0;
 }
 
-/* Create a new demo file, write the demo information structure.  If success,
- * return the file pointer positioned after the header.  If fail, return NULL.
- */
+static char *bname(const char *name, const char *suffix)
+{
+    static char buf[MAXSTR];
+
+    char *base;
+    size_t l;
+
+    /* Remove the directory delimiter */
+
+    base = strrchr(name, '/');
+#ifdef _WIN32
+    if (!base)
+        base = strrchr(name, '\\');
+    else
+    {
+        char *tmp;
+        if ((tmp = strrchr(base, '\\')))
+            base = tmp;
+    }
+#endif
+    strncpy(buf, base ? base + 1 : name, MAXSTR);
+
+    /* Remove the extension */
+
+    l = strlen(buf) - strlen(suffix);
+    if ((l > 1) && (strcmp(buf + l, suffix) == 0))
+        buf[l] = '\0';
 
-static FILE *demo_header_write(struct demo *d)
+    return buf;
+}
+
+static void demo_header_write(FILE *fp, struct demo *d)
 {
     int magic = MAGIC;
     int version = DEMO_VERSION;
     int zero  = 0;
 
-    FILE *fp;
+    char datestr[DATELEN];
 
-    if (d->filename && (fp = fopen(d->filename, FMODE_WB)))
-    {
-        put_index(fp, &magic);
-        put_index(fp, &version);
-        put_index(fp, &zero);
-        put_index(fp, &zero);
-        put_index(fp, &zero);
-        put_index(fp, &d->mode);
-        put_index(fp, (int *) &d->date);
-
-        fwrite(d->player, 1, MAXNAM, fp);
-
-        fwrite(d->shot, 1, PATHMAX, fp);
-        fwrite(d->file, 1, PATHMAX, fp);
-        fwrite(d->back, 1, PATHMAX, fp);
-        fwrite(d->grad, 1, PATHMAX, fp);
-        fwrite(d->song, 1, PATHMAX, fp);
-
-        put_index(fp, &d->time);
-        put_index(fp, &d->goal);
-        put_index(fp, &d->score);
-        put_index(fp, &d->balls);
-        put_index(fp, &d->times);
-
-        fwrite(d->nb_version, 1, 20, fp);
-
-        return fp;
-    }
-    return NULL;
+    strftime(datestr, DATELEN, "%Y-%m-%dT%H:%M:%S", gmtime(&d->date));
+
+    put_index(fp, &magic);
+    put_index(fp, &version);
+    put_index(fp, &zero);
+    put_index(fp, &zero);
+    put_index(fp, &zero);
+    put_index(fp, &d->mode);
+
+#if 0
+    put_index(fp, (int *) &d->date);
+#endif
+    fwrite(datestr, 1, DATELEN, fp);
+
+    fwrite(d->player, 1, MAXNAM, fp);
+
+    fwrite(d->shot, 1, PATHMAX, fp);
+    fwrite(d->file, 1, PATHMAX, fp);
+    fwrite(d->back, 1, PATHMAX, fp);
+    fwrite(d->grad, 1, PATHMAX, fp);
+    fwrite(d->song, 1, PATHMAX, fp);
+
+    put_index(fp, &d->time);
+    put_index(fp, &d->goal);
+    put_index(fp, &d->score);
+    put_index(fp, &d->balls);
+    put_index(fp, &d->times);
 }
 
 /* Update the demo header using the final level state. */
@@ -176,18 +240,8 @@ static void demo_scan_file(const char *filename)
     {
         if (demo_header_read(fp, d))
         {
-            char buf[PATHMAX];
-            int l;
-
-            strncpy(d->filename, config_user(filename), MAXSTR);
-
-            strncpy(buf, filename, PATHMAX);
-            l = strlen(buf) - strlen(REPLAY_EXT);
-
-            if ((l > 1) && (strcmp(buf + l, REPLAY_EXT) == 0))
-                buf[l] = '\0';
-
-            strncpy(d->name, buf, PATHMAX);
+            strncpy(d->filename, config_user(filename),       MAXSTR);
+            strncpy(d->name,     bname(filename, REPLAY_EXT), PATHMAX);
             d->name[PATHMAX - 1] = '\0';
 
             count++;
@@ -256,8 +310,8 @@ const struct demo *demo_get(int i)
 const char *date_to_str(time_t i)
 {
     static char str[MAXSTR];
-    struct tm *tm = localtime(&i);
-    strftime(str, MAXSTR, "%c", tm);
+
+    strftime(str, MAXSTR, "%c", localtime(&i));
     return str;
 }
 
@@ -323,12 +377,9 @@ int demo_play_init(const char *name,
     demo.balls = lg->balls;
     demo.times = lg->times;
 
-    strncpy(demo.nb_version, VERSION, 20);
-
-    demo_fp = demo_header_write(&demo);
-
-    if (demo_fp)
+    if (demo.filename && (demo_fp = fopen(demo.filename, FMODE_WB)))
     {
+        demo_header_write(demo_fp, &demo);
         audio_music_fade_to(2.0f, level->song);
         return game_init(level, lg->time, lg->goal);
     }
@@ -374,6 +425,10 @@ void demo_play_save(const char *name)
         strncpy(dst, config_user(name), PATHMAX);
         strcat(dst, REPLAY_EXT);
 
+#ifdef _WIN32
+        if (demo_exists(name))
+            remove(dst);
+#endif
         rename(src, dst);
     }
 }
@@ -407,7 +462,8 @@ int demo_replay_init(const char *name, struct level_game *lg)
 
     if (demo_fp && demo_header_read(demo_fp, &demo_replay))
     {
-        strncpy(demo_replay.filename, name, MAXSTR);
+        strncpy(demo_replay.filename, name,                    MAXSTR);
+        strncpy(demo_replay.name,     bname(name, REPLAY_EXT), PATHMAX);
 
         if (!demo_load_level(&demo_replay, &demo_level_replay))
             return 0;