private LaunchResult LaunchServer(ServerLaunchArguments args) { try { Console.WriteLine(args.ToArguments()); var handle = new LaunchHandle { Code = CurrentCode, Core = this, Arguments = args, Process = Process.Start(new ProcessStartInfo(JavaPath) { Arguments = args.ToArguments(), UseShellExecute = false, WorkingDirectory = ServerRootPath, RedirectStandardError = true, RedirectStandardOutput = true, RedirectStandardInput = true, CreateNoWindow = true }) }; handle.Work(); Task.Factory.StartNew(handle.Process.WaitForExit).ContinueWith(t => Exit(handle, handle.Process.ExitCode)); return new LaunchResult { Success = true, Handle = handle }; } catch (Exception ex) { return new LaunchResult { Success = false, ErrorType = ErrorType.Unknown, ErrorMessage = "启动时出现了异常", Exception = ex }; } }
internal LaunchResult LaunchInternal(ServerOptions options,params Action<ServerLaunchArguments>[] argumentsOperators) { lock (Locker) { if (!File.Exists(JavaPath)) { return new LaunchResult { Success = false, ErrorType = ErrorType.NoJAVA, ErrorMessage = "指定JAVA位置不存在" }; } CurrentCode = Random.Next(); var args = new ServerLaunchArguments(); var result = GenerateArguments(options, ref args); if(result != null) { return result; } if (argumentsOperators == null) return LaunchServer(args); foreach(var opt in argumentsOperators) { try { opt?.Invoke(args); } catch (Exception ex) { return new LaunchResult { Success = false, ErrorType = ErrorType.OperatorException, ErrorMessage = "指定的操作器引发了异常", Exception = ex }; } } return LaunchServer(args); } }
private LaunchResult GenerateArguments(ServerOptions options,ref ServerLaunchArguments args) { try { args.CGCEnabled = true; args.MaxRAM = options.MaxRAM; args.MinRAM = options.MinRAM; args.JarPath = options.JarPath; if (!string.IsNullOrWhiteSpace(options.CustomArguments)) { var cus = options.CustomArguments.Split(' '); args.CustomAruguments = cus.ToList(); } return null; } catch(Exception ex) { return new LaunchResult { Success = false, ErrorType = ErrorType.Unknown, ErrorMessage = "在省城启动参数时发生了意外的错误", Exception = ex }; } }