/// <summary> /// Get the absolute location of an installed java version /// </summary> /// <param name="jreVersion">The java version to retrieve</param> /// <returns>Returns the absolute path to java.exe</returns> public static string GetJavaPath(JavaVersion jreVersion) { if (!_initialized) { Initialize(); } switch (jreVersion) { case JavaVersion.Jre6X32: return(_jre6X32); case JavaVersion.Jre6X64: return(_jre6X64); case JavaVersion.Jre7X32: return(_jre7X32); case JavaVersion.Jre7X64: return(_jre7X64); case JavaVersion.Jre8X32: return(_jre8X32); case JavaVersion.Jre8X64: return(_jre8X64); default: return(null); } }
/// <summary> /// Returns true if a given version if installed /// </summary> /// <param name="jreVersion">The java version to check</param> /// <returns></returns> public static Boolean IsInstalled(JavaVersion jreVersion) { if (!_initialized) { Initialize(); } switch (jreVersion) { case JavaVersion.Jre6X32: return(!string.IsNullOrEmpty(_jre6X32)); case JavaVersion.Jre6X64: return(!string.IsNullOrEmpty(_jre6X64)); case JavaVersion.Jre7X32: return(!string.IsNullOrEmpty(_jre7X32)); case JavaVersion.Jre7X64: return(!string.IsNullOrEmpty(_jre7X64)); case JavaVersion.Jre8X32: return(!string.IsNullOrEmpty(_jre8X32)); case JavaVersion.Jre8X64: return(!string.IsNullOrEmpty(_jre8X64)); default: return(false); } }
private static JavaVersion InterpretJavaVersionOutput(string output) { if (output == null) { return(null); } Match versionMatch = versionRegex.Match(output); if (versionMatch.Success) { JavaVersion result = new JavaVersion { Version = versionMatch.Groups[1].Value }; if (TryParseJavaVersion(result.Version.Split(".")[0], out var computedVersion)) { if (computedVersion == 1) { TryParseJavaVersion(result.Version.Split(".")[1], out computedVersion); } result.VersionComputed = computedVersion; } if (output.Contains(bitPattern)) { result.Is64Bit = true; } return(result); } return(null); }
/// <summary> /// Get the absolute location of an installed java version /// </summary> /// <param name="jreVersion">The java version to retrieve</param> /// <returns>Returns the absolute path to java.exe</returns> public static string GetJavaPath(JavaVersion jreVersion) { if (!_initialized) { Initialize(); } if (jreVersion == JavaVersion.Custom) { string path = Config.ReadString("starter", "java_custom", ""); if (string.IsNullOrEmpty(path)) { OpenFileDialog ofd = new OpenFileDialog { Title = "Select java.exe", Filter = "Java executables (*.exe) |*.exe|All Files (*.*)|*.*" }; ofd.ShowDialog(); path = ofd.FileName; Config.WriteString("starter", "java_custom", path); } return path; } if (_javaPaths.ContainsKey(jreVersion)) return _javaPaths[jreVersion]; return null; }
public void addrelativeDocument(string path) { JavaVersion javaVersion = new JavaVersion(); javaVersion.Path = path; javaVersion.Version = GetProductVersion(path); vs.Add(javaVersion); }
/// <summary> /// Returns true if a given version if installed /// </summary> /// <param name="jreVersion">The java version to check</param> /// <returns></returns> public static Boolean IsInstalled(JavaVersion jreVersion) { if (!_initialized) { Initialize(); } return(_javaPaths.ContainsKey(jreVersion)); }
public Sided <Mappings> GetMCPMappings(JavaVersion version) { if (MCP == null) { return(null); } return(MCP.CreateMappings(version.Name)); }
/// <summary> /// Launch a new server /// </summary> /// <param name="server">the server type that will be executed</param> /// <param name="javaVersion">the java version to use</param> /// <param name="jarFile">the jar file to run</param> /// <param name="minMem">the minimum amount of memory to set</param> /// <param name="maxMem">the maximum amount of memory to set</param> /// <param name="defaultParameters">the parameters entered by the user (optional)</param> /// <param name="defaultFlags">the flags entered by the user (optional)</param> public void LaunchServer(IMinecraftServer server, JavaVersion javaVersion, string jarFile, UInt32 minMem, UInt32 maxMem, string defaultParameters = "", string defaultFlags = "") { server.PrepareLaunch(); string parameters = server.GetLaunchParameters(defaultParameters); string flags = server.GetLaunchFlags(defaultFlags); string argument = parameters + " -Xms" + minMem + "M -Xmx" + maxMem + "M -jar \"" + jarFile + "\" " + flags; string executable = JavaApi.GetJavaPath(javaVersion); ProcessHandler.StartServer(executable, argument, server); }
protected override IEnumerable <Version> FindVersions() { foreach (var folder in VersionConfig.InputFolders) { foreach (var version in Directory.EnumerateDirectories(folder)) { if (JavaVersion.LooksValid(version)) { yield return(new JavaVersion(version)); } } } }
/// <summary> /// Get the absolute location of an installed java version /// </summary> /// <param name="jreVersion">The java version to retrieve</param> /// <returns>Returns the absolute path to java.exe</returns> public static string GetJavaPath(JavaVersion jreVersion) { if (!_initialized) { Initialize(); } Logger.Log(LogLevel.Info, "JavaApi", "Getting Java Path for version " + jreVersion); string path; if (jreVersion == JavaVersion.Custom) { path = Config.ReadString("starter", "java_custom", ""); if (string.IsNullOrEmpty(path)) { OpenFileDialog ofd = new OpenFileDialog { Title = "Select java.exe", Filter = "Java executables (*.exe) |*.exe|All Files (*.*)|*.*" }; ofd.ShowDialog(); path = ofd.FileName; Config.WriteString("starter", "java_custom", path); } Logger.Log(LogLevel.Info, "JavaApi", "Using custom java path: " + path); return(path); } if (_javaPaths.ContainsKey(jreVersion)) { path = _javaPaths[jreVersion]; Logger.Log(LogLevel.Info, "JavaApi", "Using java " + jreVersion + ", path: " + path); return(path); } Logger.Log(LogLevel.Warning, "JavaApi", "Java " + jreVersion + " requested but not found!"); return(null); }
public async Task <bool> StartNetworkAsync(NetworkViewModel viewModel, bool startServers) { ConsoleWriter.Write("\n Starting network " + viewModel.Entity.Name, viewModel); Console.WriteLine("Starting network " + viewModel.Entity.Name); if (startServers) { if (viewModel.Servers.Count == 0) { ConsoleWriter.Write("WARNING: This network contains no Servers. Players will not be able to join.", viewModel); } else { ConsoleWriter.Write("Starting all " + viewModel.Servers.Count + " servers of this network...", viewModel); foreach (NetworkServer networkServer in viewModel.Servers) { if (networkServer is NetworkForkServer networkForkServer) { ServerViewModel serverViewModel = networkForkServer.ServerViewModel; if (serverViewModel.CurrentStatus == ServerStatus.STOPPED) { ConsoleWriter.Write("\nStarting server " + serverViewModel.Server + " on world: " + serverViewModel.Server.ServerSettings.LevelName, viewModel); ServerManager.Instance.StartServerAsync(serverViewModel); } else if (serverViewModel.CurrentStatus == ServerStatus.STARTING) { ConsoleWriter.Write("Server " + serverViewModel.Server + " is already starting.", viewModel); } else if (serverViewModel.CurrentStatus == ServerStatus.RUNNING) { ConsoleWriter.Write("Server " + serverViewModel.Server + " is already running.", viewModel); } } else { ConsoleWriter.Write("Server " + networkServer.Name + " can't be started automatically because it is no Fork server.", viewModel); } } } } else { ConsoleWriter.Write("Make sure that at least one server configured in the settings is running, else Players won't be able to join this network.", viewModel); } ConsoleWriter.Write("\n", viewModel); if (!viewModel.SettingsSavingTask.IsCompleted) { ConsoleWriter.Write("Saving settings files before starting proxy server...", viewModel); await viewModel.SettingsSavingTask; } //Start proxy server ConsoleWriter.Write("Starting proxy server...", viewModel); DirectoryInfo directoryInfo = new DirectoryInfo(Path.Combine(App.ServerPath, viewModel.Network.Name)); if (!directoryInfo.Exists) { ConsoleWriter.Write("ERROR: Can't find network directory: " + directoryInfo.FullName, viewModel); return(false); } JavaVersion javaVersion = JavaVersionUtils.GetInstalledJavaVersion(viewModel.Network.JavaSettings.JavaPath); if (javaVersion == null) { ConsoleWriter.Write("ERROR: Java is not installed! Minecraft networks require Java!", viewModel); return(false); } if (!javaVersion.Is64Bit) { ConsoleWriter.Write("WARN: The Java installation selected for this network is a 32-bit version, which can cause errors.", viewModel); } if (javaVersion.VersionComputed < 11) { ConsoleWriter.Write("WARN: The Java installation selected for this network is outdated. Please update Java to version 11 or higher.", viewModel); } Process process = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo { UseShellExecute = false, RedirectStandardError = true, RedirectStandardInput = true, RedirectStandardOutput = true, FileName = viewModel.Network.JavaSettings.JavaPath, WorkingDirectory = directoryInfo.FullName, Arguments = "-Xmx" + viewModel.Network.JavaSettings.MaxRam + "m " + viewModel.Network.JavaSettings.StartupParameters + " -jar server.jar nogui", WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = true }; process.StartInfo = startInfo; process.Start(); Task.Run(() => { viewModel.TrackPerformance(process); }); viewModel.CurrentStatus = ServerStatus.STARTING; ConsoleWriter.RegisterApplication(viewModel, process.StandardOutput, process.StandardError); ConsoleReader consoleReader = new ConsoleReader(process.StandardInput); viewModel.ConsoleReader = consoleReader; Task.Run(async() => { await process.WaitForExitAsync(); ApplicationManager.Instance.ActiveEntities.Remove(viewModel.Entity); viewModel.CurrentStatus = ServerStatus.STOPPED; }); ApplicationManager.Instance.ActiveEntities[viewModel.Network] = process; Console.WriteLine("Started network " + viewModel.Network); return(true); }
public async Task <bool> StartServerAsync(ServerViewModel viewModel) { ConsoleWriter.Write("\n", viewModel); //if (!viewModel.SettingsSavingTask.IsCompleted) //{ ConsoleWriter.Write("Saving settings files before starting server ...", viewModel); await Task.Run(async() => await viewModel.SettingsSavingTask); //} ConsoleWriter.Write( "Starting server " + viewModel.Server + " on world: " + viewModel.Server.ServerSettings.LevelName, viewModel); Console.WriteLine("Starting server " + viewModel.Server.Name + " on world: " + viewModel.Server.ServerSettings.LevelName); DirectoryInfo directoryInfo = new DirectoryInfo(Path.Combine(App.ServerPath, viewModel.Server.Name)); if (!directoryInfo.Exists) { return(false); } JavaVersion javaVersion = JavaVersionUtils.GetInstalledJavaVersion(viewModel.Server.JavaSettings.JavaPath); if (javaVersion == null) { ConsoleWriter.Write("ERROR: Java is not installed! Minecraft servers require Java!", viewModel); return(false); } if (!javaVersion.Is64Bit) { ConsoleWriter.Write( "WARN: The Java installation selected for this server is a 32-bit version, which can cause errors.", viewModel); } if (javaVersion.VersionComputed < 16) { if (new ServerVersion { Version = "1.17" }.CompareTo(viewModel.Entity.Version) <= 0) { ConsoleWriter.Write("ERROR: The Java installation selected for this server is outdated. Please update Java to version 16 or higher.", viewModel); return(false); } else { ConsoleWriter.Write("WARN: The Java installation selected for this server is outdated. Please update Java to version 16 or higher.", viewModel); } } if (!viewModel.Server.ServerSettings.ResourcePack.Equals("") && viewModel.Server.AutoSetSha1) { ConsoleWriter.Write(new ConsoleMessage("Generating Resource Pack hash...", ConsoleMessage.MessageLevel.INFO), viewModel); string resourcePackUrl = viewModel.Server.ServerSettings.ResourcePack.Replace("\\", ""); bool isHashUpToDate = await IsHashUpToDate(viewModel.Server.ResourcePackHashAge, resourcePackUrl); if (!string.IsNullOrEmpty(viewModel.Server.ServerSettings.ResourcePackSha1) && isHashUpToDate) { ConsoleWriter.Write(new ConsoleMessage("Resource Pack hash is still up to date. Staring server...", ConsoleMessage.MessageLevel.SUCCESS), viewModel); } else { ConsoleWriter.Write(new ConsoleMessage("Resource Pack hash is outdated. Updating it...", ConsoleMessage.MessageLevel.WARN), viewModel); DateTime hashAge = DateTime.Now; IProgress <double> downloadProgress = new Progress <double>(); string hash = await HashResourcePack(resourcePackUrl, downloadProgress); if (!string.IsNullOrEmpty(hash)) { viewModel.Server.ServerSettings.ResourcePackSha1 = hash; viewModel.Server.ResourcePackHashAge = hashAge; await viewModel.SaveProperties(); ConsoleWriter.Write(new ConsoleMessage("Successfully updated Resource Pack hash to: " + hash, ConsoleMessage.MessageLevel.SUCCESS), viewModel); ConsoleWriter.Write(new ConsoleMessage("Starting the server...", ConsoleMessage.MessageLevel.INFO), viewModel); } else { ConsoleWriter.Write(new ConsoleMessage( "Error updating the Resource Pack hash! Continuing with no hash...", ConsoleMessage.MessageLevel.ERROR), viewModel); } } } Process process = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo { UseShellExecute = false, RedirectStandardError = true, RedirectStandardInput = true, RedirectStandardOutput = true, FileName = viewModel.Server.JavaSettings.JavaPath, WorkingDirectory = directoryInfo.FullName, Arguments = "-Xmx" + viewModel.Server.JavaSettings.MaxRam + "m " + viewModel.Server.JavaSettings.StartupParameters + " -jar server.jar nogui", WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = true }; process.StartInfo = startInfo; process.Start(); Task.Run(() => { viewModel.TrackPerformance(process); }); viewModel.CurrentStatus = ServerStatus.STARTING; ConsoleWriter.RegisterApplication(viewModel, process.StandardOutput, process.StandardError); ConsoleReader consoleReader = new ConsoleReader(process.StandardInput); ServerAutomationManager.Instance.UpdateAutomation(viewModel); Task.Run(async() => { await process.WaitForExitAsync(); ApplicationManager.Instance.ActiveEntities.Remove(viewModel.Server); viewModel.CurrentStatus = ServerStatus.STOPPED; ServerAutomationManager.Instance.UpdateAutomation(viewModel); }); viewModel.ConsoleReader = consoleReader; ApplicationManager.Instance.ActiveEntities[viewModel.Server] = process; Task.Run(async() => { var worker = new QueryStatsWorker(viewModel); await process.WaitForExitAsync(); worker.Dispose(); }); Console.WriteLine("Started server " + viewModel.Server); //Register new world if created Task.Run(async() => { while (!viewModel.ServerRunning) { await Task.Delay(500); } viewModel.InitializeWorldsList(); }); return(true); }
/// <summary> /// Returns true if a given version if installed /// </summary> /// <param name="jreVersion">The java version to check</param> /// <returns></returns> public static Boolean IsInstalled(JavaVersion jreVersion) { if (!_initialized) { Initialize(); } return _javaPaths.ContainsKey(jreVersion); }
/// <summary> /// Check if a java version is 32 bit /// </summary> /// <param name="version">the version to check</param> /// <returns></returns> public static bool Is32Bitversion(JavaVersion version) { return (version.ToString().ToLower().Contains("x32")); }
/// <summary> /// Launch a new server /// </summary> /// <param name="server">the server type that will be executed</param> /// <param name="javaVersion">the java version to use</param> /// <param name="jarFile">the jar file to run</param> /// <param name="minMem">the minimum amount of memory to set</param> /// <param name="maxMem">the maximum amount of memory to set</param> /// <param name="defaultParameters">the parameters entered by the user (optional)</param> /// <param name="defaultFlags">the flags entered by the user (optional)</param> /// <param name="automated"> /// If this is an automated launch. Automated launches won't check for server updates and limit /// popups. /// </param> public void LaunchServer(IMinecraftServer server, JavaVersion javaVersion, string jarFile, UInt32 minMem, UInt32 maxMem, string defaultParameters = "", string defaultFlags = "", Boolean automated = false) { server.PrepareLaunch(); string parameters = server.GetLaunchParameters(defaultParameters); string flags = server.GetLaunchFlags(defaultFlags); string argument = parameters + " -Xms" + minMem + "M -Xmx" + maxMem + "M -jar \"" + jarFile + "\" " + flags; string executable = JavaApi.GetJavaPath(javaVersion); ProcessHandler.StartServer(executable, argument, server); }
/// <summary> /// Check if a java version is 32 bit /// </summary> /// <param name="version">the version to check</param> /// <returns></returns> public static bool Is32Bitversion(JavaVersion version) { return(version.ToString().ToLower().Contains("x32")); }
public async Task StartServerAsync(Server server) { if (server.Status != EntityStatus.Stopped) { await _console.WriteError(server, "Can't start server that was not properly stopped"); throw new ForkException("Only stopped servers can be started"); } await ChangeServerStatusAsync(server, EntityStatus.Starting); _logger.LogInformation($"Starting server {server.Name} on world {server.VanillaSettings.LevelName}"); // Get server directory DirectoryInfo serverDirectory = new DirectoryInfo(server.GetPath(_application)); if (!serverDirectory.Exists) { await ChangeServerStatusAsync(server, EntityStatus.Stopped); await _console.WriteError(server, $"This server has no directory for some reason. The path that was searched was:\n{server.GetPath(_application)}"); throw new ForkException($"Supplied server \"{server.Name}\" has no directory!"); } JavaVersion javaVersion = JavaVersionUtils.GetInstalledJavaVersion(server.JavaSettings.JavaPath); if (javaVersion == null) { await ChangeServerStatusAsync(server, EntityStatus.Stopped); await _console.WriteError(server, $"No valid Java installation was found for the configured java path:\n{server.JavaSettings.JavaPath}"); throw new ForkException( $"No valid Java installation was found for the configured java path:\n{server.JavaSettings.JavaPath}"); } if (!javaVersion.Is64Bit) { await _console.WriteWarning(server, "The Java installation selected for this server is a 32-bit version, which can cause errors and limits the RAM usage to 2GB"); } if (javaVersion.VersionComputed < 16) { if (new ServerVersion { Version = "1.17" }.CompareTo(server.Version) <= 0) { await ChangeServerStatusAsync(server, EntityStatus.Stopped); await _console.WriteError(server, "The Java installation selected for this server is outdated. Please update Java to version 16 or higher."); throw new ForkException( "The Java installation selected for this server is outdated. Please update Java to version 16 or higher."); } await _console.WriteWarning(server, "WARN: The Java installation selected for this server is outdated. Please update Java to version 16 or higher."); } if (server.VanillaSettings.ResourcePack != "" && server.AutoSetSha1) { await UpdateResourcePackHash(server); } Process process = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo { UseShellExecute = false, RedirectStandardError = true, RedirectStandardInput = true, RedirectStandardOutput = true, FileName = server.JavaSettings.JavaPath ?? "java", WorkingDirectory = serverDirectory.FullName, Arguments = "-Xmx" + server.JavaSettings.MaxRam + "m " + server.JavaSettings.StartupParameters + " -jar server.jar nogui", WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = true }; process.StartInfo = startInfo; process.Start(); CancellationTokenSource serverStoppedTokenSource = new CancellationTokenSource(); Task.Run(() => TrackServerPerformance(server, process, serverStoppedTokenSource)); await _console.BindProcessToConsole(server, process.StandardOutput, process.StandardError, status => _ = ChangeServerStatusAsync(server, status)); server.ConsoleHandler = delegate(string line) { _console.WriteLine(server, line, ConsoleMessageType.UserInput); process.StandardInput.WriteLineAsync(line); }; //TODO CKE add server automation //ServerAutomationManager.Instance.UpdateAutomation(viewModel); _ = Task.Run(async() => { //TODO CKE start performance tracker here // var worker = new QueryStatsWorker(viewModel); await process.WaitForExitAsync(); //TODO determine crash and normal stop await ChangeServerStatusAsync(server, EntityStatus.Stopped); serverStoppedTokenSource.Cancel(); //TODO CKE stop automation here //ServerAutomationManager.Instance.UpdateAutomation(viewModel); }); _logger.LogInformation("Started server " + server.Name); //Register new world if created _ = Task.Run(async() => { while (server.Status == EntityStatus.Starting) { await Task.Delay(500); } if (server.Status == EntityStatus.Started) { // TODO CKE update Worlds as a new one might have been created // viewModel.InitializeWorldsList(); } }); }
/// <summary> /// Get the absolute location of an installed java version /// </summary> /// <param name="jreVersion">The java version to retrieve</param> /// <returns>Returns the absolute path to java.exe</returns> public static string GetJavaPath(JavaVersion jreVersion) { if (!_initialized) { Initialize(); } Logger.Log(LogLevel.Info, "JavaApi","Getting Java Path for version " + jreVersion); string path; if (jreVersion == JavaVersion.Custom) { path = Config.ReadString("starter", "java_custom", ""); if (string.IsNullOrEmpty(path)) { OpenFileDialog ofd = new OpenFileDialog { Title = "Select java.exe", Filter = "Java executables (*.exe) |*.exe|All Files (*.*)|*.*" }; ofd.ShowDialog(); path = ofd.FileName; Config.WriteString("starter", "java_custom", path); } Logger.Log(LogLevel.Info, "JavaApi", "Using custom java path: " + path); return path; } if (_javaPaths.ContainsKey(jreVersion)) { path = _javaPaths[jreVersion]; Logger.Log(LogLevel.Info, "JavaApi", "Using java " + jreVersion + ", path: " + path); return path; } Logger.Log(LogLevel.Warning, "JavaApi", "Java " + jreVersion + " requested but not found!"); return null; }