[Proxy] Launch steam if necessary

This commit is contained in:
momo5502 2017-02-18 17:26:14 +01:00
parent 1b7aea6780
commit 1764708d6f
6 changed files with 64 additions and 6 deletions

View File

@ -86,7 +86,7 @@ namespace Components
return;
}
if (!Components::Flags::HasFlag("nosteam") && !Dvar::Var("cl_anonymous").get<bool>() && Steam::Proxy::SteamUser_)
if (Steam::Enabled() && !Dvar::Var("cl_anonymous").get<bool>() && Steam::Proxy::SteamUser_)
{
infostr.set("realsteamId", Utils::String::VA("%llX", Steam::Proxy::SteamUser_->GetSteamID().bits));
}

View File

@ -122,7 +122,7 @@ namespace Components
void Friends::UpdateState(bool force)
{
if (Dvar::Var("cl_anonymous").get<bool>() || Components::Flags::HasFlag("nosteam")) return;
if (Dvar::Var("cl_anonymous").get<bool>() || !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<bool>() && !Components::Flags::HasFlag("nosteam"))
if (Steam::Proxy::ClientFriends && !Dvar::Var("cl_anonymous").get<bool>() && Steam::Enabled())
{
Steam::Proxy::ClientFriends.invoke<void>("SetRichPresence", 0, key.data(), value.data());
Steam::Proxy::ClientFriends.invoke<void>("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<bool>() || Components::Flags::HasFlag("nosteam"))
if(Dvar::Var("cl_anonymous").get<bool>() || !Steam::Enabled())
{
if (Steam::Proxy::ClientFriends)
{

View File

@ -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<void>("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<BYTE*>(&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;
}

View File

@ -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();

View File

@ -102,6 +102,11 @@ namespace Steam
Callbacks::Results.clear();
}
bool Enabled()
{
return !Components::Flags::HasFlag("nosteam");
}
extern "C"
{
bool SteamAPI_Init()

View File

@ -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);