Core/Misc: Fixed windows _UNICODE incompatibilities

This commit is contained in:
Shauren
2024-06-02 14:21:13 +02:00
parent 4f6b49cccf
commit fd4ffc81b2
6 changed files with 81 additions and 50 deletions

View File

@@ -41,7 +41,7 @@ inline LPTSTR ErrorMessage(DWORD dw)
else
{
LPTSTR msgBuf = (LPTSTR)LocalAlloc(LPTR, 30);
snprintf(msgBuf, 30, "Unknown error: %u", dw);
_sntprintf(msgBuf, 30, _T("Unknown error: %d"), (int)dw);
return msgBuf;
}
@@ -125,19 +125,25 @@ PEXCEPTION_POINTERS pExceptionInfo)
++pos;
TCHAR crash_folder_path[MAX_PATH];
_stprintf_s(crash_folder_path, "%s\\%s", module_folder_name, CrashFolder);
_stprintf_s(crash_folder_path, _T("%s\\%s"), module_folder_name, CrashFolder);
if (!CreateDirectory(crash_folder_path, nullptr))
{
if (GetLastError() != ERROR_ALREADY_EXISTS)
return 0;
}
#ifdef _UNICODE
#define PRSTRc "S"
#else
#define PRSTRc "s"
#endif
SYSTEMTIME systime;
GetLocalTime(&systime);
_stprintf_s(m_szDumpFileName, "%s\\%s_%s_[%u-%u_%u-%u-%u].dmp",
_stprintf_s(m_szDumpFileName, _T("%s\\%" PRSTRc "_%s_[%u-%u_%u-%u-%u].dmp"),
crash_folder_path, GitRevision::GetHash(), pos, systime.wDay, systime.wMonth, systime.wHour, systime.wMinute, systime.wSecond);
_stprintf_s(m_szLogFileName, _T("%s\\%s_%s_[%u-%u_%u-%u-%u].txt"),
_stprintf_s(m_szLogFileName, _T("%s\\%" PRSTRc "_%s_[%u-%u_%u-%u-%u].txt"),
crash_folder_path, GitRevision::GetHash(), pos, systime.wDay, systime.wMonth, systime.wHour, systime.wMinute, systime.wSecond);
m_hDumpFile = CreateFile(m_szDumpFileName,
@@ -226,7 +232,7 @@ template<size_t size>
void ToTchar(wchar_t const* src, TCHAR (&dst)[size])
{
if constexpr (std::is_same_v<TCHAR, char>)
::wcstombs_s(nullptr, dst, src, size);
::wcstombs_s(nullptr, dst, src, _TRUNCATE);
else
::wcscpy_s(dst, size, src);
}
@@ -551,7 +557,7 @@ BOOL WheatyExceptionReport::_GetWindowsVersionFromWMI(TCHAR* szVersion, DWORD cn
_tcsncat(szVersion, _T(" "), cntMax);
memset(buf, 0, sizeof(buf));
ToTchar(field.bstrVal, buf);
if (strlen(buf))
if (_tcslen(buf))
_tcsncat(szVersion, buf, cntMax);
}
VariantClear(&field);

View File

@@ -19,6 +19,7 @@
#include "Log.h"
#include <cstring>
#include <tchar.h>
#include <windows.h>
#include <winsvc.h>
@@ -31,9 +32,9 @@
#endif
extern int main(int argc, char ** argv);
extern char serviceLongName[];
extern char serviceName[];
extern char serviceDescription[];
extern TCHAR serviceLongName[];
extern TCHAR serviceName[];
extern TCHAR serviceDescription[];
extern int m_ServiceStatus;
@@ -49,11 +50,11 @@ bool WinServiceInstall()
if (serviceControlManager)
{
char path[_MAX_PATH + 10];
TCHAR path[_MAX_PATH + 10];
if (GetModuleFileName( nullptr, path, sizeof(path)/sizeof(path[0]) ) > 0)
{
SC_HANDLE service;
std::strcat(path, " --service run");
_tcscat(path, _T(" --service run"));
service = CreateService(serviceControlManager,
serviceName, // name of service
serviceLongName, // service name to display
@@ -70,7 +71,7 @@ bool WinServiceInstall()
nullptr); // no password
if (service)
{
HMODULE advapi32 = GetModuleHandle("ADVAPI32.DLL");
HMODULE advapi32 = GetModuleHandle(_T("ADVAPI32.DLL"));
if (!advapi32)
{
CloseServiceHandle(service);
@@ -182,7 +183,16 @@ void WINAPI ServiceControlHandler(DWORD controlCode)
SetServiceStatus(serviceStatusHandle, &serviceStatus);
}
void WINAPI ServiceMain(DWORD argc, char *argv[])
template<size_t size>
void TCharToChar(TCHAR const* src, char(&dst)[size])
{
if constexpr (std::is_same_v<TCHAR, char>)
::strcpy_s(dst, src, size);
else
::wcstombs_s(nullptr, dst, src, _TRUNCATE);
}
void WINAPI ServiceMain(DWORD /*argc*/, TCHAR *argv[])
{
// initialise service status
serviceStatus.dwServiceType = SERVICE_WIN32;
@@ -197,12 +207,12 @@ void WINAPI ServiceMain(DWORD argc, char *argv[])
if ( serviceStatusHandle )
{
char path[_MAX_PATH + 1];
TCHAR path[_MAX_PATH + 1];
unsigned int i, last_slash = 0;
GetModuleFileName(nullptr, path, sizeof(path)/sizeof(path[0]));
size_t pathLen = std::strlen(path);
size_t pathLen = _tcslen(path);
for (i = 0; i < pathLen; i++)
{
if (path[i] == '\\') last_slash = i;
@@ -227,8 +237,12 @@ void WINAPI ServiceMain(DWORD argc, char *argv[])
////////////////////////
m_ServiceStatus = 1;
argc = 1;
main(argc, argv);
char cArg[_MAX_PATH + 1];
TCharToChar(argv[0], cArg);
char* cArgv[] = { cArg };
main(1, cArgv);
// service was stopped
serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;

View File

@@ -66,9 +66,10 @@ namespace fs = boost::filesystem;
#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
#include "ServiceWin32.h"
char serviceName[] = "bnetserver";
char serviceLongName[] = "TrinityCore bnet service";
char serviceDescription[] = "TrinityCore Battle.net emulator authentication service";
#include <tchar.h>
TCHAR serviceName[] = _T("bnetserver");
TCHAR serviceLongName[] = _T("TrinityCore bnet service");
TCHAR serviceDescription[] = _T("TrinityCore Battle.net emulator authentication service");
/*
* -1 - not in service mode
* 0 - stopped

View File

@@ -81,9 +81,10 @@ namespace fs = boost::filesystem;
#ifdef _WIN32
#include "ServiceWin32.h"
char serviceName[] = "worldserver";
char serviceLongName[] = "TrinityCore world service";
char serviceDescription[] = "TrinityCore World of Warcraft emulator world service";
#include <tchar.h>
TCHAR serviceName[] = _T("worldserver");
TCHAR serviceLongName[] = _T("TrinityCore world service");
TCHAR serviceDescription[] = _T("TrinityCore World of Warcraft emulator world service");
/*
* -1 - not in service mode
* 0 - stopped

View File

@@ -157,11 +157,16 @@ namespace
}
}
CASC::Storage::Storage(HANDLE handle) : _handle(handle)
namespace CASC
{
using CASCCharType = std::remove_const_t<std::remove_pointer_t<decltype(CASC_OPEN_STORAGE_ARGS::szLocalPath)>>;
using CASCStringType = std::basic_string<CASCCharType>;
Storage::Storage(HANDLE handle) : _handle(handle)
{
}
bool CASC::Storage::LoadOnlineTactKeys()
bool Storage::LoadOnlineTactKeys()
{
// attempt to download only once, not every storage opening
static Optional<std::string> const tactKeys = DownloadFile("raw.githubusercontent.com", 443, "/wowdev/TACTKeys/master/WoW.txt");
@@ -169,21 +174,22 @@ bool CASC::Storage::LoadOnlineTactKeys()
return tactKeys && CascImportKeysFromString(_handle, tactKeys->c_str());
}
CASC::Storage::~Storage()
Storage::~Storage()
{
::CascCloseStorage(_handle);
}
CASC::Storage* CASC::Storage::Open(boost::filesystem::path const& path, uint32 localeMask, char const* product)
Storage* Storage::Open(boost::filesystem::path const& path, uint32 localeMask, char const* product)
{
std::string strPath = path.string();
CASCStringType strPath = path.template string<CASCStringType>();
CASCStringType strProduct(product, product + strlen(product)); // dumb conversion from char to wchar, always ascii
CASC_OPEN_STORAGE_ARGS args = {};
args.Size = sizeof(CASC_OPEN_STORAGE_ARGS);
args.szLocalPath = strPath.c_str();
args.szCodeName = product;
args.szCodeName = strProduct.c_str();
args.dwLocaleMask = localeMask;
HANDLE handle = nullptr;
if (!::CascOpenStorageEx(nullptr, &args, false, &handle))
if (!CascOpenStorageEx(nullptr, &args, false, &handle))
{
DWORD lastError = GetCascError(); // support checking error set by *Open* call, not the next *Close*
printf("Error opening casc storage '%s': %s\n", path.string().c_str(), HumanReadableCASCError(lastError));
@@ -201,14 +207,16 @@ CASC::Storage* CASC::Storage::Open(boost::filesystem::path const& path, uint32 l
return storage;
}
CASC::Storage* CASC::Storage::OpenRemote(boost::filesystem::path const& path, uint32 localeMask, char const* product, char const* region)
Storage* Storage::OpenRemote(boost::filesystem::path const& path, uint32 localeMask, char const* product, char const* region)
{
std::string strPath = path.string();
CASCStringType strPath = path.template string<CASCStringType>();
CASCStringType strProduct(product, product + strlen(product)); // dumb conversion from char to wchar, always ascii
CASCStringType strRegion(region, region + strlen(region)); // dumb conversion from char to wchar, always ascii
CASC_OPEN_STORAGE_ARGS args = {};
args.Size = sizeof(CASC_OPEN_STORAGE_ARGS);
args.szLocalPath = strPath.c_str();
args.szCodeName = product;
args.szRegion = region;
args.szCodeName = strProduct.c_str();
args.szRegion = strRegion.c_str();
args.dwLocaleMask = localeMask;
HANDLE handle = nullptr;
@@ -224,7 +232,7 @@ CASC::Storage* CASC::Storage::OpenRemote(boost::filesystem::path const& path, ui
DWORD features = 0;
if (!GetStorageInfo(handle, CascStorageFeatures, &features) || !(features & CASC_FEATURE_ONLINE))
{
printf("Local casc storage detected in cache path \"%s\" (or its parent directory). Remote storage not opened!\n", args.szLocalPath);
printf("Local casc storage detected in cache path \"%s\" (or its parent directory). Remote storage not opened!\n", path.string().c_str());
CascCloseStorage(handle);
SetCascError(ERROR_FILE_OFFLINE);
return nullptr;
@@ -239,7 +247,7 @@ CASC::Storage* CASC::Storage::OpenRemote(boost::filesystem::path const& path, ui
return storage;
}
uint32 CASC::Storage::GetBuildNumber() const
uint32 Storage::GetBuildNumber() const
{
CASC_STORAGE_PRODUCT product;
if (GetStorageInfo(_handle, CascStorageProduct, &product))
@@ -248,7 +256,7 @@ uint32 CASC::Storage::GetBuildNumber() const
return 0;
}
uint32 CASC::Storage::GetInstalledLocalesMask() const
uint32 Storage::GetInstalledLocalesMask() const
{
DWORD locales;
if (GetStorageInfo(_handle, CascStorageInstalledLocales, &locales))
@@ -257,12 +265,12 @@ uint32 CASC::Storage::GetInstalledLocalesMask() const
return 0;
}
bool CASC::Storage::HasTactKey(uint64 keyLookup) const
bool Storage::HasTactKey(uint64 keyLookup) const
{
return CascFindEncryptionKey(_handle, keyLookup) != nullptr;
}
CASC::File* CASC::Storage::OpenFile(char const* fileName, uint32 localeMask, bool printErrors /*= false*/, bool zerofillEncryptedParts /*= false*/) const
File* Storage::OpenFile(char const* fileName, uint32 localeMask, bool printErrors /*= false*/, bool zerofillEncryptedParts /*= false*/) const
{
DWORD openFlags = CASC_OPEN_BY_NAME;
if (zerofillEncryptedParts)
@@ -283,7 +291,7 @@ CASC::File* CASC::Storage::OpenFile(char const* fileName, uint32 localeMask, boo
return new File(handle);
}
CASC::File* CASC::Storage::OpenFile(uint32 fileDataId, uint32 localeMask, bool printErrors /*= false*/, bool zerofillEncryptedParts /*= false*/) const
File* Storage::OpenFile(uint32 fileDataId, uint32 localeMask, bool printErrors /*= false*/, bool zerofillEncryptedParts /*= false*/) const
{
DWORD openFlags = CASC_OPEN_BY_FILEID;
if (zerofillEncryptedParts)
@@ -304,16 +312,16 @@ CASC::File* CASC::Storage::OpenFile(uint32 fileDataId, uint32 localeMask, bool p
return new File(handle);
}
CASC::File::File(HANDLE handle) : _handle(handle)
File::File(HANDLE handle) : _handle(handle)
{
}
CASC::File::~File()
File::~File()
{
::CascCloseFile(_handle);
}
uint32 CASC::File::GetId() const
uint32 File::GetId() const
{
CASC_FILE_FULL_INFO info;
if (!::CascGetFileInfo(_handle, CascFileFullInfo, &info, sizeof(info), nullptr))
@@ -322,7 +330,7 @@ uint32 CASC::File::GetId() const
return info.FileDataId;
}
int64 CASC::File::GetSize() const
int64 File::GetSize() const
{
ULONGLONG size;
if (!::CascGetFileSize64(_handle, &size))
@@ -331,7 +339,7 @@ int64 CASC::File::GetSize() const
return int64(size);
}
int64 CASC::File::GetPointer() const
int64 File::GetPointer() const
{
ULONGLONG position;
if (!::CascSetFilePointer64(_handle, 0, &position, FILE_CURRENT))
@@ -340,14 +348,14 @@ int64 CASC::File::GetPointer() const
return int64(position);
}
bool CASC::File::SetPointer(int64 position)
bool File::SetPointer(int64 position)
{
LONG parts[2];
memcpy(parts, &position, sizeof(parts));
return ::CascSetFilePointer64(_handle, position, nullptr, FILE_BEGIN);
}
bool CASC::File::ReadFile(void* buffer, uint32 bytes, uint32* bytesRead)
bool File::ReadFile(void* buffer, uint32 bytes, uint32* bytesRead)
{
DWORD bytesReadDWORD;
if (!::CascReadFile(_handle, buffer, bytes, &bytesReadDWORD))
@@ -358,3 +366,4 @@ bool CASC::File::ReadFile(void* buffer, uint32 bytes, uint32* bytesRead)
return true;
}
}

View File

@@ -85,12 +85,12 @@ namespace MMAP
{
#ifdef WIN32
HANDLE hFind;
WIN32_FIND_DATA findFileInfo;
WIN32_FIND_DATAA findFileInfo;
std::string directory;
directory = dirpath + "/" + filter;
hFind = FindFirstFile(directory.c_str(), &findFileInfo);
hFind = FindFirstFileA(directory.c_str(), &findFileInfo);
if (hFind == INVALID_HANDLE_VALUE)
return LISTFILE_DIRECTORY_NOT_FOUND;
@@ -99,7 +99,7 @@ namespace MMAP
if (strcmp(findFileInfo.cFileName, ".") != 0 && strcmp(findFileInfo.cFileName, "..") != 0)
fileList.push_back(std::string(findFileInfo.cFileName));
}
while (FindNextFile(hFind, &findFileInfo));
while (FindNextFileA(hFind, &findFileInfo));
FindClose(hFind);