+/*
+ * op: k_async_op_world_scan
+ * -----------------------------------------------------------------------------
+ */
+
+/*
+ * Reciever for scan completion. copies the registry_count back into t0
+ */
+VG_STATIC void workshop_async_world_reg_update( void *data, u32 size )
+{
+ vg_info( "World registry update notify\n" );
+ global_skateshop.world_registry_count =
+ global_skateshop.t1_world_registry_count;
+ skaterift_end_op();
+}
+
+/*
+ * Add a local world folder to the registry, it will verify existing ones are
+ * still there too.
+ */
+VG_STATIC void world_scan_register_local( const char *folder_name )
+{
+ u32 hash = vg_strdjb2( folder_name );
+ for( u32 i=0; i<global_skateshop.t1_world_registry_count; i++ ){
+ struct registry_world *reg =
+ &global_skateshop.world_registry[i];
+
+ if( const_str_eq( hash, folder_name, reg->foldername ) ){
+ reg->state = k_registry_board_state_indexed;
+ return;
+ }
+ }
+
+ if( global_skateshop.t1_world_registry_count == SKATESHOP_WORLDS_MAX ){
+ vg_error( "You have too many worlds installed!\n" );
+ return;
+ }
+
+ vg_info( "new listing!: %s\n", folder_name );
+
+ struct registry_world *reg = &global_skateshop.world_registry[
+ global_skateshop.t1_world_registry_count ++ ];
+
+ vg_strncpy( folder_name, reg->foldername, 64, k_strncpy_overflow_fatal );
+ reg->foldername_hash = hash;
+ reg->state = k_registry_board_state_indexed;
+ reg->meta_present = 0;
+}
+
+/*
+ * Async thread which scans local files for boards, as well as scheduling
+ * synchronous calls to the workshop
+ */
+VG_STATIC void world_scan_thread( void *_args )
+{
+ vg_linear_clear( vg_mem.scratch );
+
+ for( u32 i=0; i<global_skateshop.t1_world_registry_count; i++ ){
+ struct registry_world *reg = &global_skateshop.world_registry[i];
+ reg->state = k_registry_board_state_indexed_absent;
+ }
+
+ /*
+ * Local disk scan
+ */
+ vg_info( "Scanning maps/*.mdl\n" );
+
+ char path_buf[4096];
+ vg_str path;
+ vg_strnull( &path, path_buf, 4096 );
+ vg_strcat( &path, "maps/" );
+
+ DIR *dir = opendir( path.buffer );
+ if( !dir ){
+ vg_error( "opendir('maps') failed\n" );
+ vg_async_call( workshop_async_any_complete, NULL, 0 );
+ return;
+ }
+
+ struct dirent *entry;
+ while( (entry = readdir(dir)) ){
+ if( entry->d_type == DT_DIR ){
+ if( entry->d_name[0] == '.' ) continue;
+
+ vg_str folder = path;
+ char *folder_name = folder.buffer+folder.i;
+
+ if( strlen( entry->d_name ) >
+ vg_list_size(global_skateshop.world_registry[0].foldername)){
+ vg_warn( "Map folder too long: %s\n", entry->d_name );
+ continue;
+ }
+
+ vg_strcat( &folder, entry->d_name );
+ if( !vg_strgood( &folder ) ) break;
+
+ DIR *subdir = opendir( folder.buffer );
+ while( (entry = readdir(subdir)) ){
+ if( entry->d_type == DT_REG ){
+ if( entry->d_name[0] == '.' ) continue;
+
+ vg_str file = folder;
+ vg_strcat( &file, "/" );
+ vg_strcat( &file, entry->d_name );
+ if( !vg_strgood( &file ) ) break;
+
+ char *ext = vg_strch( &file, '.' );
+ if( !ext ) continue;
+ if( strcmp(ext,".mdl") ) continue;
+
+ vg_strcat( &folder, "" );
+ world_scan_register_local( folder_name );
+ }
+ }
+ closedir(subdir);
+ }
+ }
+ closedir(dir);
+
+ vg_async_call( workshop_async_world_reg_update, NULL, 0 );
+
+#if 0
+ tinydir_close(&dir);
+
+ if( steam_ready ) workshop_steam_scan();
+
+ vg_async_call( workshop_async_reg_update, NULL, 0 );
+ vg_async_stall();
+ workshop_visibile_load_loop_thread(NULL);
+#endif
+}
+
+/*
+ * Asynchronous scan of local disk for worlds
+ */
+VG_STATIC void skateshop_op_world_scan(void)
+{
+ skaterift_begin_op( k_async_op_world_scan );
+ vg_loader_start( world_scan_thread, NULL );
+}
+