X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=addon.c;fp=addon.c;h=c2ad38c0d929f34ab96c48da70a155b221d3715b;hb=badfa88dd109bbae5628f58504402f4707569f73;hp=0000000000000000000000000000000000000000;hpb=b8ff92a2caafa557608b84f4a037a5b3ce2628f7;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/addon.c b/addon.c new file mode 100644 index 0000000..c2ad38c --- /dev/null +++ b/addon.c @@ -0,0 +1,369 @@ +#ifndef ADDON_C +#define ADDON_C + +#include "addon.h" +#include "vg/vg_msg.h" +#include "steam.h" + +static u32 addon_count( enum workshop_file_type type ){ + return addon_system.registry_type_counts[ type ]; +} + +/* these kind of suck, oh well. */ +static addon_reg *get_addon_from_index(enum workshop_file_type type, u32 index){ + u32 count = 0; + for( u32 i=0; counttype == type ){ + if( index == count ) + return reg; + + count ++; + } + } + + return NULL; +} + +static u32 get_index_from_addon( enum workshop_file_type type, addon_reg *a ){ + u32 count = 0; + for( u32 i=0; counttype == type ){ + if( reg == a ) + return count; + + count ++; + } + } + + return 0xffffffff; +} + +static void addon_system_init( void ){ + u32 reg_size = sizeof(addon_reg)*ADDON_MOUNTED_MAX; + addon_system.registry = vg_linear_alloc( vg_mem.rtmemory, reg_size ); +} + +/* + * Scanning routines + * ----------------------------------------------------------------------------- + */ + +/* + * Reciever for scan completion. copies the registry counts back into main fred + */ +VG_STATIC void async_addon_reg_update( void *data, u32 size ) +{ + vg_info( "Registry update notify\n" ); + + for( u32 i=0; ifoldername, 64, k_strncpy_always_add_null ); + reg->foldername_hash = vg_strdjb2( reg->foldername ); +} + +/* + * Create a new registry + */ +VG_STATIC addon_reg *addon_alloc_reg( PublishedFileId_t workshop_id, + enum workshop_file_type type ){ + if( addon_system.registry_count == ADDON_MOUNTED_MAX ){ + vg_error( "You have too many addons installed!\n" ); + return NULL; + } + + addon_reg *reg = &addon_system.registry[ addon_system.registry_count ]; + reg->metadata_len = 0; + reg->userdata = NULL; + reg->state = k_addon_state_indexed; + reg->workshop_id = workshop_id; + reg->foldername[0] = '\0'; + reg->type = type; + + if( workshop_id ){ + char foldername[64]; + snprintf( foldername, 64, PRINTF_U64, workshop_id ); + addon_set_foldername( reg, foldername ); + } + return reg; +} + +/* + * If the addon.inf exists int the folder, load into the reg + */ +VG_STATIC int addon_try_load_metadata( addon_reg *reg, vg_str folder_path ){ + vg_str meta_path = folder_path; + vg_strcat( &meta_path, "/addon.inf" ); + if( !vg_strgood( &meta_path ) ){ + vg_error( "The metadata path is too long\n" ); + return 0; + } + + FILE *fp = fopen( meta_path.buffer, "rb" ); + if( !fp ){ + vg_error( "Could not open the '%s'\n", meta_path.buffer ); + return 0; + } + + reg->metadata_len = fread( reg->metadata, 1, 512, fp ); + if( reg->metadata_len != 512 ){ + if( !feof(fp) ){ + fclose(fp); + vg_error( "unknown error codition" ); + reg->metadata_len = 0; + return 0; + } + } + fclose(fp); + return 1; +} + +VG_STATIC void addon_mount_finish( addon_reg *reg ){ + vg_info( "addon_reg #%u{\n", addon_system.registry_count ); + vg_info( " type: %d\n", reg->type ); + vg_info( " workshop_id: " PRINTF_U64 "\n", reg->workshop_id ); + vg_info( " folder: [%u]%s\n", reg->foldername_hash, reg->foldername ); + vg_info( " metadata_len: %u\n", reg->metadata_len ); + vg_info( " userdata: %p\n", reg->userdata ); + vg_info( "}\n" ); + + addon_system.registry_count ++; +} + +/* + * Mount a fully packaged addon, one that certainly has a addon.inf + */ +VG_STATIC void addon_mount_workshop_folder( PublishedFileId_t workshop_id, + vg_str folder_path ) +{ + addon_reg *reg = addon_alloc_reg( workshop_id, k_workshop_file_type_none ); + if( !reg ) return; + + if( !addon_try_load_metadata( reg, folder_path ) ){ + return; + } + + enum workshop_file_type type = k_workshop_file_type_none; + vg_msg msg; + vg_msg_init( &msg, reg->metadata, reg->metadata_len ); + vg_msg_cmd cmd; + while( vg_msg_next( &msg, &cmd ) ){ + if( (msg.depth == 1) && (cmd.code == k_vg_msg_code_frame) ){ + if( VG_STRDJB2_EQ( "workshop", cmd.key, cmd.key_djb2 ) ){ + u32 depth = msg.depth; + while( (msg.depth == depth) && vg_msg_next( &msg, &cmd ) ){ + if( cmd.code & k_vg_msg_code_unsigned ){ + if( VG_STRDJB2_EQ( "type", cmd.key, cmd.key_djb2 ) ){ + type = cmd.value._u32; + } + } + } + } + } + } + + if( type == k_workshop_file_type_none ){ + vg_error( "Cannot determine addon type\n" ); + return; + } + + reg->type = type; + addon_mount_finish( reg ); +} + +/* + * Check all subscribed items + */ +VG_STATIC void addon_mount_workshop_items(void){ + if( !steam_ready ) return; + /* + * Steam workshop scan + */ + vg_info( "Mounting steam workshop subscriptions\n" ); + PublishedFileId_t workshop_ids[ ADDON_MOUNTED_MAX ]; + u32 workshop_count = ADDON_MOUNTED_MAX; + + vg_async_item *call = vg_async_alloc( + sizeof(struct async_workshop_installed_files_info)); + struct async_workshop_installed_files_info *info = call->payload; + info->buffer = workshop_ids; + info->len = &workshop_count; + vg_async_dispatch( call, async_workshop_get_installed_files ); + vg_async_stall(); + + for( u32 j=0; jworkshop_id == id ){ + reg->state = k_addon_state_indexed; + goto next_file_workshop; + } + } + + /* new one, lets find out what type it is + * ---------------------------------------------------------------*/ + vg_async_item *call1 = + vg_async_alloc( sizeof(struct async_workshop_filepath_info) ); + + char path[ 4096 ]; + + struct async_workshop_filepath_info *info = call1->payload; + info->buf = path; + info->id = id; + info->len = vg_list_size(path); + vg_async_dispatch( call1, async_workshop_get_filepath ); + vg_async_stall(); /* too bad! */ + + vg_str folder = {.buffer = path, .i=strlen(path), .len=4096}; + addon_mount_workshop_folder( id, folder ); +next_file_workshop:; + } +} + +/* + * Scan a local content folder for addons. It must find at least one file with + * the specified content_ext to be considered. + */ +VG_STATIC void addon_mount_local_folder( enum workshop_file_type type, + const char *base_folder, + const char *content_ext ) +{ + vg_info( "Mounting addons(type:%d) matching skaterift/%s/*/*%s\n", + type, base_folder, content_ext ); + + char path_buf[4096]; + vg_str path; + vg_strnull( &path, path_buf, 4096 ); + vg_strcat( &path, base_folder ); + + vg_dir dir; + if( !vg_dir_open(&dir,path.buffer) ){ + vg_error( "vg_dir_open('%s') failed\n", path.buffer ); + vg_async_call( workshop_async_any_complete, NULL, 0 ); + return; + } + + vg_strcat(&path,"/"); + + while( vg_dir_next_entry(&dir) ){ + if( vg_dir_entry_type(&dir) == k_vg_entry_type_dir ){ + const char *d_name = vg_dir_entry_name(&dir); + + vg_str folder = path; + char *folder_name = folder.buffer+folder.i; + + if( strlen( d_name ) > ADDON_FOLDERNAME_MAX ){ + vg_warn( "folder too long: %s\n", d_name ); + continue; + } + + vg_strcat( &folder, d_name ); + if( !vg_strgood( &folder ) ) continue; + + int present = 0; + u32 folder_hash = vg_strdjb2(folder_name); + for( u32 i=0; itype == type) && (reg->foldername_hash == folder_hash) ){ + if( !strcmp( reg->foldername, folder_name ) ){ + reg->state = k_addon_state_indexed; + present = 1; + break; + } + } + } + + if( !present ){ + addon_reg *reg = addon_alloc_reg( 0, type ); + if( !reg ) continue; + addon_set_foldername( reg, folder_name ); + addon_try_load_metadata( reg, folder ); + + if( reg->metadata_len == 0 ){ + /* create our own content commands */ + vg_msg msg; + vg_msg_init( &msg, reg->metadata, sizeof(reg->metadata) ); + + u32 content_count = 0; + + vg_strcat( &folder, "" ); + vg_warn( "Creating own metadata for: %s\n", folder.buffer ); + + vg_dir subdir; + if( !vg_dir_open(&subdir, folder.buffer) ){ + vg_error( "Failed to open '%s'\n", folder.buffer ); + continue; + } + + while( vg_dir_next_entry(&subdir) ){ + if( vg_dir_entry_type(&subdir) == k_vg_entry_type_file ){ + const char *fname = vg_dir_entry_name(&subdir); + vg_str file = folder; + vg_strcat( &file, "/" ); + vg_strcat( &file, fname ); + if( !vg_strgood( &file ) ) continue; + + char *ext = vg_strch( &file, '.' ); + if( !ext ) continue; + if( strcmp(ext,content_ext) ) continue; + + vg_msg_wkvstr( &msg, "content", fname ); + content_count ++; + } + } + vg_dir_close(&subdir); + + if( !content_count ){ + continue; + } + + if( msg.error == k_vg_msg_error_OK ) + reg->metadata_len = msg.cur; + else{ + vg_error( "Error creating metadata: %d\n", msg.error ); + continue; + } + } + + addon_mount_finish( reg ); + } + } + } + vg_dir_close(&dir); +} + +#if 0 +/* + * Async thread which scans local files for addons, as well as scheduling + * synchronous calls to the workshop + * + * Call this from in the loader thread. + */ +VG_STATIC void addon_system_scan_all(void){ + for( u32 i=0; istate = k_addon_state_indexed_absent; + } + + if( steam_ready ) addon_mount_workshop_items(); + + vg_async_call( addon_async_reg_update, NULL, 0 ); + vg_async_stall(); +} +#endif + +#endif /* ADDON_C */