Skip to content

Commit 8bedb47

Browse files
committed
steam api stuff
1 parent 364f674 commit 8bedb47

6 files changed

Lines changed: 108 additions & 5 deletions

File tree

MGSHDFix.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<ClCompile Include="src\resources\helper.cpp" />
6060
<ClCompile Include="src\fixes\water_reflections.cpp" />
6161
<ClCompile Include="src\features\texture_buffer_size.cpp" />
62+
<ClCompile Include="steamworks_api.cpp" />
6263
</ItemGroup>
6364
<ItemGroup>
6465
<ClInclude Include="external\safetyhook\include\safetyhook.hpp" />
@@ -88,6 +89,7 @@
8889
<ClInclude Include="src\resources\stdafx.h" />
8990
<ClInclude Include="src\fixes\water_reflections.hpp" />
9091
<ClInclude Include="src\features\texture_buffer_size.hpp" />
92+
<ClInclude Include="steamworks_api.hpp" />
9193
</ItemGroup>
9294
<ItemGroup>
9395
<None Include="MGSHDFix.ini" />

MGSHDFix.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@
9595
<ClCompile Include="src\warnings\mute_warning.cpp">
9696
<Filter>Warnings</Filter>
9797
</ClCompile>
98+
<ClCompile Include="steamworks_api.cpp">
99+
<Filter>APIs</Filter>
100+
</ClCompile>
98101
</ItemGroup>
99102
<ItemGroup>
100103
<ClInclude Include="src\fixes\line_scaling.hpp">
@@ -178,6 +181,9 @@
178181
<ClInclude Include="src\resources\d3d11_api.hpp">
179182
<Filter>APIs\Headers</Filter>
180183
</ClInclude>
184+
<ClInclude Include="steamworks_api.hpp">
185+
<Filter>APIs\Headers</Filter>
186+
</ClInclude>
181187
</ItemGroup>
182188
<ItemGroup>
183189
<Image Include="src\mgshdfix.ico">

src/dllmain.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ static void Init_LauncherConfigOverride()
13401340
void preCreateDXGIFactory()
13411341
{
13421342

1343-
1343+
13441344
}
13451345

13461346
void afterCreateDXGIFactory()
@@ -1368,10 +1368,10 @@ static void InitializeSubsystems()
13681368
{
13691369
//Initialization order (these systems initialize vars used by following ones.)
13701370
INITIALIZE(g_Logging.LogSysInfo()); //0
1371-
INITIALIZE(ASILoaderCompatibility::Check()); //1
1371+
INITIALIZE(ASILoaderCompatibility::Check()); //1
13721372
INITIALIZE(DetectGame()); //2
13731373
INITIALIZE(g_GameVars.Initialize()); //3
1374-
INITIALIZE(g_D3D11Hooks.Initialize()); //4 Caches the D3DDevice, DXGIFactory, and D3DContext from D3DCreateDevice/DXGICreateFactory
1374+
INITIALIZE(g_D3D11Hooks.Initialize()); //4 Caches the D3DDevice, DXGIFactory, and D3DContext from D3DCreateDevice/DXGICreateFactory
13751375
INITIALIZE(Init_ReadConfig()); //5
13761376
INITIALIZE(ReshadeCompatibility::Check()); //6 Dependent on ReadConfig, must also be before LauncherConfigOverride
13771377
INITIALIZE(Init_CalculateScreenSize()); //7
@@ -1406,7 +1406,6 @@ static void InitializeSubsystems()
14061406
INITIALIZE(DamagedSaveFix::Initialize());
14071407
INITIALIZE(g_MuteWarning.Setup());
14081408
INITIALIZE(g_PauseOnFocusLoss.Initialize());
1409-
14101409

14111410
}
14121411

src/resources/gamevars.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ void GameVars::Initialize()
2222
LOG_HOOK(levelTransitionMidHook, "GameVars: Level Transition")
2323
}
2424
}
25-
2625
}
2726

