diff --git a/Minecraft.Client/BufferedImage.cpp b/Minecraft.Client/BufferedImage.cpp index 8777d307b..87c5ddf3f 100644 --- a/Minecraft.Client/BufferedImage.cpp +++ b/Minecraft.Client/BufferedImage.cpp @@ -160,15 +160,15 @@ BufferedImage::BufferedImage(const wstring& File, bool filenameHasExtension /*=f name = wDrive + L"res" + filePath.substr(0,filePath.length()-4) + mipMapPath + L".png"; } - const char *pchTextureName=wstringtofilename(name); + auto pchTextureName=wstringtofilename(name); #ifndef _CONTENT_PACKAGE - app.DebugPrintf("\n--- Loading TEXTURE - %s\n\n",pchTextureName); + app.DebugPrintf("\n--- Loading TEXTURE - %s\n\n",pchTextureName.c_str()); #endif D3DXIMAGE_INFO ImageInfo; ZeroMemory(&ImageInfo,sizeof(D3DXIMAGE_INFO)); - hr=RenderManager.LoadTextureData(pchTextureName,&ImageInfo,&data[l]); + hr=RenderManager.LoadTextureData(pchTextureName.c_str(), &ImageInfo, &data[l]); if(hr!=ERROR_SUCCESS) diff --git a/Minecraft.Client/Common/GameRules/LevelGenerationOptions.cpp b/Minecraft.Client/Common/GameRules/LevelGenerationOptions.cpp index 2af1826cf..1c08999e9 100644 --- a/Minecraft.Client/Common/GameRules/LevelGenerationOptions.cpp +++ b/Minecraft.Client/Common/GameRules/LevelGenerationOptions.cpp @@ -588,9 +588,9 @@ int LevelGenerationOptions::packMounted(LPVOID pParam,int iPad,DWORD dwErr,DWORD nullptr // Unsupported ); #else - const char *pchFilename=wstringtofilename(grf.getPath()); + std::string pchFilename=wstringtofilename(grf.getPath()); HANDLE fileHandle = CreateFile( - pchFilename, // file name + pchFilename.c_str(), // file name GENERIC_READ, // access mode 0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but... nullptr, // Unused @@ -640,9 +640,9 @@ int LevelGenerationOptions::packMounted(LPVOID pParam,int iPad,DWORD dwErr,DWORD nullptr // Unsupported ); #else - const char *pchFilename=wstringtofilename(save.getPath()); + auto pchFilename=wstringtofilename(save.getPath()); HANDLE fileHandle = CreateFile( - pchFilename, // file name + pchFilename.c_str(), // file name GENERIC_READ, // access mode 0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but... nullptr, // Unused diff --git a/Minecraft.Client/Common/Network/GameNetworkManager.cpp b/Minecraft.Client/Common/Network/GameNetworkManager.cpp index a502dbfb7..6a1499a29 100644 --- a/Minecraft.Client/Common/Network/GameNetworkManager.cpp +++ b/Minecraft.Client/Common/Network/GameNetworkManager.cpp @@ -259,9 +259,9 @@ bool CGameNetworkManager::StartNetworkGame(Minecraft *minecraft, LPVOID lpParame nullptr // Unsupported ); #else - const char *pchFilename=wstringtofilename(grf.getPath()); + std::string pchFilename=wstringtofilename(grf.getPath()); HANDLE fileHandle = CreateFile( - pchFilename, // file name + pchFilename.c_str(), // file name GENERIC_READ, // access mode 0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but... nullptr, // Unused diff --git a/Minecraft.Client/DLCTexturePack.cpp b/Minecraft.Client/DLCTexturePack.cpp index f1304a9ef..4fb4e0175 100644 --- a/Minecraft.Client/DLCTexturePack.cpp +++ b/Minecraft.Client/DLCTexturePack.cpp @@ -368,9 +368,9 @@ int DLCTexturePack::packMounted(LPVOID pParam,int iPad,DWORD dwErr,DWORD dwLicen nullptr // Unsupported ); #else - const char *pchFilename=wstringtofilename(grf.getPath()); + std::string pchFilename=wstringtofilename(grf.getPath()); HANDLE fileHandle = CreateFile( - pchFilename, // file name + pchFilename.c_str(), // file name GENERIC_READ, // access mode 0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but... nullptr, // Unused @@ -420,9 +420,9 @@ int DLCTexturePack::packMounted(LPVOID pParam,int iPad,DWORD dwErr,DWORD dwLicen nullptr // Unsupported ); #else - const char *pchFilename=wstringtofilename(grf.getPath()); + auto pchFilename=wstringtofilename(grf.getPath()); HANDLE fileHandle = CreateFile( - pchFilename, // file name + pchFilename.c_str(), // file name GENERIC_READ, // access mode 0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but... nullptr, // Unused diff --git a/Minecraft.Client/Settings.cpp b/Minecraft.Client/Settings.cpp index 9d93e6cbe..99da99689 100644 --- a/Minecraft.Client/Settings.cpp +++ b/Minecraft.Client/Settings.cpp @@ -81,8 +81,8 @@ void Settings::saveProperties() stream << "# MinecraftConsoles dedicated server properties\r\n"; for (unordered_map::const_iterator it = properties.begin(); it != properties.end(); ++it) { - string key = string(wstringtochararray(it->first)); - string value = string(wstringtochararray(it->second)); + string key = wstringtofilename(it->first); + string value = wstringtofilename(it->second); stream << key << "=" << value << "\r\n"; } } diff --git a/Minecraft.Client/Textures.cpp b/Minecraft.Client/Textures.cpp index ba7c02821..824a02654 100644 --- a/Minecraft.Client/Textures.cpp +++ b/Minecraft.Client/Textures.cpp @@ -1400,7 +1400,7 @@ BufferedImage *Textures::readImage(TEXTURE_NAME texId, const wstring& name) // 4 } else { - const char *pchName=wstringtofilename(name); + std::string pchName=wstringtofilename(name); #ifdef __PS3__ if(app.GetBootedFromDiscPatch() && app.IsFileInPatchList(pchName)) { @@ -1423,7 +1423,7 @@ BufferedImage *Textures::readImage(TEXTURE_NAME texId, const wstring& name) // 4 drive = skins->getDefault()->getPath(isTu); } - const char *pchDrive=wstringtofilename(drive); + std::string pchDrive=wstringtofilename(drive); if(IsOriginalImage(texId, name) || isTu) { diff --git a/Minecraft.Client/Windows64/Windows64_Minecraft.cpp b/Minecraft.Client/Windows64/Windows64_Minecraft.cpp index 70aeb22bf..9e32f0a24 100644 --- a/Minecraft.Client/Windows64/Windows64_Minecraft.cpp +++ b/Minecraft.Client/Windows64/Windows64_Minecraft.cpp @@ -1390,14 +1390,14 @@ static int RunHeadlessServer() Settings serverSettings(new File(L"server.properties")); const wstring configuredBindIp = serverSettings.getString(L"server-ip", L""); - const char* bindIp = "*"; + std::string bindIp = "*"; if (g_Win64DedicatedServerBindIP[0] != 0) { bindIp = g_Win64DedicatedServerBindIP; } else if (!configuredBindIp.empty()) { - bindIp = wstringtochararray(configuredBindIp); + bindIp = wstringtostring(configuredBindIp); } const int port = g_Win64DedicatedServerPort > 0 ? g_Win64DedicatedServerPort : serverSettings.getInt(L"server-port", WIN64_NET_DEFAULT_PORT); diff --git a/Minecraft.World/Connection.cpp b/Minecraft.World/Connection.cpp index d44502667..5f744fac3 100644 --- a/Minecraft.World/Connection.cpp +++ b/Minecraft.World/Connection.cpp @@ -105,11 +105,11 @@ Connection::Connection(Socket *socket, const wstring& id, PacketListener *packet m_hWakeReadThread = new C4JThread::Event; m_hWakeWriteThread = new C4JThread::Event; - const char *szId = wstringtofilename(id); + std::string szId = wstringtofilename(id); char readThreadName[256]; char writeThreadName[256]; - sprintf_s(readThreadName, sizeof(readThreadName), "%.240s read\n", szId); - sprintf_s(writeThreadName, sizeof(writeThreadName), "%.240s write\n", szId); + sprintf_s(readThreadName, sizeof(readThreadName), "%.240s read\n", szId.c_str()); + sprintf_s(writeThreadName, sizeof(writeThreadName), "%.240s write\n", szId.c_str()); readThread = new C4JThread(runRead, static_cast(this), readThreadName, READ_STACK_SIZE); writeThread = new C4JThread(runWrite, this, writeThreadName, WRITE_STACK_SIZE); diff --git a/Minecraft.World/ConsoleSaveFileOriginal.cpp b/Minecraft.World/ConsoleSaveFileOriginal.cpp index 8a7d1f9ec..404cdce5b 100644 --- a/Minecraft.World/ConsoleSaveFileOriginal.cpp +++ b/Minecraft.World/ConsoleSaveFileOriginal.cpp @@ -881,10 +881,10 @@ void ConsoleSaveFileOriginal::DebugFlushToFile(void *compressedData /*= nullptr* wstring wtemp = targetFileDir.getPath() + wstring(fileName); LPCWSTR lpFileName = wtemp.c_str(); #else - LPCSTR lpFileName = wstringtofilename( targetFileDir.getPath() + wstring(fileName) ); + std::string lpFileName = wstringtofilename( targetFileDir.getPath() + wstring(fileName) ); #endif #ifndef __PSVITA__ - HANDLE hSaveFile = CreateFile( lpFileName, GENERIC_WRITE, 0, nullptr, OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, nullptr); + HANDLE hSaveFile = CreateFile( lpFileName.c_str(), GENERIC_WRITE, 0, nullptr, OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, nullptr); #endif if(compressedData != nullptr && compressedDataSize > 0) diff --git a/Minecraft.World/File.cpp b/Minecraft.World/File.cpp index 11871e426..cca01e3ee 100644 --- a/Minecraft.World/File.cpp +++ b/Minecraft.World/File.cpp @@ -37,7 +37,7 @@ File::File( const wstring& pathname ) //: parent( nullptr ) m_abstractPathName = pathname; #ifdef _WINDOWS64 - string path = wstringtochararray(m_abstractPathName); + string path = wstringtofilename(m_abstractPathName); string finalPath = StorageManager.GetMountedPath(path.c_str()); if(finalPath.size() == 0) finalPath = path; m_abstractPathName = convStringToWstring(finalPath); @@ -100,7 +100,7 @@ bool File::_delete() #ifdef _UNICODE BOOL result = DeleteFile( getPath().c_str() ); #else - BOOL result = DeleteFile( wstringtofilename(getPath()) ); + BOOL result = DeleteFile( wstringtofilename(getPath()).c_str() ); #endif if( result == 0 ) { @@ -123,7 +123,7 @@ bool File::mkdir() const return CreateDirectory( getPath().c_str(), nullptr) != 0; #else - return CreateDirectory( wstringtofilename(getPath()), nullptr) != 0; + return CreateDirectory( wstringtofilename(getPath()).c_str(), nullptr) != 0; #endif } @@ -175,9 +175,10 @@ bool File::mkdirs() const } } #else - if( GetFileAttributes( wstringtofilename(pathToHere) ) == -1 ) + auto fileName = wstringtofilename(pathToHere); + if( GetFileAttributes( fileName.c_str() ) == -1 ) { - DWORD result = CreateDirectory( wstringtofilename(pathToHere), nullptr); + DWORD result = CreateDirectory( fileName.c_str(), nullptr); if( result == 0 ) { // Failed to create @@ -210,7 +211,7 @@ bool File::exists() const return GetFileAttributes( getPath().c_str() ) != -1; #else - return GetFileAttributes( wstringtofilename(getPath()) ) != -1; + return GetFileAttributes( wstringtofilename(getPath()).c_str() ) != -1; #endif } @@ -237,12 +238,12 @@ bool File::renameTo(File dest) // called, therefore we were getting sourcePath and destPath having the same value. The solution here is to // make a copy of the sourcePath by storing it in a std::string std::string sourcePath = wstringtofilename(getPath()); - const char *destPath = wstringtofilename(dest.getPath()); + std::string destPath = wstringtofilename(dest.getPath()); #ifdef _DURANGO __debugbreak(); // TODO BOOL result = false; #else - BOOL result = MoveFile(sourcePath.c_str(), destPath); + BOOL result = MoveFile(sourcePath.c_str(), destPath.c_str()); #endif if( result == 0 ) @@ -386,7 +387,7 @@ std::vector *File::listFiles() const } #else char path[MAX_PATH] {}; - snprintf( path, MAX_PATH, "%s\\*", wstringtofilename( getPath() ) ); + snprintf( path, MAX_PATH, "%s\\*", wstringtofilename( getPath() ).c_str() ); HANDLE hFind = FindFirstFile( path, &wfd); if(hFind != INVALID_HANDLE_VALUE) { @@ -513,7 +514,7 @@ bool File::isDirectory() const #ifdef _UNICODE return exists() && ( GetFileAttributes( getPath().c_str() ) & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY; #else - return exists() && ( GetFileAttributes( wstringtofilename(getPath()) ) & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY; + return exists() && ( GetFileAttributes( wstringtofilename(getPath()).c_str() ) & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY; #endif } @@ -596,7 +597,7 @@ int64_t File::length() ); #else BOOL result = GetFileAttributesEx( - wstringtofilename(getPath()), // file or directory name + wstringtofilename(getPath()).c_str(), // file or directory name GetFileExInfoStandard, // attribute &fileInfoBuffer // attribute information ); @@ -634,7 +635,7 @@ int64_t File::lastModified() ); #else BOOL result = GetFileAttributesEx( - wstringtofilename(getPath()), // file or directory name + wstringtofilename(getPath()).c_str(), // file or directory name GetFileExInfoStandard, // attribute &fileInfoBuffer // attribute information ); diff --git a/Minecraft.World/FileInputStream.cpp b/Minecraft.World/FileInputStream.cpp index 7c34a22f3..ca94cfceb 100644 --- a/Minecraft.World/FileInputStream.cpp +++ b/Minecraft.World/FileInputStream.cpp @@ -18,7 +18,7 @@ //SecurityException - if a security manager exists and its checkRead method denies read access to the file. FileInputStream::FileInputStream(const File &file) { - const char *pchFilename=wstringtofilename(file.getPath()); + std::string pchFilename=wstringtofilename(file.getPath()); #ifdef _UNICODE m_fileHandle = CreateFile( file.getPath().c_str(), // file name @@ -31,7 +31,7 @@ FileInputStream::FileInputStream(const File &file) ); #else m_fileHandle = CreateFile( - pchFilename, // file name + pchFilename.c_str(), // file name GENERIC_READ, // access mode 0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but... nullptr, // Unused diff --git a/Minecraft.World/FileOutputStream.cpp b/Minecraft.World/FileOutputStream.cpp index 58a0ecd96..86a159ab5 100644 --- a/Minecraft.World/FileOutputStream.cpp +++ b/Minecraft.World/FileOutputStream.cpp @@ -31,7 +31,7 @@ FileOutputStream::FileOutputStream(const File &file) : m_fileHandle( INVALID_HAN ); #else m_fileHandle = CreateFile( - wstringtofilename(file.getPath()) , // file name + wstringtofilename(file.getPath()).c_str(), // file name GENERIC_WRITE, // access mode 0, // share mode // TODO 4J Stu - Will we need to share file? Probably not but... nullptr, // Unused diff --git a/Minecraft.World/StringHelpers.cpp b/Minecraft.World/StringHelpers.cpp index b56a85f14..d95f1e1aa 100644 --- a/Minecraft.World/StringHelpers.cpp +++ b/Minecraft.World/StringHelpers.cpp @@ -46,13 +46,11 @@ wstring convStringToWstring(const string& converting) return converted; } -// Convert for filename wstrings to a straight character pointer for Xbox APIs. The returned string is only valid until -// this function is called again, and it isn't thread-safe etc. as I'm just storing the returned name in a local static -// to save having to clear it up everywhere this is used. -const char *wstringtofilename(const wstring& name) +const std::string wstringtofilename(const wstring& name) { - static char buf[256]; assert(name.length()<256); + std::string buf; + buf.reserve(name.length()); for(unsigned int i = 0; i < name.length(); i++ ) { wchar_t c = name[i]; @@ -62,23 +60,36 @@ const char *wstringtofilename(const wstring& name) if(c=='/') c='\\'; #endif assert(c<128); // Will we have to do any conversion of non-ASCII characters in filenames? - buf[i] = static_cast(c); + buf += static_cast(c); } - buf[name.length()] = 0; return buf; } -const char *wstringtochararray(const wstring& name) +const std::string wstringtostring(const wstring& name) +{ + assert(name.length() < 256); + std::string buf; + buf.reserve(name.length()); + for (unsigned int i = 0; i < name.length(); i++) + { + wchar_t c = name[i]; + assert(c < 128); // Will we have to do any conversion of non-ASCII characters in filenames? + buf += static_cast(c); + } + return buf; +} + +const vector wstringtochararray(const wstring& name) { - static char buf[256]; assert(name.length()<256); + vector buf; + buf.reserve(name.length()); for(unsigned int i = 0; i < name.length(); i++ ) { wchar_t c = name[i]; assert(c<128); // Will we have to do any conversion of non-ASCII characters in filenames? buf[i] = static_cast(c); } - buf[name.length()] = 0; return buf; } diff --git a/Minecraft.World/StringHelpers.h b/Minecraft.World/StringHelpers.h index dee676bae..4844313eb 100644 --- a/Minecraft.World/StringHelpers.h +++ b/Minecraft.World/StringHelpers.h @@ -23,8 +23,9 @@ template T _fromHEXString(const std::wstring& s) } wstring convStringToWstring(const string& converting); -const char *wstringtofilename(const wstring& name); -const char *wstringtochararray(const wstring& name); +const std::string wstringtofilename(const wstring& name); +const std::string wstringtostring(const wstring& name); +const vector wstringtochararray(const wstring& name); wstring filenametowstring(const char *name); std::vector &stringSplit(const std::wstring &s, wchar_t delim, std::vector &elems); diff --git a/Minecraft.World/ZipFile.cpp b/Minecraft.World/ZipFile.cpp index 8e82111cc..1ab8d226b 100644 --- a/Minecraft.World/ZipFile.cpp +++ b/Minecraft.World/ZipFile.cpp @@ -60,8 +60,8 @@ std::vector ZipFile::extract(const std::wstring* name) { return buffer; } catch (exception e) { - OutputDebugString(wstringtochararray(L"Error extracting file from zip: " + *name + L'\n')); - OutputDebugString(wstringtochararray(L"Exception: " + convStringToWstring(e.what()) + L'\n')); + app.DebugPrintf(wstringtostring(L"Error extracting file from zip: " + *name + L'\n').c_str()); + app.DebugPrintf(wstringtostring(L"Exception: " + convStringToWstring(e.what()) + L'\n').c_str()); __debugbreak(); return {}; } @@ -132,8 +132,8 @@ bool ZipFile::open(File* file) { } } catch (exception e) { - OutputDebugString(wstringtochararray(L"Error opening zip file: " + file->getPath())); - OutputDebugString(wstringtochararray(L"\nException: " + convStringToWstring(e.what()))); + app.DebugPrintf(wstringtostring(L"Error opening zip file: " + file->getPath()).c_str()); + app.DebugPrintf(wstringtostring(L"\nException: " + convStringToWstring(e.what())).c_str()); __debugbreak(); } return false;