Skip to content

Commit 43630d3

Browse files
committed
Add SharedUtil::TryGetProcAddress, use it in various places, and address misc issues exposed during implementation
1 parent 2daba7b commit 43630d3

14 files changed

+157
-62
lines changed

Client/core/CCrashDumpWriter.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*****************************************************************************/
1212

1313
#include "StdInc.h"
14+
#include <SharedUtil.Misc.h>
1415
#include <game/CGame.h>
1516
#include <game/CPools.h>
1617
#include <game/CRenderWare.h>
@@ -903,11 +904,7 @@ void CCrashDumpWriter::DumpMiniDump(_EXCEPTION_POINTERS* pException, CExceptionI
903904
if (hDll != nullptr)
904905
{
905906
// Grab the MiniDumpWriteDump proc address
906-
auto pDump = reinterpret_cast<MINIDUMPWRITEDUMP>(GetProcAddress(hDll, "MiniDumpWriteDump"));
907-
if (pDump == nullptr)
908-
AddReportLog(9202, "CCrashDumpWriter::DumpMiniDump - Could not find MiniDumpWriteDump");
909-
910-
if (pDump != nullptr)
907+
if (MINIDUMPWRITEDUMP pDump = nullptr; SharedUtil::TryGetProcAddress(hDll, "MiniDumpWriteDump", pDump))
911908
{
912909
// Create the file
913910
HANDLE hFile = CreateFileA(CalcMTASAPath("mta\\core.dmp"), GENERIC_WRITE, FILE_SHARE_WRITE, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
@@ -1090,6 +1087,10 @@ void CCrashDumpWriter::DumpMiniDump(_EXCEPTION_POINTERS* pException, CExceptionI
10901087
}
10911088
}
10921089
}
1090+
else
1091+
{
1092+
AddReportLog(9202, "CCrashDumpWriter::DumpMiniDump - Could not find MiniDumpWriteDump");
1093+
}
10931094

10941095
// Free the DLL again
10951096
FreeLibrary(hDll);

Client/core/CExceptionInformation_Impl.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*****************************************************************************/
1111

1212
#include "StdInc.h"
13+
#include <SharedUtil.Misc.h>
1314

1415
#define MAX_MODULE_PATH 512
1516

@@ -149,16 +150,16 @@ bool CExceptionInformation_Impl::GetModule(void* pQueryAddress, char* szOutputBu
149150
* See if we're able to use GetModuleHandleExA. According to Microsoft,
150151
* this API is only available on Windows XP and Vista.
151152
*/
152-
_pfnGetModuleHandleExA pfnGetModuleHandleExA = (_pfnGetModuleHandleExA)GetProcAddress(hKern32, "GetModuleHandleExA");
153+
_pfnGetModuleHandleExA pfnGetModuleHandleExA = nullptr;
154+
if (!SharedUtil::TryGetProcAddress(hKern32, "GetModuleHandleExA", pfnGetModuleHandleExA))
155+
return false;
153156

154157
/*
155158
* TODO: Possibly use our own code to do this for other systems.
156159
* It is possible to enumerate all modules and get their starting/ending
157160
* offsets, so it would just be a simple comparison of addresses to
158161
* do this...
159162
*/
160-
if (NULL == pfnGetModuleHandleExA)
161-
return false;
162163

163164
if (0 == pfnGetModuleHandleExA(0x00000004 /*GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS*/, (LPCSTR)pQueryAddress, &hModule))
164165
{
@@ -186,3 +187,4 @@ bool CExceptionInformation_Impl::GetModule(void* pQueryAddress, char* szOutputBu
186187

187188
return false;
188189
}
190+

Client/core/CModManager.cpp

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*****************************************************************************/
1111

1212
#include "StdInc.h"
13+
#include <SharedUtil.Misc.h>
1314
#include "CModManager.h"
1415
#define DECLARE_PROFILER_SECTION_CModManager
1516
#include "profiler/SharedUtil.Profiler.h"
@@ -55,30 +56,39 @@ bool CModManager::TriggerCommand(const char* commandName, size_t commandNameLeng
5556

5657
void CModManager::DoPulsePreFrame()
5758
{
59+
TIMING_GRAPH("+DoPulsePreFrame");
60+
CCore::GetSingleton().GetFPSLimiter()->OnFrameStart(); // Prepare FPS limiting for this frame
61+
5862
if (m_client)
5963
{
6064
m_client->PreFrameExecutionHandler();
6165
}
66+
TIMING_GRAPH("-DoPulsePreFrame");
6267
}
6368

6469
void CModManager::DoPulsePreHUDRender(bool bDidUnminimize, bool bDidRecreateRenderTargets)
6570
{
71+
TIMING_GRAPH("+DoPulsePreHUDRender");
6672
if (m_client)
6773
{
6874
m_client->PreHUDRenderExecutionHandler(bDidUnminimize, bDidRecreateRenderTargets);
6975
}
76+
TIMING_GRAPH("-DoPulsePreHUDRender");
7077
}
7178

7279
void CModManager::DoPulsePostFrame()
7380
{
74-
if (m_state == State::PendingStart)
75-
{
76-
Start();
77-
}
78-
else if (m_state == State::PendingStop)
81+
auto handleStateChange = [&]()
7982
{
80-
Stop();
81-
}
83+
if (m_state == State::PendingStart)
84+
Start();
85+
else if (m_state == State::PendingStop)
86+
Stop();
87+
};
88+
89+
TIMING_GRAPH("+DoPulsePostFrame");
90+
91+
handleStateChange(); // Handle state changes before pulse
8292

8393
if (m_client)
8494
{
@@ -89,20 +99,15 @@ void CModManager::DoPulsePostFrame()
8999
CCore::GetSingleton().GetNetwork()->DoPulse();
90100
}
91101

92-
// Make sure frame rate limit gets applied
93-
if (m_client != nullptr)
94-
CCore::GetSingleton().EnsureFrameRateLimitApplied(); // Catch missed frames
95-
else
96-
CCore::GetSingleton().ApplyFrameRateLimit(); // Limit when not connected
102+
CCore::GetSingleton().DoReliablePulse(); // Do reliable pulse
97103

98-
if (m_state == State::PendingStart)
99-
{
100-
Start();
101-
}
102-
else if (m_state == State::PendingStop)
103-
{
104-
Stop();
105-
}
104+
handleStateChange(); // Handle state changes after pulse
105+
106+
// TODO: ENSURE "CModManager::DoPulsePostFrame" IS THE LAST THING BEFORE THE FRAME ENDS
107+
CCore::GetSingleton().GetFPSLimiter()->OnFrameEnd(); // Apply FPS limiting
108+
109+
TIMING_GRAPH("-DoPulsePostFrame");
110+
TIMING_GRAPH("");
106111
}
107112

108113
bool CModManager::Load(const char* arguments)
@@ -182,16 +187,16 @@ bool CModManager::TryStart()
182187
return false;
183188
}
184189

185-
CClientBase*(__cdecl * InitClient)() = reinterpret_cast<decltype(InitClient)>(GetProcAddress(library, "InitClient"));
186-
187-
if (InitClient == nullptr)
190+
using InitClientFn = CClientBase* (__cdecl*)();
191+
InitClientFn initClient = nullptr;
192+
if (!SharedUtil::TryGetProcAddress(library, "InitClient", initClient))
188193
{
189194
CCore::GetSingleton().GetConsole()->Printf("Unable to initialize deathmatch's DLL (missing init)");
190195
FreeLibrary(library);
191196
return false;
192197
}
193198

194-
CClientBase* client = InitClient();
199+
CClientBase* client = initClient();
195200

196201
if (client == nullptr || client->ClientInitialize(m_arguments.c_str(), CCore::GetSingletonPtr()) != 0)
197202
{
@@ -283,3 +288,4 @@ void CModManager::TryStop()
283288
CLocalGUI::GetSingleton().GetMainMenu()->SetIsIngame(false);
284289
CLocalGUI::GetSingleton().GetMainMenu()->SetVisible(true, false);
285290
}
291+

Client/core/CModuleLoader.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*****************************************************************************/
1111

1212
#include "StdInc.h"
13+
#include <SharedUtil.Misc.h>
1314

1415
using std::string;
1516

@@ -62,14 +63,14 @@ PVOID CModuleLoader::GetFunctionPointer(const string& FunctionName)
6263
{
6364
if (m_bStatus)
6465
{
65-
FARPROC fpProcAddr;
66-
67-
fpProcAddr = GetProcAddress(m_hLoadedModule, FunctionName.c_str());
66+
FARPROC fpProcAddr = nullptr;
67+
if (!SharedUtil::TryGetProcAddress(m_hLoadedModule, FunctionName.c_str(), fpProcAddr))
68+
return nullptr;
6869

6970
return static_cast<PVOID>(fpProcAddr);
7071
}
7172
else
72-
return NULL;
73+
return nullptr;
7374
}
7475

7576
const SString& CModuleLoader::GetLastErrorMessage() const

Client/core/CSteamClient.cpp

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <WinTrust.h>
1616
#include <SoftPub.h>
1717
#include <Psapi.h>
18+
#include <SharedUtil.Misc.h>
1819

1920
#define STEAM_GTASA_APP_ID "12120"
2021

@@ -46,6 +47,33 @@ struct HandleDeleter
4647

4748
using HandleScope = std::unique_ptr<std::remove_pointer_t<HANDLE>, HandleDeleter>;
4849

50+
namespace
51+
{
52+
DWORD GetProcessBaseName(HANDLE process, LPWSTR buffer, DWORD bufferLength)
53+
{
54+
using ModuleBaseNameFn = DWORD(WINAPI*)(HANDLE, HMODULE, LPWSTR, DWORD);
55+
ModuleBaseNameFn moduleBaseNameFn = nullptr;
56+
57+
if (HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll"); kernel32 != nullptr)
58+
{
59+
if (SharedUtil::TryGetProcAddress(kernel32, "K32GetModuleBaseNameW", moduleBaseNameFn) ||
60+
SharedUtil::TryGetProcAddress(kernel32, "GetModuleBaseNameW", moduleBaseNameFn))
61+
{
62+
return moduleBaseNameFn(process, nullptr, buffer, bufferLength);
63+
}
64+
}
65+
66+
HMODULE psapi = GetModuleHandleW(L"psapi.dll");
67+
if (psapi == nullptr)
68+
psapi = LoadLibraryW(L"psapi.dll");
69+
70+
if (psapi != nullptr && SharedUtil::TryGetProcAddress(psapi, "GetModuleBaseNameW", moduleBaseNameFn))
71+
return moduleBaseNameFn(process, nullptr, buffer, bufferLength);
72+
73+
return 0;
74+
}
75+
} // namespace
76+
4977
struct SignerInfo
5078
{
5179
SignerInfo(HCRYPTMSG Msg)
@@ -325,7 +353,7 @@ static bool IsSteamProcess(DWORD pid)
325353

326354
wchar_t processName[MAX_PATH];
327355

328-
if (!GetModuleBaseNameW(process, nullptr, processName, sizeof(processName) / sizeof(wchar_t)))
356+
if (GetProcessBaseName(process, processName, static_cast<DWORD>(sizeof(processName) / sizeof(processName[0]))) == 0)
329357
return false;
330358

331359
if (wcsicmp(processName, L"steam.exe") != 0)
@@ -380,7 +408,10 @@ bool CSteamClient::Load()
380408
static auto pAddDllDirectory = ([]() -> decltype(&AddDllDirectory) {
381409
if (const HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll"); kernel32 != nullptr)
382410
{
383-
return reinterpret_cast<decltype(&AddDllDirectory)>(static_cast<void*>(GetProcAddress(kernel32, "AddDllDirectory")));
411+
decltype(&AddDllDirectory) addDllDirectory = nullptr;
412+
if (!SharedUtil::TryGetProcAddress(kernel32, "AddDllDirectory", addDllDirectory))
413+
return nullptr;
414+
return addDllDirectory;
384415
}
385416

386417
return nullptr;
@@ -439,10 +470,8 @@ bool CSteamClient::Load()
439470
}
440471

441472
releaseLibraryLock.reset();
442-
443-
Native::CreateInterface = reinterpret_cast<decltype(Native::CreateInterface)>(static_cast<void*>(GetProcAddress(dll, "CreateInterface")));
444473

445-
if (!Native::CreateInterface)
474+
if (!SharedUtil::TryGetProcAddress(dll, "CreateInterface", Native::CreateInterface))
446475
{
447476
FreeLibrary(dll);
448477
return false;

Client/core/CrashHandler.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "StdInc.h"
1717
#include "CrashHandler.h"
1818
#include <SharedUtil.Detours.h>
19+
#include <SharedUtil.Misc.h>
1920
#include <exception>
2021
#include <new>
2122
#include <process.h>
@@ -568,25 +569,27 @@ static void InstallSehHandler() noexcept
568569
return;
569570
}
570571

571-
auto setUnhandled = reinterpret_cast<decltype(&SetUnhandledExceptionFilter)>(GetProcAddress(kernelModule, "SetUnhandledExceptionFilter"));
572-
if (setUnhandled == nullptr)
572+
if (decltype(&SetUnhandledExceptionFilter) setUnhandled = nullptr;
573+
SharedUtil::TryGetProcAddress(kernelModule, "SetUnhandledExceptionFilter", setUnhandled))
573574
{
574-
SafeDebugOutput("CrashHandler: WARNING - Failed to resolve SetUnhandledExceptionFilter address\n");
575-
return;
576-
}
577-
578-
g_kernelSetUnhandledExceptionFilterTrampoline = setUnhandled;
575+
g_kernelSetUnhandledExceptionFilterTrampoline = setUnhandled;
579576

580-
if (SharedUtil::DetourFunction(g_kernelSetUnhandledExceptionFilterTrampoline, reinterpret_cast<void*>(RedirectedSetUnhandledExceptionFilter)))
581-
{
582-
g_pfnKernelSetUnhandledExceptionFilter.store(g_kernelSetUnhandledExceptionFilterTrampoline, std::memory_order_release);
583-
SafeDebugOutput("CrashHandler: SEH detour protection installed\n");
577+
if (SharedUtil::DetourFunction(g_kernelSetUnhandledExceptionFilterTrampoline, reinterpret_cast<void*>(RedirectedSetUnhandledExceptionFilter)))
578+
{
579+
g_pfnKernelSetUnhandledExceptionFilter.store(g_kernelSetUnhandledExceptionFilterTrampoline, std::memory_order_release);
580+
SafeDebugOutput("CrashHandler: SEH detour protection installed\n");
581+
}
582+
else
583+
{
584+
SafeDebugOutput("CrashHandler: WARNING - SEH detour protection FAILED (non-critical)\n");
585+
g_pfnKernelSetUnhandledExceptionFilter.store(nullptr, std::memory_order_release);
586+
g_kernelSetUnhandledExceptionFilterTrampoline = nullptr;
587+
}
584588
}
585589
else
586590
{
587-
SafeDebugOutput("CrashHandler: WARNING - SEH detour protection FAILED (non-critical)\n");
588-
g_pfnKernelSetUnhandledExceptionFilter.store(nullptr, std::memory_order_release);
589-
g_kernelSetUnhandledExceptionFilterTrampoline = nullptr;
591+
SafeDebugOutput("CrashHandler: WARNING - Failed to resolve SetUnhandledExceptionFilter address\n");
592+
return;
590593
}
591594
}
592595

Client/utils/ide2models/Main.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#pragma once
1313

1414
#include <cstdio>
15-
using namespace std;
1615

1716
int main(int iArgumentCount, char* szArgs[]);
1817
int Convert(FILE* pInput, FILE* pOutput);

Client/utils/map2xml/Main.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
#include "CConfig.h"
1515
#include <cstdio>
16-
using namespace std;
1716

1817
int main(int iArgumentCount, char* szArgs[]);
1918
int Convert(CConfig* pInput, FILE* pOutput);

Shared/publicsdk/Common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ extern "C"
3333
#include <vector>
3434
// Obviously i can't get us this so other includes will most likely be needed later on
3535

36-
using namespace std;
36+
using std::list;
37+
using std::vector;
3738

3839
#ifndef __COMMON_H
3940
#define __COMMON_H

Shared/publicsdk/extra/CLuaArguments.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extern "C"
2626
#include "CLuaArgument.h"
2727
#include <vector>
2828

29-
using namespace std;
29+
using std::vector;
3030

3131
class CLuaArguments
3232
{

0 commit comments

Comments
 (0)