From e4ad8def0dca2bb89904887553517cc3dcea600b Mon Sep 17 00:00:00 2001 From: SNMetamorph <25657591+SNMetamorph@users.noreply.github.com> Date: Sun, 3 Oct 2021 10:08:03 +0400 Subject: [PATCH] engine: filesystem: fixed current directory changing for Windows --- common/port.h | 2 -- engine/common/common.h | 1 + engine/common/filesystem.c | 38 ++++++++++++++++++++++++++++++++++++++ engine/common/host.c | 2 +- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/common/port.h b/common/port.h index 102b6dff..183f1fe7 100644 --- a/common/port.h +++ b/common/port.h @@ -61,7 +61,6 @@ GNU General Public License for more details. #define _mkdir( x ) mkdir( x, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH ) #define LoadLibrary( x ) dlopen( x, RTLD_NOW ) #define GetProcAddress( x, y ) dlsym( x, y ) - #define SetCurrentDirectory( x ) (!chdir( x )) #define FreeLibrary( x ) dlclose( x ) #define tell( a ) lseek(a, 0, SEEK_CUR) #define HAVE_DUP @@ -72,7 +71,6 @@ GNU General Public License for more details. #define LoadLibrary( x ) (0) #define GetProcAddress( x, y ) (0) #define FreeLibrary( x ) (0) - #define SetCurrentDirectory( x ) (!chdir( x )) #endif //#define MAKEWORD( a, b ) ((short int)(((unsigned char)(a))|(((short int)((unsigned char)(b)))<<8))) #define max( a, b ) (((a) > (b)) ? (a) : (b)) diff --git a/engine/common/common.h b/engine/common/common.h index 7470877e..d6b77ff7 100644 --- a/engine/common/common.h +++ b/engine/common/common.h @@ -566,6 +566,7 @@ int FS_FileTime( const char *filename, qboolean gamedironly ); int FS_Print( file_t *file, const char *msg ); qboolean FS_Rename( const char *oldname, const char *newname ); int FS_FileExists( const char *filename, int gamedironly ); +int FS_SetCurrentDirectory( const char *path ); qboolean FS_SysFileExists( const char *path, qboolean casesensitive ); qboolean FS_FileCopy( file_t *pOutput, file_t *pInput, int fileSize ); qboolean FS_Delete( const char *path ); diff --git a/engine/common/filesystem.c b/engine/common/filesystem.c index c0fe69cd..72efd475 100644 --- a/engine/common/filesystem.c +++ b/engine/common/filesystem.c @@ -463,6 +463,22 @@ static const char *FS_FixFileCase( const char *path ) return path; } +#if XASH_WIN32 +/* +==================== +FS_PathToWideChar + +Converts input UTF-8 string to wide char string. +==================== +*/ +const wchar_t *FS_PathToWideChar( const char *path ) +{ + static wchar_t pathBuffer[MAX_PATH]; + MultiByteToWideChar( CP_UTF8, 0, path, -1, pathBuffer, MAX_PATH ); + return pathBuffer; +} +#endif + /* ==================== FS_AddFileToPack @@ -2240,7 +2256,11 @@ static file_t *FS_SysOpen( const char *filepath, const char *mode ) file->filetime = FS_SysFileTime( filepath ); file->ungetc = EOF; +#if XASH_WIN32 + file->handle = _wopen( FS_PathToWideChar(filepath), mod | opt, 0666 ); +#else file->handle = open( filepath, mod|opt, 0666 ); +#endif #if !XASH_WIN32 if( file->handle < 0 ) @@ -2394,6 +2414,24 @@ qboolean FS_SysFileExists( const char *path, qboolean caseinsensitive ) #endif } +/* +================== +FS_SetCurrentDirectory + +Sets current directory, path should be in UTF-8 encoding +================== +*/ +int FS_SetCurrentDirectory( const char *path ) +{ +#if XASH_WIN32 + return SetCurrentDirectoryW( FS_PathToWideChar(path) ); +#elif XASH_POSIX + return !chdir( path ); +#else +#error +#endif +} + /* ================== FS_SysFolderExists diff --git a/engine/common/host.c b/engine/common/host.c index b183ca3b..82d19c79 100644 --- a/engine/common/host.c +++ b/engine/common/host.c @@ -980,7 +980,7 @@ void Host_InitCommon( int argc, char **argv, const char *progname, qboolean bCha if( len && host.rodir[len - 1] == '/' ) host.rodir[len - 1] = 0; - if( !COM_CheckStringEmpty( host.rootdir ) || SetCurrentDirectory( host.rootdir ) != 0 ) + if( !COM_CheckStringEmpty( host.rootdir ) || FS_SetCurrentDirectory( host.rootdir ) != 0 ) Con_Reportf( "%s is working directory now\n", host.rootdir ); else Sys_Error( "Changing working directory to %s failed.\n", host.rootdir );