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