small refactor of model loading
[carveJwlIkooP6JGAAIwe30JlM.git] / addon.c
diff --git a/addon.c b/addon.c
index 6f60311c52980fefdcb9c76f97bc82f8a76ded4f..59b3e320978561fcc6552aef45b3541476ddd243 100644 (file)
--- a/addon.c
+++ b/addon.c
@@ -1,13 +1,15 @@
-#ifndef ADDON_C
-#define ADDON_C
-
+#include "vg/vg_engine.h"
 #include "addon.h"
 #include "addon_types.h"
 #include "vg/vg_msg.h"
 #include "steam.h"
 #include "workshop.h"
+#include <string.h>
+
+struct addon_system addon_system;
 
-static u32 addon_count( enum addon_type type, u32 ignoreflags ){
+u32 addon_count( enum addon_type type, u32 ignoreflags )
+{
    if( ignoreflags ){
       u32 typecount = 0, count = 0;
       for( u32 i=0; typecount<addon_count( type, 0 ); i++ ){
@@ -30,8 +32,9 @@ static u32 addon_count( enum addon_type type, u32 ignoreflags ){
 
 
 /* these kind of suck, oh well. */
-static addon_reg *get_addon_from_index( enum addon_type type, u32 index, 
-                                        u32 ignoreflags ){
+addon_reg *get_addon_from_index( enum addon_type type, u32 index, 
+                                 u32 ignoreflags )
+{
    u32 typecount = 0, count = 0;
    for( u32 i=0; typecount<addon_count(type,0); i++ ){
       addon_reg *reg = &addon_system.registry[i];
@@ -51,7 +54,8 @@ static addon_reg *get_addon_from_index( enum addon_type type, u32 index,
    return NULL;
 }
 
-static u32 get_index_from_addon( enum addon_type type, addon_reg *a ){
+u32 get_index_from_addon( enum addon_type type, addon_reg *a )
+{
    u32 count = 0;
    for( u32 i=0; count<addon_system.registry_type_counts[type]; i++ ){
       addon_reg *reg = &addon_system.registry[i];
@@ -66,7 +70,8 @@ static u32 get_index_from_addon( enum addon_type type, addon_reg *a ){
    return 0xffffffff;
 }
 
-static u32 addon_match( addon_alias *alias ){
+u32 addon_match( addon_alias *alias )
+{
    if( alias->type == k_addon_type_none ) return 0xffffffff;
 
    u32 foldername_djb2 = 0;
@@ -100,7 +105,8 @@ static u32 addon_match( addon_alias *alias ){
 /*
  * Create a string version of addon alias in buf
  */
-static void addon_alias_uid( addon_alias *alias, char buf[ADDON_UID_MAX] ){
+void addon_alias_uid( addon_alias *alias, char buf[ADDON_UID_MAX] )
+{
    if( alias->workshop_id ){
       snprintf( buf, 128, "sr%03d-steam-"PRINTF_U64, 
             alias->type, alias->workshop_id );
@@ -114,7 +120,8 @@ static void addon_alias_uid( addon_alias *alias, char buf[ADDON_UID_MAX] ){
 /*
  * equality check
  */
-static int addon_alias_eq( addon_alias *a, addon_alias *b ){
+int addon_alias_eq( addon_alias *a, addon_alias *b )
+{
    if( a->type == b->type ){
       if( a->workshop_id == b->workshop_id ){
          if( a->workshop_id )
@@ -131,7 +138,8 @@ static int addon_alias_eq( addon_alias *a, addon_alias *b ){
 /*
  * make alias represent NULL.
  */
-static void invalidate_addon_alias( addon_alias *alias ){
+void invalidate_addon_alias( addon_alias *alias )
+{
    alias->type = k_addon_type_none;
    alias->workshop_id = 0;
    alias->foldername[0] = '\0';
@@ -140,7 +148,8 @@ static void invalidate_addon_alias( addon_alias *alias ){
 /*
  * parse uid to alias. returns 1 if successful
  */
-static int addon_uid_to_alias( const char *uid, addon_alias *alias ){
+int addon_uid_to_alias( const char *uid, addon_alias *alias )
+{
 /*           1
  * 01234567890123
  * sr&&&-@@@@@-#*
@@ -181,7 +190,8 @@ static int addon_uid_to_alias( const char *uid, addon_alias *alias ){
    return 1;
 }
 
-static void addon_system_init( void ){
+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 );
 
@@ -224,7 +234,7 @@ static void addon_system_init( void ){
 /*
  * Reciever for scan completion. copies the registry counts back into main fred
  */
-static void async_addon_reg_update( void *data, u32 size )
+void async_addon_reg_update( void *data, u32 size )
 {
    vg_info( "Registry update notify\n" );
    
@@ -335,7 +345,7 @@ static addon_reg *addon_mount_workshop_folder( PublishedFileId_t workshop_id,
    vg_msg_init( &msg, reg->metadata, reg->metadata_len );
 
    if( vg_msg_seekframe( &msg, "workshop" )){
-      type = vg_msg_getkvu32( &msg, "type", 0 );
+      vg_msg_getkvintg( &msg, "type", k_vg_msg_u32, &type );
    }
 
    if( type == k_addon_type_none ){
@@ -351,9 +361,9 @@ static addon_reg *addon_mount_workshop_folder( PublishedFileId_t workshop_id,
 /*
  * Mount a local folder. may or may not have addon.inf
  */
-static addon_reg *addon_mount_local_addon( const char *folder,
-                                              enum addon_type type,
-                                              const char *content_ext )
+addon_reg *addon_mount_local_addon( const char *folder,
+                                    enum addon_type type,
+                                    const char *content_ext )
 {
    char folder_path_buf[4096];
    vg_str folder_path;
@@ -428,7 +438,8 @@ static addon_reg *addon_mount_local_addon( const char *folder,
 /*
  * Check all subscribed items
  */
-static void addon_mount_workshop_items(void){
+void addon_mount_workshop_items(void)
+{
    if( skaterift.demo_mode ){
       vg_info( "Won't load workshop items in demo mode\n" );
       return;
@@ -485,9 +496,9 @@ 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.
  */
-static void addon_mount_content_folder( enum addon_type type,
-                                           const char *base_folder, 
-                                           const char *content_ext )
+void addon_mount_content_folder( enum addon_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 );
@@ -527,7 +538,8 @@ static void addon_mount_content_folder( enum addon_type type,
 /*
  * write the full path of the addon's folder into the vg_str
  */
-static int addon_get_content_folder( addon_reg *reg, vg_str *folder, int async){
+int addon_get_content_folder( addon_reg *reg, vg_str *folder, int async)
+{
    if( reg->alias.workshop_id ){
       struct async_workshop_filepath_info *info = NULL;
       vg_async_item *call = NULL;
@@ -576,7 +588,8 @@ static int addon_get_content_folder( addon_reg *reg, vg_str *folder, int async){
  * Return existing cache id if reg_index points to a registry with its cache
  * already set.
  */
-static u16 addon_cache_fetch( enum addon_type type, u32 reg_index ){
+u16 addon_cache_fetch( enum addon_type type, u32 reg_index )
+{
    addon_reg *reg = NULL;
 
    if( reg_index < addon_count( type, 0 ) ){
@@ -591,7 +604,8 @@ static u16 addon_cache_fetch( enum addon_type type, u32 reg_index ){
 /*
  * Allocate a new cache item from the pool
  */
-static u16 addon_cache_alloc( enum addon_type type, u32 reg_index ){
+u16 addon_cache_alloc( enum addon_type type, u32 reg_index )
+{
    struct addon_cache *cache = &addon_system.cache[ type ];
 
    u16 new_id = vg_pool_lru( &cache->pool );
@@ -621,7 +635,8 @@ static u16 addon_cache_alloc( enum addon_type type, u32 reg_index ){
 /*
  * Get the real item data for cache id 
  */
-static void *addon_cache_item( enum addon_type type, u16 id ){
+void *addon_cache_item( enum addon_type type, u16 id )
+{
    if( !id ) return NULL;
 
    struct addon_cache *cache = &addon_system.cache[type];
@@ -631,7 +646,8 @@ static void *addon_cache_item( enum addon_type type, u16 id ){
 /*
  * Get the real item data for cache id ONLY if the item is completely loaded.
  */
-static void *addon_cache_item_if_loaded( enum addon_type type, u16 id ){
+void *addon_cache_item_if_loaded( enum addon_type type, u16 id )
+{
    if( !id ) return NULL;
 
    struct addon_cache *cache = &addon_system.cache[type];
@@ -645,7 +661,8 @@ static void *addon_cache_item_if_loaded( enum addon_type type, u16 id ){
 /* 
  * Updates the item state from the main thread
  */
-static void async_addon_setstate( void *_entry, u32 _state ){
+void async_addon_setstate( void *_entry, u32 _state )
+{
    addon_cache_entry *entry = _entry;
    SDL_AtomicLock( &addon_system.sl_cache_using_resources );
    entry->state = _state;
@@ -712,7 +729,8 @@ static void addon_cache_free_item( enum addon_type type, u16 id ){
 /*
  * Goes over cache item load requests and calls the above ^
  */
-static void addon_cache_load_loop(void){
+void addon_cache_load_loop(void)
+{
    vg_info( "Running load loop\n" );
    char path_buf[4096];
 
@@ -765,7 +783,8 @@ static void addon_cache_load_loop(void){
  * Perform the cache interactions required to create a viewslot which will
  * eventually be loaded by other parts of the system.
  */
-static u16 addon_cache_create_viewer( enum addon_type type, u16 reg_id ){
+u16 addon_cache_create_viewer( enum addon_type type, u16 reg_id )
+{
    struct addon_cache *cache = &addon_system.cache[type];
    vg_pool *pool = &cache->pool;
 
@@ -792,8 +811,9 @@ static u16 addon_cache_create_viewer( enum addon_type type, u16 reg_id ){
    return cache_id;
 }
 
-static u16 addon_cache_create_viewer_from_uid( enum addon_type type,
-                                               char uid[ADDON_UID_MAX] ){
+u16 addon_cache_create_viewer_from_uid( enum addon_type type,
+                                        char uid[ADDON_UID_MAX] )
+{
    addon_alias q;
    if( !addon_uid_to_alias( uid, &q ) ) return 0;
    if( q.type != type ) return 0;
@@ -809,7 +829,8 @@ static u16 addon_cache_create_viewer_from_uid( enum addon_type type,
    }
 }
 
-static void addon_cache_watch( enum addon_type type, u16 cache_id ){
+void addon_cache_watch( enum addon_type type, u16 cache_id )
+{
    if( !cache_id ) return;
 
    struct addon_cache *cache = &addon_system.cache[type];
@@ -817,12 +838,11 @@ static void addon_cache_watch( enum addon_type type, u16 cache_id ){
    vg_pool_watch( pool, cache_id );
 }
 
-static void addon_cache_unwatch( enum addon_type type, u16 cache_id ){
+void addon_cache_unwatch( enum addon_type type, u16 cache_id )
+{
    if( !cache_id ) return;
 
    struct addon_cache *cache = &addon_system.cache[type];
    vg_pool *pool = &cache->pool;
    vg_pool_unwatch( pool, cache_id );
 }
-
-#endif /* ADDON_C */