add automatic MemBuffer storage for file loading routines

This commit is contained in:
Garux 2021-10-17 23:10:02 +03:00
parent 6a7550a6ba
commit f5b2653b0d
21 changed files with 236 additions and 366 deletions

View File

@ -174,12 +174,9 @@ void Q_mkdir( const char *path ){
================ ================
*/ */
int Q_filelength( FILE *f ){ int Q_filelength( FILE *f ){
int pos; const int pos = ftell( f );
int end;
pos = ftell( f );
fseek( f, 0, SEEK_END ); fseek( f, 0, SEEK_END );
end = ftell( f ); const int end = ftell( f );
fseek( f, pos, SEEK_SET ); fseek( f, pos, SEEK_SET );
return end; return end;
@ -207,8 +204,8 @@ FILE *SafeOpenRead( const char *filename, const char *mode ){
} }
void SafeRead( FILE *f, void *buffer, int count ){ void SafeRead( FILE *f, MemBuffer& buffer ){
if ( fread( buffer, 1, count, f ) != (size_t)count ) { if ( fread( buffer.data(), 1, buffer.size(), f ) != buffer.size() ) {
Error( "File read failure" ); Error( "File read failure" );
} }
} }
@ -227,14 +224,7 @@ void SafeWrite( FILE *f, const void *buffer, int count ){
============== ==============
*/ */
bool FileExists( const char *filename ){ bool FileExists( const char *filename ){
FILE *f; return access( filename, R_OK ) == 0;
f = fopen( filename, "r" );
if ( !f ) {
return false;
}
fclose( f );
return true;
} }
/* /*
@ -242,49 +232,13 @@ bool FileExists( const char *filename ){
LoadFile LoadFile
============== ==============
*/ */
int LoadFile( const char *filename, void **bufferptr ){ MemBuffer LoadFile( const char *filename ){
FILE *f; FILE *f = SafeOpenRead( filename );
int length; MemBuffer buffer( Q_filelength( f ) );
void *buffer; SafeRead( f, buffer );
f = SafeOpenRead( filename );
length = Q_filelength( f );
buffer = safe_malloc( length + 1 );
( (char *)buffer )[length] = 0;
SafeRead( f, buffer, length );
fclose( f ); fclose( f );
*bufferptr = buffer; return buffer;
return length;
}
/*
==============
TryLoadFile
Allows failure
==============
*/
int TryLoadFile( const char *filename, void **bufferptr ){
FILE *f;
int length;
void *buffer;
*bufferptr = NULL;
f = fopen( filename, "rb" );
if ( !f ) {
return -1;
}
length = Q_filelength( f );
buffer = safe_malloc( length + 1 );
( (char *)buffer )[length] = 0;
SafeRead( f, buffer, length );
fclose( f );
*bufferptr = buffer;
return length;
} }
@ -294,9 +248,7 @@ int TryLoadFile( const char *filename, void **bufferptr ){
============== ==============
*/ */
void SaveFile( const char *filename, const void *buffer, int count ){ void SaveFile( const char *filename, const void *buffer, int count ){
FILE *f; FILE *f = SafeOpenWrite( filename );
f = SafeOpenWrite( filename );
SafeWrite( f, buffer, count ); SafeWrite( f, buffer, count );
fclose( f ); fclose( f );
} }

View File

@ -27,6 +27,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <utility>
class void_ptr class void_ptr
@ -40,6 +41,42 @@ public:
} }
}; };
class MemBuffer
{
byte *m_data;
size_t m_size;
public:
MemBuffer() : m_data( nullptr ), m_size( 0 ) {}
explicit MemBuffer( size_t size ) : m_data( new byte[ size + 1 ] ), m_size( size ) {
m_data[m_size] = '\0'; // NOTE: when loading a file, you have to allocate one extra byte and set it to \0
}
MemBuffer( MemBuffer&& other ) noexcept : m_data( std::exchange( other.m_data, nullptr ) ), m_size( other.m_size ) {}
MemBuffer& operator=( MemBuffer&& other ) noexcept {
std::swap( m_data, other.m_data );
std::swap( m_size, other.m_size );
return *this;
}
~MemBuffer(){
delete[] m_data;
}
void_ptr data() const {
return m_data;
}
/// \return correct buffer size in bytes, if it's not empty. May be not used for validity check!
size_t size() const {
return m_size;
}
/// \return true, if there is managed buffer
operator bool() const {
return m_data != nullptr;
}
/// \brief Delegates the ownership. Obtained buffer must be deallocated by \c delete[]
void_ptr release(){
return std::exchange( m_data, nullptr );
}
};
void_ptr safe_malloc( size_t size ); void_ptr safe_malloc( size_t size );
void_ptr safe_calloc( size_t size ); void_ptr safe_calloc( size_t size );
@ -58,11 +95,11 @@ double I_FloatTime( void );
FILE *SafeOpenWrite( const char *filename, const char *mode = "wb" ); FILE *SafeOpenWrite( const char *filename, const char *mode = "wb" );
FILE *SafeOpenRead( const char *filename, const char *mode = "rb" ); FILE *SafeOpenRead( const char *filename, const char *mode = "rb" );
void SafeRead( FILE *f, void *buffer, int count ); void SafeRead( FILE *f, MemBuffer& buffer );
void SafeWrite( FILE *f, const void *buffer, int count ); void SafeWrite( FILE *f, const void *buffer, int count );
int LoadFile( const char *filename, void **bufferptr ); /// \brief loads file from absolute \p filename path or emits \c Error
int TryLoadFile( const char *filename, void **bufferptr ); MemBuffer LoadFile( const char *filename );
void SaveFile( const char *filename, const void *buffer, int count ); void SaveFile( const char *filename, const void *buffer, int count );
bool FileExists( const char *filename ); bool FileExists( const char *filename );

View File

