From 059a394523575904ec60eb02443cbca432633a56 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Sat, 28 May 2022 16:12:37 +0200 Subject: [PATCH] Add commonmacros.h and strtools.h/.cpp from SourceSDK strtools has been redacted to just what we need --- r5dev/tier0/commonmacros.h | 174 ++++++++++++++++++++++++++ r5dev/tier0/platform.h | 15 ++- r5dev/tier1/strtools.cpp | 46 +++++++ r5dev/tier1/strtools.h | 4 + r5dev/vproj/clientsdk.vcxproj | 3 + r5dev/vproj/clientsdk.vcxproj.filters | 9 ++ r5dev/vproj/dedicated.vcxproj | 3 + r5dev/vproj/dedicated.vcxproj.filters | 9 ++ r5dev/vproj/gamesdk.vcxproj | 3 + r5dev/vproj/gamesdk.vcxproj.filters | 9 ++ 10 files changed, 274 insertions(+), 1 deletion(-) create mode 100644 r5dev/tier0/commonmacros.h create mode 100644 r5dev/tier1/strtools.cpp create mode 100644 r5dev/tier1/strtools.h diff --git a/r5dev/tier0/commonmacros.h b/r5dev/tier0/commonmacros.h new file mode 100644 index 00000000..ec13a25d --- /dev/null +++ b/r5dev/tier0/commonmacros.h @@ -0,0 +1,174 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: +// +// $NoKeywords: $ +// +//=============================================================================// +#ifndef COMMONMACROS_H +#define COMMONMACROS_H + +#ifdef _WIN32 +#pragma once +#endif + +#include "tier0/platform.h" + +// ------------------------------------------------------- +// +// commonmacros.h +// +// This should contain ONLY general purpose macros that are +// appropriate for use in engine/launcher/all tools +// +// ------------------------------------------------------- + +// Makes a 4-byte "packed ID" int out of 4 characters +#define MAKEID(d,c,b,a) ( ((int)(a) << 24) | ((int)(b) << 16) | ((int)(c) << 8) | ((int)(d)) ) + +// Compares a string with a 4-byte packed ID constant +#define STRING_MATCHES_ID( p, id ) ( (*((int *)(p)) == (id) ) ? true : false ) +#define ID_TO_STRING( id, p ) ( (p)[3] = (((id)>>24) & 0xFF), (p)[2] = (((id)>>16) & 0xFF), (p)[1] = (((id)>>8) & 0xFF), (p)[0] = (((id)>>0) & 0xFF) ) + +#define SETBITS(iBitVector, bits) ((iBitVector) |= (bits)) +#define CLEARBITS(iBitVector, bits) ((iBitVector) &= ~(bits)) +#define FBitSet(iBitVector, bits) ((iBitVector) & (bits)) + +template +inline bool IsPowerOfTwo(T value) +{ + return (value & (value - (T)1)) == (T)0; +} + +#ifndef REFERENCE +#define REFERENCE(arg) ((void)arg) +#endif + +#define CONST_INTEGER_AS_STRING(x) #x //Wraps the integer in quotes, allowing us to form constant strings with it +#define __HACK_LINE_AS_STRING__(x) CONST_INTEGER_AS_STRING(x) //__LINE__ can only be converted to an actual number by going through this, otherwise the output is literally "__LINE__" +#define __LINE__AS_STRING __HACK_LINE_AS_STRING__(__LINE__) //Gives you the line number in constant string form + +// Using ARRAYSIZE implementation from winnt.h: +#ifdef ARRAYSIZE +#undef ARRAYSIZE +#endif + +// Return the number of elements in a statically sized array. +// DWORD Buffer[100]; +// RTL_NUMBER_OF(Buffer) == 100 +// This is also popularly known as: NUMBER_OF, ARRSIZE, _countof, NELEM, etc. +// +#define RTL_NUMBER_OF_V1(A) (sizeof(A)/sizeof((A)[0])) + +#if defined(__cplusplus) && \ + !defined(MIDL_PASS) && \ + !defined(RC_INVOKED) && \ + (_MSC_FULL_VER >= 13009466) && \ + !defined(SORTPP_PASS) + +// From crtdefs.h +#if !defined(UNALIGNED) +#if defined(_M_IA64) || defined(_M_AMD64) +#define UNALIGNED __unaligned +#else +#define UNALIGNED +#endif +#endif + +// RtlpNumberOf is a function that takes a reference to an array of N Ts. +// +// typedef T array_of_T[N]; +// typedef array_of_T &reference_to_array_of_T; +// +// RtlpNumberOf returns a pointer to an array of N chars. +// We could return a reference instead of a pointer but older compilers do not accept that. +// +// typedef char array_of_char[N]; +// typedef array_of_char *pointer_to_array_of_char; +// +// sizeof(array_of_char) == N +// sizeof(*pointer_to_array_of_char) == N +// +// pointer_to_array_of_char RtlpNumberOf(reference_to_array_of_T); +// +// We never even call RtlpNumberOf, we just take the size of dereferencing its return type. +// We do not even implement RtlpNumberOf, we just decare it. +// +// Attempts to pass pointers instead of arrays to this macro result in compile time errors. +// That is the point. +extern "C++" // templates cannot be declared to have 'C' linkage +template +char(*RtlpNumberOf(UNALIGNED T(&)[N]))[N]; + +#ifdef _PREFAST_ +// The +0 is so that we can go: +// size = ARRAYSIZE(array) * sizeof(array[0]) without triggering a /analyze +// warning about multiplying sizeof. +#define RTL_NUMBER_OF_V2(A) (sizeof(*RtlpNumberOf(A))+0) +#else +#define RTL_NUMBER_OF_V2(A) (sizeof(*RtlpNumberOf(A))) +#endif + +// This does not work with: +// +// void Foo() +// { +// struct { int x; } y[2]; +// RTL_NUMBER_OF_V2(y); // illegal use of anonymous local type in template instantiation +// } +// +// You must instead do: +// +// struct Foo1 { int x; }; +// +// void Foo() +// { +// Foo1 y[2]; +// RTL_NUMBER_OF_V2(y); // ok +// } +// +// OR +// +// void Foo() +// { +// struct { int x; } y[2]; +// RTL_NUMBER_OF_V1(y); // ok +// } +// +// OR +// +// void Foo() +// { +// struct { int x; } y[2]; +// _ARRAYSIZE(y); // ok +// } + +#else +#define RTL_NUMBER_OF_V2(A) RTL_NUMBER_OF_V1(A) +#endif + +// ARRAYSIZE is more readable version of RTL_NUMBER_OF_V2 +// _ARRAYSIZE is a version useful for anonymous types +#define ARRAYSIZE(A) RTL_NUMBER_OF_V2(A) +#define _ARRAYSIZE(A) RTL_NUMBER_OF_V1(A) + +#define Q_ARRAYSIZE(p) ARRAYSIZE(p) +#define V_ARRAYSIZE(p) ARRAYSIZE(p) + +template< typename IndexType, typename T, unsigned int N > +IndexType ClampedArrayIndex(const T(&buffer)[N], IndexType index) +{ + NOTE_UNUSED(buffer); + return clamp(index, 0, (IndexType)N - 1); +} + +template< typename T, unsigned int N > +T ClampedArrayElement(const T(&buffer)[N], unsigned int uIndex) +{ + // Put index in an unsigned type to halve the clamping. + if (uIndex >= N) + uIndex = N - 1; + return buffer[uIndex]; +} + +#endif // COMMONMACROS_H diff --git a/r5dev/tier0/platform.h b/r5dev/tier0/platform.h index b16a8aeb..16ce459f 100644 --- a/r5dev/tier0/platform.h +++ b/r5dev/tier0/platform.h @@ -142,7 +142,7 @@ #endif // CROSS_PLATFORM_VERSION < 2 //----------------------------------------------------------------------------- -// +// Time stamp counter //----------------------------------------------------------------------------- inline uint64_t Plat_Rdtsc() { @@ -170,6 +170,19 @@ inline uint64_t Plat_Rdtsc() #endif } +//----------------------------------------------------------------------------- +// Silences a number of warnings on 360 compiles +//----------------------------------------------------------------------------- +inline uint64 CastPtrToUint64(const void* p) +{ + return (uint64)((uintptr_t)p); +} + +inline int64 CastPtrToInt64(const void* p) +{ + return (int64)((uintptr_t)p); +} + //----------------------------------------------------------------------------- // Stack-based allocation related helpers //----------------------------------------------------------------------------- diff --git a/r5dev/tier1/strtools.cpp b/r5dev/tier1/strtools.cpp new file mode 100644 index 00000000..66a2a713 --- /dev/null +++ b/r5dev/tier1/strtools.cpp @@ -0,0 +1,46 @@ +#include "core/stdafx.h" + + +//----------------------------------------------------------------------------- +// Purpose: Converts a UTF8 string into a unicode string +//----------------------------------------------------------------------------- +int V_UTF8ToUnicode(const char* pUTF8, wchar_t* pwchDest, int cubDestSizeInBytes) +{ + Assert(cubDestSizeInBytes >= sizeof(*pwchDest)); + pwchDest[0] = 0; + if (!pUTF8) + return 0; +#ifdef _WIN32 + int cchResult = MultiByteToWideChar(CP_UTF8, 0, pUTF8, -1, pwchDest, cubDestSizeInBytes / sizeof(wchar_t)); +#elif POSIX + int cchResult = mbstowcs(pwchDest, pUTF8, cubDestSizeInBytes / sizeof(wchar_t)); +#endif + pwchDest[(cubDestSizeInBytes / sizeof(wchar_t)) - 1] = 0; + return cchResult; +} + +//----------------------------------------------------------------------------- +// Purpose: Converts a unicode string into a UTF8 (standard) string +//----------------------------------------------------------------------------- +int V_UnicodeToUTF8(const wchar_t* pUnicode, char* pUTF8, int cubDestSizeInBytes) +{ + if (cubDestSizeInBytes > 0) + { + pUTF8[0] = 0; + } + +#ifdef _WIN32 + int cchResult = WideCharToMultiByte(CP_UTF8, 0, pUnicode, -1, pUTF8, cubDestSizeInBytes, NULL, NULL); +#elif POSIX + int cchResult = 0; + if (pUnicode && pUTF8) + cchResult = wcstombs(pUTF8, pUnicode, cubDestSizeInBytes); +#endif + + if (cubDestSizeInBytes > 0) + { + pUTF8[cubDestSizeInBytes - 1] = 0; + } + + return cchResult; +} \ No newline at end of file diff --git a/r5dev/tier1/strtools.h b/r5dev/tier1/strtools.h new file mode 100644 index 00000000..eea5879c --- /dev/null +++ b/r5dev/tier1/strtools.h @@ -0,0 +1,4 @@ +#pragma once + +int V_UTF8ToUnicode(const char* pUTF8, wchar_t* pwchDest, int cubDestSizeInBytes); +int V_UnicodeToUTF8(const wchar_t* pUnicode, char* pUTF8, int cubDestSizeInBytes); \ No newline at end of file diff --git a/r5dev/vproj/clientsdk.vcxproj b/r5dev/vproj/clientsdk.vcxproj index a24d03eb..f1b3c915 100644 --- a/r5dev/vproj/clientsdk.vcxproj +++ b/r5dev/vproj/clientsdk.vcxproj @@ -97,6 +97,7 @@ + @@ -406,6 +407,7 @@ + @@ -425,6 +427,7 @@ + diff --git a/r5dev/vproj/clientsdk.vcxproj.filters b/r5dev/vproj/clientsdk.vcxproj.filters index 658bba73..369310a8 100644 --- a/r5dev/vproj/clientsdk.vcxproj.filters +++ b/r5dev/vproj/clientsdk.vcxproj.filters @@ -498,6 +498,9 @@ sdk\tier0 + + sdk\tier1 + @@ -1490,6 +1493,12 @@ sdk\tier0 + + sdk\tier0 + + + sdk\tier1 + diff --git a/r5dev/vproj/dedicated.vcxproj b/r5dev/vproj/dedicated.vcxproj index 8aac8560..10266a46 100644 --- a/r5dev/vproj/dedicated.vcxproj +++ b/r5dev/vproj/dedicated.vcxproj @@ -395,6 +395,7 @@ + @@ -414,6 +415,7 @@ + @@ -510,6 +512,7 @@ + diff --git a/r5dev/vproj/dedicated.vcxproj.filters b/r5dev/vproj/dedicated.vcxproj.filters index 6336be98..375782d4 100644 --- a/r5dev/vproj/dedicated.vcxproj.filters +++ b/r5dev/vproj/dedicated.vcxproj.filters @@ -1107,6 +1107,12 @@ sdk\tier0 + + sdk\tier0 + + + sdk\tier1 + @@ -1355,6 +1361,9 @@ sdk\tier0 + + sdk\tier1 + diff --git a/r5dev/vproj/gamesdk.vcxproj b/r5dev/vproj/gamesdk.vcxproj index 6f218a8c..91285380 100644 --- a/r5dev/vproj/gamesdk.vcxproj +++ b/r5dev/vproj/gamesdk.vcxproj @@ -104,6 +104,7 @@ + @@ -425,6 +426,7 @@ + @@ -443,6 +445,7 @@ + diff --git a/r5dev/vproj/gamesdk.vcxproj.filters b/r5dev/vproj/gamesdk.vcxproj.filters index b2a6bf5b..1282356a 100644 --- a/r5dev/vproj/gamesdk.vcxproj.filters +++ b/r5dev/vproj/gamesdk.vcxproj.filters @@ -528,6 +528,9 @@ sdk\tier0 + + sdk\tier1 + @@ -1553,6 +1556,12 @@ sdk\tier0 + + sdk\tier1 + + + sdk\tier0 +