Merge pull request #599 from diamante0018/fix/fps-issues

[Threading]: Removing this was not a good idea
This commit is contained in:
Edo 2022-11-26 15:58:30 +00:00 committed by GitHub
commit 62c5211a94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 191 additions and 3 deletions

View File

@ -74,6 +74,7 @@ namespace Components
Loader::Register(new Gametypes());
Loader::Register(new Materials());
Loader::Register(new Scheduler());
Loader::Register(new Threading());
Loader::Register(new CardTitles());
Loader::Register(new FileSystem());
Loader::Register(new ModelSurfs());

View File

@ -106,6 +106,7 @@ namespace Components
#include "Modules/Gametypes.hpp"
#include "Modules/Materials.hpp"
#include "Modules/Singleton.hpp"
#include "Modules/Threading.hpp"
#include "Modules/CardTitles.hpp"
#include "Modules/FileSystem.hpp"
#include "Modules/ModelSurfs.hpp"

View File

@ -332,9 +332,6 @@ namespace Components
// Fix crash as nullptr goes unchecked
Utils::Hook(0x437CAD, QuickPatch::SND_GetAliasOffset_Stub, HOOK_JUMP).install()->quick();
// Make VA thread safe
Utils::Hook(0x4785B0, Utils::String::VA, HOOK_JUMP).install()->quick();
// protocol version (workaround for hacks)
Utils::Hook::Set<int>(0x4FB501, PROTOCOL);

View File

@ -0,0 +1,173 @@
#include <STDInclude.hpp>
namespace Components
{
namespace FrameTime
{
void NetSleep(int mSec)
{
if (mSec < 0) mSec = 0;
fd_set fdr;
FD_ZERO(&fdr);
auto highestFd = INVALID_SOCKET;
if (*Game::ip_socket != INVALID_SOCKET)
{
FD_SET(*Game::ip_socket, &fdr);
highestFd = *Game::ip_socket;
}
if (highestFd == INVALID_SOCKET)
{
// windows ain't happy when select is called without valid FDs
std::this_thread::sleep_for(std::chrono::milliseconds(mSec));
return;
}
timeval timeout;
timeout.tv_sec = mSec / 1000;
timeout.tv_usec = (mSec % 1000) * 1000;
const auto retVal = ::select(highestFd + 1, &fdr, nullptr, nullptr, &timeout);
if (retVal == SOCKET_ERROR)
{
Logger::Warning(Game::CON_CHANNEL_SYSTEM, "WinAPI: select failed: {}\n", Game::NET_ErrorString());
}
else if (retVal > 0)
{
if (Dedicated::IsRunning())
{
Game::Com_ServerPacketEvent();
}
else
{
Game::Com_ClientPacketEvent();
}
}
}
void SVFrameWaitFunc()
{
if (*Game::sv_timeResidual < 50)
{
NetSleep(50 - *Game::sv_timeResidual);
}
}
void __declspec(naked) SVFrameWaitStub()
{
__asm
{
pushad
call SVFrameWaitFunc
popad
push 4CD420h
ret
}
}
int ComTimeVal(int minMsec)
{
const auto timeVal = Game::Sys_Milliseconds() - *Game::com_frameTime;
return (timeVal >= minMsec ? 0 : minMsec - timeVal);
}
int ComFrameWait(int minMsec)
{
do
{
const auto timeVal = ComTimeVal(minMsec);
NetSleep(timeVal < 1 ? 0 : timeVal - 1);
} while (ComTimeVal(minMsec));
const auto lastTime = *Game::com_frameTime;
Game::Com_EventLoop();
*Game::com_frameTime = Game::Sys_Milliseconds();
return *Game::com_frameTime - lastTime;
}
void __declspec(naked) ComFrameWaitStub()
{
__asm
{
push ecx
pushad
push edi
call ComFrameWait
add esp, 4
mov [esp + 20h], eax
popad
pop eax
mov ecx, eax
mov edx, 1AD7934h // com_sv_running
cmp byte ptr [edx + 10h], 0
push 47DDC1h
ret
}
}
}
__declspec(naked) void Threading::FrameEpilogueStub()
{
__asm
{
pop edi
pop esi
pop ebx
mov esp, ebp
pop ebp
retn
}
}
__declspec(naked) void Threading::PacketEventStub()
{
__asm
{
mov eax, 49F0B0h
call eax
mov eax, 458160h
jmp eax
}
}
Threading::Threading()
{
// remove starting of server thread from Com_Init_Try_Block_Function
Utils::Hook::Nop(0x60BEC0, 5);
// make server thread function jump to per-frame stuff
Utils::Hook(0x627049, 0x6271CE, HOOK_JUMP).install()->quick();
// make SV_WaitServer insta-return
Utils::Hook::Set<std::uint8_t>(0x4256F0, 0xC3);
// dvar setting function, unknown stuff related to server thread sync
Utils::Hook::Set<std::uint8_t>(0x647781, 0xEB);
Utils::Hook(0x627695, 0x627040, HOOK_CALL).install()->quick();
Utils::Hook(0x43D1C7, PacketEventStub, HOOK_JUMP).install()->quick();
Utils::Hook(0x6272E3, FrameEpilogueStub, HOOK_JUMP).install()->quick();
// make VA thread safe
Utils::Hook(0x4785B0, Utils::String::VA, HOOK_JUMP).install()->quick();
if (Dedicated::IsEnabled())
{
Utils::Hook(0x4BAAAD, FrameTime::SVFrameWaitStub, HOOK_CALL).install()->quick();
}
else
{
Utils::Hook(0x47DD80, FrameTime::ComFrameWaitStub, HOOK_JUMP).install()->quick();
}
}
}

View File

@ -0,0 +1,14 @@
#pragma once
namespace Components
{
class Threading : public Component
{
public:
Threading();
private:
static void FrameEpilogueStub();
static void PacketEventStub();
};
}

View File

@ -20,6 +20,7 @@ namespace Game
SV_FindClientByAddress_t SV_FindClientByAddress = SV_FindClientByAddress_t(0x44F450);
int* svs_time = reinterpret_cast<int*>(0x31D9384);
int* sv_timeResidual = reinterpret_cast<int*>(0x2089E14);
int* sv_serverId_value = reinterpret_cast<int*>(0x2089DC0);
int* svs_clientCount = reinterpret_cast<int*>(0x31D938C);
client_t* svs_clients = reinterpret_cast<client_t*>(0x31D9390);

View File

@ -53,6 +53,7 @@ namespace Game
constexpr auto MAX_STATPACKETS = 7;
extern int* svs_time;
extern int* sv_timeResidual;
extern int* sv_serverId_value;
extern int* svs_clientCount;
extern client_t* svs_clients;