db609ca86a45ac0c07a9f1eeebc82cec4511713e
[carveJwlIkooP6JGAAIwe30JlM.git] / save.c
1 #include "skaterift.h"
2 #include "save.h"
3 #include "addon.h"
4 #include "vg/vg_msg.h"
5 #include "vg/vg_log.h"
6 #include "vg/vg_loader.h"
7 #include "world.h"
8
9 static const char *str_skaterift_main_save = "save.bkv";
10 static f64 last_autosave;
11
12 void savedata_file_write( savedata_file *file )
13 {
14 savedata_file *sav = file;
15 FILE *fp = fopen( sav->path, "wb" );
16 if( fp ){
17 fwrite( sav->buf, sav->len, 1, fp );
18 fclose( fp );
19 vg_success( "savedata written to '%s'\n", sav->path );
20 }
21 else {
22 vg_error( "Error writing savedata (%s)\n", sav->path );
23 }
24 }
25
26 void savedata_group_write( savedata_group *group )
27 {
28 for( u32 i=0; i<group->file_count; i++ ){
29 savedata_file_write( &group->files[i] );
30 }
31 }
32
33 void savedata_file_read( savedata_file *file )
34 {
35 FILE *fp = fopen( file->path, "rb" );
36 if( fp ){
37 file->len = fread( file->buf, 1, sizeof(file->buf), fp );
38 fclose( fp );
39 }
40 else{
41 file->len = 0;
42 vg_warn( "Error reading savedata (%s)\n", file->path );
43 }
44 }
45
46 static void skaterift_write_addon_alias( vg_msg *msg, const char *key,
47 addon_alias *alias ){
48 if( alias->workshop_id )
49 vg_msg_wkvnum( msg, key, k_vg_msg_u64, 1, &alias->workshop_id );
50 else
51 vg_msg_wkvstr( msg, key, alias->foldername );
52 }
53
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;
57
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;
61
62 if( reg )
63 skaterift_write_addon_alias( msg, key, &reg->alias );
64 }
65
66 void skaterift_read_addon_alias( vg_msg *msg, const char *key,
67 enum addon_type type,
68 addon_alias *alias )
69 {
70 alias->foldername[0] = '\0';
71 alias->workshop_id = 0;
72 alias->type = type;
73
74 vg_msg_cmd kv;
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 );
79 }
80 else
81 vg_msg_cast( kv.value, kv.code, &alias->workshop_id, k_vg_msg_u64 );
82 }
83 }
84
85 static void skaterift_populate_world_savedata( savedata_file *file,
86 enum world_purpose which ){
87 file->path[0] = '\0';
88 file->len = 0;
89 addon_reg *reg = world_static.instance_addons[ which ];
90
91 if( !reg ){
92 vg_error( "Tried to save unspecified world (reg was null)\n" );
93 return;
94 }
95
96 skaterift_world_get_save_path( which, file->path );
97
98 vg_msg sav;
99 vg_msg_init( &sav, file->buf, sizeof(file->buf) );
100
101 world_instance *instance = &world_static.instances[which];
102 world_entity_serialize( instance, &sav );
103
104 vg_msg_frame( &sav, "player" );
105 {
106 vg_msg_wkvnum( &sav, "position", k_vg_msg_float|k_vg_msg_32b, 3,
107 (which == world_static.active_instance)?
108 localplayer.rb.co:
109 instance->player_co );
110 }
111 vg_msg_end_frame( &sav );
112
113 file->len = sav.cur.co;
114 }
115
116 static void skaterift_populate_main_savedata( savedata_file *file )
117 {
118 strcpy( file->path, str_skaterift_main_save );
119
120 vg_msg sav;
121 vg_msg_init( &sav, file->buf, sizeof(file->buf) );
122 vg_msg_wkvnum( &sav, "ach", k_vg_msg_u32, 1, &skaterift.achievements );
123
124 vg_msg_frame( &sav, "player" );
125 {
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 );
130 }
131 vg_msg_end_frame( &sav );
132
133 file->len = sav.cur.co;
134 }
135
136 void skaterift_read_main_savedata( savedata_file *file )
137 {
138 strcpy( file->path, str_skaterift_main_save );
139 savedata_file_read( file );
140 }
141
142 int skaterift_autosave( int async )
143 {
144 if( async )
145 if( !vg_loader_availible() ) return 0;
146
147 u32 save_files = 2;
148 if( world_static.instances[k_world_purpose_client].status
149 == k_world_status_loaded ){
150 save_files ++;
151 }
152
153 vg_linear_clear( vg_async.buffer );
154 u32 size = sizeof(savedata_group) + sizeof(savedata_file) * save_files;
155
156 savedata_group *group;
157 if( async ){
158 size = vg_align8( size );
159 group = vg_linear_alloc( vg_async.buffer, size );
160 }
161 else
162 group = alloca( size );
163
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 );
167
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 );
172 }
173
174 if( async )
175 vg_loader_start( (void *)savedata_group_write, group );
176 else
177 savedata_group_write( group );
178
179 return 1;
180 }
181
182 void skaterift_autosave_synchronous(void)
183 {
184 skaterift_autosave(0);
185 }
186
187 void skaterift_autosave_update(void)
188 {
189 if( vg.time - last_autosave > 20.0 ){
190 if( skaterift_autosave(1) ){
191 last_autosave = vg.time;
192 }
193 }
194 }