From 3d132e0e83a0a5933c9c9702df699c530cf9c68f Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Wed, 8 Oct 2025 05:03:29 +0500 Subject: [PATCH] common: port: move filesystem related macros to filesystem module, try to include less globally for faster builds --- common/port.h | 108 ++++++++------------- engine/client/vgui/vgui_draw.c | 7 +- engine/common/imagelib/img_png.c | 85 ++++++++-------- engine/platform/posix/crash_libbacktrace.c | 1 + engine/platform/posix/lib_posix.c | 11 +++ filesystem/filesystem.c | 18 +++- game_launch/game.cpp | 3 +- 7 files changed, 112 insertions(+), 121 deletions(-) diff --git a/common/port.h b/common/port.h index defb7ff9..e5c7bae5 100644 --- a/common/port.h +++ b/common/port.h @@ -17,75 +17,49 @@ GNU General Public License for more details. #ifndef PORT_H #define PORT_H -#include "build.h" - -#if !XASH_WIN32 - #if XASH_APPLE - #include - #define OS_LIB_EXT "dylib" - #define OPEN_COMMAND "open" - #elif XASH_EMSCRIPTEN - #define OS_LIB_EXT "wasm" - #define OPEN_COMMAND "???" - #else - #define OS_LIB_EXT "so" - #define OPEN_COMMAND "xdg-open" - #endif - #define OS_LIB_PREFIX "lib" - #define VGUI_SUPPORT_DLL "libvgui_support." OS_LIB_EXT - - // Windows-specific - #define __cdecl - #define __stdcall - - #if XASH_POSIX - #include - #if XASH_NSWITCH - #define SOLDER_LIBDL_COMPAT - #include - #elif XASH_PSVITA - #define VRTLD_LIBDL_COMPAT - #include - #define O_BINARY 0 - #else - #include - #define HAVE_DUP - #define O_BINARY 0 - #endif - #define O_TEXT 0 - #define _mkdir( x ) mkdir( x, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH ) - #endif - - typedef void* HANDLE; - typedef void* HINSTANCE; - - typedef struct tagPOINT - { - int x, y; - } POINT; -#else // WIN32 - #define open _open - #define read _read - #define alloca _alloca - - #define HSPRITE WINAPI_HSPRITE - #define WIN32_LEAN_AND_MEAN - #include - #include - #undef HSPRITE - - #define OS_LIB_PREFIX "" - #define OS_LIB_EXT "dll" - #define VGUI_SUPPORT_DLL "../vgui_support." OS_LIB_EXT - #define HAVE_DUP -#endif //WIN32 - -#ifndef XASH_LOW_MEMORY -#define XASH_LOW_MEMORY 0 -#endif - #include #include #include +#include "build.h" + +#if XASH_APPLE + #include + #define OS_LIB_PREFIX "lib" + #define OS_LIB_EXT "dylib" + #define OPEN_COMMAND "open" +#elif XASH_POSIX + #include + #define OS_LIB_PREFIX "lib" + #define OS_LIB_EXT "so" + #define OPEN_COMMAND "xdg-open" +#elif XASH_WIN32 + #define HSPRITE WINAPI_HSPRITE + #define WIN32_LEAN_AND_MEAN + #define NOMINMAX + #define VC_EXTRALEAN + + #include + #undef HSPRITE + #define open _open + #define read _read + #define alloca _alloca + #define OS_LIB_PREFIX "" + #define OS_LIB_EXT "dll" + #define OPEN_COMMAND "open" +#endif + +#if !defined( _MSC_VER ) + #define __cdecl + #define __stdcall +#endif + +#if !XASH_WIN32 + typedef void* HINSTANCE; + typedef struct tagPOINT { int x, y; } POINT; // one nasty function in cdll_int.h needs it +#endif // !XASH_WIN32 + +#ifndef XASH_LOW_MEMORY + #define XASH_LOW_MEMORY 0 +#endif #endif // PORT_H diff --git a/engine/client/vgui/vgui_draw.c b/engine/client/vgui/vgui_draw.c index b68ab399..85061df1 100644 --- a/engine/client/vgui/vgui_draw.c +++ b/engine/client/vgui/vgui_draw.c @@ -343,17 +343,14 @@ qboolean VGui_LoadProgs( HINSTANCE hInstance ) if( !Sys_GetParmFromCmdLine( "-vguiloader", vguiloader )) { - Q_strncpy( vguiloader, VGUI_SUPPORT_DLL, sizeof( vguiloader )); + Q_strncpy( vguiloader, OS_LIB_PREFIX "vgui_support." OS_LIB_EXT, sizeof( vguiloader )); } hInstance = vgui.hInstance = COM_LoadLibrary( vguiloader, false, false ); if( !vgui.hInstance ) { - if( FS_FileExists( vguiloader, false )) - Con_Reportf( S_ERROR "Failed to load vgui_support library: %s\n", COM_GetLibraryError() ); - else Con_Reportf( "%s: not found\n", __func__ ); - + Con_Reportf( S_ERROR "Failed to load vgui_support library: %s\n", COM_GetLibraryError() ); return false; } } diff --git a/engine/common/imagelib/img_png.c b/engine/common/imagelib/img_png.c index a089ea37..13d6d948 100644 --- a/engine/common/imagelib/img_png.c +++ b/engine/common/imagelib/img_png.c @@ -18,15 +18,6 @@ GNU General Public License for more details. #include "xash3d_mathlib.h" #include "img_png.h" -#if defined(XASH_NO_NETWORK) - #include "platform/stub/net_stub.h" -#elif XASH_NSWITCH - // our ntohl is here - #include -#elif !XASH_WIN32 - #include -#endif - static const char png_sign[] = {0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n'}; static const char ihdr_sign[] = {'I', 'H', 'D', 'R'}; static const char trns_sign[] = {'t', 'R', 'N', 'S'}; @@ -52,41 +43,41 @@ qboolean Image_LoadPNG( const char *name, const byte *buffer, fs_offset_t filesi z_stream stream = {0}; png_t png_hdr; - if( filesize < sizeof( png_hdr ) ) + if( filesize < sizeof( png_hdr )) return false; buf_p = (byte *)buffer; // get png header - memcpy( &png_hdr, buffer, sizeof( png_t ) ); + memcpy( &png_hdr, buffer, sizeof( png_t )); // check png signature - if( memcmp( png_hdr.sign, png_sign, sizeof( png_sign ) ) ) + if( memcmp( png_hdr.sign, png_sign, sizeof( png_sign ))) { Con_DPrintf( S_ERROR "%s: Invalid PNG signature (%s)\n", __func__, name ); return false; } // convert IHDR chunk length to little endian - png_hdr.ihdr_len = ntohl( png_hdr.ihdr_len ); + png_hdr.ihdr_len = BigLong( png_hdr.ihdr_len ); // check IHDR chunk length (valid value - 13) - if( png_hdr.ihdr_len != sizeof( png_ihdr_t ) ) + if( png_hdr.ihdr_len != sizeof( png_ihdr_t )) { Con_DPrintf( S_ERROR "%s: Invalid IHDR chunk size %u (%s)\n", __func__, png_hdr.ihdr_len, name ); return false; } // check IHDR chunk signature - if( memcmp( png_hdr.ihdr_sign, ihdr_sign, sizeof( ihdr_sign ) ) ) + if( memcmp( png_hdr.ihdr_sign, ihdr_sign, sizeof( ihdr_sign ))) { Con_DPrintf( S_ERROR "%s: IHDR chunk corrupted (%s)\n", __func__, name ); return false; } // convert image width and height to little endian - image.height = png_hdr.ihdr_chunk.height = ntohl( png_hdr.ihdr_chunk.height ); - image.width = png_hdr.ihdr_chunk.width = ntohl( png_hdr.ihdr_chunk.width ); + image.height = png_hdr.ihdr_chunk.height = BigLong( png_hdr.ihdr_chunk.height ); + image.width = png_hdr.ihdr_chunk.width = BigLong( png_hdr.ihdr_chunk.width ); if( png_hdr.ihdr_chunk.height == 0 || png_hdr.ihdr_chunk.width == 0 ) { @@ -107,7 +98,7 @@ qboolean Image_LoadPNG( const char *name, const byte *buffer, fs_offset_t filesi || png_hdr.ihdr_chunk.colortype == PNG_CT_RGBA || png_hdr.ihdr_chunk.colortype == PNG_CT_GREY || png_hdr.ihdr_chunk.colortype == PNG_CT_ALPHA - || png_hdr.ihdr_chunk.colortype == PNG_CT_PALLETE ) ) + || png_hdr.ihdr_chunk.colortype == PNG_CT_PALLETE )) { Con_DPrintf( S_WARN "%s: Unknown color type %u (%s)\n", __func__, png_hdr.ihdr_chunk.colortype, name ); return false; @@ -139,11 +130,11 @@ qboolean Image_LoadPNG( const char *name, const byte *buffer, fs_offset_t filesi // calculate IHDR chunk CRC CRC32_Init( &crc32_check ); - CRC32_ProcessBuffer( &crc32_check, buf_p + sizeof( png_hdr.sign ) + sizeof( png_hdr.ihdr_len ), png_hdr.ihdr_len + sizeof( png_hdr.ihdr_sign ) ); + CRC32_ProcessBuffer( &crc32_check, buf_p + sizeof( png_hdr.sign ) + sizeof( png_hdr.ihdr_len ), png_hdr.ihdr_len + sizeof( png_hdr.ihdr_sign )); crc32_check = CRC32_Final( crc32_check ); // check IHDR chunk CRC - if( ntohl( png_hdr.ihdr_crc32 ) != crc32_check ) + if( BigLong( png_hdr.ihdr_crc32 ) != crc32_check ) { Con_DPrintf( S_ERROR "%s: IHDR chunk has wrong CRC32 sum (%s)\n", __func__, name ); return false; @@ -156,10 +147,10 @@ qboolean Image_LoadPNG( const char *name, const byte *buffer, fs_offset_t filesi while( !has_iend_chunk && ( buf_p - buffer ) < filesize ) { // get chunk length - memcpy( &chunk_len, buf_p, sizeof( chunk_len ) ); + memcpy( &chunk_len, buf_p, sizeof( chunk_len )); // convert chunk length to little endian - chunk_len = ntohl( chunk_len ); + chunk_len = BigLong( chunk_len ); if( chunk_len > INT_MAX ) { @@ -179,31 +170,31 @@ qboolean Image_LoadPNG( const char *name, const byte *buffer, fs_offset_t filesi buf_p += sizeof( chunk_len ); // find transparency - if( !memcmp( buf_p, trns_sign, sizeof( trns_sign ) ) ) + if( !memcmp( buf_p, trns_sign, sizeof( trns_sign ))) { trns = buf_p + sizeof( trns_sign ); trns_len = chunk_len; } // find pallete for indexed image - else if( !memcmp( buf_p, plte_sign, sizeof( plte_sign ) ) ) + else if( !memcmp( buf_p, plte_sign, sizeof( plte_sign ))) { pallete = buf_p + sizeof( plte_sign ); plte_len = chunk_len / 3; } // get all IDAT chunks data - else if( !memcmp( buf_p, idat_sign, sizeof( idat_sign ) ) ) + else if( !memcmp( buf_p, idat_sign, sizeof( idat_sign ))) { newsize = oldsize + chunk_len; idat_buf = (byte *)Mem_Realloc( host.imagepool, idat_buf, newsize ); memcpy( idat_buf + oldsize, buf_p + sizeof( idat_sign ), chunk_len ); oldsize = newsize; } - else if( !memcmp( buf_p, iend_sign, sizeof( iend_sign ) ) ) + else if( !memcmp( buf_p, iend_sign, sizeof( iend_sign ))) has_iend_chunk = true; // calculate chunk CRC CRC32_Init( &crc32_check ); - CRC32_ProcessBuffer( &crc32_check, buf_p, chunk_len + sizeof( idat_sign ) ); + CRC32_ProcessBuffer( &crc32_check, buf_p, chunk_len + sizeof( idat_sign )); crc32_check = CRC32_Final( crc32_check ); // move pointer @@ -211,10 +202,10 @@ qboolean Image_LoadPNG( const char *name, const byte *buffer, fs_offset_t filesi buf_p += chunk_len; // get real chunk CRC - memcpy( &crc32, buf_p, sizeof( crc32 ) ); + memcpy( &crc32, buf_p, sizeof( crc32 )); // check chunk CRC - if( ntohl( crc32 ) != crc32_check ) + if( BigLong( crc32 ) != crc32_check ) { Con_DPrintf( S_ERROR "%s: Found chunk with wrong CRC32 sum (%s)\n", __func__, name ); if( idat_buf ) Mem_Free( idat_buf ); @@ -280,7 +271,7 @@ qboolean Image_LoadPNG( const char *name, const byte *buffer, fs_offset_t filesi if( png_hdr.ihdr_chunk.colortype & PNG_CT_RGB ) image.flags |= IMAGE_HAS_COLOR; - if( trns || ( png_hdr.ihdr_chunk.colortype & PNG_CT_ALPHA ) ) + if( trns || ( png_hdr.ihdr_chunk.colortype & PNG_CT_ALPHA )) image.flags |= IMAGE_HAS_ALPHA; image.depth = 1; @@ -388,7 +379,7 @@ qboolean Image_LoadPNG( const char *name, const byte *buffer, fs_offset_t filesi pixbuf[i] = raw[i] + ( prior[i] >> 1 ); for( ; i < rowsize; i++ ) - pixbuf[i] = raw[i] + ( ( pixbuf[i - pixel_size] + prior[i] ) >> 1 ); + pixbuf[i] = raw[i] + (( pixbuf[i - pixel_size] + prior[i] ) >> 1 ); break; case PNG_F_PAETH: for( ; i < pixel_size; i++ ) @@ -603,19 +594,19 @@ qboolean Image_SavePNG( const char *name, rgbdata_t *pix ) outsize += sizeof( png_footer_t ); // write PNG header - memcpy( png_hdr.sign, png_sign, sizeof( png_sign ) ); + memcpy( png_hdr.sign, png_sign, sizeof( png_sign )); // write IHDR chunk length - png_hdr.ihdr_len = htonl( ihdr_len ); + png_hdr.ihdr_len = BigLong( ihdr_len ); // write IHDR chunk signature - memcpy( png_hdr.ihdr_sign, ihdr_sign, sizeof( ihdr_sign ) ); + memcpy( png_hdr.ihdr_sign, ihdr_sign, sizeof( ihdr_sign )); // write image width - png_hdr.ihdr_chunk.width = htonl( pix->width ); + png_hdr.ihdr_chunk.width = BigLong( pix->width ); // write image height - png_hdr.ihdr_chunk.height = htonl( pix->height ); + png_hdr.ihdr_chunk.height = BigLong( pix->height ); // write image bitdepth png_hdr.ihdr_chunk.bitdepth = 8; @@ -634,11 +625,11 @@ qboolean Image_SavePNG( const char *name, rgbdata_t *pix ) // get IHDR chunk CRC CRC32_Init( &crc32 ); - CRC32_ProcessBuffer( &crc32, &png_hdr.ihdr_sign, ihdr_len + sizeof( ihdr_sign ) ); + CRC32_ProcessBuffer( &crc32, &png_hdr.ihdr_sign, ihdr_len + sizeof( ihdr_sign )); crc32 = CRC32_Final( crc32 ); // write IHDR chunk CRC - png_hdr.ihdr_crc32 = htonl( crc32 ); + png_hdr.ihdr_crc32 = BigLong( crc32 ); out = buffer = (byte *)Mem_Malloc( host.imagepool, outsize ); @@ -673,43 +664,43 @@ qboolean Image_SavePNG( const char *name, rgbdata_t *pix ) idat_len = stream.total_out; outsize += idat_len; - memcpy( out, &png_hdr, sizeof( png_t ) ); + memcpy( out, &png_hdr, sizeof( png_t )); out += sizeof( png_t ); // convert IDAT chunk length to big endian - big_idat_len = htonl( idat_len ); + big_idat_len = BigLong( idat_len ); // write IDAT chunk length - memcpy( out, &big_idat_len, sizeof( idat_len ) ); + memcpy( out, &big_idat_len, sizeof( idat_len )); out += sizeof( idat_len ); // write IDAT chunk signature - memcpy( out, idat_sign, sizeof( idat_sign ) ); + memcpy( out, idat_sign, sizeof( idat_sign )); // calculate IDAT chunk CRC CRC32_Init( &crc32 ); - CRC32_ProcessBuffer( &crc32, out, idat_len + sizeof( idat_sign ) ); + CRC32_ProcessBuffer( &crc32, out, idat_len + sizeof( idat_sign )); crc32 = CRC32_Final( crc32 ); out += sizeof( idat_sign ); out += idat_len; // write IDAT chunk CRC - png_ftr.idat_crc32 = htonl( crc32 ); + png_ftr.idat_crc32 = BigLong( crc32 ); // write IEND chunk length png_ftr.iend_len = 0; // write IEND chunk signature - memcpy( png_ftr.iend_sign, iend_sign, sizeof( iend_sign ) ); + memcpy( png_ftr.iend_sign, iend_sign, sizeof( iend_sign )); // write IEND chunk CRC - png_ftr.iend_crc32 = htonl( iend_crc32 ); + png_ftr.iend_crc32 = BigLong( iend_crc32 ); // write PNG footer to buffer - memcpy( out, &png_ftr, sizeof( png_ftr ) ); + memcpy( out, &png_ftr, sizeof( png_ftr )); FS_WriteFile( name, buffer, outsize ); diff --git a/engine/platform/posix/crash_libbacktrace.c b/engine/platform/posix/crash_libbacktrace.c index d1a466f4..6f2cdbbf 100644 --- a/engine/platform/posix/crash_libbacktrace.c +++ b/engine/platform/posix/crash_libbacktrace.c @@ -16,6 +16,7 @@ GNU General Public License for more details. #if HAVE_LIBBACKTRACE #include +#include #include "common.h" #include "backtrace.h" #include "input.h" diff --git a/engine/platform/posix/lib_posix.c b/engine/platform/posix/lib_posix.c index 0e66ee4c..2bc4dcc3 100644 --- a/engine/platform/posix/lib_posix.c +++ b/engine/platform/posix/lib_posix.c @@ -17,6 +17,17 @@ GNU General Public License for more details. #endif #include "platform/platform.h" #if XASH_LIB == LIB_POSIX + +#if XASH_NSWITCH + #define SOLDER_LIBDL_COMPAT + #include +#elif XASH_PSVITA + #define VRTLD_LIBDL_COMPAT + #include +#else + #include +#endif + #ifdef XASH_IRIX #include "platform/irix/dladdr.h" #endif diff --git a/filesystem/filesystem.c b/filesystem/filesystem.c index bfc9c1d5..9cd69595 100644 --- a/filesystem/filesystem.c +++ b/filesystem/filesystem.c @@ -51,6 +51,18 @@ GNU General Public License for more details. #define FILE_COPY_SIZE (1024 * 1024) #define SAVE_AGED_COUNT 2 // the default count of quick and auto saves +#if !defined( O_BINARY ) + #define O_BINARY 0 +#endif // !defined( O_BINARY ) + +#if !defined( O_TEXT ) + #define O_TEXT 0 +#endif // !defined( O_TEXT ) + +#if !XASH_PSVITA && !XASH_NSWITCH + #define HAVE_DUP +#endif // !XASH_PSVITA && !XASH_NSWITCH + fs_globals_t FI; poolhandle_t fs_mempool; char fs_rootdir[MAX_SYSPATH]; @@ -357,7 +369,11 @@ void FS_CreatePath( char *path ) // create the directory save = *ofs; *ofs = 0; - _mkdir( path ); +#if XASH_WIN32 + _mkdir( path ); // use _wmkdir maybe? +#else // !XASH_WIN32 + mkdir( path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH ); +#endif // !XASH_WIN32 *ofs = save; } } diff --git a/game_launch/game.cpp b/game_launch/game.cpp index f708fe7a..65e95840 100644 --- a/game_launch/game.cpp +++ b/game_launch/game.cpp @@ -22,7 +22,8 @@ GNU General Public License for more details. #include #if XASH_POSIX -#define XASHLIB "libxash." OS_LIB_EXT +#include +#define XASHLIB OS_LIB_PREFIX "xash." OS_LIB_EXT #define FreeLibrary( x ) dlclose( x ) #elif XASH_WIN32 #include // CommandLineToArgvW