Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DLL: implemented discord rich presence #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Resources/mod_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ EnableDebugMenu = true # Enable the debug menu. Toggled with the ho
EnableCoreLogging = false # Log all core loads to the debug console.
EnableAssetLogging = false # Log all asset loads to the debug console.
EnableAssetOverrides = true # Enable the [AssetOverrides] section below.
#EnableDiscordRichPresence = false # Not yet implemented. Has no effect.
EnableDiscordRichPresence = false # shows you are playing HZD on discord like other games does.
DebugMenuFontScale = 1.0 # Change the global font scale in the debug menu.

[Gameplay]
Expand Down
74 changes: 71 additions & 3 deletions hzd_test/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <atomic>
#include <stdlib.h>
#include <chrono>
#include <filesystem>
#include <detours/Detours.h>

#include "HRZ/PGraphics3D/SwapChainDX12.h"
Expand All @@ -18,6 +20,10 @@
#include "RTTI/MSRTTI.h"
#include "RTTI/RTTIScanner.h"

//https://github.com/discord/discord-rpc
#include "discord rpc/include/discord_rpc.h"
#include "discord rpc/include/discord_register.h"

#include "LogHooks.h"
#include "ModConfig.h"
#include "ModCoreEvents.h"
Expand Down Expand Up @@ -267,6 +273,14 @@ void LoadSignatures(GameType Game)
Offsets::MapSignature("RenderingConfigDescriptorHeapOffsetPtr", "4C 8D 45 98 45 0F AF CF 41 8B D5 49 8B CE 4C 03 08 4C 89 4E F4", -0x10);
Offsets::MapSignature("RenderingConfigDescriptorHandleIncrementSizeOffsetPtr", "4C 8D 45 98 45 0F AF CF 41 8B D5 49 8B CE 4C 03 08 4C 89 4E F4", -0x4);
Offsets::MapSignature("RenderingConfigDescriptorCommandQueueOffsetPtr", "48 89 7D 30 48 8B 01 FF 50 78 85 C0 79 14", -0x4);

//node graphs currently working but needs further reassurance

// std::string_view loc = ("48 8D 15 ? ? ? ? 48 8B CB E8 ? ? ? ? B0 01 48 83 C4 20 5B C3 48 8D 4C 24 38");

// Offsets::MapSignature("NodeGraphAlert", loc, -0x72);
// Offsets::MapSignature("NodeGraphAlertWithName", loc, -0x62);
// Offsets::MapSignature("NodeGraphTrace", loc, -0x42);
}
}

Expand Down Expand Up @@ -306,9 +320,9 @@ void ApplyHooks(GameType Game)
XUtil::DetourCall(Offsets::ResolveID<"PreLoadObjectHookLoc">(), &LogHooks::PreLoadObjectHook);
XUtil::DetourCall(Offsets::ResolveID<"PostLoadObjectHookLoc">(), &LogHooks::PostLoadObjectHook);

//XUtil::DetourJump(moduleBase + 0x0605830, &LogHooks::NodeGraphAlert);
//XUtil::DetourJump(moduleBase + 0x0605840, &LogHooks::NodeGraphAlertWithName);
//XUtil::DetourJump(moduleBase + 0x0605860, &LogHooks::NodeGraphTrace);
// XUtil::DetourJump(Offsets::ResolveID<"NodeGraphAlert">(), &LogHooks::NodeGraphAlert);
// XUtil::DetourJump(Offsets::ResolveID<"NodeGraphAlertWithName">(), &LogHooks::NodeGraphAlertWithName);
// XUtil::DetourJump(Offsets::ResolveID<"NodeGraphTrace">(), &LogHooks::NodeGraphTrace)

// Rendering
XUtil::DetourCall(Offsets::ResolveID<"SwapChainDX12PresentHookLoc">(), &hk_SwapChainDX12_Present);
Expand Down Expand Up @@ -347,6 +361,54 @@ void ApplyHooks(GameType Game)
}
}

void discordInitialize(GameType Game)
{
if (Game == GameType::HorizonZeroDawn)
{
if (ModConfiguration.EnableDiscordRichPresence)
{
static int64_t StartupTime = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();

std::string state = "Playing Horizon Zero Dawn";

// Initialize Discord
DiscordRichPresence DiscordRPC;

DiscordEventHandlers handlers;
memset(&handlers, 0, sizeof(handlers));
memset(&DiscordRPC, 0, sizeof(DiscordRPC));
Discord_Initialize("946049284309135402", &handlers, 1, "1151640"); //not sure if steamappID is required


DiscordRPC.state = state.c_str();
DiscordRPC.startTimestamp = StartupTime;
DiscordRPC.endTimestamp = NULL;
DiscordRPC.largeImageKey = "horizon_zero_dawn_1"; //you can use either horizon_zero_dawn_1 or horizon_zero_dawn_2 both fits perfectly
DiscordRPC.largeImageText = "Horizon Zero Dawn";
DiscordRPC.smallImageKey = "horizon_zero_dawn_2"; //sometimes looks ugly you can disable this if you want
DiscordRPC.smallImageText = "Horizon Zero Dawn"; ////sometimes looks ugly you can disable this if you want

Discord_UpdatePresence(&DiscordRPC);

}
}
}

void discordUnInitialize()
{
//since we are only using discord-rpc for HZD we don't want this running duing DS so checking for the executable here instead of inside of Dll main

if (std::filesystem::exists(".\\HorizonZeroDawn.exe"))
{
if (ModConfiguration.EnableDiscordRichPresence) //don't use this when discord not initilized
{
Discord_ClearPresence();
Discord_Shutdown();

}
}
}

BOOL APIENTRY DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpReserved)
{
if (fdwReason == DLL_PROCESS_ATTACH)
Expand All @@ -371,6 +433,7 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpReserved)

LoadSignatures(gameType);
ApplyHooks(gameType);
discordInitialize(gameType);
}
catch (const std::exception& e)
{
Expand All @@ -384,5 +447,10 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpReserved)
}
}

else if (fdwReason == DLL_PROCESS_DETACH)
{
discordUnInitialize();
}

return TRUE;
}