X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=save.c;h=a8076c76a9f0fe3434eb7b1c55a2f07cd4d65639;hb=6b036c10d38e7d691eb0bc06c29235b450c3ff10;hp=6ea4e0f83307e437a04d7aa9e401a98e7e9fc06d;hpb=6929408e937dca64da707a2d61f8cdf6ab857e93;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/save.c b/save.c index 6ea4e0f..a8076c7 100644 --- 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; ifile_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,26 +61,60 @@ 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; - vg_msg_cmd kv = vg_msg_seekkv( msg, key, 0 ); - if( kv.code == k_vg_msg_kvstring ){ - vg_strncpy( kv.value, alias->foldername, sizeof(alias->foldername), - k_strncpy_allow_cutoff ); + vg_msg_cmd kv; + if( vg_msg_getkvcmd( msg, key, &kv ) ){ + if( kv.code == k_vg_msg_kvstring ){ + vg_strncpy( kv.value, alias->foldername, sizeof(alias->foldername), + k_strncpy_allow_cutoff ); + } + else + vg_msg_cast( kv.value, kv.code, &alias->workshop_id, k_vg_msg_u64 ); + } +} + +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; + vg_msg_init( &sav, file->buf, 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 - alias->workshop_id = vg_msg_read_as_u64( &kv ); + else { + for( u32 i=1; istatus == k_world_status_loaded ){ + world_entity_serialize( instance, &sav ); + } + } + } + + file->len = sav.cur.co; } -static void skaterift_write_savedata(void){ - if( !vg_loader_availible() ) return; +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); + vg_msg sav; + vg_msg_init( &sav, file->buf, 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", ®->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.cur.co; } -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 */