AVA2 cross-platform client parity baseline
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
#include "CRC32.h"
|
||||
#include <cstdio>
|
||||
#include <vector>
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
struct CrcTable
|
||||
{
|
||||
DWORD v[256];
|
||||
CrcTable()
|
||||
{
|
||||
for (DWORD n = 0; n < 256; ++n)
|
||||
{
|
||||
DWORD c = n;
|
||||
for (int k = 0; k < 8; ++k)
|
||||
c = (c & 1) ? (0xEDB88320u ^ (c >> 1)) : (c >> 1);
|
||||
v[n] = c;
|
||||
}
|
||||
}
|
||||
};
|
||||
const CrcTable s_crcTable;
|
||||
}
|
||||
|
||||
#define CRCTable (s_crcTable.v)
|
||||
|
||||
#define DO1(buf, i) crc = CRCTable[(crc ^ buf[i]) & 0xff] ^ (crc >> 8)
|
||||
#define DO2(buf, i) DO1(buf, i); DO1(buf, i + 1);
|
||||
#define DO4(buf, i) DO2(buf, i); DO2(buf, i + 2);
|
||||
#define DO8(buf, i) DO4(buf, i); DO4(buf, i + 4);
|
||||
#define DO16(buf, i) DO8(buf, i); DO8(buf, i + 8);
|
||||
|
||||
DWORD GetCRC32(const char * buf, size_t len)
|
||||
{
|
||||
DWORD crc = 0xffffffff;
|
||||
|
||||
if (len >= 16)
|
||||
{
|
||||
do
|
||||
{
|
||||
DO16(buf, 0);
|
||||
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
} while (len >= 16);
|
||||
}
|
||||
|
||||
if (len != 0)
|
||||
{
|
||||
do
|
||||
{
|
||||
DO1(buf, 0);
|
||||
++buf;
|
||||
--len;
|
||||
} while (len > 0);
|
||||
}
|
||||
|
||||
crc ^= 0xffffffff;
|
||||
return crc;
|
||||
}
|
||||
|
||||
#ifndef UPPER
|
||||
#define UPPER(c) (((c)>='a' && (c) <= 'z') ? ((c)+('A'-'a')) : (c))
|
||||
#endif
|
||||
|
||||
#define DO1CI(buf, i) crc = CRCTable[(crc ^ UPPER(buf[i])) & 0xff] ^ (crc >> 8)
|
||||
#define DO2CI(buf, i) DO1CI(buf, i); DO1CI(buf, i + 1);
|
||||
#define DO4CI(buf, i) DO2CI(buf, i); DO2CI(buf, i + 2);
|
||||
#define DO8CI(buf, i) DO4CI(buf, i); DO4CI(buf, i + 4);
|
||||
#define DO16CI(buf, i) DO8CI(buf, i); DO8CI(buf, i + 8);
|
||||
|
||||
DWORD GetCaseCRC32(const char * buf, size_t len)
|
||||
{
|
||||
DWORD crc = 0xffffffff;
|
||||
|
||||
if (16 <= len)
|
||||
{
|
||||
do
|
||||
{
|
||||
DO16CI(buf, 0);
|
||||
|
||||
buf += 16;
|
||||
len -= 16;
|
||||
} while (len >= 16);
|
||||
}
|
||||
|
||||
if (0 != len)
|
||||
{
|
||||
do
|
||||
{
|
||||
DO1CI(buf, 0);
|
||||
|
||||
++buf;
|
||||
--len;
|
||||
} while (len > 0);
|
||||
}
|
||||
|
||||
crc ^= 0xffffffff;
|
||||
return crc;
|
||||
}
|
||||
|
||||
DWORD GetHFILECRC32(HANDLE hFile)
|
||||
{
|
||||
FILE* fp = static_cast<FILE*>(hFile);
|
||||
struct stat info{};
|
||||
if (!fp || fstat(fileno(fp), &info) != 0 || !S_ISREG(info.st_mode))
|
||||
return 0;
|
||||
const long pos = std::ftell(fp);
|
||||
if (pos < 0 || std::fseek(fp, 0, SEEK_END) != 0)
|
||||
return 0;
|
||||
const long size = std::ftell(fp);
|
||||
if (size < 0 || std::fseek(fp, 0, SEEK_SET) != 0)
|
||||
{
|
||||
std::fseek(fp, pos, SEEK_SET);
|
||||
return 0;
|
||||
}
|
||||
std::vector<char> data(size > 0 ? size : 0);
|
||||
const size_t got = data.empty() ? 0 : std::fread(data.data(), 1, data.size(), fp);
|
||||
std::fseek(fp, pos, SEEK_SET);
|
||||
if (got != data.size() || data.empty())
|
||||
return 0;
|
||||
return GetCRC32(data.data(), data.size());
|
||||
}
|
||||
|
||||
DWORD GetFileCRC32(const char* c_szFileName)
|
||||
{
|
||||
FILE* fp = std::fopen(c_szFileName, "rb");
|
||||
if (!fp)
|
||||
return 0;
|
||||
|
||||
DWORD dwRetCRC32=GetHFILECRC32(fp);
|
||||
|
||||
std::fclose(fp);
|
||||
|
||||
return dwRetCRC32;
|
||||
}
|
||||
|
||||
DWORD GetFileSize(const char* c_szFileName)
|
||||
{
|
||||
FILE* fp = std::fopen(c_szFileName, "rb");
|
||||
if (!fp)
|
||||
return 0;
|
||||
|
||||
std::fseek(fp, 0, SEEK_END);
|
||||
const long size = std::ftell(fp);
|
||||
std::fclose(fp);
|
||||
|
||||
return size < 0 ? 0 : static_cast<DWORD>(size);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "Win32Types.h"
|
||||
#include <cstddef>
|
||||
|
||||
DWORD GetCRC32(const char* buffer, size_t count);
|
||||
DWORD GetCaseCRC32(const char * buf, size_t len);
|
||||
DWORD GetHFILECRC32(HANDLE hFile);
|
||||
DWORD GetFileCRC32(const char* c_szFileName);
|
||||
DWORD GetFileSize(const char* c_szFileName);
|
||||
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include "shared/common/Win32Types.h"
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
|
||||
template<typename T>
|
||||
class CDynamicPool
|
||||
{
|
||||
public:
|
||||
CDynamicPool() : m_uInitCapacity(0), m_uUsedCapacity(0) {}
|
||||
virtual ~CDynamicPool()
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
|
||||
void Create(UINT uCapacity)
|
||||
{
|
||||
m_uInitCapacity = uCapacity;
|
||||
m_kVct_pkData.reserve(uCapacity);
|
||||
m_kVct_pkFree.reserve(uCapacity);
|
||||
}
|
||||
|
||||
void Destroy()
|
||||
{
|
||||
for (auto v : m_kVct_pkData)
|
||||
delete v;
|
||||
m_kVct_pkData.clear();
|
||||
m_kVct_pkFree.clear();
|
||||
m_uUsedCapacity = 0;
|
||||
}
|
||||
|
||||
T* Alloc()
|
||||
{
|
||||
if (m_kVct_pkFree.empty())
|
||||
{
|
||||
T* pkNewData = new T;
|
||||
m_kVct_pkData.push_back(pkNewData);
|
||||
++m_uUsedCapacity;
|
||||
return pkNewData;
|
||||
}
|
||||
|
||||
T* pkFreeData = m_kVct_pkFree.back();
|
||||
m_kVct_pkFree.pop_back();
|
||||
return pkFreeData;
|
||||
}
|
||||
|
||||
void Free(T* pkData)
|
||||
{
|
||||
if (!pkData) return;
|
||||
m_kVct_pkFree.push_back(pkData);
|
||||
}
|
||||
|
||||
private:
|
||||
UINT m_uInitCapacity;
|
||||
UINT m_uUsedCapacity;
|
||||
std::vector<T*> m_kVct_pkData;
|
||||
std::vector<T*> m_kVct_pkFree;
|
||||
};
|
||||
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include "Win32Types.h"
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#ifndef SAFE_DELETE
|
||||
#define SAFE_DELETE(p) { if (p) { delete (p); (p) = nullptr; } }
|
||||
#endif
|
||||
|
||||
#ifndef SAFE_DELETE_ARRAY
|
||||
#define SAFE_DELETE_ARRAY(p) { if (p) { delete[] (p); (p) = nullptr; } }
|
||||
#endif
|
||||
|
||||
#ifndef SAFE_RELEASE
|
||||
#define SAFE_RELEASE(p) { if (p) { (p)->Release(); (p) = nullptr; } }
|
||||
#endif
|
||||
|
||||
#ifndef IS_SET
|
||||
#define IS_SET(flag,bit) ((flag) & (bit))
|
||||
#endif
|
||||
|
||||
#ifndef SET_BIT
|
||||
#define SET_BIT(var,bit) ((var) |= (bit))
|
||||
#endif
|
||||
|
||||
#ifndef REMOVE_BIT
|
||||
#define REMOVE_BIT(var,bit) ((var) &= ~(bit))
|
||||
#endif
|
||||
|
||||
#ifndef TOGGLE_BIT
|
||||
#define TOGGLE_BIT(var,bit) ((var) = (var) ^ (bit))
|
||||
#endif
|
||||
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <Python.h>
|
||||
|
||||
// The 40250 Python API represents native handles as integers. Keep that API while allowing a
|
||||
// 64-bit pointer to survive the round trip on macOS and mobile builds.
|
||||
template <typename T>
|
||||
PyObject* Py_BuildPointer(T* pointer)
|
||||
{
|
||||
return PyLong_FromVoidPtr(static_cast<void*>(pointer));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool PyTuple_GetPointer(PyObject* args, int position, T** out)
|
||||
{
|
||||
if (position < 0 || position >= PyTuple_Size(args))
|
||||
return false;
|
||||
PyObject* value = PyTuple_GetItem(args, position);
|
||||
if (!value)
|
||||
return false;
|
||||
void* pointer = PyLong_AsVoidPtr(value);
|
||||
if (PyErr_Occurred())
|
||||
return false;
|
||||
*out = static_cast<T*>(pointer);
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
#pragma once
|
||||
|
||||
#include <Python.h>
|
||||
#include "PortPythonPointer.h"
|
||||
#include <string>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
|
||||
inline PyObject * Py_BuildNone()
|
||||
{
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
inline void Py_ReleaseNone()
|
||||
{
|
||||
Py_DECREF(Py_None);
|
||||
}
|
||||
|
||||
inline PyObject * Py_BadArgument()
|
||||
{
|
||||
PyErr_BadArgument();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
inline PyObject * Py_BuildException(const char * c_pszErr = nullptr, ...)
|
||||
{
|
||||
if (!c_pszErr)
|
||||
PyErr_Clear();
|
||||
else
|
||||
{
|
||||
char szErrBuf[512+1];
|
||||
va_list args;
|
||||
va_start(args, c_pszErr);
|
||||
vsnprintf(szErrBuf, sizeof(szErrBuf), c_pszErr, args);
|
||||
va_end(args);
|
||||
|
||||
PyErr_SetString(PyExc_RuntimeError, szErrBuf);
|
||||
}
|
||||
|
||||
return Py_BuildNone();
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetObject(PyObject* poArgs, int pos, PyObject** ret)
|
||||
{
|
||||
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
||||
return false;
|
||||
|
||||
PyObject * poItem = PyTuple_GetItem(poArgs, pos);
|
||||
if (!poItem)
|
||||
return false;
|
||||
|
||||
*ret = poItem;
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetLong(PyObject* poArgs, int pos, long* ret)
|
||||
{
|
||||
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
||||
return false;
|
||||
|
||||
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
|
||||
if (!poItem || !PyInt_Check(poItem))
|
||||
return false;
|
||||
|
||||
*ret = PyInt_AsLong(poItem);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetDouble(PyObject* poArgs, int pos, double* ret)
|
||||
{
|
||||
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
||||
return false;
|
||||
|
||||
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
|
||||
if (!poItem)
|
||||
return false;
|
||||
|
||||
if (PyFloat_Check(poItem)) {
|
||||
*ret = PyFloat_AsDouble(poItem);
|
||||
return true;
|
||||
} else if (PyInt_Check(poItem)) {
|
||||
*ret = (double)PyInt_AsLong(poItem);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetFloat(PyObject* poArgs, int pos, float* ret)
|
||||
{
|
||||
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
||||
return false;
|
||||
|
||||
PyObject * poItem = PyTuple_GetItem(poArgs, pos);
|
||||
if (!poItem)
|
||||
return false;
|
||||
|
||||
if (PyFloat_Check(poItem)) {
|
||||
*ret = (float)PyFloat_AsDouble(poItem);
|
||||
return true;
|
||||
} else if (PyInt_Check(poItem)) {
|
||||
*ret = (float)PyInt_AsLong(poItem);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetInteger(PyObject* poArgs, int pos, int* ret)
|
||||
{
|
||||
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
||||
return false;
|
||||
|
||||
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
|
||||
if (!poItem)
|
||||
return false;
|
||||
|
||||
if (PyInt_Check(poItem))
|
||||
{
|
||||
*ret = (int)PyInt_AsLong(poItem);
|
||||
return true;
|
||||
}
|
||||
if (PyLong_Check(poItem))
|
||||
{
|
||||
*ret = (int)PyLong_AsUnsignedLongMask(poItem);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetByte(PyObject* poArgs, int pos, unsigned char* ret)
|
||||
{
|
||||
int val = 0;
|
||||
bool result = PyTuple_GetInteger(poArgs, pos, &val);
|
||||
*ret = (unsigned char)val;
|
||||
return result;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetInteger(PyObject* poArgs, int pos, unsigned char* ret)
|
||||
{
|
||||
int val = 0;
|
||||
bool result = PyTuple_GetInteger(poArgs, pos, &val);
|
||||
*ret = (unsigned char)val;
|
||||
return result;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetInteger(PyObject* poArgs, int pos, unsigned short* ret)
|
||||
{
|
||||
int val = 0;
|
||||
bool result = PyTuple_GetInteger(poArgs, pos, &val);
|
||||
*ret = (unsigned short)val;
|
||||
return result;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetLongLong(PyObject* poArgs, int pos, long long* ret)
|
||||
{
|
||||
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
||||
return false;
|
||||
|
||||
PyObject * poItem = PyTuple_GetItem(poArgs, pos);
|
||||
if (!poItem)
|
||||
return false;
|
||||
|
||||
*ret = PyLong_AsLongLong(poItem);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetUnsignedLong(PyObject* poArgs, int pos, unsigned long* ret)
|
||||
{
|
||||
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
||||
return false;
|
||||
|
||||
PyObject * poItem = PyTuple_GetItem(poArgs, pos);
|
||||
if (!poItem)
|
||||
return false;
|
||||
|
||||
*ret = PyLong_AsUnsignedLong(poItem);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetUnsignedInteger(PyObject* poArgs, int pos, unsigned int* ret)
|
||||
{
|
||||
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
||||
return false;
|
||||
|
||||
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
|
||||
if (!poItem)
|
||||
return false;
|
||||
|
||||
*ret = (unsigned int)PyLong_AsUnsignedLong(poItem);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetString(PyObject* poArgs, int pos, char** ret)
|
||||
{
|
||||
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
||||
return false;
|
||||
|
||||
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
|
||||
if (!poItem || !PyString_Check(poItem))
|
||||
return false;
|
||||
|
||||
*ret = PyString_AsString(poItem);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetBoolean(PyObject* poArgs, int pos, bool* ret)
|
||||
{
|
||||
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
||||
return false;
|
||||
|
||||
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
|
||||
if (!poItem)
|
||||
return false;
|
||||
|
||||
*ret = PyLong_AsLong(poItem) ? true : false;
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool __PyCallClassMemberFunc_ByCString(PyObject* poClass, const char* c_szFunc, PyObject* poArgs, PyObject** ppoRet)
|
||||
{
|
||||
if (!poClass)
|
||||
{
|
||||
Py_XDECREF(poArgs);
|
||||
return false;
|
||||
}
|
||||
|
||||
PyObject * poFunc = PyObject_GetAttrString(poClass, (char *)c_szFunc);
|
||||
if (!poFunc)
|
||||
{
|
||||
PyErr_Clear();
|
||||
Py_XDECREF(poArgs);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!PyCallable_Check(poFunc))
|
||||
{
|
||||
Py_DECREF(poFunc);
|
||||
Py_XDECREF(poArgs);
|
||||
return false;
|
||||
}
|
||||
|
||||
PyObject * poRet = PyObject_CallObject(poFunc, poArgs);
|
||||
if (!poRet)
|
||||
{
|
||||
PyErr_Print();
|
||||
Py_DECREF(poFunc);
|
||||
Py_XDECREF(poArgs);
|
||||
return false;
|
||||
}
|
||||
|
||||
*ppoRet = poRet;
|
||||
Py_DECREF(poFunc);
|
||||
Py_XDECREF(poArgs);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool PyCallClassMemberFunc(PyObject* poClass, const char* c_szFunc, PyObject* poArgs)
|
||||
{
|
||||
PyObject* poRet = nullptr;
|
||||
if (!poClass)
|
||||
{
|
||||
Py_XDECREF(poArgs);
|
||||
return false;
|
||||
}
|
||||
if (!__PyCallClassMemberFunc_ByCString(poClass, c_szFunc, poArgs, &poRet))
|
||||
return false;
|
||||
Py_DECREF(poRet);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool PyCallClassMemberFunc(PyObject* poClass, const char* c_szFunc, PyObject* poArgs, bool* pisRet)
|
||||
{
|
||||
PyObject* poRet = nullptr;
|
||||
if (!__PyCallClassMemberFunc_ByCString(poClass, c_szFunc, poArgs, &poRet))
|
||||
return false;
|
||||
|
||||
if (PyNumber_Check(poRet))
|
||||
*pisRet = (PyLong_AsLong(poRet) != 0);
|
||||
else
|
||||
*pisRet = true;
|
||||
|
||||
Py_DECREF(poRet);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool PyCallClassMemberFunc(PyObject* poClass, const char* c_szFunc, PyObject* poArgs, long * plRetValue)
|
||||
{
|
||||
PyObject* poRet = nullptr;
|
||||
if (!__PyCallClassMemberFunc_ByCString(poClass, c_szFunc, poArgs, &poRet))
|
||||
return false;
|
||||
|
||||
if (PyNumber_Check(poRet))
|
||||
{
|
||||
*plRetValue = PyLong_AsLong(poRet);
|
||||
Py_DECREF(poRet);
|
||||
return true;
|
||||
}
|
||||
|
||||
Py_DECREF(poRet);
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#include "ReferenceObject.h"
|
||||
#include <cassert>
|
||||
|
||||
template<typename T>
|
||||
class CRef
|
||||
{
|
||||
public:
|
||||
CRef() : m_pObject(nullptr) {}
|
||||
CRef(CReferenceObject* pObject) : m_pObject(nullptr) { Initialize(pObject); }
|
||||
CRef(const CRef& rRef) : m_pObject(nullptr) { Initialize(rRef.m_pObject); }
|
||||
~CRef() { Clear(); }
|
||||
|
||||
void operator = (CReferenceObject* pObject) { SetPointer(pObject); }
|
||||
void operator = (const CRef& rRef) { SetPointer(rRef.m_pObject); }
|
||||
|
||||
void Clear()
|
||||
{
|
||||
if (m_pObject)
|
||||
{
|
||||
m_pObject->Release();
|
||||
m_pObject = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool IsNull() const { return m_pObject == nullptr; }
|
||||
T* GetPointer() const { return static_cast<T*>(m_pObject); }
|
||||
T* operator->() const { return static_cast<T*>(m_pObject); }
|
||||
|
||||
private:
|
||||
void Initialize(CReferenceObject* pObject)
|
||||
{
|
||||
m_pObject = pObject;
|
||||
if (m_pObject)
|
||||
m_pObject->AddReference();
|
||||
}
|
||||
|
||||
void SetPointer(CReferenceObject* pObject)
|
||||
{
|
||||
if (m_pObject == pObject)
|
||||
return;
|
||||
Clear();
|
||||
Initialize(pObject);
|
||||
}
|
||||
|
||||
CReferenceObject* m_pObject;
|
||||
};
|
||||
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
|
||||
class CReferenceObject
|
||||
{
|
||||
public:
|
||||
CReferenceObject() : m_refCount(0), m_destructed(false) {}
|
||||
virtual ~CReferenceObject() {}
|
||||
|
||||
void AddReference()
|
||||
{
|
||||
if (m_refCount == 0)
|
||||
OnConstruct();
|
||||
++m_refCount;
|
||||
}
|
||||
|
||||
void AddReferenceOnly()
|
||||
{
|
||||
++m_refCount;
|
||||
}
|
||||
|
||||
void Release()
|
||||
{
|
||||
if (m_refCount > 1)
|
||||
{
|
||||
--m_refCount;
|
||||
return;
|
||||
}
|
||||
|
||||
assert(m_destructed == false);
|
||||
m_refCount = 0;
|
||||
OnSelfDestruct();
|
||||
}
|
||||
|
||||
int GetReferenceCount() const { return m_refCount; }
|
||||
bool canDestroy() const { return m_refCount <= 0; }
|
||||
|
||||
protected:
|
||||
virtual void OnConstruct() { m_destructed = false; }
|
||||
virtual void OnSelfDestruct() { m_destructed = true; delete this; }
|
||||
|
||||
private:
|
||||
int m_refCount;
|
||||
bool m_destructed;
|
||||
};
|
||||
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
|
||||
#ifndef __forceinline
|
||||
#define __forceinline inline __attribute__((always_inline))
|
||||
#endif
|
||||
|
||||
template <typename T>
|
||||
class CSingleton
|
||||
{
|
||||
static T * ms_singleton;
|
||||
|
||||
public:
|
||||
CSingleton()
|
||||
{
|
||||
assert(!ms_singleton);
|
||||
intptr_t offset = (intptr_t) (T*) 1 - (intptr_t) (CSingleton <T>*) (T*) 1;
|
||||
ms_singleton = (T*) ((intptr_t) this + offset);
|
||||
}
|
||||
|
||||
virtual ~CSingleton()
|
||||
{
|
||||
assert(ms_singleton);
|
||||
ms_singleton = nullptr;
|
||||
}
|
||||
|
||||
__forceinline static T & Instance()
|
||||
{
|
||||
assert(ms_singleton);
|
||||
return (*ms_singleton);
|
||||
}
|
||||
|
||||
__forceinline static T * InstancePtr()
|
||||
{
|
||||
return (ms_singleton);
|
||||
}
|
||||
|
||||
__forceinline static T & instance()
|
||||
{
|
||||
assert(ms_singleton);
|
||||
return (*ms_singleton);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T> T * CSingleton<T>::ms_singleton = nullptr;
|
||||
@@ -0,0 +1,302 @@
|
||||
#pragma once
|
||||
// Win32 scalar and handle types with the widths the 40250 client was built with (32-bit Win32,
|
||||
// ILP32). Scalars are fixed-width on every target; handle/pointer types stay pointer-sized and
|
||||
// belong behind platform adapters. `long` itself cannot be redefined: ported code that relies on
|
||||
// the 32-bit `long` uses LONG/DWORD or an explicit int32_t/uint32_t (docs/PORT-PLAN.md section 3).
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
#if defined(_WIN32)
|
||||
// Windows (LLP64) already has these names with the same widths; use the SDK's so platform code
|
||||
// that includes <windows.h> in the same translation unit does not conflict.
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#else
|
||||
|
||||
typedef uint8_t BYTE;
|
||||
typedef uint16_t WORD;
|
||||
typedef uint32_t DWORD;
|
||||
typedef int32_t LONG;
|
||||
typedef uint32_t ULONG;
|
||||
typedef int32_t BOOL;
|
||||
typedef int32_t INT;
|
||||
typedef uint32_t UINT;
|
||||
typedef int16_t SHORT;
|
||||
typedef uint16_t USHORT;
|
||||
typedef char CHAR;
|
||||
typedef uint8_t UCHAR;
|
||||
typedef float FLOAT;
|
||||
typedef int64_t LONGLONG;
|
||||
typedef uint64_t ULONGLONG;
|
||||
typedef int64_t INT64;
|
||||
typedef uint64_t UINT64;
|
||||
typedef uint64_t DWORD64;
|
||||
typedef wchar_t WCHAR;
|
||||
|
||||
typedef void* LPVOID;
|
||||
typedef const void* LPCVOID;
|
||||
typedef void* PVOID;
|
||||
typedef char* LPSTR;
|
||||
typedef const char* LPCSTR;
|
||||
typedef char* PSTR;
|
||||
typedef const char* PCSTR;
|
||||
typedef CHAR* PCHAR;
|
||||
typedef BYTE* LPBYTE;
|
||||
typedef BYTE* PBYTE;
|
||||
typedef WORD* LPWORD;
|
||||
typedef DWORD* LPDWORD;
|
||||
typedef DWORD* PDWORD;
|
||||
typedef LONG* LPLONG;
|
||||
typedef BOOL* LPBOOL;
|
||||
typedef INT* LPINT;
|
||||
typedef INT* PINT;
|
||||
typedef UINT* PUINT;
|
||||
typedef WCHAR* LPWSTR;
|
||||
typedef const WCHAR* LPCWSTR;
|
||||
typedef uintptr_t SIZE_T;
|
||||
typedef WORD LANGID;
|
||||
|
||||
typedef intptr_t INT_PTR;
|
||||
typedef uintptr_t UINT_PTR;
|
||||
typedef intptr_t LONG_PTR;
|
||||
typedef uintptr_t ULONG_PTR;
|
||||
typedef uintptr_t DWORD_PTR;
|
||||
typedef uintptr_t WPARAM;
|
||||
typedef intptr_t LPARAM;
|
||||
typedef intptr_t LRESULT;
|
||||
|
||||
typedef LONG HRESULT;
|
||||
#define S_OK ((HRESULT)0L)
|
||||
#define S_FALSE ((HRESULT)1L)
|
||||
#define E_FAIL ((HRESULT)0x80004005L)
|
||||
#define E_OUTOFMEMORY ((HRESULT)0x8007000EL)
|
||||
#define E_INVALIDARG ((HRESULT)0x80070057L)
|
||||
#define SUCCEEDED(hr) (((HRESULT)(hr)) >= 0)
|
||||
#define FAILED(hr) (((HRESULT)(hr)) < 0)
|
||||
|
||||
typedef void* HANDLE;
|
||||
struct HWND__;
|
||||
typedef HWND__* HWND;
|
||||
struct HINSTANCE__;
|
||||
typedef HINSTANCE__* HINSTANCE;
|
||||
typedef HINSTANCE HMODULE;
|
||||
struct HDC__;
|
||||
typedef HDC__* HDC;
|
||||
struct HBITMAP__;
|
||||
typedef HBITMAP__* HBITMAP;
|
||||
struct HICON__;
|
||||
typedef HICON__* HICON;
|
||||
typedef HICON HCURSOR;
|
||||
struct HBRUSH__;
|
||||
typedef HBRUSH__* HBRUSH;
|
||||
struct HKL__;
|
||||
typedef HKL__* HKL;
|
||||
typedef LRESULT (*WNDPROC)(HWND, UINT, WPARAM, LPARAM);
|
||||
|
||||
typedef struct tagPOINT
|
||||
{
|
||||
LONG x;
|
||||
LONG y;
|
||||
} POINT, *PPOINT, *LPPOINT;
|
||||
|
||||
typedef struct tagRECT
|
||||
{
|
||||
LONG left;
|
||||
LONG top;
|
||||
LONG right;
|
||||
LONG bottom;
|
||||
} RECT, *PRECT, *LPRECT;
|
||||
|
||||
typedef struct tagSIZE
|
||||
{
|
||||
LONG cx;
|
||||
LONG cy;
|
||||
} SIZE, *PSIZE, *LPSIZE;
|
||||
|
||||
typedef union _LARGE_INTEGER
|
||||
{
|
||||
struct
|
||||
{
|
||||
DWORD LowPart;
|
||||
LONG HighPart;
|
||||
};
|
||||
LONGLONG QuadPart;
|
||||
} LARGE_INTEGER, *PLARGE_INTEGER;
|
||||
|
||||
typedef struct _GUID
|
||||
{
|
||||
DWORD Data1;
|
||||
WORD Data2;
|
||||
WORD Data3;
|
||||
BYTE Data4[8];
|
||||
} GUID;
|
||||
|
||||
// The 40250 projects build with the multi-byte character set: TCHAR and the GDI structs are the
|
||||
// `A` variants.
|
||||
typedef char TCHAR;
|
||||
#define LF_FACESIZE 32
|
||||
|
||||
typedef struct tagLOGFONTA
|
||||
{
|
||||
LONG lfHeight;
|
||||
LONG lfWidth;
|
||||
LONG lfEscapement;
|
||||
LONG lfOrientation;
|
||||
LONG lfWeight;
|
||||
BYTE lfItalic;
|
||||
BYTE lfUnderline;
|
||||
BYTE lfStrikeOut;
|
||||
BYTE lfCharSet;
|
||||
BYTE lfOutPrecision;
|
||||
BYTE lfClipPrecision;
|
||||
BYTE lfQuality;
|
||||
BYTE lfPitchAndFamily;
|
||||
CHAR lfFaceName[LF_FACESIZE];
|
||||
} LOGFONTA, LOGFONT, *PLOGFONT, *LPLOGFONT;
|
||||
|
||||
typedef struct tagTEXTMETRICA
|
||||
{
|
||||
LONG tmHeight;
|
||||
LONG tmAscent;
|
||||
LONG tmDescent;
|
||||
LONG tmInternalLeading;
|
||||
LONG tmExternalLeading;
|
||||
LONG tmAveCharWidth;
|
||||
LONG tmMaxCharWidth;
|
||||
LONG tmWeight;
|
||||
LONG tmOverhang;
|
||||
LONG tmDigitizedAspectX;
|
||||
LONG tmDigitizedAspectY;
|
||||
BYTE tmFirstChar;
|
||||
BYTE tmLastChar;
|
||||
BYTE tmDefaultChar;
|
||||
BYTE tmBreakChar;
|
||||
BYTE tmItalic;
|
||||
BYTE tmUnderlined;
|
||||
BYTE tmStruckOut;
|
||||
BYTE tmPitchAndFamily;
|
||||
BYTE tmCharSet;
|
||||
} TEXTMETRICA, TEXTMETRIC, *PTEXTMETRIC, *LPTEXTMETRIC;
|
||||
|
||||
typedef struct tagBITMAPINFOHEADER
|
||||
{
|
||||
DWORD biSize;
|
||||
LONG biWidth;
|
||||
LONG biHeight;
|
||||
WORD biPlanes;
|
||||
WORD biBitCount;
|
||||
DWORD biCompression;
|
||||
DWORD biSizeImage;
|
||||
LONG biXPelsPerMeter;
|
||||
LONG biYPelsPerMeter;
|
||||
DWORD biClrUsed;
|
||||
DWORD biClrImportant;
|
||||
} BITMAPINFOHEADER, *LPBITMAPINFOHEADER;
|
||||
|
||||
typedef struct tagRGBQUAD
|
||||
{
|
||||
BYTE rgbBlue;
|
||||
BYTE rgbGreen;
|
||||
BYTE rgbRed;
|
||||
BYTE rgbReserved;
|
||||
} RGBQUAD;
|
||||
|
||||
typedef struct tagBITMAPINFO
|
||||
{
|
||||
BITMAPINFOHEADER bmiHeader;
|
||||
RGBQUAD bmiColors[1];
|
||||
} BITMAPINFO, *LPBITMAPINFO;
|
||||
|
||||
typedef struct _OSVERSIONINFOA
|
||||
{
|
||||
DWORD dwOSVersionInfoSize;
|
||||
DWORD dwMajorVersion;
|
||||
DWORD dwMinorVersion;
|
||||
DWORD dwBuildNumber;
|
||||
DWORD dwPlatformId;
|
||||
CHAR szCSDVersion[128];
|
||||
} OSVERSIONINFOA, OSVERSIONINFO, *LPOSVERSIONINFOA, *LPOSVERSIONINFO;
|
||||
|
||||
struct HFONT__;
|
||||
typedef HFONT__* HFONT;
|
||||
|
||||
#ifndef VOID
|
||||
#define VOID void
|
||||
#endif
|
||||
#ifndef CONST
|
||||
#define CONST const
|
||||
#endif
|
||||
#ifndef IN
|
||||
#define IN
|
||||
#endif
|
||||
#ifndef OUT
|
||||
#define OUT
|
||||
#endif
|
||||
#ifndef TRUE
|
||||
#define TRUE 1
|
||||
#endif
|
||||
#ifndef FALSE
|
||||
#define FALSE 0
|
||||
#endif
|
||||
#define CALLBACK
|
||||
#define WINAPI
|
||||
#define APIENTRY
|
||||
#define __stdcall
|
||||
#define __cdecl
|
||||
#define __forceinline inline __attribute__((always_inline))
|
||||
|
||||
#define MAX_PATH 260
|
||||
#define INFINITE 0xFFFFFFFF
|
||||
|
||||
#define LOBYTE(w) ((BYTE)(((DWORD_PTR)(w)) & 0xff))
|
||||
#define HIBYTE(w) ((BYTE)((((DWORD_PTR)(w)) >> 8) & 0xff))
|
||||
#define LOWORD(l) ((WORD)(((DWORD_PTR)(l)) & 0xffff))
|
||||
#define HIWORD(l) ((WORD)((((DWORD_PTR)(l)) >> 16) & 0xffff))
|
||||
#define MAKEWORD(a, b) ((WORD)(((BYTE)(((DWORD_PTR)(a)) & 0xff)) | ((WORD)((BYTE)(((DWORD_PTR)(b)) & 0xff))) << 8))
|
||||
struct D3DXVECTOR2
|
||||
{
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
D3DXVECTOR2() = default;
|
||||
D3DXVECTOR2(float _x, float _y) : x(_x), y(_y) {}
|
||||
};
|
||||
|
||||
struct D3DXCOLOR
|
||||
{
|
||||
float r = 0.0f, g = 0.0f, b = 0.0f, a = 0.0f;
|
||||
D3DXCOLOR() = default;
|
||||
D3DXCOLOR(float _r, float _g, float _b, float _a) : r(_r), g(_g), b(_b), a(_a) {}
|
||||
D3DXCOLOR(DWORD dwColor)
|
||||
{
|
||||
a = ((dwColor >> 24) & 0xff) / 255.0f;
|
||||
r = ((dwColor >> 16) & 0xff) / 255.0f;
|
||||
g = ((dwColor >> 8) & 0xff) / 255.0f;
|
||||
b = (dwColor & 0xff) / 255.0f;
|
||||
}
|
||||
operator DWORD() const
|
||||
{
|
||||
return ((DWORD(a * 255.0f) & 0xff) << 24) |
|
||||
((DWORD(r * 255.0f) & 0xff) << 16) |
|
||||
((DWORD(g * 255.0f) & 0xff) << 8) |
|
||||
(DWORD(b * 255.0f) & 0xff);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // _WIN32
|
||||
|
||||
// Widths the 40250 binary was compiled with; serialized structs depend on them.
|
||||
static_assert(sizeof(BYTE) == 1 && sizeof(WORD) == 2 && sizeof(DWORD) == 4, "Win32 unsigned widths");
|
||||
static_assert(sizeof(LONG) == 4 && sizeof(BOOL) == 4 && sizeof(UINT) == 4 && sizeof(INT) == 4, "Win32 signed widths");
|
||||
static_assert(sizeof(SHORT) == 2 && sizeof(FLOAT) == 4, "Win32 short/float widths");
|
||||
static_assert(sizeof(POINT) == 8 && sizeof(RECT) == 16, "Win32 POINT/RECT layout");
|
||||
static_assert(sizeof(GUID) == 16 && sizeof(LARGE_INTEGER) == 8, "Win32 GUID/LARGE_INTEGER layout");
|
||||
static_assert(sizeof(LOGFONTA) == 60 && sizeof(TEXTMETRICA) == 56, "Win32 GDI struct layout");
|
||||
static_assert(sizeof(OSVERSIONINFOA) == 148, "Win32 OSVERSIONINFOA layout");
|
||||
static_assert(sizeof(BITMAPINFOHEADER) == 40 && sizeof(BITMAPINFO) == 44, "Win32 DIB header layout");
|
||||
static_assert(sizeof(WPARAM) == sizeof(void*) && sizeof(HANDLE) == sizeof(void*), "handles stay pointer-sized");
|
||||
Reference in New Issue
Block a user