Example #1
0
        private LaunchResult Launch(LaunchSetting setting)
        {
            try
            {
                if (setting.AuthenticateAccessToken == null || setting.AuthenticateUUID == null)
                {
                    return(new LaunchResult(new ArgumentException("启动所需必要的验证参数为空")));
                }
                if (setting.Version == null)
                {
                    return(new LaunchResult(new ArgumentException("启动所需必要的版本参数为空")));
                }

                if (setting.MaxMemory == 0)
                {
                    setting.MaxMemory = SystemTools.GetBestMemory(this.Java);
                }

                Stopwatch sw = new Stopwatch();
                sw.Start();

                string arg = argumentsParser.Parse(setting);

                #region 处理库文件
                foreach (var item in setting.Version.Natives)
                {
                    string nativePath = GetNativePath(item);
                    if (File.Exists(nativePath))
                    {
                        App.logHandler.AppendDebug(string.Format("检查并解压不存在的库文件:{0}", nativePath));
                        Unzip.UnZipFile(nativePath, GetGameVersionRootDir(setting.Version) + @"\$natives", item.Exclude);
                    }
                    else
                    {
                        return(new LaunchResult(new NativeNotFoundException(item, nativePath)));
                    }
                }
                #endregion

                ProcessStartInfo startInfo = new ProcessStartInfo(Java.Path, arg)
                {
                    RedirectStandardError = true, RedirectStandardOutput = true, UseShellExecute = false, WorkingDirectory = GetGameVersionRootDir(setting.Version)
                };
                var process = Process.Start(startInfo);
                sw.Stop();
                App.logHandler.AppendInfo(string.Format("成功启动游戏进程,总共用时:{0}ms", sw.ElapsedMilliseconds));

                Task.Factory.StartNew(() =>
                {
                    process.WaitForExit();
                    App.logHandler.AppendInfo("游戏进程退出,代码:" + process.ExitCode);
                    GameExit?.Invoke(this, process.ExitCode);
                });

                process.OutputDataReceived += Process_OutputDataReceived;
                process.ErrorDataReceived  += Process_ErrorDataReceived;
                process.BeginErrorReadLine();
                process.BeginOutputReadLine();

                #region 配置文件
                App.config.MainConfig.History.LastLaunchUsingMs = sw.ElapsedMilliseconds;
                App.config.MainConfig.History.LastLaunchTime    = DateTime.Now;
                App.config.MainConfig.History.LastLaunchVersion = setting.Version.ID;
                App.config.Save();
                #endregion

                return(new LaunchResult()
                {
                    Process = process, IsSuccess = true, LaunchArguments = arg
                });
            }
            catch (LaunchException.LaunchException ex)
            {
                return(new LaunchResult(ex));
            }
            catch (Exception ex)
            {
                return(new LaunchResult(ex));
            }
        }
Example #2
0
        private LaunchResult Launch(LaunchSetting setting)
        {
            try
            {
                IsBusyLaunching = true;
                lock (launchLocker)
                {
                    if (Java == null)
                    {
                        return(new LaunchResult(new NullJavaException()));
                    }
                    if (setting.AuthenticateResult == null)
                    {
                        return(new LaunchResult(new ArgumentException("启动所需必要的验证参数为空")));
                    }
                    if (setting.Version == null)
                    {
                        return(new LaunchResult(new ArgumentException("启动所需必要的版本参数为空")));
                    }

                    if (setting.MaxMemory == 0)
                    {
                        setting.MaxMemory = SystemTools.GetBestMemory(this.Java);
                    }

                    Stopwatch sw = new Stopwatch();
                    sw.Start();


                    if (setting.LaunchType == LaunchType.SAFE)
                    {
                        setting.AdvencedGameArguments = null;
                        setting.AdvencedJvmArguments  = null;
                        setting.GCArgument            = null;
                        setting.JavaAgent             = null;
                    }

                    string arg = argumentsParser.Parse(setting);

                    if (setting.LaunchType == LaunchType.CREATE_SHORT)
                    {
                        File.WriteAllText(Environment.CurrentDirectory + "\\LaunchMinecraft.bat", string.Format("\"{0}\" {1}", Java.Path, arg));
                        return(new LaunchResult()
                        {
                            IsSuccess = true, LaunchArguments = arg
                        });
                    }

                    #region 处理库文件
                    foreach (var item in setting.Version.Natives)
                    {
                        string nativePath = GetNativePath(item);
                        if (File.Exists(nativePath))
                        {
                            AppendLaunchInfoLog(string.Format("检查并解压不存在的库文件:{0}", nativePath));
                            Unzip.UnZipNativeFile(nativePath, GetGameVersionRootDir(setting.Version) + @"\$natives", item.Exclude, setting.LaunchType != LaunchType.SAFE);
                        }
                        else
                        {
                            return(new LaunchResult(new NativeNotFoundException(item, nativePath)));
                        }
                    }
                    #endregion

                    AppendLaunchInfoLog(string.Format("开始启动游戏进程,使用JAVA路径:{0}", this.Java.Path));
                    ProcessStartInfo startInfo = new ProcessStartInfo(Java.Path, arg)
                    {
                        RedirectStandardError = true, RedirectStandardOutput = true, UseShellExecute = false, WorkingDirectory = GetGameVersionRootDir(setting.Version)
                    };
                    var process = Process.Start(startInfo);
                    sw.Stop();
                    long launchUsingMsTime = sw.ElapsedMilliseconds;
                    AppendLaunchInfoLog(string.Format("成功启动游戏进程,总共用时:{0}ms", launchUsingMsTime));

                    Task.Factory.StartNew(() =>
                    {
                        process.WaitForExit();
                        AppendLaunchInfoLog("游戏进程退出,代码:" + process.ExitCode);
                        GameExit?.Invoke(this, new GameExitArg()
                        {
                            Process  = process,
                            Version  = setting.Version,
                            ExitCode = process.ExitCode,
                            Duration = (process.StartTime - process.ExitTime)
                        });
                    });

                    process.OutputDataReceived += Process_OutputDataReceived;
                    process.ErrorDataReceived  += Process_ErrorDataReceived;
                    process.BeginErrorReadLine();
                    process.BeginOutputReadLine();

                    return(new LaunchResult()
                    {
                        Process = process, IsSuccess = true, LaunchArguments = arg, LaunchUsingMs = launchUsingMsTime
                    });
                }
            }
            catch (LaunchException.LaunchException ex)
            {
                return(new LaunchResult(ex));
            }
            catch (Exception ex)
            {
                return(new LaunchResult(ex));
            }
            finally
            {
                IsBusyLaunching = false;
            }
        }
Example #3
0
 private void ExitGame()
 {
     OnGameExit?.Invoke();
     GameManager.Instance.ClearAllGameData();
 }