quite a lot of changes
[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 vg_async_call( workshop_async_any_complete, NULL, 0 );
389 return;
390 }
391
392 vg_strcat(&path,"/");
393
394 while( vg_dir_next_entry(&dir) ){
395 if( vg_dir_entry_type(&dir) == k_vg_entry_type_dir ){
396 const char *d_name = vg_dir_entry_name(&dir);
397
398 vg_str folder = path;
399 if( strlen( d_name ) > ADDON_FOLDERNAME_MAX ){
400 vg_warn( "folder too long: %s\n", d_name );
401 continue;
402 }
403
404 vg_strcat( &folder, d_name );
405 if( !vg_strgood( &folder ) ) continue;
406
407 addon_mount_local_addon( folder.buffer, type, content_ext );
408 }
409 }
410 vg_dir_close(&dir);
411 }
412
413 /*
414 * write the full path of the addon's folder into the vg_str
415 */
416 static int addon_get_content_folder( addon_reg *reg, vg_str *folder ){
417 if( reg->alias.workshop_id ){
418 vg_async_item *call =
419 vg_async_alloc( sizeof(struct async_workshop_filepath_info) );
420 struct async_workshop_filepath_info *info = call->payload;
421 info->buf = folder->buffer;
422 info->id = reg->alias.workshop_id;
423 info->len = folder->len;
424 vg_async_dispatch( call, async_workshop_get_filepath );
425 vg_async_stall(); /* too bad! */
426 if( info->buf[0] == '\0' ){
427 vg_error( "Failed SteamAPI_GetItemInstallInfo(" PRINTF_U64 ")\n",
428 reg->alias.workshop_id );
429 return 0;
430 }
431 folder->i = strlen( folder->buffer );
432 return 1;
433 }
434 else{
435 folder->i = 0;
436
437 const char *local_folder =
438 addon_type_infos[reg->alias.type].local_content_folder;
439
440 if( !local_folder ) return 0;
441 vg_strcat( folder, local_folder );
442 vg_strcat( folder, reg->alias.foldername );
443 return 1;
444 }
445 }
446
447 /*
448 * Return existing cache id if reg_index points to a registry with its cache
449 * already set.
450 */
451 static u16 addon_cache_fetch( enum addon_type type, u32 reg_index ){
452 addon_reg *reg = NULL;
453
454 if( reg_index < addon_count( type ) ){
455 reg = get_addon_from_index( type, reg_index );
456 if( reg->cache_id )
457 return reg->cache_id;
458 }
459
460 return 0;
461 }
462
463 /*
464 * Allocate a new cache item from the pool
465 */
466 static u16 addon_cache_alloc( enum addon_type type, u32 reg_index ){
467 struct addon_cache *cache = &addon_system.cache[ type ];
468
469 u16 new_id = vg_pool_lru( &cache->pool );
470 struct addon_cache_entry *new_entry = vg_pool_item( &cache->pool, new_id );
471
472 addon_reg *reg = NULL;
473 if( reg_index < addon_count( type ) )
474 reg = get_addon_from_index( type, reg_index );
475
476 if( new_entry ){
477 if( new_entry->reg_ptr )
478 new_entry->reg_ptr->cache_id = 0;
479
480 if( reg )
481 reg->cache_id = new_id;
482
483 new_entry->reg_ptr = reg;
484 new_entry->reg_index = reg_index;
485 return new_id;
486 }
487 else{
488 vg_error( "cache full (type: %u)!\n", type );
489 return 0;
490 }
491 }
492
493 /*
494 * Get the real item data for cache id
495 */
496 static void *addon_cache_item( enum addon_type type, u16 id ){
497 if( !id ) return NULL;
498
499 struct addon_cache *cache = &addon_system.cache[type];
500 return cache->items + ((size_t)(id-1) * cache->stride);
501 }
502
503 /*
504 * Get the real item data for cache id ONLY if the item is completely loaded.
505 */
506 static void *addon_cache_item_if_loaded( enum addon_type type, u16 id ){
507 if( !id ) return NULL;
508
509 struct addon_cache *cache = &addon_system.cache[type];
510 struct addon_cache_entry *entry = vg_pool_item( &cache->pool, id );
511
512 if( entry->state == k_addon_cache_state_loaded )
513 return addon_cache_item( type, id );
514 else return NULL;
515 }
516
517 /*
518 * Updates the item state from the main thread
519 */
520 static void async_addon_setstate( void *_entry, u32 _state ){
521 addon_cache_entry *entry = _entry;
522 SDL_AtomicLock( &addon_system.sl_cache_using_resources );
523 entry->state = _state;
524 SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
525 vg_success( " loaded (%s)\n", entry->reg_ptr->alias.foldername );
526 }
527
528 /*
529 * Handles the loading of an individual item
530 */
531 static int addon_cache_load_request( enum addon_type type, u16 id,
532 addon_reg *reg, vg_str folder ){
533
534 /* load content files
535 * --------------------------------- */
536 vg_str content_path = folder;
537
538 vg_msg root = {0};
539 root.buf = reg->metadata;
540 root.len = reg->metadata_len;
541 root.max = sizeof(reg->metadata);
542
543 const char *kv_content = vg_msg_seekkvstr( &root, "content", 0 );
544 if( kv_content ){
545 vg_strcat( &content_path, "/" );
546 vg_strcat( &content_path, kv_content );
547 }
548 else{
549 vg_error( " No content paths in metadata\n" );
550 return 0;
551 }
552
553 if( !vg_strgood( &content_path ) ) {
554 vg_error( " Metadata path too long\n" );
555 return 0;
556 }
557
558 if( type == k_addon_type_board ){
559 struct player_board *board = addon_cache_item( type, id );
560 player_board_load( board, content_path.buffer );
561 return 1;
562 }
563 else if( type == k_addon_type_player ){
564 struct player_model *model = addon_cache_item( type, id );
565 player_model_load( model, content_path.buffer );
566 return 1;
567 }
568 else {
569 return 0;
570 }
571
572 return 0;
573 }
574
575 static void addon_cache_free_item( enum addon_type type, u16 id ){
576 if( type == k_addon_type_board ){
577 struct player_board *board = addon_cache_item( type, id );
578 player_board_unload( board );
579 }
580 else if( type == k_addon_type_player ){
581 struct player_model *model = addon_cache_item( type, id );
582 player_model_unload( model );
583 }
584 }
585
586 /*
587 * Goes over cache item load requests and calls the above ^
588 */
589 static void addon_cache_load_loop(void){
590 vg_info( "Running load loop\n" );
591 char path_buf[4096];
592
593 for( u32 type=0; type<k_addon_type_max; type++ ){
594 struct addon_cache *cache = &addon_system.cache[type];
595
596 for( u32 id=1; id<=cache->pool.count; id++ ){
597 addon_cache_entry *entry = vg_pool_item( &cache->pool, id );
598
599 SDL_AtomicLock( &addon_system.sl_cache_using_resources );
600 if( entry->state == k_addon_cache_state_load_request ){
601 vg_info( "process cache load request (%u#%u, reg:%u)\n",
602 type, id, entry->reg_index );
603
604 if( entry->reg_index >= addon_count(type) ){
605 /* should maybe have a different value for this case */
606 entry->state = k_addon_cache_state_none;
607 SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
608 continue;
609 }
610
611 SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
612
613 /* continue with the request */
614 addon_reg *reg = get_addon_from_index( type, entry->reg_index );
615 entry->reg_ptr = reg;
616
617 vg_str folder;
618 vg_strnull( &folder, path_buf, 4096 );
619 if( addon_get_content_folder( reg, &folder ) ){
620 if( addon_cache_load_request( type, id, reg, folder ) ){
621 vg_async_call( async_addon_setstate,
622 entry, k_addon_cache_state_loaded );
623 continue;
624 }
625 }
626
627 vg_warn( "cache item did not load (%u#%u)\n", type, id );
628 SDL_AtomicLock( &addon_system.sl_cache_using_resources );
629 entry->state = k_addon_cache_state_none;
630 SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
631 }
632 else
633 SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
634 }
635 }
636 }
637
638 /*
639 * Perform the cache interactions required to create a viewslot which will
640 * eventually be loaded by other parts of the system.
641 */
642 static u16 addon_cache_create_viewer( enum addon_type type, u16 reg_id ){
643 struct addon_cache *cache = &addon_system.cache[type];
644 vg_pool *pool = &cache->pool;
645
646 u16 cache_id = addon_cache_fetch( type, reg_id );
647 if( !cache_id ){
648 cache_id = addon_cache_alloc( type, reg_id );
649
650 if( cache_id ){
651 SDL_AtomicLock( &addon_system.sl_cache_using_resources );
652 addon_cache_entry *entry = vg_pool_item( pool, cache_id );
653
654 if( entry->state == k_addon_cache_state_loaded ){
655 addon_cache_free_item( type, cache_id );
656 }
657
658 entry->state = k_addon_cache_state_load_request;
659 SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
660 }
661 }
662
663 if( cache_id )
664 vg_pool_watch( pool, cache_id );
665
666 return cache_id;
667 }
668
669 static void addon_cache_watch( enum addon_type type, u16 cache_id ){
670 if( !cache_id ) return;
671
672 struct addon_cache *cache = &addon_system.cache[type];
673 vg_pool *pool = &cache->pool;
674 vg_pool_watch( pool, cache_id );
675 }
676
677 static void addon_cache_unwatch( enum addon_type type, u16 cache_id ){
678 if( !cache_id ) return;
679
680 struct addon_cache *cache = &addon_system.cache[type];
681 vg_pool *pool = &cache->pool;
682 vg_pool_unwatch( pool, cache_id );
683 }
684
685 #endif /* ADDON_C */