core: Add Insurgency support for amd64 Windows (#1295)
* Add basic Insurgency support on Windows x64 This allows SourceMod to load on x64 Insurgency. There are still a lot of variable truncation warnings that have to be dealt with. * Fix 32bit builds * Compile MySQL extension as well The hack for __iob_func being removed from the core runtime, but required by the old mysql we're building against can be simplified a lot due to the `_ReturnAddress` intrinsic available since MSVC 2015. * Don't include the offset we want to extract in the signature
This commit is contained in:
@@ -1,79 +1,37 @@
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <Windows.h>
|
||||
#include <stdlib.h>
|
||||
#include <corecrt_wstdio.h>
|
||||
#include <intrin.h>
|
||||
|
||||
// Fix from from https://stackoverflow.com/a/34655235.
|
||||
//
|
||||
// __iob_func required by the MySQL we use,
|
||||
// but no longer exists in the VS 14.0+ crt.
|
||||
|
||||
#pragma comment(lib, "DbgHelp.lib")
|
||||
#pragma warning(disable:4091) // 'typedef ': ignored on left of '' when no variable is declared
|
||||
#include <DbgHelp.h>
|
||||
#include <corecrt_wstdio.h>
|
||||
|
||||
#define GET_CURRENT_CONTEXT(c, contextFlags) \
|
||||
do { \
|
||||
c.ContextFlags = contextFlags; \
|
||||
__asm call x \
|
||||
__asm x: pop eax \
|
||||
__asm mov c.Eip, eax \
|
||||
__asm mov c.Ebp, ebp \
|
||||
__asm mov c.Esp, esp \
|
||||
} while(0);
|
||||
|
||||
#pragma intrinsic(_ReturnAddress)
|
||||
|
||||
FILE * __cdecl __iob_func(void)
|
||||
{
|
||||
CONTEXT c = { 0 };
|
||||
STACKFRAME64 s = { 0 };
|
||||
DWORD imageType;
|
||||
HANDLE hThread = GetCurrentThread();
|
||||
HANDLE hProcess = GetCurrentProcess();
|
||||
|
||||
GET_CURRENT_CONTEXT(c, CONTEXT_FULL);
|
||||
|
||||
imageType = IMAGE_FILE_MACHINE_I386;
|
||||
s.AddrPC.Offset = c.Eip;
|
||||
s.AddrPC.Mode = AddrModeFlat;
|
||||
s.AddrFrame.Offset = c.Ebp;
|
||||
s.AddrFrame.Mode = AddrModeFlat;
|
||||
s.AddrStack.Offset = c.Esp;
|
||||
s.AddrStack.Mode = AddrModeFlat;
|
||||
|
||||
if (!StackWalk64(imageType, hProcess, hThread, &s, &c, NULL, SymFunctionTableAccess64, SymGetModuleBase64, NULL))
|
||||
unsigned char const * assembly = (unsigned char const *)_ReturnAddress();
|
||||
if (!assembly)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (s.AddrReturn.Offset == 0)
|
||||
|
||||
if (*assembly == 0x83 && *(assembly + 1) == 0xC0 && (*(assembly + 2) == 0x20 || *(assembly + 2) == 0x40))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
{
|
||||
unsigned char const * assembly = (unsigned char const *)(s.AddrReturn.Offset);
|
||||
|
||||
if (*assembly == 0x83 && *(assembly + 1) == 0xC0 && (*(assembly + 2) == 0x20 || *(assembly + 2) == 0x40))
|
||||
if (*(assembly + 2) == 32)
|
||||
{
|
||||
if (*(assembly + 2) == 32)
|
||||
{
|
||||
return (FILE*)((unsigned char *)stdout - 32);
|
||||
}
|
||||
if (*(assembly + 2) == 64)
|
||||
{
|
||||
return (FILE*)((unsigned char *)stderr - 64);
|
||||
}
|
||||
|
||||
return (FILE*)((unsigned char *)stdout - 32);
|
||||
}
|
||||
else
|
||||
if (*(assembly + 2) == 64)
|
||||
{
|
||||
return stdin;
|
||||
return (FILE*)((unsigned char *)stderr - 64);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
return stdin;
|
||||
}
|
||||
|
||||
// Adapted from dosmap.c in Visual Studio 12.0 CRT sources.
|
||||
|
||||
@@ -185,7 +185,7 @@ bool MyDatabase::DoSimpleQuery(const char *query)
|
||||
|
||||
IQuery *MyDatabase::DoQuery(const char *query)
|
||||
{
|
||||
if (mysql_real_query(m_mysql, query, strlen(query)) != 0)
|
||||
if (mysql_real_query(m_mysql, query, static_cast<unsigned long>(strlen(query))) != 0)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
@@ -216,7 +216,7 @@ bool MyDatabase::DoSimpleQueryEx(const char *query, size_t len)
|
||||
|
||||
IQuery *MyDatabase::DoQueryEx(const char *query, size_t len)
|
||||
{
|
||||
if (mysql_real_query(m_mysql, query, len) != 0)
|
||||
if (mysql_real_query(m_mysql, query, static_cast<unsigned long>(len)) != 0)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
@@ -258,7 +258,7 @@ IPreparedQuery *MyDatabase::PrepareQuery(const char *query, char *error, size_t
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (mysql_stmt_prepare(stmt, query, strlen(query)) != 0)
|
||||
if (mysql_stmt_prepare(stmt, query, static_cast<unsigned long>(strlen(query))) != 0)
|
||||
{
|
||||
if (error)
|
||||
{
|
||||
|
||||
@@ -224,7 +224,7 @@ void MyDriver::ShutdownThreadSafety()
|
||||
mysql_thread_end();
|
||||
}
|
||||
|
||||
unsigned int strncopy(char *dest, const char *src, size_t count)
|
||||
size_t strncopy(char *dest, const char *src, size_t count)
|
||||
{
|
||||
if (!count)
|
||||
{
|
||||
|
||||
@@ -88,6 +88,6 @@ private:
|
||||
|
||||
extern MyDriver g_MyDriver;
|
||||
|
||||
unsigned int strncopy(char *dest, const char *src, size_t count);
|
||||
size_t strncopy(char *dest, const char *src, size_t count);
|
||||
|
||||
#endif //_INCLUDE_SM_MYSQL_DRIVER_H_
|
||||
|
||||
@@ -66,7 +66,12 @@ void InitializeValveGlobals()
|
||||
{
|
||||
return;
|
||||
}
|
||||
#ifdef PLATFORM_X86
|
||||
g_ppGameRules = *reinterpret_cast<void ***>(addr + offset);
|
||||
#else
|
||||
int32_t varOffset = *(int32_t *) ((unsigned char *) addr + offset);
|
||||
g_ppGameRules = *reinterpret_cast<void ***>((unsigned char *) addr + offset + sizeof(int32_t) + varOffset);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user