2827
bool GameVars::InCutscene() const
@@ -49,3 +48,4 @@ void GameVars::OnLevelTransition()
4948
{
5049
g_EffectSpeedFix.Reset();
5150
}
51+

steamworks_api.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "common.hpp"
2+
#include "steamworks_api.hpp"
3+
4+
#include <spdlog/spdlog.h>
5+
6+
7+
8+
void SteamAPIHook::FetchAndCacheSteamID()
9+
{
10+
if (!SteamUserFn || !GetSteamIDFn)
11+
{
12+
spdlog::error("SteamAPI functions not loaded. Call Setup() first.");
13+
return;
14+
}
15+
16+
pSteamUser = SteamUserFn();
17+
if (!pSteamUser)
18+
{
19+
spdlog::error("SteamUser interface not initialized yet.");
20+
return;
21+
}
22+
23+
steamID = GetSteamIDFn(pSteamUser);
24+
spdlog::info("SteamID fetched and cached: {}", *steamID);
25+
}
26+
27+
std::optional<uint64_t> SteamAPIHook::GetSteamID64() const noexcept
28+
{
29+
return steamID;
30+
}
31+
32+
33+
void SteamAPIHook::Setup()
34+
{
35+
hSteamAPI = GetModuleHandleA("steam_api64.dll");
36+
if (!hSteamAPI)
37+
{
38+
hSteamAPI = LoadLibraryA("steam_api64.dll");
39+
}
40+
if (!hSteamAPI)
41+
{
42+
spdlog::error("steam_api64.dll not loaded.");
43+
return;
44+
}
45+
46+
SteamUserFn = reinterpret_cast<SteamAPI_SteamUser_v021_t>(GetProcAddress(hSteamAPI, "SteamAPI_SteamUser_v021"));
47+
if (!SteamUserFn)
48+
{
49+
spdlog::error("SteamAPI_SteamUser_v021 not found.");
50+
return;
51+
}
52+
53+
GetSteamIDFn = reinterpret_cast<SteamAPI_ISteamUser_GetSteamID_t>(GetProcAddress(hSteamAPI, "SteamAPI_ISteamUser_GetSteamID"));
54+
if (!GetSteamIDFn)
55+
{
56+
spdlog::error("SteamAPI_ISteamUser_GetSteamID not found.");
57+
return;
58+
}
59+
60+
spdlog::info("SteamAPIHook Setup: steam_api64.dll loaded and functions resolved.");
61+
62+
if (uint8_t* AfterSteamSetupResult = Memory::PatternScan(baseModule, "48 8B 05 ?? ?? ?? ?? 8B CB", "Steam API Initalization"))
63+
{
64+
static SafetyHookMid SteamMidhook {};
65+
SteamMidhook = safetyhook::create_mid(AfterSteamSetupResult,
66+
[](SafetyHookContext& ctx)
67+
{
68+
g_SteamAPI.FetchAndCacheSteamID();
69+
});
70+
}
71+
}

steamworks_api.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
3+
class SteamAPIHook final
4+
{
5+
public:
6+
void Setup();
7+
[[nodiscard]] std::optional<uint64_t> GetSteamID64() const noexcept;
8+
9+
private:
10+
void FetchAndCacheSteamID();
11+
12+
using SteamAPI_SteamUser_v021_t = void* (__cdecl*)();
13+
using SteamAPI_ISteamUser_GetSteamID_t = uint64_t(__cdecl*)(void*);
14+
15+
SteamAPI_SteamUser_v021_t SteamUserFn { nullptr };
16+
SteamAPI_ISteamUser_GetSteamID_t GetSteamIDFn { nullptr };
17+
18+
void* pSteamUser { nullptr };
19+
std::optional<uint64_t> steamID;
20+
21+
HMODULE hSteamAPI { nullptr };
22+
};
23+
24+
25+
inline SteamAPIHook g_SteamAPI;

0 commit comments

Comments
 (0)