save location & map, lighting qol
[carveJwlIkooP6JGAAIwe30JlM.git] / addon.c
1 #ifndef ADDON_C
2 #define ADDON_C
3
4 #include "addon.h"
5 #include "addon_types.h"
6 #include "vg/vg_msg.h"
7 #include "steam.h"
8 #include "workshop.h"
9
10 static u32 addon_count( enum addon_type type ){
11 return addon_system.registry_type_counts[ type ];
12 }
13
14 /* these kind of suck, oh well. */
15 static addon_reg *get_addon_from_index(enum addon_type type, u32 index){
16 u32 count = 0;
17 for( u32 i=0; count<addon_count(type); i++ ){
18 addon_reg *reg = &addon_system.registry[i];
19 if( reg->alias.type == type ){
20 if( index == count )
21 return reg;
22
23 count ++;
24 }
25 }
26
27 return NULL;
28 }
29
30 static u32 get_index_from_addon( enum addon_type type, addon_reg *a ){
31 u32 count = 0;
32 for( u32 i=0; count<addon_system.registry_type_counts[type]; i++ ){
33 addon_reg *reg = &addon_system.registry[i];
34 if( reg->alias.type == type ){
35 if( reg == a )
36 return count;
37
38 count ++;
39 }
40 }
41
42 return 0xffffffff;
43 }
44
45 static u32 addon_match( addon_alias *alias ){
46 u32 foldername_djb2 = vg_strdjb2( alias->foldername );
47
48 u32 count = 0;
49 for( u32 i=0; count<addon_system.registry_type_counts[alias->type]; i++ ){
50 addon_reg *reg = &addon_system.registry[i];
51 if( reg->alias.type == alias->type ){
52
53 if( alias->workshop_id ){
54 if( alias->workshop_id == reg->alias.workshop_id )
55 return count;
56 }
57 else{
58 if( reg->foldername_hash == foldername_djb2 ){
59 if( !strcmp( reg->alias.foldername, alias->foldername ) ){
60 return count;
61 }
62 }
63 }
64
65 count ++;
66 }
67 }
68
69 return 0xffffffff;
70 }
71
72 static void addon_system_init( void ){
73 u32 reg_size = sizeof(addon_reg)*ADDON_MOUNTED_MAX;
74 addon_system.registry = vg_linear_alloc( vg_mem.rtmemory, reg_size );
75
76 for( u32 type=0; type<k_addon_type_max; type++ ){
77 struct addon_type_info *inf = &addon_type_infos[type];
78 struct addon_cache *cache = &addon_system.cache[type];
79
80 if( inf->cache_count ){
81 /* create the allocations pool */
82 u32 alloc_size = sizeof(struct addon_cache_entry)*inf->cache_count;
83 cache->allocs = vg_linear_alloc( vg_mem.rtmemory, alloc_size );
84 memset( cache->allocs, 0, alloc_size );
85
86 cache->pool.buffer = cache->allocs;
87 cache->pool.count = inf->cache_count;
88 cache->pool.stride = sizeof( struct addon_cache_entry );
89 cache->pool.offset = offsetof( struct addon_cache_entry, poolnode );
90 vg_pool_init( &cache->pool );
91
92 /* create the real memory */
93 u32 cache_size = inf->cache_stride*inf->cache_count;
94 cache->items = vg_linear_alloc( vg_mem.rtmemory, cache_size );
95 cache->stride = inf->cache_stride;
96 memset( cache->items, 0, cache_size );
97
98 for( i32 j=0; j<inf->cache_count; j++ ){
99 struct addon_cache_entry *alloc = &cache->allocs[j];
100 alloc->reg_ptr = NULL;
101 alloc->reg_index = 0xffffffff;
102 }
103 }
104 }
105 }
106
107 /*
108 * Scanning routines
109 * -----------------------------------------------------------------------------
110 */
111
112 /*
113 * Reciever for scan completion. copies the registry counts back into main fred
114 */
115 VG_STATIC void async_addon_reg_update( void *data, u32 size )
116 {
117 vg_info( "Registry update notify\n" );
118
119 for( u32 i=0; i<k_addon_type_max; i++ ){
120 addon_system.registry_type_counts[i] = 0;
121 }
122
123 for( u32 i=0; i<addon_system.registry_count; i++ ){
124 enum addon_type type = addon_system.registry[i].alias.type;
125 addon_system.registry_type_counts[ type ] ++;
126 }
127 }
128
129 VG_STATIC void addon_set_foldername( addon_reg *reg, const char name[64] ){
130 vg_strncpy( name, reg->alias.foldername, 64, k_strncpy_always_add_null );
131 reg->foldername_hash = vg_strdjb2( reg->alias.foldername );
132 }
133
134 /*
135 * Create a new registry
136 */
137 VG_STATIC addon_reg *addon_alloc_reg( PublishedFileId_t workshop_id,
138 enum addon_type type ){
139 if( addon_system.registry_count == ADDON_MOUNTED_MAX ){
140 vg_error( "You have too many addons installed!\n" );
141 return NULL;
142 }
143
144 addon_reg *reg = &addon_system.registry[ addon_system.registry_count ];
145 reg->metadata_len = 0;
146 reg->cache_id = 0;
147 reg->state = k_addon_state_indexed;
148 reg->alias.workshop_id = workshop_id;
149 reg->alias.foldername[0] = '\0';
150 reg->alias.type = type;
151
152 if( workshop_id ){
153 char foldername[64];
154 snprintf( foldername, 64, PRINTF_U64, workshop_id );
155 addon_set_foldername( reg, foldername );
156 }
157 return reg;
158 }
159
160 /*
161 * If the addon.inf exists int the folder, load into the reg
162 */
163 VG_STATIC int addon_try_load_metadata( addon_reg *reg, vg_str folder_path ){
164 vg_str meta_path = folder_path;
165 vg_strcat( &meta_path, "/addon.inf" );
166 if( !vg_strgood( &meta_path ) ){
167 vg_error( "The metadata path is too long\n" );
168 return 0;
169 }
170
171 FILE *fp = fopen( meta_path.buffer, "rb" );
172 if( !fp ){
173 vg_error( "Could not open the '%s'\n", meta_path.buffer );
174 return 0;
175 }
176
177 reg->metadata_len = fread( reg->metadata, 1, 512, fp );
178 if( reg->metadata_len != 512 ){
179 if( !feof(fp) ){
180 fclose(fp);
181 vg_error( "unknown error codition" );
182 reg->metadata_len = 0;
183 return 0;
184 }
185 }
186 fclose(fp);
187 return 1;
188 }
189
190 VG_STATIC void addon_print_info( addon_reg *reg ){
191 vg_info( "addon_reg #%u{\n", addon_system.registry_count );
192 vg_info( " type: %d\n", reg->alias.type );
193 vg_info( " workshop_id: " PRINTF_U64 "\n", reg->alias.workshop_id );
194 vg_info( " folder: [%u]%s\n", reg->foldername_hash, reg->alias.foldername );
195 vg_info( " metadata_len: %u\n", reg->metadata_len );
196 vg_info( " cache_id: %hu\n", reg->cache_id );
197 vg_info( "}\n" );
198 }
199
200 VG_STATIC void addon_mount_finish( addon_reg *reg ){
201 addon_print_info( reg );
202 addon_system.registry_count ++;
203 }
204
205 /*
206 * Mount a fully packaged addon, one that certainly has a addon.inf
207 */
208 VG_STATIC addon_reg *addon_mount_workshop_folder( PublishedFileId_t workshop_id,
209 vg_str folder_path )
210 {
211 addon_reg *reg = addon_alloc_reg( workshop_id, k_addon_type_none );
212 if( !reg ) return NULL;
213
214 if( !addon_try_load_metadata( reg, folder_path ) ){
215 return NULL;
216 }
217
218 enum addon_type type = k_addon_type_none;
219 vg_msg root = {0};
220 root.buf = reg->metadata;
221 root.len = reg->metadata_len;
222 root.max = sizeof(reg->metadata);
223
224 vg_msg workshop = root;
225 if( vg_msg_seekframe( &workshop, "workshop", k_vg_msg_first )){
226 type = vg_msg_seekkvu32( &workshop, "type", k_vg_msg_first );
227 }
228
229 if( type == k_addon_type_none ){
230 vg_error( "Cannot determine addon type\n" );
231 return NULL;
232 }
233
234 reg->alias.type = type;
235 addon_mount_finish( reg );
236 return reg;
237 }
238
239 /*
240 * Mount a local folder. may or may not have addon.inf
241 */
242 VG_STATIC addon_reg *addon_mount_local_addon( const char *folder,
243 enum addon_type type,
244 const char *content_ext )
245 {
246 char folder_path_buf[4096];
247 vg_str folder_path;
248 vg_strnull( &folder_path, folder_path_buf, 4096 );
249 vg_strcat( &folder_path, folder );
250
251 const char *folder_name = vg_strch( &folder_path, '/' )+1;
252 u32 folder_hash = vg_strdjb2(folder_name);
253 for( u32 i=0; i<addon_system.registry_count; i++ ){
254 addon_reg *reg = &addon_system.registry[i];
255
256 if( (reg->alias.type == type) && (reg->foldername_hash == folder_hash) ){
257 if( !strcmp( reg->alias.foldername, folder_name ) ){
258 reg->state = k_addon_state_indexed;
259 return NULL;
260 }
261 }
262 }
263
264 addon_reg *reg = addon_alloc_reg( 0, type );
265 if( !reg ) return NULL;
266 addon_set_foldername( reg, folder_name );
267 addon_try_load_metadata( reg, folder_path );
268
269 if( reg->metadata_len == 0 ){
270 /* create our own content commands */
271 vg_msg msg = {0};
272 msg.buf = reg->metadata;
273 msg.len = 0;
274 msg.max = sizeof(reg->metadata);
275
276 u32 content_count = 0;
277
278 vg_strcat( &folder_path, "" );
279 vg_warn( "Creating own metadata for: %s\n", folder_path.buffer );
280
281 vg_dir subdir;
282 if( !vg_dir_open(&subdir, folder_path.buffer) ){
283 vg_error( "Failed to open '%s'\n", folder_path.buffer );
284 return NULL;
285 }
286
287 while( vg_dir_next_entry(&subdir) ){
288 if( vg_dir_entry_type(&subdir) == k_vg_entry_type_file ){
289 const char *fname = vg_dir_entry_name(&subdir);
290 vg_str file = folder_path;
291 vg_strcat( &file, "/" );
292 vg_strcat( &file, fname );
293 if( !vg_strgood( &file ) ) continue;
294
295 char *ext = vg_strch( &file, '.' );
296 if( !ext ) continue;
297 if( strcmp(ext,content_ext) ) continue;
298
299 vg_msg_wkvstr( &msg, "content", fname );
300 content_count ++;
301 }
302 }
303 vg_dir_close(&subdir);
304
305 if( !content_count ) return NULL;
306 if( msg.error == k_vg_msg_error_OK )
307 reg->metadata_len = msg.cur;
308 else{
309 vg_error( "Error creating metadata: %d\n", msg.error );
310 return NULL;
311 }
312 }
313
314 addon_mount_finish( reg );
315 return reg;
316 }
317
318 /*
319 * Check all subscribed items
320 */
321 VG_STATIC void addon_mount_workshop_items(void){
322 if( !steam_ready ) return;
323 /*
324 * Steam workshop scan
325 */
326 vg_info( "Mounting steam workshop subscriptions\n" );
327 PublishedFileId_t workshop_ids[ ADDON_MOUNTED_MAX ];
328 u32 workshop_count = ADDON_MOUNTED_MAX;
329
330 vg_async_item *call = vg_async_alloc(
331 sizeof(struct async_workshop_installed_files_info));
332 struct async_workshop_installed_files_info *info = call->payload;
333 info->buffer = workshop_ids;
334 info->len = &workshop_count;
335 vg_async_dispatch( call, async_workshop_get_installed_files );
336 vg_async_stall();
337
338 for( u32 j=0; j<workshop_count; j++ ){
339 /* check for existance in both our caches
340 * ----------------------------------------------------------*/
341 PublishedFileId_t id = workshop_ids[j];
342 for( u32 i=0; i<addon_system.registry_count; i++ ){
343 addon_reg *reg = &addon_system.registry[i];
344
345 if( reg->alias.workshop_id == id ){
346 reg->state = k_addon_state_indexed;
347 goto next_file_workshop;
348 }
349 }
350
351 vg_async_item *call1 =
352 vg_async_alloc( sizeof(struct async_workshop_filepath_info) );
353
354 char path[ 4096 ];
355
356 struct async_workshop_filepath_info *info = call1->payload;
357 info->buf = path;
358 info->id = id;
359 info->len = vg_list_size(path);
360 vg_async_dispatch( call1, async_workshop_get_filepath );
361 vg_async_stall(); /* too bad! */
362
363 vg_str folder = {.buffer = path, .i=strlen(path), .len=4096};
364 addon_mount_workshop_folder( id, folder );
365 next_file_workshop:;
366 }
367 }
368
369 /*
370 * Scan a local content folder for addons. It must find at least one file with
371 * the specified content_ext to be considered.
372 */
373 VG_STATIC void addon_mount_content_folder( enum addon_type type,
374 const char *base_folder,
375 const char *content_ext )
376 {
377 vg_info( "Mounting addons(type:%d) matching skaterift/%s/*/*%s\n",
378 type, base_folder, content_ext );
379
380 char path_buf[4096];
381 vg_str path;
382 vg_strnull( &path, path_buf, 4096 );
383 vg_strcat( &path, base_folder );
384
385 vg_dir dir;
386 if( !vg_dir_open(&dir,path.buffer) ){
387 vg_error( "vg_dir_open('%s') failed\n", path.buffer );
388 return;
389 }
390
391 vg_strcat(&path,"/");
392
393 while( vg_dir_next_entry(&dir) ){
394 if( vg_dir_entry_type(&dir) == k_vg_entry_type_dir ){
395 const char *d_name = vg_dir_entry_name(&dir);
396
397 vg_str folder = path;
398 if( strlen( d_name ) > ADDON_FOLDERNAME_MAX ){
399 vg_warn( "folder too long: %s\n", d_name );
400 continue;
401 }
402
403 vg_strcat( &folder, d_name );
404 if( !vg_strgood( &folder ) ) continue;
405
406 addon_mount_local_addon( folder.buffer, type, content_ext );
407 }
408 }
409 vg_dir_close(&dir);
410 }
411
412 /*
413 * write the full path of the addon's folder into the vg_str
414 */
415 static int addon_get_content_folder( addon_reg *reg, vg_str *folder ){
416 if( reg->alias.workshop_id ){
417 vg_async_item *call =
418 vg_async_alloc( sizeof(struct async_workshop_filepath_info) );
419 struct async_workshop_filepath_info *info = call->payload;
420 info->buf = folder->buffer;
421 info->id = reg->alias.workshop_id;
422 info->len = folder->len;
423 vg_async_dispatch( call, async_workshop_get_filepath );
424 vg_async_stall(); /* too bad! */
425 if( info->buf[0] == '\0' ){
426 vg_error( "Failed SteamAPI_GetItemInstallInfo(" PRINTF_U64 ")\n",
427 reg->alias.workshop_id );
428 return 0;
429 }
430 folder->i = strlen( folder->buffer );
431 return 1;
432 }
433 else{
434 folder->i = 0;
435
436 const char *local_folder =
437 addon_type_infos[reg->alias.type].local_content_folder;
438
439 if( !local_folder ) return 0;
440 vg_strcat( folder, local_folder );
441 vg_strcat( folder, reg->alias.foldername );
442 return 1;
443 }
444 }
445
446 /*
447 * Return existing cache id if reg_index points to a registry with its cache
448 * already set.
449 */
450 static u16 addon_cache_fetch( enum addon_type type, u32 reg_index ){
451 addon_reg *reg = NULL;
452
453 if( reg_index < addon_count( type ) ){
454 reg = get_addon_from_index( type, reg_index );
455 if( reg->cache_id )
456 return reg->cache_id;
457 }
458
459 return 0;
460 }
461
462 /*
463 * Allocate a new cache item from the pool
464 */
465 static u16 addon_cache_alloc( enum addon_type type, u32 reg_index ){
466 struct addon_cache *cache = &addon_system.cache[ type ];
467
468 u16 new_id = vg_pool_lru( &cache->pool );
469 struct addon_cache_entry *new_entry = vg_pool_item( &cache->pool, new_id );
470
471 addon_reg *reg = NULL;
472 if( reg_index < addon_count( type ) )
473 reg = get_addon_from_index( type, reg_index );
474
475 if( new_entry ){
476 if( new_entry->reg_ptr )
477 new_entry->reg_ptr->cache_id = 0;
478
479 if( reg )
480 reg->cache_id = new_id;
481
482 new_entry->reg_ptr = reg;
483 new_entry->reg_index = reg_index;
484 return new_id;
485 }
486 else{
487 vg_error( "cache full (type: %u)!\n", type );
488 return 0;
489 }
490 }
491
492 /*
493 * Get the real item data for cache id
494 */
495 static void *addon_cache_item( enum addon_type type, u16 id ){
496 if( !id ) return NULL;
497
498 struct addon_cache *cache = &addon_system.cache[type];
499 return cache->items + ((size_t)(id-1) * cache->stride);
500 }
501
502 /*
503 * Get the real item data for cache id ONLY if the item is completely loaded.
504 */
505 static void *addon_cache_item_if_loaded( enum addon_type type, u16 id ){
506 if( !id ) return NULL;
507
508 struct addon_cache *cache = &addon_system.cache[type];
509 struct addon_cache_entry *entry = vg_pool_item( &cache->pool, id );
510
511 if( entry->state == k_addon_cache_state_loaded )
512 return addon_cache_item( type, id );
513 else return NULL;
514 }
515
516 /*
517 * Updates the item state from the main thread
518 */
519 static void async_addon_setstate( void *_entry, u32 _state ){
520 addon_cache_entry *entry = _entry;
521 SDL_AtomicLock( &addon_system.sl_cache_using_resources );
522 entry->state = _state;
523 SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
524 vg_success( " loaded (%s)\n", entry->reg_ptr->alias.foldername );
525 }
526
527 /*
528 * Handles the loading of an individual item
529 */
530 static int addon_cache_load_request( enum addon_type type, u16 id,
531 addon_reg *reg, vg_str folder ){
532
533 /* load content files
534 * --------------------------------- */
535 vg_str content_path = folder;
536
537 vg_msg root = {0};
538 root.buf = reg->metadata;
539 root.len = reg->metadata_len;
540 root.max = sizeof(reg->metadata);
541
542 const char *kv_content = vg_msg_seekkvstr( &root, "content", 0 );
543 if( kv_content ){
544 vg_strcat( &content_path, "/" );
545 vg_strcat( &content_path, kv_content );
546 }
547 else{
548 vg_error( " No content paths in metadata\n" );
549 return 0;
550 }
551
552 if( !vg_strgood( &content_path ) ) {
553 vg_error( " Metadata path too long\n" );
554 return 0;
555 }
556
557 if( type == k_addon_type_board ){
558 struct player_board *board = addon_cache_item( type, id );
559 player_board_load( board, content_path.buffer );
560 return 1;
561 }
562 else if( type == k_addon_type_player ){
563 struct player_model *model = addon_cache_item( type, id );
564 player_model_load( model, content_path.buffer );
565 return 1;
566 }
567 else {
568 return 0;
569 }
570
571 return 0;
572 }
573
574 static void addon_cache_free_item( enum addon_type type, u16 id ){
575 if( type == k_addon_type_board ){
576 struct player_board *board = addon_cache_item( type, id );
577 player_board_unload( board );
578 }
579 else if( type == k_addon_type_player ){
580 struct player_model *model = addon_cache_item( type, id );
581 player_model_unload( model );
582 }
583 }
584
585 /*
586 * Goes over cache item load requests and calls the above ^
587 */
588 static void addon_cache_load_loop(void){
589 vg_info( "Running load loop\n" );
590 char path_buf[4096];
591
592 for( u32 type=0; type<k_addon_type_max; type++ ){
593 struct addon_cache *cache = &addon_system.cache[type];
594
595 for( u32 id=1; id<=cache->pool.count; id++ ){
596 addon_cache_entry *entry = vg_pool_item( &cache->pool, id );
597
598 SDL_AtomicLock( &addon_system.sl_cache_using_resources );
599 if( entry->state == k_addon_cache_state_load_request ){
600 vg_info( "process cache load request (%u#%u, reg:%u)\n",
601 type, id, entry->reg_index );
602
603 if( entry->reg_index >= addon_count(type) ){
604 /* should maybe have a different value for this case */
605 entry->state = k_addon_cache_state_none;
606 SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
607 continue;
608 }
609
610 SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
611
612 /* continue with the request */
613 addon_reg *reg = get_addon_from_index( type, entry->reg_index );
614 entry->reg_ptr = reg;
615
616 vg_str folder;
617 vg_strnull( &folder, path_buf, 4096 );
618 if( addon_get_content_folder( reg, &folder ) ){
619 if( addon_cache_load_request( type, id, reg, folder ) ){
620 vg_async_call( async_addon_setstate,
621 entry, k_addon_cache_state_loaded );
622 continue;
623 }
624 }
625
626 vg_warn( "cache item did not load (%u#%u)\n", type, id );
627 SDL_AtomicLock( &addon_system.sl_cache_using_resources );
628 entry->state = k_addon_cache_state_none;
629 SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
630 }
631 else
632 SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
633 }
634 }
635 }
636
637 /*
638 * Perform the cache interactions required to create a viewslot which will
639 * eventually be loaded by other parts of the system.
640 */
641 static u16 addon_cache_create_viewer( enum addon_type type, u16 reg_id ){
642 struct addon_cache *cache = &addon_system.cache[type];
643 vg_pool *pool = &cache->pool;
644
645 u16 cache_id = addon_cache_fetch( type, reg_id );
646 if( !cache_id ){
647 cache_id = addon_cache_alloc( type, reg_id );
648
649 if( cache_id ){
650 SDL_AtomicLock( &addon_system.sl_cache_using_resources );
651 addon_cache_entry *entry = vg_pool_item( pool, cache_id );
652
653 if( entry->state == k_addon_cache_state_loaded ){
654 addon_cache_free_item( type, cache_id );
655 }
656
657 entry->state = k_addon_cache_state_load_request;
658 SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
659 }
660 }
661
662 if( cache_id )
663 vg_pool_watch( pool, cache_id );
664
665 return cache_id;
666 }
667
668 static void addon_cache_watch( enum addon_type type, u16 cache_id ){
669 if( !cache_id ) return;
670
671 struct addon_cache *cache = &addon_system.cache[type];
672 vg_pool *pool = &cache->pool;
673 vg_pool_watch( pool, cache_id );
674 }
675
676 static void addon_cache_unwatch( enum addon_type type, u16 cache_id ){
677 if( !cache_id ) return;
678
679 struct addon_cache *cache = &addon_system.cache[type];
680 vg_pool *pool = &cache->pool;
681 vg_pool_unwatch( pool, cache_id );
682 }
683
684 #endif /* ADDON_C */