Skip to content

Commit

Permalink
handle errors in saving in the server better
Browse files Browse the repository at this point in the history
  • Loading branch information
ckirmse committed Nov 16, 2024
1 parent 9d82c3a commit beba2e8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 48 deletions.
28 changes: 15 additions & 13 deletions blakserv/saveall.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,30 @@ INT64 SaveAll(void)
INT64 save_time;
char save_name[MAX_PATH+FILENAME_MAX];
char time_str[100];

/* Note: You must call GarbageCollect() right before SaveAll() */

/*
charlie: machine sets the local time from a time synch server
its potentially dangerous, but only if the time has been changed
between either server startup or last save, which is unlikely to
have drifted by much, things will only start to freak out if the
have drifted by much, things will only start to freak out if the
difference is more than say a minute behind, meaning the all the
timers will run for an extra minute.. its better this way as the
timers will run for an extra minute.. its better this way as the
time is going to be corrected every (currently) 4 hours , even
the worst PC clocks aren`t going to degrade that much in 4 hours
*/
/* The current time is used as a suffix to the save filenames.

/* The current time is used as a suffix to the save filenames.
We make our own copy since the time functions use a static
buffer. */
save_time = GetTime();
snprintf(time_str, sizeof(time_str), "%lli",(long long) save_time);

save_ok = True;

lprintf("Saving game (time stamp %s)...\n", time_str);

snprintf(save_name, sizeof(save_name), "%s%s%s",ConfigStr(PATH_LOADSAVE),GAME_FILE_SAVE,time_str);
if (SaveGame(save_name) == False)
save_ok = False;
Expand All @@ -67,13 +67,15 @@ INT64 SaveAll(void)
if (!SaveDynamicRsc(save_name))
save_ok = False;

if (save_ok)
if (save_ok) {
SaveControlFile(save_time);

lprintf("Save game successful (time stamp %s).\n", time_str);

if (save_ok)
lprintf("Save game successful (time stamp %s).\n", time_str);

return save_time;
}

lprintf("Save game NOT successful (time stamp %s).\n", time_str);

return 0;
}
Expand Down Expand Up @@ -105,6 +107,6 @@ void SaveControlFile(INT64 save_time)
fprintf(savefile,"#\n");
fprintf(savefile,"\n");
fprintf(savefile,"LASTSAVE %lli\n",(long long) save_time);

fclose(savefile);
}
80 changes: 45 additions & 35 deletions blakserv/savegame.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,63 @@
This module saves game information to the file, so it can be loaded
in by loadgame.c at some future time.
*/

#include "blakserv.h"

static FILE *savefile;
Bool is_save_successful;

#define SaveGameWrite(buf,len) \
{ \
if (fwrite(buf,len,1,savefile) != 1) \
eprintf("File %s Line %i error writing to file!\n",__FILE__,__LINE__); \
}
if (fwrite(buf,len,1,savefile) != 1) { \
eprintf("File %s Line %i error writing to file!\n",__FILE__,__LINE__); \
is_save_successful = False; \
} \
}

#define SaveGameWriteByte(byte) \
{ \
char ch; \
ch = byte; \
if (fwrite(&ch,1,1,savefile) != 1) \
eprintf("File %s Line %i error writing to file!\n",__FILE__,__LINE__); \
}
if (fwrite(&ch,1,1,savefile) != 1) { \
eprintf("File %s Line %i error writing to file!\n",__FILE__,__LINE__); \
is_save_successful = False; \
} \
}

#define SaveGameWriteInt(num) \
{ \
int temp; \
temp = num; \
if (fwrite(&temp,4,1,savefile) != 1) \
eprintf("File %s Line %i error writing to file!\n",__FILE__,__LINE__); \
}
if (fwrite(&temp,4,1,savefile) != 1) { \
eprintf("File %s Line %i error writing to file!\n",__FILE__,__LINE__); \
is_save_successful = False; \
} \
}

