Beispiel #1
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;
            }
        }