From 3f2a77b71ca2c3159400f67370f2ed4967cf0694 Mon Sep 17 00:00:00 2001 From: asmodai Date: Sun, 10 Jan 2016 17:04:28 +0300 Subject: [PATCH] Fixed crash on bot disconnect Using inline algorithms instead of defines --- rehlds/common/mathlib.h | 51 ++++++++++- rehlds/engine/common.cpp | 154 ++++++++++++++------------------ rehlds/engine/host.cpp | 4 + rehlds/engine/net_ws.cpp | 2 +- rehlds/engine/sv_main.cpp | 2 +- rehlds/public/basetypes.h | 15 +--- rehlds/public/rehlds/osconfig.h | 2 - 7 files changed, 126 insertions(+), 104 deletions(-) diff --git a/rehlds/common/mathlib.h b/rehlds/common/mathlib.h index 142c38c..9a84e92 100644 --- a/rehlds/common/mathlib.h +++ b/rehlds/common/mathlib.h @@ -13,8 +13,8 @@ * ****/ -#pragma once - +#ifndef MATHLIB_H +#define MATHLIB_H /* <42b7f> ../common/mathlib.h:3 */ typedef float vec_t; @@ -41,6 +41,50 @@ typedef union DLONG_u #define M_PI 3.14159265358979323846 +#ifdef __cplusplus +#ifdef min +#undef min +#endif + +#ifdef max +#undef max +#endif + +#ifdef clamp +#undef clamp +#endif + +template +inline T min(T a, T b) { + return (a < b) ? a : b; +} + +template +inline T max(T a, T b) { + return (a < b) ? b : a; +} + +template +inline T clamp(T a, T min, T max) { + return (a > max) ? max : (a < min) ? min : a; +} + +template +inline T bswap(T s) { + switch (sizeof(T)) { +#ifdef _WIN32 + case 2: {auto res = _byteswap_ushort(*(uint16 *)&s); return *(T *)&res;} + case 4: {auto res = _byteswap_ulong(*(uint32 *)(&s)); return *(T *)&res;} + case 8: {auto res = _byteswap_uint64(*(uint64 *)&s); return *(T *)&res;} +#else + case 2: {auto res = _bswap16(*(uint16 *)&s); return *(T *)&res;} + case 4: {auto res = _bswap(*(uint32 *)&s); return *(T *)&res;} + case 8: {auto res = _bswap64(*(uint64 *)&s); return *(T *)&res;} +#endif + default: return s; + } +} +#else // __cplusplus #ifndef max #define max(a,b) (((a) > (b)) ? (a) : (b)) #endif @@ -50,3 +94,6 @@ typedef union DLONG_u #endif #define clamp(val, min, max) (((val) > (max)) ? (max) : (((val) < (min)) ? (min) : (val))) +#endif // __cplusplus + +#endif // MATHLIB_H diff --git a/rehlds/engine/common.cpp b/rehlds/engine/common.cpp index 15fced7..b9772eb 100644 --- a/rehlds/engine/common.cpp +++ b/rehlds/engine/common.cpp @@ -265,15 +265,7 @@ float (*LittleFloat)(float l); int LongSwap(int l) { - /*byte b1, b2, b3, b4; - - b1 = l & 0xFF; - b2 = (l >> 8) & 0xFF; - b3 = (l >> 16) & 0xFF; - b4 = (l >> 24) & 0xFF; - - return ((int)b1 << 24) + ((int)b2 << 16) + ((int)b3 << 8) + b4;*/ - return _byteswap_ulong(l); + return bswap(l); } int LongNoSwap(int l) @@ -283,16 +275,7 @@ int LongNoSwap(int l) short ShortSwap(short l) { -#ifdef _WIN32 - return _byteswap_ushort(l); // xchg -#else - byte b1, b2; - - b1 = l & 0xFF; - b2 = (l >> 8) & 0xFF; - - return (b1 << 8) + b2; -#endif + return bswap(l); } short ShortNoSwap(short l) @@ -315,8 +298,9 @@ float FloatSwap(float f) dat2.b[3] = dat1.b[0]; return dat2.f;*/ - unsigned long u = _byteswap_ulong(*(unsigned long *)&f); - return *(float *)&u; + //unsigned long u = bswap(*(unsigned long *)&f); + //return *(float *)&u; + return bswap(f); } float FloatNoSwap(float f) @@ -2632,62 +2616,62 @@ void COM_Munge2(unsigned char *data, int len, int seq) unsigned int *end; unsigned int mSeq; - mSeq = _byteswap_ulong(~seq) ^ seq; + mSeq = bswap(~seq) ^ seq; len /= 4; end = (unsigned int *)data + (len & ~15); for (pc = (unsigned int *)data; pc < end; pc += 16) { - pc[0] = _byteswap_ulong(pc[0]) ^ mSeq ^ 0xFFFFE7A5; - pc[1] = _byteswap_ulong(pc[1]) ^ mSeq ^ 0xBFEFFFE5; - pc[2] = _byteswap_ulong(pc[2]) ^ mSeq ^ 0xFFBFEFFF; - pc[3] = _byteswap_ulong(pc[3]) ^ mSeq ^ 0xBFEFBFED; - pc[4] = _byteswap_ulong(pc[4]) ^ mSeq ^ 0xBFAFEFBF; - pc[5] = _byteswap_ulong(pc[5]) ^ mSeq ^ 0xFFBFAFEF; - pc[6] = _byteswap_ulong(pc[6]) ^ mSeq ^ 0xFFEFBFAD; - pc[7] = _byteswap_ulong(pc[7]) ^ mSeq ^ 0xFFFFEFBF; - pc[8] = _byteswap_ulong(pc[8]) ^ mSeq ^ 0xFFEFF7EF; - pc[9] = _byteswap_ulong(pc[9]) ^ mSeq ^ 0xBFEFE7F5; - pc[10] = _byteswap_ulong(pc[10]) ^ mSeq ^ 0xBFBFE7E5; - pc[11] = _byteswap_ulong(pc[11]) ^ mSeq ^ 0xFFAFB7E7; - pc[12] = _byteswap_ulong(pc[12]) ^ mSeq ^ 0xBFFFAFB5; - pc[13] = _byteswap_ulong(pc[13]) ^ mSeq ^ 0xBFAFFFAF; - pc[14] = _byteswap_ulong(pc[14]) ^ mSeq ^ 0xFFAFA7FF; - pc[15] = _byteswap_ulong(pc[15]) ^ mSeq ^ 0xFFEFA7A5; + pc[0] = bswap(pc[0]) ^ mSeq ^ 0xFFFFE7A5; + pc[1] = bswap(pc[1]) ^ mSeq ^ 0xBFEFFFE5; + pc[2] = bswap(pc[2]) ^ mSeq ^ 0xFFBFEFFF; + pc[3] = bswap(pc[3]) ^ mSeq ^ 0xBFEFBFED; + pc[4] = bswap(pc[4]) ^ mSeq ^ 0xBFAFEFBF; + pc[5] = bswap(pc[5]) ^ mSeq ^ 0xFFBFAFEF; + pc[6] = bswap(pc[6]) ^ mSeq ^ 0xFFEFBFAD; + pc[7] = bswap(pc[7]) ^ mSeq ^ 0xFFFFEFBF; + pc[8] = bswap(pc[8]) ^ mSeq ^ 0xFFEFF7EF; + pc[9] = bswap(pc[9]) ^ mSeq ^ 0xBFEFE7F5; + pc[10] = bswap(pc[10]) ^ mSeq ^ 0xBFBFE7E5; + pc[11] = bswap(pc[11]) ^ mSeq ^ 0xFFAFB7E7; + pc[12] = bswap(pc[12]) ^ mSeq ^ 0xBFFFAFB5; + pc[13] = bswap(pc[13]) ^ mSeq ^ 0xBFAFFFAF; + pc[14] = bswap(pc[14]) ^ mSeq ^ 0xFFAFA7FF; + pc[15] = bswap(pc[15]) ^ mSeq ^ 0xFFEFA7A5; } switch(len & 15) { case 15: - pc[14] = _byteswap_ulong(pc[14]) ^ mSeq ^ 0xFFAFA7FF; + pc[14] = bswap(pc[14]) ^ mSeq ^ 0xFFAFA7FF; case 14: - pc[13] = _byteswap_ulong(pc[13]) ^ mSeq ^ 0xBFAFFFAF; + pc[13] = bswap(pc[13]) ^ mSeq ^ 0xBFAFFFAF; case 13: - pc[12] = _byteswap_ulong(pc[12]) ^ mSeq ^ 0xBFFFAFB5; + pc[12] = bswap(pc[12]) ^ mSeq ^ 0xBFFFAFB5; case 12: - pc[11] = _byteswap_ulong(pc[11]) ^ mSeq ^ 0xFFAFB7E7; + pc[11] = bswap(pc[11]) ^ mSeq ^ 0xFFAFB7E7; case 11: - pc[10] = _byteswap_ulong(pc[10]) ^ mSeq ^ 0xBFBFE7E5; + pc[10] = bswap(pc[10]) ^ mSeq ^ 0xBFBFE7E5; case 10: - pc[9] = _byteswap_ulong(pc[9]) ^ mSeq ^ 0xBFEFE7F5; + pc[9] = bswap(pc[9]) ^ mSeq ^ 0xBFEFE7F5; case 9: - pc[8] = _byteswap_ulong(pc[8]) ^ mSeq ^ 0xFFEFF7EF; + pc[8] = bswap(pc[8]) ^ mSeq ^ 0xFFEFF7EF; case 8: - pc[7] = _byteswap_ulong(pc[7]) ^ mSeq ^ 0xFFFFEFBF; + pc[7] = bswap(pc[7]) ^ mSeq ^ 0xFFFFEFBF; case 7: - pc[6] = _byteswap_ulong(pc[6]) ^ mSeq ^ 0xFFEFBFAD; + pc[6] = bswap(pc[6]) ^ mSeq ^ 0xFFEFBFAD; case 6: - pc[5] = _byteswap_ulong(pc[5]) ^ mSeq ^ 0xFFBFAFEF; + pc[5] = bswap(pc[5]) ^ mSeq ^ 0xFFBFAFEF; case 5: - pc[4] = _byteswap_ulong(pc[4]) ^ mSeq ^ 0xBFAFEFBF; + pc[4] = bswap(pc[4]) ^ mSeq ^ 0xBFAFEFBF; case 4: - pc[3] = _byteswap_ulong(pc[3]) ^ mSeq ^ 0xBFEFBFED; + pc[3] = bswap(pc[3]) ^ mSeq ^ 0xBFEFBFED; case 3: - pc[2] = _byteswap_ulong(pc[2]) ^ mSeq ^ 0xFFBFEFFF; + pc[2] = bswap(pc[2]) ^ mSeq ^ 0xFFBFEFFF; case 2: - pc[1] = _byteswap_ulong(pc[1]) ^ mSeq ^ 0xBFEFFFE5; + pc[1] = bswap(pc[1]) ^ mSeq ^ 0xBFEFFFE5; case 1: - pc[0] = _byteswap_ulong(pc[0]) ^ mSeq ^ 0xFFFFE7A5; + pc[0] = bswap(pc[0]) ^ mSeq ^ 0xFFFFE7A5; } } #else // REHLDS_FIXES @@ -2731,62 +2715,62 @@ void COM_UnMunge2(unsigned char *data, int len, int seq) unsigned int *end; unsigned int mSeq; - mSeq = _byteswap_ulong(~seq) ^ seq; + mSeq = bswap(~seq) ^ seq; len /= 4; end = (unsigned int *)data + (len & ~15); for (pc = (unsigned int *)data; pc < end; pc += 16) { - pc[0] = _byteswap_ulong(pc[0] ^ mSeq ^ 0xFFFFE7A5); - pc[1] = _byteswap_ulong(pc[1] ^ mSeq ^ 0xBFEFFFE5); - pc[2] = _byteswap_ulong(pc[2] ^ mSeq ^ 0xFFBFEFFF); - pc[3] = _byteswap_ulong(pc[3] ^ mSeq ^ 0xBFEFBFED); - pc[4] = _byteswap_ulong(pc[4] ^ mSeq ^ 0xBFAFEFBF); - pc[5] = _byteswap_ulong(pc[5] ^ mSeq ^ 0xFFBFAFEF); - pc[6] = _byteswap_ulong(pc[6] ^ mSeq ^ 0xFFEFBFAD); - pc[7] = _byteswap_ulong(pc[7] ^ mSeq ^ 0xFFFFEFBF); - pc[8] = _byteswap_ulong(pc[8] ^ mSeq ^ 0xFFEFF7EF); - pc[9] = _byteswap_ulong(pc[9] ^ mSeq ^ 0xBFEFE7F5); - pc[10] = _byteswap_ulong(pc[10] ^ mSeq ^ 0xBFBFE7E5); - pc[11] = _byteswap_ulong(pc[11] ^ mSeq ^ 0xFFAFB7E7); - pc[12] = _byteswap_ulong(pc[12] ^ mSeq ^ 0xBFFFAFB5); - pc[13] = _byteswap_ulong(pc[13] ^ mSeq ^ 0xBFAFFFAF); - pc[14] = _byteswap_ulong(pc[14] ^ mSeq ^ 0xFFAFA7FF); - pc[15] = _byteswap_ulong(pc[15] ^ mSeq ^ 0xFFEFA7A5); + pc[0] = bswap(pc[0] ^ mSeq ^ 0xFFFFE7A5); + pc[1] = bswap(pc[1] ^ mSeq ^ 0xBFEFFFE5); + pc[2] = bswap(pc[2] ^ mSeq ^ 0xFFBFEFFF); + pc[3] = bswap(pc[3] ^ mSeq ^ 0xBFEFBFED); + pc[4] = bswap(pc[4] ^ mSeq ^ 0xBFAFEFBF); + pc[5] = bswap(pc[5] ^ mSeq ^ 0xFFBFAFEF); + pc[6] = bswap(pc[6] ^ mSeq ^ 0xFFEFBFAD); + pc[7] = bswap(pc[7] ^ mSeq ^ 0xFFFFEFBF); + pc[8] = bswap(pc[8] ^ mSeq ^ 0xFFEFF7EF); + pc[9] = bswap(pc[9] ^ mSeq ^ 0xBFEFE7F5); + pc[10] = bswap(pc[10] ^ mSeq ^ 0xBFBFE7E5); + pc[11] = bswap(pc[11] ^ mSeq ^ 0xFFAFB7E7); + pc[12] = bswap(pc[12] ^ mSeq ^ 0xBFFFAFB5); + pc[13] = bswap(pc[13] ^ mSeq ^ 0xBFAFFFAF); + pc[14] = bswap(pc[14] ^ mSeq ^ 0xFFAFA7FF); + pc[15] = bswap(pc[15] ^ mSeq ^ 0xFFEFA7A5); } switch(len & 15) { case 15: - pc[14] = _byteswap_ulong(pc[14] ^ mSeq ^ 0xFFAFA7FF); + pc[14] = bswap(pc[14] ^ mSeq ^ 0xFFAFA7FF); case 14: - pc[13] = _byteswap_ulong(pc[13] ^ mSeq ^ 0xBFAFFFAF); + pc[13] = bswap(pc[13] ^ mSeq ^ 0xBFAFFFAF); case 13: - pc[12] = _byteswap_ulong(pc[12] ^ mSeq ^ 0xBFFFAFB5); + pc[12] = bswap(pc[12] ^ mSeq ^ 0xBFFFAFB5); case 12: - pc[11] = _byteswap_ulong(pc[11] ^ mSeq ^ 0xFFAFB7E7); + pc[11] = bswap(pc[11] ^ mSeq ^ 0xFFAFB7E7); case 11: - pc[10] = _byteswap_ulong(pc[10] ^ mSeq ^ 0xBFBFE7E5); + pc[10] = bswap(pc[10] ^ mSeq ^ 0xBFBFE7E5); case 10: - pc[9] = _byteswap_ulong(pc[9] ^ mSeq ^ 0xBFEFE7F5); + pc[9] = bswap(pc[9] ^ mSeq ^ 0xBFEFE7F5); case 9: - pc[8] = _byteswap_ulong(pc[8] ^ mSeq ^ 0xFFEFF7EF); + pc[8] = bswap(pc[8] ^ mSeq ^ 0xFFEFF7EF); case 8: - pc[7] = _byteswap_ulong(pc[7] ^ mSeq ^ 0xFFFFEFBF); + pc[7] = bswap(pc[7] ^ mSeq ^ 0xFFFFEFBF); case 7: - pc[6] = _byteswap_ulong(pc[6] ^ mSeq ^ 0xFFEFBFAD); + pc[6] = bswap(pc[6] ^ mSeq ^ 0xFFEFBFAD); case 6: - pc[5] = _byteswap_ulong(pc[5] ^ mSeq ^ 0xFFBFAFEF); + pc[5] = bswap(pc[5] ^ mSeq ^ 0xFFBFAFEF); case 5: - pc[4] = _byteswap_ulong(pc[4] ^ mSeq ^ 0xBFAFEFBF); + pc[4] = bswap(pc[4] ^ mSeq ^ 0xBFAFEFBF); case 4: - pc[3] = _byteswap_ulong(pc[3] ^ mSeq ^ 0xBFEFBFED); + pc[3] = bswap(pc[3] ^ mSeq ^ 0xBFEFBFED); case 3: - pc[2] = _byteswap_ulong(pc[2] ^ mSeq ^ 0xFFBFEFFF); + pc[2] = bswap(pc[2] ^ mSeq ^ 0xFFBFEFFF); case 2: - pc[1] = _byteswap_ulong(pc[1] ^ mSeq ^ 0xBFEFFFE5); + pc[1] = bswap(pc[1] ^ mSeq ^ 0xBFEFFFE5); case 1: - pc[0] = _byteswap_ulong(pc[0] ^ mSeq ^ 0xFFFFE7A5); + pc[0] = bswap(pc[0] ^ mSeq ^ 0xFFFFE7A5); } } #else // REHLDS_FIXES diff --git a/rehlds/engine/host.cpp b/rehlds/engine/host.cpp index 3a885b3..4d0ea4c 100644 --- a/rehlds/engine/host.cpp +++ b/rehlds/engine/host.cpp @@ -531,6 +531,10 @@ void SV_DropClient_internal(client_t *cl, qboolean crash, const char *string) Q_memset(cl->userinfo, 0, sizeof(cl->userinfo)); Q_memset(cl->physinfo, 0, sizeof(cl->physinfo)); +#ifdef REHLDS_FIXES + g_GameClients[host_client - g_psvs.clients]->SetSpawnedOnce(false); +#endif // REHLDS_FIXES + SV_FullClientUpdate(cl, &g_psv.reliable_datagram); NotifyDedicatedServerUI("UpdatePlayers"); diff --git a/rehlds/engine/net_ws.cpp b/rehlds/engine/net_ws.cpp index cf2dc54..4532c9c 100644 --- a/rehlds/engine/net_ws.cpp +++ b/rehlds/engine/net_ws.cpp @@ -1370,7 +1370,7 @@ int NET_SendLong(netsrc_t sock, int s, const char *buf, int len, int flags, cons while (len > 0) { - size = min(SPLIT_SIZE, len); + size = min(int(SPLIT_SIZE), len); pPacket->packetID = (packetNumber << 4) + packetCount; diff --git a/rehlds/engine/sv_main.cpp b/rehlds/engine/sv_main.cpp index dfb05ce..3d394b2 100644 --- a/rehlds/engine/sv_main.cpp +++ b/rehlds/engine/sv_main.cpp @@ -4862,7 +4862,7 @@ void SV_ExtractFromUserinfo(client_t *cl) if (val[0] != 0) { i = Q_atoi(val); - cl->netchan.rate = clamp(i, MIN_RATE, MAX_RATE); + cl->netchan.rate = clamp(float(i), MIN_RATE, MAX_RATE); } val = Info_ValueForKey(userinfo, "topcolor"); diff --git a/rehlds/public/basetypes.h b/rehlds/public/basetypes.h index 5f3a677..19c3fa4 100644 --- a/rehlds/public/basetypes.h +++ b/rehlds/public/basetypes.h @@ -44,6 +44,7 @@ #define PAD_NUMBER(number, boundary) \ ( ((number) + ((boundary)-1)) / (boundary) ) * (boundary) +#ifndef MATHLIB_H // In case this ever changes #define M_PI 3.14159265358979323846 @@ -54,19 +55,7 @@ #ifndef max #define max(a,b) (((a) > (b)) ? (a) : (b)) #endif - -#ifdef __cplusplus -template -inline T clamp2(T const &val, T const &minVal, T const &maxVal) //renamed to clamp2 to avoid conflicts with clamp defined in mathlib -{ - if (val < minVal) - return minVal; - else if (val > maxVal) - return maxVal; - else - return val; -} -#endif +#endif // MATHLIB_H #ifndef FALSE #define FALSE 0 diff --git a/rehlds/public/rehlds/osconfig.h b/rehlds/public/rehlds/osconfig.h index 54bb0c8..b916ae6 100644 --- a/rehlds/public/rehlds/osconfig.h +++ b/rehlds/public/rehlds/osconfig.h @@ -166,8 +166,6 @@ #define WSAENOPROTOOPT ENOPROTOOPT - inline unsigned long _byteswap_ulong(unsigned long val) { return _bswap(val); } - #ifndef FALSE #define FALSE 0 #endif