Merge branch 'develop'

This commit is contained in:
FutureRave 2022-05-03 21:05:34 +01:00
commit 1c008b585d
82 changed files with 5026 additions and 5120 deletions

View File

@ -39,7 +39,7 @@ jobs:
uses: microsoft/setup-msbuild@v1.1 uses: microsoft/setup-msbuild@v1.1
- name: Generate project files - name: Generate project files
run: tools/premake5 vs2022 --ac-disable run: tools/premake5 vs2022
- name: Set up problem matching - name: Set up problem matching
uses: ammaraskar/msvc-problem-matcher@master uses: ammaraskar/msvc-problem-matcher@master

View File

@ -4,6 +4,38 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog v0.3.0](http://keepachangelog.com/en/0.3.0/) and this project adheres to [Semantic Versioning](http://semver.org/). The format is based on [Keep a Changelog v0.3.0](http://keepachangelog.com/en/0.3.0/) and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.7.1] - 2022-05-03
### Added
- Add ToUpper GSC Function (#216)
- Add StrICmp GSC Function (#216)
- Add IsEndStr GSC Function (#216)
- Add DropAllBots GSC Function (#174)
- Add GSC entity field `entityflags` (#228)
- Add GSC client field `clientflags` (#228)
- Add bg_surfacePenetration Dvar (#241)
- Add bg_bulletRange Dvar (#241)
### Changed
- Test clients' native functionality has been restored by default (#162)
- Custom GSC functions can be called correctly from a game script (#216)
- Master server list will be used instead of the node system (load server list faster) (#234)
### Fixed
- Fixed issue with mouse acceleration when polling rate is greater than 125Hz (#230)
- Fixed issue with player speed caused by sprinting from the prone position (#232)
- Fixed client crash when cg_chatHeight was set to 0 (#237)
- Fixed GSC function Scr_TableLookupIStringByRow (#162)
### Known issue
- HTTPS is not supported for fast downloads at the moment.
- Sound issue fix is experimental as the bug is not fully understood.
- `reloadmenus` command does not free resources used by custom menus.
## [0.7.0] - 2022-05-01 ## [0.7.0] - 2022-05-01
### Added ### Added

View File

@ -19,15 +19,9 @@
|:----------------------------|:-----------------------------------------------| |:----------------------------|:-----------------------------------------------|
| `--copy-to=PATH` | Optional, copy the DLL to a custom folder after build, define the path here if wanted. | | `--copy-to=PATH` | Optional, copy the DLL to a custom folder after build, define the path here if wanted. |
| `--copy-pdb` | Copy debug information for binaries as well to the path given via --copy-to. | | `--copy-pdb` | Copy debug information for binaries as well to the path given via --copy-to. |
| `--ac-disable` | Disable anticheat. |
| `--ac-debug-detections` | Log anticheat detections. |
| `--ac-debug-load-library` | Log libraries that get loaded. |
| `--force-unit-tests` | Always compile unit tests. | | `--force-unit-tests` | Always compile unit tests. |
| `--force-exception-handler` | Install custom unhandled exception handler even for Debug builds. | | `--force-exception-handler` | Install custom unhandled exception handler even for Debug builds. |
| `--force-minidump-upload` | Upload minidumps even for Debug builds. | | `--force-minidump-upload` | Upload minidumps even for Debug builds. |
| `--disable-bitmessage` | Disable use of BitMessage completely. |
| `--disable-base128` | Disable base128 encoding for minidumps. |
| `--no-new-structure` | Do not use new virtual path structure (separating headers and source files). |
| `--iw4x-zones` | Zonebuilder generates iw4x zones that cannot be loaded without IW4x specific patches. | | `--iw4x-zones` | Zonebuilder generates iw4x zones that cannot be loaded without IW4x specific patches. |
## Command line arguments ## Command line arguments

2
deps/libtommath vendored

@ -1 +1 @@
Subproject commit 5108f12350b6daa4aa5dbc846517ad1db2f8388a Subproject commit 4b47368501321c795d5b54d87a5bab35a21a7940

View File

@ -1,4 +1,4 @@
@echo off @echo off
echo Updating submodules... echo Updating submodules...
call git submodule update --init --recursive call git submodule update --init --recursive
call tools\premake5 %* vs2022 --ac-disable call tools\premake5 %* vs2022

Binary file not shown.

Binary file not shown.

View File

@ -1,102 +0,0 @@
#pragma once
#if defined(__APPLE__) || defined(__unix__)
#define VMP_IMPORT
#define VMP_API
#define VMP_WCHAR unsigned short
#else
#define VMP_IMPORT __declspec(dllimport)
#define VMP_API __stdcall
#define VMP_WCHAR wchar_t
#ifdef _WIN64
#pragma comment(lib, "VMProtectSDK64.lib")
#else
#pragma comment(lib, "VMProtectSDK32.lib")
#endif // _WIN64
#endif // __APPLE__ || __unix__
#ifdef __cplusplus
extern "C" {
#endif
// protection
VMP_IMPORT void VMP_API VMProtectBegin(const char *);
VMP_IMPORT void VMP_API VMProtectBeginVirtualization(const char *);
VMP_IMPORT void VMP_API VMProtectBeginMutation(const char *);
VMP_IMPORT void VMP_API VMProtectBeginUltra(const char *);
VMP_IMPORT void VMP_API VMProtectBeginVirtualizationLockByKey(const char *);
VMP_IMPORT void VMP_API VMProtectBeginUltraLockByKey(const char *);
VMP_IMPORT void VMP_API VMProtectEnd(void);
// utils
VMP_IMPORT bool VMP_API VMProtectIsProtected();
VMP_IMPORT bool VMP_API VMProtectIsDebuggerPresent(bool);
VMP_IMPORT bool VMP_API VMProtectIsVirtualMachinePresent(void);
VMP_IMPORT bool VMP_API VMProtectIsValidImageCRC(void);
VMP_IMPORT const char * VMP_API VMProtectDecryptStringA(const char *value);
VMP_IMPORT const VMP_WCHAR * VMP_API VMProtectDecryptStringW(const VMP_WCHAR *value);
VMP_IMPORT bool VMP_API VMProtectFreeString(const void *value);
// licensing
enum VMProtectSerialStateFlags
{
SERIAL_STATE_SUCCESS = 0,
SERIAL_STATE_FLAG_CORRUPTED = 0x00000001,
SERIAL_STATE_FLAG_INVALID = 0x00000002,
SERIAL_STATE_FLAG_BLACKLISTED = 0x00000004,
SERIAL_STATE_FLAG_DATE_EXPIRED = 0x00000008,
SERIAL_STATE_FLAG_RUNNING_TIME_OVER = 0x00000010,
SERIAL_STATE_FLAG_BAD_HWID = 0x00000020,
SERIAL_STATE_FLAG_MAX_BUILD_EXPIRED = 0x00000040,
};
#pragma pack(push, 1)
typedef struct
{
unsigned short wYear;
unsigned char bMonth;
unsigned char bDay;
} VMProtectDate;
typedef struct
{
int nState; // VMProtectSerialStateFlags
VMP_WCHAR wUserName[256]; // user name
VMP_WCHAR wEMail[256]; // email
VMProtectDate dtExpire; // date of serial number expiration
VMProtectDate dtMaxBuild; // max date of build, that will accept this key
int bRunningTime; // running time in minutes
unsigned char nUserDataLength; // length of user data in bUserData
unsigned char bUserData[255]; // up to 255 bytes of user data
} VMProtectSerialNumberData;
#pragma pack(pop)
VMP_IMPORT int VMP_API VMProtectSetSerialNumber(const char *serial);
VMP_IMPORT int VMP_API VMProtectGetSerialNumberState();
VMP_IMPORT bool VMP_API VMProtectGetSerialNumberData(VMProtectSerialNumberData *data, int size);
VMP_IMPORT int VMP_API VMProtectGetCurrentHWID(char *hwid, int size);
// activation
enum VMProtectActivationFlags
{
ACTIVATION_OK = 0,
ACTIVATION_SMALL_BUFFER,
ACTIVATION_NO_CONNECTION,
ACTIVATION_BAD_REPLY,
ACTIVATION_BANNED,
ACTIVATION_CORRUPTED,
ACTIVATION_BAD_CODE,
ACTIVATION_ALREADY_USED,
ACTIVATION_SERIAL_UNKNOWN,
ACTIVATION_EXPIRED,
ACTIVATION_NOT_AVAILABLE
};
VMP_IMPORT int VMP_API VMProtectActivateLicense(const char *code, char *serial, int size);
VMP_IMPORT int VMP_API VMProtectDeactivateLicense(const char *serial);
VMP_IMPORT int VMP_API VMProtectGetOfflineActivationString(const char *code, char *buf, int size);
VMP_IMPORT int VMP_API VMProtectGetOfflineDeactivationString(const char *serial, char *buf, int size);
#ifdef __cplusplus
}
#endif

View File

@ -71,21 +71,6 @@ newoption {
description = "Copy debug information for binaries as well to the path given via --copy-to." description = "Copy debug information for binaries as well to the path given via --copy-to."
} }
newoption {
trigger = "ac-disable",
description = "Disable anticheat."
}
newoption {
trigger = "ac-debug-detections",
description = "Log anticheat detections."
}
newoption {
trigger = "ac-debug-load-library",
description = "Log libraries that get loaded."
}
newoption { newoption {
trigger = "force-unit-tests", trigger = "force-unit-tests",
description = "Always compile unit tests." description = "Always compile unit tests."
@ -269,23 +254,11 @@ workspace "iw4x"
"./src", "./src",
"./lib/include", "./lib/include",
} }
syslibdirs {
"./lib/bin",
}
resincludedirs { resincludedirs {
"$(ProjectDir)src" -- fix for VS IDE "$(ProjectDir)src" -- fix for VS IDE
} }
-- Debug flags -- Debug flags
if _OPTIONS["ac-disable"] then
defines {"DISABLE_ANTICHEAT"}
end
if _OPTIONS["ac-debug-detections"] then
defines {"DEBUG_DETECTIONS"}
end
if _OPTIONS["ac-debug-load-library"] then
defines {"DEBUG_LOAD_LIBRARY"}
end
if _OPTIONS["force-unit-tests"] then if _OPTIONS["force-unit-tests"] then
defines {"FORCE_UNIT_TESTS"} defines {"FORCE_UNIT_TESTS"}
end end

View File

@ -69,7 +69,6 @@ namespace Components
Loader::Register(new Renderer()); Loader::Register(new Renderer());
Loader::Register(new UIFeeder()); Loader::Register(new UIFeeder());
Loader::Register(new UIScript()); Loader::Register(new UIScript());
Loader::Register(new AntiCheat());
Loader::Register(new Changelog()); Loader::Register(new Changelog());
Loader::Register(new Dedicated()); Loader::Register(new Dedicated());
Loader::Register(new Discovery()); Loader::Register(new Discovery());
@ -84,6 +83,7 @@ namespace Components
Loader::Register(new ModelSurfs()); Loader::Register(new ModelSurfs());
Loader::Register(new PlayerName()); Loader::Register(new PlayerName());
Loader::Register(new QuickPatch()); Loader::Register(new QuickPatch());
Loader::Register(new Security());
Loader::Register(new ServerInfo()); Loader::Register(new ServerInfo());
Loader::Register(new ServerList()); Loader::Register(new ServerList());
Loader::Register(new SlowMotion()); Loader::Register(new SlowMotion());
@ -104,8 +104,9 @@ namespace Components
Loader::Register(new Movement()); Loader::Register(new Movement());
Loader::Register(new Elevators()); Loader::Register(new Elevators());
Loader::Register(new ClientCommand()); Loader::Register(new ClientCommand());
Loader::Register(new ScriptExtension());
Loader::Register(new Client()); Loader::Register(new RawMouse());
Loader::Register(new Bullet());
Loader::Pregame = false; Loader::Pregame = false;
} }

View File

@ -85,6 +85,7 @@ namespace Components
#include "Modules/Network.hpp" #include "Modules/Network.hpp"
#include "Modules/Theatre.hpp" #include "Modules/Theatre.hpp"
#include "Modules/QuickPatch.hpp" #include "Modules/QuickPatch.hpp"
#include "Modules/Security.hpp"
#include "Modules/Node.hpp" #include "Modules/Node.hpp"
#include "Modules/RCon.hpp" #include "Modules/RCon.hpp"
#include "Modules/Party.hpp" // Destroys the order, but requires network classes :D #include "Modules/Party.hpp" // Destroys the order, but requires network classes :D
@ -99,7 +100,6 @@ namespace Components
#include "Modules/RawFiles.hpp" #include "Modules/RawFiles.hpp"
#include "Modules/Renderer.hpp" #include "Modules/Renderer.hpp"
#include "Modules/UIFeeder.hpp" #include "Modules/UIFeeder.hpp"
#include "Modules/AntiCheat.hpp"
#include "Modules/Changelog.hpp" #include "Modules/Changelog.hpp"
#include "Modules/Dedicated.hpp" #include "Modules/Dedicated.hpp"
#include "Modules/Discovery.hpp" #include "Modules/Discovery.hpp"
@ -134,6 +134,7 @@ namespace Components
#include "Modules/Movement.hpp" #include "Modules/Movement.hpp"
#include "Modules/Elevators.hpp" #include "Modules/Elevators.hpp"
#include "Modules/ClientCommand.hpp" #include "Modules/ClientCommand.hpp"
#include "Modules/Gamepad.hpp" #include "Modules/Gamepad.hpp"
#include "Modules/Client.hpp" #include "Modules/ScriptExtension.hpp"
#include "Modules/RawMouse.hpp"
#include "Modules/Bullet.hpp"

View File

@ -1,944 +0,0 @@
#include <STDInclude.hpp>
namespace Components
{
Utils::Time::Interval AntiCheat::LastCheck;
Utils::Hook AntiCheat::CreateThreadHook;
Utils::Hook AntiCheat::LoadLibHook[6];
Utils::Hook AntiCheat::VirtualProtectHook[2];
unsigned long AntiCheat::Flags = NO_FLAG;
std::mutex AntiCheat::ThreadMutex;
std::vector<DWORD> AntiCheat::OwnThreadIds;
std::map<DWORD, std::shared_ptr<Utils::Hook>> AntiCheat::ThreadHookMap;
// This function does nothing, it only adds the two passed variables and returns the value
// The only important thing it does is to clean the first parameter, and then return
// By returning, the crash procedure will be called, as it hasn't been cleaned from the stack
__declspec(naked) void AntiCheat::NullSub()
{
__asm
{
push ebp
push ecx
mov ebp, esp
xor eax, eax
mov eax, [ebp + 8h]
mov ecx, [ebp + 0Ch]
add eax, ecx
pop ecx
pop ebp
retn 4
}
}
void AntiCheat::CrashClient()
{
__VMProtectBeginUltra("");
#ifdef DEBUG_DETECTIONS
Logger::Flush();
MessageBoxA(nullptr, "Check the log for more information!", "AntiCheat triggered", MB_ICONERROR);
ExitProcess(0xFFFFFFFF);
#else
static std::thread triggerThread;
if (!triggerThread.joinable())
{
triggerThread = std::thread([]()
{
std::this_thread::sleep_for(43s);
Utils::Hook::Set<BYTE>(0x41BA2C, 0xEB);
});
}
#endif
__VMProtectEnd;
}
void AntiCheat::AssertCalleeModule(void* callee)
{
__VMProtectBeginUltra("");
HMODULE hModuleSelf = nullptr, hModuleTarget = nullptr, hModuleProcess = GetModuleHandleA(nullptr);
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast<char*>(callee), &hModuleTarget);
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast<char*>(AntiCheat::AssertCalleeModule), &hModuleSelf);
if (!hModuleSelf || !hModuleTarget || !hModuleProcess || (hModuleTarget != hModuleSelf && hModuleTarget != hModuleProcess))
{
#ifdef DEBUG_DETECTIONS
char buffer[MAX_PATH] = { 0 };
GetModuleFileNameA(hModuleTarget, buffer, sizeof buffer);
Logger::Print(Utils::String::VA("AntiCheat: Callee assertion failed: %X %s", reinterpret_cast<uint32_t>(callee), buffer));
#endif
AntiCheat::CrashClient();
}
__VMProtectEnd;
}
void AntiCheat::InitLoadLibHook()
{
__VMProtectBeginUltra("");
static uint8_t kernel32Str[] = { 0xB4, 0x9A, 0x8D, 0xB1, 0x9A, 0x93, 0xCC, 0xCD, 0xD1, 0x9B, 0x93, 0x93 }; // KerNel32.dll
static uint8_t loadLibAStr[] = { 0xB3, 0x90, 0x9E, 0x9B, 0xB3, 0x96, 0x9D, 0x8D, 0x9E, 0x8D, 0x86, 0xBE }; // LoadLibraryA
static uint8_t loadLibWStr[] = { 0xB3, 0x90, 0x9E, 0x9B, 0xB3, 0x96, 0x9D, 0x8D, 0x9E, 0x8D, 0x86, 0xA8 }; // LoadLibraryW
HMODULE kernel32 = GetModuleHandleA(Utils::String::XOR(std::string(reinterpret_cast<char*>(kernel32Str), sizeof kernel32Str), -1).data());
if (kernel32)
{
FARPROC loadLibA = GetProcAddress(kernel32, Utils::String::XOR(std::string(reinterpret_cast<char*>(loadLibAStr), sizeof loadLibAStr), -1).data());
FARPROC loadLibW = GetProcAddress(kernel32, Utils::String::XOR(std::string(reinterpret_cast<char*>(loadLibWStr), sizeof loadLibWStr), -1).data());
std::string libExA = Utils::String::XOR(std::string(reinterpret_cast<char*>(loadLibAStr), sizeof loadLibAStr), -1);
std::string libExW = Utils::String::XOR(std::string(reinterpret_cast<char*>(loadLibWStr), sizeof loadLibWStr), -1);
libExA.insert(libExA.end() - 1, 'E');
libExA.insert(libExA.end() - 1, 'x');
libExW.insert(libExW.end() - 1, 'E');
libExW.insert(libExW.end() - 1, 'x');
FARPROC loadLibExA = GetProcAddress(kernel32, libExA.data());
FARPROC loadLibExW = GetProcAddress(kernel32, libExW.data());
if (loadLibA && loadLibW && loadLibExA && loadLibExW)
{
#ifdef DEBUG_LOAD_LIBRARY
AntiCheat::LoadLibHook[0].initialize(loadLibA, LoadLibaryAStub, HOOK_JUMP);
AntiCheat::LoadLibHook[1].initialize(loadLibW, LoadLibaryWStub, HOOK_JUMP);
AntiCheat::LoadLibHook[2].initialize(loadLibExA, LoadLibaryExAStub, HOOK_JUMP);
AntiCheat::LoadLibHook[3].initialize(loadLibExW, LoadLibaryExWStub, HOOK_JUMP);
#else
static uint8_t loadLibStub[] = { 0x33, 0xC0, 0xC2, 0x04, 0x00 }; // xor eax, eax; retn 04h
static uint8_t loadLibExStub[] = { 0x33, 0xC0, 0xC2, 0x0C, 0x00 }; // xor eax, eax; retn 0Ch
AntiCheat::LoadLibHook[0].initialize(loadLibA, loadLibStub, HOOK_JUMP);
AntiCheat::LoadLibHook[1].initialize(loadLibW, loadLibStub, HOOK_JUMP);
AntiCheat::LoadLibHook[2].initialize(loadLibExA, loadLibExStub, HOOK_JUMP);
AntiCheat::LoadLibHook[3].initialize(loadLibExW, loadLibExStub, HOOK_JUMP);
#endif
}
}
static uint8_t ldrLoadDllStub[] = { 0x33, 0xC0, 0xC2, 0x10, 0x00 };
static uint8_t ldrLoadDll[] = { 0xB3, 0x9B, 0x8D, 0xB3, 0x90, 0x9E, 0x9B, 0xBB, 0x93, 0x93 }; // LdrLoadDll
HMODULE ntdll = Utils::GetNTDLL();
//AntiCheat::LoadLibHook[4].initialize(GetProcAddress(ntdll, Utils::String::XOR(std::string(reinterpret_cast<char*>(ldrLoadDll), sizeof ldrLoadDll), -1).data()), ldrLoadDllStub, HOOK_JUMP);
// Patch LdrpLoadDll
Utils::Hook::Signature::Container container;
container.signature = "\x8B\xFF\x55\x8B\xEC\x83\xE4\xF8\x81\xEC\x00\x00\x00\x00\xA1\x00\x00\x00\x00\x33\xC4\x89\x84\x24\x00\x00\x00\x00\x53\x8B\x5D\x10\x56\x57";
container.mask = "xxxxxxxxxx????x????xxxxx????xxxxxx";
container.callback = [](char* addr)
{
static uint8_t ldrpLoadDllStub[] = { 0x33, 0xC0, 0xC2, 0x0C, 0x00 };
AntiCheat::LoadLibHook[5].initialize(addr, ldrpLoadDllStub, HOOK_JUMP);
};
Utils::Hook::Signature signature(ntdll, Utils::GetModuleSize(ntdll));
signature.add(container);
//signature.process();
__VMProtectEnd;
}
void AntiCheat::ReadIntegrityCheck()
{
__VMProtectBeginUltra("");
#ifdef PROCTECT_PROCESS
static Utils::Time::Interval check;
if (check.elapsed(20s))
{
check.update();
if (HANDLE h = OpenProcess(PROCESS_VM_READ, FALSE, GetCurrentProcessId()))
{
#ifdef DEBUG_DETECTIONS
Logger::Print("AntiCheat: Process integrity check failed");
#endif
CloseHandle(h);
AntiCheat::CrashClient();
}
}
// Set the integrity flag
AntiCheat::Flags |= AntiCheat::IntergrityFlag::READ_INTEGRITY_CHECK;
#endif
__VMProtectEnd;
}
void AntiCheat::FlagIntegrityCheck()
{
__VMProtectBeginUltra("");
static Utils::Time::Interval check;
if (check.elapsed(30s))
{
check.update();
unsigned long flags = ((AntiCheat::IntergrityFlag::MAX_FLAG - 1) << 1) - 1;
if (AntiCheat::Flags != flags)
{
#ifdef DEBUG_DETECTIONS
Logger::Print(Utils::String::VA("AntiCheat: Flag integrity check failed: %X", AntiCheat::Flags));
#endif
AntiCheat::CrashClient();
}
}
__VMProtectEnd;
}
void AntiCheat::ScanIntegrityCheck()
{
__VMProtectBeginUltra("");
// If there was no check within the last 40 seconds, crash!
if (AntiCheat::LastCheck.elapsed(40s))
{
#ifdef DEBUG_DETECTIONS
Logger::Print("AntiCheat: Integrity check failed");
#endif
AntiCheat::CrashClient();
}
// Set the integrity flag
AntiCheat::Flags |= AntiCheat::IntergrityFlag::SCAN_INTEGRITY_CHECK;
__VMProtectEnd;
}
void AntiCheat::PerformScan()
{
__VMProtectBeginUltra("");
static std::optional<unsigned int> hashVal;
// Perform check only every 20 seconds
if (!AntiCheat::LastCheck.elapsed(20s)) return;
AntiCheat::LastCheck.update();
// Hash .text segment
// Add 1 to each value, so searching in memory doesn't reveal anything
size_t textSize = 0x2D6001;
char* textBase = reinterpret_cast<char*>(0x401001);
unsigned int hash = Utils::Cryptography::JenkinsOneAtATime::Compute(textBase - 1, textSize - 1);
// Set the hash, if none is set
if (!hashVal.has_value())
{
hashVal.emplace(hash);
}
// Crash if the hashes don't match
else if (hashVal.value() != hash)
{
#ifdef DEBUG_DETECTIONS
Logger::Print("AntiCheat: Memory scan failed");
#endif
AntiCheat::CrashClient();
}
// Set the memory scan flag
AntiCheat::Flags |= AntiCheat::IntergrityFlag::MEMORY_SCAN;
__VMProtectEnd;
}
void AntiCheat::QuickCodeScanner1()
{
__VMProtectBeginUltra("");
static Utils::Time::Interval interval;
static std::optional<unsigned int> hashVal;
if (!interval.elapsed(32s)) return;
interval.update();
// Hash .text segment
// Add 1 to each value, so searching in memory doesn't reveal anything
size_t textSize = 0x2D5FFF;
char* textBase = reinterpret_cast<char*>(0x400FFF);
unsigned int hash = Utils::Cryptography::JenkinsOneAtATime::Compute(textBase + 1, textSize + 1);
if (hashVal.has_value() && hash != hashVal.value())
{
Utils::Hook::Set<BYTE>(0x42A667, 0x90); // Crash
}
hashVal.emplace(hash);
__VMProtectEnd;
}
void AntiCheat::QuickCodeScanner2()
{
__VMProtectBeginUltra("");
static Utils::Time::Interval interval;
static std::optional<unsigned int> hashVal;
if (!interval.elapsed(42s)) return;
interval.update();
// Hash .text segment
unsigned int hash = Utils::Cryptography::JenkinsOneAtATime::Compute(reinterpret_cast<char*>(0x401000), 0x2D6000);
if (hashVal.has_value() && hash != hashVal.value())
{
Utils::Hook::Set<BYTE>(0x40797C, 0x90); // Crash
}
hashVal.emplace(hash);
__VMProtectEnd;
}
#ifdef DEBUG_LOAD_LIBRARY
HANDLE AntiCheat::LoadLibary(std::wstring library, HANDLE file, DWORD flags, void* callee)
{
HMODULE module;
char buffer[MAX_PATH] = { 0 };
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast<char*>(callee), &module);
GetModuleFileNameA(module, buffer, sizeof buffer);
MessageBoxA(nullptr, Utils::String::VA("Loading library %s via %s %X", std::string(library.begin(), library.end()).data(), buffer, reinterpret_cast<uint32_t>(callee)), nullptr, 0);
AntiCheat::LoadLibHook[3].uninstall();
HANDLE h = LoadLibraryExW(library.data(), file, flags);
AntiCheat::LoadLibHook[3].install();
return h;
}
HANDLE WINAPI AntiCheat::LoadLibaryAStub(const char* library)
{
std::string lib(library);
return AntiCheat::LoadLibary(std::wstring(lib.begin(), lib.end()), nullptr, 0, _ReturnAddress());
}
HANDLE WINAPI AntiCheat::LoadLibaryWStub(const wchar_t* library)
{
return AntiCheat::LoadLibary(library, nullptr, 0, _ReturnAddress());
}
HANDLE WINAPI AntiCheat::LoadLibaryExAStub(const char* library, HANDLE file, DWORD flags)
{
std::string lib(library);
return AntiCheat::LoadLibary(std::wstring(lib.begin(), lib.end()), file, flags, _ReturnAddress());
}
HANDLE WINAPI AntiCheat::LoadLibaryExWStub(const wchar_t* library, HANDLE file, DWORD flags)
{
return AntiCheat::LoadLibary(library, file, flags, _ReturnAddress());
}
#endif
void AntiCheat::UninstallLibHook()
{
for (int i = 0; i < ARRAYSIZE(AntiCheat::LoadLibHook); ++i)
{
AntiCheat::LoadLibHook[i].uninstall();
}
}
void AntiCheat::InstallLibHook()
{
for (int i = 0; i < ARRAYSIZE(AntiCheat::LoadLibHook); ++i)
{
AntiCheat::LoadLibHook[i].install();
}
}
void AntiCheat::PatchWinAPI()
{
LibUnlocker _;
// Initialize directx
Utils::Hook::Call<void()>(0x5078C0)();
}
void AntiCheat::SoundInitStub(int a1, int a2, int a3)
{
LibUnlocker _;
Game::SND_Init(a1, a2, a3);
}
void AntiCheat::SoundInitDriverStub()
{
LibUnlocker _;
Game::SND_InitDriver();
}
void AntiCheat::LostD3DStub()
{
LibUnlocker _;
// Reset directx
Utils::Hook::Call<void()>(0x508070)();
}
__declspec(naked) void AntiCheat::CinematicStub()
{
__asm
{
pushad
call AntiCheat::UninstallLibHook
popad
call Game::R_Cinematic_StartPlayback_Now
pushad
call AntiCheat::InstallLibHook
popad
retn
}
}
__declspec(naked) void AntiCheat::DObjGetWorldTagPosStub()
{
__asm
{
pushad
push [esp + 20h]
call AntiCheat::AssertCalleeModule
pop esi
popad
push ecx
mov ecx, [esp + 10h]
push 426585h
retn
}
}
__declspec(naked) void AntiCheat::AimTargetGetTagPosStub()
{
__asm
{
pushad
push [esp + 20h]
call AntiCheat::AssertCalleeModule
pop esi
popad
sub esp, 14h
cmp dword ptr[esi + 0E0h], 1
push 56AC6Ah
ret
}
}
bool AntiCheat::IsPageChangeAllowed(void* callee, void* addr, size_t len)
{
__VMProtectBeginUltra("");
HMODULE hModuleSelf = nullptr, hModuleTarget = nullptr, hModuleMain = GetModuleHandle(nullptr);
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast<char*>(callee), &hModuleTarget);
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast<char*>(AntiCheat::IsPageChangeAllowed), &hModuleSelf);
size_t mainSize = Utils::GetModuleSize(hModuleMain), selfSize = Utils::GetModuleSize(hModuleSelf);
DWORD self = DWORD(hModuleSelf), main = DWORD(hModuleMain), address = DWORD(addr);
// If the address that should be changed is within our module or the main binary, then we need to check if we are changing it or someone else
if (Utils::HasIntercection(self, selfSize, address, len) || Utils::HasIntercection(main, mainSize, address, len))
{
if (!hModuleSelf || !hModuleTarget || (hModuleTarget != hModuleSelf))
{
return false;
}
}
__VMProtectEnd;
return true;
}
BOOL WINAPI AntiCheat::VirtualProtectStub(LPVOID lpAddress, SIZE_T dwSize, DWORD flNewProtect, PDWORD lpflOldProtect)
{
__VMProtectBeginUltra("");
if (!AntiCheat::IsPageChangeAllowed(_ReturnAddress(), lpAddress, dwSize)) return FALSE;
AntiCheat::VirtualProtectHook[0].uninstall(false);
BOOL result = VirtualProtect(lpAddress, dwSize, flNewProtect, lpflOldProtect);
AntiCheat::VirtualProtectHook[0].install(false);
__VMProtectEnd;
return result;
}
BOOL WINAPI AntiCheat::VirtualProtectExStub(HANDLE hProcess, LPVOID lpAddress, SIZE_T dwSize, DWORD flNewProtect, PDWORD lpflOldProtect)
{
__VMProtectBeginUltra("");
if (GetCurrentProcessId() == GetProcessId(hProcess) && !AntiCheat::IsPageChangeAllowed(_ReturnAddress(), lpAddress, dwSize)) return FALSE;
AntiCheat::VirtualProtectHook[1].uninstall(false);
BOOL result = VirtualProtectEx(hProcess, lpAddress, dwSize, flNewProtect, lpflOldProtect);
AntiCheat::VirtualProtectHook[1].install(false);
__VMProtectEnd;
return result;
}
unsigned long AntiCheat::ProtectProcess()
{
#ifdef PROCTECT_PROCESS
__VMProtectBeginUltra("");
Utils::Memory::Allocator allocator;
HANDLE hToken = nullptr;
if (!OpenProcessToken(GetCurrentProcess(), /*TOKEN_ADJUST_PRIVILEGES | */TOKEN_READ, &hToken))
{
if (!OpenThreadToken(GetCurrentThread(), /*TOKEN_ADJUST_PRIVILEGES | */TOKEN_READ, TRUE, &hToken))
{
return GetLastError();
}
}
auto freeSid = [](void* sid)
{
if (sid)
{
FreeSid(reinterpret_cast<PSID>(sid));
}
};
allocator.reference(hToken, [](void* hToken)
{
if (hToken)
{
CloseHandle(hToken);
}
});
//AntiCheat::AcquireDebugPrivilege(hToken);
DWORD dwSize = 0;
PVOID pTokenInfo = nullptr;
if (GetTokenInformation(hToken, TokenUser, nullptr, 0, &dwSize) || GetLastError() != ERROR_INSUFFICIENT_BUFFER) return GetLastError();
if (dwSize)
{
pTokenInfo = allocator.allocate(dwSize);
if (!pTokenInfo) return GetLastError();
}
if (!GetTokenInformation(hToken, TokenUser, pTokenInfo, dwSize, &dwSize) || !pTokenInfo) return GetLastError();
PSID psidCurUser = reinterpret_cast<TOKEN_USER*>(pTokenInfo)->User.Sid;
PSID psidEveryone = nullptr;
SID_IDENTIFIER_AUTHORITY sidEveryone = SECURITY_WORLD_SID_AUTHORITY;
if (!AllocateAndInitializeSid(&sidEveryone, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &psidEveryone) || !psidEveryone) return GetLastError();
allocator.reference(psidEveryone, freeSid);
PSID psidSystem = nullptr;
SID_IDENTIFIER_AUTHORITY sidSystem = SECURITY_NT_AUTHORITY;
if (!AllocateAndInitializeSid(&sidSystem, 1, SECURITY_LOCAL_SYSTEM_RID, 0, 0, 0, 0, 0, 0, 0, &psidSystem) || !psidSystem) return GetLastError();
allocator.reference(psidSystem, freeSid);
PSID psidAdmins = nullptr;
SID_IDENTIFIER_AUTHORITY sidAdministrators = SECURITY_NT_AUTHORITY;
if (!AllocateAndInitializeSid(&sidAdministrators, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &psidAdmins) || !psidAdmins) return GetLastError();
allocator.reference(psidAdmins, freeSid);
const PSID psidArray[] =
{
psidEveryone, /* Deny most rights to everyone */
psidCurUser, /* Allow what was not denied */
psidSystem, /* Full control */
psidAdmins, /* Full control */
};
// Determine required size of the ACL
dwSize = sizeof(ACL);
// First the DENY, then the ALLOW
dwSize += GetLengthSid(psidArray[0]);
dwSize += sizeof(ACCESS_DENIED_ACE) - sizeof(DWORD);
for (UINT i = 1; i < _countof(psidArray); ++i)
{
// DWORD is the SidStart field, which is not used for absolute format
dwSize += GetLengthSid(psidArray[i]);
dwSize += sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD);
}
PACL pDacl = reinterpret_cast<PACL>(allocator.allocate(dwSize));
if (!pDacl || !InitializeAcl(pDacl, dwSize, ACL_REVISION)) return GetLastError();
// Just give access to what steam needs
//static const DWORD dwPoison = 0UL | ~(SYNCHRONIZE | GENERIC_EXECUTE | GENERIC_ALL);
static const DWORD dwPoison =
/*READ_CONTROL |*/ WRITE_DAC | WRITE_OWNER |
PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD |
PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION |
PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION |
PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE |
// In addition to protected process
PROCESS_SUSPEND_RESUME | PROCESS_TERMINATE;
if (!AddAccessDeniedAce(pDacl, ACL_REVISION, dwPoison, psidArray[0])) return GetLastError();
// Standard and specific rights not explicitly denied
//static const DWORD dwAllowed = 0UL | SYNCHRONIZE;
static const DWORD dwAllowed = (~dwPoison & 0x1FFF) | SYNCHRONIZE;
if (!AddAccessAllowedAce(pDacl, ACL_REVISION, dwAllowed, psidArray[1])) return GetLastError();
// Because of ACE ordering, System will effectively have dwAllowed even
// though the ACE specifies PROCESS_ALL_ACCESS (unless software uses
// SeDebugPrivilege or SeTcbName and increases access).
// As an exercise, check behavior of tools such as Process Explorer under XP,
// Vista, and above. Vista and above should exhibit slightly different behavior
// due to Restricted tokens.
if (!AddAccessAllowedAce(pDacl, ACL_REVISION, PROCESS_ALL_ACCESS, psidArray[2])) return GetLastError();
// Because of ACE ordering, Administrators will effectively have dwAllowed
// even though the ACE specifies PROCESS_ALL_ACCESS (unless the Administrator
// invokes 'discretionary security' by taking ownership and increasing access).
// As an exercise, check behavior of tools such as Process Explorer under XP,
// Vista, and above. Vista and above should exhibit slightly different behavior
// due to Restricted tokens.
if (!AddAccessAllowedAce(pDacl, ACL_REVISION, PROCESS_ALL_ACCESS, psidArray[3])) return GetLastError();
PSECURITY_DESCRIPTOR pSecDesc = allocator.allocate<SECURITY_DESCRIPTOR>();
if (!pSecDesc) return GetLastError();
// InitializeSecurityDescriptor initializes a security descriptor in
// absolute format, rather than self-relative format. See
// http://msdn.microsoft.com/en-us/library/aa378863(VS.85).aspx
if (!InitializeSecurityDescriptor(pSecDesc, SECURITY_DESCRIPTOR_REVISION)) return GetLastError();
if (!SetSecurityDescriptorDacl(pSecDesc, TRUE, pDacl, FALSE)) return GetLastError();
__VMProtectEnd;
return SetSecurityInfo(
GetCurrentProcess(),
SE_KERNEL_OBJECT, // process object
OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
psidCurUser, // NULL, // Owner SID
nullptr, // Group SID
pDacl,
nullptr // SACL
);
#else
return 0;
#endif
}
void AntiCheat::AcquireDebugPrivilege(HANDLE hToken)
{
__VMProtectBeginUltra("");
LUID luid;
TOKEN_PRIVILEGES tp = { 0 };
DWORD cb = sizeof(TOKEN_PRIVILEGES);
if (!LookupPrivilegeValueA(nullptr, SE_DEBUG_NAME, &luid)) return;
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tp, cb, nullptr, nullptr);
//if (GetLastError() != ERROR_SUCCESS) return;
__VMProtectEnd;
}
void AntiCheat::PatchVirtualProtect(void* vp, void* vpex)
{
__VMProtectBeginUltra("");
AntiCheat::VirtualProtectHook[1].initialize(vpex, AntiCheat::VirtualProtectExStub, HOOK_JUMP)->install(true, true);
AntiCheat::VirtualProtectHook[0].initialize(vp, AntiCheat::VirtualProtectStub, HOOK_JUMP)->install(true, true);
__VMProtectEnd;
}
NTSTATUS NTAPI AntiCheat::NtCreateThreadExStub(PHANDLE phThread, ACCESS_MASK desiredAccess, LPVOID objectAttributes, HANDLE processHandle, LPTHREAD_START_ROUTINE startAddress, LPVOID parameter, BOOL createSuspended, DWORD stackZeroBits, DWORD sizeOfStackCommit, DWORD sizeOfStackReserve, LPVOID bytesBuffer)
{
__VMProtectBeginUltra("");
HANDLE hThread = nullptr;
std::lock_guard<std::mutex> _(AntiCheat::ThreadMutex);
AntiCheat::CreateThreadHook.uninstall();
NTSTATUS result = NtCreateThreadEx_t(AntiCheat::CreateThreadHook.getAddress())(&hThread, desiredAccess, objectAttributes, processHandle, startAddress, parameter, createSuspended, stackZeroBits, sizeOfStackCommit, sizeOfStackReserve, bytesBuffer);
AntiCheat::CreateThreadHook.install();
if (phThread) *phThread = hThread;
if (GetProcessId(processHandle) == GetCurrentProcessId())
{
AntiCheat::OwnThreadIds.push_back(GetThreadId(hThread));
}
__VMProtectEnd;
return result;
}
void AntiCheat::PatchThreadCreation()
{
__VMProtectBeginUltra("");
HMODULE ntdll = Utils::GetNTDLL();
if (ntdll)
{
static uint8_t ntCreateThreadEx[] = { 0xB1, 0x8B, 0xBC, 0x8D, 0x9A, 0x9E, 0x8B, 0x9A, 0xAB, 0x97, 0x8D, 0x9A, 0x9E, 0x9B, 0xBA, 0x87 }; // NtCreateThreadEx
FARPROC createThread = GetProcAddress(ntdll, Utils::String::XOR(std::string(reinterpret_cast<char*>(ntCreateThreadEx), sizeof ntCreateThreadEx), -1).data());
if (createThread)
{
AntiCheat::CreateThreadHook.initialize(createThread, AntiCheat::NtCreateThreadExStub, HOOK_JUMP)->install();
}
}
__VMProtectEnd;
}
int AntiCheat::ValidateThreadTermination(void* addr)
{
__VMProtectBeginUltra("");
{
std::lock_guard<std::mutex> _(AntiCheat::ThreadMutex);
DWORD id = GetCurrentThreadId();
auto threadHook = AntiCheat::ThreadHookMap.find(id);
if (threadHook != AntiCheat::ThreadHookMap.end())
{
threadHook->second->uninstall(false);
AntiCheat::ThreadHookMap.erase(threadHook); // Uninstall and delete the hook
return 1; // Kill
}
}
while (true)
{
std::lock_guard<std::mutex> _(AntiCheat::ThreadMutex);
// It would be better to wait for the thread
// but we don't know if there are multiple hooks at the same address
bool found = false;
for (auto threadHook : AntiCheat::ThreadHookMap)
{
if (threadHook.second->getAddress() == addr)
{
found = true;
break;
}
}
if (!found) break;
std::this_thread::sleep_for(10ms);
}
__VMProtectEnd;
return 0; // Don't kill
}
__declspec(naked) void AntiCheat::ThreadEntryPointStub()
{
__asm
{
push eax
push eax
pushad
// Reinitialize the return address
mov eax, [esp + 28h]
sub eax, 5
mov [esp + 28h], eax
push eax
call AntiCheat::ValidateThreadTermination
add esp, 4h
mov [esp + 20h], eax
popad
pop eax
test eax, eax
jz dontKill
pop eax
add esp, 4h // Remove return address (simulate a jump hook)
retn
dontKill:
pop eax
retn
}
}
void AntiCheat::VerifyThreadIntegrity()
{
__VMProtectBeginUltra("");
bool kill = true;
{
std::lock_guard<std::mutex> _(AntiCheat::ThreadMutex);
auto threadHook = std::find(AntiCheat::OwnThreadIds.begin(), AntiCheat::OwnThreadIds.end(), GetCurrentThreadId());
if (threadHook != AntiCheat::OwnThreadIds.end())
{
AntiCheat::OwnThreadIds.erase(threadHook);
kill = false;
}
}
if (kill)
{
static bool first = true;
if (first) first = false; // We can't control the main thread, as it's spawned externally
else
{
std::lock_guard<std::mutex> _(AntiCheat::ThreadMutex);
HMODULE ntdll = Utils::GetNTDLL(), targetModule;
if (!ntdll) return; // :(
void* address = Utils::GetThreadStartAddress(GetCurrentThread());
if (address)
{
GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast<char*>(address), &targetModule);
if (targetModule == ntdll) return; // Better not kill kernel threads
DWORD id = GetCurrentThreadId();
{
auto threadHook = AntiCheat::ThreadHookMap.find(id);
if (threadHook != AntiCheat::ThreadHookMap.end())
{
threadHook->second->uninstall(false);
AntiCheat::ThreadHookMap.erase(threadHook);
}
}
std::shared_ptr<Utils::Hook> hook = std::make_shared<Utils::Hook>();
AntiCheat::ThreadHookMap[id] = hook;
// Hook the entry point of the thread to properly terminate it
hook->initialize(address, AntiCheat::ThreadEntryPointStub, HOOK_CALL)->install(true, true);
}
}
}
__VMProtectEnd;
}
void AntiCheat::SystemTimeDiff(LPSYSTEMTIME stA, LPSYSTEMTIME stB, LPSYSTEMTIME stC) {
FILETIME ftA, ftB, ftC;
ULARGE_INTEGER uiA, uiB, uiC;
SystemTimeToFileTime(stA, &ftA);
SystemTimeToFileTime(stB, &ftB);
uiA.HighPart = ftA.dwHighDateTime;
uiA.LowPart = ftA.dwLowDateTime;
uiB.HighPart = ftB.dwHighDateTime;
uiB.LowPart = ftB.dwLowDateTime;
uiC.QuadPart = uiA.QuadPart - uiB.QuadPart;
ftC.dwHighDateTime = uiC.HighPart;
ftC.dwLowDateTime = uiC.LowPart;
FileTimeToSystemTime(&ftC, stC);
}
void AntiCheat::CheckStartupTime()
{
__VMProtectBeginUltra("");
FILETIME creation, exit, kernel, user;
SYSTEMTIME current, creationSt, diffSt;
GetSystemTime(&current);
GetProcessTimes(GetCurrentProcess(), &creation, &exit, &kernel, &user);
FileTimeToSystemTime(&creation, &creationSt);
AntiCheat::SystemTimeDiff(&current, &creationSt, &diffSt);
#ifdef DEBUG
char buf[512];
snprintf(buf, 512, "creation: %d:%d:%d:%d\n", creationSt.wHour, creationSt.wMinute, creationSt.wSecond, creationSt.wMilliseconds);
OutputDebugStringA(buf);
snprintf(buf, 512, "current: %d:%d:%d:%d\n", current.wHour, current.wMinute, current.wSecond, current.wMilliseconds);
OutputDebugStringA(buf);
snprintf(buf, 512, "diff: %d:%d:%d:%d\n", diffSt.wHour, diffSt.wMinute, diffSt.wSecond, diffSt.wMilliseconds);
OutputDebugStringA(buf);
#endif
// crash client if they are using process suspension to inject dlls during startup (aka before we got to here)
// maybe tweak this value depending on what the above logging reveals during testing,
// but 5 seconds seems about right for now
int time = diffSt.wMilliseconds + (diffSt.wSecond * 1000) + (diffSt.wMinute * 1000 * 60);
if (time > 5000) {
Components::AntiCheat::CrashClient();
}
// use below for logging when using StartSuspended.exe
// FILE* f = fopen("times.txt", "a");
// fwrite(buf, 1, strlen(buf), f);
// fclose(f);
__VMProtectEnd;
}
AntiCheat::AntiCheat()
{
__VMProtectBeginUltra("");
time(nullptr);
AntiCheat::Flags = NO_FLAG;
#ifndef DISABLE_ANTICHEAT
Utils::Hook(0x507BD5, AntiCheat::PatchWinAPI, HOOK_CALL).install()->quick();
Utils::Hook(0x5082FD, AntiCheat::LostD3DStub, HOOK_CALL).install()->quick();
Utils::Hook(0x51C76C, AntiCheat::CinematicStub, HOOK_CALL).install()->quick();
Utils::Hook(0x418209, AntiCheat::SoundInitStub, HOOK_CALL).install()->quick();
Utils::Hook(0x60BE9D, AntiCheat::SoundInitStub, HOOK_CALL).install()->quick();
Utils::Hook(0x60BE8E, AntiCheat::SoundInitDriverStub, HOOK_CALL).install()->quick();
Utils::Hook(0x418204, AntiCheat::SoundInitDriverStub, HOOK_CALL).install()->quick();
Scheduler::OnFrame(AntiCheat::PerformScan, true);
// Detect aimbots
Utils::Hook(0x426580, AntiCheat::DObjGetWorldTagPosStub, HOOK_JUMP).install()->quick();
Utils::Hook(0x56AC60, AntiCheat::AimTargetGetTagPosStub, HOOK_JUMP).install()->quick();
// TODO: Probably move that :P
if (!Dedicated::IsEnabled())
{
AntiCheat::InitLoadLibHook();
}
// Prevent external processes from accessing our memory
AntiCheat::ProtectProcess();
Renderer::OnDeviceRecoveryEnd([]()
{
AntiCheat::ProtectProcess();
});
// Set the integrity flag
AntiCheat::Flags |= AntiCheat::IntergrityFlag::INITIALIZATION;
#endif
__VMProtectEnd;
}
AntiCheat::~AntiCheat()
{
AntiCheat::Flags = NO_FLAG;
AntiCheat::OwnThreadIds.clear();
AntiCheat::ThreadHookMap.clear();
for (int i = 0; i < ARRAYSIZE(AntiCheat::LoadLibHook); ++i)
{
AntiCheat::LoadLibHook[i].uninstall();
}
for (int i = 0; i < ARRAYSIZE(AntiCheat::VirtualProtectHook); ++i)
{
AntiCheat::VirtualProtectHook[i].uninstall(false);
}
}
}

View File

@ -1,120 +0,0 @@
#pragma once
#ifndef DEBUG
// Hide AntiCheat in embeded symbol names
#define AntiCheat SubComponent
#else
# ifndef DISABLE_ANTICHEAT
# define DISABLE_ANTICHEAT
# endif
#endif
// Uncomment to enable process protection (conflicts with steam!)
#define PROCTECT_PROCESS
namespace Components
{
class AntiCheat : public Component
{
public:
AntiCheat();
~AntiCheat();
class LibUnlocker
{
public:
LibUnlocker()
{
UninstallLibHook();
}
~LibUnlocker()
{
InstallLibHook();
}
};
static void CrashClient();
static void InitLoadLibHook();
static void ReadIntegrityCheck();
static void ScanIntegrityCheck();
static void FlagIntegrityCheck();
static unsigned long ProtectProcess();
static void PatchVirtualProtect(void* vp, void* vpex);
static void PatchThreadCreation();
static void VerifyThreadIntegrity();
static void QuickCodeScanner1();
static void QuickCodeScanner2();
static void UninstallLibHook();
static void InstallLibHook();
static void CheckStartupTime();
static void SystemTimeDiff(LPSYSTEMTIME stA, LPSYSTEMTIME stB, LPSYSTEMTIME stC);
private:
enum IntergrityFlag
{
NO_FLAG = (0),
INITIALIZATION = (1 << 0),
MEMORY_SCAN = (1 << 1),
SCAN_INTEGRITY_CHECK = (1 << 2),
#ifdef PROCTECT_PROCESS
READ_INTEGRITY_CHECK = (1 << 3),
#endif
MAX_FLAG,
};
static Utils::Time::Interval LastCheck;
static unsigned long Flags;
static void PerformScan();
static void PatchWinAPI();
static void NullSub();
static bool IsPageChangeAllowed(void* callee, void* addr, size_t len);
static void AssertCalleeModule(void* callee);
#ifdef DEBUG_LOAD_LIBRARY
static HANDLE LoadLibary(std::wstring library, HANDLE file, DWORD flags, void* callee);
static HANDLE WINAPI LoadLibaryAStub(const char* library);
static HANDLE WINAPI LoadLibaryWStub(const wchar_t* library);
static HANDLE WINAPI LoadLibaryExAStub(const char* library, HANDLE file, DWORD flags);
static HANDLE WINAPI LoadLibaryExWStub(const wchar_t* library, HANDLE file, DWORD flags);
#endif
static BOOL WINAPI VirtualProtectStub(LPVOID lpAddress, SIZE_T dwSize, DWORD flNewProtect, PDWORD lpflOldProtect);
static BOOL WINAPI VirtualProtectExStub(HANDLE hProcess, LPVOID lpAddress, SIZE_T dwSize, DWORD flNewProtect, PDWORD lpflOldProtect);
static void LostD3DStub();
static void CinematicStub();
static void SoundInitStub(int a1, int a2, int a3);
static void SoundInitDriverStub();
static void DObjGetWorldTagPosStub();
static void AimTargetGetTagPosStub();
static void AcquireDebugPrivilege(HANDLE hToken);
static NTSTATUS NTAPI NtCreateThreadExStub(PHANDLE hThread, ACCESS_MASK desiredAccess, LPVOID objectAttributes, HANDLE processHandle, LPTHREAD_START_ROUTINE startAddress, LPVOID parameter, BOOL createSuspended, DWORD stackZeroBits, DWORD sizeOfStackCommit, DWORD sizeOfStackReserve, LPVOID bytesBuffer);
static int ValidateThreadTermination(void* addr);
static void ThreadEntryPointStub();
static std::mutex ThreadMutex;
static std::vector<DWORD> OwnThreadIds;
static std::map<DWORD, std::shared_ptr<Utils::Hook>> ThreadHookMap;
static Utils::Hook CreateThreadHook;
static Utils::Hook LoadLibHook[6];
static Utils::Hook VirtualProtectHook[2];
};
}

View File

@ -520,7 +520,7 @@ namespace Components
{ {
for (auto& asset : AssetHandler::EmptyAssets) for (auto& asset : AssetHandler::EmptyAssets)
{ {
Game::Sys_Error(25, reinterpret_cast<char*>(0x724428), Game::DB_GetXAssetTypeName(asset.first), asset.second.data()); Game::Com_PrintWarning(25, reinterpret_cast<const char*>(0x724428), Game::DB_GetXAssetTypeName(asset.first), asset.second.data());
} }
AssetHandler::EmptyAssets.clear(); AssetHandler::EmptyAssets.clear();

View File

@ -174,7 +174,7 @@ namespace Components
return; return;
} }
if (*Game::svs_numclients <= num) if (*Game::svs_clientCount <= num)
{ {
Logger::Print("Player %d is not on the server\n", num); Logger::Print("Player %d is not on the server\n", num);
return; return;
@ -187,7 +187,7 @@ namespace Components
Bans::InsertBan({guid, client->netchan.remoteAddress.ip}); Bans::InsertBan({guid, client->netchan.remoteAddress.ip});
Game::SV_KickClientError(client, reason); Game::SV_GameDropClient(num, reason.data());
} }
void Bans::UnbanClient(SteamID id) void Bans::UnbanClient(SteamID id)

View File

@ -1,104 +1,61 @@
#include <STDInclude.hpp> #include <STDInclude.hpp>
#define KEY_MASK_FIRE 1
#define KEY_MASK_SPRINT 2
#define KEY_MASK_MELEE 4
#define KEY_MASK_RELOAD 16
#define KEY_MASK_LEANLEFT 64
#define KEY_MASK_LEANRIGHT 128
#define KEY_MASK_PRONE 256
#define KEY_MASK_CROUCH 512
#define KEY_MASK_JUMP 1024
#define KEY_MASK_ADS_MODE 2048
#define KEY_MASK_TEMP_ACTION 4096
#define KEY_MASK_HOLDBREATH 8192
#define KEY_MASK_FRAG 16384
#define KEY_MASK_SMOKE 32768
#define KEY_MASK_NIGHTVISION 262144
#define KEY_MASK_ADS 524288
#define KEY_MASK_USE 0x28
#define MAX_G_BOTAI_ENTRIES 18
namespace Components namespace Components
{ {
std::vector<std::string> Bots::BotNames; std::vector<std::string> Bots::BotNames;
typedef struct BotMovementInfo_t struct BotMovementInfo
{ {
/* Actions */ int buttons; // Actions
int buttons; int8_t forward;
/* Movement */ int8_t right;
int8 forward; uint16_t weapon;
int8 right; bool active;
/* Weapon */ };
unsigned short weapon;
} BotMovementInfo_t;
static BotMovementInfo_t g_botai[MAX_G_BOTAI_ENTRIES]; static BotMovementInfo g_botai[18];
struct BotAction_t struct BotAction
{ {
const char* action; std::string action;
int key; int key;
}; };
static const BotAction_t BotActions[] = static const BotAction BotActions[] =
{ {
{ "gostand", KEY_MASK_JUMP }, { "gostand", Game::usercmdButtonBits::CMD_BUTTON_UP },
{ "gocrouch", KEY_MASK_CROUCH }, { "gocrouch", Game::usercmdButtonBits::CMD_BUTTON_CROUCH },
{ "goprone", KEY_MASK_PRONE }, { "goprone", Game::usercmdButtonBits::CMD_BUTTON_PRONE },
{ "fire", KEY_MASK_FIRE }, { "fire", Game::usercmdButtonBits::CMD_BUTTON_ATTACK },
{ "melee", KEY_MASK_MELEE }, { "melee", Game::usercmdButtonBits::CMD_BUTTON_MELEE },
{ "frag", KEY_MASK_FRAG }, { "frag", Game::usercmdButtonBits::CMD_BUTTON_FRAG },
{ "smoke", KEY_MASK_SMOKE }, { "smoke", Game::usercmdButtonBits::CMD_BUTTON_OFFHAND_SECONDARY },
{ "reload", KEY_MASK_RELOAD }, { "reload", Game::usercmdButtonBits::CMD_BUTTON_RELOAD },
{ "sprint", KEY_MASK_SPRINT }, { "sprint", Game::usercmdButtonBits::CMD_BUTTON_SPRINT },
{ "leanleft", KEY_MASK_LEANLEFT }, { "leanleft", Game::usercmdButtonBits::CMD_BUTTON_LEAN_LEFT },
{ "leanright", KEY_MASK_LEANRIGHT }, { "leanright", Game::usercmdButtonBits::CMD_BUTTON_LEAN_RIGHT },
{ "ads", KEY_MASK_ADS_MODE }, { "ads", Game::usercmdButtonBits::CMD_BUTTON_ADS },
{ "holdbreath", KEY_MASK_HOLDBREATH }, { "holdbreath", Game::usercmdButtonBits::CMD_BUTTON_BREATH },
{ "use", KEY_MASK_USE }, { "usereload", Game::usercmdButtonBits::CMD_BUTTON_USE_RELOAD },
{ "0", 8 }, { "activate", Game::usercmdButtonBits::CMD_BUTTON_ACTIVATE },
{ "1", 32 },
{ "2", 65536 },
{ "3", 131072 },
{ "4", 1048576 },
{ "5", 2097152 },
{ "6", 4194304 },
{ "7", 8388608 },
{ "8", 16777216 },
{ "9", 33554432 },
}; };
unsigned int Bots::GetClientNum(Game::client_s* cl) int Bots::BuildConnectString(char* buffer, const char* connectString, int num, int, int protocol, int checksum, int statVer, int statStuff, int port)
{ {
unsigned int num; static size_t botId = 0;
static bool loadedNames = false; // Load file only once
num = ((byte*)cl - (byte*)Game::svs_clients) / sizeof(Game::client_s);
return num;
}
bool Bots::IsValidClientNum(unsigned int cNum)
{
return (cNum >= 0) && (cNum < (unsigned int)*Game::svs_numclients);
}
void Bots::BuildConnectString(char* buffer, const char* connectString, int num, int, int protocol, int checksum, int statVer, int statStuff, int port)
{
static int botId = 0;
const char* botName; const char* botName;
if (Bots::BotNames.empty()) if (Bots::BotNames.empty() && !loadedNames)
{ {
FileSystem::File bots("bots.txt"); FileSystem::File bots("bots.txt");
loadedNames = true;
if (bots.exists()) if (bots.exists())
{ {
std::vector<std::string> names = Utils::String::Split(bots.getBuffer(), '\n'); auto names = Utils::String::Split(bots.getBuffer(), '\n');
for (auto name : names) for (auto& name : names)
{ {
Utils::String::Replace(name, "\r", ""); Utils::String::Replace(name, "\r", "");
name = Utils::String::Trim(name); name = Utils::String::Trim(name);
@ -121,276 +78,253 @@ namespace Components
botName = Utils::String::VA("bot%d", ++botId); botName = Utils::String::VA("bot%d", ++botId);
} }
strncpy_s(buffer, 0x400, Utils::String::VA(connectString, num, botName, protocol, checksum, statVer, statStuff, port), 0x400); return _snprintf_s(buffer, 0x400, _TRUNCATE, connectString, num, botName, protocol, checksum, statVer, statStuff, port);
} }
void Bots::Spawn(unsigned int count) void Bots::Spawn(unsigned int count)
{ {
for (unsigned int i = 0; i < count; ++i) for (auto i = 0u; i < count; ++i)
{ {
Scheduler::OnDelay([]() Scheduler::OnDelay([]()
{ {
for (int i = 0; i < 3; ++i) auto* ent = Game::SV_AddTestClient();
{ if (ent == nullptr)
Game::gentity_t* entRef = Game::SV_AddTestClient(); return;
if (entRef)
{ Scheduler::OnDelay([ent]()
Scheduler::OnDelay([entRef]()
{ {
Game::Scr_AddString("autoassign"); Game::Scr_AddString("autoassign");
Game::Scr_AddString("team_marinesopfor"); Game::Scr_AddString("team_marinesopfor");
Game::Scr_Notify(entRef, Game::SL_GetString("menuresponse", 0), 2); Game::Scr_Notify(ent, Game::SL_GetString("menuresponse", 0), 2);
Scheduler::OnDelay([entRef]() Scheduler::OnDelay([ent]()
{ {
Game::Scr_AddString(Utils::String::VA("class%d", Utils::Cryptography::Rand::GenerateInt() % 5)); Game::Scr_AddString(Utils::String::VA("class%u", Utils::Cryptography::Rand::GenerateInt() % 5u));
Game::Scr_AddString("changeclass"); Game::Scr_AddString("changeclass");
Game::Scr_Notify(entRef, Game::SL_GetString("menuresponse", 0), 2); Game::Scr_Notify(ent, Game::SL_GetString("menuresponse", 0), 2);
}, 1s); }, 1s);
}, 1s); }, 1s);
break;
}
}
}, 500ms * (i + 1)); }, 500ms * (i + 1));
} }
} }
void Bots::AddMethods() void Bots::AddMethods()
{ {
Script::AddFunction("SetPing", [](Game::scr_entref_t id) // gsc: self SetPing(<int>) Script::AddMethod("SetPing", [](Game::scr_entref_t entref) // gsc: self SetPing(<int>)
{ {
if (Game::Scr_GetNumParam() != 1u || Game::Scr_GetType(0) != Game::VAR_INTEGER)
{
Game::Scr_Error("^1SetPing: Needs one integer parameter!\n");
return;
}
auto ping = Game::Scr_GetInt(0); auto ping = Game::Scr_GetInt(0);
if (ping < 0 || ping > 999) ping = std::clamp(ping, 0, 999);
{
Game::Scr_Error("^1SetPing: Ping needs to between 0 and 999!\n");
return;
}
Game::gentity_t* gentity = Script::getEntFromEntRef(id); const auto* ent = Game::GetPlayerEntity(entref);
Game::client_t* client = Script::getClientFromEnt(gentity); auto* client = Script::GetClient(ent);
unsigned int clientNum = GetClientNum(client);
if (!Bots::IsValidClientNum(clientNum)) if (!client->bIsTestClient)
{
Game::Scr_Error("^1SetPing: Need to call on a player entity!\n");
return;
}
if (client->state < 3)
{
Game::Scr_Error("^1SetPing: Need to call on a connected player!\n");
return;
}
if (!client->isBot)
{ {
Game::Scr_Error("^1SetPing: Can only call on a bot!\n"); Game::Scr_Error("^1SetPing: Can only call on a bot!\n");
return; return;
} }
client->ping = (short)ping; client->ping = static_cast<int16_t>(ping);
}); });
Script::AddFunction("isBot", [](Game::scr_entref_t id) // Usage: <bot> isBot(); Script::AddMethod("IsTestClient", [](Game::scr_entref_t entref) // Usage: <bot> IsTestClient();
{ {
Game::gentity_t* gentity = Script::getEntFromEntRef(id); const auto* gentity = Game::GetPlayerEntity(entref);
Game::client_t* client = Script::getClientFromEnt(gentity); const auto* client = Script::GetClient(gentity);
unsigned int clientNum = GetClientNum(client);
if (!Bots::IsValidClientNum(clientNum)) Game::Scr_AddBool(client->bIsTestClient == 1);
{
Game::Scr_Error("^1isBot: Need to call on a player entity!\n");
return;
}
if (client->state < 3)
{
Game::Scr_Error("^1isBot: Needs to be connected.\n");
return;
}
Game::Scr_AddInt(client->isBot);
}); });
Script::AddFunction("botStop", [](Game::scr_entref_t id) // Usage: <bot> botStop(); Script::AddMethod("BotStop", [](Game::scr_entref_t entref) // Usage: <bot> BotStop();
{ {
Game::gentity_t* gentity = Script::getEntFromEntRef(id); const auto* ent = Game::GetPlayerEntity(entref);
Game::client_t* client = Script::getClientFromEnt(gentity); const auto* client = Script::GetClient(ent);
unsigned int clientNum = GetClientNum(client);
if (!Bots::IsValidClientNum(clientNum)) if (!client->bIsTestClient)
{ {
Game::Scr_Error("^1botStop: Need to call on a player entity!\n"); Game::Scr_Error("^1BotStop: Can only call on a bot!\n");
return; return;
} }
if (client->state < 3) g_botai[entref.entnum] = {0};
{ g_botai[entref.entnum].weapon = 1;
Game::Scr_Error("^1botStop: Needs to be connected.\n"); g_botai[entref.entnum].active = true;
return;
}
if (!client->isBot)
{
Game::Scr_Error("^1botStop: Can only call on a bot!\n");
return;
}
g_botai[clientNum] = { 0 };
g_botai[clientNum].weapon = 1;
}); });
Script::AddFunction("botWeapon", [](Game::scr_entref_t id) // Usage: <bot> botWeapon(<str>); Script::AddMethod("BotWeapon", [](Game::scr_entref_t entref) // Usage: <bot> BotWeapon(<str>);
{ {
if (Game::Scr_GetNumParam() != 1u || Game::Scr_GetType(0) != Game::VAR_STRING) const auto* weapon = Game::Scr_GetString(0);
const auto* ent = Game::GetPlayerEntity(entref);
const auto* client = Script::GetClient(ent);
if (!client->bIsTestClient)
{ {
Game::Scr_Error("^1botWeapon: Needs one string parameter!\n"); Game::Scr_Error("^1BotWeapon: Can only call on a bot!\n");
return; return;
} }
auto weapon = Game::Scr_GetString(0); if (weapon == nullptr || weapon[0] == '\0')
Game::gentity_t* gentity = Script::getEntFromEntRef(id);
Game::client_t* client = Script::getClientFromEnt(gentity);
unsigned int clientNum = GetClientNum(client);
if (!Bots::IsValidClientNum(clientNum))
{ {
Game::Scr_Error("^1botWeapon: Need to call on a player entity!\n"); g_botai[entref.entnum].weapon = 1;
return; return;
} }
if (client->state < 3) const auto weapId = Game::G_GetWeaponIndexForName(weapon);
{ g_botai[entref.entnum].weapon = static_cast<uint16_t>(weapId);
Game::Scr_Error("^1botWeapon: Needs to be connected.\n"); g_botai[entref.entnum].active = true;
return;
}
if (!client->isBot)
{
Game::Scr_Error("^1botWeapon: Can only call on a bot!\n");
return;
}
if (weapon == ""s)
{
g_botai[clientNum].weapon = 1;
return;
}
int weapId = Game::G_GetWeaponIndexForName(weapon);
g_botai[clientNum].weapon = (unsigned short)weapId;
}); });
Script::AddFunction("botAction", [](Game::scr_entref_t id) // Usage: <bot> botAction(<str action>); Script::AddMethod("BotAction", [](Game::scr_entref_t entref) // Usage: <bot> BotAction(<str action>);
{ {
if (Game::Scr_GetNumParam() != 1u || Game::Scr_GetType(0) != Game::VAR_STRING) const auto* action = Game::Scr_GetString(0);
if (action == nullptr)
{ {
Game::Scr_Error("^1botAction: Needs one string parameter!\n"); Game::Scr_ParamError(0, "^1BotAction: Illegal parameter!\n");
return; return;
} }
auto action = Game::Scr_GetString(0); const auto* ent = Game::GetPlayerEntity(entref);
const auto* client = Script::GetClient(ent);
Game::gentity_t* gentity = Script::getEntFromEntRef(id); if (!client->bIsTestClient)
Game::client_t* client = Script::getClientFromEnt(gentity);
unsigned int clientNum = GetClientNum(client);
if (!Bots::IsValidClientNum(clientNum))
{ {
Game::Scr_Error("^1botAction: Need to call on a player entity!\n"); Game::Scr_Error("^1BotAction: Can only call on a bot!\n");
return; return;
} }
if (client->state < 3)
{
Game::Scr_Error("^1botAction: Needs to be connected.\n");
return;
}
if (!client->isBot)
{
Game::Scr_Error("^1botAction: Can only call on a bot!\n");
return;
}
if (action[0] != '+' && action[0] != '-') if (action[0] != '+' && action[0] != '-')
{ {
Game::Scr_Error("^1botAction: Sign for action must be '+' or '-'.\n"); Game::Scr_ParamError(0, "^1BotAction: Sign for action must be '+' or '-'.\n");
return; return;
} }
for (size_t i = 0; i < sizeof(BotActions) / sizeof(BotAction_t); ++i) for (auto i = 0u; i < std::extent_v<decltype(BotActions)>; ++i)
{ {
if (strcmp(&action[1], BotActions[i].action)) if (Utils::String::ToLower(&action[1]) != BotActions[i].action)
continue; continue;
if (action[0] == '+') if (action[0] == '+')
g_botai[clientNum].buttons |= BotActions[i].key; g_botai[entref.entnum].buttons |= BotActions[i].key;
else else
g_botai[clientNum].buttons &= ~(BotActions[i].key); g_botai[entref.entnum].buttons &= ~BotActions[i].key;
g_botai[entref.entnum].active = true;
return; return;
} }
Game::Scr_Error("^1botAction: Unknown action.\n"); Game::Scr_ParamError(0, "^1BotAction: Unknown action.\n");
}); });
Script::AddFunction("botMovement", [](Game::scr_entref_t id) // Usage: <bot> botMovement(<int>, <int>); Script::AddMethod("BotMovement", [](Game::scr_entref_t entref) // Usage: <bot> BotMovement(<int>, <int>);
{ {
if (Game::Scr_GetNumParam() != 2u || Game::Scr_GetType(0) != Game::VAR_INTEGER || Game::Scr_GetType(1) != Game::VAR_INTEGER)
{
Game::Scr_Error("^1botMovement: Needs two integer parameters!\n");
return;
}
auto forwardInt = Game::Scr_GetInt(0); auto forwardInt = Game::Scr_GetInt(0);
auto rightInt = Game::Scr_GetInt(1); auto rightInt = Game::Scr_GetInt(1);
Game::gentity_t* gentity = Script::getEntFromEntRef(id); const auto* ent = Game::GetPlayerEntity(entref);
Game::client_t* client = Script::getClientFromEnt(gentity); const auto* client = Script::GetClient(ent);
unsigned int clientNum = GetClientNum(client);
if (!Bots::IsValidClientNum(clientNum)) if (!client->bIsTestClient)
{ {
Game::Scr_Error("^1botMovement: Need to call on a player entity!\n"); Game::Scr_Error("^1BotMovement: Can only call on a bot!\n");
return; return;
} }
if (client->state < 3) forwardInt = std::clamp<int>(forwardInt, std::numeric_limits<char>::min(), std::numeric_limits<char>::max());
{ rightInt = std::clamp<int>(rightInt, std::numeric_limits<char>::min(), std::numeric_limits<char>::max());
Game::Scr_Error("^1botMovement: Needs to be connected.\n");
return;
}
if (!client->isBot) g_botai[entref.entnum].forward = static_cast<int8_t>(forwardInt);
{ g_botai[entref.entnum].right = static_cast<int8_t>(rightInt);
Game::Scr_Error("^1botMovement: Can only call on a bot!\n"); g_botai[entref.entnum].active = true;
return;
}
if (forwardInt > 127)
forwardInt = 127;
if (forwardInt < -127)
forwardInt = -127;
if (rightInt > 127)
rightInt = 127;
if (rightInt < -127)
rightInt = -127;
g_botai[clientNum].forward = (int8)forwardInt;
g_botai[clientNum].right = (int8)rightInt;
}); });
} }
void Bots::BotAiAction(Game::client_t* cl)
{
if (cl->gentity == nullptr)
return;
const auto entnum = cl->gentity->s.number;
// Keep test client functionality
if (!g_botai[entnum].active)
{
Game::SV_BotUserMove(cl);
return;
}
Game::usercmd_s userCmd = {0};
userCmd.serverTime = *Game::svs_time;
userCmd.buttons = g_botai[entnum].buttons;
userCmd.forwardmove = g_botai[entnum].forward;
userCmd.rightmove = g_botai[entnum].right;
userCmd.weapon = g_botai[entnum].weapon;
Game::SV_ClientThink(cl, &userCmd);
}
constexpr auto SV_BotUserMove = 0x626E50;
__declspec(naked) void Bots::SV_BotUserMove_Hk()
{
__asm
{
pushad
push edi
call Bots::BotAiAction
add esp, 4
popad
ret
}
}
void Bots::G_SelectWeaponIndex(int clientNum, int iWeaponIndex)
{
if (g_botai[clientNum].active)
{
g_botai[clientNum].weapon = static_cast<uint16_t>(iWeaponIndex);
}
}
__declspec(naked) void Bots::G_SelectWeaponIndex_Hk()
{
__asm
{
pushad
push [esp + 0x20 + 0x8]
push [esp + 0x20 + 0x8]
call Bots::G_SelectWeaponIndex
add esp, 0x8
popad
// Code skipped by hook
mov eax, [esp + 0x8]
push eax
push 0x441B85
retn
}
}
/*
* Should be called when a client drops from the server
* but not "between levels" (Quake-III-Arena)
*/
void Bots::ClientDisconnect_Hk(int clientNum)
{
g_botai[clientNum].active = false;
// Call original function
Utils::Hook::Call<void(int)>(0x4AA430)(clientNum);
}
Bots::Bots() Bots::Bots()
{ {
// Replace connect string // Replace connect string
@ -399,61 +333,48 @@ namespace Components
// Intercept sprintf for the connect string // Intercept sprintf for the connect string
Utils::Hook(0x48ADAB, Bots::BuildConnectString, HOOK_CALL).install()->quick(); Utils::Hook(0x48ADAB, Bots::BuildConnectString, HOOK_CALL).install()->quick();
// Stop default behavour of bots spinning and shooting Utils::Hook(0x627021, Bots::SV_BotUserMove_Hk, HOOK_CALL).install()->quick();
Utils::Hook(0x627021, 0x4BB9B0, HOOK_CALL).install()->quick(); Utils::Hook(0x627241, Bots::SV_BotUserMove_Hk, HOOK_CALL).install()->quick();
Utils::Hook(0x627241, 0x4BB9B0, HOOK_CALL).install()->quick();
// zero the bot command array Utils::Hook(0x441B80, Bots::G_SelectWeaponIndex_Hk, HOOK_JUMP).install()->quick();
for (int i = 0; i < MAX_G_BOTAI_ENTRIES; i++)
// Reset BotMovementInfo.active when client is dropped
Utils::Hook(0x625235, Bots::ClientDisconnect_Hk, HOOK_CALL).install()->quick();
// Zero the bot command array
for (auto i = 0u; i < std::extent_v<decltype(g_botai)>; i++)
{ {
g_botai[i] = {0}; g_botai[i] = {0};
g_botai[i].weapon = 1; // prevent the bots from defaulting to the 'none' weapon g_botai[i].weapon = 1; // Prevent the bots from defaulting to the 'none' weapon
} }
// have the bots perform the command every server frame
Scheduler::OnFrame([]()
{
if (!Game::SV_Loaded())
return;
int time = *Game::svs_time;
int numClients = *Game::svs_numclients;
for (int i = 0; i < numClients; ++i)
{
Game::client_t* client = &Game::svs_clients[i];
if (client->state < 3)
continue;
if (!client->isBot)
continue;
Game::usercmd_s ucmd = { 0 };
ucmd.serverTime = time;
ucmd.buttons = g_botai[i].buttons;
ucmd.forwardmove = g_botai[i].forward;
ucmd.rightmove = g_botai[i].right;
ucmd.weapon = g_botai[i].weapon;
client->deltaMessage = client->netchan.outgoingSequence - 1;
Game::SV_ClientThink(client, &ucmd);
}
});
Command::Add("spawnBot", [](Command::Params* params) Command::Add("spawnBot", [](Command::Params* params)
{ {
unsigned int count = 1; auto count = 1u;
if (params->size() > 1) if (params->size() > 1)
{ {
if (params->get(1) == "all"s) count = static_cast<unsigned int>(-1); if (params->get(1) == "all"s)
else count = atoi(params->get(1)); {
count = *Game::svs_clientCount;
}
else
{
char* end;
const auto* input = params->get(1);
count = std::strtoul(input, &end, 10);
if (input == end)
{
Logger::Print("Warning: %s is not a valid input\n"
"Usage: %s optional <number of bots> or optional <\"all\">\n",
input, params->get(0));
return;
}
}
} }
count = std::min(18u, count); count = std::min(static_cast<unsigned int>(*Game::svs_clientCount), count);
// Check if ingame and host // Check if ingame and host
if (!Game::SV_Loaded()) if (!Game::SV_Loaded())
@ -470,10 +391,14 @@ namespace Components
}); });
Bots::AddMethods(); Bots::AddMethods();
}
Bots::~Bots() // In case a loaded mod didn't call "BotStop" before the VM shutdown
Script::OnVMShutdown([]
{ {
Bots::BotNames.clear(); for (auto i = 0u; i < std::extent_v<decltype(g_botai)>; i++)
{
g_botai[i].active = false;
}
});
} }
} }

View File

@ -6,17 +6,22 @@ namespace Components
{ {
public: public:
Bots(); Bots();
~Bots();
static unsigned int GetClientNum(Game::client_s*);
static bool IsValidClientNum(unsigned int);
private: private:
static std::vector<std::string> BotNames; static std::vector<std::string> BotNames;
static void BuildConnectString(char* buffer, const char* connectString, int num, int, int protocol, int checksum, int statVer, int statStuff, int port); static int BuildConnectString(char* buffer, const char* connectString, int num, int, int protocol, int checksum, int statVer, int statStuff, int port);
static void Spawn(unsigned int count); static void Spawn(unsigned int count);
static void AddMethods(); static void AddMethods();
static void BotAiAction(Game::client_t* cl);
static void SV_BotUserMove_Hk();
static void G_SelectWeaponIndex(int clientNum, int iWeaponIndex);
static void G_SelectWeaponIndex_Hk();
static void ClientDisconnect_Hk(int clientNum);
}; };
} }

View File

@ -0,0 +1,60 @@
#include <STDInclude.hpp>
namespace Components
{
Dvar::Var Bullet::BGSurfacePenetration;
Game::dvar_t* Bullet::BGBulletRange;
float Bullet::BG_GetSurfacePenetrationDepthStub(const Game::WeaponDef* weapDef, int surfaceType)
{
assert(weapDef != nullptr);
assert(weapDef->penetrateType != Game::PenetrateType::PENETRATE_TYPE_NONE);
assert(weapDef->penetrateType < Game::PenetrateType::PENETRATE_TYPE_COUNT);
assert(static_cast<size_t>(surfaceType) < Game::materialSurfType_t::SURF_TYPE_COUNT);
const auto penetrationDepth = BGSurfacePenetration.get<float>();
if (penetrationDepth > 0.0f)
{
// Custom depth
return penetrationDepth;
}
// Game's code
if (surfaceType != Game::materialSurfType_t::SURF_TYPE_DEFAULT)
{
return (*Game::penetrationDepthTable)[weapDef->penetrateType][surfaceType];
}
return 0.0f;
}
__declspec(naked) void Bullet::Bullet_FireStub()
{
__asm
{
push eax
mov eax, BGBulletRange
fld dword ptr [eax + 0x10] // dvar_t.current.value
pop eax
push 0x440346
retn
}
}
Bullet::Bullet()
{
Dvar::OnInit([]
{
BGSurfacePenetration = Dvar::Register<float>("bg_surfacePenetration", 0.0f,
0.0f, std::numeric_limits<float>::max(), Game::dvar_flag::DVAR_CODINFO,
"Set to a value greater than 0 to override the surface penetration depth");
BGBulletRange = Game::Dvar_RegisterFloat("bg_bulletRange", 8192.0f,
0.0f, std::numeric_limits<float>::max(), Game::dvar_flag::DVAR_CODINFO,
"Max range used when calculating the bullet end position");
});
Utils::Hook(0x4F6980, BG_GetSurfacePenetrationDepthStub, HOOK_JUMP).install()->quick();
Utils::Hook(0x440340, Bullet_FireStub, HOOK_JUMP).install()->quick();
}
}

View File

@ -0,0 +1,19 @@
#pragma once
namespace Components
{
class Bullet : public Component
{
public:
Bullet();
private:
static Dvar::Var BGSurfacePenetration;
// Can't use Var class inside assembly stubs
static Game::dvar_t* BGBulletRange;
static float BG_GetSurfacePenetrationDepthStub(const Game::WeaponDef* weapDef, int surfaceType);
static void Bullet_FireStub();
};
}

View File

@ -218,8 +218,5 @@ namespace Components
// This is placed here in case the anticheat has been disabled! // This is placed here in case the anticheat has been disabled!
// This checks specifically for launching the process suspended to inject a dll // This checks specifically for launching the process suspended to inject a dll
#if !defined(DISABLE_ANTICHEAT)
AntiCheat::CheckStartupTime();
#endif
} }
} }

View File

@ -11,6 +11,7 @@ namespace Components
std::uint8_t padding3[4]; std::uint8_t padding3[4];
std::int32_t tableColumn; std::int32_t tableColumn;
}; };
struct playercarddata_s struct playercarddata_s
{ {
std::uint32_t padding; std::uint32_t padding;

View File

@ -55,9 +55,5 @@ namespace Components
// Changelog // Changelog
UIFeeder::Add(62.0f, Changelog::GetChangelogCount, Changelog::GetChangelogText, Changelog::SelectChangelog); UIFeeder::Add(62.0f, Changelog::GetChangelogCount, Changelog::GetChangelogText, Changelog::SelectChangelog);
#ifndef DISABLE_ANTICHEAT
Scheduler::OnFrameAsync(AntiCheat::QuickCodeScanner1);
#endif
} }
} }

View File

@ -110,6 +110,7 @@ namespace Components
inputBuffer += lastFontIconPos - lineBuffer; inputBuffer += lastFontIconPos - lineBuffer;
lineBuffer = lastFontIconPos; lineBuffer = lastFontIconPos;
} }
*lineBuffer = 0; *lineBuffer = 0;
len = 0.0f; len = 0.0f;
Game::cgsArray[0].teamChatMsgTimes[Game::cgsArray[0].teamChatPos % chatHeight] = Game::cgArray[0].time; Game::cgsArray[0].teamChatMsgTimes[Game::cgsArray[0].teamChatPos % chatHeight] = Game::cgArray[0].time;
@ -132,7 +133,7 @@ namespace Components
const auto chatHeight = (*cg_chatHeight)->current.integer; const auto chatHeight = (*cg_chatHeight)->current.integer;
const auto chatWidth = static_cast<float>(cg_chatWidth.get<int>()); const auto chatWidth = static_cast<float>(cg_chatWidth.get<int>());
const auto chatTime = (*cg_chatTime)->current.integer; const auto chatTime = (*cg_chatTime)->current.integer;
if (chatHeight < 0 || static_cast<unsigned>(chatHeight) > std::extent_v<decltype(Game::cgs_t::teamChatMsgs)> || chatWidth <= 0 || chatTime <= 0) if (chatHeight <= 0 || static_cast<unsigned>(chatHeight) > std::extent_v<decltype(Game::cgs_t::teamChatMsgs)> || chatWidth <= 0 || chatTime <= 0)
{ {
Game::cgsArray[0].teamLastChatPos = 0; Game::cgsArray[0].teamLastChatPos = 0;
Game::cgsArray[0].teamChatPos = 0; Game::cgsArray[0].teamChatPos = 0;

View File

@ -1,176 +0,0 @@
#include <STDInclude.hpp>
namespace Components
{
void Client::AddFunctions()
{
//File functions
Script::AddFunction("fileWrite", [](Game::scr_entref_t) // gsc: fileWrite(<filepath>, <string>, <mode>)
{
std::string path = Game::Scr_GetString(0);
auto text = Game::Scr_GetString(1);
auto mode = Game::Scr_GetString(2);
if (path.empty())
{
Game::Com_Printf(0, "^1fileWrite: filepath not defined!\n");
return;
}
std::vector<const char*> queryStrings = { R"(..)", R"(../)", R"(..\)" };
for (auto i = 0u; i < queryStrings.size(); i++)
{
if (path.find(queryStrings[i]) != std::string::npos)
{
Game::Com_Printf(0, "^1fileWrite: directory traversal is not allowed!\n");
return;
}
}
if (mode != "append"s && mode != "write"s)
{
Game::Com_Printf(0, "^3fileWrite: mode not defined or was wrong, defaulting to 'write'\n");
mode = const_cast<char*>("write");
}
if (mode == "write"s)
{
FileSystem::FileWriter(path).write(text);
}
else if (mode == "append"s)
{
FileSystem::FileWriter(path, true).write(text);
}
});
Script::AddFunction("fileRead", [](Game::scr_entref_t) // gsc: fileRead(<filepath>)
{
std::string path = Game::Scr_GetString(0);
if (path.empty())
{
Game::Com_Printf(0, "^1fileRead: filepath not defined!\n");
return;
}
std::vector<const char*> queryStrings = { R"(..)", R"(../)", R"(..\)" };
for (auto i = 0u; i < queryStrings.size(); i++)
{
if (path.find(queryStrings[i]) != std::string::npos)
{
Game::Com_Printf(0, "^1fileRead: directory traversal is not allowed!\n");
return;
}
}
if (!FileSystem::FileReader(path).exists())
{
Game::Com_Printf(0, "^1fileRead: file not found!\n");
return;
}
Game::Scr_AddString(FileSystem::FileReader(path).getBuffer().data());
});
Script::AddFunction("fileExists", [](Game::scr_entref_t) // gsc: fileExists(<filepath>)
{
std::string path = Game::Scr_GetString(0);
if (path.empty())
{
Game::Com_Printf(0, "^1fileExists: filepath not defined!\n");
return;
}
std::vector<const char*> queryStrings = { R"(..)", R"(../)", R"(..\)" };
for (auto i = 0u; i < queryStrings.size(); i++)
{
if (path.find(queryStrings[i]) != std::string::npos)
{
Game::Com_Printf(0, "^1fileExists: directory traversal is not allowed!\n");
return;
}
}
Game::Scr_AddInt(FileSystem::FileReader(path).exists());
});
Script::AddFunction("fileRemove", [](Game::scr_entref_t) // gsc: fileRemove(<filepath>)
{
std::string path = Game::Scr_GetString(0);
if (path.empty())
{
Game::Com_Printf(0, "^1fileRemove: filepath not defined!\n");
return;
}
std::vector<const char*> queryStrings = { R"(..)", R"(../)", R"(..\)" };
for (auto i = 0u; i < queryStrings.size(); i++)
{
if (path.find(queryStrings[i]) != std::string::npos)
{
Game::Com_Printf(0, "^1fileRemove: directory traversal is not allowed!\n");
return;
}
}
auto p = std::filesystem::path(path);
std::string folder = p.parent_path().string();
std::string file = p.filename().string();
Game::Scr_AddInt(FileSystem::DeleteFile(folder, file));
});
}
void Client::AddMethods()
{
// Client methods
Script::AddFunction("getIp", [](Game::scr_entref_t id) // gsc: self getIp()
{
Game::gentity_t* gentity = Script::getEntFromEntRef(id);
Game::client_t* client = Script::getClientFromEnt(gentity);
if (client->state >= 3)
{
std::string ip = Game::NET_AdrToString(client->netchan.remoteAddress);
if (ip.find_first_of(":") != std::string::npos)
ip.erase(ip.begin() + ip.find_first_of(":"), ip.end()); // erase port
Game::Scr_AddString(ip.data());
}
});
Script::AddFunction("getPing", [](Game::scr_entref_t id) // gsc: self getPing()
{
Game::gentity_t* gentity = Script::getEntFromEntRef(id);
Game::client_t* client = Script::getClientFromEnt(gentity);
if (client->state >= 3)
{
int ping = (int)client->ping;
Game::Scr_AddInt(ping);
}
});
}
void Client::AddCommands()
{
Command::Add("NULL", [](Command::Params*)
{
return NULL;
});
}
Client::Client()
{
Client::AddFunctions();
Client::AddMethods();
Client::AddCommands();
}
Client::~Client()
{
}
}

View File

@ -1,17 +0,0 @@
#pragma once
namespace Components
{
class Client : public Component
{
public:
Client();
~Client();
private:
static void AddFunctions();
static void AddMethods();
static void AddCommands();
};
}

View File

@ -2,7 +2,7 @@
namespace Components namespace Components
{ {
std::unordered_map<std::string, Utils::Slot<ClientCommand::Callback>> ClientCommand::FunctionMap; std::unordered_map<std::string, std::function<void(Game::gentity_s*, Command::ServerParams*)>> ClientCommand::HandlersSV;
bool ClientCommand::CheatsOk(const Game::gentity_s* ent) bool ClientCommand::CheatsOk(const Game::gentity_s* ent)
{ {
@ -17,7 +17,7 @@ namespace Components
if (ent->health < 1) if (ent->health < 1)
{ {
Logger::Print("CheatsOk: entity %u must be alive to use this command!\n", entNum); Logger::Print("CheatsOk: entity %i must be alive to use this command!\n", entNum);
Game::SV_GameSendServerCommand(entNum, 0, Utils::String::VA("%c \"GAME_MUSTBEALIVECOMMAND\"", 0x65)); Game::SV_GameSendServerCommand(entNum, 0, Utils::String::VA("%c \"GAME_MUSTBEALIVECOMMAND\"", 0x65));
return false; return false;
} }
@ -25,49 +25,38 @@ namespace Components
return true; return true;
} }
bool ClientCommand::CallbackHandler(Game::gentity_s* ent, const char* cmd) void ClientCommand::Add(const char* name, std::function<void(Game::gentity_s*, Command::ServerParams*)> callback)
{
const auto command = Utils::String::ToLower(cmd);
if (ClientCommand::FunctionMap.find(command) != ClientCommand::FunctionMap.end())
{
ClientCommand::FunctionMap[command](ent);
return true;
}
return false;
}
void ClientCommand::Add(const char* name, Utils::Slot<Callback> callback)
{ {
const auto command = Utils::String::ToLower(name); const auto command = Utils::String::ToLower(name);
ClientCommand::FunctionMap[command] = callback; ClientCommand::HandlersSV[command] = std::move(callback);
} }
void ClientCommand::ClientCommandStub(const int clientNum) void ClientCommand::ClientCommandStub(const int clientNum)
{ {
char cmd[1024]{}; const auto ent = &Game::g_entities[clientNum];
const auto entity = &Game::g_entities[clientNum];
if (entity->client == nullptr) if (ent->client == nullptr)
{ {
Logger::Print("ClientCommand: client %d is not fully in game yet\n", clientNum); Logger::Print("ClientCommand: client %d is not fully in game yet\n", clientNum);
return; return;
} }
Game::SV_Cmd_ArgvBuffer(0, cmd, sizeof(cmd)); Command::ServerParams params;
const auto command = Utils::String::ToLower(params.get(0));
if (!ClientCommand::CallbackHandler(entity, cmd)) if (const auto got = HandlersSV.find(command); got != HandlersSV.end())
{ {
// If no callback was found call original game function got->second(ent, &params);
Utils::Hook::Call<void(const int)>(0x416790)(clientNum); return;
} }
Utils::Hook::Call<void(const int)>(0x416790)(clientNum);
} }
void ClientCommand::AddCheatCommands() void ClientCommand::AddCheatCommands()
{ {
ClientCommand::Add("noclip", [](Game::gentity_s* ent) ClientCommand::Add("noclip", [](Game::gentity_s* ent, [[maybe_unused]] Command::ServerParams* params)
{ {
if (!ClientCommand::CheatsOk(ent)) if (!ClientCommand::CheatsOk(ent))
return; return;
@ -75,13 +64,13 @@ namespace Components
ent->client->flags ^= Game::PLAYER_FLAG_NOCLIP; ent->client->flags ^= Game::PLAYER_FLAG_NOCLIP;
const auto entNum = ent->s.number; const auto entNum = ent->s.number;
Logger::Print("Noclip toggled for entity %u\n", entNum); Logger::Print("Noclip toggled for entity %i\n", entNum);
Game::SV_GameSendServerCommand(entNum, 0, Utils::String::VA("%c \"%s\"", 0x65, Game::SV_GameSendServerCommand(entNum, 0, Utils::String::VA("%c \"%s\"", 0x65,
(ent->client->flags & Game::PLAYER_FLAG_NOCLIP) ? "GAME_NOCLIPON" : "GAME_NOCLIPOFF")); (ent->client->flags & Game::PLAYER_FLAG_NOCLIP) ? "GAME_NOCLIPON" : "GAME_NOCLIPOFF"));
}); });
ClientCommand::Add("ufo", [](Game::gentity_s* ent) ClientCommand::Add("ufo", [](Game::gentity_s* ent, [[maybe_unused]] Command::ServerParams* params)
{ {
if (!ClientCommand::CheatsOk(ent)) if (!ClientCommand::CheatsOk(ent))
return; return;
@ -89,13 +78,13 @@ namespace Components
ent->client->flags ^= Game::PLAYER_FLAG_UFO; ent->client->flags ^= Game::PLAYER_FLAG_UFO;
const auto entNum = ent->s.number; const auto entNum = ent->s.number;
Logger::Print("UFO toggled for entity %u\n", entNum); Logger::Print("UFO toggled for entity %i\n", entNum);
Game::SV_GameSendServerCommand(entNum, 0, Utils::String::VA("%c \"%s\"", 0x65, Game::SV_GameSendServerCommand(entNum, 0, Utils::String::VA("%c \"%s\"", 0x65,
(ent->client->flags & Game::PLAYER_FLAG_UFO) ? "GAME_UFOON" : "GAME_UFOOFF")); (ent->client->flags & Game::PLAYER_FLAG_UFO) ? "GAME_UFOON" : "GAME_UFOOFF"));
}); });
ClientCommand::Add("god", [](Game::gentity_s* ent) ClientCommand::Add("god", [](Game::gentity_s* ent, [[maybe_unused]] Command::ServerParams* params)
{ {
if (!ClientCommand::CheatsOk(ent)) if (!ClientCommand::CheatsOk(ent))
return; return;
@ -103,13 +92,13 @@ namespace Components
ent->flags ^= Game::FL_GODMODE; ent->flags ^= Game::FL_GODMODE;
const auto entNum = ent->s.number; const auto entNum = ent->s.number;
Logger::Print("God toggled for entity %u\n", entNum); Logger::Print("God toggled for entity %i\n", entNum);
Game::SV_GameSendServerCommand(entNum, 0, Utils::String::VA("%c \"%s\"", 0x65, Game::SV_GameSendServerCommand(entNum, 0, Utils::String::VA("%c \"%s\"", 0x65,
(ent->flags & Game::FL_GODMODE) ? "GAME_GODMODE_ON" : "GAME_GODMODE_OFF")); (ent->flags & Game::FL_GODMODE) ? "GAME_GODMODE_ON" : "GAME_GODMODE_OFF"));
}); });
ClientCommand::Add("demigod", [](Game::gentity_s* ent) ClientCommand::Add("demigod", [](Game::gentity_s* ent, [[maybe_unused]] Command::ServerParams* params)
{ {
if (!ClientCommand::CheatsOk(ent)) if (!ClientCommand::CheatsOk(ent))
return; return;
@ -117,13 +106,13 @@ namespace Components
ent->flags ^= Game::FL_DEMI_GODMODE; ent->flags ^= Game::FL_DEMI_GODMODE;
const auto entNum = ent->s.number; const auto entNum = ent->s.number;
Logger::Print("Demigod toggled for entity %u\n", entNum); Logger::Print("Demigod toggled for entity %i\n", entNum);
Game::SV_GameSendServerCommand(entNum, 0, Utils::String::VA("%c \"%s\"", 0x65, Game::SV_GameSendServerCommand(entNum, 0, Utils::String::VA("%c \"%s\"", 0x65,
(ent->flags & Game::FL_DEMI_GODMODE) ? "GAME_DEMI_GODMODE_ON" : "GAME_DEMI_GODMODE_OFF")); (ent->flags & Game::FL_DEMI_GODMODE) ? "GAME_DEMI_GODMODE_ON" : "GAME_DEMI_GODMODE_OFF"));
}); });
ClientCommand::Add("notarget", [](Game::gentity_s* ent) ClientCommand::Add("notarget", [](Game::gentity_s* ent, [[maybe_unused]] Command::ServerParams* params)
{ {
if (!ClientCommand::CheatsOk(ent)) if (!ClientCommand::CheatsOk(ent))
return; return;
@ -131,23 +120,22 @@ namespace Components
ent->flags ^= Game::FL_NOTARGET; ent->flags ^= Game::FL_NOTARGET;
const auto entNum = ent->s.number; const auto entNum = ent->s.number;
Logger::Print("Notarget toggled for entity %u\n", entNum); Logger::Print("Notarget toggled for entity %i\n", entNum);
Game::SV_GameSendServerCommand(entNum, 0, Utils::String::VA("%c \"%s\"", 0x65, Game::SV_GameSendServerCommand(entNum, 0, Utils::String::VA("%c \"%s\"", 0x65,
(ent->flags & Game::FL_NOTARGET) ? "GAME_NOTARGETON" : "GAME_NOTARGETOFF")); (ent->flags & Game::FL_NOTARGET) ? "GAME_NOTARGETON" : "GAME_NOTARGETOFF"));
}); });
ClientCommand::Add("setviewpos", [](Game::gentity_s* ent) ClientCommand::Add("setviewpos", [](Game::gentity_s* ent, [[maybe_unused]] Command::ServerParams* params)
{ {
assert(ent != nullptr); assert(ent != nullptr);
if (!ClientCommand::CheatsOk(ent)) if (!ClientCommand::CheatsOk(ent))
return; return;
Command::ServerParams params = {};
Game::vec3_t origin, angles{0.f, 0.f, 0.f}; Game::vec3_t origin, angles{0.f, 0.f, 0.f};
if (params.size() < 4 || params.size() > 6) if (params->size() < 4 || params->size() > 6)
{ {
Game::SV_GameSendServerCommand(ent->s.number, 0, Game::SV_GameSendServerCommand(ent->s.number, 0,
Utils::String::VA("%c \"GAME_USAGE\x15: setviewpos x y z [yaw] [pitch]\n\"", 0x65)); Utils::String::VA("%c \"GAME_USAGE\x15: setviewpos x y z [yaw] [pitch]\n\"", 0x65));
@ -156,149 +144,220 @@ namespace Components
for (auto i = 0; i < 3; i++) for (auto i = 0; i < 3; i++)
{ {
origin[i] = std::strtof(params.get(i + 1), nullptr); origin[i] = std::strtof(params->get(i + 1), nullptr);
} }
if (params.size() >= 5) if (params->size() >= 5)
{ {
angles[1] = std::strtof(params.get(4), nullptr); // Yaw angles[1] = std::strtof(params->get(4), nullptr); // Yaw
} }
if (params.size() == 6) if (params->size() == 6)
{ {
angles[0] = std::strtof(params.get(5), nullptr); // Pitch angles[0] = std::strtof(params->get(5), nullptr); // Pitch
} }
Game::TeleportPlayer(ent, origin, angles); Game::TeleportPlayer(ent, origin, angles);
}); });
} }
void ClientCommand::AddDevelopmentCommands()
{
ClientCommand::Add("dropallbots", []([[maybe_unused]] Game::gentity_s* ent, [[maybe_unused]] Command::ServerParams* params)
{
Game::SV_DropAllBots();
});
ClientCommand::Add("entitylist", []([[maybe_unused]] Game::gentity_s* ent, [[maybe_unused]] Command::ServerParams* params)
{
Game::Svcmd_EntityList_f();
});
ClientCommand::Add("printentities", []([[maybe_unused]] Game::gentity_s* ent, [[maybe_unused]] Command::ServerParams* params)
{
Game::G_PrintEntities();
});
ClientCommand::Add("entitycount", []([[maybe_unused]] Game::gentity_s* ent, [[maybe_unused]] Command::ServerParams* params)
{
Logger::Print("Entity count = %i\n", Game::level->num_entities);
});
// Also known as: "vis"
ClientCommand::Add("visionsetnaked", []([[maybe_unused]] Game::gentity_s* ent, [[maybe_unused]] Command::ServerParams* params)
{
if (params->size() < 2)
{
Logger::Print("USAGE: visionSetNaked <name> <duration>\n");
return;
}
auto duration = 1000;
if (params->size() > 2)
{
const auto input = std::strtof(params->get(2), nullptr);
duration = static_cast<int>(std::floorf(input * 1000.0f + 0.5f));
}
assert(ent->client != nullptr);
constexpr auto visMode = Game::visionSetMode_t::VISIONSET_NORMAL;
const auto* name = params->get(1);
ent->client->visionDuration[visMode] = duration;
strncpy_s(ent->client->visionName[visMode],
sizeof(Game::gclient_t::visionName[0]) / sizeof(char), name, _TRUNCATE);
Game::SV_GameSendServerCommand(ent->s.number, 1,
Utils::String::VA("%c \"%s\" %i", Game::MY_CMDS[visMode], name, duration));
});
ClientCommand::Add("visionsetnight", []([[maybe_unused]] Game::gentity_s* ent, [[maybe_unused]] Command::ServerParams* params)
{
if (params->size() < 2)
{
Logger::Print("USAGE: visionSetNight <name> <duration>\n");
return;
}
auto duration = 1000;
if (params->size() > 2)
{
const auto input = std::strtof(params->get(2), nullptr);
duration = static_cast<int>(std::floorf(input * 1000.0f + 0.5f));
}
assert(ent->client != nullptr);
constexpr auto visMode = Game::visionSetMode_t::VISIONSET_NIGHT;
const auto* name = params->get(1);
ent->client->visionDuration[visMode] = duration;
strncpy_s(ent->client->visionName[visMode],
sizeof(Game::gclient_t::visionName[0]) / sizeof(char), name, _TRUNCATE);
Game::SV_GameSendServerCommand(ent->s.number, 1,
Utils::String::VA("%c \"%s\" %i", Game::MY_CMDS[visMode], name, duration));
});
ClientCommand::Add("g_testCmd", []([[maybe_unused]] Game::gentity_s* ent, [[maybe_unused]] Command::ServerParams* params)
{
assert(ent != nullptr);
ent->client->ps.stunTime = 1000 + Game::level->time; // 1000 is the default test stun time
});
}
void ClientCommand::AddScriptFunctions() void ClientCommand::AddScriptFunctions()
{ {
Script::AddFunction("Noclip", [](Game::scr_entref_t entref) // gsc: Noclip(<optional int toggle>); Script::AddMethod("Noclip", [](Game::scr_entref_t entref) // gsc: Noclip(<optional int toggle>);
{ {
if (entref >= Game::MAX_GENTITIES || Game::g_entities[entref].client == nullptr) const auto* ent = Game::GetPlayerEntity(entref);
{
Game::Scr_Error(Utils::String::VA("^1NoClip: entity %u is not a client\n", entref));
return;
}
if (Game::Scr_GetNumParam() == 1u && Game::Scr_GetType(0) == Game::VAR_INTEGER) if (Game::Scr_GetNumParam() >= 1u)
{ {
if (Game::Scr_GetInt(0)) if (Game::Scr_GetInt(0))
{ {
Game::g_entities[entref].client->flags |= Game::PLAYER_FLAG_NOCLIP; ent->client->flags |= Game::PLAYER_FLAG_NOCLIP;
} }
else else
{ {
Game::g_entities[entref].client->flags &= ~Game::PLAYER_FLAG_NOCLIP; ent->client->flags &= ~Game::PLAYER_FLAG_NOCLIP;
} }
} }
else else
{ {
Game::g_entities[entref].client->flags ^= Game::PLAYER_FLAG_NOCLIP; ent->client->flags ^= Game::PLAYER_FLAG_NOCLIP;
} }
}); });
Script::AddFunction("Ufo", [](Game::scr_entref_t entref) // gsc: Ufo(<optional int toggle>); Script::AddMethod("Ufo", [](Game::scr_entref_t entref) // gsc: Ufo(<optional int toggle>);
{ {
if (entref >= Game::MAX_GENTITIES || Game::g_entities[entref].client == nullptr) const auto* ent = Game::GetPlayerEntity(entref);
{
Game::Scr_Error(Utils::String::VA("^1Ufo: entity %u is not a client\n", entref));
return;
}
if (Game::Scr_GetNumParam() == 1u && Game::Scr_GetType(0) == Game::VAR_INTEGER) if (Game::Scr_GetNumParam() >= 1u)
{ {
if (Game::Scr_GetInt(0)) if (Game::Scr_GetInt(0))
{ {
Game::g_entities[entref].client->flags |= Game::PLAYER_FLAG_UFO; ent->client->flags |= Game::PLAYER_FLAG_UFO;
} }
else else
{ {
Game::g_entities[entref].client->flags &= ~Game::PLAYER_FLAG_UFO; ent->client->flags &= ~Game::PLAYER_FLAG_UFO;
} }
} }
else else
{ {
Game::g_entities[entref].client->flags ^= Game::PLAYER_FLAG_UFO; ent->client->flags ^= Game::PLAYER_FLAG_UFO;
} }
}); });
Script::AddFunction("God", [](Game::scr_entref_t entref) // gsc: God(<optional int toggle>); Script::AddMethod("God", [](Game::scr_entref_t entref) // gsc: God(<optional int toggle>);
{ {
if (entref >= Game::MAX_GENTITIES) auto* ent = Game::GetEntity(entref);
{
Game::Scr_Error(Utils::String::VA("^1God: entity %u is out of bounds\n", entref));
return;
}
if (Game::Scr_GetNumParam() == 1u && Game::Scr_GetType(0) == Game::VAR_INTEGER) if (Game::Scr_GetNumParam() >= 1u)
{ {
if (Game::Scr_GetInt(0)) if (Game::Scr_GetInt(0))
{ {
Game::g_entities[entref].flags |= Game::FL_GODMODE; ent->flags |= Game::FL_GODMODE;
} }
else else
{ {
Game::g_entities[entref].flags &= ~Game::FL_GODMODE; ent->flags &= ~Game::FL_GODMODE;
} }
} }
else else
{ {
Game::g_entities[entref].flags ^= Game::FL_GODMODE; ent->flags ^= Game::FL_GODMODE;
} }
}); });
Script::AddFunction("Demigod", [](Game::scr_entref_t entref) // gsc: Demigod(<optional int toggle>); Script::AddMethod("Demigod", [](Game::scr_entref_t entref) // gsc: Demigod(<optional int toggle>);
{ {
if (entref >= Game::MAX_GENTITIES) auto* ent = Game::GetEntity(entref);
{
Game::Scr_Error(Utils::String::VA("^1Demigod: entity %u is out of bounds\n", entref));
return;
}
if (Game::Scr_GetNumParam() == 1u && Game::Scr_GetType(0) == Game::VAR_INTEGER) if (Game::Scr_GetNumParam() >= 1u)
{ {
if (Game::Scr_GetInt(0)) if (Game::Scr_GetInt(0))
{ {
Game::g_entities[entref].flags |= Game::FL_DEMI_GODMODE; ent->flags |= Game::FL_DEMI_GODMODE;
} }
else else
{ {
Game::g_entities[entref].flags &= ~Game::FL_DEMI_GODMODE; ent->flags &= ~Game::FL_DEMI_GODMODE;
} }
} }
else else
{ {
Game::g_entities[entref].flags ^= Game::FL_DEMI_GODMODE; ent->flags ^= Game::FL_DEMI_GODMODE;
} }
}); });
Script::AddFunction("Notarget", [](Game::scr_entref_t entref) // gsc: Notarget(<optional int toggle>); Script::AddMethod("Notarget", [](Game::scr_entref_t entref) // gsc: Notarget(<optional int toggle>);
{ {
if (entref >= Game::MAX_GENTITIES) auto* ent = Game::GetEntity(entref);
{
Game::Scr_Error(Utils::String::VA("^1Notarget: entity %u is out of bounds\n", entref));
return;
}
if (Game::Scr_GetNumParam() == 1u && Game::Scr_GetType(0) == Game::VAR_INTEGER) if (Game::Scr_GetNumParam() >= 1u)
{ {
if (Game::Scr_GetInt(0)) if (Game::Scr_GetInt(0))
{ {
Game::g_entities[entref].flags |= Game::FL_NOTARGET; ent->flags |= Game::FL_NOTARGET;
} }
else else
{ {
Game::g_entities[entref].flags &= ~Game::FL_NOTARGET; ent->flags &= ~Game::FL_NOTARGET;
} }
} }
else else
{ {
Game::g_entities[entref].flags ^= Game::FL_NOTARGET; ent->flags ^= Game::FL_NOTARGET;
} }
}); });
Script::AddFunction("DropAllBots", []() // gsc: DropAllBots();
{
Game::SV_DropAllBots();
});
} }
ClientCommand::ClientCommand() ClientCommand::ClientCommand()
@ -308,5 +367,8 @@ namespace Components
ClientCommand::AddCheatCommands(); ClientCommand::AddCheatCommands();
ClientCommand::AddScriptFunctions(); ClientCommand::AddScriptFunctions();
#ifdef _DEBUG
ClientCommand::AddDevelopmentCommands();
#endif
} }
} }

View File

@ -5,19 +5,17 @@ namespace Components
class ClientCommand : public Component class ClientCommand : public Component
{ {
public: public:
typedef void(Callback)(Game::gentity_s* entity);
ClientCommand(); ClientCommand();
static void Add(const char* name, Utils::Slot<Callback> callback); static void Add(const char* name, std::function<void(Game::gentity_s*, Command::ServerParams*)> callback);
static bool CheatsOk(const Game::gentity_s* ent); static bool CheatsOk(const Game::gentity_s* ent);
private: private:
static std::unordered_map<std::string, Utils::Slot<Callback>> FunctionMap; static std::unordered_map<std::string, std::function<void(Game::gentity_s*, Command::ServerParams*)>> HandlersSV;
static bool CallbackHandler(Game::gentity_s* ent, const char* cmd);
static void ClientCommandStub(const int clientNum); static void ClientCommandStub(const int clientNum);
static void AddCheatCommands(); static void AddCheatCommands();
static void AddDevelopmentCommands();
static void AddScriptFunctions(); static void AddScriptFunctions();
}; };
} }

View File

@ -2,8 +2,8 @@
namespace Components namespace Components
{ {
std::unordered_map<std::string, Utils::Slot<Command::Callback>> Command::FunctionMap; std::unordered_map<std::string, std::function<void(Command::Params*)>> Command::FunctionMap;
std::unordered_map<std::string, Utils::Slot<Command::Callback>> Command::FunctionMapSV; std::unordered_map<std::string, std::function<void(Command::Params*)>> Command::FunctionMapSV;
std::string Command::Params::join(const int index) std::string Command::Params::join(const int index)
{ {
@ -60,7 +60,7 @@ namespace Components
return Game::sv_cmd_args->argv[this->nesting_][index]; return Game::sv_cmd_args->argv[this->nesting_][index];
} }
void Command::Add(const char* name, Utils::Slot<Command::Callback> callback) void Command::Add(const char* name, std::function<void(Command::Params*)> callback)
{ {
const auto command = Utils::String::ToLower(name); const auto command = Utils::String::ToLower(name);
@ -69,10 +69,10 @@ namespace Components
Command::AddRaw(name, Command::MainCallback); Command::AddRaw(name, Command::MainCallback);
} }
Command::FunctionMap[command] = std::move(callback); Command::FunctionMap.insert_or_assign(command, std::move(callback));
} }
void Command::AddSV(const char* name, Utils::Slot<Command::Callback> callback) void Command::AddSV(const char* name, std::function<void(Command::Params*)> callback)
{ {
if (Loader::IsPregame()) if (Loader::IsPregame())
{ {
@ -95,7 +95,7 @@ namespace Components
Command::AddRaw(name, Game::Cbuf_AddServerText); Command::AddRaw(name, Game::Cbuf_AddServerText);
} }
FunctionMapSV[command] = std::move(callback); FunctionMapSV.insert_or_assign(command, std::move(callback));
} }
void Command::AddRaw(const char* name, void(*callback)(), bool key) void Command::AddRaw(const char* name, void(*callback)(), bool key)
@ -127,11 +127,11 @@ namespace Components
Game::cmd_function_t* Command::Find(const std::string& command) Game::cmd_function_t* Command::Find(const std::string& command)
{ {
Game::cmd_function_t* cmdFunction = *Game::cmd_functions; auto* cmdFunction = *Game::cmd_functions;
while (cmdFunction) while (cmdFunction != nullptr)
{ {
if (cmdFunction->name && cmdFunction->name == command) if (cmdFunction->name != nullptr && cmdFunction->name == command)
{ {
return cmdFunction; return cmdFunction;
} }
@ -149,12 +149,10 @@ namespace Components
void Command::MainCallback() void Command::MainCallback()
{ {
Command::ClientParams params; ClientParams params;
const auto command = Utils::String::ToLower(params[0]); const auto command = Utils::String::ToLower(params[0]);
const auto got = Command::FunctionMap.find(command);
if (got != Command::FunctionMap.end()) if (const auto got = FunctionMap.find(command); got != FunctionMap.end())
{ {
got->second(&params); got->second(&params);
} }
@ -162,12 +160,10 @@ namespace Components
void Command::MainCallbackSV() void Command::MainCallbackSV()
{ {
Command::ServerParams params; ServerParams params;
const auto command = Utils::String::ToLower(params[0]); const auto command = Utils::String::ToLower(params[0]);
const auto got = Command::FunctionMapSV.find(command);
if (got != Command::FunctionMapSV.end()) if (const auto got = FunctionMapSV.find(command); got != FunctionMapSV.end())
{ {
got->second(&params); got->second(&params);
} }

View File

@ -8,7 +8,8 @@ namespace Components
class Params class Params
{ {
public: public:
Params() {}; Params() = default;
virtual ~Params() = default;
virtual int size() = 0; virtual int size() = 0;
virtual const char* get(int index) = 0; virtual const char* get(int index) = 0;
@ -20,7 +21,7 @@ namespace Components
} }
}; };
class ClientParams : public Params class ClientParams final : public Params
{ {
public: public:
ClientParams(); ClientParams();
@ -32,7 +33,7 @@ namespace Components
int nesting_; int nesting_;
}; };
class ServerParams : public Params class ServerParams final : public Params
{ {
public: public:
ServerParams(); ServerParams();
@ -44,14 +45,12 @@ namespace Components
int nesting_; int nesting_;
}; };
typedef void(Callback)(Command::Params* params);
Command(); Command();
static Game::cmd_function_t* Allocate(); static Game::cmd_function_t* Allocate();
static void Add(const char* name, Utils::Slot<Callback> callback); static void Add(const char* name, std::function<void(Command::Params*)> callback);
static void AddSV(const char* name, Utils::Slot<Callback> callback); static void AddSV(const char* name, std::function<void(Command::Params*)> callback);
static void AddRaw(const char* name, void(*callback)(), bool key = false); static void AddRaw(const char* name, void(*callback)(), bool key = false);
static void AddRawSV(const char* name, void(*callback)()); static void AddRawSV(const char* name, void(*callback)());
static void Execute(std::string command, bool sync = true); static void Execute(std::string command, bool sync = true);
@ -59,8 +58,8 @@ namespace Components
static Game::cmd_function_t* Find(const std::string& command); static Game::cmd_function_t* Find(const std::string& command);
private: private:
static std::unordered_map<std::string, Utils::Slot<Callback>> FunctionMap; static std::unordered_map<std::string, std::function<void(Command::Params*)>> FunctionMap;
static std::unordered_map<std::string, Utils::Slot<Callback>> FunctionMapSV; static std::unordered_map<std::string, std::function<void(Command::Params*)>> FunctionMapSV;
static void MainCallback(); static void MainCallback();
static void MainCallbackSV(); static void MainCallbackSV();

View File

@ -51,8 +51,8 @@ namespace Components
{ {
SetConsoleTitleA(hostname.data()); SetConsoleTitleA(hostname.data());
int clientCount = 0; auto clientCount = 0;
int maxclientCount = *Game::svs_numclients; auto maxclientCount = *Game::svs_clientCount;
if (maxclientCount) if (maxclientCount)
{ {

View File

@ -374,7 +374,6 @@ namespace Components
} }
}); });
#ifdef USE_LEGACY_SERVER_LIST
// Heartbeats // Heartbeats
Scheduler::Once(Dedicated::Heartbeat); Scheduler::Once(Dedicated::Heartbeat);
Scheduler::OnFrame([]() Scheduler::OnFrame([]()
@ -387,7 +386,6 @@ namespace Components
Dedicated::Heartbeat(); Dedicated::Heartbeat();
} }
}); });
#endif
Dvar::OnInit([]() Dvar::OnInit([]()
{ {

View File

@ -83,16 +83,6 @@ namespace Components
ServerList::InsertRequest(address); ServerList::InsertRequest(address);
} }
}); });
// This is placed here in case the anticheat has been disabled!
// Make sure this is called after the memory scan!
#ifndef DISABLE_ANTICHEAT
Utils::Hook(0x5ACB9E, []() // Somewhere in the renderer, past the scan check
{
AntiCheat::ScanIntegrityCheck();
return Utils::Hook::Call<void()>(0x4AA720)();
}, HOOK_CALL).install()->quick();
#endif
} }
void Discovery::preDestroy() void Discovery::preDestroy()

View File

@ -381,7 +381,7 @@ namespace Components
{ {
Network::Address address(nc->sa.sa); Network::Address address(nc->sa.sa);
for (int i = 0; i < *Game::svs_numclients; ++i) for (int i = 0; i < *Game::svs_clientCount; ++i)
{ {
Game::client_t* client = &Game::svs_clients[i]; Game::client_t* client = &Game::svs_clients[i];
@ -964,13 +964,20 @@ namespace Components
Download::ScriptDownloads.clear(); Download::ScriptDownloads.clear();
}); });
Script::AddFunction("httpGet", [](Game::scr_entref_t) Script::AddFunction("HttpGet", []()
{ {
if (!Dedicated::IsEnabled() && !Flags::HasFlag("scriptablehttp")) return; if (!Flags::HasFlag("scriptablehttp"))
if (Game::Scr_GetNumParam() < 1u) return; return;
std::string url = Game::Scr_GetString(0); const auto* url = Game::Scr_GetString(0);
unsigned int object = Game::AllocObject();
if (url == nullptr)
{
Game::Scr_ParamError(0, "^1HttpGet: Illegal parameter!\n");
return;
}
auto object = Game::AllocObject();
Game::Scr_AddObject(object); Game::Scr_AddObject(object);
@ -978,13 +985,13 @@ namespace Components
Game::RemoveRefToObject(object); Game::RemoveRefToObject(object);
}); });
Script::AddFunction("httpCancel", [](Game::scr_entref_t) Script::AddFunction("HttpCancel", []()
{ {
if (!Dedicated::IsEnabled() && !Flags::HasFlag("scriptablehttp")) return; if (!Flags::HasFlag("scriptablehttp"))
if (Game::Scr_GetNumParam() < 1u) return; return;
unsigned int object = Game::Scr_GetObject(0); const auto object = Game::Scr_GetObject(0);
for (auto& download : Download::ScriptDownloads) for (const auto& download : Download::ScriptDownloads)
{ {
if (object == download->getObject()) if (object == download->getObject())
{ {

View File

@ -137,10 +137,6 @@ namespace Components
Utils::IO::CreateDir("minidumps"); Utils::IO::CreateDir("minidumps");
PathCombineA(filename, "minidumps\\", Utils::String::VA("%s-" VERSION "-%s.dmp", exeFileName, filenameFriendlyTime)); PathCombineA(filename, "minidumps\\", Utils::String::VA("%s-" VERSION "-%s.dmp", exeFileName, filenameFriendlyTime));
#ifndef DISABLE_ANTICHEAT
AntiCheat::UninstallLibHook();
#endif
DWORD fileShare = FILE_SHARE_READ | FILE_SHARE_WRITE; DWORD fileShare = FILE_SHARE_READ | FILE_SHARE_WRITE;
HANDLE hFile = CreateFileA(filename, GENERIC_WRITE | GENERIC_READ, fileShare, nullptr, (fileShare & FILE_SHARE_WRITE) > 0 ? OPEN_ALWAYS : OPEN_EXISTING, NULL, nullptr); HANDLE hFile = CreateFileA(filename, GENERIC_WRITE | GENERIC_READ, fileShare, nullptr, (fileShare & FILE_SHARE_WRITE) > 0 ? OPEN_ALWAYS : OPEN_EXISTING, NULL, nullptr);
MINIDUMP_EXCEPTION_INFORMATION ex = { GetCurrentThreadId(), ExceptionInfo, FALSE }; MINIDUMP_EXCEPTION_INFORMATION ex = { GetCurrentThreadId(), ExceptionInfo, FALSE };
@ -157,10 +153,6 @@ namespace Components
TerminateProcess(GetCurrentProcess(), ExceptionInfo->ExceptionRecord->ExceptionCode); TerminateProcess(GetCurrentProcess(), ExceptionInfo->ExceptionRecord->ExceptionCode);
} }
#ifndef DISABLE_ANTICHEAT
AntiCheat::InstallLibHook();
#endif
return EXCEPTION_CONTINUE_SEARCH; return EXCEPTION_CONTINUE_SEARCH;
} }

View File

@ -28,7 +28,7 @@ namespace Components
if (!rawfile || Game::DB_IsXAssetDefault(Game::XAssetType::ASSET_TYPE_RAWFILE, this->filePath.data())) return; if (!rawfile || Game::DB_IsXAssetDefault(Game::XAssetType::ASSET_TYPE_RAWFILE, this->filePath.data())) return;
this->buffer.resize(Game::DB_GetRawFileLen(rawfile)); this->buffer.resize(Game::DB_GetRawFileLen(rawfile));
Game::DB_GetRawBuffer(rawfile, const_cast<char*>(this->buffer.data()), this->buffer.size()); Game::DB_GetRawBuffer(rawfile, this->buffer.data(), static_cast<int>(this->buffer.size()));
} }
FileSystem::FileReader::FileReader(const std::string& file) : handle(0), name(file) FileSystem::FileReader::FileReader(const std::string& file) : handle(0), name(file)

View File

@ -11,6 +11,10 @@ namespace Components
std::recursive_mutex Friends::Mutex; std::recursive_mutex Friends::Mutex;
std::vector<Friends::Friend> Friends::FriendsList; std::vector<Friends::Friend> Friends::FriendsList;
Dvar::Var Friends::UIStreamFriendly;
Dvar::Var Friends::CLAnonymous;
Dvar::Var Friends::CLNotifyFriendState;
void Friends::SortIndividualList(std::vector<Friends::Friend>* list) void Friends::SortIndividualList(std::vector<Friends::Friend>* list)
{ {
std::stable_sort(list->begin(), list->end(), [](Friends::Friend const& friend1, Friends::Friend const& friend2) std::stable_sort(list->begin(), list->end(), [](Friends::Friend const& friend1, Friends::Friend const& friend2)
@ -111,8 +115,8 @@ namespace Components
Friends::SortList(); Friends::SortList();
const auto notify = Dvar::Var("cl_notifyFriendState").get<bool>(); const auto notify = Friends::CLNotifyFriendState.get<bool>();
if (gotOnline && (!notify || (notify && !Game::CL_IsCgameInitialized())) && !Dvar::Var("ui_streamFriendly").get<bool>()) if (gotOnline && (!notify || (notify && !Game::CL_IsCgameInitialized())) && !Friends::UIStreamFriendly.get<bool>())
{ {
Game::Material* material = Friends::CreateAvatar(user); Game::Material* material = Friends::CreateAvatar(user);
Toast::Show(material, entry->name, "is playing IW4x", 3000, [material]() Toast::Show(material, entry->name, "is playing IW4x", 3000, [material]()
@ -124,7 +128,7 @@ namespace Components
void Friends::UpdateState(bool force) void Friends::UpdateState(bool force)
{ {
if (Dvar::Var("cl_anonymous").get<bool>() || Friends::IsInvisible() || !Steam::Enabled()) return; if (Friends::CLAnonymous.get<bool>() || Friends::IsInvisible() || !Steam::Enabled()) return;
if (force) if (force)
{ {
@ -228,7 +232,7 @@ namespace Components
void Friends::SetPresence(const std::string& key, const std::string& value) void Friends::SetPresence(const std::string& key, const std::string& value)
{ {
if (Steam::Proxy::ClientFriends && Steam::Proxy::SteamUtils && !Dvar::Var("cl_anonymous").get<bool>() && !Friends::IsInvisible() && Steam::Enabled()) if (Steam::Proxy::ClientFriends && Steam::Proxy::SteamUtils && !Friends::CLAnonymous.get<bool>() && !Friends::IsInvisible() && Steam::Enabled())
{ {
Friends::SetRawPresence(key.data(), value.data()); Friends::SetRawPresence(key.data(), value.data());
} }
@ -576,10 +580,15 @@ namespace Components
{ {
Friends::LoggedOn = false; Friends::LoggedOn = false;
if (Dedicated::IsEnabled() || ZoneBuilder::IsEnabled() || Monitor::IsEnabled()) return; if (Dedicated::IsEnabled() || ZoneBuilder::IsEnabled() || Monitor::IsEnabled())
return;
Dvar::Register<bool>("cl_anonymous", false, Game::DVAR_ARCHIVE, "Enable invisible mode for Steam"); Dvar::OnInit([]
Dvar::Register<bool>("cl_notifyFriendState", true, Game::DVAR_ARCHIVE, "Update friends about current game status"); {
Friends::UIStreamFriendly = Dvar::Register<bool>("ui_streamFriendly", false, Game::DVAR_ARCHIVE, "Stream friendly UI");
Friends::CLAnonymous = Dvar::Register<bool>("cl_anonymous", false, Game::DVAR_ARCHIVE, "Enable invisible mode for Steam");
Friends::CLNotifyFriendState = Dvar::Register<bool>("cl_notifyFriendState", true, Game::DVAR_ARCHIVE, "Update friends about current game status");
});
Command::Add("addFriend", [](Command::Params* params) Command::Add("addFriend", [](Command::Params* params)
{ {
@ -712,11 +721,11 @@ namespace Components
Friends::InitialState = Steam::Proxy::SteamFriends->GetFriendPersonaState(Steam::Proxy::SteamUser_->GetSteamID()); Friends::InitialState = Steam::Proxy::SteamFriends->GetFriendPersonaState(Steam::Proxy::SteamUser_->GetSteamID());
} }
if (Dvar::Var("cl_anonymous").get<bool>() || Friends::IsInvisible() || !Steam::Enabled()) if (Friends::CLAnonymous.get<bool>() || Friends::IsInvisible() || !Steam::Enabled())
{ {
if (Steam::Proxy::ClientFriends) if (Steam::Proxy::ClientFriends)
{ {
for (auto id : Friends::GetAppIdList()) for (const auto id : Friends::GetAppIdList())
{ {
Steam::Proxy::ClientFriends.invoke<void>("ClearRichPresence", id); Steam::Proxy::ClientFriends.invoke<void>("ClearRichPresence", id);
} }

View File

@ -25,6 +25,10 @@ namespace Components
static bool IsInvisible(); static bool IsInvisible();
static Dvar::Var UIStreamFriendly;
static Dvar::Var CLAnonymous;
static Dvar::Var CLNotifyFriendState;
private: private:
#pragma pack(push, 4) #pragma pack(push, 4)
struct FriendRichPresenceUpdate struct FriendRichPresenceUpdate

View File

@ -1531,11 +1531,9 @@ namespace Components
gpad_present.setRaw(gpadPresent); gpad_present.setRaw(gpadPresent);
} }
void Gamepad::IN_Frame_Hk() void Gamepad::IN_Frame_Hk()
{ {
// Call original method RawMouse::IN_MouseMove();
Utils::Hook::Call<void()>(0x64C490)();
IN_GamePadsMove(); IN_GamePadsMove();
} }
@ -1763,7 +1761,7 @@ namespace Components
void Gamepad::CG_RegisterDvars_Hk() void Gamepad::CG_RegisterDvars_Hk()
{ {
// Call original method // Call original function
Utils::Hook::Call<void()>(0x4F8DC0)(); Utils::Hook::Call<void()>(0x4F8DC0)();
InitDvars(); InitDvars();

View File

@ -98,15 +98,5 @@ namespace Components
// Dynamically grab gametypes // Dynamically grab gametypes
Utils::Hook(0x5FA46C, Gametypes::BuildGametypeList, HOOK_CALL).install()->quick(); // Scr_UpdateGameTypeList Utils::Hook(0x5FA46C, Gametypes::BuildGametypeList, HOOK_CALL).install()->quick(); // Scr_UpdateGameTypeList
Utils::Hook(0x632155, Gametypes::BuildGametypeList, HOOK_CALL).install()->quick(); // UI_UpdateGameTypesList Utils::Hook(0x632155, Gametypes::BuildGametypeList, HOOK_CALL).install()->quick(); // UI_UpdateGameTypesList
// This is placed here in case the anticheat has been disabled!
// Make sure this is called after every onther anticheat check!
#ifndef DISABLE_ANTICHEAT
Utils::Hook(0x5ACBA3, []() // Somewhere in the renderer, past other renderer hooks!
{
AntiCheat::FlagIntegrityCheck();
return Utils::Hook::Call<void()>(0x50AB20)();
}, HOOK_CALL).install()->quick();
#endif
} }
} }

View File

@ -297,13 +297,6 @@ namespace Components
} }
} }
}); });
// #ifndef DISABLE_ANTICHEAT
// if (!Dedicated::IsEnabled() && !ZoneBuilder::IsEnabled() && !Utils::IsWineEnvironment() && !Loader::IsPerformingUnitTests())
// {
// AntiCheat::PatchVirtualProtect(VirtualProtect, VirtualProtectEx);
// }
// #endif
} }
Localization::~Localization() Localization::~Localization()

View File

@ -77,16 +77,14 @@ namespace Components
std::string Logger::Format(const char** message) std::string Logger::Format(const char** message)
{ {
const size_t bufferSize = 0x10000; char buffer[4096] = {0};
Utils::Memory::Allocator allocator;
char* buffer = allocator.allocateArray<char>(bufferSize);
va_list ap = reinterpret_cast<char*>(const_cast<char**>(&message[1])); va_list ap = reinterpret_cast<char*>(const_cast<char**>(&message[1]));
//va_start(ap, *message);
_vsnprintf_s(buffer, bufferSize, bufferSize, *message, ap); _vsnprintf_s(buffer, _TRUNCATE, *message, ap);
va_end(ap); va_end(ap);
return buffer; return {buffer};
} }
void Logger::Flush() void Logger::Flush()
@ -138,8 +136,8 @@ namespace Components
{ {
if (!data) return; if (!data) return;
std::string buffer(data); const std::string buffer(data);
for (auto& addr : Logger::LoggingAddresses[gLog & 1]) for (const auto& addr : Logger::LoggingAddresses[gLog & 1])
{ {
Network::SendCommand(addr, "print", buffer); Network::SendCommand(addr, "print", buffer);
} }
@ -373,9 +371,9 @@ namespace Components
Logger::MessageMutex.unlock(); Logger::MessageMutex.unlock();
// Flush the console log // Flush the console log
if (int fh = *reinterpret_cast<int*>(0x1AD8F28)) if (const auto logfile = *reinterpret_cast<int*>(0x1AD8F28))
{ {
Game::FS_FCloseFile(fh); Game::FS_FCloseFile(logfile);
} }
} }
} }

View File

@ -788,7 +788,7 @@ namespace Components
{ {
int dlc = token.get<int>(); int dlc = token.get<int>();
for (auto pack : Maps::DlcPacks) for (const auto& pack : Maps::DlcPacks)
{ {
if (pack.index == dlc) if (pack.index == dlc)
{ {

View File

@ -70,7 +70,7 @@ namespace Components
script->next = nullptr; script->next = nullptr;
Game::source_t* source = allocator->allocate<Game::source_t>(); auto* source = allocator->allocate<Game::source_t>();
if (!source) if (!source)
{ {
Game::FreeMemory(script); Game::FreeMemory(script);
@ -116,7 +116,7 @@ namespace Components
{ {
Utils::Memory::Allocator* allocator = Utils::Memory::GetAllocator(); Utils::Memory::Allocator* allocator = Utils::Memory::GetAllocator();
Game::menuDef_t* menu = allocator->allocate<Game::menuDef_t>(); auto* menu = allocator->allocate<Game::menuDef_t>();
if (!menu) return nullptr; if (!menu) return nullptr;
menu->items = allocator->allocateArray<Game::itemDef_s*>(512); menu->items = allocator->allocateArray<Game::itemDef_s*>(512);
@ -284,17 +284,7 @@ namespace Components
if (menus.empty()) if (menus.empty())
{ {
// // Try loading the original menu, if we can't load our custom one
// Game::menuDef_t* originalMenu = AssetHandler::FindOriginalAsset(Game::XAssetType::ASSET_TYPE_MENU, menudef->window.name).menu;
//
// if (originalMenu)
// {
// menus.push_back({ false, originalMenu });
// }
// else
// {
menus.push_back({ false, menudef }); // Native menu menus.push_back({ false, menudef }); // Native menu
// }
} }
return menus; return menus;
@ -308,7 +298,7 @@ namespace Components
if (menus.empty()) return nullptr; if (menus.empty()) return nullptr;
// Allocate new menu list // Allocate new menu list
Game::MenuList* newList = allocator->allocate<Game::MenuList>(); auto* newList = allocator->allocate<Game::MenuList>();
if (!newList) return nullptr; if (!newList) return nullptr;
newList->menus = allocator->allocateArray<Game::menuDef_t*>(menus.size()); newList->menus = allocator->allocateArray<Game::menuDef_t*>(menus.size());
@ -319,7 +309,7 @@ namespace Components
} }
newList->name = allocator->duplicateString(menu); newList->name = allocator->duplicateString(menu);
newList->menuCount = menus.size(); newList->menuCount = static_cast<int>(menus.size());
// Copy new menus // Copy new menus
for (unsigned int i = 0; i < menus.size(); ++i) for (unsigned int i = 0; i < menus.size(); ++i)
@ -759,50 +749,14 @@ namespace Components
void Menus::RegisterCustomMenusHook() void Menus::RegisterCustomMenusHook()
{ {
Game::UiContext* uiInfoArray = (Game::UiContext*)0x62E2858;
// Game::MenuList list;
Utils::Hook::Call<void()>(0x401700)(); // call original load functions Utils::Hook::Call<void()>(0x401700)(); // call original load functions
//Game::XAssetHeader header = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_MENULIST, "ui_mp/iw4x.txt");
//if (header.data && !(header.menuList->menuCount == 1 && !_stricmp("default_menu", header.menuList->menus[0]->window.name)))
//{
// // Utils::Hook::Call<void(void*, Game::MenuList*, int)>(0x401700)(uiInfoArray, header.menuList, 1); // add loaded menus
// std::memcpy(&list, header.data, sizeof(Game::MenuList));
// for (int i = 0; i < uiInfoArray->menuCount; i++) #ifdef _DEBUG
// { for (int i = 0; i < Game::uiContext->menuCount; i++)
// for (int j = 0; j < list.menuCount; j++)
// {
// if (!list.menus[j]) continue; // skip already used entries
// if (!stricmp(list.menus[j]->window.name, uiInfoArray->Menus[i]->window.name))
// {
// uiInfoArray->Menus[i] = list.menus[j]; // overwrite UiContext pointer
// list.menus[j] = nullptr; // clear entries that already exist so we don't add them later
// }
// }
// }
// for (int i = 0; i < list.menuCount; i++)
// {
// if (list.menus[i])
// {
// uiInfoArray->Menus[uiInfoArray->menuCount++] = list.menus[i];
// }
// }
//}
for (int i = 0; i < uiInfoArray->menuCount; i++)
{ {
OutputDebugStringA(Utils::String::VA("%s\n", uiInfoArray->Menus[i]->window.name)); OutputDebugStringA(Utils::String::VA("%s\n", Game::uiContext->Menus[i]->window.name));
} }
#endif
/*
header = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_MENULIST, "ui_mp/mod.txt");
if (header.data && !(header.menuList->menuCount == 1 && !_stricmp("default_menu", header.menuList->menus[0]->window.name)))
{
Utils::Hook::Call<void(void*, Game::MenuList*, int)>(0x401700)(uiInfoArray, header.menuList, 1); // add loaded menus
}
*/
} }
Menus::Menus() Menus::Menus()
@ -883,10 +837,6 @@ namespace Components
} }
}); });
#ifndef DISABLE_ANTICHEAT
Scheduler::OnFrameAsync(AntiCheat::QuickCodeScanner2);
#endif
Command::Add("mp_QuickMessage", [](Command::Params*) Command::Add("mp_QuickMessage", [](Command::Params*)
{ {
Command::Execute("openmenu quickmessage"); Command::Execute("openmenu quickmessage");

View File

@ -2,9 +2,6 @@
namespace Components namespace Components
{ {
Dvar::Var Movement::PlayerDuckedSpeedScale;
Dvar::Var Movement::PlayerLastStandCrawlSpeedScale;
Dvar::Var Movement::PlayerProneSpeedScale;
Dvar::Var Movement::PlayerSpectateSpeedScale; Dvar::Var Movement::PlayerSpectateSpeedScale;
Dvar::Var Movement::CGUfoScaler; Dvar::Var Movement::CGUfoScaler;
Dvar::Var Movement::CGNoclipScaler; Dvar::Var Movement::CGNoclipScaler;
@ -13,99 +10,50 @@ namespace Components
Dvar::Var Movement::BGPlayerEjection; Dvar::Var Movement::BGPlayerEjection;
Dvar::Var Movement::BGPlayerCollision; Dvar::Var Movement::BGPlayerCollision;
Game::dvar_t* Movement::BGBounces; Game::dvar_t* Movement::BGBounces;
Game::dvar_t* Movement::PlayerDuckedSpeedScale;
Game::dvar_t* Movement::PlayerProneSpeedScale;
float Movement::PM_CmdScaleForStance(const Game::pmove_s* pm) __declspec(naked) void Movement::PM_PlayerDuckedSpeedScaleStub()
{
assert(pm->ps != nullptr);
const auto* playerState = pm->ps;
float scale;
if (playerState->viewHeightLerpTime != 0 && playerState->viewHeightLerpTarget == 0xB)
{
scale = pm->cmd.serverTime - playerState->viewHeightLerpTime / 400.0f;
if (0.0f <= scale)
{
if (scale > 1.0f)
{
scale = 1.0f;
return scale * 0.15f + (1.0f - scale) * 0.65f;
}
if (scale != 0.0f)
{
return scale * 0.15f + (1.0f - scale) * 0.65f;
}
}
}
if ((playerState->viewHeightLerpTime != 0 && playerState->viewHeightLerpTarget == 0x28) &&
playerState->viewHeightLerpDown == 0)
{
scale = 400.0f / pm->cmd.serverTime - playerState->viewHeightLerpTime;
if (0.0f <= scale)
{
if (scale > 1.0f)
{
scale = 1.0f;
}
else if (scale != 0.0f)
{
return scale * 0.65f + (1.0f - scale) * 0.15f;
}
}
}
scale = 1.0f;
const auto stance = Game::PM_GetEffectiveStance(playerState);
if (stance == Game::PM_EFF_STANCE_PRONE)
{
scale = Movement::PlayerProneSpeedScale.get<float>();
}
else if (stance == Game::PM_EFF_STANCE_DUCKED)
{
scale = Movement::PlayerDuckedSpeedScale.get<float>();
}
else if (stance == Game::PM_EFF_STANCE_LASTSTANDCRAWL)
{
scale = Movement::PlayerLastStandCrawlSpeedScale.get<float>();
}
return scale;
}
__declspec(naked) void Movement::PM_CmdScaleForStanceStub()
{ {
__asm __asm
{ {
pushad push eax
mov eax, Movement::PlayerDuckedSpeedScale
fld dword ptr [eax + 0x10] // dvar_t.current.value
pop eax
push edx // Game's code
call Movement::PM_CmdScaleForStance // pm pop ecx
add esp, 4
popad
ret ret
} }
} }
float Movement::PM_MoveScale(Game::playerState_s* ps, float forwardmove, __declspec(naked) void Movement::PM_PlayerProneSpeedScaleStub()
float rightmove, float upmove) {
__asm
{
push eax
mov eax, Movement::PlayerProneSpeedScale
fld dword ptr [eax + 0x10] // dvar_t.current.value
pop eax
// Game's code
pop ecx
ret
}
}
float Movement::PM_MoveScale(Game::playerState_s* ps, float fmove,
float rmove, float umove)
{ {
assert(ps != nullptr); assert(ps != nullptr);
auto max = (std::fabsf(forwardmove) < std::fabsf(rightmove)) auto max = std::fabsf(fmove) < std::fabsf(rmove)
? std::fabsf(rightmove) ? std::fabsf(rmove) : std::fabsf(fmove);
: std::fabsf(forwardmove);
if (std::fabsf(upmove) > max) if (std::fabsf(umove) > max)
{ {
max = std::fabsf(upmove); max = std::fabsf(umove);
} }
if (max == 0.0f) if (max == 0.0f)
@ -113,28 +61,28 @@ namespace Components
return 0.0f; return 0.0f;
} }
auto total = std::sqrtf(forwardmove * forwardmove auto total = std::sqrtf(fmove * fmove
+ rightmove * rightmove + upmove * upmove); + rmove * rmove + umove * umove);
auto scale = (ps->speed * max) / (127.0f * total); auto scale = (static_cast<float>(ps->speed) * max) / (127.0f * total);
if (ps->pm_flags & Game::PMF_WALKING || ps->leanf != 0.0f) if (ps->pm_flags & Game::PMF_WALKING || ps->leanf != 0.0f)
{ {
scale *= 0.4f; scale *= 0.4f;
} }
if (ps->pm_type == Game::PM_NOCLIP) switch (ps->pm_type)
{ {
return scale * Movement::CGNoclipScaler.get<float>(); case Game::pmtype_t::PM_NOCLIP:
} scale *= Movement::CGNoclipScaler.get<float>();
break;
if (ps->pm_type == Game::PM_UFO) case Game::pmtype_t::PM_UFO:
{ scale *= Movement::CGUfoScaler.get<float>();
return scale * Movement::CGUfoScaler.get<float>(); break;
} case Game::pmtype_t::PM_SPECTATOR:
scale *= Movement::PlayerSpectateSpeedScale.get<float>();
if (ps->pm_type == Game::PM_SPECTATOR) break;
{ default:
return scale * Movement::PlayerSpectateSpeedScale.get<float>(); break;
} }
return scale; return scale;
@ -146,9 +94,9 @@ namespace Components
{ {
pushad pushad
push [esp + 0xC + 0x20] // upmove push [esp + 0xC + 0x20] // umove
push [esp + 0xC + 0x20] // rightmove push [esp + 0xC + 0x20] // rmove
push [esp + 0xC + 0x20] // forwardmove push [esp + 0xC + 0x20] // fmove
push esi // ps push esi // ps
call Movement::PM_MoveScale call Movement::PM_MoveScale
add esp, 0x10 add esp, 0x10
@ -230,9 +178,9 @@ namespace Components
if (ent->client != nullptr && BGRocketJump.get<bool>()) if (ent->client != nullptr && BGRocketJump.get<bool>())
{ {
ent->client->ps.velocity[0] += (0 - wp->forward[0]) * 64.0f; ent->client->ps.velocity[0] += (0.0f - wp->forward[0]) * 64.0f;
ent->client->ps.velocity[1] += (0 - wp->forward[1]) * 64.0f; ent->client->ps.velocity[1] += (0.0f - wp->forward[1]) * 64.0f;
ent->client->ps.velocity[2] += (0 - wp->forward[2]) * 64.0f; ent->client->ps.velocity[2] += (0.0f - wp->forward[2]) * 64.0f;
} }
return result; return result;
@ -260,15 +208,6 @@ namespace Components
} }
} }
Game::dvar_t* Movement::Dvar_RegisterLastStandSpeedScale(const char* dvarName, float value,
float min, float max, unsigned __int16 /*flags*/, const char* description)
{
Movement::PlayerLastStandCrawlSpeedScale = Dvar::Register<float>(dvarName, value,
min, max, Game::DVAR_CHEAT | Game::DVAR_CODINFO, description);
return Movement::PlayerLastStandCrawlSpeedScale.get<Game::dvar_t*>();
}
Game::dvar_t* Movement::Dvar_RegisterSpectateSpeedScale(const char* dvarName, float value, Game::dvar_t* Movement::Dvar_RegisterSpectateSpeedScale(const char* dvarName, float value,
float min, float max, unsigned __int16 /*flags*/, const char* description) float min, float max, unsigned __int16 /*flags*/, const char* description)
{ {
@ -290,11 +229,11 @@ namespace Components
nullptr nullptr
}; };
Movement::PlayerDuckedSpeedScale = Dvar::Register<float>("player_duckedSpeedScale", Movement::PlayerDuckedSpeedScale = Game::Dvar_RegisterFloat("player_duckedSpeedScale",
0.65f, 0.0f, 5.0f, Game::DVAR_CHEAT | Game::DVAR_CODINFO, 0.65f, 0.0f, 5.0f, Game::DVAR_CHEAT | Game::DVAR_CODINFO,
"The scale applied to the player speed when ducking"); "The scale applied to the player speed when ducking");
Movement::PlayerProneSpeedScale = Dvar::Register<float>("player_proneSpeedScale", Movement::PlayerProneSpeedScale = Game::Dvar_RegisterFloat("player_proneSpeedScale",
0.15f, 0.0f, 5.0f, Game::DVAR_CHEAT | Game::DVAR_CODINFO, 0.15f, 0.0f, 5.0f, Game::DVAR_CHEAT | Game::DVAR_CODINFO,
"The scale applied to the player speed when crawling"); "The scale applied to the player speed when crawling");
@ -323,18 +262,13 @@ namespace Components
true, Game::DVAR_CODINFO, "Push intersecting players away from each other"); true, Game::DVAR_CODINFO, "Push intersecting players away from each other");
}); });
// Hook PM_CmdScaleForStance in PM_CmdScale_Walk
Utils::Hook(0x572F34, Movement::PM_CmdScaleForStanceStub, HOOK_CALL).install()->quick();
//Hook PM_CmdScaleForStance in PM_GetMaxSpeed
Utils::Hook(0x57395F, Movement::PM_CmdScaleForStanceStub, HOOK_CALL).install()->quick();
// Hook Dvar_RegisterFloat. Only thing that's changed is that the 0x80 flag is not used.
Utils::Hook(0x448B66, Movement::Dvar_RegisterLastStandSpeedScale, HOOK_CALL).install()->quick();
// Hook Dvar_RegisterFloat. Only thing that's changed is that the 0x80 flag is not used. // Hook Dvar_RegisterFloat. Only thing that's changed is that the 0x80 flag is not used.
Utils::Hook(0x448990, Movement::Dvar_RegisterSpectateSpeedScale, HOOK_CALL).install()->quick(); Utils::Hook(0x448990, Movement::Dvar_RegisterSpectateSpeedScale, HOOK_CALL).install()->quick();
// PM_CmdScaleForStance
Utils::Hook(0x572D9B, Movement::PM_PlayerDuckedSpeedScaleStub, HOOK_JUMP).install()->quick();
Utils::Hook(0x572DA5, Movement::PM_PlayerProneSpeedScaleStub, HOOK_JUMP).install()->quick();
// Hook PM_MoveScale so we can add custom speed scale for Ufo and Noclip // Hook PM_MoveScale so we can add custom speed scale for Ufo and Noclip
Utils::Hook(0x56F845, Movement::PM_MoveScaleStub, HOOK_CALL).install()->quick(); Utils::Hook(0x56F845, Movement::PM_MoveScaleStub, HOOK_CALL).install()->quick();
Utils::Hook(0x56FABD, Movement::PM_MoveScaleStub, HOOK_CALL).install()->quick(); Utils::Hook(0x56FABD, Movement::PM_MoveScaleStub, HOOK_CALL).install()->quick();

View File

@ -10,9 +10,6 @@ namespace Components
private: private:
enum BouncesSettings { DISABLED, ENABLED, DOUBLE }; enum BouncesSettings { DISABLED, ENABLED, DOUBLE };
static Dvar::Var PlayerDuckedSpeedScale;
static Dvar::Var PlayerLastStandCrawlSpeedScale;
static Dvar::Var PlayerProneSpeedScale;
static Dvar::Var PlayerSpectateSpeedScale; static Dvar::Var PlayerSpectateSpeedScale;
static Dvar::Var CGUfoScaler; static Dvar::Var CGUfoScaler;
static Dvar::Var CGNoclipScaler; static Dvar::Var CGNoclipScaler;
@ -22,11 +19,13 @@ namespace Components
static Dvar::Var BGPlayerCollision; static Dvar::Var BGPlayerCollision;
// Can't use Var class inside assembly stubs // Can't use Var class inside assembly stubs
static Game::dvar_t* BGBounces; static Game::dvar_t* BGBounces;
static Game::dvar_t* PlayerDuckedSpeedScale;
static Game::dvar_t* PlayerProneSpeedScale;
static float PM_CmdScaleForStance(const Game::pmove_s* move); static void PM_PlayerDuckedSpeedScaleStub();
static void PM_CmdScaleForStanceStub(); static void PM_PlayerProneSpeedScaleStub();
static float PM_MoveScale(Game::playerState_s* ps, float forwardmove, float rightmove, float upmove); static float PM_MoveScale(Game::playerState_s* ps, float fmove, float rmove, float umove);
static void PM_MoveScaleStub(); static void PM_MoveScaleStub();
// Bounce logic // Bounce logic
@ -40,7 +39,6 @@ namespace Components
static int StuckInClient_Hk(Game::gentity_s* self); static int StuckInClient_Hk(Game::gentity_s* self);
static void CM_TransformedCapsuleTrace_Hk(Game::trace_t* results, const float* start, const float* end, const Game::Bounds* bounds, const Game::Bounds* capsule, int contents, const float* origin, const float* angles); static void CM_TransformedCapsuleTrace_Hk(Game::trace_t* results, const float* start, const float* end, const Game::Bounds* bounds, const Game::Bounds* capsule, int contents, const float* origin, const float* angles);
static Game::dvar_t* Dvar_RegisterLastStandSpeedScale(const char* dvarName, float value, float min, float max, unsigned __int16 flags, const char* description);
static Game::dvar_t* Dvar_RegisterSpectateSpeedScale(const char* dvarName, float value, float min, float max, unsigned __int16 flags, const char* description); static Game::dvar_t* Dvar_RegisterSpectateSpeedScale(const char* dvarName, float value, float min, float max, unsigned __int16 flags, const char* description);
}; };
} }

View File

@ -10,48 +10,59 @@ namespace Components
{ {
Game::NET_StringToAdr(addrString.data(), &this->address); Game::NET_StringToAdr(addrString.data(), &this->address);
} }
Network::Address::Address(sockaddr* addr) Network::Address::Address(sockaddr* addr)
{ {
Game::SockadrToNetadr(addr, &this->address); Game::SockadrToNetadr(addr, &this->address);
} }
bool Network::Address::operator==(const Network::Address& obj) const bool Network::Address::operator==(const Network::Address& obj) const
{ {
return Game::NET_CompareAdr(this->address, obj.address); return Game::NET_CompareAdr(this->address, obj.address);
} }
void Network::Address::setPort(unsigned short port) void Network::Address::setPort(unsigned short port)
{ {
this->address.port = htons(port); this->address.port = htons(port);
} }
unsigned short Network::Address::getPort() unsigned short Network::Address::getPort()
{ {
return ntohs(this->address.port); return ntohs(this->address.port);
} }
void Network::Address::setIP(DWORD ip) void Network::Address::setIP(DWORD ip)
{ {
this->address.ip.full = ip; this->address.ip.full = ip;
} }
void Network::Address::setIP(Game::netIP_t ip) void Network::Address::setIP(Game::netIP_t ip)
{ {
this->address.ip = ip; this->address.ip = ip;
} }
Game::netIP_t Network::Address::getIP() Game::netIP_t Network::Address::getIP()
{ {
return this->address.ip; return this->address.ip;
} }
void Network::Address::setType(Game::netadrtype_t type) void Network::Address::setType(Game::netadrtype_t type)
{ {
this->address.type = type; this->address.type = type;
} }
Game::netadrtype_t Network::Address::getType() Game::netadrtype_t Network::Address::getType()
{ {
return this->address.type; return this->address.type;
} }
sockaddr Network::Address::getSockAddr() sockaddr Network::Address::getSockAddr()
{ {
sockaddr addr; sockaddr addr;
this->toSockAddr(&addr); this->toSockAddr(&addr);
return addr; return addr;
} }
void Network::Address::toSockAddr(sockaddr* addr) void Network::Address::toSockAddr(sockaddr* addr)
{ {
if (addr) if (addr)
@ -59,22 +70,27 @@ namespace Components
Game::NetadrToSockadr(&this->address, addr); Game::NetadrToSockadr(&this->address, addr);
} }
} }
void Network::Address::toSockAddr(sockaddr_in* addr) void Network::Address::toSockAddr(sockaddr_in* addr)
{ {
this->toSockAddr(reinterpret_cast<sockaddr*>(addr)); this->toSockAddr(reinterpret_cast<sockaddr*>(addr));
} }
Game::netadr_t* Network::Address::get() Game::netadr_t* Network::Address::get()
{ {
return &this->address; return &this->address;
} }
const char* Network::Address::getCString() const const char* Network::Address::getCString() const
{ {
return Game::NET_AdrToString(this->address); return Game::NET_AdrToString(this->address);
} }
std::string Network::Address::getString() const std::string Network::Address::getString() const
{ {
return this->getCString(); return this->getCString();
} }
bool Network::Address::isLocal() bool Network::Address::isLocal()
{ {
// According to: https://en.wikipedia.org/wiki/Private_network // According to: https://en.wikipedia.org/wiki/Private_network
@ -95,6 +111,7 @@ namespace Components
return false; return false;
} }
bool Network::Address::isSelf() bool Network::Address::isSelf()
{ {
if (Game::NET_IsLocalAddress(this->address)) return true; // Loopback if (Game::NET_IsLocalAddress(this->address)) return true; // Loopback
@ -110,6 +127,7 @@ namespace Components
return false; return false;
} }
bool Network::Address::isLoopback() bool Network::Address::isLoopback()
{ {
if (this->getIP().full == 0x100007f) // 127.0.0.1 if (this->getIP().full == 0x100007f) // 127.0.0.1
@ -119,10 +137,12 @@ namespace Components
return Game::NET_IsLocalAddress(this->address); return Game::NET_IsLocalAddress(this->address);
} }
bool Network::Address::isValid() bool Network::Address::isValid()
{ {
return (this->getType() != Game::netadrtype_t::NA_BAD && this->getType() >= Game::netadrtype_t::NA_BOT && this->getType() <= Game::netadrtype_t::NA_IP); return (this->getType() != Game::netadrtype_t::NA_BAD && this->getType() >= Game::netadrtype_t::NA_BOT && this->getType() <= Game::netadrtype_t::NA_IP);
} }
void Network::Handle(const std::string& packet, Utils::Slot<Network::Callback> callback) void Network::Handle(const std::string& packet, Utils::Slot<Network::Callback> callback)
{ {
Network::PacketHandlers[Utils::String::ToLower(packet)] = callback; Network::PacketHandlers[Utils::String::ToLower(packet)] = callback;

View File

@ -167,6 +167,7 @@ namespace Components
void Node::RunFrame() void Node::RunFrame()
{ {
if (ServerList::useMasterServer) return;
if (Dedicated::IsEnabled() && Dvar::Var("sv_lanOnly").get<bool>()) return; if (Dedicated::IsEnabled() && Dvar::Var("sv_lanOnly").get<bool>()) return;
if (!Dedicated::IsEnabled() && *Game::clcState > 0) if (!Dedicated::IsEnabled() && *Game::clcState > 0)
@ -246,7 +247,7 @@ namespace Components
if (list.isnode() && (!list.port() || list.port() == address.getPort())) if (list.isnode() && (!list.port() || list.port() == address.getPort()))
{ {
if (!Dedicated::IsEnabled() && ServerList::IsOnlineList() && list.protocol() == PROTOCOL) if (!Dedicated::IsEnabled() && ServerList::IsOnlineList() && !ServerList::useMasterServer && list.protocol() == PROTOCOL)
{ {
NODE_LOG("Inserting %s into the serverlist\n", address.getCString()); NODE_LOG("Inserting %s into the serverlist\n", address.getCString());
ServerList::InsertRequest(address); ServerList::InsertRequest(address);

View File

@ -307,7 +307,7 @@ namespace Components
{ {
int botCount = 0; int botCount = 0;
int clientCount = 0; int clientCount = 0;
int maxclientCount = *Game::svs_numclients; int maxclientCount = *Game::svs_clientCount;
if (maxclientCount) if (maxclientCount)
{ {
@ -315,7 +315,7 @@ namespace Components
{ {
if (Game::svs_clients[i].state >= 3) if (Game::svs_clients[i].state >= 3)
{ {
if (Game::svs_clients[i].isBot) ++botCount; if (Game::svs_clients[i].bIsTestClient) ++botCount;
else ++clientCount; else ++clientCount;
} }
} }

View File

@ -26,12 +26,12 @@ namespace Components
} }
} }
__declspec(naked) void PlayerName::ClientUserinfoChanged() __declspec(naked) void PlayerName::ClientCleanName()
{ {
__asm __asm
{ {
mov eax, [esp + 4h] // length mov eax, [esp + 4h] // length
//sub eax, 1
push eax push eax
push ecx // name push ecx // name
@ -53,12 +53,57 @@ namespace Components
return buf; return buf;
} }
char* PlayerName::CleanStrStub(char* string) char* PlayerName::CleanStrStub(char* string)
{ {
TextRenderer::StripColors(string, string, strlen(string) + 1); TextRenderer::StripColors(string, string, strlen(string) + 1);
return string; return string;
} }
bool PlayerName::CopyClientNameCheck(char* dest, const char* source, int size)
{
Utils::Hook::Call<void(char*, const char*, int)>(0x4D6F80)(dest, source, size); // I_strncpyz
auto i = 0;
while (i < size - 1 && dest[i] != '\0')
{
if (dest[i] > 125 || dest[i] < 32 || dest[i] == '%')
{
return false; // Illegal string
}
++i;
}
return true;
}
__declspec(naked) void PlayerName::SV_UserinfoChangedStub()
{
__asm
{
call CopyClientNameCheck
test al, al
jnz returnSafe
pushad
push 1 // tellThem
push INVALID_NAME_MSG // reason
push edi // drop
mov eax, 0x4D1600 // SV_DropClient
call eax
add esp, 0xC
popad
returnSafe:
push 0x401988
retn
}
}
PlayerName::PlayerName() PlayerName::PlayerName()
{ {
sv_allowColoredNames = Dvar::Register<bool>("sv_allowColoredNames", true, Game::dvar_flag::DVAR_NONE, "Allow colored names on the server"); sv_allowColoredNames = Dvar::Register<bool>("sv_allowColoredNames", true, Game::dvar_flag::DVAR_NONE, "Allow colored names on the server");
@ -66,13 +111,17 @@ namespace Components
// Disable SV_UpdateUserinfo_f, to block changing the name ingame // Disable SV_UpdateUserinfo_f, to block changing the name ingame
Utils::Hook::Set<BYTE>(0x6258D0, 0xC3); Utils::Hook::Set<BYTE>(0x6258D0, 0xC3);
// Allow colored names ingame // Allow colored names ingame. Hook placed in ClientUserinfoChanged
Utils::Hook(0x5D8B40, ClientUserinfoChanged, HOOK_JUMP).install()->quick(); Utils::Hook(0x5D8B40, ClientCleanName, HOOK_JUMP).install()->quick();
// Though, don't apply that to overhead names. // Though, don't apply that to overhead names.
Utils::Hook(0x581932, GetClientName, HOOK_CALL).install()->quick(); Utils::Hook(0x581932, GetClientName, HOOK_CALL).install()->quick();
// Patch I_CleanStr // Patch I_CleanStr
Utils::Hook(0x4AD470, CleanStrStub, HOOK_JUMP).install()->quick(); Utils::Hook(0x4AD470, CleanStrStub, HOOK_JUMP).install()->quick();
// Detect invalid characters including '%' to prevent format string vulnerabilities.
// Kicks the player as soon as possible
Utils::Hook(0x401983, SV_UserinfoChangedStub, HOOK_JUMP).install()->quick();
} }
} }

View File

@ -11,9 +11,14 @@ namespace Components
private: private:
static Dvar::Var sv_allowColoredNames; static Dvar::Var sv_allowColoredNames;
// Message used when kicking players
static constexpr auto INVALID_NAME_MSG = "Invalid name detected";
static char* CleanStrStub(char* string); static char* CleanStrStub(char* string);
static void ClientUserinfoChanged(); static void ClientCleanName();
static char* GetClientName(int localClientNum, int index, char* buf, size_t size); static char* GetClientName(int localClientNum, int index, char* buf, size_t size);
static bool CopyClientNameCheck(char* dest, const char* source, int size);
static void SV_UserinfoChangedStub();
}; };
} }

View File

@ -47,62 +47,6 @@ namespace Components
} }
} }
int QuickPatch::MsgReadBitsCompressCheckSV(const char *from, char *to, int size)
{
static char buffer[0x8000];
if (size > 0x800) return 0;
size = Game::MSG_ReadBitsCompress(from, buffer, size);
if (size > 0x800) return 0;
std::memcpy(to, buffer, size);
return size;
}
int QuickPatch::MsgReadBitsCompressCheckCL(const char *from, char *to, int size)
{
static char buffer[0x100000];
if (size > 0x20000) return 0;
size = Game::MSG_ReadBitsCompress(from, buffer, size);
if (size > 0x20000) return 0;
std::memcpy(to, buffer, size);
return size;
}
int QuickPatch::SVCanReplaceServerCommand(Game::client_t* /*client*/, const char* /*cmd*/)
{
// This is a fix copied from V2. As I don't have time to investigate, let's simply trust them
return -1;
}
long QuickPatch::AtolAdjustPlayerLimit(const char* string)
{
return std::min(atol(string), 18l);
}
void QuickPatch::SelectStringTableEntryInDvarStub()
{
Command::ClientParams params;
if (params.size() >= 4)
{
const auto* dvarName = params[3];
const auto* dvar = Game::Dvar_FindVar(dvarName);
if (Command::Find(dvarName) ||
(dvar != nullptr && dvar->flags & (Game::DVAR_WRITEPROTECTED | Game::DVAR_CHEAT | Game::DVAR_READONLY)))
{
return;
}
}
Game::CL_SelectStringTableEntryInDvar_f();
}
__declspec(naked) void QuickPatch::JavelinResetHookStub() __declspec(naked) void QuickPatch::JavelinResetHookStub()
{ {
__asm __asm
@ -117,69 +61,6 @@ namespace Components
} }
} }
__declspec(naked) int QuickPatch::G_GetClientScore()
{
__asm
{
mov eax, [esp + 4] // index
mov ecx, ds : 1A831A8h // level: &g_clients
test ecx, ecx;
jz invalid_ptr;
imul eax, 366Ch
mov eax, [eax + ecx + 3134h]
ret
invalid_ptr:
xor eax, eax
ret
}
}
bool QuickPatch::InvalidNameCheck(char* dest, const char* source, int size)
{
Utils::Hook::Call<void(char*, const char*, int)>(0x4D6F80)(dest, source, size); // I_strncpyz
for (int i = 0; i < size - 1; i++)
{
if (!dest[i]) break;
if (dest[i] > 125 || dest[i] < 32 || dest[i] == '%')
{
return false;
}
}
return true;
}
__declspec(naked) void QuickPatch::InvalidNameStub()
{
static const char* kick_reason = "Invalid name detected.";
__asm
{
call InvalidNameCheck;
test al, al
jnz returnSafe;
pushad;
push 1;
push kick_reason;
push edi;
mov eax, 0x004D1600; // SV_DropClientInternal
call eax;
add esp, 12;
popad;
returnSafe:
push 0x00401988;
retn;
}
}
Game::dvar_t* QuickPatch::g_antilag; Game::dvar_t* QuickPatch::g_antilag;
__declspec(naked) void QuickPatch::ClientEventsFireWeaponStub() __declspec(naked) void QuickPatch::ClientEventsFireWeaponStub()
{ {
@ -378,9 +259,6 @@ namespace Components
Utils::Hook(0x5D6D56, QuickPatch::ClientEventsFireWeaponStub, HOOK_JUMP).install()->quick(); Utils::Hook(0x5D6D56, QuickPatch::ClientEventsFireWeaponStub, HOOK_JUMP).install()->quick();
Utils::Hook(0x5D6D6A, QuickPatch::ClientEventsFireWeaponMeleeStub, HOOK_JUMP).install()->quick(); Utils::Hook(0x5D6D6A, QuickPatch::ClientEventsFireWeaponMeleeStub, HOOK_JUMP).install()->quick();
// Disallow invalid player names
Utils::Hook(0x401983, QuickPatch::InvalidNameStub, HOOK_JUMP).install()->quick();
// Javelin fix // Javelin fix
Utils::Hook(0x578F52, QuickPatch::JavelinResetHookStub, HOOK_JUMP).install()->quick(); Utils::Hook(0x578F52, QuickPatch::JavelinResetHookStub, HOOK_JUMP).install()->quick();
@ -644,21 +522,6 @@ namespace Components
} }
}); });
// Exploit fixes
Utils::Hook::Set<BYTE>(0x412370, 0xC3); // SV_SteamAuthClient
Utils::Hook::Set<BYTE>(0x5A8C70, 0xC3); // CL_HandleRelayPacket
Utils::Hook(0x414D92, QuickPatch::MsgReadBitsCompressCheckSV, HOOK_CALL).install()->quick(); // SV_ExecuteClientCommands
Utils::Hook(0x4A9F56, QuickPatch::MsgReadBitsCompressCheckCL, HOOK_CALL).install()->quick(); // CL_ParseServerMessage
Utils::Hook(0x407376, QuickPatch::SVCanReplaceServerCommand , HOOK_CALL).install()->quick(); // SV_CanReplaceServerCommand
Utils::Hook(0x5B67ED, QuickPatch::AtolAdjustPlayerLimit , HOOK_CALL).install()->quick(); // PartyHost_HandleJoinPartyRequest
Utils::Hook::Nop(0x41698E, 5); // Disable Svcmd_EntityList_f
// Patch selectStringTableEntryInDvar
Utils::Hook::Set(0x405959, QuickPatch::SelectStringTableEntryInDvarStub);
// Patch G_GetClientScore for uninitialised game
Utils::Hook(0x469AC0, QuickPatch::G_GetClientScore, HOOK_JUMP).install()->quick();
// Ignore call to print 'Offhand class mismatch when giving weapon...' // Ignore call to print 'Offhand class mismatch when giving weapon...'
Utils::Hook(0x5D9047, 0x4BB9B0, HOOK_CALL).install()->quick(); Utils::Hook(0x5D9047, 0x4BB9B0, HOOK_CALL).install()->quick();
@ -858,9 +721,6 @@ namespace Components
}); });
#endif #endif
// Dvars
Dvar::Register<bool>("ui_streamFriendly", false, Game::DVAR_ARCHIVE, "Stream friendly UI");
// Debug patches // Debug patches
#ifdef DEBUG #ifdef DEBUG
// ui_debugMode 1 // ui_debugMode 1

View File

@ -12,21 +12,8 @@ namespace Components
static void UnlockStats(); static void UnlockStats();
private: private:
static void SelectStringTableEntryInDvarStub();
static int SVCanReplaceServerCommand(Game::client_t *client, const char *cmd);
static int G_GetClientScore();
static int MsgReadBitsCompressCheckSV(const char *from, char *to, int size);
static int MsgReadBitsCompressCheckCL(const char *from, char *to, int size);
static long AtolAdjustPlayerLimit(const char* string);
static void JavelinResetHookStub(); static void JavelinResetHookStub();
static bool InvalidNameCheck(char* dest, const char* source, int size);
static void InvalidNameStub();
static Dvar::Var r_customAspectRatio; static Dvar::Var r_customAspectRatio;
static Game::dvar_t* Dvar_RegisterAspectRatioDvar(const char* dvarName, const char** valueList, int defaultIndex, unsigned __int16 flags, const char* description); static Game::dvar_t* Dvar_RegisterAspectRatioDvar(const char* dvarName, const char** valueList, int defaultIndex, unsigned __int16 flags, const char* description);
static void SetAspectRatioStub(); static void SetAspectRatioStub();

View File

@ -0,0 +1,161 @@
#include <STDInclude.hpp>
namespace Components
{
Dvar::Var RawMouse::M_RawInput;
int RawMouse::MouseRawX = 0;
int RawMouse::MouseRawY = 0;
void RawMouse::IN_ClampMouseMove()
{
tagRECT rc;
tagPOINT curPos;
GetCursorPos(&curPos);
GetWindowRect(Window::GetWindow(), &rc);
auto isClamped = false;
if (curPos.x >= rc.left)
{
if (curPos.x >= rc.right)
{
curPos.x = rc.right - 1;
isClamped = true;
}
}
else
{
curPos.x = rc.left;
isClamped = true;
}
if (curPos.y >= rc.top)
{
if (curPos.y >= rc.bottom)
{
curPos.y = rc.bottom - 1;
isClamped = true;
}
}
else
{
curPos.y = rc.top;
isClamped = true;
}
if (isClamped)
{
SetCursorPos(curPos.x, curPos.y);
}
}
BOOL RawMouse::OnRawInput(LPARAM lParam, WPARAM)
{
auto dwSize = sizeof(RAWINPUT);
static BYTE lpb[sizeof(RAWINPUT)];
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER));
auto* raw = reinterpret_cast<RAWINPUT*>(lpb);
if (raw->header.dwType == RIM_TYPEMOUSE)
{
// Is there's really absolute mouse on earth?
if (raw->data.mouse.usFlags & MOUSE_MOVE_ABSOLUTE)
{
MouseRawX = raw->data.mouse.lLastX;
MouseRawY = raw->data.mouse.lLastY;
}
else
{
MouseRawX += raw->data.mouse.lLastX;
MouseRawY += raw->data.mouse.lLastY;
}
}
return TRUE;
}
void RawMouse::IN_RawMouseMove()
{
static auto r_fullscreen = Dvar::Var("r_fullscreen");
if (GetForegroundWindow() == Window::GetWindow())
{
if (r_fullscreen.get<bool>())
IN_ClampMouseMove();
static auto oldX = 0, oldY = 0;
auto dx = MouseRawX - oldX;
auto dy = MouseRawY - oldY;
oldX = MouseRawX;
oldY = MouseRawY;
// Don't use raw input for menu?
// Because it needs to call the ScreenToClient
tagPOINT curPos;
GetCursorPos(&curPos);
Game::s_wmv->oldPos = curPos;
ScreenToClient(Window::GetWindow(), &curPos);
auto recenterMouse = Game::CL_MouseEvent(curPos.x, curPos.y, dx, dy);
if (recenterMouse)
{
Game::IN_RecenterMouse();
}
}
}
void RawMouse::IN_RawMouse_Init()
{
if (Window::GetWindow() && RawMouse::M_RawInput.get<bool>())
{
#ifdef DEBUG
Logger::Print("Raw Mouse Init.\n");
#endif
RAWINPUTDEVICE Rid[1];
Rid[0].usUsagePage = 0x01; // HID_USAGE_PAGE_GENERIC
Rid[0].usUsage = 0x02; // HID_USAGE_GENERIC_MOUSE
Rid[0].dwFlags = RIDEV_INPUTSINK;
Rid[0].hwndTarget = Window::GetWindow();
RegisterRawInputDevices(Rid, ARRAYSIZE(Rid), sizeof(Rid[0]));
}
}
void RawMouse::IN_Init()
{
Game::IN_Init();
IN_RawMouse_Init();
}
void RawMouse::IN_MouseMove()
{
if (RawMouse::M_RawInput.get<bool>())
{
IN_RawMouseMove();
}
else
{
Game::IN_MouseMove();
}
}
RawMouse::RawMouse()
{
Utils::Hook(0x475E65, RawMouse::IN_MouseMove, HOOK_JUMP).install()->quick();
Utils::Hook(0x475E8D, RawMouse::IN_MouseMove, HOOK_JUMP).install()->quick();
Utils::Hook(0x467C03, RawMouse::IN_Init, HOOK_CALL).install()->quick();
Utils::Hook(0x64D095, RawMouse::IN_Init, HOOK_JUMP).install()->quick();
Dvar::OnInit([]()
{
RawMouse::M_RawInput = Dvar::Register<bool>("m_rawinput", true, Game::dvar_flag::DVAR_ARCHIVE, "Use raw mouse input, Improves accuracy & has better support for higher polling rates. Use in_restart to take effect if not enabled.");
});
Window::OnWndMessage(WM_INPUT, RawMouse::OnRawInput);
Window::OnCreate(RawMouse::IN_RawMouse_Init);
}
}

View File

@ -0,0 +1,22 @@
#pragma once
namespace Components
{
class RawMouse : public Component
{
public:
RawMouse();
static void IN_MouseMove();
private:
static Dvar::Var M_RawInput;
static int MouseRawX, MouseRawY;
static void IN_ClampMouseMove();
static BOOL OnRawInput(LPARAM lParam, WPARAM);
static void IN_RawMouseMove();
static void IN_RawMouse_Init();
static void IN_Init();
};
}

View File

@ -18,7 +18,6 @@ namespace Components
static void OnDeviceRecoveryEnd(Utils::Slot<Scheduler::Callback> callback); static void OnDeviceRecoveryEnd(Utils::Slot<Scheduler::Callback> callback);
static void OnDeviceRecoveryBegin(Utils::Slot<Scheduler::Callback> callback); static void OnDeviceRecoveryBegin(Utils::Slot<Scheduler::Callback> callback);
private: private:
static void FrameStub(); static void FrameStub();

View File

@ -4,29 +4,30 @@ namespace Components
{ {
std::string Script::ScriptName; std::string Script::ScriptName;
std::vector<int> Script::ScriptHandles; std::vector<int> Script::ScriptHandles;
std::vector<Script::Function> Script::ScriptFunctions; std::unordered_map<std::string, Game::BuiltinFunctionDef> Script::CustomScrFunctions;
std::unordered_map<std::string, Game::BuiltinMethodDef> Script::CustomScrMethods;
std::vector<std::string> Script::ScriptNameStack; std::vector<std::string> Script::ScriptNameStack;
unsigned short Script::FunctionName; unsigned short Script::FunctionName;
std::unordered_map<std::string, std::string> Script::ScriptStorage; std::unordered_map<std::string, std::string> Script::ScriptStorage;
std::unordered_map<int, std::string> Script::ScriptBaseProgramNum; std::unordered_map<int, std::string> Script::ScriptBaseProgramNum;
std::unordered_map<const char*, const char*> Script::ReplacedFunctions; std::unordered_map<const char*, const char*> Script::ReplacedFunctions;
const char* Script::ReplacedPos = 0; const char* Script::ReplacedPos = nullptr;
int Script::LastFrameTime = -1; int Script::LastFrameTime = -1;
Utils::Signal<Scheduler::Callback> Script::VMShutdownSignal; Utils::Signal<Scheduler::Callback> Script::VMShutdownSignal;
void Script::FunctionError() void Script::FunctionError()
{ {
std::string funcName = Game::SL_ConvertToString(Script::FunctionName); const auto* funcName = Game::SL_ConvertToString(Script::FunctionName);
Game::Scr_ShutdownAllocNode(); Game::Scr_ShutdownAllocNode();
Logger::Print(23, "\n"); Logger::Print(23, "\n");
Logger::Print(23, "******* script compile error *******\n"); Logger::Print(23, "******* script compile error *******\n");
Logger::Print(23, "Error: unknown function %s in %s\n", funcName.data(), Script::ScriptName.data()); Logger::Print(23, "Error: unknown function %s in %s\n", funcName, Script::ScriptName.data());
Logger::Print(23, "************************************\n"); Logger::Print(23, "************************************\n");
Logger::Error(Game::ERR_SCRIPT_DROP, "script compile error\nunknown function %s\n%s\n\n", funcName.data(), Script::ScriptName.data()); Logger::Error(Game::ERR_SCRIPT_DROP, "script compile error\nunknown function %s\n%s\n\n", funcName, Script::ScriptName.data());
} }
__declspec(naked) void Script::StoreFunctionNameStub() __declspec(naked) void Script::StoreFunctionNameStub()
@ -45,6 +46,37 @@ namespace Components
} }
} }
void Script::RuntimeError(const char* codePos, unsigned int index, const char* msg, const char* dialogMessage)
{
const auto developer = Dvar::Var("developer").get<int>();
// Allow error messages to be printed if developer mode is on
// Should check scrVarPub.developer but it's absent
// in this version of the game so let's check the dvar
if (!Game::scrVmPub->terminal_error && !developer)
return;
// If were are developing let's call RuntimeErrorInternal
// scrVmPub.debugCode seems to be always false
if (Game::scrVmPub->debugCode || Game::scrVarPub->developer_script)
{
Game::RuntimeErrorInternal(23, codePos, index, msg);
}
else
{
Logger::Print(23, "%s\n", msg);
}
// Let's not throw error unless we have to
if (Game::scrVmPub->terminal_error)
{
if (dialogMessage == nullptr)
dialogMessage = "";
Logger::Error(Game::ERR_SCRIPT_DROP, "\x15script runtime error\n(see console for details)\n%s\n%s", msg, dialogMessage);
}
}
void Script::StoreScriptName(const char* name) void Script::StoreScriptName(const char* name)
{ {
Script::ScriptNameStack.push_back(Script::ScriptName); Script::ScriptNameStack.push_back(Script::ScriptName);
@ -108,11 +140,9 @@ namespace Components
std::string buffer = script.getBuffer(); std::string buffer = script.getBuffer();
Utils::String::Replace(buffer, "\t", " "); Utils::String::Replace(buffer, "\t", " ");
int line = 1; auto line = 1, lineOffset = 0, inlineOffset = 0;
int lineOffset = 0;
int inlineOffset = 0;
for (unsigned int i = 0; i < buffer.size(); ++i) for (size_t i = 0; i < buffer.size(); ++i)
{ {
// Terminate line // Terminate line
if (i == offset) if (i == offset)
@ -129,7 +159,7 @@ namespace Components
if (buffer[i] == '\n') if (buffer[i] == '\n')
{ {
++line; ++line;
lineOffset = i; // Includes the line break! lineOffset = static_cast<int>(i); // Includes the line break!
inlineOffset = 0; inlineOffset = 0;
} }
else else
@ -141,7 +171,7 @@ namespace Components
Logger::Print(23, "in file %s, line %d:", filename, line); Logger::Print(23, "in file %s, line %d:", filename, line);
Logger::Print(23, "%s\n", buffer.data() + lineOffset); Logger::Print(23, "%s\n", buffer.data() + lineOffset);
for (int i = 0; i < (inlineOffset - 1); ++i) for (auto i = 0; i < (inlineOffset - 1); ++i)
{ {
Logger::Print(23, " "); Logger::Print(23, " ");
} }
@ -157,10 +187,10 @@ namespace Components
void Script::CompileError(unsigned int offset, const char* message, ...) void Script::CompileError(unsigned int offset, const char* message, ...)
{ {
char msgbuf[1024] = {0}; char msgbuf[1024] = {0};
va_list v; va_list va;
va_start(v, message); va_start(va, message);
_vsnprintf_s(msgbuf, sizeof(msgbuf), message, v); _vsnprintf_s(msgbuf, _TRUNCATE, message, va);
va_end(v); va_end(va);
Game::Scr_ShutdownAllocNode(); Game::Scr_ShutdownAllocNode();
@ -180,7 +210,7 @@ namespace Components
if (!Game::Scr_LoadScript(script.data())) if (!Game::Scr_LoadScript(script.data()))
{ {
Logger::Print("Script %s encountered an error while loading. (doesn't exist?)", script.data()); Logger::Print("Script %s encountered an error while loading. (doesn't exist?)", script.data());
Logger::Error(Game::ERR_DROP, reinterpret_cast<char*>(0x70B810), script.data()); Logger::Error(Game::ERR_DROP, reinterpret_cast<const char*>(0x70B810), script.data());
} }
else else
{ {
@ -188,7 +218,7 @@ namespace Components
} }
Logger::Print("Finding script handle %s::%s...\n", script.data(), label.data()); Logger::Print("Finding script handle %s::%s...\n", script.data(), label.data());
int handle = Game::Scr_GetFunctionHandle(script.data(), label.data()); const auto handle = Game::Scr_GetFunctionHandle(script.data(), label.data());
if (handle) if (handle)
{ {
Logger::Print("Script handle %s::%s loaded successfully.\n", script.data(), label.data()); Logger::Print("Script handle %s::%s loaded successfully.\n", script.data(), label.data());
@ -201,7 +231,7 @@ namespace Components
void Script::LoadGameType() void Script::LoadGameType()
{ {
for (auto handle : Script::ScriptHandles) for (const auto& handle : Script::ScriptHandles)
{ {
Game::Scr_FreeThread(Game::Scr_ExecThread(handle, 0)); Game::Scr_FreeThread(Game::Scr_ExecThread(handle, 0));
} }
@ -213,19 +243,23 @@ namespace Components
{ {
Script::ScriptHandles.clear(); Script::ScriptHandles.clear();
auto list = FileSystem::GetFileList("scripts/", "gsc"); const auto list = FileSystem::GetFileList("scripts/", "gsc");
for (auto file : list) for (auto file : list)
{ {
file = "scripts/" + file; file.insert(0, "scripts/");
if (Utils::String::EndsWith(file, ".gsc")) if (Utils::String::EndsWith(file, ".gsc"))
{ {
file = file.substr(0, file.size() - 4); file = file.substr(0, file.size() - 4);
} }
int handle = Script::LoadScriptAndLabel(file, "init"); auto handle = Script::LoadScriptAndLabel(file, "init");
if (handle) Script::ScriptHandles.push_back(handle);
if (handle)
{
Script::ScriptHandles.push_back(handle);
}
else else
{ {
handle = Script::LoadScriptAndLabel(file, "main"); handle = Script::LoadScriptAndLabel(file, "main");
@ -236,62 +270,73 @@ namespace Components
Game::GScr_LoadGameTypeScript(); Game::GScr_LoadGameTypeScript();
} }
void Script::AddFunction(const std::string& name, Game::scr_function_t function, bool isDev) void Script::AddFunction(const char* name, Game::BuiltinFunction func, int type)
{ {
for (auto i = Script::ScriptFunctions.begin(); i != Script::ScriptFunctions.end();) Game::BuiltinFunctionDef toAdd;
{ toAdd.actionString = name;
if (i->getName() == name) toAdd.actionFunc = func;
{ toAdd.type = type;
i = Script::ScriptFunctions.erase(i);
continue; CustomScrFunctions.insert_or_assign(Utils::String::ToLower(name), toAdd);
} }
++i; void Script::AddMethod(const char* name, Game::BuiltinMethod func, int type)
{
Game::BuiltinMethodDef toAdd;
toAdd.actionString = name;
toAdd.actionFunc = func;
toAdd.type = type;
CustomScrMethods.insert_or_assign(Utils::String::ToLower(name), toAdd);
} }
Script::ScriptFunctions.push_back({ name, function, isDev }); Game::BuiltinFunction Script::BuiltIn_GetFunctionStub(const char** pName, int* type)
} {
if (pName != nullptr)
{
const auto got = Script::CustomScrFunctions.find(Utils::String::ToLower(*pName));
Game::scr_function_t Script::GetFunction(void* caller, const char** name, int* isDev) // If no function was found let's call game's function
if (got != Script::CustomScrFunctions.end())
{ {
for (auto& function : Script::ScriptFunctions) *type = got->second.type;
{ return got->second.actionFunc;
if (name && *name)
{
if (Utils::String::ToLower(*name) == Utils::String::ToLower(function.getName()))
{
*name = function.getName();
*isDev = function.isDev();
return function.getFunction();
} }
} }
else if (caller == reinterpret_cast<void*>(0x465781)) else
{ {
Game::Scr_RegisterFunction(function.getFunction()); for (const auto& [name, builtin] : Script::CustomScrFunctions)
{
Game::Scr_RegisterFunction(reinterpret_cast<int>(builtin.actionFunc), name.data());
} }
} }
return nullptr; return Utils::Hook::Call<Game::BuiltinFunction(const char**, int*)>(0x5FA2B0)(pName, type); // BuiltIn_GetFunction
} }
__declspec(naked) void Script::GetFunctionStub() Game::BuiltinMethod Script::BuiltIn_GetMethod(const char** pName, int* type)
{ {
__asm if (pName != nullptr)
{ {
test eax, eax const auto got = Script::CustomScrMethods.find(Utils::String::ToLower(*pName));
jnz returnSafe
sub esp, 8h // If no method was found let's call game's function
push [esp + 10h] if (got != Script::CustomScrMethods.end())
call Script::GetFunction {
add esp, 0Ch *type = got->second.type;
return got->second.actionFunc;
returnSafe:
pop edi
pop esi
retn
} }
} }
else
{
for (const auto& [name, builtin] : Script::CustomScrMethods)
{
Game::Scr_RegisterFunction(reinterpret_cast<int>(builtin.actionFunc), name.data());
}
}
return Utils::Hook::Call<Game::BuiltinMethod(const char**, int*)>(0x5FA360)(pName, type); // Player_GetMethod
}
void Script::StoreScriptBaseProgramNum() void Script::StoreScriptBaseProgramNum()
{ {
@ -300,14 +345,12 @@ namespace Components
void Script::Scr_PrintPrevCodePos(int scriptPos) void Script::Scr_PrintPrevCodePos(int scriptPos)
{ {
int bestCodePos = -1; auto bestCodePos = -1, nextCodePos = -1, offset = -1;
int nextCodePos = -1;
int offset = -1;
std::string file; std::string file;
for (auto kv : Script::ScriptBaseProgramNum) for (const auto& [key, value] : Script::ScriptBaseProgramNum)
{ {
int codePos = kv.first; const auto codePos = key;
if (codePos > scriptPos) if (codePos > scriptPos)
{ {
@ -322,17 +365,15 @@ namespace Components
bestCodePos = codePos; bestCodePos = codePos;
file = kv.second; file = value;
offset = scriptPos - bestCodePos; offset = scriptPos - bestCodePos;
} }
if (bestCodePos == -1) if (bestCodePos == -1)
return; return;
float onehundred = 100.0;
Logger::Print(23, "\n@ %d (%d - %d)\n", scriptPos, bestCodePos, nextCodePos); Logger::Print(23, "\n@ %d (%d - %d)\n", scriptPos, bestCodePos, nextCodePos);
Logger::Print(23, "in %s (%.1f%% through the source)\n\n", file.c_str(), ((offset * onehundred) / (nextCodePos - bestCodePos))); Logger::Print(23, "in %s (%.1f%% through the source)\n\n", file.data(), ((offset * 100.0f) / (nextCodePos - bestCodePos)));
} }
__declspec(naked) void Script::Scr_PrintPrevCodePosStub() __declspec(naked) void Script::Scr_PrintPrevCodePosStub()
@ -373,15 +414,15 @@ namespace Components
void Script::OnVMShutdown(Utils::Slot<Scheduler::Callback> callback) void Script::OnVMShutdown(Utils::Slot<Scheduler::Callback> callback)
{ {
Script::ScriptBaseProgramNum.clear(); Script::ScriptBaseProgramNum.clear();
Script::VMShutdownSignal.connect(callback); Script::VMShutdownSignal.connect(std::move(callback));
} }
void Script::ScrShutdownSystemStub(int num) void Script::ScrShutdownSystemStub(unsigned char sys)
{ {
Script::VMShutdownSignal(); Script::VMShutdownSignal();
// Scr_ShutdownSystem // Scr_ShutdownSystem
Utils::Hook::Call<void(int)>(0x421EE0)(num); Utils::Hook::Call<void(unsigned char)>(0x421EE0)(sys);
} }
unsigned int Script::SetExpFogStub() unsigned int Script::SetExpFogStub()
@ -403,7 +444,7 @@ namespace Components
{ {
if (static_cast<unsigned int>(index) >= Game::scrVmPub->outparamcount) if (static_cast<unsigned int>(index) >= Game::scrVmPub->outparamcount)
{ {
Game::Scr_Error("^1GetCodePosForParam: Index is out of range!\n"); Game::Scr_ParamError(static_cast<unsigned int>(index), "^1GetCodePosForParam: Index is out of range!\n");
return ""; return "";
} }
@ -411,7 +452,7 @@ namespace Components
if (value->type != Game::VAR_FUNCTION) if (value->type != Game::VAR_FUNCTION)
{ {
Game::Scr_Error("^1GetCodePosForParam: Expects a function as parameter!\n"); Game::Scr_ParamError(static_cast<unsigned int>(index), "^1GetCodePosForParam: Expects a function as parameter!\n");
return ""; return "";
} }
@ -430,7 +471,7 @@ namespace Components
{ {
if (what[0] == '\0' || with[0] == '\0') if (what[0] == '\0' || with[0] == '\0')
{ {
Logger::Print("Warning: Invalid paramters passed to ReplacedFunctions\n"); Logger::Print("Warning: Invalid parameters passed to ReplacedFunctions\n");
return; return;
} }
@ -487,24 +528,28 @@ namespace Components
} }
} }
Game::gentity_t* Script::getEntFromEntRef(Game::scr_entref_t entref) Game::client_t* Script::GetClient(const Game::gentity_t* ent)
{ {
Game::gentity_t* gentity = &Game::g_entities[entref]; assert(ent != nullptr);
return gentity;
if (ent->client == nullptr)
{
Game::Scr_ObjectError(Utils::String::VA("Entity %i is not a player", ent->s.number));
return nullptr;
} }
Game::client_t* Script::getClientFromEnt(Game::gentity_t* gentity) if (ent->s.number >= *Game::svs_clientCount)
{ {
if (!gentity->client) Game::Scr_ObjectError(Utils::String::VA("Entity %i is out of bounds", ent->s.number));
{ return nullptr;
Logger::Error(Game::ERR_SCRIPT_DROP, "Entity: %i is not a client", gentity);
} }
return &Game::svs_clients[gentity->s.number];
return &Game::svs_clients[ent->s.number];
} }
void Script::AddFunctions() void Script::AddFunctions()
{ {
Script::AddFunction("ReplaceFunc", [](Game::scr_entref_t) // gsc: ReplaceFunc(<function>, <function>) Script::AddFunction("ReplaceFunc", []() // gsc: ReplaceFunc(<function>, <function>)
{ {
if (Game::Scr_GetNumParam() != 2u) if (Game::Scr_GetNumParam() != 2u)
{ {
@ -519,7 +564,7 @@ namespace Components
}); });
// System time // System time
Script::AddFunction("GetSystemTime", [](Game::scr_entref_t) // gsc: GetSystemTime() Script::AddFunction("GetSystemTime", []() // gsc: GetSystemTime()
{ {
SYSTEMTIME time; SYSTEMTIME time;
GetSystemTime(&time); GetSystemTime(&time);
@ -527,7 +572,7 @@ namespace Components
Game::Scr_AddInt(time.wSecond); Game::Scr_AddInt(time.wSecond);
}); });
Script::AddFunction("GetSystemMilliseconds", [](Game::scr_entref_t) // gsc: GetSystemMilliseconds() Script::AddFunction("GetSystemMilliseconds", []() // gsc: GetSystemMilliseconds()
{ {
SYSTEMTIME time; SYSTEMTIME time;
GetSystemTime(&time); GetSystemTime(&time);
@ -535,106 +580,116 @@ namespace Components
Game::Scr_AddInt(time.wMilliseconds); Game::Scr_AddInt(time.wMilliseconds);
}); });
// Print to console, even without being in 'developer 1'.
Script::AddFunction("PrintConsole", [](Game::scr_entref_t) // gsc: PrintConsole(<string>)
{
if (Game::Scr_GetNumParam() != 1u || Game::Scr_GetType(0) != Game::VAR_STRING)
{
Game::Scr_Error("^1PrintConsole: Needs one string parameter!\n");
return;
}
auto str = Game::Scr_GetString(0);
Game::Com_Printf(0, str);
});
// Executes command to the console // Executes command to the console
Script::AddFunction("Exec", [](Game::scr_entref_t) // gsc: Exec(<string>) Script::AddFunction("Exec", []() // gsc: Exec(<string>)
{ {
if (Game::Scr_GetNumParam() != 1u || Game::Scr_GetType(0) != Game::VAR_STRING) const auto str = Game::Scr_GetString(0);
if (str == nullptr)
{ {
Game::Scr_Error("^1Exec: Needs one string parameter!\n"); Game::Scr_ParamError(0, "^1Exec: Illegal parameter!\n");
return; return;
} }
auto str = Game::Scr_GetString(0);
Command::Execute(str, false); Command::Execute(str, false);
}); });
// Allow printing to the console even when developer is 0
Script::AddFunction("PrintConsole", []() // gsc: PrintConsole(<string>)
{
for (auto i = 0u; i < Game::Scr_GetNumParam(); i++)
{
const auto str = Game::Scr_GetString(i);
// Script Storage Funcs if (str == nullptr)
Script::AddFunction("StorageSet", [](Game::scr_entref_t) // gsc: StorageSet(<str key>, <str data>);
{ {
if (Game::Scr_GetNumParam() != 2u || Game::Scr_GetType(0) != Game::VAR_STRING || Game::Scr_GetType(1) != Game::VAR_STRING) Game::Scr_ParamError(i, "^1PrintConsole: Illegal parameter!\n");
{
Game::Scr_Error("^1StorageSet: Needs two string parameters!\n");
return; return;
} }
std::string key = Game::Scr_GetString(0); Logger::Print(Game::level->scriptPrintChannel, "%s", str);
std::string data = Game::Scr_GetString(1); }
Script::ScriptStorage.insert_or_assign(key, data);
}); });
Script::AddFunction("StorageRemove", [](Game::scr_entref_t) // gsc: StorageRemove(<str key>); // Script Storage Functions
Script::AddFunction("StorageSet", []() // gsc: StorageSet(<str key>, <str data>);
{ {
if (Game::Scr_GetNumParam() != 1u || Game::Scr_GetType(0) != Game::VAR_STRING) const auto* key = Game::Scr_GetString(0);
const auto* value = Game::Scr_GetString(1);
if (key == nullptr || value == nullptr)
{ {
Game::Scr_Error("^1StorageRemove: Needs one string parameter!\n"); Game::Scr_Error("^1StorageSet: Illegal parameters!\n");
return; return;
} }
std::string key = Game::Scr_GetString(0); Script::ScriptStorage.insert_or_assign(key, value);
});
Script::AddFunction("StorageRemove", []() // gsc: StorageRemove(<str key>);
{
const auto* key = Game::Scr_GetString(0);
if (key == nullptr)
{
Game::Scr_Error("^1StorageRemove: Illegal parameter!\n");
return;
}
if (!Script::ScriptStorage.count(key)) if (!Script::ScriptStorage.count(key))
{ {
Game::Scr_Error(Utils::String::VA("^1StorageRemove: Store does not have key '%s'!\n", key.c_str())); Game::Scr_Error(Utils::String::VA("^1StorageRemove: Store does not have key '%s'!\n", key));
return; return;
} }
Script::ScriptStorage.erase(key); Script::ScriptStorage.erase(key);
}); });
Script::AddFunction("StorageGet", [](Game::scr_entref_t) // gsc: StorageGet(<str key>); Script::AddFunction("StorageGet", []() // gsc: StorageGet(<str key>);
{ {
if (Game::Scr_GetNumParam() != 1u || Game::Scr_GetType(0) != Game::VAR_STRING) const auto* key = Game::Scr_GetString(0);
if (key == nullptr)
{ {
Game::Scr_Error("^1StorageGet: Needs one string parameter!\n"); Game::Scr_Error("^1StorageGet: Illegal parameter!\n");
return; return;
} }
std::string key = Game::Scr_GetString(0);
if (!Script::ScriptStorage.count(key)) if (!Script::ScriptStorage.count(key))
{ {
Game::Scr_Error(Utils::String::VA("^1StorageGet: Store does not have key '%s'!\n", key.c_str())); Game::Scr_Error(Utils::String::VA("^1StorageGet: Store does not have key '%s'!\n", key));
return; return;
} }
auto data = Script::ScriptStorage.at(key); const auto& data = Script::ScriptStorage.at(key);
Game::Scr_AddString(data.c_str()); Game::Scr_AddString(data.data());
}); });
Script::AddFunction("StorageHas", [](Game::scr_entref_t) // gsc: StorageHas(<str key>); Script::AddFunction("StorageHas", []() // gsc: StorageHas(<str key>);
{ {
if (Game::Scr_GetNumParam() != 1u || Game::Scr_GetType(0) != Game::VAR_STRING) const auto* key = Game::Scr_GetString(0);
if (key == nullptr)
{ {
Game::Scr_Error("^1StorageHas: Needs one string parameter!\n"); Game::Scr_Error("^1StorageHas: Illegal parameter!\n");
return; return;
} }
std::string key = Game::Scr_GetString(0); Game::Scr_AddBool(static_cast<int>(Script::ScriptStorage.count(key))); // Until C++17
Game::Scr_AddInt(Script::ScriptStorage.count(key));
}); });
Script::AddFunction("StorageClear", [](Game::scr_entref_t) // gsc: StorageClear(); Script::AddFunction("StorageClear", []() // gsc: StorageClear();
{ {
Script::ScriptStorage.clear(); Script::ScriptStorage.clear();
}); });
// PlayerCmd_AreControlsFrozen GSC function from Black Ops 2
Script::AddMethod("AreControlsFrozen", [](Game::scr_entref_t entref) // Usage: self AreControlsFrozen();
{
const auto* ent = Game::GetPlayerEntity(entref);
Game::Scr_AddBool((ent->client->flags & Game::PLAYER_FLAG_FROZEN) != 0);
});
} }
Script::Script() Script::Script()
@ -645,14 +700,12 @@ namespace Components
Utils::Hook(0x426C2D, Script::StoreScriptBaseProgramNumStub, HOOK_JUMP).install()->quick(); Utils::Hook(0x426C2D, Script::StoreScriptBaseProgramNumStub, HOOK_JUMP).install()->quick();
Utils::Hook(0x42281B, Script::Scr_PrintPrevCodePosStub, HOOK_JUMP).install()->quick(); Utils::Hook(0x42281B, Script::Scr_PrintPrevCodePosStub, HOOK_JUMP).install()->quick();
// enable scr_error printing if in developer Utils::Hook(0x61E3AD, Script::RuntimeError, HOOK_CALL).install()->quick();
Dvar::OnInit([]() Utils::Hook(0x621976, Script::RuntimeError, HOOK_CALL).install()->quick();
{ Utils::Hook(0x62246E, Script::RuntimeError, HOOK_CALL).install()->quick();
int developer = Dvar::Var("developer").get<int>(); // Skip check in GScr_CheckAllowedToSetPersistentData to prevent log spam in RuntimeError.
// On IW5 the function is entirely nullsubbed
if (developer > 0 && Dedicated::IsEnabled()) Utils::Hook::Set<BYTE>(0x5F8DBF, 0xEB);
Utils::Hook::Set<BYTE>(0x48D8C7, 0x75);
});
Utils::Hook(0x612E8D, Script::FunctionError, HOOK_CALL).install()->quick(); Utils::Hook(0x612E8D, Script::FunctionError, HOOK_CALL).install()->quick();
Utils::Hook(0x612EA2, Script::FunctionError, HOOK_CALL).install()->quick(); Utils::Hook(0x612EA2, Script::FunctionError, HOOK_CALL).install()->quick();
@ -661,27 +714,29 @@ namespace Components
Utils::Hook(0x48EFFE, Script::LoadGameType, HOOK_CALL).install()->quick(); Utils::Hook(0x48EFFE, Script::LoadGameType, HOOK_CALL).install()->quick();
Utils::Hook(0x45D44A, Script::LoadGameTypeScript, HOOK_CALL).install()->quick(); Utils::Hook(0x45D44A, Script::LoadGameTypeScript, HOOK_CALL).install()->quick();
Utils::Hook(0x44E736, Script::GetFunctionStub, HOOK_JUMP).install()->quick(); // Scr_GetFunction // Fetch custom functions
Utils::Hook(0x4EC8E5, Script::GetFunctionStub, HOOK_JUMP).install()->quick(); // Scr_GetMethod Utils::Hook(0x44E72E, Script::BuiltIn_GetFunctionStub, HOOK_CALL).install()->quick(); // Scr_GetFunction
Utils::Hook(0x4EC8DD, Script::BuiltIn_GetMethod, HOOK_CALL).install()->quick(); // Scr_GetMethod
Utils::Hook(0x5F41A3, Script::SetExpFogStub, HOOK_CALL).install()->quick(); Utils::Hook(0x5F41A3, Script::SetExpFogStub, HOOK_CALL).install()->quick();
Utils::Hook(0x61E92E, Script::VMExecuteInternalStub, HOOK_JUMP).install()->quick(); Utils::Hook(0x61E92E, Script::VMExecuteInternalStub, HOOK_JUMP).install()->quick();
Utils::Hook::Nop(0x61E933, 1); Utils::Hook::Nop(0x61E933, 1);
Utils::Hook(0x47548B, Script::ScrShutdownSystemStub, HOOK_CALL).install()->quick(); Utils::Hook(0x47548B, Script::ScrShutdownSystemStub, HOOK_CALL).install()->quick(); // G_LoadGame
Utils::Hook(0x4D06BA, Script::ScrShutdownSystemStub, HOOK_CALL).install()->quick(); Utils::Hook(0x4D06BA, Script::ScrShutdownSystemStub, HOOK_CALL).install()->quick(); // G_ShutdownGame
Scheduler::OnFrame([]() Scheduler::OnFrame([]()
{ {
if (!Game::SV_Loaded()) if (!Game::SV_Loaded())
return; return;
int nowMs = Game::Sys_Milliseconds(); const auto nowMs = Game::Sys_Milliseconds();
if (Script::LastFrameTime != -1) if (Script::LastFrameTime != -1)
{ {
int timeTaken = static_cast<int>((nowMs - Script::LastFrameTime) * Dvar::Var("timescale").get<float>()); const auto timeScale = Dvar::Var("timescale").get<float>();
const auto timeTaken = static_cast<int>((nowMs - Script::LastFrameTime) * timeScale);
if (timeTaken >= 500) if (timeTaken >= 500)
Logger::Print(23, "Hitch warning: %i msec frame time\n", timeTaken); Logger::Print(23, "Hitch warning: %i msec frame time\n", timeTaken);
@ -690,10 +745,19 @@ namespace Components
Script::LastFrameTime = nowMs; Script::LastFrameTime = nowMs;
}); });
Script::AddFunction("debugBox", [](Game::scr_entref_t) #ifdef _DEBUG
Script::AddFunction("DebugBox", []()
{ {
MessageBoxA(nullptr, Game::Scr_GetString(0), "DEBUG", 0); const auto* message = Game::Scr_GetString(0);
}, true);
if (message == nullptr)
{
Game::Scr_Error("^1DebugBox: Illegal parameter!\n");
}
MessageBoxA(nullptr, message, "DEBUG", MB_OK);
}, 1);
#endif
Script::AddFunctions(); Script::AddFunctions();

View File

@ -6,36 +6,23 @@ namespace Components
class Script : public Component class Script : public Component
{ {
public: public:
class Function
{
public:
Function(const std::string& _name, Game::scr_function_t _callback, bool _dev) : name(_name), callback(_callback), dev(_dev) {}
const char* getName() const { return this->name.data(); }
bool isDev() const { return this->dev; }
Game::scr_function_t getFunction() const { return this->callback; }
private:
std::string name;
Game::scr_function_t callback;
bool dev;
};
Script(); Script();
~Script(); ~Script();
static int LoadScriptAndLabel(const std::string& script, const std::string& label); static int LoadScriptAndLabel(const std::string& script, const std::string& label);
static void AddFunction(const std::string& name, Game::scr_function_t function, bool isDev = false);
static void AddFunction(const char* name, Game::BuiltinFunction func, int type = 0);
static void AddMethod(const char* name, Game::BuiltinMethod func, int type = 0);
static void OnVMShutdown(Utils::Slot<Scheduler::Callback> callback); static void OnVMShutdown(Utils::Slot<Scheduler::Callback> callback);
static Game::gentity_t* getEntFromEntRef(Game::scr_entref_t entref); static Game::client_t* GetClient(const Game::gentity_t* gentity);
static Game::client_t* getClientFromEnt(Game::gentity_t* gentity);
private: private:
static std::string ScriptName; static std::string ScriptName;
static std::vector<int> ScriptHandles; static std::vector<int> ScriptHandles;
static std::vector<Function> ScriptFunctions; static std::unordered_map<std::string, Game::BuiltinFunctionDef> CustomScrFunctions;
static std::unordered_map<std::string, Game::BuiltinMethodDef> CustomScrMethods;
static std::vector<std::string> ScriptNameStack; static std::vector<std::string> ScriptNameStack;
static unsigned short FunctionName; static unsigned short FunctionName;
static std::unordered_map<std::string, std::string> ScriptStorage; static std::unordered_map<std::string, std::string> ScriptStorage;
@ -51,6 +38,7 @@ namespace Components
static void FunctionError(); static void FunctionError();
static void StoreFunctionNameStub(); static void StoreFunctionNameStub();
static void RuntimeError(const char* codePos, unsigned int index, const char* msg, const char* dialogMessage);
static void StoreScriptName(const char* name); static void StoreScriptName(const char* name);
static void StoreScriptNameStub(); static void StoreScriptNameStub();
@ -61,10 +49,10 @@ namespace Components
static void LoadGameType(); static void LoadGameType();
static void LoadGameTypeScript(); static void LoadGameTypeScript();
static Game::scr_function_t GetFunction(void* caller, const char** name, int* isDev); static Game::BuiltinFunction BuiltIn_GetFunctionStub(const char** pName, int* type);
static void GetFunctionStub(); static Game::BuiltinMethod BuiltIn_GetMethod(const char** pName, int* type);
static void ScrShutdownSystemStub(int); static void ScrShutdownSystemStub(unsigned char sys);
static void StoreScriptBaseProgramNumStub(); static void StoreScriptBaseProgramNumStub();
static void StoreScriptBaseProgramNum(); static void StoreScriptBaseProgramNum();
static void Scr_PrintPrevCodePosStub(); static void Scr_PrintPrevCodePosStub();

View File

@ -0,0 +1,401 @@
#include <STDInclude.hpp>
namespace Components
{
const char* ScriptExtension::QueryStrings[] = { R"(..)", R"(../)", R"(..\)" };
std::unordered_map<std::uint16_t, Game::ent_field_t> ScriptExtension::CustomEntityFields;
std::unordered_map<std::uint16_t, Game::client_fields_s> ScriptExtension::CustomClientFields;
void ScriptExtension::AddEntityField(const char* name, Game::fieldtype_t type,
const Game::ScriptCallbackEnt& setter, const Game::ScriptCallbackEnt& getter)
{
static std::uint16_t fieldOffsetStart = 15; // fields count
assert((fieldOffsetStart & Game::ENTFIELD_MASK) == Game::ENTFIELD_ENTITY);
ScriptExtension::CustomEntityFields[fieldOffsetStart] = {name, fieldOffsetStart, type, setter, getter};
++fieldOffsetStart;
}
void ScriptExtension::AddClientField(const char* name, Game::fieldtype_t type,
const Game::ScriptCallbackClient& setter, const Game::ScriptCallbackClient& getter)
{
static std::uint16_t fieldOffsetStart = 21; // fields count
assert((fieldOffsetStart & Game::ENTFIELD_MASK) == Game::ENTFIELD_ENTITY);
const auto offset = fieldOffsetStart | Game::ENTFIELD_CLIENT; // This is how client field's offset is calculated
// Use 'index' in 'array' as map key. It will be used later in Scr_SetObjectFieldStub
ScriptExtension::CustomClientFields[fieldOffsetStart] = {name, offset, type, setter, getter};
++fieldOffsetStart;
}
void ScriptExtension::GScr_AddFieldsForEntityStub()
{
for (const auto& [offset, field] : ScriptExtension::CustomEntityFields)
{
Game::Scr_AddClassField(Game::ClassNum::CLASS_NUM_ENTITY, field.name, field.ofs);
}
Utils::Hook::Call<void()>(0x4A7CF0)(); // GScr_AddFieldsForClient
for (const auto& [offset, field] : ScriptExtension::CustomClientFields)
{
Game::Scr_AddClassField(Game::ClassNum::CLASS_NUM_ENTITY, field.name, field.ofs);
}
}
// Because some functions are inlined we have to hook this function instead of Scr_SetEntityField
int ScriptExtension::Scr_SetObjectFieldStub(unsigned int classnum, int entnum, int offset)
{
if (classnum == Game::ClassNum::CLASS_NUM_ENTITY)
{
const auto entity_offset = static_cast<std::uint16_t>(offset);
const auto got = ScriptExtension::CustomEntityFields.find(entity_offset);
if (got != ScriptExtension::CustomEntityFields.end())
{
got->second.setter(&Game::g_entities[entnum], offset);
return 1;
}
}
// No custom generic field was found, let the game handle it
return Game::Scr_SetObjectField(classnum, entnum, offset);
}
// Offset was already converted to array 'index' following binop offset & ~Game::ENTFIELD_MASK
void ScriptExtension::Scr_SetClientFieldStub(Game::gclient_s* client, int offset)
{
const auto client_offset = static_cast<std::uint16_t>(offset);
const auto got = ScriptExtension::CustomClientFields.find(client_offset);
if (got != ScriptExtension::CustomClientFields.end())
{
got->second.setter(client, &got->second);
return;
}
// No custom field client was found, let the game handle it
Game::Scr_SetClientField(client, offset);
}
void ScriptExtension::Scr_GetEntityFieldStub(int entnum, int offset)
{
if ((offset & Game::ENTFIELD_MASK) == Game::ENTFIELD_CLIENT)
{
// If we have a ENTFIELD_CLIENT offset we need to check g_entity is actually a fully connected client
if (Game::g_entities[entnum].client != nullptr)
{
const auto client_offset = static_cast<std::uint16_t>(offset & ~Game::ENTFIELD_MASK);
const auto got = ScriptExtension::CustomClientFields.find(client_offset);
if (got != ScriptExtension::CustomClientFields.end())
{
// Game functions probably don't ever need to use the reference to client_fields_s...
got->second.getter(Game::g_entities[entnum].client, &got->second);
return;
}
}
}
// Regular entity offsets can be searched directly in our custom handler
const auto entity_offset = static_cast<std::uint16_t>(offset);
const auto got = ScriptExtension::CustomEntityFields.find(entity_offset);
if (got != ScriptExtension::CustomEntityFields.end())
{
got->second.getter(&Game::g_entities[entnum], offset);
return;
}
// No custom generic field was found, let the game handle it
Game::Scr_GetEntityField(entnum, offset);
}
void ScriptExtension::AddFunctions()
{
// File functions
Script::AddFunction("FileWrite", []() // gsc: FileWrite(<filepath>, <string>, <mode>)
{
const auto* path = Game::Scr_GetString(0);
auto* text = Game::Scr_GetString(1);
auto* mode = Game::Scr_GetString(2);
if (path == nullptr)
{
Game::Scr_ParamError(0, "^1FileWrite: filepath is not defined!\n");
return;
}
if (text == nullptr || mode == nullptr)
{
Game::Scr_Error("^1FileWrite: Illegal parameters!\n");
return;
}
for (auto i = 0u; i < ARRAYSIZE(ScriptExtension::QueryStrings); ++i)
{
if (std::strstr(path, ScriptExtension::QueryStrings[i]) != nullptr)
{
Logger::Print("^1FileWrite: directory traversal is not allowed!\n");
return;
}
}
if (mode != "append"s && mode != "write"s)
{
Logger::Print("^3FileWrite: mode not defined or was wrong, defaulting to 'write'\n");
mode = "write";
}
if (mode == "write"s)
{
FileSystem::FileWriter(path).write(text);
}
else if (mode == "append"s)
{
FileSystem::FileWriter(path, true).write(text);
}
});
Script::AddFunction("FileRead", []() // gsc: FileRead(<filepath>)
{
const auto* path = Game::Scr_GetString(0);
if (path == nullptr)
{
Game::Scr_ParamError(0, "^1FileRead: filepath is not defined!\n");
return;
}
for (auto i = 0u; i < ARRAYSIZE(ScriptExtension::QueryStrings); ++i)
{
if (std::strstr(path, ScriptExtension::QueryStrings[i]) != nullptr)
{
Logger::Print("^1FileRead: directory traversal is not allowed!\n");
return;
}
}
if (!FileSystem::FileReader(path).exists())
{
Logger::Print("^1FileRead: file not found!\n");
return;
}
Game::Scr_AddString(FileSystem::FileReader(path).getBuffer().data());
});
Script::AddFunction("FileExists", []() // gsc: FileExists(<filepath>)
{
const auto* path = Game::Scr_GetString(0);
if (path == nullptr)
{
Game::Scr_ParamError(0, "^1FileExists: filepath is not defined!\n");
return;
}
for (auto i = 0u; i < ARRAYSIZE(ScriptExtension::QueryStrings); ++i)
{
if (std::strstr(path, ScriptExtension::QueryStrings[i]) != nullptr)
{
Logger::Print("^1FileExists: directory traversal is not allowed!\n");
return;
}
}
Game::Scr_AddInt(FileSystem::FileReader(path).exists());
});
Script::AddFunction("FileRemove", []() // gsc: FileRemove(<filepath>)
{
const auto* path = Game::Scr_GetString(0);
if (path == nullptr)
{
Game::Scr_ParamError(0, "^1FileRemove: filepath is not defined!\n");
return;
}
for (auto i = 0u; i < ARRAYSIZE(ScriptExtension::QueryStrings); ++i)
{
if (std::strstr(path, ScriptExtension::QueryStrings[i]) != nullptr)
{
Logger::Print("^1FileRemove: directory traversal is not allowed!\n");
return;
}
}
const auto p = std::filesystem::path(path);
const auto& folder = p.parent_path().string();
const auto& file = p.filename().string();
Game::Scr_AddInt(FileSystem::DeleteFile(folder, file));
});
// Misc functions
Script::AddFunction("ToUpper", []() // gsc: ToUpper(<string>)
{
const auto scriptValue = Game::Scr_GetConstString(0);
const auto* string = Game::SL_ConvertToString(scriptValue);
char out[1024] = {0}; // 1024 is the max for a string in this SL system
bool changed = false;
size_t i = 0;
while (i < sizeof(out))
{
const auto value = *string;
const auto result = static_cast<char>(std::toupper(static_cast<unsigned char>(value)));
out[i] = result;
if (value != result)
changed = true;
if (result == '\0') // Finished converting string
break;
++string;
++i;
}
// Null terminating character was overwritten
if (i >= sizeof(out))
{
Game::Scr_Error("string too long");
return;
}
if (changed)
{
Game::Scr_AddString(out);
}
else
{
Game::SL_AddRefToString(scriptValue);
Game::Scr_AddConstString(scriptValue);
Game::SL_RemoveRefToString(scriptValue);
}
});
// Func present on IW5
Script::AddFunction("StrICmp", []() // gsc: StrICmp(<string>, <string>)
{
const auto value1 = Game::Scr_GetConstString(0);
const auto value2 = Game::Scr_GetConstString(1);
const auto result = _stricmp(Game::SL_ConvertToString(value1),
Game::SL_ConvertToString(value2));
Game::Scr_AddInt(result);
});
// Func present on IW5
Script::AddFunction("IsEndStr", []() // gsc: IsEndStr(<string>, <string>)
{
const auto* s1 = Game::Scr_GetString(0);
const auto* s2 = Game::Scr_GetString(1);
if (s1 == nullptr || s2 == nullptr)
{
Game::Scr_Error("^1IsEndStr: Illegal parameters!\n");
return;
}
Game::Scr_AddBool(Utils::String::EndsWith(s1, s2));
});
}
void ScriptExtension::AddMethods()
{
// ScriptExtension methods
Script::AddMethod("GetIp", [](Game::scr_entref_t entref) // gsc: self GetIp()
{
const auto* ent = Game::GetPlayerEntity(entref);
const auto* client = Script::GetClient(ent);
std::string ip = Game::NET_AdrToString(client->netchan.remoteAddress);
if (const auto pos = ip.find_first_of(":"); pos != std::string::npos)
ip.erase(ip.begin() + pos, ip.end()); // Erase port
Game::Scr_AddString(ip.data());
});
Script::AddMethod("GetPing", [](Game::scr_entref_t entref) // gsc: self GetPing()
{
const auto* ent = Game::GetPlayerEntity(entref);
const auto* client = Script::GetClient(ent);
Game::Scr_AddInt(client->ping);
});
}
void ScriptExtension::Scr_TableLookupIStringByRow()
{
if (Game::Scr_GetNumParam() < 3)
{
Game::Scr_Error("USAGE: tableLookupIStringByRow( filename, rowNum, returnValueColumnNum )\n");
return;
}
const auto* fileName = Game::Scr_GetString(0);
const auto rowNum = Game::Scr_GetInt(1);
const auto returnValueColumnNum = Game::Scr_GetInt(2);
const auto* table = Game::DB_FindXAssetHeader(Game::ASSET_TYPE_STRINGTABLE, fileName).stringTable;
if (table == nullptr)
{
Game::Scr_ParamError(0, Utils::String::VA("%s does not exist\n", fileName));
return;
}
const auto* value = Game::StringTable_GetColumnValueForRow(table, rowNum, returnValueColumnNum);
Game::Scr_AddIString(value);
}
void ScriptExtension::AddEntityFields()
{
ScriptExtension::AddEntityField("entityflags", Game::fieldtype_t::F_INT,
[](Game::gentity_s* ent, [[maybe_unused]] int offset)
{
ent->flags = Game::Scr_GetInt(0);
},
[](Game::gentity_s* ent, [[maybe_unused]] int offset)
{
Game::Scr_AddInt(ent->flags);
});
}
void ScriptExtension::AddClientFields()
{
ScriptExtension::AddClientField("clientflags", Game::fieldtype_t::F_INT,
[](Game::gclient_s* pSelf, [[maybe_unused]] const Game::client_fields_s* pField)
{
pSelf->flags = Game::Scr_GetInt(0);
},
[](Game::gclient_s* pSelf, [[maybe_unused]] const Game::client_fields_s* pField)
{
Game::Scr_AddInt(pSelf->flags);
});
}
ScriptExtension::ScriptExtension()
{
ScriptExtension::AddFunctions();
ScriptExtension::AddMethods();
ScriptExtension::AddEntityFields();
ScriptExtension::AddClientFields();
// Correct builtin function pointer
Utils::Hook::Set<void(*)()>(0x79A90C, ScriptExtension::Scr_TableLookupIStringByRow);
Utils::Hook(0x4EC721, ScriptExtension::GScr_AddFieldsForEntityStub, HOOK_CALL).install()->quick(); // GScr_AddFieldsForEntity
Utils::Hook(0x41BED2, ScriptExtension::Scr_SetObjectFieldStub, HOOK_CALL).install()->quick(); // SetEntityFieldValue
Utils::Hook(0x5FBF01, ScriptExtension::Scr_SetClientFieldStub, HOOK_CALL).install()->quick(); // Scr_SetObjectField
Utils::Hook(0x4FF413, ScriptExtension::Scr_GetEntityFieldStub, HOOK_CALL).install()->quick(); // Scr_GetObjectField
}
}

View File

@ -0,0 +1,34 @@
#pragma once
namespace Components
{
class ScriptExtension : public Component
{
public:
ScriptExtension();
static void AddEntityField(const char* name, Game::fieldtype_t type, const Game::ScriptCallbackEnt& setter, const Game::ScriptCallbackEnt& getter);
static void AddClientField(const char* name, Game::fieldtype_t type, const Game::ScriptCallbackClient& setter, const Game::ScriptCallbackClient& getter);
private:
static const char* QueryStrings[];
static std::unordered_map<std::uint16_t, Game::ent_field_t> CustomEntityFields;
static std::unordered_map<std::uint16_t, Game::client_fields_s> CustomClientFields;
static void GScr_AddFieldsForEntityStub();
// Two hooks because it makes our code cleaner (luckily functions were not inlined)
static int Scr_SetObjectFieldStub(unsigned int classnum, int entnum, int offset);
static void Scr_SetClientFieldStub(Game::gclient_s* client, int offset);
// One hook because functions were inlined
static void Scr_GetEntityFieldStub(int entnum, int offset);
static void AddFunctions();
static void AddMethods();
static void AddEntityFields();
static void AddClientFields();
static void Scr_TableLookupIStringByRow();
};
}

View File

@ -0,0 +1,112 @@
#include <STDInclude.hpp>
namespace Components
{
int Security::MsgReadBitsCompressCheckSV(const char* from, char* to, int size)
{
static char buffer[0x8000];
if (size > 0x800) return 0;
size = Game::MSG_ReadBitsCompress(from, buffer, size);
if (size > 0x800) return 0;
std::memcpy(to, buffer, size);
return size;
}
int Security::MsgReadBitsCompressCheckCL(const char* from, char* to, int size)
{
static char buffer[0x100000];
if (size > 0x20000) return 0;
size = Game::MSG_ReadBitsCompress(from, buffer, size);
if (size > 0x20000) return 0;
std::memcpy(to, buffer, size);
return size;
}
int Security::SVCanReplaceServerCommand(Game::client_t* /*client*/, const char* /*cmd*/)
{
// This is a fix copied from V2. As I don't have time to investigate, let's simply trust them
return -1;
}
long Security::AtolAdjustPlayerLimit(const char* string)
{
return std::min<long>(std::atol(string), 18);
}
void Security::SelectStringTableEntryInDvarStub()
{
Command::ClientParams params;
if (params.size() >= 4)
{
const auto* dvarName = params[3];
const auto* dvar = Game::Dvar_FindVar(dvarName);
if (Command::Find(dvarName) ||
(dvar != nullptr && dvar->flags & (Game::DVAR_WRITEPROTECTED | Game::DVAR_CHEAT | Game::DVAR_READONLY)))
{
Logger::Print(0, "CL_SelectStringTableEntryInDvar_f: illegal parameter\n");
return;
}
}
Game::CL_SelectStringTableEntryInDvar_f();
}
__declspec(naked) int Security::G_GetClientScore()
{
__asm
{
mov eax, [esp + 4] // index
mov ecx, ds:1A831A8h // level: &g_clients
test ecx, ecx
jz invalid_ptr
imul eax, 366Ch
mov eax, [eax + ecx + 3134h]
ret
invalid_ptr:
xor eax, eax
ret
}
}
void Security::G_LogPrintfStub(const char* fmt)
{
Game::G_LogPrintf("%s", fmt);
}
Security::Security()
{
// Exploit fixes
Utils::Hook(0x414D92, MsgReadBitsCompressCheckSV, HOOK_CALL).install()->quick(); // SV_ExecuteClientCommands
Utils::Hook(0x4A9F56, MsgReadBitsCompressCheckCL, HOOK_CALL).install()->quick(); // CL_ParseServerMessage
Utils::Hook(0x407376, SVCanReplaceServerCommand, HOOK_CALL).install()->quick(); // SV_CanReplaceServerCommand
Utils::Hook::Set<BYTE>(0x412370, 0xC3); // SV_SteamAuthClient
Utils::Hook::Set<BYTE>(0x5A8C70, 0xC3); // CL_HandleRelayPacket
Utils::Hook::Nop(0x41698E, 5); // Disable Svcmd_EntityList_f
// Patch selectStringTableEntryInDvar
Utils::Hook::Set<void(*)()>(0x405959, Security::SelectStringTableEntryInDvarStub);
// Patch G_GetClientScore for uninitialized game
Utils::Hook(0x469AC0, G_GetClientScore, HOOK_JUMP).install()->quick();
// Requests can be malicious
Utils::Hook(0x5B67ED, AtolAdjustPlayerLimit, HOOK_CALL).install()->quick(); // PartyHost_HandleJoinPartyRequest
// Patch unsecure call to G_LogPrint inside GScr_LogPrint
// This function is unsafe because IW devs forgot to G_LogPrintf("%s", fmt)
Utils::Hook(0x5F70B5, G_LogPrintfStub, HOOK_CALL).install()->quick();
}
}

View File

@ -0,0 +1,24 @@
#pragma once
namespace Components
{
class Security : public Component
{
public:
Security();
private:
static int MsgReadBitsCompressCheckSV(const char* from, char* to, int size);
static int MsgReadBitsCompressCheckCL(const char* from, char* to, int size);
static int SVCanReplaceServerCommand(Game::client_t* client, const char* cmd);
static long AtolAdjustPlayerLimit(const char* string);
static void SelectStringTableEntryInDvarStub();
static int G_GetClientScore();
static void G_LogPrintfStub(const char* fmt);
};
}

View File

@ -2,24 +2,24 @@
namespace Components namespace Components
{ {
std::unordered_map<std::int32_t, Utils::Slot<bool(Command::Params*)>> ServerCommands::Commands; std::unordered_map<std::int32_t, std::function<bool(Command::Params*)>> ServerCommands::Commands;
void ServerCommands::OnCommand(std::int32_t cmd, Utils::Slot<bool(Command::Params*)> cb) void ServerCommands::OnCommand(std::int32_t cmd, std::function<bool(Command::Params*)> callback)
{ {
ServerCommands::Commands[cmd] = cb; ServerCommands::Commands.insert_or_assign(cmd, std::move(callback));
} }
bool ServerCommands::OnServerCommand() bool ServerCommands::OnServerCommand()
{ {
Command::ClientParams params; Command::ClientParams params;
for (const auto& serverCommandCB : ServerCommands::Commands) for (const auto& [id, callback] : ServerCommands::Commands)
{ {
if (params.size() >= 1) if (params.size() >= 1)
{ {
if (params.get(0)[0] == serverCommandCB.first) if (params.get(0)[0] == id) // Compare ID of server command
{ {
return serverCommandCB.second(&params); return callback(&params);
} }
} }
} }
@ -27,7 +27,7 @@ namespace Components
return false; return false;
} }
__declspec(naked) void ServerCommands::OnServerCommandStub() __declspec(naked) void ServerCommands::CG_DeployServerCommand_Stub()
{ {
__asm __asm
{ {
@ -44,7 +44,7 @@ namespace Components
test eax, eax test eax, eax
jle error jle error
mov eax, DWORD PTR[edx * 4 + 1AAC634h] mov eax, dword ptr [edx * 4 + 1AAC634h]
mov eax, [eax] mov eax, [eax]
push 5944B3h push 5944B3h
@ -63,6 +63,6 @@ namespace Components
ServerCommands::ServerCommands() ServerCommands::ServerCommands()
{ {
// Server command receive hook // Server command receive hook
Utils::Hook(0x59449F, ServerCommands::OnServerCommandStub).install()->quick(); Utils::Hook(0x59449F, ServerCommands::CG_DeployServerCommand_Stub).install()->quick();
} }
} }

View File

@ -7,12 +7,12 @@ namespace Components
public: public:
ServerCommands(); ServerCommands();
static void OnCommand(std::int32_t cmd, Utils::Slot<bool(Command::Params*)> cb); static void OnCommand(std::int32_t cmd, std::function<bool(Command::Params*)> callback);
private: private:
static std::unordered_map<std::int32_t, Utils::Slot<bool(Command::Params*)>> Commands; static std::unordered_map<std::int32_t, std::function<bool(Command::Params*)>> Commands;
static bool OnServerCommand(); static bool OnServerCommand();
static void OnServerCommandStub(); static void CG_DeployServerCommand_Stub();
}; };
} }

View File

@ -4,6 +4,9 @@ namespace Components
{ {
ServerInfo::Container ServerInfo::PlayerContainer; ServerInfo::Container ServerInfo::PlayerContainer;
Game::dvar_t** ServerInfo::CGScoreboardHeight;
Game::dvar_t** ServerInfo::CGScoreboardWidth;
unsigned int ServerInfo::GetPlayerCount() unsigned int ServerInfo::GetPlayerCount()
{ {
return ServerInfo::PlayerContainer.playerList.size(); return ServerInfo::PlayerContainer.playerList.size();
@ -74,22 +77,24 @@ namespace Components
void ServerInfo::DrawScoreboardInfo(int localClientNum) void ServerInfo::DrawScoreboardInfo(int localClientNum)
{ {
Game::Font_s* font = Game::R_RegisterFont("fonts/bigfont", 0); Game::Font_s* font = Game::R_RegisterFont("fonts/bigfont", 0);
void* cxt = Game::ScrPlace_GetActivePlacement(localClientNum); const auto* cxt = Game::ScrPlace_GetActivePlacement(localClientNum);
std::string addressText = Network::Address(*Game::connectedHost).getString(); auto addressText = Network::Address(*Game::connectedHost).getString();
if (addressText == "0.0.0.0:0" || addressText == "loopback") addressText = "Listen Server";
// get x positions if (addressText == "0.0.0.0:0" || addressText == "loopback")
float fontSize = 0.35f; addressText = "Listen Server";
float y = (480.0f - Dvar::Var("cg_scoreboardHeight").get<float>()) * 0.5f;
y += Dvar::Var("cg_scoreboardHeight").get<float>() + 6.0f;
float x = 320.0f - Dvar::Var("cg_scoreboardWidth").get<float>() * 0.5f; // Get x positions
float x2 = 320.0f + Dvar::Var("cg_scoreboardWidth").get<float>() * 0.5f; auto y = (480.0f - (*ServerInfo::CGScoreboardHeight)->current.value) * 0.5f;
y += (*ServerInfo::CGScoreboardHeight)->current.value + 6.0f;
// draw only when stream friendly ui is not enabled const auto x = 320.0f - (*ServerInfo::CGScoreboardWidth)->current.value * 0.5f;
if (!Dvar::Var("ui_streamFriendly").get<bool>()) const auto x2 = 320.0f + (*ServerInfo::CGScoreboardWidth)->current.value * 0.5f;
// Draw only when stream friendly ui is not enabled
if (!Friends::UIStreamFriendly.get<bool>())
{ {
constexpr auto fontSize = 0.35f;
Game::UI_DrawText(cxt, reinterpret_cast<const char*>(0x7ED3F8), 0x7FFFFFFF, font, x, y, 0, 0, fontSize, reinterpret_cast<float*>(0x747F34), 3); Game::UI_DrawText(cxt, reinterpret_cast<const char*>(0x7ED3F8), 0x7FFFFFFF, font, x, y, 0, 0, fontSize, reinterpret_cast<float*>(0x747F34), 3);
Game::UI_DrawText(cxt, addressText.data(), 0x7FFFFFFF, font, x2 - Game::UI_TextWidth(addressText.data(), 0, font, fontSize), y, 0, 0, fontSize, reinterpret_cast<float*>(0x747F34), 3); Game::UI_DrawText(cxt, addressText.data(), 0x7FFFFFFF, font, x2 - Game::UI_TextWidth(addressText.data(), 0, font, fontSize), y, 0, 0, fontSize, reinterpret_cast<float*>(0x747F34), 3);
} }
@ -125,7 +130,7 @@ namespace Components
Utils::InfoString ServerInfo::GetInfo() Utils::InfoString ServerInfo::GetInfo()
{ {
int maxclientCount = *Game::svs_numclients; int maxclientCount = *Game::svs_clientCount;
if (!maxclientCount) if (!maxclientCount)
{ {
@ -172,7 +177,9 @@ namespace Components
ServerInfo::ServerInfo() ServerInfo::ServerInfo()
{ {
ServerInfo::PlayerContainer.currentPlayer = 0; ServerInfo::PlayerContainer.currentPlayer = 0;
ServerInfo::PlayerContainer.playerList.clear();
ServerInfo::CGScoreboardHeight = reinterpret_cast<Game::dvar_t**>(0x9FD070);
ServerInfo::CGScoreboardWidth = reinterpret_cast<Game::dvar_t**>(0x9FD0AC);
// Draw IP and hostname on the scoreboard // Draw IP and hostname on the scoreboard
Utils::Hook(0x4FC6EA, ServerInfo::DrawScoreboardStub, HOOK_CALL).install()->quick(); Utils::Hook(0x4FC6EA, ServerInfo::DrawScoreboardStub, HOOK_CALL).install()->quick();

View File

@ -28,6 +28,9 @@ namespace Components
Network::Address target; Network::Address target;
}; };
static Game::dvar_t** CGScoreboardHeight;
static Game::dvar_t** CGScoreboardWidth;
static Container PlayerContainer; static Container PlayerContainer;
static void ServerStatus(UIScript::Token); static void ServerStatus(UIScript::Token);

View File

@ -14,6 +14,13 @@ namespace Components
std::vector<unsigned int> ServerList::VisibleList; std::vector<unsigned int> ServerList::VisibleList;
Dvar::Var ServerList::UIServerSelected;
Dvar::Var ServerList::UIServerSelectedMap;
Dvar::Var ServerList::NETServerQueryLimit;
Dvar::Var ServerList::NETServerFrames;
bool ServerList::useMasterServer = true;
std::vector<ServerList::ServerInfo>* ServerList::GetList() std::vector<ServerList::ServerInfo>* ServerList::GetList()
{ {
if (ServerList::IsOnlineList()) if (ServerList::IsOnlineList())
@ -154,13 +161,13 @@ namespace Components
if (info) if (info)
{ {
Dvar::Var("ui_serverSelected").set(true); ServerList::UIServerSelected.set(true);
Dvar::Var("ui_serverSelectedMap").set(info->mapname); ServerList::UIServerSelectedMap.set(info->mapname);
Dvar::Var("ui_serverSelectedGametype").set(info->gametype); Dvar::Var("ui_serverSelectedGametype").set(info->gametype);
} }
else else
{ {
Dvar::Var("ui_serverSelected").set(false); ServerList::UIServerSelected.set(false);
} }
} }
@ -200,13 +207,6 @@ namespace Components
auto list = ServerList::GetList(); auto list = ServerList::GetList();
if (!list) return; if (!list) return;
// Refresh entirely, if there is no entry in the list
if (list->empty())
{
ServerList::Refresh(UIScript::Token());
return;
}
bool ui_browserShowFull = Dvar::Var("ui_browserShowFull").get<bool>(); bool ui_browserShowFull = Dvar::Var("ui_browserShowFull").get<bool>();
bool ui_browserShowEmpty = Dvar::Var("ui_browserShowEmpty").get<bool>(); bool ui_browserShowEmpty = Dvar::Var("ui_browserShowEmpty").get<bool>();
int ui_browserShowHardcore = Dvar::Var("ui_browserKillcam").get<int>(); int ui_browserShowHardcore = Dvar::Var("ui_browserKillcam").get<int>();
@ -269,22 +269,28 @@ namespace Components
} }
else if (ServerList::IsOnlineList()) else if (ServerList::IsOnlineList())
{ {
#ifdef USE_LEGACY_SERVER_LIST const auto masterPort = Dvar::Var("masterPort").get<int>();
const auto masterServerName = Dvar::Var("masterServerName").get<const char*>();
// Check if our dvars can properly convert to a address
Game::netadr_t masterServerAddr;
if (!ServerList::GetMasterServer(masterServerName, masterPort, masterServerAddr))
{
Logger::Print("Could not resolve address for %s:%u", masterServerName, masterPort);
Toast::Show("cardicon_headshot", "^1Error", Utils::String::VA("Could not resolve address for %s:%u", masterServerName, masterPort), 5000);
return;
}
Toast::Show("cardicon_headshot", "Server Browser", "Fetching servers...", 3000);
useMasterServer = true;
ServerList::RefreshContainer.awatingList = true; ServerList::RefreshContainer.awatingList = true;
ServerList::RefreshContainer.awaitTime = Game::Sys_Milliseconds(); ServerList::RefreshContainer.awaitTime = Game::Sys_Milliseconds();
int masterPort = Dvar::Var("masterPort").get<int>();
const char* masterServerName = Dvar::Var("masterServerName").get<const char*>();
ServerList::RefreshContainer.host = Network::Address(Utils::String::VA("%s:%u", masterServerName, masterPort)); ServerList::RefreshContainer.host = Network::Address(Utils::String::VA("%s:%u", masterServerName, masterPort));
Logger::Print("Sending serverlist request to master: %s:%u\n", masterServerName, masterPort); Logger::Print("Sending serverlist request to master\n");
Network::SendCommand(ServerList::RefreshContainer.host, "getservers", Utils::String::VA("IW4 %i full empty", PROTOCOL)); Network::SendCommand(ServerList::RefreshContainer.host, "getservers", Utils::String::VA("IW4 %i full empty", PROTOCOL));
//Network::SendCommand(ServerList::RefreshContainer.Host, "getservers", "0 full empty");
#else
Node::Synchronize();
#endif
} }
else if (ServerList::IsFavouriteList()) else if (ServerList::IsFavouriteList())
{ {
@ -565,8 +571,7 @@ namespace Components
void ServerList::SortList() void ServerList::SortList()
{ {
// Only sort when the serverlist is open // Only sort when the serverlist is open
Game::menuDef_t* menu = Game::Menus_FindByName(Game::uiContext, "pc_join_unranked"); if (!ServerList::IsServerListOpen()) return;
if (!menu || !Game::Menu_IsVisible(Game::uiContext, menu)) return;
std::stable_sort(ServerList::VisibleList.begin(), ServerList::VisibleList.end(), [](const unsigned int &server1, const unsigned int &server2) -> bool std::stable_sort(ServerList::VisibleList.begin(), ServerList::VisibleList.end(), [](const unsigned int &server1, const unsigned int &server2) -> bool
{ {
@ -621,24 +626,37 @@ namespace Components
void ServerList::Frame() void ServerList::Frame()
{ {
static Utils::Time::Interval frameLimit; static Utils::Time::Interval frameLimit;
int interval = static_cast<int>(1000.0f / Dvar::Var("net_serverFrames").get<int>()); const auto interval = static_cast<int>(1000.0f / ServerList::NETServerFrames.get<int>());
if (!frameLimit.elapsed(std::chrono::milliseconds(interval))) return;
if (!frameLimit.elapsed(std::chrono::milliseconds(interval)))
return;
frameLimit.update(); frameLimit.update();
std::lock_guard<std::recursive_mutex> _(ServerList::RefreshContainer.mutex); std::lock_guard<std::recursive_mutex> _(ServerList::RefreshContainer.mutex);
if (ServerList::RefreshContainer.awatingList) if (ServerList::RefreshContainer.awatingList)
{ {
// Check if we haven't got a response within 10 seconds // Stop counting if we are out of the server browser menu
if (!ServerList::IsServerListOpen())
{
ServerList::RefreshContainer.awatingList = false;
}
// Check if we haven't got a response within 5 seconds
if (Game::Sys_Milliseconds() - ServerList::RefreshContainer.awaitTime > 5000) if (Game::Sys_Milliseconds() - ServerList::RefreshContainer.awaitTime > 5000)
{ {
ServerList::RefreshContainer.awatingList = false; ServerList::RefreshContainer.awatingList = false;
Logger::Print("We haven't received a response from the master within %d seconds!\n", (Game::Sys_Milliseconds() - ServerList::RefreshContainer.awaitTime) / 1000); Logger::Print("We haven't received a response from the master within %d seconds!\n", (Game::Sys_Milliseconds() - ServerList::RefreshContainer.awaitTime) / 1000);
Toast::Show("cardicon_headshot", "^1Error", "Failed to reach master server, using node servers instead.", 5000);
useMasterServer = false;
Node::Synchronize();
} }
} }
int requestLimit = Dvar::Var("net_serverQueryLimit").get<int>(); auto requestLimit = ServerList::NETServerQueryLimit.get<int>();
for (unsigned int i = 0; i < ServerList::RefreshContainer.servers.size() && requestLimit > 0; ++i) for (unsigned int i = 0; i < ServerList::RefreshContainer.servers.size() && requestLimit > 0; ++i)
{ {
ServerList::Container::ServerContainer* server = &ServerList::RefreshContainer.servers[i]; ServerList::Container::ServerContainer* server = &ServerList::RefreshContainer.servers[i];
@ -725,6 +743,20 @@ namespace Components
} }
} }
bool ServerList::GetMasterServer(const char* ip, int port, Game::netadr_t& address)
{
return Game::NET_StringToAdr(Utils::String::VA("%s:%u", ip, port), &address);
}
bool ServerList::IsServerListOpen()
{
auto* menu = Game::Menus_FindByName(Game::uiContext, "pc_join_unranked");
if (!menu)
return false;
return Game::Menu_IsVisible(Game::uiContext, menu);
}
ServerList::ServerList() ServerList::ServerList()
{ {
ServerList::OnlineList.clear(); ServerList::OnlineList.clear();
@ -734,11 +766,15 @@ namespace Components
Dvar::OnInit([]() Dvar::OnInit([]()
{ {
Dvar::Register<bool>("ui_serverSelected", false, Game::dvar_flag::DVAR_NONE, "Whether a server has been selected in the serverlist"); ServerList::UIServerSelected = Dvar::Register<bool>("ui_serverSelected", false,
Dvar::Register<const char*>("ui_serverSelectedMap", "mp_afghan", Game::dvar_flag::DVAR_NONE, "Map of the selected server"); Game::dvar_flag::DVAR_NONE, "Whether a server has been selected in the serverlist");
ServerList::UIServerSelectedMap = Dvar::Register<const char*>("ui_serverSelectedMap", "mp_afghan",
Game::dvar_flag::DVAR_NONE, "Map of the selected server");
Dvar::Register<int>("net_serverQueryLimit", 1, 1, 10, Dedicated::IsEnabled() ? Game::dvar_flag::DVAR_NONE : Game::dvar_flag::DVAR_ARCHIVE, "Amount of server queries per frame"); ServerList::NETServerQueryLimit = Dvar::Register<int>("net_serverQueryLimit", 1,
Dvar::Register<int>("net_serverFrames", 30, 1, 60, Dedicated::IsEnabled() ? Game::dvar_flag::DVAR_NONE : Game::dvar_flag::DVAR_ARCHIVE, "Amount of server query frames per second"); 1, 10, Dedicated::IsEnabled() ? Game::dvar_flag::DVAR_NONE : Game::dvar_flag::DVAR_ARCHIVE, "Amount of server queries per frame");
ServerList::NETServerFrames = Dvar::Register<int>("net_serverFrames", 30,
1, 60, Dedicated::IsEnabled() ? Game::dvar_flag::DVAR_NONE : Game::dvar_flag::DVAR_ARCHIVE, "Amount of server query frames per second");
}); });
// Fix ui_netsource dvar // Fix ui_netsource dvar
@ -780,11 +816,9 @@ namespace Components
}); });
// Set default masterServerName + port and save it // Set default masterServerName + port and save it
#ifdef USE_LEGACY_SERVER_LIST Utils::Hook::Set<const char*>(0x60AD92, "master.xlabs.dev");
Utils::Hook::Set<char*>(0x60AD92, "127.0.0.1");
Utils::Hook::Set<BYTE>(0x60AD90, Game::dvar_flag::DVAR_ARCHIVE); // masterServerName Utils::Hook::Set<BYTE>(0x60AD90, Game::dvar_flag::DVAR_ARCHIVE); // masterServerName
Utils::Hook::Set<BYTE>(0x60ADC6, Game::dvar_flag::DVAR_ARCHIVE); // masterPort Utils::Hook::Set<BYTE>(0x60ADC6, Game::dvar_flag::DVAR_ARCHIVE); // masterPort
#endif
// Add server list feeder // Add server list feeder
UIFeeder::Add(2.0f, ServerList::GetServerCount, ServerList::GetServerText, ServerList::SelectServer); UIFeeder::Add(2.0f, ServerList::GetServerCount, ServerList::GetServerText, ServerList::SelectServer);
@ -794,6 +828,7 @@ namespace Components
UIScript::Add("RefreshFilter", ServerList::UpdateVisibleList); UIScript::Add("RefreshFilter", ServerList::UpdateVisibleList);
UIScript::Add("RefreshServers", ServerList::Refresh); UIScript::Add("RefreshServers", ServerList::Refresh);
UIScript::Add("JoinServer", [](UIScript::Token) UIScript::Add("JoinServer", [](UIScript::Token)
{ {
ServerList::ServerInfo* info = ServerList::GetServer(ServerList::CurrentServer); ServerList::ServerInfo* info = ServerList::GetServer(ServerList::CurrentServer);
@ -803,6 +838,7 @@ namespace Components
Party::Connect(info->addr); Party::Connect(info->addr);
} }
}); });
UIScript::Add("ServerSort", [](UIScript::Token token) UIScript::Add("ServerSort", [](UIScript::Token token)
{ {
int key = token.get<int>(); int key = token.get<int>();
@ -820,6 +856,7 @@ namespace Components
Logger::Print("Sorting server list by token: %d\n", ServerList::SortKey); Logger::Print("Sorting server list by token: %d\n", ServerList::SortKey);
ServerList::SortList(); ServerList::SortList();
}); });
UIScript::Add("CreateListFavorite", [](UIScript::Token) UIScript::Add("CreateListFavorite", [](UIScript::Token)
{ {
ServerList::ServerInfo* info = ServerList::GetCurrentServer(); ServerList::ServerInfo* info = ServerList::GetCurrentServer();
@ -829,10 +866,12 @@ namespace Components
ServerList::StoreFavourite(info->addr.getString()); ServerList::StoreFavourite(info->addr.getString());
} }
}); });
UIScript::Add("CreateFavorite", [](UIScript::Token) UIScript::Add("CreateFavorite", [](UIScript::Token)
{ {
ServerList::StoreFavourite(Dvar::Var("ui_favoriteAddress").get<std::string>()); ServerList::StoreFavourite(Dvar::Var("ui_favoriteAddress").get<std::string>());
}); });
UIScript::Add("CreateCurrentServerFavorite", [](UIScript::Token) UIScript::Add("CreateCurrentServerFavorite", [](UIScript::Token)
{ {
if (Game::CL_IsCgameInitialized()) if (Game::CL_IsCgameInitialized())
@ -844,6 +883,7 @@ namespace Components
} }
} }
}); });
UIScript::Add("DeleteFavorite", [](UIScript::Token) UIScript::Add("DeleteFavorite", [](UIScript::Token)
{ {
ServerList::ServerInfo* info = ServerList::GetCurrentServer(); ServerList::ServerInfo* info = ServerList::GetCurrentServer();
@ -854,28 +894,24 @@ namespace Components
}; };
}); });
#ifdef _DEBUG
Command::Add("playerCount", [](Command::Params*) Command::Add("playerCount", [](Command::Params*)
{ {
int count = 0; auto count = 0;
for (auto server : ServerList::OnlineList) for (const auto& server : ServerList::OnlineList)
{ {
count += server.clients; count += server.clients;
} }
Logger::Print("There are %d players playing.\n", count); Logger::Print("There are %d players playing.\n", count);
}); });
#endif
// Add required ownerDraws // Add required ownerDraws
UIScript::AddOwnerDraw(220, ServerList::UpdateSource); UIScript::AddOwnerDraw(220, ServerList::UpdateSource);
UIScript::AddOwnerDraw(253, ServerList::UpdateGameType); UIScript::AddOwnerDraw(253, ServerList::UpdateGameType);
// Add frame callback // Add frame callback
Scheduler::OnFrame(ServerList::Frame); Scheduler::OnFrame(ServerList::Frame);
// This is placed here in case the anticheat has been disabled!
#if !defined(DISABLE_ANTICHEAT) && defined(PROCTECT_PROCESS)
Scheduler::OnFrame(AntiCheat::ReadIntegrityCheck, true);
#endif
} }
ServerList::~ServerList() ServerList::~ServerList()

View File

@ -50,6 +50,9 @@ namespace Components
static void UpdateVisibleInfo(); static void UpdateVisibleInfo();
static bool GetMasterServer(const char* ip, int port, Game::netadr_t& address);
static bool useMasterServer;
private: private:
enum Column enum Column
{ {
@ -138,5 +141,12 @@ namespace Components
static std::vector<ServerInfo> FavouriteList; static std::vector<ServerInfo> FavouriteList;
static std::vector<unsigned int> VisibleList; static std::vector<unsigned int> VisibleList;
static Dvar::Var UIServerSelected;
static Dvar::Var UIServerSelectedMap;
static Dvar::Var NETServerQueryLimit;
static Dvar::Var NETServerFrames;
static bool IsServerListOpen();
}; };
} }

View File

@ -68,7 +68,7 @@ namespace Components
SlowMotion::Delay = delay; SlowMotion::Delay = delay;
// set snapshot num to 1 behind (T6 does this, why shouldn't we?) // set snapshot num to 1 behind (T6 does this, why shouldn't we?)
for (int i = 0; i < *Game::svs_numclients; ++i) for (int i = 0; i < *Game::svs_clientCount; ++i)
{ {
Game::svs_clients[i].snapNum = *reinterpret_cast<DWORD*>(0x31D9384) - 1; Game::svs_clients[i].snapNum = *reinterpret_cast<DWORD*>(0x31D9384) - 1;
} }

View File

@ -1386,6 +1386,7 @@ namespace Components
++current; ++current;
++in; ++in;
} }
*out = '\0'; *out = '\0';
} }

View File

@ -41,7 +41,17 @@ namespace Components
if (params.size() <= 1) if (params.size() <= 1)
return 0; return 0;
int index = atoi(params[1]); char* end;
const auto* input = params.get(1);
auto index = std::strtol(input, &end, 10);
if (input == end)
{
Logger::Print("Warning: %s is not a valid input\n"
"Usage: %s <weapon index>\n",
input, params.get(0));
return 0;
}
if (index >= 4139) if (index >= 4139)
{ {
@ -56,7 +66,7 @@ namespace Components
return 0; return 0;
} }
Utils::Hook::Call<void(int, int)>(0x4BD520)(0, index); Game::CG_SetupWeaponDef(0, index);
return 1; return 1;
} }

View File

@ -7,6 +7,8 @@ namespace Components
HWND Window::MainWindow = nullptr; HWND Window::MainWindow = nullptr;
BOOL Window::CursorVisible = TRUE; BOOL Window::CursorVisible = TRUE;
std::unordered_map<UINT, Utils::Slot<Window::WndProcCallback>> Window::WndMessageCallbacks;
Utils::Signal<Window::CreateCallback> Window::CreateSignals;
int Window::Width() int Window::Width()
{ {
@ -66,6 +68,16 @@ namespace Components
return Window::MainWindow; return Window::MainWindow;
} }
void Window::OnWndMessage(UINT Msg, Utils::Slot<Window::WndProcCallback> callback)
{
WndMessageCallbacks.emplace(Msg, callback);
}
void Window::OnCreate(Utils::Slot<CreateCallback> callback)
{
CreateSignals.connect(callback);
}
int Window::IsNoBorder() int Window::IsNoBorder()
{ {
return Window::NoBorder.get<bool>(); return Window::NoBorder.get<bool>();
@ -121,6 +133,9 @@ namespace Components
HWND WINAPI Window::CreateMainWindow(DWORD dwExStyle, LPCSTR lpClassName, LPCSTR lpWindowName, DWORD dwStyle, int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam) HWND WINAPI Window::CreateMainWindow(DWORD dwExStyle, LPCSTR lpClassName, LPCSTR lpWindowName, DWORD dwStyle, int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)
{ {
Window::MainWindow = CreateWindowExA(dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam); Window::MainWindow = CreateWindowExA(dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
CreateSignals();
return Window::MainWindow; return Window::MainWindow;
} }
@ -132,15 +147,21 @@ namespace Components
BOOL WINAPI Window::MessageHandler(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) BOOL WINAPI Window::MessageHandler(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{ {
if (Msg == WM_SETCURSOR) if (const auto cb = WndMessageCallbacks.find(Msg); cb != WndMessageCallbacks.end())
{ {
Window::ApplyCursor(); return cb->second(lParam, wParam);
return TRUE;
} }
return Utils::Hook::Call<BOOL(__stdcall)(HWND, UINT, WPARAM, LPARAM)>(0x4731F0)(hWnd, Msg, wParam, lParam); return Utils::Hook::Call<BOOL(__stdcall)(HWND, UINT, WPARAM, LPARAM)>(0x4731F0)(hWnd, Msg, wParam, lParam);
} }
void Window::EnableDpiAwareness()
{
const Utils::Library user32{"user32.dll"};
user32.invokePascal<void>("SetProcessDpiAwarenessContext", DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
}
Window::Window() Window::Window()
{ {
// Borderless window // Borderless window
@ -184,5 +205,13 @@ namespace Components
// Use custom message handler // Use custom message handler
Utils::Hook::Set(0x64D298, Window::MessageHandler); Utils::Hook::Set(0x64D298, Window::MessageHandler);
Window::OnWndMessage(WM_SETCURSOR, [](WPARAM, LPARAM)
{
Window::ApplyCursor();
return TRUE;
});
Window::EnableDpiAwareness();
} }
} }

View File

@ -5,6 +5,9 @@ namespace Components
class Window : public Component class Window : public Component
{ {
public: public:
typedef BOOL(WndProcCallback)(WPARAM wParam, LPARAM lParam);
typedef void(CreateCallback)();
Window(); Window();
static int Width(); static int Width();
@ -18,10 +21,15 @@ namespace Components
static HWND GetWindow(); static HWND GetWindow();
static void OnWndMessage(UINT Msg, Utils::Slot<WndProcCallback> callback);
static void OnCreate(Utils::Slot<CreateCallback> callback);
private: private:
static BOOL CursorVisible; static BOOL CursorVisible;
static Dvar::Var NoBorder; static Dvar::Var NoBorder;
static Dvar::Var NativeCursor; static Dvar::Var NativeCursor;
static std::unordered_map<UINT, Utils::Slot<WndProcCallback>> WndMessageCallbacks;
static Utils::Signal<CreateCallback> CreateSignals;
static HWND MainWindow; static HWND MainWindow;
@ -36,5 +44,7 @@ namespace Components
static void StyleHookStub(); static void StyleHookStub();
static HWND WINAPI CreateMainWindow(DWORD dwExStyle, LPCSTR lpClassName, LPCSTR lpWindowName, DWORD dwStyle, int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam); static HWND WINAPI CreateMainWindow(DWORD dwExStyle, LPCSTR lpClassName, LPCSTR lpWindowName, DWORD dwStyle, int X, int Y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam);
static void EnableDpiAwareness();
}; };
} }

View File

@ -5,19 +5,19 @@ namespace Game
std::vector<std::string> Sys_ListFilesWrapper(const std::string& directory, const std::string& extension) std::vector<std::string> Sys_ListFilesWrapper(const std::string& directory, const std::string& extension)
{ {
auto fileCount = 0; auto fileCount = 0;
auto files = Game::Sys_ListFiles(directory.data(), extension.data(), 0, &fileCount, 0); auto** const files = Sys_ListFiles(directory.data(), extension.data(), nullptr, &fileCount, 0);
std::vector<std::string> result; std::vector<std::string> result;
for (auto i = 0; i < fileCount; i++) for (auto i = 0; i < fileCount; i++)
{ {
if (files[i]) if (files[i] != nullptr)
{ {
result.push_back(files[i]); result.emplace_back(files[i]);
} }
} }
Game::FS_FreeFileList(files); FS_FreeFileList(files);
return result; return result;
} }
@ -31,6 +31,7 @@ namespace Game
BG_GetWeaponName_t BG_GetWeaponName = BG_GetWeaponName_t(0x4E6EC0); BG_GetWeaponName_t BG_GetWeaponName = BG_GetWeaponName_t(0x4E6EC0);
BG_LoadWeaponDef_LoadObj_t BG_LoadWeaponDef_LoadObj = BG_LoadWeaponDef_LoadObj_t(0x57B5F0); BG_LoadWeaponDef_LoadObj_t BG_LoadWeaponDef_LoadObj = BG_LoadWeaponDef_LoadObj_t(0x57B5F0);
BG_GetWeaponDef_t BG_GetWeaponDef = BG_GetWeaponDef_t(0x440EB0); BG_GetWeaponDef_t BG_GetWeaponDef = BG_GetWeaponDef_t(0x440EB0);
BG_GetEntityTypeName_t BG_GetEntityTypeName = BG_GetEntityTypeName_t(0x43A0E0);
Cbuf_AddServerText_t Cbuf_AddServerText = Cbuf_AddServerText_t(0x4BB9B0); Cbuf_AddServerText_t Cbuf_AddServerText = Cbuf_AddServerText_t(0x4BB9B0);
Cbuf_AddText_t Cbuf_AddText = Cbuf_AddText_t(0x404B20); Cbuf_AddText_t Cbuf_AddText = Cbuf_AddText_t(0x404B20);
@ -44,6 +45,8 @@ namespace Game
CG_ScoresUp_f_t CG_ScoresUp_f = CG_ScoresUp_f_t(0x5802C0); CG_ScoresUp_f_t CG_ScoresUp_f = CG_ScoresUp_f_t(0x5802C0);
CG_ScrollScoreboardUp_t CG_ScrollScoreboardUp = CG_ScrollScoreboardUp_t(0x47A5C0); CG_ScrollScoreboardUp_t CG_ScrollScoreboardUp = CG_ScrollScoreboardUp_t(0x47A5C0);
CG_ScrollScoreboardDown_t CG_ScrollScoreboardDown = CG_ScrollScoreboardDown_t(0x493B50); CG_ScrollScoreboardDown_t CG_ScrollScoreboardDown = CG_ScrollScoreboardDown_t(0x493B50);
CG_GetTeamName_t CG_GetTeamName = CG_GetTeamName_t(0x4B6210);
CG_SetupWeaponDef_t CG_SetupWeaponDef = CG_SetupWeaponDef_t(0x4BD520);
CL_GetClientName_t CL_GetClientName = CL_GetClientName_t(0x4563D0); CL_GetClientName_t CL_GetClientName = CL_GetClientName_t(0x4563D0);
CL_IsCgameInitialized_t CL_IsCgameInitialized = CL_IsCgameInitialized_t(0x43EB20); CL_IsCgameInitialized_t CL_IsCgameInitialized = CL_IsCgameInitialized_t(0x43EB20);
@ -73,6 +76,7 @@ namespace Game
Com_MatchToken_t Com_MatchToken = Com_MatchToken_t(0x447130); Com_MatchToken_t Com_MatchToken = Com_MatchToken_t(0x447130);
Com_SetSlowMotion_t Com_SetSlowMotion = Com_SetSlowMotion_t(0x446E20); Com_SetSlowMotion_t Com_SetSlowMotion = Com_SetSlowMotion_t(0x446E20);
Com_Quitf_t Com_Quit_f = Com_Quitf_t(0x4D4000); Com_Quitf_t Com_Quit_f = Com_Quitf_t(0x4D4000);
Com_PrintWarning_t Com_PrintWarning = Com_PrintWarning_t(0x4E0200);
Con_DrawMiniConsole_t Con_DrawMiniConsole = Con_DrawMiniConsole_t(0x464F30); Con_DrawMiniConsole_t Con_DrawMiniConsole = Con_DrawMiniConsole_t(0x464F30);
Con_DrawSolidConsole_t Con_DrawSolidConsole = Con_DrawSolidConsole_t(0x5A5040); Con_DrawSolidConsole_t Con_DrawSolidConsole = Con_DrawSolidConsole_t(0x5A5040);
@ -149,8 +153,13 @@ namespace Game
FS_IsShippedIWD_t FS_IsShippedIWD = FS_IsShippedIWD_t(0x642440); FS_IsShippedIWD_t FS_IsShippedIWD = FS_IsShippedIWD_t(0x642440);
FS_Delete_t FS_Delete = FS_Delete_t(0x48A5B0); FS_Delete_t FS_Delete = FS_Delete_t(0x48A5B0);
G_LogPrintf_t G_LogPrintf = G_LogPrintf_t(0x4B0150);
G_GetWeaponIndexForName_t G_GetWeaponIndexForName = G_GetWeaponIndexForName_t(0x49E540); G_GetWeaponIndexForName_t G_GetWeaponIndexForName = G_GetWeaponIndexForName_t(0x49E540);
G_SpawnEntitiesFromString_t G_SpawnEntitiesFromString = G_SpawnEntitiesFromString_t(0x4D8840); G_SpawnEntitiesFromString_t G_SpawnEntitiesFromString = G_SpawnEntitiesFromString_t(0x4D8840);
G_PrintEntities_t G_PrintEntities = G_PrintEntities_t(0x4E6A50);
G_GetEntityTypeName_t G_GetEntityTypeName = G_GetEntityTypeName_t(0x4EB810);
Svcmd_EntityList_f_t Svcmd_EntityList_f = Svcmd_EntityList_f_t(0x4B6A70);
GScr_LoadGameTypeScript_t GScr_LoadGameTypeScript = GScr_LoadGameTypeScript_t(0x4ED9A0); GScr_LoadGameTypeScript_t GScr_LoadGameTypeScript = GScr_LoadGameTypeScript_t(0x4ED9A0);
@ -259,6 +268,8 @@ namespace Game
Scr_GetFunctionHandle_t Scr_GetFunctionHandle = Scr_GetFunctionHandle_t(0x4234F0); Scr_GetFunctionHandle_t Scr_GetFunctionHandle = Scr_GetFunctionHandle_t(0x4234F0);
Scr_GetString_t Scr_GetString = Scr_GetString_t(0x425900); Scr_GetString_t Scr_GetString = Scr_GetString_t(0x425900);
Scr_GetConstString_t Scr_GetConstString = Scr_GetConstString_t(0x494830);
Scr_GetDebugString_t Scr_GetDebugString = Scr_GetDebugString_t(0x4EBF50);
Scr_GetFloat_t Scr_GetFloat = Scr_GetFloat_t(0x443140); Scr_GetFloat_t Scr_GetFloat = Scr_GetFloat_t(0x443140);
Scr_GetInt_t Scr_GetInt = Scr_GetInt_t(0x4F31D0); Scr_GetInt_t Scr_GetInt = Scr_GetInt_t(0x4F31D0);
Scr_GetObject_t Scr_GetObject = Scr_GetObject_t(0x462100); Scr_GetObject_t Scr_GetObject = Scr_GetObject_t(0x462100);
@ -269,16 +280,31 @@ namespace Game
Scr_AddEntity_t Scr_AddEntity = Scr_AddEntity_t(0x4BFB40); Scr_AddEntity_t Scr_AddEntity = Scr_AddEntity_t(0x4BFB40);
Scr_AddString_t Scr_AddString = Scr_AddString_t(0x412310); Scr_AddString_t Scr_AddString = Scr_AddString_t(0x412310);
Scr_AddConstString_t Scr_AddConstString = Scr_AddConstString_t(0x488860);
Scr_AddIString_t Scr_AddIString = Scr_AddIString_t(0x455F20);
Scr_AddInt_t Scr_AddInt = Scr_AddInt_t(0x41D7D0); Scr_AddInt_t Scr_AddInt = Scr_AddInt_t(0x41D7D0);
Scr_AddFloat_t Scr_AddFloat = Scr_AddFloat_t(0x61E860); Scr_AddFloat_t Scr_AddFloat = Scr_AddFloat_t(0x61E860);
Scr_AddObject_t Scr_AddObject = Scr_AddObject_t(0x430F40); Scr_AddObject_t Scr_AddObject = Scr_AddObject_t(0x430F40);
Scr_Notify_t Scr_Notify = Scr_Notify_t(0x4A4750); Scr_Notify_t Scr_Notify = Scr_Notify_t(0x4A4750);
Scr_NotifyLevel_t Scr_NotifyLevel = Scr_NotifyLevel_t(0x4D9C30); Scr_NotifyLevel_t Scr_NotifyLevel = Scr_NotifyLevel_t(0x4D9C30);
Scr_Error_t Scr_Error = Scr_Error_t(0x61E8B0); Scr_Error_t Scr_Error = Scr_Error_t(0x61E8B0);
Scr_ObjectError_t Scr_ObjectError = Scr_ObjectError_t(0x42EF40);
Scr_ParamError_t Scr_ParamError = Scr_ParamError_t(0x4FBC70);
Scr_GetType_t Scr_GetType = Scr_GetType_t(0x422900); Scr_GetType_t Scr_GetType = Scr_GetType_t(0x422900);
Scr_ClearOutParams_t Scr_ClearOutParams = Scr_ClearOutParams_t(0x4386E0); Scr_ClearOutParams_t Scr_ClearOutParams = Scr_ClearOutParams_t(0x4386E0);
Scr_GetObjectField_t Scr_GetObjectField = Scr_GetObjectField_t(0x4FF3D0);
Scr_SetObjectField_t Scr_SetObjectField = Scr_SetObjectField_t(0x4F20F0);
Scr_GetEntityField_t Scr_GetEntityField = Scr_GetEntityField_t(0x4E8390);
Scr_SetClientField_t Scr_SetClientField = Scr_SetClientField_t(0x4A6DF0);
Scr_AddClassField_t Scr_AddClassField = Scr_AddClassField_t(0x4C0E70);
GetEntity_t GetEntity = GetEntity_t(0x4BC270);
GetPlayerEntity_t GetPlayerEntity = GetPlayerEntity_t(0x49C4A0);
Scr_RegisterFunction_t Scr_RegisterFunction = Scr_RegisterFunction_t(0x492D50); Scr_RegisterFunction_t Scr_RegisterFunction = Scr_RegisterFunction_t(0x492D50);
Scr_ShutdownAllocNode_t Scr_ShutdownAllocNode = Scr_ShutdownAllocNode_t(0x441650); Scr_ShutdownAllocNode_t Scr_ShutdownAllocNode = Scr_ShutdownAllocNode_t(0x441650);
Scr_IsSystemActive_t Scr_IsSystemActive = Scr_IsSystemActive_t(0x4B24E0); Scr_IsSystemActive_t Scr_IsSystemActive = Scr_IsSystemActive_t(0x4B24E0);
@ -302,6 +328,8 @@ namespace Game
SL_ConvertToString_t SL_ConvertToString = SL_ConvertToString_t(0x4EC1D0); SL_ConvertToString_t SL_ConvertToString = SL_ConvertToString_t(0x4EC1D0);
SL_GetString_t SL_GetString = SL_GetString_t(0x4CDC10); SL_GetString_t SL_GetString = SL_GetString_t(0x4CDC10);
SL_AddRefToString_t SL_AddRefToString = SL_AddRefToString_t(0x4D9B00);
SL_RemoveRefToString_t SL_RemoveRefToString = SL_RemoveRefToString_t(0x47CD70);
SND_Init_t SND_Init = SND_Init_t(0x46A630); SND_Init_t SND_Init = SND_Init_t(0x46A630);
SND_InitDriver_t SND_InitDriver = SND_InitDriver_t(0x4F5090); SND_InitDriver_t SND_InitDriver = SND_InitDriver_t(0x4F5090);
@ -311,9 +339,11 @@ namespace Game
Steam_JoinLobby_t Steam_JoinLobby = Steam_JoinLobby_t(0x49CF70); Steam_JoinLobby_t Steam_JoinLobby = Steam_JoinLobby_t(0x49CF70);
StringTable_Lookup_t StringTable_Lookup = StringTable_Lookup_t(0x42F0E0); StringTable_Lookup_t StringTable_Lookup = StringTable_Lookup_t(0x42F0E0);
StringTable_GetColumnValueForRow_t StringTable_GetColumnValueForRow = StringTable_GetColumnValueForRow_t(0x4F2C80);
StringTable_HashString_t StringTable_HashString = StringTable_HashString_t(0x475EB0); StringTable_HashString_t StringTable_HashString = StringTable_HashString_t(0x475EB0);
SV_AddTestClient_t SV_AddTestClient = SV_AddTestClient_t(0x48AD30); SV_AddTestClient_t SV_AddTestClient = SV_AddTestClient_t(0x48AD30);
SV_IsTestClient_t SV_IsTestClient = SV_IsTestClient_t(0x4D6E40);
SV_GameClientNum_Score_t SV_GameClientNum_Score = SV_GameClientNum_Score_t(0x469AC0); SV_GameClientNum_Score_t SV_GameClientNum_Score = SV_GameClientNum_Score_t(0x469AC0);
SV_GameSendServerCommand_t SV_GameSendServerCommand = SV_GameSendServerCommand_t(0x4BC3A0); SV_GameSendServerCommand_t SV_GameSendServerCommand = SV_GameSendServerCommand_t(0x4BC3A0);
SV_Cmd_TokenizeString_t SV_Cmd_TokenizeString = SV_Cmd_TokenizeString_t(0x4B5780); SV_Cmd_TokenizeString_t SV_Cmd_TokenizeString = SV_Cmd_TokenizeString_t(0x4B5780);
@ -323,10 +353,10 @@ namespace Game
SV_SetConfigstring_t SV_SetConfigstring = SV_SetConfigstring_t(0x4982E0); SV_SetConfigstring_t SV_SetConfigstring = SV_SetConfigstring_t(0x4982E0);
SV_Loaded_t SV_Loaded = SV_Loaded_t(0x4EE3E0); SV_Loaded_t SV_Loaded = SV_Loaded_t(0x4EE3E0);
SV_ClientThink_t SV_ClientThink = SV_ClientThink_t(0x44ADD0); SV_ClientThink_t SV_ClientThink = SV_ClientThink_t(0x44ADD0);
SV_DropClient_t SV_DropClient = SV_DropClient_t(0x4D1600);
SV_GetPlayerByName_t SV_GetPlayerByName = SV_GetPlayerByName_t(0x6242B0); SV_GetPlayerByName_t SV_GetPlayerByName = SV_GetPlayerByName_t(0x6242B0);
SV_GetPlayerByNum_t SV_GetPlayerByNum = SV_GetPlayerByNum_t(0x624390); SV_GetPlayerByNum_t SV_GetPlayerByNum = SV_GetPlayerByNum_t(0x624390);
Sys_Error_t Sys_Error = Sys_Error_t(0x4E0200);
Sys_FreeFileList_t Sys_FreeFileList = Sys_FreeFileList_t(0x4D8580); Sys_FreeFileList_t Sys_FreeFileList = Sys_FreeFileList_t(0x4D8580);
Sys_IsDatabaseReady_t Sys_IsDatabaseReady = Sys_IsDatabaseReady_t(0x4CA4A0); Sys_IsDatabaseReady_t Sys_IsDatabaseReady = Sys_IsDatabaseReady_t(0x4CA4A0);
Sys_IsDatabaseReady2_t Sys_IsDatabaseReady2 = Sys_IsDatabaseReady2_t(0x441280); Sys_IsDatabaseReady2_t Sys_IsDatabaseReady2 = Sys_IsDatabaseReady2_t(0x441280);
@ -339,6 +369,7 @@ namespace Game
Sys_SuspendOtherThreads_t Sys_SuspendOtherThreads = Sys_SuspendOtherThreads_t(0x45A190); Sys_SuspendOtherThreads_t Sys_SuspendOtherThreads = Sys_SuspendOtherThreads_t(0x45A190);
Sys_ListFiles_t Sys_ListFiles = Sys_ListFiles_t(0x45A660); Sys_ListFiles_t Sys_ListFiles = Sys_ListFiles_t(0x45A660);
Sys_Milliseconds_t Sys_Milliseconds = Sys_Milliseconds_t(0x42A660); Sys_Milliseconds_t Sys_Milliseconds = Sys_Milliseconds_t(0x42A660);
Sys_Error_t Sys_Error = Sys_Error_t(0x43D570);
Sys_LockWrite_t Sys_LockWrite = Sys_LockWrite_t(0x435880); Sys_LockWrite_t Sys_LockWrite = Sys_LockWrite_t(0x435880);
Sys_TempPriorityAtLeastNormalBegin_t Sys_TempPriorityAtLeastNormalBegin = Sys_TempPriorityAtLeastNormalBegin_t(0x478680); Sys_TempPriorityAtLeastNormalBegin_t Sys_TempPriorityAtLeastNormalBegin = Sys_TempPriorityAtLeastNormalBegin_t(0x478680);
Sys_TempPriorityEnd_t Sys_TempPriorityEnd = Sys_TempPriorityEnd_t(0x4DCF00); Sys_TempPriorityEnd_t Sys_TempPriorityEnd = Sys_TempPriorityEnd_t(0x4DCF00);
@ -390,6 +421,13 @@ namespace Game
PM_Trace_t PM_Trace = PM_Trace_t(0x441F60); PM_Trace_t PM_Trace = PM_Trace_t(0x441F60);
PM_GetEffectiveStance_t PM_GetEffectiveStance = PM_GetEffectiveStance_t(0x412540); PM_GetEffectiveStance_t PM_GetEffectiveStance = PM_GetEffectiveStance_t(0x412540);
CL_MouseEvent_t CL_MouseEvent = CL_MouseEvent_t(0x4D7C50);
IN_RecenterMouse_t IN_RecenterMouse = IN_RecenterMouse_t(0x463D80);
IN_MouseMove_t IN_MouseMove = IN_MouseMove_t(0x64C490);
IN_Init_t IN_Init = IN_Init_t(0x45D620);
IN_Shutdown_t IN_Shutdown = IN_Shutdown_t(0x426360);
XAssetHeader* DB_XAssetPool = reinterpret_cast<XAssetHeader*>(0x7998A8); XAssetHeader* DB_XAssetPool = reinterpret_cast<XAssetHeader*>(0x7998A8);
unsigned int* g_poolSize = reinterpret_cast<unsigned int*>(0x7995E8); unsigned int* g_poolSize = reinterpret_cast<unsigned int*>(0x7995E8);
@ -405,7 +443,7 @@ namespace Game
float* cgameFOVSensitivityScale = reinterpret_cast<float*>(0xB2F884); float* cgameFOVSensitivityScale = reinterpret_cast<float*>(0xB2F884);
int* svs_time = reinterpret_cast<int*>(0x31D9384); int* svs_time = reinterpret_cast<int*>(0x31D9384);
int* svs_numclients = reinterpret_cast<int*>(0x31D938C); int* svs_clientCount = reinterpret_cast<int*>(0x31D938C);
client_t* svs_clients = reinterpret_cast<client_t*>(0x31D9390); client_t* svs_clients = reinterpret_cast<client_t*>(0x31D9390);
UiContext* uiContext = reinterpret_cast<UiContext*>(0x62E2858); UiContext* uiContext = reinterpret_cast<UiContext*>(0x62E2858);
@ -473,6 +511,7 @@ namespace Game
unsigned short* db_hashTable = reinterpret_cast<unsigned short*>(0x12412B0); unsigned short* db_hashTable = reinterpret_cast<unsigned short*>(0x12412B0);
scrVmPub_t* scrVmPub = reinterpret_cast<scrVmPub_t*>(0x2040CF0); scrVmPub_t* scrVmPub = reinterpret_cast<scrVmPub_t*>(0x2040CF0);
scrVarPub_t* scrVarPub = reinterpret_cast<scrVarPub_t*>(0x201A408);
clientstate_t* clcState = reinterpret_cast<clientstate_t*>(0xB2C540); clientstate_t* clcState = reinterpret_cast<clientstate_t*>(0xB2C540);
@ -501,10 +540,23 @@ namespace Game
GraphFloat* aaInputGraph = reinterpret_cast<GraphFloat*>(0x7A2FC0); GraphFloat* aaInputGraph = reinterpret_cast<GraphFloat*>(0x7A2FC0);
const char* MY_CMDS = reinterpret_cast<const char*>(0x73C9C4); // Count 5
XModel** cached_models = reinterpret_cast<XModel**>(0x1AA20C8);
FastCriticalSection* db_hashCritSect = reinterpret_cast<FastCriticalSection*>(0x16B8A54); FastCriticalSection* db_hashCritSect = reinterpret_cast<FastCriticalSection*>(0x16B8A54);
vec3_t* CorrectSolidDeltas = reinterpret_cast<vec3_t*>(0x739BB8); // Count 26 vec3_t* CorrectSolidDeltas = reinterpret_cast<vec3_t*>(0x739BB8); // Count 26
level_locals_t* level = reinterpret_cast<level_locals_t*>(0x1A831A8);
float(*penetrationDepthTable)[PENETRATE_TYPE_COUNT][SURF_TYPE_COUNT] = reinterpret_cast<float(*)[PENETRATE_TYPE_COUNT][SURF_TYPE_COUNT]>(0x7C4878);
WinMouseVars_t* s_wmv = reinterpret_cast<WinMouseVars_t*>(0x649D640);
int* window_center_x = reinterpret_cast<int*>(0x649D638);
int* window_center_y = reinterpret_cast<int*>(0x649D630);
void Sys_LockRead(FastCriticalSection* critSect) void Sys_LockRead(FastCriticalSection* critSect)
{ {
InterlockedIncrement(&critSect->readCount); InterlockedIncrement(&critSect->readCount);
@ -517,6 +569,13 @@ namespace Game
InterlockedDecrement(&critSect->readCount); InterlockedDecrement(&critSect->readCount);
} }
XModel* G_GetModel(const int index)
{
assert(index > 0);
assert(index < MAX_MODELS);
return cached_models[index];
}
XAssetHeader ReallocateAssetPool(XAssetType type, unsigned int newSize) XAssetHeader ReallocateAssetPool(XAssetType type, unsigned int newSize)
{ {
int elSize = DB_GetXAssetSizeHandlers[type](); int elSize = DB_GetXAssetSizeHandlers[type]();
@ -703,24 +762,49 @@ namespace Game
return hash; return hash;
} }
void SV_KickClientError(client_t* client, const std::string& reason) void SV_GameDropClient(int clientNum, const char* reason)
{ {
if (client->state < 5) const auto maxClients = Dvar_FindVar("sv_maxclients")->current.integer;
assert(maxClients >= 1 && maxClients <= 18);
if (clientNum >= 0 && clientNum < maxClients)
{ {
Components::Network::SendCommand(client->netchan.remoteAddress, "error", reason); SV_DropClient(&svs_clients[clientNum], reason, true);
}
} }
SV_KickClient(client, reason.data()); void SV_DropAllBots()
{
for (auto i = 0; i < *svs_clientCount; ++i)
{
if (svs_clients[i].state != clientstate_t::CS_FREE
&& svs_clients[i].netchan.remoteAddress.type == netadrtype_t::NA_BOT)
{
SV_GameDropClient(i, "GAME_GET_TO_COVER");
}
}
} }
void Scr_iPrintLn(int clientNum, const std::string& message) void IncInParam()
{ {
Game::SV_GameSendServerCommand(clientNum, 0, Utils::String::VA("%c \"%s\"", 0x66, message.data())); Scr_ClearOutParams();
if (scrVmPub->top == scrVmPub->maxStack)
{
Sys_Error("Internal script stack overflow");
} }
void Scr_iPrintLnBold(int clientNum, const std::string& message) scrVmPub->top++;
scrVmPub->inparamcount++;
}
void Scr_AddBool(int value)
{ {
Game::SV_GameSendServerCommand(clientNum, 0, Utils::String::VA("%c \"%s\"", 0x67, message.data())); assert(value == 0 || value == 1);
IncInParam();
scrVmPub->top->type = VAR_INTEGER;
scrVmPub->top->u.intValue = value;
} }
int FS_FOpenFileReadCurrentThread(const char* file, int* fh) int FS_FOpenFileReadCurrentThread(const char* file, int* fh)
@ -1100,19 +1184,23 @@ namespace Game
} }
} }
bool PM_IsAdsAllowed(Game::playerState_s* playerState) __declspec(naked) bool PM_IsAdsAllowed(playerState_s* /*ps*/)
{ {
bool result;
__asm __asm
{ {
mov esi, playerState push eax
mov ebx, 0x5755A0 pushad
call ebx
mov result, al // AL
}
return result; mov esi, [esp + 0x24 + 0x4] // ps
mov ecx, 0x5755A0
call ecx
mov [esp + 0x20], eax
popad
pop eax
ret
}
} }
__declspec(naked) void FS_AddLocalizedGameDirectory(const char* /*path*/, const char* /*dir*/) __declspec(naked) void FS_AddLocalizedGameDirectory(const char* /*path*/, const char* /*dir*/)
@ -1164,28 +1252,6 @@ namespace Game
} }
} }
__declspec(naked) void SV_KickClient(client_t* /*client*/, const char* /*reason*/)
{
__asm
{
pushad
mov edi, 0
mov esi, [esp + 24h]
push [esp + 28h]
push 0
push 0
mov eax, 6249A0h
call eax
add esp, 0Ch
popad
retn
}
}
__declspec(naked) void Scr_NotifyId(unsigned int /*id*/, unsigned __int16 /*stringValue*/, unsigned int /*paramcount*/) __declspec(naked) void Scr_NotifyId(unsigned int /*id*/, unsigned __int16 /*stringValue*/, unsigned int /*paramcount*/)
{ {
__asm __asm
@ -1207,6 +1273,27 @@ namespace Game
} }
} }
__declspec(naked) void RuntimeErrorInternal(int /*channel*/, const char* /*codePos*/, unsigned int /*index*/, const char* /*msg*/)
{
__asm
{
pushad
mov eax, [esp + 0x10 + 0x20] // msg
mov edi, [esp + 0x4 + 0x20] // channel
push [esp + 0xC + 0x20] // index
push [esp + 0xC + 0x20] // codePos
mov edx, 0x61ABE0
call edx
add esp, 0x8
popad
ret
}
}
__declspec(naked) void IN_KeyUp(kbutton_t* /*button*/) __declspec(naked) void IN_KeyUp(kbutton_t* /*button*/)
{ {
__asm __asm
@ -1540,5 +1627,20 @@ namespace Game
} }
} }
constexpr auto SV_BotUserMove_Addr = 0x626E50;
__declspec(naked) void SV_BotUserMove(client_t* /*client*/)
{
__asm
{
pushad
mov edi, [esp + 0x20 + 0x4]
call SV_BotUserMove_Addr
popad
ret
}
}
#pragma optimize("", on) #pragma optimize("", on)
} }

View File

@ -43,6 +43,9 @@ namespace Game
typedef WeaponDef* (__cdecl * BG_GetWeaponDef_t)(int weaponIndex); typedef WeaponDef* (__cdecl * BG_GetWeaponDef_t)(int weaponIndex);
extern BG_GetWeaponDef_t BG_GetWeaponDef; extern BG_GetWeaponDef_t BG_GetWeaponDef;
typedef const char*(__cdecl * BG_GetEntityTypeName_t)(const int eType);
extern BG_GetEntityTypeName_t BG_GetEntityTypeName;
typedef void(__cdecl * Cbuf_AddServerText_t)(); typedef void(__cdecl * Cbuf_AddServerText_t)();
extern Cbuf_AddServerText_t Cbuf_AddServerText; extern Cbuf_AddServerText_t Cbuf_AddServerText;
@ -76,6 +79,12 @@ namespace Game
typedef void(__cdecl * CG_ScrollScoreboardDown_t)(cg_s* cgameGlob); typedef void(__cdecl * CG_ScrollScoreboardDown_t)(cg_s* cgameGlob);
extern CG_ScrollScoreboardDown_t CG_ScrollScoreboardDown; extern CG_ScrollScoreboardDown_t CG_ScrollScoreboardDown;
typedef const char*(__cdecl * CG_GetTeamName_t)(team_t team);
extern CG_GetTeamName_t CG_GetTeamName;
typedef void(__cdecl * CG_SetupWeaponDef_t)(int localClientNum, unsigned int weapIndex);
extern CG_SetupWeaponDef_t CG_SetupWeaponDef;
typedef char*(__cdecl * CL_GetClientName_t)(int localClientNum, int index, char *buf, size_t size); typedef char*(__cdecl * CL_GetClientName_t)(int localClientNum, int index, char *buf, size_t size);
extern CL_GetClientName_t CL_GetClientName; extern CL_GetClientName_t CL_GetClientName;
@ -154,6 +163,9 @@ namespace Game
typedef void(__cdecl * Com_Quitf_t)(); typedef void(__cdecl * Com_Quitf_t)();
extern Com_Quitf_t Com_Quit_f; extern Com_Quitf_t Com_Quit_f;
typedef void(__cdecl * Com_PrintWarning_t)(int, const char*, ...);
extern Com_PrintWarning_t Com_PrintWarning;
typedef char* (__cdecl * Con_DrawMiniConsole_t)(int localClientNum, int xPos, int yPos, float alpha); typedef char* (__cdecl * Con_DrawMiniConsole_t)(int localClientNum, int xPos, int yPos, float alpha);
extern Con_DrawMiniConsole_t Con_DrawMiniConsole; extern Con_DrawMiniConsole_t Con_DrawMiniConsole;
@ -329,7 +341,7 @@ namespace Game
typedef int(__cdecl * FS_FOpenFileReadForThread_t)(const char *filename, int *file, int thread); typedef int(__cdecl * FS_FOpenFileReadForThread_t)(const char *filename, int *file, int thread);
extern FS_FOpenFileReadForThread_t FS_FOpenFileReadForThread; extern FS_FOpenFileReadForThread_t FS_FOpenFileReadForThread;
typedef int(__cdecl * FS_FCloseFile_t)(int fh); typedef int(__cdecl * FS_FCloseFile_t)(int stream);
extern FS_FCloseFile_t FS_FCloseFile; extern FS_FCloseFile_t FS_FCloseFile;
typedef bool(__cdecl * FS_FileExists_t)(const char* file); typedef bool(__cdecl * FS_FileExists_t)(const char* file);
@ -368,12 +380,24 @@ namespace Game
typedef int(__cdecl* FS_Delete_t)(const char* fileName); typedef int(__cdecl* FS_Delete_t)(const char* fileName);
extern FS_Delete_t FS_Delete; extern FS_Delete_t FS_Delete;
typedef int(__cdecl* G_GetWeaponIndexForName_t)(char*); typedef void(__cdecl * G_LogPrintf_t)(const char* fmt, ...);
extern G_LogPrintf_t G_LogPrintf;
typedef unsigned int(__cdecl * G_GetWeaponIndexForName_t)(const char*);
extern G_GetWeaponIndexForName_t G_GetWeaponIndexForName; extern G_GetWeaponIndexForName_t G_GetWeaponIndexForName;
typedef void(__cdecl * G_SpawnEntitiesFromString_t)(); typedef void(__cdecl * G_SpawnEntitiesFromString_t)();
extern G_SpawnEntitiesFromString_t G_SpawnEntitiesFromString; extern G_SpawnEntitiesFromString_t G_SpawnEntitiesFromString;
typedef void(__cdecl * G_PrintEntities_t)();
extern G_PrintEntities_t G_PrintEntities;
typedef const char*(__cdecl * G_GetEntityTypeName_t)(const gentity_s* ent);
extern G_GetEntityTypeName_t G_GetEntityTypeName;
typedef void(__cdecl * Svcmd_EntityList_f_t)();
extern Svcmd_EntityList_f_t Svcmd_EntityList_f;
typedef void(__cdecl * GScr_LoadGameTypeScript_t)(); typedef void(__cdecl * GScr_LoadGameTypeScript_t)();
extern GScr_LoadGameTypeScript_t GScr_LoadGameTypeScript; extern GScr_LoadGameTypeScript_t GScr_LoadGameTypeScript;
@ -555,7 +579,7 @@ namespace Game
typedef bool(__cdecl * NET_IsLocalAddress_t)(netadr_t adr); typedef bool(__cdecl * NET_IsLocalAddress_t)(netadr_t adr);
extern NET_IsLocalAddress_t NET_IsLocalAddress; extern NET_IsLocalAddress_t NET_IsLocalAddress;
typedef bool(__cdecl * NET_StringToAdr_t)(const char *s, netadr_t *a); typedef int(__cdecl * NET_StringToAdr_t)(const char *s, netadr_t *a);
extern NET_StringToAdr_t NET_StringToAdr; extern NET_StringToAdr_t NET_StringToAdr;
typedef void(__cdecl * NET_OutOfBandPrint_t)(netsrc_t sock, netadr_t adr, const char *data); typedef void(__cdecl * NET_OutOfBandPrint_t)(netsrc_t sock, netadr_t adr, const char *data);
@ -636,16 +660,22 @@ namespace Game
typedef void(__cdecl * RemoveRefToObject_t)(unsigned int id); typedef void(__cdecl * RemoveRefToObject_t)(unsigned int id);
extern RemoveRefToObject_t RemoveRefToObject; extern RemoveRefToObject_t RemoveRefToObject;
typedef void(__cdecl * Scr_AddEntity_t)(gentity_s const*); typedef void(__cdecl * Scr_AddEntity_t)(const gentity_s* ent);
extern Scr_AddEntity_t Scr_AddEntity; extern Scr_AddEntity_t Scr_AddEntity;
typedef void(__cdecl * Scr_AddString_t)(const char* str); typedef void(__cdecl * Scr_AddString_t)(const char* value);
extern Scr_AddString_t Scr_AddString; extern Scr_AddString_t Scr_AddString;
typedef void(__cdecl * Scr_AddInt_t)(int num); typedef void(__cdecl * Scr_AddConstString_t)(unsigned int value);
extern Scr_AddConstString_t Scr_AddConstString;
typedef void(__cdecl * Scr_AddIString_t)(const char* value);
extern Scr_AddIString_t Scr_AddIString;
typedef void(__cdecl * Scr_AddInt_t)(int value);
extern Scr_AddInt_t Scr_AddInt; extern Scr_AddInt_t Scr_AddInt;
typedef void(__cdecl * Scr_AddFloat_t)(float); typedef void(__cdecl * Scr_AddFloat_t)(float value);
extern Scr_AddFloat_t Scr_AddFloat; extern Scr_AddFloat_t Scr_AddFloat;
typedef void(__cdecl * Scr_AddObject_t)(unsigned int id); typedef void(__cdecl * Scr_AddObject_t)(unsigned int id);
@ -660,22 +690,28 @@ namespace Game
typedef int(__cdecl * Scr_LoadScript_t)(const char*); typedef int(__cdecl * Scr_LoadScript_t)(const char*);
extern Scr_LoadScript_t Scr_LoadScript; extern Scr_LoadScript_t Scr_LoadScript;
typedef char* (__cdecl * Scr_GetString_t)(int); typedef const char*(__cdecl * Scr_GetString_t)(unsigned int index);
extern Scr_GetString_t Scr_GetString; extern Scr_GetString_t Scr_GetString;
typedef float(__cdecl * Scr_GetFloat_t)(int); typedef scr_string_t(__cdecl * Scr_GetConstString_t)(unsigned int index);
extern Scr_GetConstString_t Scr_GetConstString;
typedef const char*(__cdecl * Scr_GetDebugString_t)(unsigned int index);
extern Scr_GetDebugString_t Scr_GetDebugString;
typedef float(__cdecl * Scr_GetFloat_t)(unsigned int index);
extern Scr_GetFloat_t Scr_GetFloat; extern Scr_GetFloat_t Scr_GetFloat;
typedef int(__cdecl * Scr_GetInt_t)(int); typedef int(__cdecl * Scr_GetInt_t)(unsigned int index);
extern Scr_GetInt_t Scr_GetInt; extern Scr_GetInt_t Scr_GetInt;
typedef unsigned int(__cdecl * Scr_GetObject_t)(int); typedef unsigned int(__cdecl * Scr_GetObject_t)(unsigned int index);
extern Scr_GetObject_t Scr_GetObject; extern Scr_GetObject_t Scr_GetObject;
typedef unsigned int(__cdecl * Scr_GetNumParam_t)(); typedef unsigned int(__cdecl * Scr_GetNumParam_t)();
extern Scr_GetNumParam_t Scr_GetNumParam; extern Scr_GetNumParam_t Scr_GetNumParam;
typedef int(__cdecl * Scr_GetFunctionHandle_t)(const char*, const char*); typedef int(__cdecl * Scr_GetFunctionHandle_t)(const char* filename, const char* name);
extern Scr_GetFunctionHandle_t Scr_GetFunctionHandle; extern Scr_GetFunctionHandle_t Scr_GetFunctionHandle;
typedef int(__cdecl * Scr_ExecThread_t)(int, int); typedef int(__cdecl * Scr_ExecThread_t)(int, int);
@ -693,7 +729,7 @@ namespace Game
typedef void(__cdecl * Scr_ClearOutParams_t)(); typedef void(__cdecl * Scr_ClearOutParams_t)();
extern Scr_ClearOutParams_t Scr_ClearOutParams; extern Scr_ClearOutParams_t Scr_ClearOutParams;
typedef void(__cdecl * Scr_RegisterFunction_t)(scr_function_t function); typedef void(__cdecl * Scr_RegisterFunction_t)(int func, const char* name);
extern Scr_RegisterFunction_t Scr_RegisterFunction; extern Scr_RegisterFunction_t Scr_RegisterFunction;
typedef bool(__cdecl * Scr_IsSystemActive_t)(); typedef bool(__cdecl * Scr_IsSystemActive_t)();
@ -705,6 +741,33 @@ namespace Game
typedef void(__cdecl * Scr_Error_t)(const char*); typedef void(__cdecl * Scr_Error_t)(const char*);
extern Scr_Error_t Scr_Error; extern Scr_Error_t Scr_Error;
typedef void(__cdecl * Scr_ObjectError_t)(const char*);
extern Scr_ObjectError_t Scr_ObjectError;
typedef void(__cdecl * Scr_ParamError_t)(unsigned int paramIndex, const char*);
extern Scr_ParamError_t Scr_ParamError;
typedef void(__cdecl * Scr_GetObjectField_t)(unsigned int classnum, int entnum, int offset);
extern Scr_GetObjectField_t Scr_GetObjectField;
typedef int(__cdecl * Scr_SetObjectField_t)(unsigned int classnum, int entnum, int offset);
extern Scr_SetObjectField_t Scr_SetObjectField;
typedef void(__cdecl * Scr_SetClientField_t)(gclient_s* client, int offset);
extern Scr_SetClientField_t Scr_SetClientField;
typedef void(__cdecl * Scr_GetEntityField_t)(int entnum, int offset);
extern Scr_GetEntityField_t Scr_GetEntityField;
typedef void(__cdecl * Scr_AddClassField_t)(unsigned int classnum, const char* name, unsigned int offset);
extern Scr_AddClassField_t Scr_AddClassField;
typedef gentity_s*(__cdecl * GetPlayerEntity_t)(scr_entref_t entref);
extern GetPlayerEntity_t GetPlayerEntity;
typedef gentity_s*(__cdecl * GetEntity_t)(scr_entref_t entref);
extern GetEntity_t GetEntity;
typedef script_t* (__cdecl * Script_Alloc_t)(int length); typedef script_t* (__cdecl * Script_Alloc_t)(int length);
extern Script_Alloc_t Script_Alloc; extern Script_Alloc_t Script_Alloc;
@ -723,12 +786,18 @@ namespace Game
typedef unsigned int(__cdecl* SEH_ReadCharFromString_t)(const char** text, int* isTrailingPunctuation); typedef unsigned int(__cdecl* SEH_ReadCharFromString_t)(const char** text, int* isTrailingPunctuation);
extern SEH_ReadCharFromString_t SEH_ReadCharFromString; extern SEH_ReadCharFromString_t SEH_ReadCharFromString;
typedef char* (__cdecl * SL_ConvertToString_t)(unsigned short stringValue); typedef const char*(__cdecl * SL_ConvertToString_t)(scr_string_t stringValue);
extern SL_ConvertToString_t SL_ConvertToString; extern SL_ConvertToString_t SL_ConvertToString;
typedef short(__cdecl * SL_GetString_t)(const char *str, unsigned int user); typedef short(__cdecl * SL_GetString_t)(const char *str, unsigned int user);
extern SL_GetString_t SL_GetString; extern SL_GetString_t SL_GetString;
typedef void(__cdecl * SL_AddRefToString_t)(unsigned int stringValue);
extern SL_AddRefToString_t SL_AddRefToString;
typedef void(__cdecl * SL_RemoveRefToString_t)(unsigned int stringValue);
extern SL_RemoveRefToString_t SL_RemoveRefToString;
typedef void(__cdecl * SND_Init_t)(int a1, int a2, int a3); typedef void(__cdecl * SND_Init_t)(int a1, int a2, int a3);
extern SND_Init_t SND_Init; extern SND_Init_t SND_Init;
@ -741,15 +810,21 @@ namespace Game
typedef void(__cdecl * Steam_JoinLobby_t)(SteamID, char); typedef void(__cdecl * Steam_JoinLobby_t)(SteamID, char);
extern Steam_JoinLobby_t Steam_JoinLobby; extern Steam_JoinLobby_t Steam_JoinLobby;
typedef const char*(__cdecl * StringTable_Lookup_t)(StringTable *table, const int comparisonColumn, const char *value, const int valueColumn); typedef const char*(__cdecl * StringTable_Lookup_t)(const StringTable *table, const int comparisonColumn, const char *value, const int valueColumn);
extern StringTable_Lookup_t StringTable_Lookup; extern StringTable_Lookup_t StringTable_Lookup;
typedef const char* (__cdecl * StringTable_GetColumnValueForRow_t)(const StringTable* table, int, int column);
extern StringTable_GetColumnValueForRow_t StringTable_GetColumnValueForRow;
typedef int(__cdecl * StringTable_HashString_t)(const char* string); typedef int(__cdecl * StringTable_HashString_t)(const char* string);
extern StringTable_HashString_t StringTable_HashString; extern StringTable_HashString_t StringTable_HashString;
typedef gentity_t*(__cdecl* SV_AddTestClient_t)(); typedef gentity_t*(__cdecl* SV_AddTestClient_t)();
extern SV_AddTestClient_t SV_AddTestClient; extern SV_AddTestClient_t SV_AddTestClient;
typedef int(__cdecl * SV_IsTestClient_t)(int clientNum);
extern SV_IsTestClient_t SV_IsTestClient;
typedef int(__cdecl* SV_GameClientNum_Score_t)(int clientID); typedef int(__cdecl* SV_GameClientNum_Score_t)(int clientID);
extern SV_GameClientNum_Score_t SV_GameClientNum_Score; extern SV_GameClientNum_Score_t SV_GameClientNum_Score;
@ -777,13 +852,16 @@ namespace Game
typedef void(__cdecl * SV_ClientThink_t)(client_s*, usercmd_s*); typedef void(__cdecl * SV_ClientThink_t)(client_s*, usercmd_s*);
extern SV_ClientThink_t SV_ClientThink; extern SV_ClientThink_t SV_ClientThink;
typedef void(__cdecl * SV_DropClient_t)(client_t* drop, const char* reason, bool tellThem);
extern SV_DropClient_t SV_DropClient;
typedef client_t*(__cdecl * SV_GetPlayerByName_t)(); typedef client_t*(__cdecl * SV_GetPlayerByName_t)();
extern SV_GetPlayerByName_t SV_GetPlayerByName; extern SV_GetPlayerByName_t SV_GetPlayerByName;
typedef client_t*(__cdecl * SV_GetPlayerByNum_t)(); typedef client_t*(__cdecl * SV_GetPlayerByNum_t)();
extern SV_GetPlayerByNum_t SV_GetPlayerByNum; extern SV_GetPlayerByNum_t SV_GetPlayerByNum;
typedef int(__cdecl * Sys_Error_t)(int, char *, ...); typedef void(__cdecl * Sys_Error_t)(const char* error, ...);
extern Sys_Error_t Sys_Error; extern Sys_Error_t Sys_Error;
typedef void(__cdecl * Sys_FreeFileList_t)(char** list); typedef void(__cdecl * Sys_FreeFileList_t)(char** list);
@ -861,7 +939,7 @@ namespace Game
typedef int(__cdecl * UI_TextWidth_t)(const char *text, int maxChars, Font_s *font, float scale); typedef int(__cdecl * UI_TextWidth_t)(const char *text, int maxChars, Font_s *font, float scale);
extern UI_TextWidth_t UI_TextWidth; extern UI_TextWidth_t UI_TextWidth;
typedef void(__cdecl * UI_DrawText_t)(void* scrPlace, const char *text, int maxChars, Font_s *font, float x, float y, int horzAlign, int vertAlign, float scale, const float *color, int style); typedef void(__cdecl * UI_DrawText_t)(const ScreenPlacement* scrPlace, const char* text, int maxChars, Font_s* font, float x, float y, int horzAlign, int vertAlign, float scale, const float* color, int style);
extern UI_DrawText_t UI_DrawText; extern UI_DrawText_t UI_DrawText;
typedef Font_s* (__cdecl* UI_GetFontHandle_t)(ScreenPlacement* scrPlace, int fontEnum, float scale); typedef Font_s* (__cdecl* UI_GetFontHandle_t)(ScreenPlacement* scrPlace, int fontEnum, float scale);
@ -927,6 +1005,21 @@ namespace Game
typedef EffectiveStance(__cdecl * PM_GetEffectiveStance_t)(const playerState_s* ps); typedef EffectiveStance(__cdecl * PM_GetEffectiveStance_t)(const playerState_s* ps);
extern PM_GetEffectiveStance_t PM_GetEffectiveStance; extern PM_GetEffectiveStance_t PM_GetEffectiveStance;
typedef int(__cdecl * CL_MouseEvent_t)(int x, int y, int dx, int dy);
extern CL_MouseEvent_t CL_MouseEvent;
typedef void(__cdecl * IN_RecenterMouse_t)();
extern IN_RecenterMouse_t IN_RecenterMouse;
typedef void(__cdecl * IN_MouseMove_t)();
extern IN_MouseMove_t IN_MouseMove;
typedef void(__cdecl * IN_Init_t)();
extern IN_Init_t IN_Init;
typedef void(__cdecl * IN_Shutdown_t)();
extern IN_Shutdown_t IN_Shutdown;
extern XAssetHeader* DB_XAssetPool; extern XAssetHeader* DB_XAssetPool;
extern unsigned int* g_poolSize; extern unsigned int* g_poolSize;
@ -940,7 +1033,7 @@ namespace Game
extern float* cgameFOVSensitivityScale; extern float* cgameFOVSensitivityScale;
extern int* svs_time; extern int* svs_time;
extern int* svs_numclients; extern int* svs_clientCount;
extern client_t* svs_clients; extern client_t* svs_clients;
extern source_t **sourceFiles; extern source_t **sourceFiles;
@ -1012,6 +1105,7 @@ namespace Game
extern unsigned short* db_hashTable; extern unsigned short* db_hashTable;
extern scrVmPub_t* scrVmPub; extern scrVmPub_t* scrVmPub;
extern scrVarPub_t* scrVarPub;
extern clientstate_t* clcState; extern clientstate_t* clcState;
@ -1043,13 +1137,29 @@ namespace Game
constexpr auto AIM_ASSIST_GRAPH_COUNT = 4u; constexpr auto AIM_ASSIST_GRAPH_COUNT = 4u;
extern GraphFloat* aaInputGraph; extern GraphFloat* aaInputGraph;
extern const char* MY_CMDS;
constexpr auto MAX_MODELS = 512;
extern XModel** cached_models;
extern vec3_t* CorrectSolidDeltas; extern vec3_t* CorrectSolidDeltas;
extern FastCriticalSection* db_hashCritSect; extern FastCriticalSection* db_hashCritSect;
extern level_locals_t* level;
extern float(*penetrationDepthTable)[PENETRATE_TYPE_COUNT][SURF_TYPE_COUNT];
extern WinMouseVars_t* s_wmv;
extern int* window_center_x;
extern int* window_center_y;
void Sys_LockRead(FastCriticalSection* critSect); void Sys_LockRead(FastCriticalSection* critSect);
void Sys_UnlockRead(FastCriticalSection* critSect); void Sys_UnlockRead(FastCriticalSection* critSect);
XModel* G_GetModel(int index);
XAssetHeader ReallocateAssetPool(XAssetType type, unsigned int newSize); XAssetHeader ReallocateAssetPool(XAssetType type, unsigned int newSize);
void Menu_FreeItemMemory(Game::itemDef_s* item); void Menu_FreeItemMemory(Game::itemDef_s* item);
void Menu_SetNextCursorItem(Game::UiContext* ctx, Game::menuDef_t* currentMenu, int unk = 1); void Menu_SetNextCursorItem(Game::UiContext* ctx, Game::menuDef_t* currentMenu, int unk = 1);
@ -1069,7 +1179,7 @@ namespace Game
void FS_AddLocalizedGameDirectory(const char *path, const char *dir); void FS_AddLocalizedGameDirectory(const char *path, const char *dir);
bool PM_IsAdsAllowed(Game::playerState_s* playerState); bool PM_IsAdsAllowed(playerState_s* ps);
void ShowMessageBox(const std::string& message, const std::string& title); void ShowMessageBox(const std::string& message, const std::string& title);
@ -1078,12 +1188,15 @@ namespace Game
void R_LoadSunThroughDvars(const char* mapname, sunflare_t* sun); void R_LoadSunThroughDvars(const char* mapname, sunflare_t* sun);
void R_SetSunFromDvars(sunflare_t* sun); void R_SetSunFromDvars(sunflare_t* sun);
void SV_KickClient(client_t* client, const char* reason); void SV_GameDropClient(int clientNum, const char* reason);
void SV_KickClientError(client_t* client, const std::string& reason); void SV_DropAllBots();
void SV_BotUserMove(client_t* client);
void RuntimeErrorInternal(int channel, const char* codePos, unsigned int index, const char* msg);
void IncInParam();
void Scr_iPrintLn(int clientNum, const std::string& message);
void Scr_iPrintLnBold(int clientNum, const std::string& message);
void Scr_NotifyId(unsigned int id, unsigned __int16 stringValue, unsigned int paramcount); void Scr_NotifyId(unsigned int id, unsigned __int16 stringValue, unsigned int paramcount);
void Scr_AddBool(int value);
void IN_KeyUp(kbutton_t* button); void IN_KeyUp(kbutton_t* button);
void IN_KeyDown(kbutton_t* button); void IN_KeyDown(kbutton_t* button);

View File

@ -20,8 +20,30 @@ namespace Game
typedef vec_t vec3_t[3]; typedef vec_t vec3_t[3];
typedef vec_t vec4_t[4]; typedef vec_t vec4_t[4];
typedef unsigned int scr_entref_t; typedef unsigned __int16 scr_string_t;
typedef void(__cdecl * scr_function_t)(scr_entref_t);
struct scr_entref_t
{
unsigned __int16 entnum;
unsigned __int16 classnum;
};
typedef void(__cdecl * BuiltinFunction)();
typedef void(__cdecl * BuiltinMethod)(scr_entref_t);
struct BuiltinFunctionDef
{
const char* actionString;
BuiltinFunction actionFunc;
int type;
};
struct BuiltinMethodDef
{
const char* actionString;
BuiltinMethod actionFunc;
int type;
};
enum XAssetType enum XAssetType
{ {
@ -74,6 +96,43 @@ namespace Game
ASSET_TYPE_INVALID = -1, ASSET_TYPE_INVALID = -1,
}; };
enum materialSurfType_t
{
SURF_TYPE_DEFAULT,
SURF_TYPE_BARK,
SURF_TYPE_BRICK,
SURF_TYPE_CARPET,
SURF_TYPE_CLOTH,
SURF_TYPE_CONCRETE,
SURF_TYPE_DIRT,
SURF_TYPE_FLESH,
SURF_TYPE_FOLIAGE,
SURF_TYPE_GLASS,
SURF_TYPE_GRASS,
SURF_TYPE_GRAVEL,
SURF_TYPE_ICE,
SURF_TYPE_METAL,
SURF_TYPE_MUD,
SURF_TYPE_PAPER,
SURF_TYPE_PLASTER,
SURF_TYPE_ROCK,
SURF_TYPE_SAND,
SURF_TYPE_SNOW,
SURF_TYPE_WATER,
SURF_TYPE_WOOD,
SURF_TYPE_ASPHALT,
SURF_TYPE_CERAMIC,
SURF_TYPE_PLASTIC,
SURF_TYPE_RUBBER,
SURF_TYPE_CUSHION,
SURF_TYPE_FRUIT,
SURF_TYPE_PAINTED_METAL,
SURF_TYPE_RIOT_SHIELD,
SURF_TYPE_SLUSH,
SURF_TYPE_COUNT
};
enum dvar_flag : unsigned __int16 enum dvar_flag : unsigned __int16
{ {
DVAR_NONE = 0x0, // No flags DVAR_NONE = 0x0, // No flags
@ -209,6 +268,17 @@ namespace Game
FL_MOVER_SLIDE = 0x8000000 FL_MOVER_SLIDE = 0x8000000
}; };
enum ClassNum : unsigned int
{
CLASS_NUM_ENTITY = 0x0,
CLASS_NUM_HUDELEM = 0x1,
CLASS_NUM_PATHNODE = 0x2,
CLASS_NUM_VEHICLENODE = 0x3,
CLASS_NUM_VEHTRACK_SEGMENT = 0x4,
CLASS_NUM_FXENTITY = 0x5,
CLASS_NUM_COUNT = 0x6,
};
typedef enum typedef enum
{ {
HITLOC_NONE, HITLOC_NONE,
@ -1457,6 +1527,8 @@ namespace Game
CMD_BUTTON_ACTIVATE = 0x8, CMD_BUTTON_ACTIVATE = 0x8,
CMD_BUTTON_RELOAD = 0x10, CMD_BUTTON_RELOAD = 0x10,
CMD_BUTTON_USE_RELOAD = 0x20, CMD_BUTTON_USE_RELOAD = 0x20,
CMD_BUTTON_LEAN_LEFT = 0x40,
CMD_BUTTON_LEAN_RIGHT = 0x80,
CMD_BUTTON_PRONE = 0x100, CMD_BUTTON_PRONE = 0x100,
CMD_BUTTON_CROUCH = 0x200, CMD_BUTTON_CROUCH = 0x200,
CMD_BUTTON_UP = 0x400, CMD_BUTTON_UP = 0x400,
@ -1465,7 +1537,7 @@ namespace Game
CMD_BUTTON_BREATH = 0x2000, CMD_BUTTON_BREATH = 0x2000,
CMD_BUTTON_FRAG = 0x4000, CMD_BUTTON_FRAG = 0x4000,
CMD_BUTTON_OFFHAND_SECONDARY = 0x8000, CMD_BUTTON_OFFHAND_SECONDARY = 0x8000,
CMD_BUTTON_THROW = 0x80000, CMD_BUTTON_THROW = 0x80000
}; };
#pragma pack(push, 4) #pragma pack(push, 4)
@ -4999,6 +5071,21 @@ namespace Game
int dataCount; int dataCount;
} gameState; } gameState;
struct HunkUser
{
HunkUser* current;
HunkUser* next;
int maxSize;
int end;
int pos;
const char* name;
bool fixed;
int type;
char buf[1];
};
static_assert(sizeof(HunkUser) == 36);
struct VariableStackBuffer struct VariableStackBuffer
{ {
const char *pos; const char *pos;
@ -5094,6 +5181,40 @@ namespace Game
VariableValue stack[2048]; VariableValue stack[2048];
}; };
struct scrVarPub_t
{
const char* fieldBuffer;
unsigned __int16 canonicalStrCount;
bool developer_script;
bool evaluate;
const char* error_message;
int error_index;
int time;
int timeArrayId;
int pauseArrayId;
int notifyArrayId;
int objectStackId;
int levelId;
int gameId;
int animId;
int freeEntList;
int tempVariable;
int numScriptValues[2];
bool bInited;
unsigned __int16 savecount;
unsigned __int16 savecountMark;
int checksum;
int entId;
int entFieldName;
HunkUser* programHunkUser;
const char* programBuffer;
const char* endScriptBuffer;
unsigned __int16 saveIdMap[36864];
unsigned __int16 saveIdMapRev[36864];
};
static_assert(sizeof(scrVarPub_t) == 0x24060);
enum UILocalVarType enum UILocalVarType
{ {
UILOCALVAR_INT = 0x0, UILOCALVAR_INT = 0x0,
@ -5503,15 +5624,25 @@ namespace Game
CON_CONNECTED = 0x2 CON_CONNECTED = 0x2
} clientConnected_t; } clientConnected_t;
typedef enum
{
VISIONSET_NORMAL,
VISIONSET_NIGHT,
VISIONSET_MISSILECAM,
VISIONSET_THERMAL,
VISIONSET_PAIN,
VISIONSETCOUNT
} visionSetMode_t;
typedef struct gclient_s typedef struct gclient_s
{ {
playerState_s ps; playerState_s ps;
sessionState_t sessionState; // 12572 sessionState_t sessionState; // 12572
char pad0[40]; unsigned char __pad0[40];
clientConnected_t connected; // 12616 clientConnected_t connected; // 12616
char pad1[144]; unsigned char __pad1[144];
unsigned int team; // 12764 team_t team; // 12764
char pad2[436]; unsigned char __pad2[436];
int flags; // 13204 int flags; // 13204
int spectatorClient; int spectatorClient;
int lastCmdTime; int lastCmdTime;
@ -5519,7 +5650,10 @@ namespace Game
int oldbuttons; // 13220 int oldbuttons; // 13220
int latched_buttons; // 13224 int latched_buttons; // 13224
int buttonsSinceLastFrame; // 13228 int buttonsSinceLastFrame; // 13228
char pad3[700]; // 13232 unsigned char __pad3[324]; // 13232
int visionDuration[5];
char visionName[5][64];
unsigned char __pad4[36];
} gclient_t; } gclient_t;
static_assert(sizeof(gclient_t) == 13932); static_assert(sizeof(gclient_t) == 13932);
@ -5619,6 +5753,53 @@ namespace Game
static_assert(sizeof(gentity_s) == 0x274); static_assert(sizeof(gentity_s) == 0x274);
enum $1C4253065710F064DA9E4D59ED6EC544
{
ENTFIELD_ENTITY = 0x0,
ENTFIELD_SENTIENT = 0x2000,
ENTFIELD_ACTOR = 0x4000,
ENTFIELD_CLIENT = 0x6000,
ENTFIELD_VEHICLE = 0x8000,
ENTFIELD_MASK = 0xE000,
};
enum fieldtype_t
{
F_INT = 0x0,
F_SHORT = 0x1,
F_BYTE = 0x2,
F_FLOAT = 0x3,
F_CSTRING = 0x4,
F_STRING = 0x5,
F_VECTOR = 0x6,
F_ENTITY = 0x7,
F_ENTHANDLE = 0x8,
F_ANGLES_YAW = 0x9,
F_OBJECT = 0xA,
F_MODEL = 0xB,
};
struct ent_field_t
{
const char* name;
int ofs;
fieldtype_t type;
void(__cdecl * setter)(gentity_s*, int);
void(__cdecl * getter)(gentity_s*, int);
};
struct client_fields_s
{
const char* name;
int ofs;
fieldtype_t type;
void(__cdecl * setter)(gclient_s*, const client_fields_s*);
void(__cdecl * getter)(gclient_s*, const client_fields_s*);
};
typedef void(__cdecl * ScriptCallbackEnt)(gentity_s*, int);
typedef void(__cdecl * ScriptCallbackClient)(gclient_s*, const client_fields_s*);
struct lockonFireParms struct lockonFireParms
{ {
bool lockon; bool lockon;
@ -5661,7 +5842,7 @@ namespace Game
int pureAuthentic; // 135896 int pureAuthentic; // 135896
char __pad7[133138]; // 135900 char __pad7[133138]; // 135900
short scriptID; // 269038 short scriptID; // 269038
int isBot; // 269040 int bIsTestClient; // 269040
int serverID; // 269044 int serverID; // 269044
char __pad8[9224]; // 269048 char __pad8[9224]; // 269048
unsigned __int64 steamID; // 278272 unsigned __int64 steamID; // 278272
@ -7196,6 +7377,149 @@ namespace Game
TempPriority tempPriority; TempPriority tempPriority;
}; };
struct trigger_info_t
{
unsigned __int16 entnum;
unsigned __int16 otherEntnum;
int useCount;
int otherUseCount;
};
struct com_parse_mark_t
{
int lines;
const char* text;
int ungetToken;
int backup_lines;
const char* backup_text;
};
struct cached_tag_mat_t
{
int time;
int entnum;
unsigned __int16 name;
float tagMat[4][3];
};
struct Turret
{
bool inuse;
int flags;
int fireTime;
float arcmin[2];
float arcmax[2];
float dropPitch;
int stance;
int prevStance;
int fireSndDelay;
float userOrigin[3];
float playerSpread;
int state;
EntHandle target;
float targetOffset[3];
EntHandle manualTarget;
float manualTargetOffset[3];
int targetTime;
int stateChangeTime;
int modeChangeTime;
float maxRangeSquared;
int prevTargetIndex;
team_t eTeam;
int convergenceTime[2];
float targetPos[3];
float missOffsetNormalized[3];
float scanSpeed;
float scanDecelYaw;
int scanPauseTime;
bool triggerDown;
float heatLevel;
int heatPenaltyEndTime;
float barrelRollRate;
int autoRotationStopDelay;
int lastAutoRotationRequestTime;
unsigned __int8 fireSnd;
unsigned __int8 fireSndPlayer;
unsigned __int8 stopSnd;
unsigned __int8 stopSndPlayer;
unsigned __int8 scanSnd;
};
static_assert(sizeof(Turret) == 0xC4);
struct level_locals_t
{
gclient_s* clients;
gentity_s* gentities;
int num_entities;
gentity_s* firstFreeEnt;
gentity_s* lastFreeEnt;
Turret* turrets;
void* logFile;
int initializing;
int clientIsSpawning;
objective_t objectives[32];
int maxclients;
int framenum;
int time;
int previousTime;
int frametime;
int startTime;
int teamScores[4];
int lastTeammateHealthTime;
int bUpdateScoresForIntermission;
bool teamHasRadar[4];
bool teamRadarBlocked[4];
int manualNameChange;
int numConnectedClients;
int sortedClients[18];
char voteString[1024];
char voteDisplayString[1024];
int voteTime;
int voteExecuteTime;
int voteYes;
int voteNo;
int numVotingClients;
SpawnVar spawnVar;
int savepersist;
EntHandle droppedWeaponCue[32];
float fFogOpaqueDist;
float fFogOpaqueDistSqrd;
int currentPlayerClone;
trigger_info_t pendingTriggerList[256];
trigger_info_t currentTriggerList[256];
int pendingTriggerListSize;
int currentTriggerListSize;
int finished;
int bPlayerIgnoreRadiusDamage;
int bPlayerIgnoreRadiusDamageLatched;
int registerWeapons;
int bRegisterItems;
int currentEntityThink;
void* openScriptIOFileHandles[1];
char* openScriptIOFileBuffers[1];
com_parse_mark_t currentScriptIOLineMark[1];
cached_tag_mat_t cachedTagMat;
int scriptPrintChannel;
float compassMapUpperLeft[2];
float compassMapWorldSize[2];
float compassNorth[2];
void* vehicles;
int hudElemLastAssignedSoundID;
};
static_assert(sizeof(level_locals_t) == 0x2F78);
struct WinMouseVars_t
{
int oldButtonState;
tagPOINT oldPos;
bool mouseActive;
bool mouseInitialized;
};
static_assert(sizeof(WinMouseVars_t) == 0x10);
#pragma endregion #pragma endregion
#ifndef IDA #ifndef IDA

View File

@ -55,7 +55,7 @@ BOOL APIENTRY DllMain(HMODULE /*hModule*/, DWORD ul_reason_for_call, LPVOID /*l
Steam::Proxy::RunMod(); Steam::Proxy::RunMod();
// Ensure we're working with our desired binary // Ensure we're working with our desired binary
char* _module = reinterpret_cast<char*>(0x400000); auto* _module = reinterpret_cast<char*>(0x400000);
auto hash1 = Utils::Cryptography::JenkinsOneAtATime::Compute(_module + 0x1000, 0x2D531F); // .text auto hash1 = Utils::Cryptography::JenkinsOneAtATime::Compute(_module + 0x1000, 0x2D531F); // .text
auto hash2 = Utils::Cryptography::JenkinsOneAtATime::Compute(_module + 0x2D75FC, 0xBDA04); // .rdata auto hash2 = Utils::Cryptography::JenkinsOneAtATime::Compute(_module + 0x2D75FC, 0xBDA04); // .rdata
if ((hash1 != 0x54684DBE if ((hash1 != 0x54684DBE
@ -67,17 +67,6 @@ BOOL APIENTRY DllMain(HMODULE /*hModule*/, DWORD ul_reason_for_call, LPVOID /*l
return FALSE; return FALSE;
} }
#ifndef DISABLE_ANTICHEAT
[]()
{
if (!Components::Dedicated::IsEnabled() && !Components::Loader::IsPerformingUnitTests())
{
Components::AntiCheat::ProtectProcess();
Components::AntiCheat::PatchThreadCreation();
}
}();
#endif
DWORD oldProtect; DWORD oldProtect;
VirtualProtect(_module + 0x1000, 0x2D6000, PAGE_EXECUTE_READ, &oldProtect); // Protect the .text segment VirtualProtect(_module + 0x1000, 0x2D6000, PAGE_EXECUTE_READ, &oldProtect); // Protect the .text segment
@ -88,18 +77,6 @@ BOOL APIENTRY DllMain(HMODULE /*hModule*/, DWORD ul_reason_for_call, LPVOID /*l
{ {
Main::Uninitialize(); Main::Uninitialize();
} }
else if (ul_reason_for_call == DLL_THREAD_ATTACH)
{
#ifndef DISABLE_ANTICHEAT
[]()
{
if (!Components::Dedicated::IsEnabled() && !Components::Loader::IsPerformingUnitTests())
{
Components::AntiCheat::VerifyThreadIntegrity();
}
}();
#endif
}
return TRUE; return TRUE;
} }

View File

@ -90,17 +90,6 @@
#undef min #undef min
#endif #endif
// VMProtect
// #define USE_VMP
#ifdef USE_VMP
#include <VMProtect/VMProtectSDK.h>
#define __VMProtectBeginUltra VMProtectBeginUltra
#define __VMProtectEnd VMProtectEnd()
#else
#define __VMProtectBeginUltra
#define __VMProtectEnd
#endif
// Protobuf // Protobuf
#include "proto/session.pb.h" #include "proto/session.pb.h"
#include "proto/party.pb.h" #include "proto/party.pb.h"

View File

@ -23,16 +23,24 @@ namespace Utils
return result; return result;
} }
std::string ToLower(std::string input) std::string ToLower(std::string text)
{ {
std::transform(input.begin(), input.end(), input.begin(), ::tolower); std::transform(text.begin(), text.end(), text.begin(), [](const unsigned char input)
return input; {
return static_cast<char>(std::tolower(input));
});
return text;
} }
std::string ToUpper(std::string input) std::string ToUpper(std::string text)
{ {
std::transform(input.begin(), input.end(), input.begin(), ::toupper); std::transform(text.begin(), text.end(), text.begin(), [](const unsigned char input)
return input; {
return static_cast<char>(std::toupper(input));
});
return text;
} }
std::string DumpHex(const std::string& data, const std::string& separator) std::string DumpHex(const std::string& data, const std::string& separator)
@ -89,12 +97,13 @@ namespace Utils
bool StartsWith(const std::string& haystack, const std::string& needle) bool StartsWith(const std::string& haystack, const std::string& needle)
{ {
return (haystack.size() >= needle.size() && haystack.substr(0, needle.size()) == needle); return haystack.find(needle) == 0; // If the pos of the first found char is 0, string starts with 'needle'
} }
bool EndsWith(const std::string& haystack, const std::string& needle) bool EndsWith(const std::string& haystack, const std::string& needle)
{ {
return (haystack.size() >= needle.size() && haystack.substr(haystack.size() - needle.size()) == needle); if (needle.size() > haystack.size()) return false;
return std::equal(needle.rbegin(), needle.rend(), haystack.rbegin());
} }
int IsSpace(int c) int IsSpace(int c)

View File

@ -11,7 +11,7 @@ namespace Utils
static_assert(Buffers != 0 && MinBufferSize != 0, "Buffers and MinBufferSize mustn't be 0"); static_assert(Buffers != 0 && MinBufferSize != 0, "Buffers and MinBufferSize mustn't be 0");
VAProvider() : currentBuffer(0) {} VAProvider() : currentBuffer(0) {}
~VAProvider() {} ~VAProvider() = default;
const char* get(const char* format, va_list ap) const char* get(const char* format, va_list ap)
{ {
@ -25,7 +25,7 @@ namespace Utils
while (true) while (true)
{ {
int res = vsnprintf_s(entry->buffer, entry->size, _TRUNCATE, format, ap); const auto res = _vsnprintf_s(entry->buffer, entry->size, _TRUNCATE, format, ap);
if (res > 0) break; // Success if (res > 0) break; // Success
if (res == 0) return ""; // Error if (res == 0) return ""; // Error
@ -75,12 +75,13 @@ namespace Utils
const char *VA(const char *fmt, ...); const char *VA(const char *fmt, ...);
int IsSpace(int c); int IsSpace(int c);
std::string ToLower(std::string input); std::string ToLower(std::string text);
std::string ToUpper(std::string input); std::string ToUpper(std::string text);
bool EndsWith(const std::string& haystack, const std::string& needle);
std::vector<std::string> Split(const std::string& str, const char delim); std::vector<std::string> Split(const std::string& str, const char delim);
void Replace(std::string& string, const std::string& find, const std::string& replace); void Replace(std::string& string, const std::string& find, const std::string& replace);
bool StartsWith(const std::string& haystack, const std::string& needle); bool StartsWith(const std::string& haystack, const std::string& needle);
bool EndsWith(const std::string& haystack, const std::string& needle);
std::string& LTrim(std::string& str); std::string& LTrim(std::string& str);
std::string& RTrim(std::string& str); std::string& RTrim(std::string& str);
std::string& Trim(std::string& str); std::string& Trim(std::string& str);

View File

@ -123,10 +123,6 @@ namespace Utils
void SafeShellExecute(HWND hwnd, LPCSTR lpOperation, LPCSTR lpFile, LPCSTR lpParameters, LPCSTR lpDirectory, INT nShowCmd) void SafeShellExecute(HWND hwnd, LPCSTR lpOperation, LPCSTR lpFile, LPCSTR lpParameters, LPCSTR lpDirectory, INT nShowCmd)
{ {
#ifndef DISABLE_ANTICHEAT
Components::AntiCheat::LibUnlocker _;
#endif
[=]() [=]()
{ {
__try __try