Esempio n. 1
0
        /// <summary>
        /// Gets the Steam process ID from the registry; returns 0 if Steam isn't running or isn't installed.
        /// </summary>
        /// <returns>Steam's process ID if it is running; otherwise, 0.</returns>
        public static int GetSteamPid()
        {
            if (!IsSteamInstalled)
            {
                return(0);
            }

            string pidRegKey;
            string pidRegName;

            if (SysNative.Is64Bit())
            {
                pidRegKey  = @"HKEY_CURRENT_USER\Software\Valve\Steam\ActiveProcess";
                pidRegName = "pid";
            }
            else
            {
                pidRegKey  = @"HKEY_LOCAL_MACHINE\Software\Valve\Steam";
                pidRegName = "SteamPID";
            }

            Logger.Info($"Attempting to retrieve Steam PID from registry value named '{pidRegName}' inside the key '{pidRegKey}'.");
            var pid = (int)Registry.GetValue(pidRegKey, pidRegName, -1);

            if (pid == -1)
            {
                Logger.Warning($"The Steam PID could not be retrieved because the registry value '{pidRegName}' or registry key '{pidRegKey}' was invalid or did not exist.");
                return(0);
            }

            Logger.Info($"The Steam PID value retrieved from the registry is '{pid}'.");
            return(pid);
        }
