X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=save.c;h=88dfada37adfea272434b22bd19f8e6f5091346d;hb=e311bbe2fa903a7e2a922f202f389b799193195d;hp=93169db81eb4c32e25cf5c7b3903163062d1d523;hpb=844527ec68c063d78d4993bd8e4053f9ddc47b78;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/save.c b/save.c index 93169db..88dfada 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 void savedata_group_write( savedata_group *group ){ + for( u32 i=0; ifile_count; i++ ){ + savedata_file_write( &group->files[i] ); + } } -static savedata; -static void skaterift_write_savedata_thread(void *_){ - FILE *fp = fopen( "save.bkv", "wb" ); +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,58 @@ 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 = world_static.instance_addons[ which ]; + + 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) ); + + world_instance *instance = &world_static.instances[which]; + world_entity_serialize( instance, &sav ); + + vg_msg_frame( &sav, "player" ); + { + vg_msg_wkvnum( &sav, "position", k_vg_msg_float|k_vg_msg_32b, 3, + (which == world_static.active_instance)? + localplayer.rb.co: + instance->player_co ); } - else - alias->workshop_id = vg_msg_read_as_u64( &kv ); + vg_msg_end_frame( &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_wkvu32( &sav, "ach", skaterift.achievements ); vg_msg_frame( &sav, "player" ); { @@ -71,28 +123,50 @@ static void skaterift_write_savedata(void){ } vg_msg_end_frame( &sav ); - vg_msg_frame( &sav, "world" ); - { - 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 ); - } + file->len = sav.cur.co; +} + +static int skaterift_autosave( int async ){ + if( async ) + if( !vg_loader_availible() ) return 0; + + u32 save_files = 2; + if( world_static.instances[k_world_purpose_client].status + == k_world_status_loaded ){ + save_files ++; } - vg_msg_end_frame( &sav ); - savedata.len = sav.len; - vg_loader_start( skaterift_write_savedata_thread, NULL ); -} + vg_linear_clear( vg_async.buffer ); + u32 size = sizeof(savedata_group) + sizeof(savedata_file) * save_files; -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 ); + 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.instances[ k_world_purpose_client ].status + == k_world_status_loaded ){ + 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 */