#define SaveGameWriteInt64(num) \
{ \
INT64 temp; \
temp = num; \
if (fwrite(&temp,8,1,savefile) != 1) \
eprintf("File %s Line %i error writing to file!\n",__FILE__,__LINE__); \
}
if (fwrite(&temp,8,1,savefile) != 1) { \
eprintf("File %s Line %i error writing to file!\n",__FILE__,__LINE__); \
is_save_successful = False; \
} \
}

#define SaveGameWriteString(s) \
{ \
size_t len_s; \
if (s == NULL) \
{ \
eprintf("SaveGameWriteString line %i can't save NULL string, invalid saved game\n", \
__LINE__); \
len_s = 0; \
SaveGameWrite(&len_s,2); \
return; \
} \
{ \
eprintf("SaveGameWriteString line %i can't save NULL string, invalid saved game\n", __LINE__); \
len_s = 0; \
SaveGameWrite(&len_s,2); \
is_save_successful = False; \
return; \
} \
len_s = strlen(s); \
SaveGameWrite(&len_s,2); \
SaveGameWrite(s,len_s); \
Expand Down Expand Up @@ -93,6 +102,7 @@ Bool SaveGame(char *filename)
eprintf("SaveGame can't open %s to save everything!!!\n",filename);
return False;
}
is_save_successful = true;

// Version number
SaveGameWriteByte('V');
Expand All @@ -103,12 +113,12 @@ Bool SaveGame(char *filename)
SaveSystem();
SaveObjects();
SaveListNodes();
SaveTimers();
SaveTimers();
SaveUsers();

fclose(savefile);
return True;

return is_save_successful;
}

void SaveClasses(void)
Expand All @@ -119,20 +129,20 @@ void SaveClasses(void)
void SaveEachClass(class_node *c)
{
int i;

if ((GetClassByID(c->class_id) != c) ||
(c->super_id != NO_SUPERCLASS && GetClassByID(c->super_id) == NULL) ||
(c->class_name == NULL))
{
eprintf("SaveEachClass is not saving invalid class %i\n",c->class_id);
return;
}

SaveGameWriteByte(SAVE_GAME_CLASS);
SaveGameWriteInt(c->class_id);
SaveGameWriteString(c->class_name);
SaveGameWriteInt(c->num_properties);

for (i=1;i<=c->num_properties;i++)
{
const char *s = GetPropertyNameByID(c,i);
Expand All @@ -157,7 +167,7 @@ void SaveEachResource(resource_node *r)
eprintf("SaveEachResource is not saving invalid resource %i\n",r->resource_id);
return;
}

SaveGameWriteByte(SAVE_GAME_RESOURCE);
SaveGameWriteInt(r->resource_id);
SaveGameWriteString(r->resource_name);
Expand All @@ -178,19 +188,19 @@ void SaveEachObject(object_node *o)
{
int i;
class_node *c;

c = GetClassByID(o->class_id);
if (c == NULL)
{
eprintf("SaveEachObject can't get class %i\n",o->class_id);
return;
}

SaveGameWriteByte(SAVE_GAME_OBJECT);
SaveGameWriteInt(o->object_id);
SaveGameWriteInt(o->class_id);
SaveGameWriteInt(c->num_properties);

/* remember, don't save self (p[0]) because no name for it!
* loadall will set self for us, fortunately.
*/
Expand Down Expand Up @@ -222,26 +232,26 @@ void SaveEachTimer(timer_node *t)
INT64 save_time;
object_node *o;
const char *s;

o = GetObjectByID(t->object_id);
if (o == NULL)
{
eprintf("SaveEachTimer can't get object %i\n",t->object_id);
return;
}

s = GetNameByID(t->message_id);
if (!s)
{
eprintf("SaveEachTimer is not saving invalid timer %i\n",t->object_id);
return;
}

SaveGameWriteByte(SAVE_GAME_TIMER);
SaveGameWriteInt(t->timer_id);
SaveGameWriteInt(t->object_id);
SaveGameWriteString(GetNameByID(t->message_id));

save_time = (INT64)(t->time - GetMilliCount());
if (save_time < 0)
save_time = 0;
Expand Down

0 comments on commit beba2e8

Please sign in to comment.