Esempio n. 2
0
        /// <summary>
        /// Retrieves the Steam installation path from the registry or returns null if not found.
        /// </summary>
        /// <returns>A string containing the Steam installation path; if not found, returns null.</returns>
        private static string GetSteamInstallPath()
        {
            string installPath;

            if (SysNative.Is64Bit())
            {
                installPath = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Valve\Steam", "SteamPath", null);
            }
            else
            {
                installPath = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Valve\Steam", "InstallPath", null);
            }

            if (installPath == null)
            {
                const string errorMsg = "No valid Steam installation path could be found.";
                Logger.Error(errorMsg);
                throw new InvalidOperationException(errorMsg);
            }

            installPath = installPath.Replace("/", @"\");
            Logger.Info($"Setting Steam installation path to: '{installPath}'");

            return(installPath);
        }
Esempio n. 3
0
        /// <summary>
        /// Loads the steam library.
        /// </summary>
        /// <returns>A value indicating if the load was successful.</returns>

        /*public static bool LoadSteam()
         * {
         *  if (SteamHandle != IntPtr.Zero)
         *      return true;
         *
         *  string path = SteamProcessInfo.SteamInstallPath;
         *
         *  if (!string.IsNullOrEmpty(path))
         *      SysNative.SetDllDirectory(path + ";" + Path.Combine(path, "bin"));
         *
         *  path = Path.Combine(path, "steam.dll");
         *
         *  IntPtr module = SysNative.LoadLibraryEx(path, IntPtr.Zero, SysNative.LOAD_WITH_ALTERED_SEARCH_PATH);
         *
         *  if (module == IntPtr.Zero)
         *      return false;
         *
         *  _callCreateSteamInterface = SysNative.GetExportFunction<SteamNative._f>(module, "_f");
         *  if (_callCreateSteamInterface == null)
         *      return false;
         *
         *  SteamHandle = module;
         *
         *  return true;
         * }*/

        /// <summary>
        /// Loads the steamclient library.
        /// </summary>
        /// <returns>A value indicating if the load was successful.</returns>
        public static bool LoadSteamClient()
        {
            if (SteamClientHandle != IntPtr.Zero)
            {
                return(true);
            }

            string path = SteamProcessInfo.SteamInstallPath;

            if (!string.IsNullOrEmpty(path))
            {
                SysNative.SetDllDirectory(path + ";" + Path.Combine(path, "bin"));
            }

            if (SysNative.Is64Bit())
            {
                path = Path.Combine(path, "steamclient64.dll");
            }
            else
            {
                path = Path.Combine(path, "steamclient.dll");
            }

            IntPtr module = SysNative.LoadLibraryEx(path, IntPtr.Zero, SysNative.LOAD_WITH_ALTERED_SEARCH_PATH);

            if (module == IntPtr.Zero)
            {
                return(false);
            }

            _callCreateInterface = SysNative.GetExportFunction <SteamNative.CreateInterface>(module, "CreateInterface");
            if (_callCreateInterface == null)
            {
                return(false);
            }

            _callSteamBGetCallback = SysNative.GetExportFunction <SteamNative.SteamBGetCallback>(module, "Steam_BGetCallback");
            if (_callSteamBGetCallback == null)
            {
                return(false);
            }

            _callSteamFreeLastCallback = SysNative.GetExportFunction <SteamNative.SteamFreeLastCallback>(module, "Steam_FreeLastCallback");
            if (_callSteamFreeLastCallback == null)
            {
                return(false);
            }

            CallSteamGetAPICallResult = SysNative.GetExportFunction <SteamNative.SteamGetAPICallResult>(module, "Steam_GetAPICallResult");
            if (CallSteamGetAPICallResult == null)
            {
                return(false);
            }

            SteamClientHandle = module;

            return(true);
        }
Esempio n. 4
0
        /// <summary>
        /// Gets the path of the Steam Client DLL.
        /// </summary>
        /// <returns>A string containing the Steam Client DLL path; if not found, returns null.</returns>
        private static string GetSteamClientDllPath()
        {
            if (string.IsNullOrWhiteSpace(SteamInstallPath))
            {
                return(null);
            }

            var dllPath = Path.Combine(SteamInstallPath, SysNative.Is64Bit() ? "steamclient64.dll" : "steamclient.dll");

            Logger.Info($"Setting Steam Client DLL path to: '{dllPath}'");

            return(dllPath);
        }
Esempio n. 5
0
        /// <summary>
        /// To be run immediately after starting a new Steam process to wait until complete if a Steam update is underway.
        /// </summary>
        /// <param name="steamProcess">The new Steam process that was just started.</param>
        private static void WaitOnSteamUpdate(Process steamProcess)
        {
            Logger.Info("Checking if the Steam client is in the process of updating...");
            System.Threading.Thread.Sleep(2000);

            try
            {
                var mainWindowHandle = steamProcess.MainWindowHandle;
                if (mainWindowHandle == IntPtr.Zero)
                {
                    return;
                }

                StringBuilder sb          = null;
                var           loopCounter = 0;

                var hStatic = SysNative.FindWindowEx(mainWindowHandle, IntPtr.Zero, "Static", null);

                while (hStatic != IntPtr.Zero)
                {
                    if (sb == null)
                    {
                        sb = new StringBuilder(25);
                    }

                    SysNative.SendMessage(hStatic, SysNative.WM_GETTEXT, sb.Capacity, sb);
                    if (sb.Length == 0 || !sb.ToString().Contains("updat"))
                    {
                        break;
                    }

                    System.Threading.Thread.Sleep(1000);
                    if (loopCounter == 0)
                    {
                        Logger.Info("A Steam client update is in progress; waiting for completion.", 1);
                    }

                    loopCounter++;
                }

                if (loopCounter > 0)
                {
                    Logger.Info("The Steam client update is complete; continuing program execution.", 1);
                }
            }
            catch (Exception ex)
            {
                Logger.Warning($"The following exception occurred while attempting to detect if a Steam client update was in progress: {ex.Message}");
            }
        }
        public LaunchViaSteamMenuItem()
        {
            var defaultConfigLoaded = false;

            try
            {
                // Try to load the plugin's configuration file
                Config.Load();
            }
            catch (FileNotFoundException ex)
            {
                // Load default values if no configuration file is found
                Config.Default();
                defaultConfigLoaded = true;
            }

            // Enable debug logging if enabled in the config
            if (Config.Instance.DebugLogEnabled)
            {
                try
                {
                    Logger.EnableDebugLog(Config.LOG_PATH);
                }
                catch (Exception ex)
                {
                    Logger.Error("An error occurred while attempting to enable debug logging to file. " +
                                 $"Debug messages will only be output to console. Exception: {ex.Message}");
                }
            }

            if (defaultConfigLoaded)
            {
                Logger.Warning($"The configuration file '{Config.CONFIG_FILENAME}' could not be found. Loading defaults.");
            }

            Logger.Info($"SteamLauncher plugin loaded ({(SysNative.Is64Bit() ? "64-bit" : "32-bit")} mode).");

            _activeWindow = new ActiveWindow();
        }
Esempio n. 7
0
        private bool InitSteam()
        {
            if (SteamProcessInfo.SteamProcess == null)
            {
                Logger.Error("The Steam process could not be located or started. Aborting Steam initialization.");
                return(false);
            }

            if (IsLoaded(SteamClientDllHandle))
            {
                Logger.Info("Steam is already initialized.");
                return(true);
            }

            Logger.Info("Beginning initialization of Steam...");

            SysNative.SetDllDirectory(SteamProcessInfo.SteamInstallPath + ";" + Path.Combine(SteamProcessInfo.SteamInstallPath, "bin"));
            var clientDllHandle = SysNative.LoadLibraryEx(SteamProcessInfo.SteamClientDllPath, IntPtr.Zero, SysNative.LOAD_WITH_ALTERED_SEARCH_PATH);

            if (!IsLoaded(clientDllHandle))
            {
                Logger.Error("Failed to load the Steam Client DLL. Aborting initialization.", 1);
                return(false);
            }

            _callCreateInterface = SysNative.GetExportFunction <SteamNative.CreateInterface>(clientDllHandle, "CreateInterface");
            if (_callCreateInterface == null)
            {
                Logger.Error("Failed to retrieve the 'CreateInterface' export function. Aborting initialization.", 1);
                return(false);
            }

            _callCreateSteamPipe = SysNative.GetExportFunction <SteamNative.CreateSteamPipe>(clientDllHandle, "Steam_CreateSteamPipe");
            if (_callCreateSteamPipe == null)
            {
                Logger.Error("Failed to retrieve the 'Steam_CreateSteamPipe' export function. Aborting initialization.", 1);
                return(false);
            }

            _callBReleaseSteamPipe = SysNative.GetExportFunction <SteamNative.BReleaseSteamPipe>(clientDllHandle, "Steam_BReleaseSteamPipe");
            if (_callBReleaseSteamPipe == null)
            {
                Logger.Error("Failed to retrieve the 'Steam_BReleaseSteamPipe' export function. Aborting initialization.", 1);
                return(false);
            }

            _callConnectToGlobalUser = SysNative.GetExportFunction <SteamNative.ConnectToGlobalUser>(clientDllHandle, "Steam_ConnectToGlobalUser");
            if (_callConnectToGlobalUser == null)
            {
                Logger.Error("Failed to retrieve the 'Steam_ConnectToGlobalUser' export function. Aborting initialization.", 1);
                return(false);
            }

            _callReleaseUser = SysNative.GetExportFunction <SteamNative.ReleaseUser>(clientDllHandle, "Steam_ReleaseUser");
            if (_callReleaseUser == null)
            {
                Logger.Error("Failed to retrieve the 'Steam_ReleaseUser' export function. Aborting initialization.", 1);
                return(false);
            }

            Pipe = CreateSteamPipe();
            if (!IsValidPipe(Pipe))
            {
                Logger.Error("Failed to create a Steam pipe (IPC). Aborting initialization.", 1);
                return(false);
            }

            User = ConnectToGlobalUser(Pipe);
            if (!IsValidUser(User))
            {
                Logger.Error("Failed to connect to Steam global user (IPC). Aborting initialization.", 1);
                return(false);
            }

            SteamClientDllHandle = clientDllHandle;
            Logger.Info("Steam initialization succeeded!", 1);

            return(true);
        }