now fall in immobile mode
[carveJwlIkooP6JGAAIwe30JlM.git] / save.c
diff --git a/save.c b/save.c
index 6ea4e0f83307e437a04d7aa9e401a98e7e9fc06d..a3990d981dbef4c66e45e2569af79ab5704e9781 100644 (file)
--- a/save.c
+++ b/save.c
@@ -2,20 +2,40 @@
 #define SAVE_C
 
 #include "save.h"
+#include "addon.h"
 #include "vg/vg_msg.h"
+#include "vg/vg_log.h"
+#include "world.h"
 
-struct {
-   u8  buf[1024];
-   u32 len;
+static void savedata_file_write( savedata_file *file ){
+   savedata_file *sav = file;
+   FILE *fp = fopen( sav->path, "wb" );
+   if( fp ){
+      fwrite( sav->buf, sav->len, 1, fp );
+      fclose( fp );
+      vg_success( "savedata written to '%s'\n", sav->path );
+   }
+   else {
+      vg_error( "Error writing savedata (%s)\n", sav->path );
+   }
 }
-static savedata;
 
-static void skaterift_write_savedata_thread(void *_){
-   FILE *fp = fopen( "save.bkv", "wb" );
+static void savedata_group_write( savedata_group *group ){
+   for( u32 i=0; i<group->file_count; i++ ){
+      savedata_file_write( &group->files[i] );
+   }
+}
+
+static void savedata_file_read( savedata_file *file ){
+   FILE *fp = fopen( file->path, "rb" );
    if( fp ){
-      fwrite( savedata.buf, savedata.len, 1, fp );
+      file->len = fread( file->buf, 1, sizeof(file->buf), fp );
       fclose( fp );
    }
+   else{
+      file->len = 0;
+      vg_warn( "Error reading savedata (%s)\n", file->path );
+   }
 }
 
 static void skaterift_write_addon_alias( vg_msg *msg, const char *key,
@@ -41,7 +61,6 @@ static void skaterift_write_viewslot( vg_msg *msg, const char *key,
 static void skaterift_read_addon_alias( vg_msg *msg, const char *key,
                                         enum addon_type type, 
                                         addon_alias *alias ){
-   
    alias->foldername[0] = '\0';
    alias->workshop_id = 0;
    alias->type = type;
@@ -55,12 +74,47 @@ static void skaterift_read_addon_alias( vg_msg *msg, const char *key,
       alias->workshop_id = vg_msg_read_as_u64( &kv );
 }
 
-static void skaterift_write_savedata(void){
-   if( !vg_loader_availible() ) return;
+static void skaterift_populate_world_savedata( savedata_file *file,
+                                              enum world_purpose which ){
+   file->path[0] = '\0';
+   file->len = 0;
+   addon_reg *reg = NULL;
+   if( which == k_world_purpose_hub ) reg = world_static.addon_hub;
+   else reg = world_static.addon_client;
+
+   if( !reg ){
+      vg_error( "Tried to save unspecified world (reg was null)\n" );
+      return;
+   }
+
+   skaterift_world_get_save_path( which, file->path );
+
+   vg_msg sav = {0};
+   sav.buf = file->buf;
+   sav.max = sizeof(file->buf);
+
+   if( which == k_world_purpose_hub ){
+      if( world_static.instances[0].status == k_world_status_loaded )
+         world_entity_serialize( &world_static.instances[0], &sav );
+   }
+   else {
+      for( u32 i=1; i<vg_list_size(world_static.instances); i++ ){
+         world_instance *instance = &world_static.instances[i];
+         if( instance->status == k_world_status_loaded ){
+            world_entity_serialize( instance, &sav );
+         }
+      }
+   }
+
+   file->len = sav.len;
+}
+
+static void skaterift_populate_main_savedata( savedata_file *file ){
+   strcpy( file->path, str_skaterift_main_save );
 
    vg_msg sav = {0};
-   sav.buf = savedata.buf;
-   sav.max = sizeof(savedata.buf);
+   sav.buf = file->buf;
+   sav.max = sizeof(file->buf);
 
    vg_msg_frame( &sav, "player" );
    {
@@ -73,25 +127,56 @@ static void skaterift_write_savedata(void){
 
    vg_msg_frame( &sav, "world" );
    {
-      if( world_loader.reg && (world_static.active_world > 0) ){
-         skaterift_write_addon_alias( &sav, "alias", &world_loader.reg->alias );
-         vg_msg_wkvu32( &sav, "index", world_static.active_world );
-         vg_msg_wkvnum( &sav, "position", k_vg_msg_float|k_vg_msg_32b, 3, 
-                        localplayer.rb.co );
+      addon_reg *reg = world_static.addon_client;
+      if( reg && (world_static.active_instance > 0) ){
+         skaterift_write_addon_alias( &sav, "alias", &reg->alias );
       }
+      vg_msg_wkvu32( &sav, "index", world_static.active_instance );
+      vg_msg_wkvnum( &sav, "position", k_vg_msg_float|k_vg_msg_32b, 3, 
+                     localplayer.rb.co );
    }
    vg_msg_end_frame( &sav );
 
-   savedata.len = sav.len;
-   vg_loader_start( skaterift_write_savedata_thread, NULL );
+   file->len = sav.len;
 }
 
-static void skaterift_read_savedata(void){
-   FILE *fp = fopen( "save.bkv", "rb" );
-   if( fp ){
-      savedata.len = fread( savedata.buf, 1, sizeof(savedata.buf), fp );
-      fclose( fp );
+static int skaterift_autosave( int async ){
+   if( async )
+      if( !vg_loader_availible() ) return 0;
+
+   u32 save_files = 2;
+   if( world_static.addon_client )
+      save_files ++;
+
+   vg_linear_clear( vg_async.buffer );
+   u32 size = sizeof(savedata_group) + sizeof(savedata_file) * save_files;
+
+   savedata_group *group;
+   if( async ){
+      size = vg_align8( size );
+      group = vg_linear_alloc( vg_async.buffer, size );
    }
+   else
+      group = alloca( size );
+
+   group->file_count = save_files;
+   skaterift_populate_main_savedata( &group->files[0] );
+   skaterift_populate_world_savedata( &group->files[1], k_world_purpose_hub );
+
+   if( world_static.addon_client )
+      skaterift_populate_world_savedata( &group->files[2], 
+                                          k_world_purpose_client );
+
+   if( async )
+      vg_loader_start( (void *)savedata_group_write, group );
+   else
+      savedata_group_write( group );
+
+   return 1;
+}
+
+static void skaterift_autosave_synchronous(void){
+   skaterift_autosave(0);
 }
 
 #endif /* SAVE_C */