From 06a2af0fa6b736c6360c702f07088bc64e9864e6 Mon Sep 17 00:00:00 2001 From: Wanne Date: Mon, 20 Jul 2009 08:14:37 +0000 Subject: [PATCH] Win 98 VFS Fixes (by BirdFlu) - add ini option to disable UNICODE in VFS (and use the current codepage) - when UNICODE disabled in VFS, open files with default encoding (instead of UTF8/16) filenames git-svn-id: https://ja2svn.mooo.com/source/ja2/trunk/GameSource/ja2_v1.13/Build@3118 3b4a5df2-a311-0410-b5c6-a8a6f20db521 --- Standard Gaming Platform/sgp.cpp | 4 ++ VFS/File/vfs_dir_file.cpp | 25 ++++++++--- VFS/File/vfs_file.cpp | 34 +++++++++++---- VFS/iteratedir.cpp | 72 ++++++++++++++++++++++++++------ VFS/iteratedir.h | 6 ++- VFS/utf8string.cpp | 58 +++++++++++++++++++++++++ VFS/utf8string.h | 13 ++++-- VFS/vfs_init.cpp | 11 +++-- 8 files changed, 191 insertions(+), 32 deletions(-) diff --git a/Standard Gaming Platform/sgp.cpp b/Standard Gaming Platform/sgp.cpp index 75047758..0872b436 100644 --- a/Standard Gaming Platform/sgp.cpp +++ b/Standard Gaming Platform/sgp.cpp @@ -599,6 +599,7 @@ BOOLEAN InitializeStandardGamingPlatform(HINSTANCE hInstance, int sCommandShow) } catch(CBasicException& ex) { + LogException(ex); // nothing is set up, no vfs, no video manager // regular error processing wouldn't work here // set default values and continue as if nothing has happened @@ -1224,6 +1225,7 @@ void SGPExit(void) } +extern bool g_VFS_NO_UNICODE; void GetRuntimeSettings( ) { #ifndef USE_VFS @@ -1248,6 +1250,8 @@ void GetRuntimeSettings( ) #else iResolution = oProps.GetIntProperty(L"Ja2 Settings", L"SCREEN_RESOLUTION", -1); + g_VFS_NO_UNICODE = oProps.GetBoolProperty(L"Ja2 Settings", L"VFS_NO_UNICODE", false); + std::list ini_list; if(oProps.GetStringListProperty(L"Ja2 Settings", L"VFS_CONFIG_INI", ini_list, L"")) { diff --git a/VFS/File/vfs_dir_file.cpp b/VFS/File/vfs_dir_file.cpp index 9caf43c6..7aac24b0 100644 --- a/VFS/File/vfs_dir_file.cpp +++ b/VFS/File/vfs_dir_file.cpp @@ -2,6 +2,8 @@ #include "../Interface/vfs_directory_interface.h" #include "../iteratedir.h" +extern bool g_VFS_NO_UNICODE; + vfs::Path vfs::CVFSFile::GetFullPath() { if(_pLoc_) @@ -65,7 +67,10 @@ bool vfs::CVFSFile::OpenRead() // try to open Mode = std::ios::in|std::ios::binary; #ifdef WIN32 - m_oFile.open(sFileName().c_wcs().c_str(),Mode); + utf8string::str_t const& fname = sFileName().c_wcs(); + g_VFS_NO_UNICODE ? + m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) : + m_oFile.open(fname.c_str(),Mode); #else m_oFile.open(sFileName().utf8().c_str(),Mode); #endif @@ -106,7 +111,10 @@ bool vfs::CVFSFile::OpenWrite(bool bCreateWhenNotExist, bool bTruncate) Mode |= std::ios::trunc; } #ifdef WIN32 - m_oFile.open(sFileName().c_wcs().c_str(),Mode); + utf8string::str_t const& fname = sFileName().c_wcs(); + g_VFS_NO_UNICODE ? + m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) : + m_oFile.open(fname.c_str(),Mode); #else m_oFile.open(sFileName().utf8().c_str(),Mode); #endif @@ -122,7 +130,9 @@ bool vfs::CVFSFile::OpenWrite(bool bCreateWhenNotExist, bool bTruncate) Mode |= std::ios::trunc; } #ifdef WIN32 - m_oFile.open(sFileName().c_wcs().c_str(),Mode); + g_VFS_NO_UNICODE ? + m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) : + m_oFile.open(fname.c_str(),Mode); #else m_oFile.open(sFileName().utf8().c_str(),Mode); #endif @@ -133,7 +143,9 @@ bool vfs::CVFSFile::OpenWrite(bool bCreateWhenNotExist, bool bTruncate) m_oFile.close(); Mode |= std::ios::in; #ifdef WIN32 - m_oFile.open(sFileName().c_wcs().c_str(),Mode); + g_VFS_NO_UNICODE ? + m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) : + m_oFile.open(fname.c_str(),Mode); #else m_oFile.open(sFileName().utf8().c_str(),Mode); #endif @@ -185,7 +197,10 @@ bool vfs::CVFSTextFile::OpenRead() std::ios::openmode Mode; Mode = std::ios::in; #ifdef WIN32 - m_oFile.open(sFileName().c_wcs().c_str(),Mode); + utf8string::str_t const& fname = sFileName().c_wcs(); + g_VFS_NO_UNICODE ? + m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) : + m_oFile.open(fname.c_str(),Mode); #else m_oFile.open(sFileName().utf8().c_str(),Mode); #endif diff --git a/VFS/File/vfs_file.cpp b/VFS/File/vfs_file.cpp index bbaa61b1..4ef142b0 100644 --- a/VFS/File/vfs_file.cpp +++ b/VFS/File/vfs_file.cpp @@ -3,6 +3,8 @@ #include +extern bool g_VFS_NO_UNICODE; + vfs::CFile::CFile(vfs::Path const& sFileName) : vfs::IFileTemplate(sFileName), m_bIsOpen_read(false), m_bIsOpen_write(false) { @@ -58,7 +60,10 @@ bool vfs::CFile::OpenRead() // try to open Mode = std::ios::in|std::ios::binary; #ifdef WIN32 - m_oFile.open(m_sFileName().c_wcs().c_str(), Mode); + utf8string::str_t const& fname = m_sFileName().c_wcs(); + g_VFS_NO_UNICODE ? + m_oFile.open( utf8string::narrow(fname.c_str(),fname.length()).c_str(), Mode ) : + m_oFile.open( fname.c_str(), Mode); #else m_oFile.open(m_sFileName().utf8().c_str(), Mode); #endif @@ -144,7 +149,10 @@ bool vfs::CFile::OpenWrite(bool bCreateWhenNotExist, bool bTruncate) Mode |= std::ios::trunc; } #ifdef WIN32 - m_oFile.open(m_sFileName().c_wcs().c_str(),Mode); + utf8string::str_t const& fname = m_sFileName().c_wcs(); + g_VFS_NO_UNICODE ? + m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) : + m_oFile.open(fname.c_str(),Mode); #else m_oFile.open(m_sFileName().utf8().c_str(),Mode); #endif @@ -161,7 +169,9 @@ bool vfs::CFile::OpenWrite(bool bCreateWhenNotExist, bool bTruncate) } //m_oFile.open(VfsString(sFileName).AsString().c_str(),Mode); #ifdef WIN32 - m_oFile.open(m_sFileName().c_wcs().c_str(),Mode); + g_VFS_NO_UNICODE ? + m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) : + m_oFile.open(fname.c_str(),Mode); #else m_oFile.open(m_sFileName().utf8().c_str(),Mode); #endif @@ -172,7 +182,9 @@ bool vfs::CFile::OpenWrite(bool bCreateWhenNotExist, bool bTruncate) m_oFile.close(); Mode |= std::ios::in; #ifdef WIN32 - m_oFile.open(m_sFileName().c_wcs().c_str(),Mode); + g_VFS_NO_UNICODE ? + m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) : + m_oFile.open(fname.c_str(),Mode); #else m_oFile.open(m_sFileName().utf8().c_str(),Mode); #endif @@ -299,7 +311,10 @@ bool vfs::CTextFile::OpenRead() std::ios::openmode Mode; Mode = std::ios::in; #ifdef WIN32 - m_oFile.open(m_sFileName().c_wcs().c_str(),Mode); + utf8string::str_t const& fname = m_sFileName().c_wcs(); + g_VFS_NO_UNICODE ? + m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) : + m_oFile.open(fname.c_str(),Mode); #else m_oFile.open(m_sFileName().utf8().c_str(),Mode); #endif @@ -322,7 +337,10 @@ bool vfs::CTextFile::OpenWrite(bool bCreateWhenNotExist, bool bTruncate) std::ios::openmode Mode; Mode = std::ios::out|std::ios::app; #ifdef WIN32 - m_oFile.open(m_sFileName().c_wcs().c_str(),Mode); + utf8string::str_t const& fname = m_sFileName().c_wcs(); + g_VFS_NO_UNICODE ? + m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) : + m_oFile.open(fname.c_str(),Mode); #else m_oFile.open(m_sFileName().utf8().c_str(),Mode); #endif @@ -335,7 +353,9 @@ bool vfs::CTextFile::OpenWrite(bool bCreateWhenNotExist, bool bTruncate) Mode = std::ios::out; Mode |= bTruncate ? std::ios::trunc : std::ios::app; #ifdef WIN32 - m_oFile.open(m_sFileName().c_wcs().c_str(),Mode); + g_VFS_NO_UNICODE ? + m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) : + m_oFile.open(fname.c_str(),Mode); #else m_oFile.open(m_sFileName().utf8().c_str(),Mode); #endif diff --git a/VFS/iteratedir.cpp b/VFS/iteratedir.cpp index e0d569ea..73e3529f 100644 --- a/VFS/iteratedir.cpp +++ b/VFS/iteratedir.cpp @@ -24,10 +24,21 @@ int file_select(struct direct *entry) #endif +extern bool g_VFS_NO_UNICODE; + os::CIterateDirectory::CIterateDirectory(vfs::Path const& sPath, utf8string const& searchPattern) { #ifdef WIN32 - fSearchHandle = FindFirstFileW((sPath+searchPattern)().c_wcs().c_str(), &fFileInfo); + if(g_VFS_NO_UNICODE) + { + std::string s; + utf8string::narrow((sPath+searchPattern)().c_wcs(), s); + fSearchHandle = FindFirstFileA(s.c_str(), &fFileInfoA); + } + else + { + fSearchHandle = FindFirstFileW((sPath+searchPattern)().c_wcs().c_str(), &fFileInfoW); + } if (fSearchHandle == INVALID_HANDLE_VALUE) { DWORD error = GetLastError(); @@ -58,12 +69,27 @@ bool os::CIterateDirectory::NextFile(utf8string &fileName, CIterateDirectory::EF { fFirstRequest = false; } - else if ( !FindNextFileW(fSearchHandle, &fFileInfo) ) + //else { - return false; + if(g_VFS_NO_UNICODE) + { + if( !FindNextFileA(fSearchHandle, &fFileInfoA) ) + { + return false; + } + fileName.r_wcs().assign( utf8string::widen( fFileInfoA.cFileName, strlen(fFileInfoA.cFileName) ) ); + attrib = (fFileInfoA.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? CIterateDirectory::FA_DIRECTORY : CIterateDirectory::FA_FILE; + } + else + { + if ( !FindNextFileW(fSearchHandle, &fFileInfoW) ) + { + return false; + } + fileName.r_wcs().assign(fFileInfoW.cFileName); + attrib = (fFileInfoW.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? CIterateDirectory::FA_DIRECTORY : CIterateDirectory::FA_FILE; + } } - fileName.r_wcs().assign(fFileInfo.cFileName); - attrib = (fFileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? CIterateDirectory::FA_DIRECTORY : CIterateDirectory::FA_FILE; return true; #else if(current_pos < count) @@ -84,15 +110,33 @@ bool os::CreateRealDirecory(vfs::Path& sDir, bool bDoNotCreate) if(bDoNotCreate) { bool bDirExists = false; - WIN32_FIND_DATAW fd; - memset(&fd,0,sizeof(WIN32_FIND_DATAW)); - HANDLE hFile = FindFirstFileW( sDir().c_wcs().c_str(), &fd); - bDirExists = fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY; - FindClose(hFile); + if(g_VFS_NO_UNICODE) + { + WIN32_FIND_DATAA fd; + memset(&fd,0,sizeof(WIN32_FIND_DATAA)); + + std::string s; + utf8string::narrow(sDir().c_wcs(), s); + + HANDLE hFile = FindFirstFileA( s.c_str(), &fd); + bDirExists = fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY; + FindClose(hFile); + } + else + { + WIN32_FIND_DATAW fd; + memset(&fd,0,sizeof(WIN32_FIND_DATAW)); + HANDLE hFile = FindFirstFileW( sDir().c_wcs().c_str(), &fd); + bDirExists = fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY; + FindClose(hFile); + } return bDirExists; } BOOL success; - success = CreateDirectoryW(sDir().c_wcs().c_str(),NULL); + utf8string::str_t const& str = sDir().c_wcs(); + success = g_VFS_NO_UNICODE ? + CreateDirectoryA( utf8string::narrow( str.c_str(), str.length() ).c_str(), NULL ) : + CreateDirectoryW(sDir().c_wcs().c_str(),NULL); if(success == 0) { DWORD error = GetLastError(); @@ -110,7 +154,11 @@ bool os::CreateRealDirecory(vfs::Path& sDir, bool bDoNotCreate) bool os::DeleteRealFile(vfs::Path &sDir) { #ifdef WIN32 - return (DeleteFileW( sDir().c_wcs().c_str() ) != FALSE); + utf8string::str_t const& str = sDir().c_wcs(); + BOOL del = g_VFS_NO_UNICODE ? + DeleteFileA( utf8string::narrow(str.c_str(), str.length()).c_str() ) : + DeleteFileW( str.c_str() ); + return (del != FALSE); #else return (remove( sDir().utf8().c_str() ) == 0); #endif diff --git a/VFS/iteratedir.h b/VFS/iteratedir.h index ac2a9f2f..9cce307f 100644 --- a/VFS/iteratedir.h +++ b/VFS/iteratedir.h @@ -35,7 +35,11 @@ namespace os private: #ifdef WIN32 HANDLE fSearchHandle; - WIN32_FIND_DATAW fFileInfo; + union + { + WIN32_FIND_DATAA fFileInfoA; + WIN32_FIND_DATAW fFileInfoW; + }; #else struct direct **files; int count, current_pos; diff --git a/VFS/utf8string.cpp b/VFS/utf8string.cpp index 743a2cc7..a94020e0 100644 --- a/VFS/utf8string.cpp +++ b/VFS/utf8string.cpp @@ -5,6 +5,9 @@ #include "vfs_debug.h" //////////////////////////////////////////////////////////////////// +bool g_VFS_NO_UNICODE = false; + +//////////////////////////////////////////////////////////////////// namespace _StrCmp { //////////////////////////////////////////////////////////////// @@ -242,6 +245,61 @@ std::string utf8string::as_utf8(std::string const& str) return str; } +size_t utf8string::narrow(std::wstring const& src, std::string& dst) +{ + size_t len = utf8string::narrow(src.c_str(),src.length(),NULL,0); + dst.resize(len); + return utf8string::narrow(src.c_str(),src.length(),&dst[0],len); +} +std::string utf8string::narrow(wchar_t const* str, size_t length) +{ + size_t len = utf8string::narrow(str,length,NULL,0); + std::string s; + s.resize(len); + utf8string::narrow(str,length,&s[0],len); + return s; +} +size_t utf8string::narrow(wchar_t const* src_str, size_t src_len, char* dst_str, size_t dst_len) +{ + if(src_str && src_len>0) + { + if(!dst_str || dst_len==0) + { + return wcstombs(NULL, src_str, src_len); + } + return wcstombs(dst_str, src_str, std::min(src_len,dst_len)); + } + return 0; +} +// +std::wstring utf8string::widen(char const* str, size_t length) +{ + size_t len = utf8string::widen(str,length,NULL,0); + std::wstring ws; + ws.resize(len); + utf8string::widen(str,length,&ws[0],len); + return ws; +} +size_t utf8string::widen(std::string const& src, std::wstring& dst) +{ + size_t len = utf8string::widen(src.c_str(),src.length(),NULL,0); + dst.resize(len); + return utf8string::widen(src.c_str(),src.length(),&dst[0],len); +} +size_t utf8string::widen(char const* src_str, size_t src_len, wchar_t* dst_str, size_t dst_len) +{ + if(src_str && src_len>0) + { + if(!dst_str || dst_len==0) + { + return mbstowcs(NULL, src_str, src_len); + } + return mbstowcs(dst_str, src_str, std::min(src_len,dst_len)); + } + return 0; +} + + //////////////////////////////////////////////////////////////////// /// get string methods //////////////////////////////////////////////////////////////////// diff --git a/VFS/utf8string.h b/VFS/utf8string.h index 80d6d66d..6dd3e7de 100644 --- a/VFS/utf8string.h +++ b/VFS/utf8string.h @@ -51,11 +51,18 @@ public: static utf8string::str_t as_utf16(std::string const& str); static void as_utf16(std::string const& str, utf8string::str_t &str16); // fast conversion without creating an internal copy - static std::string as_utf8(utf8string const& str); - static std::string as_utf8(std::wstring const& str); + static std::string as_utf8(utf8string const& str); + static std::string as_utf8(std::wstring const& str); // if 'strlen' is 0, length is determined automatically - static std::string as_utf8(const wchar_t* str, unsigned int strlength=0); + static std::string as_utf8(const wchar_t* str, unsigned int strlength=0); // + static std::string narrow(wchar_t const* str, size_t length); + static size_t narrow(wchar_t const* src_str, size_t src_len, char* dst_str, size_t dst_len); + static size_t narrow(std::wstring const& src, std::string& dst); + // + static std::wstring widen(char const* str, size_t length); + static size_t widen(char const* src_str, size_t src_len, wchar_t* dst_str, size_t dst_len); + static size_t widen(std::string const& src, std::wstring& dst); // convenience method, for the case it should be used in generic code (overloading) static std::string as_utf8(std::string const& str); diff --git a/VFS/vfs_init.cpp b/VFS/vfs_init.cpp index 6c354bae..a35def4e 100644 --- a/VFS/vfs_init.cpp +++ b/VFS/vfs_init.cpp @@ -10,12 +10,14 @@ #include "PropertyContainer.h" #include "Tools/Log.h" -//#define LOG_VFS_INITIALIZATION +#define LOG_VFS_INITIALIZATION #ifdef LOG_VFS_INITIALIZATION -#define LOG(x) (x) +#define LOG(x) x #else #define LOG(x) #endif + +extern bool g_VFS_NO_UNICODE; /********************************************************************/ /********************************************************************/ @@ -41,8 +43,9 @@ bool InitVirtualFileSystem(CPropertyContainer& oVFSProps) vfs::CVirtualFileSystem *pVirtFileSys = GetVFS(); - LOG(_LOG << "Initializing Virtual File System"); - LOG(_LOG.Endl()); + LOG(_LOG << "Initializing Virtual File System" << CLog::endl); + + if(g_VFS_NO_UNICODE){ LOG(_LOG.Endl() << "UNICODE disabled" << CLog::endl); } LOG(_LOG.Endl() << "reading profiles .. "); std::list lProfiles, lLocSections;