mirror of
https://github.com/CommonLoon102/NBloodServerSupervisor.git
synced 2024-12-22 18:52:44 +01:00
use blood working directory
This commit is contained in:
parent
9f696fdd7f
commit
496ad6629b
11
Common/Constants.cs
Normal file
11
Common/Constants.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Common
|
||||
{
|
||||
public class Constants
|
||||
{
|
||||
public const string NBloodExecutable = "nblood_server";
|
||||
}
|
||||
}
|
36
Common/NBloodServerStartInfo.cs
Normal file
36
Common/NBloodServerStartInfo.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace Common
|
||||
{
|
||||
public static class NBloodServerStartInfo
|
||||
{
|
||||
private static readonly string workingDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "blood");
|
||||
|
||||
public static ProcessStartInfo Get(int maxPlayers, int port)
|
||||
{
|
||||
var psi = new ProcessStartInfo(GetExecutable(), $"-server {maxPlayers} -port {port} -pname Server")
|
||||
{
|
||||
UseShellExecute = true,
|
||||
WorkingDirectory = workingDir
|
||||
};
|
||||
|
||||
return psi;
|
||||
}
|
||||
|
||||
private static string GetExecutable()
|
||||
{
|
||||
string nbloodServer = Constants.NBloodExecutable;
|
||||
bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
||||
if (isWindows)
|
||||
nbloodServer += ".exe";
|
||||
|
||||
return nbloodServer;
|
||||
}
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
using Model;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
@ -45,11 +46,7 @@ namespace Supervisor
|
||||
if (IsNewServerNeeded(i))
|
||||
{
|
||||
int port = PortUtils.GetPort();
|
||||
string nbloodServer = "nblood_server";
|
||||
bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
||||
if (isWindows)
|
||||
nbloodServer += ".exe";
|
||||
var process = Process.Start(nbloodServer, $"-server {i} -port {port}");
|
||||
var process = Process.Start(NBloodServerStartInfo.Get(i, port));
|
||||
Program.State.Servers.AddOrUpdate(port, new Server()
|
||||
{
|
||||
Port = port,
|
||||
|
@ -64,19 +64,14 @@ namespace WebInterface.Controllers
|
||||
if (parameters.ApiKey != _config.GetValue<string>("ApiKey"))
|
||||
return new StartServerResponse("Invalid ApiKey.");
|
||||
|
||||
string nbloodPath = _config.GetValue<string>("NBloodPath");
|
||||
if (!System.IO.File.Exists(nbloodPath))
|
||||
throw new Exception($"The configured path for the nblood executable is invalid.");
|
||||
|
||||
string processName = Path.GetFileNameWithoutExtension(nbloodPath);
|
||||
string processName = Constants.NBloodExecutable;
|
||||
int serversRunning = Process.GetProcessesByName(processName).Count();
|
||||
if (serversRunning >= _config.GetValue<int>("MaximumServers"))
|
||||
return new StartServerResponse("The maximum number of servers are already running.");
|
||||
|
||||
int port = PortUtils.GetPort();
|
||||
|
||||
string args = BuildArgs(parameters, port);
|
||||
var process = Process.Start(nbloodPath, args);
|
||||
var process = Process.Start(NBloodServerStartInfo.Get(parameters.Players, port));
|
||||
byte[] payload = Encoding.ASCII.GetBytes($"B{port}\t{process.Id}\0");
|
||||
socket.SendTo(payload, webApiListenerEndPoint);
|
||||
|
||||
@ -97,17 +92,6 @@ namespace WebInterface.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
private static string BuildArgs(ServerParameters parameters, int port)
|
||||
{
|
||||
string args = $"-server {parameters.Players} -port {port}";
|
||||
if (parameters.IsBroadcast)
|
||||
{
|
||||
args += " -broadcast";
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("[controller]/api/listservers")]
|
||||
public ListServersResponse ListServers()
|
||||
|
@ -9,6 +9,5 @@ namespace WebInterface
|
||||
{
|
||||
public string ApiKey { get; set; }
|
||||
public int Players { get; set; }
|
||||
public bool IsBroadcast { get; set; }
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user