use blood working directory

This commit is contained in:
CommonLoon102 2020-01-24 14:34:10 +01:00
parent 9f696fdd7f
commit 496ad6629b
5 changed files with 51 additions and 24 deletions

11
Common/Constants.cs Normal file
View 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";
}
}

View 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;
}
}
}

View File

@ -2,6 +2,7 @@
using Model; using Model;
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
@ -45,11 +46,7 @@ namespace Supervisor
if (IsNewServerNeeded(i)) if (IsNewServerNeeded(i))
{ {
int port = PortUtils.GetPort(); int port = PortUtils.GetPort();
string nbloodServer = "nblood_server"; var process = Process.Start(NBloodServerStartInfo.Get(i, port));
bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
if (isWindows)
nbloodServer += ".exe";
var process = Process.Start(nbloodServer, $"-server {i} -port {port}");
Program.State.Servers.AddOrUpdate(port, new Server() Program.State.Servers.AddOrUpdate(port, new Server()
{ {
Port = port, Port = port,

View File

@ -64,19 +64,14 @@ namespace WebInterface.Controllers
if (parameters.ApiKey != _config.GetValue<string>("ApiKey")) if (parameters.ApiKey != _config.GetValue<string>("ApiKey"))
return new StartServerResponse("Invalid ApiKey."); return new StartServerResponse("Invalid ApiKey.");
string nbloodPath = _config.GetValue<string>("NBloodPath"); string processName = Constants.NBloodExecutable;
if (!System.IO.File.Exists(nbloodPath))
throw new Exception($"The configured path for the nblood executable is invalid.");
string processName = Path.GetFileNameWithoutExtension(nbloodPath);
int serversRunning = Process.GetProcessesByName(processName).Count(); int serversRunning = Process.GetProcessesByName(processName).Count();
if (serversRunning >= _config.GetValue<int>("MaximumServers")) if (serversRunning >= _config.GetValue<int>("MaximumServers"))
return new StartServerResponse("The maximum number of servers are already running."); return new StartServerResponse("The maximum number of servers are already running.");
int port = PortUtils.GetPort(); int port = PortUtils.GetPort();
string args = BuildArgs(parameters, port); var process = Process.Start(NBloodServerStartInfo.Get(parameters.Players, port));
var process = Process.Start(nbloodPath, args);
byte[] payload = Encoding.ASCII.GetBytes($"B{port}\t{process.Id}\0"); byte[] payload = Encoding.ASCII.GetBytes($"B{port}\t{process.Id}\0");
socket.SendTo(payload, webApiListenerEndPoint); 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] [HttpGet]
[Route("[controller]/api/listservers")] [Route("[controller]/api/listservers")]
public ListServersResponse ListServers() public ListServersResponse ListServers()

View File

@ -9,6 +9,5 @@ namespace WebInterface
{ {
public string ApiKey { get; set; } public string ApiKey { get; set; }
public int Players { get; set; } public int Players { get; set; }
public bool IsBroadcast { get; set; }
} }
} }