diff --git a/src/Components/Modules/Auth.cpp b/src/Components/Modules/Auth.cpp index 69438768..815233e8 100644 --- a/src/Components/Modules/Auth.cpp +++ b/src/Components/Modules/Auth.cpp @@ -86,7 +86,7 @@ namespace Components return; } - if (!Components::Flags::HasFlag("nosteam") && !Dvar::Var("cl_anonymous").get() && Steam::Proxy::SteamUser_) + if (Steam::Enabled() && !Dvar::Var("cl_anonymous").get() && Steam::Proxy::SteamUser_) { infostr.set("realsteamId", Utils::String::VA("%llX", Steam::Proxy::SteamUser_->GetSteamID().bits)); } diff --git a/src/Components/Modules/Friends.cpp b/src/Components/Modules/Friends.cpp index 75de869d..6a6481bb 100644 --- a/src/Components/Modules/Friends.cpp +++ b/src/Components/Modules/Friends.cpp @@ -122,7 +122,7 @@ namespace Components void Friends::UpdateState(bool force) { - if (Dvar::Var("cl_anonymous").get() || Components::Flags::HasFlag("nosteam")) return; + if (Dvar::Var("cl_anonymous").get() || !Steam::Enabled()) return; if(force) { @@ -169,7 +169,7 @@ namespace Components void Friends::SetPresence(std::string key, std::string value) { - if (Steam::Proxy::ClientFriends && !Dvar::Var("cl_anonymous").get() && !Components::Flags::HasFlag("nosteam")) + if (Steam::Proxy::ClientFriends && !Dvar::Var("cl_anonymous").get() && Steam::Enabled()) { Steam::Proxy::ClientFriends.invoke("SetRichPresence", 0, key.data(), value.data()); Steam::Proxy::ClientFriends.invoke("SetRichPresence", Steam::Proxy::AppId, key.data(), value.data()); @@ -584,7 +584,7 @@ namespace Components Friends::InitialState = Steam::Proxy::SteamFriends->GetPersonaState(); } - if(Dvar::Var("cl_anonymous").get() || Components::Flags::HasFlag("nosteam")) + if(Dvar::Var("cl_anonymous").get() || !Steam::Enabled()) { if (Steam::Proxy::ClientFriends) { diff --git a/src/Steam/Proxy.cpp b/src/Steam/Proxy.cpp index b56cf9de..87a81fcb 100644 --- a/src/Steam/Proxy.cpp +++ b/src/Steam/Proxy.cpp @@ -79,18 +79,21 @@ namespace Steam void Proxy::RunGame() { - if (!Components::Flags::HasFlag("nosteam") && !Components::Dedicated::IsEnabled()) + if (Steam::Enabled() && !Components::Dedicated::IsEnabled()) { SetEnvironmentVariableA("SteamAppId", ::Utils::String::VA("%lu", Proxy::AppId)); SetEnvironmentVariableA("SteamGameId", ::Utils::String::VA("%llu", Proxy::AppId & 0xFFFFFF)); ::Utils::IO::WriteFile("steam_appid.txt", ::Utils::String::VA("%lu", Proxy::AppId), false); + + Interface clientUtils(Proxy::ClientEngine->GetIClientUtils(Proxy::SteamPipe, "CLIENTUTILS_INTERFACE_VERSION001")); + clientUtils.invoke("SetAppIDForCurrentPipe", Proxy::AppId, false); } } void Proxy::SetMod(std::string mod) { - if (!Proxy::ClientUser || Components::Flags::HasFlag("nosteam") || Components::Dedicated::IsEnabled()) return; + if (!Proxy::ClientUser || !Steam::Enabled() || Components::Dedicated::IsEnabled()) return; GameID_t gameID; gameID.type = 1; // k_EGameIDTypeGameMod @@ -270,6 +273,49 @@ namespace Steam Proxy::UnregisterCalls(); } + void Proxy::StartSteamIfNecessary() + { + if (!Steam::Enabled() || Proxy::GetSteamDirectory().empty()) return; + + HKEY hRegKey; + DWORD pid = 0; + if (RegOpenKeyExA(HKEY_CURRENT_USER, STEAM_REGISTRY_PROCESS_PATH, 0, KEY_QUERY_VALUE, &hRegKey) != ERROR_SUCCESS) return; + + DWORD dwLength = sizeof(pid); + RegQueryValueExA(hRegKey, "pid", nullptr, nullptr, reinterpret_cast(&pid), &dwLength); + RegCloseKey(hRegKey); + + if (pid) + { + HANDLE process = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid); + if (process) + { + ::Utils::Memory::Allocator allocator; + allocator.reference(process, [](HANDLE hProcess) + { + CloseHandle(hProcess); + }); + + DWORD exitCode; + if (!GetExitCodeProcess(process, &exitCode)) return; + if (exitCode == STILL_ACTIVE) return; + } + } + + std::string steamExe = Proxy::GetSteamDirectory() + "\\steam.exe"; + if (::Utils::IO::FileExists(steamExe)) + { + Components::Toast::Template templ = Components::Toast::Template(Components::Toast::Template::TextTwoLines); + templ.setTextField(L"IW4x", Components::Toast::Template::FirstLine); + templ.setTextField(L"Starting Steam...", Components::Toast::Template::SecondLine); + Components::Toast::ShowNative(templ); + + ShellExecuteA(nullptr, nullptr, steamExe.data(), "-silent", nullptr, 1); + + std::this_thread::sleep_for(10s); + } + } + bool Proxy::Inititalize() { std::string directoy = Proxy::GetSteamDirectory(); @@ -279,6 +325,8 @@ namespace Steam if (!Components::Dedicated::IsEnabled() || !Components::ZoneBuilder::IsEnabled()) { + Proxy::StartSteamIfNecessary(); + Proxy::Overlay = ::Utils::Library(GAMEOVERLAY_LIB, false); if (!Proxy::Overlay.valid()) return false; } diff --git a/src/Steam/Proxy.hpp b/src/Steam/Proxy.hpp index 7f95dac8..34672ff1 100644 --- a/src/Steam/Proxy.hpp +++ b/src/Steam/Proxy.hpp @@ -8,6 +8,7 @@ #define GAMEOVERLAY_LIB "gameoverlayrenderer.dll" #define STEAMCLIENT_LIB "steamclient.dll" #define STEAM_REGISTRY_PATH "Software\\Valve\\Steam" +#define STEAM_REGISTRY_PROCESS_PATH "Software\\Valve\\Steam\\ActiveProcess" #endif namespace Steam @@ -184,6 +185,8 @@ namespace Steam static bool Inititalize(); static void Uninititalize(); + static void StartSteamIfNecessary(); + static void SetGame(uint32_t appId); static void RunGame(); diff --git a/src/Steam/Steam.cpp b/src/Steam/Steam.cpp index fcae26f5..207eaefb 100644 --- a/src/Steam/Steam.cpp +++ b/src/Steam/Steam.cpp @@ -102,6 +102,11 @@ namespace Steam Callbacks::Results.clear(); } + bool Enabled() + { + return !Components::Flags::HasFlag("nosteam"); + } + extern "C" { bool SteamAPI_Init() diff --git a/src/Steam/Steam.hpp b/src/Steam/Steam.hpp index b4a1481b..3a99536c 100644 --- a/src/Steam/Steam.hpp +++ b/src/Steam/Steam.hpp @@ -97,6 +97,8 @@ namespace Steam static std::recursive_mutex Mutex; }; + bool Enabled(); + STEAM_EXPORT bool SteamAPI_Init(); STEAM_EXPORT void SteamAPI_RegisterCallResult(Callbacks::Base* result, uint64_t call); STEAM_EXPORT void SteamAPI_RegisterCallback(Callbacks::Base* handler, int callback);