Example #1
0
        public bool Run(string gameExe, string extraGameArguments, out string errorMessage)
        {
            errorMessage = default;
            if (!this._client.CanRun(gameExe, extraGameArguments))
            {
                return(false);
            }

            gameExe ??= "Bannerlord.exe";
            var actualGameExe = Path.Combine(this.GameExeFolder, gameExe);

            if (string.IsNullOrEmpty(actualGameExe))
            {
                errorMessage = "Game executable could not be detected";
                this.Log().Error(errorMessage);
                return(false);
            }

            if (!File.Exists(actualGameExe))
            {
                errorMessage = $"{actualGameExe} could not be found";
                this.Log().Error(errorMessage);
                return(false);
            }

            foreach (var dll in this.GetAssemblies().Distinct())
            {
                try
                {
                    var fi = new System.IO.FileInfo(dll);
                    if (!fi.Exists)
                    {
                        continue;
                    }
                    try
                    {
                        if (!fi.AlternateDataStreamExists("Zone.Identifier"))
                        {
                            continue;
                        }
                        var s = fi.GetAlternateDataStream("Zone.Identifier", System.IO.FileMode.Open);
                        s.Delete();
                    }
                    catch (Exception e)
                    {
                        this.Log().Error(e);
                    }
                }
                catch
                {
                    //
                }
            }

            extraGameArguments ??= "";
            var args = extraGameArguments.Trim() + " " + this.GameArguments().Trim();

            this.Log().Warn($"Trying to execute: {actualGameExe} {args}");
            var info = new ProcessStartInfo
            {
                Arguments        = args,
                FileName         = actualGameExe,
                WorkingDirectory = Path.GetDirectoryName(actualGameExe) ?? throw new InvalidOperationException(),
                                         UseShellExecute = false
            };

            try
            {
                Process.Start(info);
            }
            catch (Exception e)
            {
                errorMessage = "Exception when trying to run the game. See the log for details";
                this.Log().Error(e);
                return(false);
            }

            return(true);
        }