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 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,

View File

@ -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()

View File

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