@ -194,7 +194,7 @@ byte *LBMRLEDecompress( byte *source,byte *unpacked, int bpwidth ){
================= =================
*/ */
void LoadLBM( const char *filename, byte **picture, byte **palette ){ void LoadLBM( const char *filename, byte **picture, byte **palette ){
byte *LBMbuffer, *picbuffer, *cmapbuffer; byte *picbuffer, *cmapbuffer;
int y; int y;
byte *LBM_P, *LBMEND_P; byte *LBM_P, *LBMEND_P;
byte *pic_p; byte *pic_p;
@ -210,13 +210,13 @@ void LoadLBM( const char *filename, byte **picture, byte **palette ){
// //
// load the LBM // load the LBM
// //
LoadFile( filename, (void **)&LBMbuffer ); MemBuffer file = LoadFile( filename );
// //
// parse the LBM header // parse the LBM header
// //
LBM_P = LBMbuffer; LBM_P = file.data();
if ( *(int *)LBMbuffer != LittleLong( FORMID ) ) { if ( *(int *)LBM_P != LittleLong( FORMID ) ) {
Error( "No FORM ID at start of file!\n" ); Error( "No FORM ID at start of file!\n" );
} }
@ -296,8 +296,6 @@ void LoadLBM( const char *filename, byte **picture, byte **palette ){
LBM_P += Align( chunklength ); LBM_P += Align( chunklength );
} }
free( LBMbuffer );
*picture = picbuffer; *picture = picbuffer;
if ( palette ) { if ( palette ) {
@ -469,20 +467,19 @@ void LoadPCX( const char *filename, byte **pic, byte **palette, int *width, int
byte *raw; byte *raw;
pcx_t *pcx; pcx_t *pcx;
int x, y, lsize; int x, y, lsize;
int len;
int dataByte, runLength; int dataByte, runLength;
byte *out, *pix; byte *out, *pix;
/* load the file */ /* load the file */
len = vfsLoadFile( filename, (void **)&raw, 0 ); MemBuffer buffer = vfsLoadFile( filename );
if ( len == -1 ) { if ( !buffer ) {
Error( "LoadPCX: Couldn't read %s", filename ); Error( "LoadPCX: Couldn't read %s", filename );
} }
/* parse the PCX file */ /* parse the PCX file */
pcx = (pcx_t *)raw; pcx = buffer.data();
raw = &pcx->data; raw = &pcx->data;
pcx->xmin = LittleShort( pcx->xmin ); pcx->xmin = LittleShort( pcx->xmin );
@ -505,7 +502,7 @@ void LoadPCX( const char *filename, byte **pic, byte **palette, int *width, int
if ( palette ) { if ( palette ) {
*palette = safe_malloc( 768 ); *palette = safe_malloc( 768 );
memcpy( *palette, (byte *)pcx + len - 768, 768 ); memcpy( *palette, (byte *)pcx + buffer.size() - 768, 768 );
} }
if ( width ) { if ( width ) {
@ -551,10 +548,9 @@ void LoadPCX( const char *filename, byte **pic, byte **palette, int *width, int
} }
/* validity check */ /* validity check */
if ( raw - (byte *) pcx > len ) { if ( raw - (byte *) pcx > int( buffer.size() ) ) {
Error( "PCX file %s was malformed", filename ); Error( "PCX file %s was malformed", filename );
} }
free( pcx );
} }
@ -693,10 +689,12 @@ void LoadBMP( const char *filename, byte **pic, byte **palette, int *width, int
byte *in; byte *in;
int len, pos = 0; int len, pos = 0;
len = vfsLoadFile( filename, (void **)&in, 0 ); MemBuffer buffer = vfsLoadFile( filename );
if ( len == -1 ) { if ( !buffer ) {
Error( "Couldn't read %s", filename ); Error( "Couldn't read %s", filename );
} }
in = buffer.data();
len = buffer.size();
i = bufLittleShort( in, len, &pos ); i = bufLittleShort( in, len, &pos );
if ( i != 'B' + ( 'M' << 8 ) ) { if ( i != 'B' + ( 'M' << 8 ) ) {
@ -780,7 +778,6 @@ void LoadBMP( const char *filename, byte **pic, byte **palette, int *width, int
} }
if ( !pic ) { if ( !pic ) {
free( in );
return; return;
} }
@ -798,8 +795,6 @@ void LoadBMP( const char *filename, byte **pic, byte **palette, int *width, int
memcpy( out, in + pos, bcWidth * bcHeight ); memcpy( out, in + pos, bcWidth * bcHeight );
pos += bcWidth * bcHeight; pos += bcWidth * bcHeight;
} }
free( in );
} }
@ -890,18 +885,19 @@ void TargaError( TargaHeader *t, const char *message ){
LoadTGABuffer LoadTGABuffer
============= =============
*/ */
void LoadTGABuffer( const byte *f, const byte *enddata, byte **pic, int *width, int *height ){ void LoadTGABuffer( const byte *f, const size_t dataSize, byte **pic, int *width, int *height ){
int x, y, row_inc, compressed, readpixelcount, red, green, blue, alpha, runlen, pindex, alphabits, image_width, image_height; int x, y, row_inc, compressed, readpixelcount, red, green, blue, alpha, runlen, pindex, alphabits, image_width, image_height;
byte *pixbuf, *image_rgba; byte *pixbuf, *image_rgba;
const byte *fin; const byte *fin;
unsigned char *p; unsigned char *p;
TargaHeader targa_header; TargaHeader targa_header;
unsigned char palette[256 * 4]; unsigned char palette[256 * 4];
const byte * const enddata = f + dataSize;
*pic = NULL; *pic = NULL;
// abort if it is too small to parse // abort if it is too small to parse
if ( enddata - f < 19 ) { if ( dataSize < 19 ) {
return; return;
} }
@ -1107,18 +1103,10 @@ void LoadTGABuffer( const byte *f, const byte *enddata, byte **pic, int *width,
============= =============
*/ */
void LoadTGA( const char *name, byte **pixels, int *width, int *height ){ void LoadTGA( const char *name, byte **pixels, int *width, int *height ){
byte *buffer; if ( MemBuffer buffer = vfsLoadFile( name ) )
int nLen; LoadTGABuffer( buffer.data(), buffer.size(), pixels, width, height );
// else
// load the file
//
nLen = vfsLoadFile( name, (void **)&buffer, 0 );
if ( nLen == -1 ) {
Error( "Couldn't read %s", name ); Error( "Couldn't read %s", name );
}
LoadTGABuffer( buffer, buffer + nLen, pixels, width, height );
} }

View File

@ -37,7 +37,7 @@ void Save256Image( const char *name, byte *pixels, byte *palette,
void LoadTGA( const char *filename, byte **pixels, int *width, int *height ); void LoadTGA( const char *filename, byte **pixels, int *width, int *height );
void LoadTGABuffer( const byte *buffer, const byte* enddata, byte **pic, int *width, int *height ); void LoadTGABuffer( const byte *buffer, const size_t dataSize, byte **pic, int *width, int *height );
void WriteTGA( const char *filename, const byte *data, int width, int height ); void WriteTGA( const char *filename, const byte *data, int width, int height );
void WriteTGAGray( const char *filename, byte *data, int width, int height ); void WriteTGAGray( const char *filename, byte *data, int width, int height );
int LoadJPGBuff( void *src_buffer, int src_size, unsigned char **pic, int *width, int *height ); int LoadJPGBuff( void *src_buffer, int src_size, unsigned char **pic, int *width, int *height );

View File

@ -28,6 +28,7 @@
#include "scriplib.h" #include "scriplib.h"
#include "vfs.h" #include "vfs.h"
#include <list> #include <list>
#include <errno.h>
/* /*
============================================================================= =============================================================================
@ -39,22 +40,18 @@
struct script_t struct script_t
{ {
CopiedString filename; const CopiedString filename;
char *buffer; const MemBuffer buffer;
const char *it, *end; const char *it, *end;
int line; int line;
/* buffer is optional: != nullptr == own it */ script_t( const char *filename, MemBuffer&& buffer_ ) :
script_t( const char *filename, char *buffer, char *start, int size ) :
filename( filename ), filename( filename ),
buffer( buffer ), buffer( std::move( buffer_ ) ),
it( start ), it( buffer.data() ),
end( start + size ), end( it + buffer_.size() ),
line( 1 ) line( 1 )
{} {}
script_t( script_t&& ) noexcept = delete; script_t( script_t&& ) noexcept = delete;
~script_t(){
free( buffer );
}
}; };
std::list<script_t> scriptstack; std::list<script_t> scriptstack;
@ -68,15 +65,8 @@ bool tokenready; // only true if UnGetToken was just called
AddScriptToStack AddScriptToStack
============== ==============
*/ */
static void AddScriptToStack( const char *filename, int index, bool verbose ){ static bool AddScriptToStack( const char *filename, int index, bool verbose ){
void* buffer; if ( MemBuffer buffer = vfsLoadFile( filename, index ) ) {
const int size = vfsLoadFile( filename, &buffer, index );
if ( size == -1 ) {
Sys_FPrintf( SYS_WRN, "Script file %s was not found\n", filename );
}
else
{
if( verbose ){ if( verbose ){
if ( index > 0 ) if ( index > 0 )
Sys_Printf( "entering %s (%d)\n", filename, index + 1 ); Sys_Printf( "entering %s (%d)\n", filename, index + 1 );
@ -84,7 +74,17 @@ static void AddScriptToStack( const char *filename, int index, bool verbose ){
Sys_Printf( "entering %s\n", filename ); Sys_Printf( "entering %s\n", filename );
} }
scriptstack.emplace_back( filename, void_ptr( buffer ), void_ptr( buffer ), size ); scriptstack.emplace_back( filename, std::move( buffer ) );
return true;
}
else
{
if( index >= 0 )
Sys_FPrintf( SYS_WRN, "Script file %s was not found\n", filename );
else
Sys_FPrintf( SYS_WRN, "Script file %s was not found: %s\n", filename, strerror( errno ) );
return false;
} }
} }
@ -94,10 +94,10 @@ static void AddScriptToStack( const char *filename, int index, bool verbose ){
LoadScriptFile LoadScriptFile
============== ==============
*/ */
void LoadScriptFile( const char *filename, int index, bool verbose /* = true */ ){ bool LoadScriptFile( const char *filename, int index /* = 0 */, bool verbose /* = true */ ){
scriptstack.clear(); scriptstack.clear();
AddScriptToStack( filename, index, verbose );
tokenready = false; tokenready = false;
return AddScriptToStack( filename, index, verbose );
} }
/* /*
@ -105,10 +105,12 @@ void LoadScriptFile( const char *filename, int index, bool verbose /* = true */
ParseFromMemory ParseFromMemory
============== ==============
*/ */
void ParseFromMemory( char *buffer, int size ){ void ParseFromMemory( char *buffer, size_t size ){
scriptstack.clear(); scriptstack.clear();
scriptstack.emplace_back( "memory buffer", nullptr, buffer, size );
tokenready = false; tokenready = false;
MemBuffer bu( size );
memcpy( bu.data(), buffer, size );
scriptstack.emplace_back( "memory buffer", std::move( bu ) );
} }

View File

@ -28,9 +28,11 @@
extern char token[MAXTOKEN]; extern char token[MAXTOKEN];
extern int scriptline; extern int scriptline;
/// \param[in] index -1: \p filename is absolute path
void LoadScriptFile( const char *filename, int index, bool verbose = true ); /// \param[in] index >= 0: \p filename is relative path in VSF, Nth occurrence of file
void ParseFromMemory( char *buffer, int size ); /// \return true on success
bool LoadScriptFile( const char *filename, int index = 0, bool verbose = true );
void ParseFromMemory( char *buffer, size_t size );
/// \param[in] crossline true: write next token to \c token or return false on EOF /// \param[in] crossline true: write next token to \c token or return false on EOF
/// \param[in] crossline false: find next token on the current line or emit \c Error /// \param[in] crossline false: find next token on the current line or emit \c Error

View File

@ -255,7 +255,7 @@ int vfsGetFileCount( const char *filename ){
for ( const auto& dir : g_strDirs ) for ( const auto& dir : g_strDirs )
{ {
if ( access( StringOutputStream( 256 )( dir, filename ), R_OK ) == 0 ) { if ( FileExists( StringOutputStream( 256 )( dir, filename ) ) ) {
++count; ++count;
} }
} }
@ -264,33 +264,27 @@ int vfsGetFileCount( const char *filename ){
} }
// NOTE: when loading a file, you have to allocate one extra byte and set it to \0 // NOTE: when loading a file, you have to allocate one extra byte and set it to \0
int vfsLoadFile( const char *filename, void **bufferptr, int index ){ MemBuffer vfsLoadFile( const char *filename, int index /* = 0 */ ){
const auto load_full_path = [bufferptr] ( const char *filename ) -> int const auto load_full_path = [] ( const char *filename ) -> MemBuffer
{ {
strcpy( g_strLoadedFileLocation, filename ); strcpy( g_strLoadedFileLocation, filename );
FILE *f = fopen( filename, "rb" ); MemBuffer buffer;
if ( f == NULL ) {
return -1;
}
FILE *f = fopen( filename, "rb" );
if ( f != NULL ) {
fseek( f, 0, SEEK_END ); fseek( f, 0, SEEK_END );
const long len = ftell( f ); buffer = MemBuffer( ftell( f ) );
rewind( f ); rewind( f );
*bufferptr = safe_malloc( len + 1 ); if ( fread( buffer.data(), 1, buffer.size(), f ) != buffer.size() ) {
buffer = MemBuffer();
if ( fread( *bufferptr, 1, len, f ) != (size_t) len ) {
fclose( f );
return -1;
} }
fclose( f ); fclose( f );
}
// we need to end the buffer with a 0 return buffer;
( (char*) ( *bufferptr ) )[len] = 0;
return len;
}; };
// filename is a full path // filename is a full path
@ -301,7 +295,7 @@ int vfsLoadFile( const char *filename, void **bufferptr, int index ){
for ( const auto& dir : g_strDirs ) for ( const auto& dir : g_strDirs )
{ {
const auto fullpath = StringOutputStream( 256 )( dir, filename ); const auto fullpath = StringOutputStream( 256 )( dir, filename );
if ( access( fullpath, R_OK ) == 0 && 0 == index-- ) { if ( FileExists( fullpath ) && 0 == index-- ) {
return load_full_path( fullpath ); return load_full_path( fullpath );
} }
} }
@ -309,6 +303,8 @@ int vfsLoadFile( const char *filename, void **bufferptr, int index ){
auto fixed = StringOutputStream( 64 )( PathCleaned( filename ) ); auto fixed = StringOutputStream( 64 )( PathCleaned( filename ) );
strLower( fixed.c_str() ); strLower( fixed.c_str() );
MemBuffer buffer;
for ( const VFS_PAKFILE& file : g_pakFiles ) for ( const VFS_PAKFILE& file : g_pakFiles )
{ {
if ( strEqual( file.name.c_str(), fixed ) && 0 == index-- ) if ( strEqual( file.name.c_str(), fixed ) && 0 == index-- )
@ -318,26 +314,19 @@ int vfsLoadFile( const char *filename, void **bufferptr, int index ){
unzFile zipfile = file.pak.zipfile; unzFile zipfile = file.pak.zipfile;
*(unz_s*)zipfile = file.zipinfo; *(unz_s*)zipfile = file.zipinfo;
if ( unzOpenCurrentFile( zipfile ) != UNZ_OK ) { if ( unzOpenCurrentFile( zipfile ) == UNZ_OK ) {
return -1; buffer = MemBuffer( file.size );
if ( unzReadCurrentFile( zipfile, buffer.data(), file.size ) < 0 ) {
buffer = MemBuffer();
} }
*bufferptr = safe_malloc( file.size + 1 );
// we need to end the buffer with a 0
( (char*) ( *bufferptr ) )[file.size] = 0;
const int size = unzReadCurrentFile( zipfile, *bufferptr, file.size );
unzCloseCurrentFile( zipfile ); unzCloseCurrentFile( zipfile );
if ( size < 0 ) {
return -1;
}
else{
return file.size;
} }
return buffer;
} }
} }
return -1; return buffer;
} }
@ -360,35 +349,28 @@ bool vfsPackFile( const char *filename, const char *packname, const int compLeve
unzFile zipfile = file.pak.zipfile; unzFile zipfile = file.pak.zipfile;
*(unz_s*)zipfile = file.zipinfo; *(unz_s*)zipfile = file.zipinfo;
if ( unzOpenCurrentFile( zipfile ) != UNZ_OK ) { if ( unzOpenCurrentFile( zipfile ) == UNZ_OK ) {
return false; MemBuffer buffer( file.size );
}
byte *bufferptr = safe_malloc( file.size + 1 ); const int size = unzReadCurrentFile( zipfile, buffer.data(), file.size );
// we need to end the buffer with a 0
bufferptr[file.size] = 0;
const int size = unzReadCurrentFile( zipfile, bufferptr, file.size );
unzCloseCurrentFile( zipfile ); unzCloseCurrentFile( zipfile );
if ( size < 0 ) { if ( size >= 0 ) {
return false; if ( !mz_zip_add_mem_to_archive_file_in_place_with_time( packname, filename, buffer.data(), size, 0, 0, compLevel, file.zipinfo.cur_file_info.dosDate ) ){
}
else{
if ( !mz_zip_add_mem_to_archive_file_in_place_with_time( packname, filename, bufferptr, size, 0, 0, compLevel, file.zipinfo.cur_file_info.dosDate ) ){
Error( "Failed creating zip archive \"%s\"!\n", packname ); Error( "Failed creating zip archive \"%s\"!\n", packname );
} }
free( bufferptr );
return true; return true;
} }
} }
return false;
}
} }
return false; return false;
} }
bool vfsPackFile_Absolute_Path( const char *filepath, const char *filename, const char *packname, const int compLevel ){ bool vfsPackFile_Absolute_Path( const char *filepath, const char *filename, const char *packname, const int compLevel ){
if ( access( filepath, R_OK ) == 0 ) { if ( FileExists( filepath ) ) {
if ( access( packname, R_OK ) == 0 ) { if ( FileExists( packname ) ) {
mz_zip_archive zip; mz_zip_archive zip;
memset( &zip, 0, sizeof(zip) ); memset( &zip, 0, sizeof(zip) );

View File

@ -33,7 +33,11 @@
void vfsInitDirectory( const char *path ); void vfsInitDirectory( const char *path );
void vfsShutdown(); void vfsShutdown();
int vfsGetFileCount( const char *filename ); int vfsGetFileCount( const char *filename );
int vfsLoadFile( const char *filename, void **buffer, int index );
/// \param[in] index -1: \p filename is absolute path
/// \param[in] index >= 0: \p filename is relative path in VSF, Nth occurrence of file
/// \return non-empty \c MemBuffer on success
MemBuffer vfsLoadFile( const char *filename, int index = 0 );
std::vector<CopiedString> vfsListShaderFiles( const char *shaderPath ); std::vector<CopiedString> vfsListShaderFiles( const char *shaderPath );
bool vfsPackFile( const char *filename, const char *packname, const int compLevel ); bool vfsPackFile( const char *filename, const char *packname, const int compLevel );
bool vfsPackFile_Absolute_Path( const char *filepath, const char *filename, const char *packname, const int compLevel ); bool vfsPackFile_Absolute_Path( const char *filepath, const char *filename, const char *packname, const int compLevel );

View File

@ -225,18 +225,8 @@ static inline void parseEXblock( StrList& list, const char *exName ){
} }
static const Exclusions parseEXfile( const char* filename ){ static const Exclusions parseEXfile( const char* filename ){
Sys_Printf( "Loading %s\n", filename );
Exclusions ex; Exclusions ex;
byte *buffer; if ( LoadScriptFile( filename, -1 ) ) {
const int size = TryLoadFile( filename, (void**) &buffer );
if ( size < 0 ) {
Sys_Warning( "Unable to load exclusions file %s.\n", filename );
}
else{
/* parse the file */
ParseFromMemory( (char *) buffer, size );
/* tokenize it */ /* tokenize it */
while ( GetToken( true ) ) /* test for end of file */ while ( GetToken( true ) ) /* test for end of file */
{ {
@ -261,15 +251,15 @@ static const Exclusions parseEXfile( const char* filename ){
} }
} }
/* free the buffer */
free( buffer );
/* prepare ex.pureTextures */ /* prepare ex.pureTextures */
for ( const auto& s : ex.textures ){ for ( const auto& s : ex.textures ){
if( !StrList_find( ex.shaders, s ) ) if( !StrList_find( ex.shaders, s ) )
ex.pureTextures.emplace_back( s ); ex.pureTextures.emplace_back( s );
} }
} }
else{
Sys_Warning( "Unable to load exclusions file %s.\n", filename );
}
return ex; return ex;
} }
@ -731,24 +721,15 @@ int repackBSPMain( Args& args ){
/* load bsps paths list */ /* load bsps paths list */
/* do some path mangling */ /* do some path mangling */
strcpy( source, ExpandArg( fileName ) ); strcpy( source, ExpandArg( fileName ) );
Sys_Printf( "Loading %s\n", source ); if ( !LoadScriptFile( source, -1 ) ) {
byte *buffer;
const int size = TryLoadFile( source, (void**) &buffer );
if ( size <= 0 ) {
Error( "Unable to open bsps paths list file %s.\n", source ); Error( "Unable to open bsps paths list file %s.\n", source );
} }
/* parse the file */
ParseFromMemory( (char *) buffer, size );
/* tokenize it */ /* tokenize it */
while ( GetToken( true ) ) while ( GetToken( true ) )
{ {
bspList.emplace_back( stream( PathExtensionless( token ), ".bsp" ) ); bspList.emplace_back( stream( PathExtensionless( token ), ".bsp" ) );
} }
/* free the buffer */
free( buffer );
} }
/* parse bsps */ /* parse bsps */
@ -961,9 +942,7 @@ int repackBSPMain( Args& args ){
/* write shader */ /* write shader */
stream( g_enginePath, nameOFpack, "_strippedBYrepacker.shader" ); stream( g_enginePath, nameOFpack, "_strippedBYrepacker.shader" );
FILE *f = fopen( stream, "wb" ); SaveFile( stream, allShaders.c_str(), allShaders.end() - allShaders.begin() );
fwrite( allShaders.c_str(), sizeof( char ), allShaders.end() - allShaders.begin(), f );
fclose( f );
Sys_Printf( "Shaders saved to %s\n", stream.c_str() ); Sys_Printf( "Shaders saved to %s\n", stream.c_str() );
/* make a pack */ /* make a pack */

View File

@ -213,11 +213,10 @@ struct ibspGridPoint_t
*/ */
void LoadIBSPFile( const char *filename ){ void LoadIBSPFile( const char *filename ){
ibspHeader_t *header; /* load the file */
MemBuffer file = LoadFile( filename );
ibspHeader_t *header = file.data();
/* load the file header */
LoadFile( filename, (void**) &header );
/* swap the header (except the first 4 bytes) */ /* swap the header (except the first 4 bytes) */
SwapBlock( (int*) ( (byte*) header + 4 ), sizeof( *header ) - 4 ); SwapBlock( (int*) ( (byte*) header + 4 ), sizeof( *header ) - 4 );
@ -256,9 +255,6 @@ void LoadIBSPFile( const char *filename ){
else{ else{
bspAds.clear(); bspAds.clear();
} }
/* free the file buffer */
free( header );
} }
/* /*
@ -267,11 +263,10 @@ void LoadIBSPFile( const char *filename ){
*/ */
void LoadIBSPorRBSPFilePartially( const char *filename ){ void LoadIBSPorRBSPFilePartially( const char *filename ){
ibspHeader_t *header; /* load the file */
MemBuffer file = LoadFile( filename );
ibspHeader_t *header = file.data();
/* load the file header */
LoadFile( filename, (void**) &header );
/* swap the header (except the first 4 bytes) */ /* swap the header (except the first 4 bytes) */
SwapBlock( (int*) ( (byte*) header + 4 ), sizeof( *header ) - 4 ); SwapBlock( (int*) ( (byte*) header + 4 ), sizeof( *header ) - 4 );
@ -293,9 +288,6 @@ void LoadIBSPorRBSPFilePartially( const char *filename ){
CopyLump( (bspHeader_t*) header, LUMP_FOGS, bspFogs ); CopyLump( (bspHeader_t*) header, LUMP_FOGS, bspFogs );
CopyLump( (bspHeader_t*) header, LUMP_ENTITIES, bspEntData ); CopyLump( (bspHeader_t*) header, LUMP_ENTITIES, bspEntData );
/* free the file buffer */
free( header );
} }
/* /*

View File

@ -179,11 +179,10 @@ static void AddLightGridLumps( FILE *file, rbspHeader_t& header ){
*/ */
void LoadRBSPFile( const char *filename ){ void LoadRBSPFile( const char *filename ){
rbspHeader_t *header; /* load the file */
MemBuffer file = LoadFile( filename );
rbspHeader_t *header = file.data();
/* load the file header */
LoadFile( filename, (void**) &header );
/* swap the header (except the first 4 bytes) */ /* swap the header (except the first 4 bytes) */
SwapBlock( (int*) ( (byte*) header + 4 ), sizeof( *header ) - 4 ); SwapBlock( (int*) ( (byte*) header + 4 ), sizeof( *header ) - 4 );
@ -214,9 +213,6 @@ void LoadRBSPFile( const char *filename ){
CopyLump( (bspHeader_t*) header, LUMP_LIGHTMAPS, bspLightBytes ); CopyLump( (bspHeader_t*) header, LUMP_LIGHTMAPS, bspLightBytes );
CopyLump( (bspHeader_t*) header, LUMP_ENTITIES, bspEntData ); CopyLump( (bspHeader_t*) header, LUMP_ENTITIES, bspEntData );
CopyLightGridLumps( header ); CopyLightGridLumps( header );
/* free the file buffer */
free( header );
} }

View File

@ -44,11 +44,6 @@ static void AAS_DData( unsigned char *data, int size ){
*/ */
int FixAAS( Args& args ){ int FixAAS( Args& args ){
int length, checksum;
void *buffer;
char aas[ 1024 ];
/* arg checking */ /* arg checking */
if ( args.empty() ) { if ( args.empty() ) {
Sys_Printf( "Usage: q3map2 -fixaas [-v] <mapname>\n" ); Sys_Printf( "Usage: q3map2 -fixaas [-v] <mapname>\n" );
@ -64,17 +59,18 @@ int FixAAS( Args& args ){
/* load the bsp */ /* load the bsp */
Sys_Printf( "Loading %s\n", source ); Sys_Printf( "Loading %s\n", source );
length = LoadFile( source, &buffer ); MemBuffer buffer = LoadFile( source );
/* create bsp checksum */ /* create bsp checksum */
Sys_Printf( "Creating checksum...\n" ); Sys_Printf( "Creating checksum...\n" );
checksum = LittleLong( (int)Com_BlockChecksum( buffer, length ) ); // md4 checksum for a block of data int checksum = LittleLong( (int)Com_BlockChecksum( buffer.data(), buffer.size() ) ); // md4 checksum for a block of data
AAS_DData( (unsigned char *) &checksum, 4 ); AAS_DData( (unsigned char *) &checksum, 4 );
/* write checksum to aas */ /* write checksum to aas */
for( auto&& ext : { ".aas", "_b0.aas", "_b1.aas" } ) for( auto&& ext : { ".aas", "_b0.aas", "_b1.aas" } )
{ {
/* mangle name */ /* mangle name */
char aas[ 1024 ];
strcpy( aas, source ); strcpy( aas, source );
path_set_extension( aas, ext ); path_set_extension( aas, ext );
Sys_Printf( "Trying %s\n", aas ); Sys_Printf( "Trying %s\n", aas );
@ -117,7 +113,7 @@ struct abspLumpTest_t
int AnalyzeBSP( Args& args ){ int AnalyzeBSP( Args& args ){
abspHeader_t *header; abspHeader_t *header;
int size, i, version, offset, length, lumpInt, count; int i, version, offset, length, lumpInt, count;
char ident[ 5 ]; char ident[ 5 ];
void *lump; void *lump;
float lumpFloat; float lumpFloat;
@ -160,11 +156,8 @@ int AnalyzeBSP( Args& args ){
Sys_Printf( "Loading %s\n", source ); Sys_Printf( "Loading %s\n", source );
/* load the file */ /* load the file */
size = LoadFile( source, (void**) &header ); MemBuffer file = LoadFile( source );
if ( size == 0 || header == NULL ) { header = file.data();
Sys_Printf( "Unable to load %s.\n", source );
return -1;
}
/* analyze ident/version */ /* analyze ident/version */
memcpy( ident, header->ident, 4 ); memcpy( ident, header->ident, 4 );
@ -238,14 +231,14 @@ int AnalyzeBSP( Args& args ){
Sys_Printf( "---------------------------------------\n" ); Sys_Printf( "---------------------------------------\n" );
/* end of file */ /* end of file */
if ( offset + length >= size ) { if ( offset + length >= int( file.size() ) ) {
break; break;
} }
} }
/* last stats */ /* last stats */
Sys_Printf( "Lump count: %d\n", i + 1 ); Sys_Printf( "Lump count: %d\n", i + 1 );
Sys_Printf( "File size: %d bytes\n", size ); Sys_Printf( "File size: %zu bytes\n", file.size() );
/* return to caller */ /* return to caller */
return 0; return 0;

View File

@ -410,11 +410,8 @@ static void write_json( const char *directory ){
inline rapidjson::Document load_json( const char *fileName ){ inline rapidjson::Document load_json( const char *fileName ){
Sys_Printf( "Loading %s\n", fileName ); Sys_Printf( "Loading %s\n", fileName );
void *buffer;
LoadFile( fileName, &buffer );
rapidjson::Document doc; rapidjson::Document doc;
doc.Parse( (const char*)buffer ); doc.Parse( (const char*)LoadFile( fileName ).data() );
free( buffer );
ENSURE( !doc.HasParseError() ); ENSURE( !doc.HasParseError() );
return doc; return doc;
} }

View File

@ -218,7 +218,7 @@ int Convert_CountLightmaps( const char* dirname ){
void Convert_ReferenceLightmaps( const char* base, std::vector<int>& lmIndices ){ void Convert_ReferenceLightmaps( const char* base, std::vector<int>& lmIndices ){
char shaderfile[256]; char shaderfile[256];
sprintf( shaderfile, "%s/q3map2_%s.shader", g_game->shaderPath, base ); sprintf( shaderfile, "%s/q3map2_%s.shader", g_game->shaderPath, base );
LoadScriptFile( shaderfile, 0 ); LoadScriptFile( shaderfile );
/* tokenize it */ /* tokenize it */
while ( GetToken( true ) ) /* test for end of file */ while ( GetToken( true ) ) /* test for end of file */
{ {

View File

@ -279,45 +279,30 @@ const image_t *ImageLoad( const char *name ){
byte *pixels = nullptr; byte *pixels = nullptr;
int width, height; int width, height;
char filename[ 1024 ]; char filename[ 1024 ];
int size; MemBuffer buffer;
byte *buffer = nullptr;
bool alphaHack = false; bool alphaHack = false;
/* attempt to load tga */ /* attempt to load various formats */
sprintf( filename, "%s.tga", name ); // StripExtension( name ); already if ( sprintf( filename, "%s.tga", name ); buffer = vfsLoadFile( filename ) ) // StripExtension( name ); already
size = vfsLoadFile( filename, (void**) &buffer, 0 );
if ( size > 0 ) {
LoadTGABuffer( buffer, buffer + size, &pixels, &width, &height );
}
else
{ {
/* attempt to load png */ LoadTGABuffer( buffer.data(), buffer.size(), &pixels, &width, &height );
path_set_extension( filename, ".png" );
size = vfsLoadFile( filename, (void**) &buffer, 0 );
if ( size > 0 ) {
LoadPNGBuffer( buffer, size, &pixels, &width, &height );
} }
else else if( path_set_extension( filename, ".png" ); buffer = vfsLoadFile( filename ) )
{ {
/* attempt to load jpg */ LoadPNGBuffer( buffer.data(), buffer.size(), &pixels, &width, &height );
path_set_extension( filename, ".jpg" ); }
size = vfsLoadFile( filename, (void**) &buffer, 0 ); else if( path_set_extension( filename, ".jpg" ); buffer = vfsLoadFile( filename ) )
if ( size > 0 ) { {
if ( LoadJPGBuff( buffer, size, &pixels, &width, &height ) == -1 && pixels != nullptr ) { if ( LoadJPGBuff( buffer.data(), buffer.size(), &pixels, &width, &height ) == -1 && pixels != nullptr ) {
// On error, LoadJPGBuff might store a pointer to the error message in pixels // On error, LoadJPGBuff might store a pointer to the error message in pixels
Sys_Warning( "LoadJPGBuff %s %s\n", filename, (unsigned char*) pixels ); Sys_Warning( "LoadJPGBuff %s %s\n", filename, (unsigned char*) pixels );
pixels = nullptr; pixels = nullptr;
} }
alphaHack = true; alphaHack = true;
} }
else else if( path_set_extension( filename, ".dds" ); buffer = vfsLoadFile( filename ) )
{ {
/* attempt to load dds */ LoadDDSBuffer( buffer.data(), buffer.size(), &pixels, &width, &height );
path_set_extension( filename, ".dds" );
size = vfsLoadFile( filename, (void**) &buffer, 0 );
if ( size > 0 ) {
LoadDDSBuffer( buffer, size, &pixels, &width, &height );
/* debug code */ /* debug code */
#if 0 #if 0
{ {
@ -331,26 +316,15 @@ const image_t *ImageLoad( const char *name ){
} }
#endif #endif
} }
else else if( path_set_extension( filename, ".ktx" ); buffer = vfsLoadFile( filename ) )
{ {
/* attempt to load ktx */ LoadKTXBufferFirstImage( buffer.data(), buffer.size(), &pixels, &width, &height );
path_set_extension( filename, ".ktx" );
size = vfsLoadFile( filename, (void**) &buffer, 0 );
if ( size > 0 ) {
LoadKTXBufferFirstImage( buffer, size, &pixels, &width, &height );
} }
}
}
}
}
/* free file buffer */
free( buffer );
/* make sure everything's kosher */ /* make sure everything's kosher */
if ( size <= 0 || width <= 0 || height <= 0 || pixels == nullptr ) { if ( !buffer || width <= 0 || height <= 0 || pixels == nullptr ) {
//% Sys_Printf( "size = %d width = %d height = %d pixels = 0x%08x (%s)\n", //% Sys_Printf( "size = %zu width = %d height = %d pixels = 0x%08x (%s)\n",
//% size, width, height, pixels, filename ); //% buffer.size(), width, height, pixels, filename );
return nullptr; return nullptr;
} }
@ -358,10 +332,8 @@ const image_t *ImageLoad( const char *name ){
image_t& image = *images.emplace_after( images.cbegin(), name, filename, width, height, pixels ); image_t& image = *images.emplace_after( images.cbegin(), name, filename, width, height, pixels );
if ( alphaHack ) { if ( alphaHack ) {
path_set_extension( filename, "_alpha.jpg" ); if ( path_set_extension( filename, "_alpha.jpg" ); buffer = vfsLoadFile( filename ) ) {
size = vfsLoadFile( (const char*) filename, (void**) &buffer, 0 ); if ( LoadJPGBuff( buffer.data(), buffer.size(), &pixels, &width, &height ) == -1 ) {
if ( size > 0 ) {
if ( LoadJPGBuff( buffer, size, &pixels, &width, &height ) == -1 ) {
if ( pixels ) { if ( pixels ) {
// On error, LoadJPGBuff might store a pointer to the error message in pixels // On error, LoadJPGBuff might store a pointer to the error message in pixels
Sys_Warning( "LoadJPGBuff %s %s\n", filename, (unsigned char*) pixels ); Sys_Warning( "LoadJPGBuff %s %s\n", filename, (unsigned char*) pixels );
@ -373,7 +345,6 @@ const image_t *ImageLoad( const char *name ){
} }
free( pixels ); free( pixels );
} }
free( buffer );
} }
} }

View File

@ -1152,7 +1152,7 @@ void MapRawLightmap( int rawLightmapNum ){
/* divine a normal and origin from neighboring luxels */ /* divine a normal and origin from neighboring luxels */
fake.xyz.set( 0 ); fake.xyz.set( 0 );
fake.normal.set( 0 ); fake.normal.set( 0 );
fake.lightmap[ 0 ] = { x, y }; //% 0.0001 + x; //% 0.0001 + y; fake.lightmap[ 0 ] = Vector2( x, y ); //% 0.0001 + x; //% 0.0001 + y;
samples = 0.0f; samples = 0.0f;
for ( sy = ( y - 1 ); sy <= ( y + 1 ); sy++ ) for ( sy = ( y - 1 ); sy <= ( y + 1 ); sy++ )
{ {
@ -3750,7 +3750,7 @@ void SetupFloodLight( void ){
sscanf( value, "%lf %lf %lf %lf %lf %lf", &v1, &v2, &v3, &v4, &v5, &v6 ); sscanf( value, "%lf %lf %lf %lf %lf %lf", &v1, &v2, &v3, &v4, &v5, &v6 );
floodlightRGB = { v1, v2, v3 }; floodlightRGB = Vector3( v1, v2, v3 );
if ( vector3_length( floodlightRGB ) == 0 ) { if ( vector3_length( floodlightRGB ) == 0 ) {
floodlightRGB = { 0.94, 0.94, 1.0 }; floodlightRGB = { 0.94, 0.94, 1.0 };

View File

@ -175,9 +175,9 @@ int ExportLightmapsMain( Args& args ){
*/ */
int ImportLightmapsMain( Args& args ){ int ImportLightmapsMain( Args& args ){
int i, x, y, len, width, height; int i, x, y, width, height;
char dirname[ 1024 ], filename[ 1024 ]; char dirname[ 1024 ], filename[ 1024 ];
byte *lightmap, *buffer, *pixels, *in, *out; byte *lightmap, *pixels, *in, *out;
/* arg checking */ /* arg checking */
@ -215,17 +215,15 @@ int ImportLightmapsMain( Args& args ){
/* read a tga image */ /* read a tga image */
sprintf( filename, "%s/lightmap_%04d.tga", dirname, i ); sprintf( filename, "%s/lightmap_%04d.tga", dirname, i );
Sys_Printf( "Loading %s\n", filename ); Sys_Printf( "Loading %s\n", filename );
buffer = NULL; MemBuffer buffer = vfsLoadFile( filename, -1 );
len = vfsLoadFile( filename, (void**) &buffer, -1 ); if ( !buffer ) {
if ( len < 0 ) {
Sys_Warning( "Unable to load image %s\n", filename ); Sys_Warning( "Unable to load image %s\n", filename );
continue; continue;
} }
/* parse file into an image */ /* parse file into an image */
pixels = NULL; pixels = NULL;
LoadTGABuffer( buffer, buffer + len, &pixels, &width, &height ); LoadTGABuffer( buffer.data(), buffer.size(), &pixels, &width, &height );
free( buffer );
/* sanity check it */ /* sanity check it */
if ( pixels == NULL ) { if ( pixels == NULL ) {

View File

@ -1657,20 +1657,15 @@ static bool ParseMapEntity( bool onlyLights, bool noCollapseGroups ){
*/ */
void LoadMapFile( const char *filename, bool onlyLights, bool noCollapseGroups ){ void LoadMapFile( const char *filename, bool onlyLights, bool noCollapseGroups ){
FILE *file;
int oldNumEntities = 0; int oldNumEntities = 0;
/* note it */ /* note it */
Sys_FPrintf( SYS_VRB, "--- LoadMapFile ---\n" ); Sys_FPrintf( SYS_VRB, "--- LoadMapFile ---\n" );
Sys_Printf( "Loading %s\n", filename );
/* hack */
file = SafeOpenRead( filename );
fclose( file );
/* load the map file */ /* load the map file */
LoadScriptFile( filename, -1 ); if( !LoadScriptFile( filename, -1 ) )
Error( "" );
/* setup */ /* setup */
if ( onlyLights ) { if ( onlyLights ) {

View File

@ -117,10 +117,8 @@ public:
* you probably have to supply an own implementation of IOStream as well. * you probably have to supply an own implementation of IOStream as well.
*/ */
Assimp::IOStream* Open( const char* pFile, const char* pMode = "rb" ) override { Assimp::IOStream* Open( const char* pFile, const char* pMode = "rb" ) override {
const uint8_t *boo; if ( MemBuffer boo = vfsLoadFile( pFile ) ) {
const int size = vfsLoadFile( pFile, (void**) &boo, 0 ); return new Assimp::MemoryIOStream( boo.release(), boo.size(), true );
if ( size >= 0 ) {
return new Assimp::MemoryIOStream( boo, size, true );
} }
return nullptr; return nullptr;
} }
@ -1011,19 +1009,18 @@ void InsertModel( const char *name, int skin, int frame, const Matrix4& transfor
std::list<remap_t> skins; std::list<remap_t> skins;
{ {
auto skinfilename = StringOutputStream(99)( PathExtensionless( name ), '_', skin, ".skin" ); auto skinfilename = StringOutputStream(99)( PathExtensionless( name ), '_', skin, ".skin" );
char *skinfilecontent; MemBuffer skinfile = vfsLoadFile( skinfilename );
int skinfilesize = vfsLoadFile( skinfilename, (void**) &skinfilecontent, 0 ); if ( skinfile && skin != 0 ) {
if ( skinfilesize < 0 && skin != 0 ) {
/* fallback to skin 0 if invalid */ /* fallback to skin 0 if invalid */
skinfilename( PathExtensionless( name ), "_0.skin" ); skinfilename( PathExtensionless( name ), "_0.skin" );
skinfilesize = vfsLoadFile( skinfilename, (void**) &skinfilecontent, 0 ); skinfile = vfsLoadFile( skinfilename );
if ( skinfilesize >= 0 ) { if ( skinfile ) {
Sys_Printf( "Skin %d of %s does not exist, using 0 instead\n", skin, name ); Sys_Printf( "Skin %d of %s does not exist, using 0 instead\n", skin, name );
} }
} }
if ( skinfilesize >= 0 ) { if ( skinfile ) {
Sys_Printf( "Using skin %d of %s\n", skin, name ); Sys_Printf( "Using skin %d of %s\n", skin, name );
for ( char *skinfilenextptr, *skinfileptr = skinfilecontent; !strEmpty( skinfileptr ); skinfileptr = skinfilenextptr ) for ( char *skinfilenextptr, *skinfileptr = skinfile.data(); !strEmpty( skinfileptr ); skinfileptr = skinfilenextptr )
{ {
// for fscanf // for fscanf
char format[64]; char format[64];
@ -1060,7 +1057,6 @@ void InsertModel( const char *name, int skin, int frame, const Matrix4& transfor
/* invalid input line -> discard skin struct */ /* invalid input line -> discard skin struct */
Sys_Printf( "Discarding skin directive in %s: %s\n", skinfilename.c_str(), skinfileptr ); Sys_Printf( "Discarding skin directive in %s: %s\n", skinfilename.c_str(), skinfileptr );
} }
free( skinfilecontent );
} }
} }

View File

@ -874,7 +874,7 @@ static void ParseShaderFile( const char *filename ){
ShaderTextCollector text; ShaderTextCollector text;
/* load the shader */ /* load the shader */
LoadScriptFile( filename, 0 ); LoadScriptFile( filename );
/* tokenize it */ /* tokenize it */
while ( GetToken( true ) ) /* test for end of file */ while ( GetToken( true ) ) /* test for end of file */
@ -1827,7 +1827,7 @@ static void ParseCustomInfoParms( void ){
} }
/* load it */ /* load it */
LoadScriptFile( "scripts/custinfoparms.txt", 0 ); LoadScriptFile( "scripts/custinfoparms.txt" );
/* clear the array */ /* clear the array */
memset( custSurfaceParms, 0, sizeof( custSurfaceParms ) ); memset( custSurfaceParms, 0, sizeof( custSurfaceParms ) );

View File

@ -280,33 +280,22 @@ void WriteSurfaceExtraFile( const char *path ){
*/ */
void LoadSurfaceExtraFile( const char *path ){ void LoadSurfaceExtraFile( const char *path ){
char srfPath[ 1024 ];
surfaceExtra_t *se;
int surfaceNum, size;
byte *buffer;
/* dummy check */ /* dummy check */
if ( strEmptyOrNull( path ) ) { if ( strEmptyOrNull( path ) ) {
return; return;
} }
/* load the file */ /* load the file */
strcpy( srfPath, path ); auto srfPath = StringOutputStream( 256 )( PathExtensionless( path ), ".srf" );
path_set_extension( srfPath, ".srf" );
Sys_Printf( "Loading %s\n", srfPath );
size = LoadFile( srfPath, (void**) &buffer );
if ( size <= 0 ) {
Sys_Warning( "Unable to find surface file %s, using defaults.\n", srfPath );
return;
}
/* parse the file */ /* parse the file */
ParseFromMemory( (char *) buffer, size ); if( !LoadScriptFile( srfPath, -1 ) )
Error( "" );
/* tokenize it */ /* tokenize it */
while ( GetToken( true ) ) /* test for end of file */ while ( GetToken( true ) ) /* test for end of file */
{ {
surfaceExtra_t *se;
/* default? */ /* default? */
if ( striEqual( token, "default" ) ) { if ( striEqual( token, "default" ) ) {
se = &seDefault; se = &seDefault;
@ -315,9 +304,9 @@ void LoadSurfaceExtraFile( const char *path ){
/* surface number */ /* surface number */
else else
{ {
surfaceNum = atoi( token ); const int surfaceNum = atoi( token );
if ( surfaceNum < 0 || surfaceNum > MAX_MAP_DRAW_SURFS ) { if ( surfaceNum < 0 || surfaceNum > MAX_MAP_DRAW_SURFS ) {
Error( "ReadSurfaceExtraFile(): %s, line %d: bogus surface num %d", srfPath, scriptline, surfaceNum ); Error( "ReadSurfaceExtraFile(): %s, line %d: bogus surface num %d", srfPath.c_str(), scriptline, surfaceNum );
} }
while ( surfaceNum >= numSurfaceExtras ) while ( surfaceNum >= numSurfaceExtras )
se = AllocSurfaceExtra(); se = AllocSurfaceExtra();
@ -326,7 +315,7 @@ void LoadSurfaceExtraFile( const char *path ){
/* handle { } section */ /* handle { } section */
if ( !( GetToken( true ) && strEqual( token, "{" ) ) ) { if ( !( GetToken( true ) && strEqual( token, "{" ) ) ) {
Error( "ReadSurfaceExtraFile(): %s, line %d: { not found", srfPath, scriptline ); Error( "ReadSurfaceExtraFile(): %s, line %d: { not found", srfPath.c_str(), scriptline );
} }
while ( GetToken( true ) && !strEqual( token, "}" ) ) while ( GetToken( true ) && !strEqual( token, "}" ) )
{ {
@ -382,7 +371,4 @@ void LoadSurfaceExtraFile( const char *path ){
GetToken( false ); GetToken( false );
} }
} }
/* free the buffer */
free( buffer );
} }