db609ca86a45ac0c07a9f1eeebc82cec4511713e
6 #include "vg/vg_loader.h"
9 static const char *str_skaterift_main_save
= "save.bkv";
10 static f64 last_autosave
;
12 void savedata_file_write( savedata_file
*file
)
14 savedata_file
*sav
= file
;
15 FILE *fp
= fopen( sav
->path
, "wb" );
17 fwrite( sav
->buf
, sav
->len
, 1, fp
);
19 vg_success( "savedata written to '%s'\n", sav
->path
);
22 vg_error( "Error writing savedata (%s)\n", sav
->path
);
26 void savedata_group_write( savedata_group
*group
)
28 for( u32 i
=0; i
<group
->file_count
; i
++ ){
29 savedata_file_write( &group
->files
[i
] );
33 void savedata_file_read( savedata_file
*file
)
35 FILE *fp
= fopen( file
->path
, "rb" );
37 file
->len
= fread( file
->buf
, 1, sizeof(file
->buf
), fp
);
42 vg_warn( "Error reading savedata (%s)\n", file
->path
);
46 static void skaterift_write_addon_alias( vg_msg
*msg
, const char *key
,
48 if( alias
->workshop_id
)
49 vg_msg_wkvnum( msg
, key
, k_vg_msg_u64
, 1, &alias
->workshop_id
);
51 vg_msg_wkvstr( msg
, key
, alias
->foldername
);
54 static void skaterift_write_viewslot( vg_msg
*msg
, const char *key
,
55 enum addon_type type
, u16 cache_id
){
56 if( !cache_id
) return;
58 struct addon_cache
*cache
= &addon_system
.cache
[type
];
59 addon_cache_entry
*entry
= vg_pool_item( &cache
->pool
, cache_id
);
60 addon_reg
*reg
= entry
->reg_ptr
;
63 skaterift_write_addon_alias( msg
, key
, ®
->alias
);
66 void skaterift_read_addon_alias( vg_msg
*msg
, const char *key
,
70 alias
->foldername
[0] = '\0';
71 alias
->workshop_id
= 0;
75 if( vg_msg_getkvcmd( msg
, key
, &kv
) ){
76 if( kv
.code
== k_vg_msg_kvstring
){
77 vg_strncpy( kv
.value
, alias
->foldername
, sizeof(alias
->foldername
),
78 k_strncpy_allow_cutoff
);
81 vg_msg_cast( kv
.value
, kv
.code
, &alias
->workshop_id
, k_vg_msg_u64
);
85 static void skaterift_populate_world_savedata( savedata_file
*file
,
86 enum world_purpose which
){
89 addon_reg
*reg
= world_static
.instance_addons
[ which
];
92 vg_error( "Tried to save unspecified world (reg was null)\n" );
96 skaterift_world_get_save_path( which
, file
->path
);
99 vg_msg_init( &sav
, file
->buf
, sizeof(file
->buf
) );
101 world_instance
*instance
= &world_static
.instances
[which
];
102 world_entity_serialize( instance
, &sav
);
104 vg_msg_frame( &sav
, "player" );
106 vg_msg_wkvnum( &sav
, "position", k_vg_msg_float
|k_vg_msg_32b
, 3,
107 (which
== world_static
.active_instance
)?
109 instance
->player_co
);
111 vg_msg_end_frame( &sav
);
113 file
->len
= sav
.cur
.co
;
116 static void skaterift_populate_main_savedata( savedata_file
*file
)
118 strcpy( file
->path
, str_skaterift_main_save
);
121 vg_msg_init( &sav
, file
->buf
, sizeof(file
->buf
) );
122 vg_msg_wkvnum( &sav
, "ach", k_vg_msg_u32
, 1, &skaterift
.achievements
);
124 vg_msg_frame( &sav
, "player" );
126 skaterift_write_viewslot( &sav
, "board", k_addon_type_board
,
127 localplayer
.board_view_slot
);
128 skaterift_write_viewslot( &sav
, "playermodel", k_addon_type_player
,
129 localplayer
.playermodel_view_slot
);
131 vg_msg_end_frame( &sav
);
133 file
->len
= sav
.cur
.co
;
136 void skaterift_read_main_savedata( savedata_file
*file
)
138 strcpy( file
->path
, str_skaterift_main_save
);
139 savedata_file_read( file
);
142 int skaterift_autosave( int async
)
145 if( !vg_loader_availible() ) return 0;
148 if( world_static
.instances
[k_world_purpose_client
].status
149 == k_world_status_loaded
){
153 vg_linear_clear( vg_async
.buffer
);
154 u32 size
= sizeof(savedata_group
) + sizeof(savedata_file
) * save_files
;
156 savedata_group
*group
;
158 size
= vg_align8( size
);
159 group
= vg_linear_alloc( vg_async
.buffer
, size
);
162 group
= alloca( size
);
164 group
->file_count
= save_files
;
165 skaterift_populate_main_savedata( &group
->files
[0] );
166 skaterift_populate_world_savedata( &group
->files
[1], k_world_purpose_hub
);
168 if( world_static
.instances
[ k_world_purpose_client
].status
169 == k_world_status_loaded
){
170 skaterift_populate_world_savedata( &group
->files
[2],
171 k_world_purpose_client
);
175 vg_loader_start( (void *)savedata_group_write
, group
);
177 savedata_group_write( group
);
182 void skaterift_autosave_synchronous(void)
184 skaterift_autosave(0);
187 void skaterift_autosave_update(void)
189 if( vg
.time
- last_autosave
> 20.0 ){
190 if( skaterift_autosave(1) ){
191 last_autosave
= vg
.time
;