From 1d2752b69b6285d5eb6436728c6cf84228a9ccde Mon Sep 17 00:00:00 2001 From: hgn Date: Mon, 12 Jul 2021 16:23:03 +0100 Subject: [PATCH] command line, multisampling, optimisations --- csRadar.c | 255 +++++++++++++++++++++++++++++++-------------------- csrDraw.h | 161 ++++++++++++++++++++++++++------ csrMath.h | 22 ++++- csrOpt.h | 175 +++++++++++++++++++++++++++++++++++ makeradar.sh | 1 + vfilesys.h | 90 +++++++++--------- vmf.h | 6 ++ 7 files changed, 539 insertions(+), 171 deletions(-) create mode 100644 csrOpt.h create mode 100755 makeradar.sh diff --git a/csRadar.c b/csRadar.c index 43b9c0e..2bde602 100644 --- a/csRadar.c +++ b/csRadar.c @@ -6,6 +6,7 @@ #include // CSR lib +#include "csrOpt.h" #include "csrTypes.h" #include "csrMath.h" #include "csrMem.h" @@ -21,130 +22,186 @@ #include "vmf.h" // CSR main -#include "csrDraw.h" #include "csr32f.h" +#include "csrDraw.h" +#define CSR_VERSION "0.0.1" -//#include "stretchy_buffer.h" +// gcc -Wall -fsanitize=address csRadar.c -o csRadar -lm int main( int argc, char *argv[] ) { - if( argc == 2 ) + char *arg; + char *strings[ 20 ]; + int num_strings = 0; + + float padding = 128.f; + u32 resolution = 1024; + int standard_layers = 0; + int write_txt = 1; + int multi_sample = 1; + char output_path[ 512 ]; // Full path eg. /home/harry/my_map.vmf + char vmf_name[ 128 ]; // Just the base name eg. my_map + int output_set = 0; + + while( csr_argp( argc, argv ) ) { - fs_set_gameinfo( "/home/harry/SteamLibrary/steamapps/common/Counter-Strike Global Offensive/csgo/gameinfo.txt" ); - - printf( "read: %s\n", argv[1] ); - //vdf_node *node = vdf_open_file( argv[1] ); - - vmf_map *map = vmf_init( argv[1], 1 ); - - csr_target target; - - csr_create_target( &target, 1024, 1024 ); - csr_rt_clear( &target ); - - csr_filter filter_layout = + if( (arg = csr_arg()) ) { - .classname = NULL, - .visgroup = "tar_layout" - }; + if( num_strings == 20 ) + { + fprintf( stderr, "Too many arguments! Max 20\n" ); + fs_exit(); + exit(0); + } + + strings[ num_strings ++ ] = arg; + } - csr_filter filter_buyzone = + if( (arg = csr_opt_arg( 'o' )) ) { - .classname = "func_buyzone", - .visgroup = NULL - }; + strcpy( output_path, arg ); + csr_path_winunix( output_path ); + + output_set = 1; + } - csr_filter filter_setup = + if( (arg = csr_opt_arg( 'g' )) ) { - .classname = NULL, - .visgroup = NULL, - .compute_bounds_only = 1 - }; - - draw_vmf_group( &target, map, map->root, &filter_setup, NULL, NULL ); - csr_auto_fit( &target, 128.f ); - - draw_vmf_group( &target, map, map->root, NULL, NULL, NULL ); - - float *rgba_test = (float *)csr_malloc( 1024*1024*sizeof(float)*3 ); + fs_set_gameinfo( arg ); + } - for( int l = 0; l < 1024; l ++ ) + if( (arg = csr_opt_arg( 'r' )) ) { - for( int x = 0; x < 1024; x ++ ) - { - float *dst = &rgba_test[ (l*1024+x)*3 ]; - csr_frag *src = &target.fragments[ ((1023-l)*1024+x) ]; - - dst[0] = src->co[0]; - dst[1] = src->co[1]; - dst[2] = src->co[2]; - } + resolution = atoi( arg ); } - csr_32f_write( "hello.pfm", 1024, 1024, rgba_test ); - - csr_rt_free( &target ); - - free( rgba_test ); - vmf_free( map ); - fs_exit(); - - /* - vmf_solid solid_main; - - - solidgen_ctx_init( &solid_main ); - - vdf_node *world = vdf_next( node, "world", NULL ); - - vdf_foreach( world, "solid", brush ) + if( (arg = csr_long_opt_arg( "padding" )) ) { - solidgen_push( &solid_main, brush ); + padding = atof( arg ); } - clock_t t; - t = clock(); - printf("Timer starts\n"); - - csr_frag *image = (csr_frag *)csr_malloc( 1024*1024*sizeof(csr_frag) ); - clear_depth( image, 1024, 1024 ); - - for( int i = 0; i < csr_sb_count( solid_main.indices )/3; i ++ ) + if( csr_opt( 'v' ) || csr_long_opt( "version" ) ) + { + printf( "csRadar version: " CSR_VERSION "\n" ); + return 0; + } + + if( csr_opt( 'h' ) || csr_long_opt( "help" ) ) + { + // Display help + printf + ( + "csRadar Copyright (C) 2021 Harry Godden (hgn)\n" + "\n" + "Usage: ./csRadar map.vmf -g \"/gamedir/gameinfo.txt\" layout cover\n" + " VMF file is first, then any other arguments (eg. layout, cover), will specify\n" + " visgroups to be rendered into individual files\n" + " No visgroups specified will simply draw everything\n" + "\n" + "Options:\n" + " -g Required if you are loading models\n" + " -r 1024 Output resolution\n" + " -o Specify output name/path\n" + " --padding=128 When cropping radar, add padding units to border\n" + " --standard-layers Use standard TAR layers/groups\n" + " --no-txt Don't create matching radar txt\n" + " --multi-sample= [ none, 2, 4, 4r, 8kn (default), 16c ]\n" + "\n" + " -v --version Display program version\n" + " -h --help Display this help text\n" + ); + + return 0; + } + } + + if( num_strings ) + { + vmf_map *map = vmf_init( strings[0], 1 ); + + if( map ) { - u32 * base = solid_main.indices + i*3; + // Path handling + if( !output_set ) + { + strcpy( output_path, strings[0] ); + csr_stripext( output_path ); + } + + char *base_name; + if( !(base_name = csr_findext( output_path, '/' ) )) + { + base_name = output_path; + } - vmf_vert tri[3]; + strcpy( vmf_name, base_name ); + + printf( "output_path: '%s'\nvmf_name: '%s'\n", output_path, vmf_name ); + + + // Main + + csr_target target; + + csr_create_target( &target, resolution, resolution ); + csr_rt_clear( &target ); + + // Compute bounds + csr_filter filter = + { + .classname = NULL, + .visgroup = NULL, + .compute_bounds_only = 1 + }; + draw_vmf_group( &target, map, map->root, &filter, NULL, NULL ); + csr_auto_fit( &target, padding ); + filter.compute_bounds_only = 0; + + if( num_strings == 1 ) + { + // Draw everything + draw_vmf_group( &target, map, map->root, NULL, NULL, NULL ); + csr_rt_save_buffers( &target, output_path, "all" ); + } + else + { + // Draw groups + for( int i = 1; i < num_strings; i ++ ) + { + filter.visgroup = strings[ i ]; + + draw_vmf_group( &target, map, map->root, NULL, NULL, NULL ); + csr_rt_save_buffers( &target, output_path, strings[i] ); + + csr_rt_clear( &target ); + } + } - tri[0] = solid_main.verts[ base[0] ]; - tri[1] = solid_main.verts[ base[1] ]; - tri[2] = solid_main.verts[ base[2] ]; + if( write_txt ) + { + char txt_path[ 512 ]; + + strcpy( txt_path, output_path ); + strcat( txt_path, ".txt" ); + + csr_write_txt( txt_path, vmf_name, &target ); + } - draw_buffers( image, 1024, 1024, (v4f){ -1000.f, -1000.f, 1000.f, 1000.f }, tri, 1 ); + csr_rt_free( &target ); + vmf_free( map ); } - - printf("Timer ends \n"); - t = clock() - t; - double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time - printf("Tracing took %f seconds to execute\n", time_taken); - - float *rgba_test = (float *)csr_malloc( 1024*1024*sizeof(float)*3 ); - - for( int i = 0; i < 1024*1024; i ++ ) + else { - rgba_test[i*3+0] = image[i].qa; - rgba_test[i*3+1] = image[i].qb; - rgba_test[i*3+2] = image[i].depth; + fprintf( stderr, "Could not load VMF\n" ); } - - csr_32f_write( "hello.pfm", 1024, 1024, rgba_test ); - - free( rgba_test ); - free( image ); - - solidgen_to_obj( &solid_main, "hello.obj" ); - - vdf_free_r( node ); - */ } + else + { + fprintf( stderr, "Missing required argument: mapfile\n" ); + } + + fs_exit(); + + return 0; } diff --git a/csrDraw.h b/csrDraw.h index 955ea30..29189a1 100644 --- a/csrDraw.h +++ b/csrDraw.h @@ -3,20 +3,23 @@ typedef struct csr_target csr_target; typedef struct csr_filter csr_filter; struct csr_frag -{ - u32 id; // Triangle index - float depth; // 'depth testing' - +{ v3f co; v3f nrm; + + float depth; }; struct csr_target { csr_frag *fragments; + u32 x, y; boxf bounds; float scale; + + v2f subsamples[ 16 ]; + int num_samples; }; struct csr_filter @@ -31,11 +34,31 @@ void csr_create_target( csr_target *rt, u32 x, u32 y ) { rt->x = x; rt->y = y; - rt->fragments = (csr_frag *)csr_malloc( x*y*sizeof(csr_frag) ); + rt->num_samples = 4; + + rt->fragments = (csr_frag *)csr_malloc( x*y*sizeof(csr_frag)*rt->num_samples ); + v3_fill( rt->bounds[0], INFINITY ); v3_fill( rt->bounds[1], -INFINITY ); } +void csr_update_subsamples( csr_target *rt ) +{ + float range_x = (rt->bounds[1][0]-rt->bounds[0][0]); + float range_y = (rt->bounds[1][1]-rt->bounds[0][1]); + + v2f pixel_size = { range_x/(float)rt->x, range_y/(float)rt->y }; + + rt->subsamples[0][0] = pixel_size[0] * -0.25f; + rt->subsamples[0][1] = 0.f; + rt->subsamples[1][0] = pixel_size[0] * 0.75f; + rt->subsamples[1][1] = pixel_size[1] * 0.25f; + rt->subsamples[2][0] = 0.f; + rt->subsamples[2][1] = pixel_size[1] * 0.5f; + rt->subsamples[3][0] = pixel_size[0] * 0.5f; + rt->subsamples[3][1] = pixel_size[1] * 0.75f; +} + void csr_rt_free( csr_target *rt ) { free( rt->fragments ); @@ -43,9 +66,11 @@ void csr_rt_free( csr_target *rt ) void csr_rt_clear( csr_target *rt ) { - for( u32 i = 0; i < rt->x*rt->y; i ++ ) + for( u32 i = 0; i < rt->x*rt->y*rt->num_samples; i ++ ) { - rt->fragments[ i ].depth = 0.f; + v3_zero( rt->fragments[ i ].co ); + v3_zero( rt->fragments[ i ].nrm ); + rt->fragments[i].depth = 0.f; } } @@ -68,13 +93,29 @@ void csr_auto_fit( csr_target *rt, float padding ) rt->bounds[1][1] = cy + d + padding; rt->scale = d + padding; + + csr_update_subsamples( rt ); } -void simple_raster( csr_target *rt, vmf_vert tri[3], int id ) +void csr_write_txt( char const *path, const char *name, csr_target *rt ) { - // Very simplified tracing algorithm - float tqa = 0.f, tqb = 0.f; + FILE *write_ptr; + + write_ptr = fopen( path, "w" ); + fprintf( write_ptr, "\"%s\"\n\{\n", name ); + fprintf( write_ptr, "\t\"material\" \"overviews/%s\"\n", name ); + fprintf( write_ptr, "\t\"pos_x\" \"%.8f\"\n", rt->bounds[0][0] ); + fprintf( write_ptr, "\t\"pos_y\" \"%.8f\"\n", rt->bounds[0][1] ); + fprintf( write_ptr, "\t\"scale\" \"%.8f\"\n", rt->scale / (float)rt->x ); + fprintf( write_ptr, "}\n" ); + + fclose( write_ptr ); +} + +void simple_raster( csr_target *rt, vmf_vert tri[3] ) +{ + // Very very simplified rasterizing algorithm v2f bmin = { 0.f, 0.f }; v2f bmax = { rt->x, rt->y }; @@ -87,13 +128,24 @@ void simple_raster( csr_target *rt, vmf_vert tri[3], int id ) float range_x = (rt->bounds[1][0]-rt->bounds[0][0])/(float)rt->x; float range_y = (rt->bounds[1][1]-rt->bounds[0][1])/(float)rt->y; - int start_x = csr_min( rt->x-1, csr_max( 0, floorf( (bmin[0]-rt->bounds[0][0])/range_x))); - int end_x = csr_max( 0, csr_min( rt->x-1, floorf( (bmax[0]-rt->bounds[0][0])/range_x ))); - int start_y = csr_min( rt->y-1, csr_max( 0, ceilf( (bmin[1]-rt->bounds[0][1])/range_y ))); - int end_y = csr_max( 0, csr_min( rt->y-1, ceilf( (bmax[1]-rt->bounds[0][1])/range_y ))); + int start_x = csr_min( rt->x-1, csr_max( 0, floorf( (bmin[0]-rt->bounds[0][0])/range_x))); + int end_x = csr_max( 0, csr_min( rt->x-1, ceilf( (bmax[0]-rt->bounds[0][0])/range_x))); + int start_y = csr_min( rt->y-1, csr_max( 0, floorf( (bmin[1]-rt->bounds[0][1])/range_y))); + int end_y = csr_max( 0, csr_min( rt->y-1, ceilf( (bmax[1]-rt->bounds[0][1])/range_y))); + + v2f v0, v1, v2, vp; + float d, bca = 0.f, bcb = 0.f, bcc = 0.f; - v3f trace_dir = { 0.f, 0.f, 1.f }; - v3f trace_origin = { 0.f, 0.f, -16385.f }; + v2_sub( tri[1].co, tri[0].co, v0 ); + v2_sub( tri[2].co, tri[0].co, v1 ); + v2_sub( tri[1].co, tri[2].co, v2 ); + d = 1.f / (v0[0]*v1[1] - v1[0]*v0[1]); + + // Backface culling + if( v2_cross( v0, v1 ) > 0.f ) + return; + + v2f trace_origin; for( u32 py = start_y; py <= end_y; py ++ ) { @@ -101,18 +153,44 @@ void simple_raster( csr_target *rt, vmf_vert tri[3], int id ) for( u32 px = start_x; px <= end_x; px ++ ) { - csr_frag *frag = &rt->fragments[ py * rt->y + px ]; + csr_frag *frag = &rt->fragments[ (py * rt->y + px) * rt->num_samples ]; trace_origin[0] = csr_lerpf( rt->bounds[0][0], rt->bounds[1][0], (float)px/(float)rt->x ); - float tdepth = csr_ray_tri( trace_origin, trace_dir, tri[0].co, tri[1].co, tri[2].co, &tqa, &tqb ); - if( tdepth > frag->depth ) + // Determine coverage + for( int i = 0; i < rt->num_samples; i ++ ) { - frag->depth = tdepth; + v3f sample_origin; - v3_muls( tri[1].co, tqa, frag->co ); - v3_muladds( frag->co, tri[2].co, tqb, frag->co ); - v3_muladds( frag->co, tri[0].co, 1.f - tqa - tqb, frag->co ); + v2_add( rt->subsamples[ i ], trace_origin, sample_origin ); + v2_sub( sample_origin, tri[0].co, vp ); + + if( v2_cross( v0, vp ) > 0.f ) + continue; + if( v2_cross( vp, v1 ) > 0.f ) + continue; + + v2f vp2; + v2_sub( sample_origin, tri[2].co, vp2 ); + + if( v2_cross( vp2, v2 ) > 0.f ) + continue; + + bcb = (vp[0]*v1[1] - v1[0]*vp[1]) * d; + bcc = (v0[0]*vp[1] - vp[0]*v0[1]) * d; + bca = 1.f - bcb - bcc; + + float hit = (tri[0].co[2] * bca + tri[1].co[2] * bcb + tri[2].co[2] * bcc) +16385.f; + + if( hit > frag[i].depth ) + { + frag[i].depth = hit; + v3_muls( tri[0].co, bca, frag[i].co ); + v3_muladds( frag[i].co, tri[1].co, bcb, frag[i].co ); + v3_muladds( frag[i].co, tri[2].co, bcc, frag[i].co ); + + // TODO: Same for normal map + } } } } @@ -138,7 +216,7 @@ void csr_draw( csr_target *rt, vmf_vert *triangles, u32 triangle_count, m4x3f tr m3x3_mulv( normal, triangle[1].nrm, new_tri[1].nrm ); m3x3_mulv( normal, triangle[2].nrm, new_tri[2].nrm ); - simple_raster( rt, new_tri, 0 ); + simple_raster( rt, new_tri ); } } @@ -220,7 +298,7 @@ void draw_vmf_group( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *f m4x3_transform_aabb( model, trf_bounds ); // Join - //box_concat( rt->bounds, trf_bounds ); + box_concat( rt->bounds, trf_bounds ); } else { @@ -278,8 +356,35 @@ void draw_vmf_group( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *f solidgen_ctx_reset( &solid ); solidgen_ctx_free( &solid ); +} + +void csr_rt_save_buffers( csr_target *rt, const char *basename, const char *subname ) +{ + char output[ 512 ]; + + float *image = (float *)csr_malloc( 1024*1024*sizeof(float)*3 ); + + for( int l = 0; l < rt->x; l ++ ) + { + for( int x = 0; x < rt->y; x ++ ) + { + float *dst = &image[ (l*1024+x)*3 ]; + csr_frag *src = &rt->fragments[ ((1023-l)*1024+x)*rt->num_samples ]; + + v3_zero( dst ); + v3_muls( src[0].co, 1.f/(float)rt->num_samples, dst ); + v3_muladds( dst, src[1].co, 1.f/(float)rt->num_samples, dst ); + v3_muladds( dst, src[2].co, 1.f/(float)rt->num_samples, dst ); + v3_muladds( dst, src[3].co, 1.f/(float)rt->num_samples, dst ); + } + } + + // Save position buffer + strcpy( output, basename ); + strcat( output, "." ); + strcat( output, subname ); + strcat( output, "_position.pfm" ); + csr_32f_write( output, rt->x, rt->y, image ); - printf( "Bounds resolved to: (%.3f, %.3f, %.3f) -> (%.3f, %.3f, %.3f)\n", - rt->bounds[0][0],rt->bounds[0][1],rt->bounds[0][2], - rt->bounds[1][0],rt->bounds[1][1],rt->bounds[1][2] ); + free( image ); } diff --git a/csrMath.h b/csrMath.h index fae268d..31891f8 100644 --- a/csrMath.h +++ b/csrMath.h @@ -63,9 +63,29 @@ void v2_maxv( v2f a, v2f b, v2f dest ) dest[1] = csr_maxf(a[1], b[1]); } +void v2_sub( v2f a, v2f b, v2f d ) +{ + d[0] = a[0]-b[0]; d[1] = a[1]-b[1]; +} + +float v2_cross( v2f a, v2f b ) +{ + return a[0] * b[1] - a[1] * b[0]; +} + +void v2_add( v2f a, v2f b, v2f d ) +{ + d[0] = a[0]+b[0]; d[1] = a[1]+b[1]; +} + // Vector 3 // ================================================================================================================== +void v3_zero( v3f a ) +{ + a[0] = 0.f; a[1] = 0.f; a[2] = 0.f; +} + void v3_copy( v3f a, v3f b ) { b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; @@ -491,7 +511,7 @@ int csr_slabs( v3f box[2], v3f o, v3f id ) float csr_ray_tri( v3f o, v3f d, v3f v0, v3f v1, v3f v2, float *u, float *v ) { - float const k_cullEpsilon = 0.0001f; + float const k_cullEpsilon = 0.000001f; v3f v0v1; v3f v0v2; diff --git a/csrOpt.h b/csrOpt.h new file mode 100644 index 0000000..9195790 --- /dev/null +++ b/csrOpt.h @@ -0,0 +1,175 @@ +// Supported: +// -abc +// -a value +// -ab value +// +// --long-value=test +// regular_thing + +static int csr_argi = 1; +static int csr_argj = 1; +static int csr_argc = 0; +static int csr_consume_next = 0; +static char **csr_argv; + +// Will return 0 if exhausted +int csr_argp( int argc, char *argv[] ) +{ + csr_argv = argv; + csr_argc = argc; + + static int delta_i = 0; + static int delta_j = 0; + + if( csr_argj != 1 && !csr_argv[ csr_argi ][ csr_argj ] ) + { + csr_argj = 1; + csr_argi ++; + } + + if( csr_consume_next ) + { + csr_consume_next = 0; + csr_argi ++; + } + + if( csr_argi >= argc ) + return 0; + + if( (delta_i == csr_argi) && (delta_j == csr_argj) ) + { + char *cur = &csr_argv[ csr_argi ][ csr_argj ]; + + if( *cur != '-' ) + { + fprintf( stderr, "Unknown opt '-%c'\n", *cur ); + } + else + { + fprintf( stderr, "Unknown opt '--%s'\n", cur + 1 ); + } + + exit(0); + } + + delta_i = csr_argi; + delta_j = csr_argj; + + return 1; +} + +// Example: see if -c is set +int csr_opt( char c ) +{ + char *carg = csr_argv[ csr_argi ]; + + if( carg[0] == '-' ) + { + if( carg[1] == '-' ) + return 0; + + if( carg[ csr_argj ] == c ) + { + csr_argj ++; + + return 1; + } + } + + return 0; +} + +// Example: get -c *value* +char *csr_opt_arg( char c ) +{ + if( csr_opt( c ) ) + { + if( csr_argi < csr_argc-1 ) + { + if( csr_argv[ csr_argi + 1 ][0] != '-' ) + { + csr_consume_next = 1; + return csr_argv[ csr_argi + 1 ]; + } + } + + fprintf( stderr, "Option '%c' requires argument!\n", c ); + exit(0); + } + + return NULL; +} + +// Example see if --big is set +int csr_long_opt( char *name ) +{ + char *carg = csr_argv[ csr_argi ]; + + if( carg[0] == '-' ) + { + if( carg[1] == '-' ) + { + if( !strcmp( name, carg+2 ) ) + { + csr_consume_next = 1; + return 1; + } + } + } + + return 0; +} + +// Example: get --big=value +char *csr_long_opt_arg( char *name ) +{ + char *carg = csr_argv[ csr_argi ]; + + if( carg[0] == '-' ) + { + if( carg[1] == '-' ) + { + int k = 2; int set = 0; + while( carg[ k ] ) + { + if( carg[ k ] == '=' ) + { + set = 1; + break; + } + + k ++; + } + + if( !strncmp( name, carg+2, k-2 ) ) + { + csr_consume_next = 1; + + // the rest + if( set ) + { + return carg + k + 1; + } + else + { + fprintf( stderr, "Long option '%s' requires argument\n", name ); + } + } + } + } + + return NULL; +} + +char *csr_arg(void) +{ + char *carg = csr_argv[ csr_argi ]; + + if( carg[0] != '-' ) + { + csr_argi ++; + return carg; + } + + return NULL; +} diff --git a/makeradar.sh b/makeradar.sh new file mode 100755 index 0000000..df2e713 --- /dev/null +++ b/makeradar.sh @@ -0,0 +1 @@ +./csRadar testmap.vmf -g "/home/harry/SteamLibrary/steamapps/common/Counter-Strike Global Offensive/csgo/gameinfo.txt" diff --git a/vfilesys.h b/vfilesys.h index 5206d57..8441c83 100644 --- a/vfilesys.h +++ b/vfilesys.h @@ -103,8 +103,11 @@ void fs_exit(void) csr_sb_free( fs->searchpaths ); fs->searchpaths = NULL; - vpk_free( fs->vpk ); - fs->vpk = NULL; + if( fs->vpk ) + { + vpk_free( fs->vpk ); + fs->vpk = NULL; + } if( fs->current_archive ) { @@ -120,61 +123,62 @@ char *valve_fs_get( const char *path ) VPKDirectoryEntry *entry; char pak[ 533 ]; - if( (entry = vpk_find( fs->vpk, path )) ) + if( fs->vpk ) { - if( entry->ArchiveIndex != fs->current_idx ) + if( (entry = vpk_find( fs->vpk, path )) ) { - if( fs->current_archive ) + if( entry->ArchiveIndex != fs->current_idx ) { - fclose( fs->current_archive ); - fs->current_archive = NULL; + if( fs->current_archive ) + { + fclose( fs->current_archive ); + fs->current_archive = NULL; + } + + fs->current_idx = entry->ArchiveIndex; } - fs->current_idx = entry->ArchiveIndex; - } - - if( !fs->current_archive ) - { - sprintf( pak, "%scsgo/pak01_%03hu.vpk", fs->exedir, fs->current_idx ); - fs->current_archive = fopen( pak, "rb" ); - if( !fs->current_archive ) { - fprintf( stderr, "Could not locate %s\n", pak ); + sprintf( pak, "%scsgo/pak01_%03hu.vpk", fs->exedir, fs->current_idx ); + fs->current_archive = fopen( pak, "rb" ); + + if( !fs->current_archive ) + { + fprintf( stderr, "Could not locate %s\n", pak ); + return NULL; + } + } + + char *filebuf = csr_malloc( entry->EntryLength ); + + fseek( fs->current_archive, entry->EntryOffset, SEEK_SET ); + if( fread( filebuf, 1, entry->EntryLength, fs->current_archive ) == entry->EntryLength ) + { + return filebuf; + } + else + { + free( filebuf ); return NULL; } } - - char *filebuf = csr_malloc( entry->EntryLength ); - - fseek( fs->current_archive, entry->EntryOffset, SEEK_SET ); - if( fread( filebuf, 1, entry->EntryLength, fs->current_archive ) == entry->EntryLength ) - { - return filebuf; - } - else - { - free( filebuf ); - return NULL; - } } - else + + // Use physical searchpaths + char path_buf[ 512 ]; + + for( int i = 0; i < csr_sb_count( fs->searchpaths ); i ++ ) { - // Use physical searchpaths - char path_buf[ 512 ]; + strcpy( path_buf, fs->searchpaths[ i ] ); + strcat( path_buf, path ); - for( int i = 0; i < csr_sb_count( fs->searchpaths ); i ++ ) + char *filebuf; + if( (filebuf = csr_asset_read( path_buf )) ) { - strcpy( path_buf, fs->searchpaths[ i ] ); - strcat( path_buf, path ); - - char *filebuf; - if( (filebuf = csr_asset_read( path_buf )) ) - { - return filebuf; - } + return filebuf; } - - return NULL; } + + return NULL; } diff --git a/vmf.h b/vmf.h index d2bbbd2..ec9573a 100644 --- a/vmf.h +++ b/vmf.h @@ -725,6 +725,12 @@ vmf_map *vmf_init( const char *path, int load_models ) vmf_map *map = csr_calloc( sizeof( vmf_map ) ); map->root = vdf_open_file( path ); + if( !map->root ) + { + free( map ); + return NULL; + } + // Prepare instances vmf_load_all_instances( map, map->root ); -- 2.25.1