mirror of
https://github.com/CommonLoon102/NBloodServerSupervisor.git
synced 2024-12-31 23:22:45 +01:00
show available custom maps and let them download (#8)
This commit is contained in:
parent
90ed231088
commit
3417571b70
@ -1,11 +1,15 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace Common
|
namespace Common
|
||||||
{
|
{
|
||||||
public static class CommandLineUtils
|
public static class CommandLineUtils
|
||||||
{
|
{
|
||||||
|
public static string BloodDir => Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "blood");
|
||||||
|
|
||||||
public static string GetClientLaunchCommand(string host, int port, string modCommandLine) =>
|
public static string GetClientLaunchCommand(string host, int port, string modCommandLine) =>
|
||||||
$"nblood -client {host} -port {port} {modCommandLine}";
|
$"nblood -client {host} -port {port} {modCommandLine}";
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -18,7 +16,6 @@ namespace Common
|
|||||||
private const int maximumServers = 80;
|
private const int maximumServers = 80;
|
||||||
private const string nBloodExecutable = "nblood_server";
|
private const string nBloodExecutable = "nblood_server";
|
||||||
|
|
||||||
private static readonly string workingDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "blood");
|
|
||||||
private static readonly Random rnd = new Random();
|
private static readonly Random rnd = new Random();
|
||||||
|
|
||||||
public static SpawnedServerInfo SpawnServer(int players, string modName)
|
public static SpawnedServerInfo SpawnServer(int players, string modName)
|
||||||
@ -67,7 +64,7 @@ namespace Common
|
|||||||
var psi = new ProcessStartInfo(GetExecutableName(), $"-server {maxPlayers} -port {port} -pname Server {mod.CommandLine}")
|
var psi = new ProcessStartInfo(GetExecutableName(), $"-server {maxPlayers} -port {port} -pname Server {mod.CommandLine}")
|
||||||
{
|
{
|
||||||
UseShellExecute = true,
|
UseShellExecute = true,
|
||||||
WorkingDirectory = workingDir
|
WorkingDirectory = CommandLineUtils.BloodDir
|
||||||
};
|
};
|
||||||
|
|
||||||
return psi;
|
return psi;
|
||||||
|
33
WebInterface/Controllers/CustomMapsController.cs
Normal file
33
WebInterface/Controllers/CustomMapsController.cs
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using WebInterface.Services;
|
||||||
|
|
||||||
|
namespace WebInterface.Controllers
|
||||||
|
{
|
||||||
|
public class CustomMapsController : Controller
|
||||||
|
{
|
||||||
|
private readonly ICustomMapService _customMapService;
|
||||||
|
|
||||||
|
public CustomMapsController(ICustomMapService customMapService)
|
||||||
|
{
|
||||||
|
_customMapService = customMapService;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Route("nblood/custommaps", Name = "CustomMaps")]
|
||||||
|
public IActionResult Index()
|
||||||
|
{
|
||||||
|
var viewModel = _customMapService.ListCustomMaps();
|
||||||
|
return View(viewModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Route("nblood/custommaps/download", Name = "DownloadCustomMap")]
|
||||||
|
public FileResult Index([FromQuery] string map)
|
||||||
|
{
|
||||||
|
byte[] customMap = _customMapService.GetCustomMapBytes(map);
|
||||||
|
return File(customMap, System.Net.Mime.MediaTypeNames.Application.Octet, map);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
55
WebInterface/Services/CustomMapService.cs
Normal file
55
WebInterface/Services/CustomMapService.cs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace WebInterface.Services
|
||||||
|
{
|
||||||
|
public class CustomMapService : ICustomMapService
|
||||||
|
{
|
||||||
|
private static readonly List<string> crypticMaps = new List<string>()
|
||||||
|
{
|
||||||
|
"CPSL.MAP",
|
||||||
|
"CP01.MAP",
|
||||||
|
"CP02.MAP",
|
||||||
|
"CP03.MAP",
|
||||||
|
"CP04.MAP",
|
||||||
|
"CP05.MAP",
|
||||||
|
"CP06.MAP",
|
||||||
|
"CP07.MAP",
|
||||||
|
"CP08.MAP",
|
||||||
|
"CP09.MAP",
|
||||||
|
"CPBB01.MAP",
|
||||||
|
"CPBB02.MAP",
|
||||||
|
"CPBB03.MAP",
|
||||||
|
"CPBB04.MAP",
|
||||||
|
};
|
||||||
|
|
||||||
|
private List<string> ListableCustomMaps => Directory.GetFiles(Common.CommandLineUtils.BloodDir,
|
||||||
|
"*.map", SearchOption.TopDirectoryOnly)
|
||||||
|
.Select(m => Path.GetFileName(m))
|
||||||
|
.Where(m => !ContainsString(crypticMaps, m))
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
public IList<string> ListCustomMaps() => ListableCustomMaps;
|
||||||
|
|
||||||
|
public byte[] GetCustomMapBytes(string map)
|
||||||
|
{
|
||||||
|
if (ListableCustomMaps.Any(m => StringsAreSame(m, map)))
|
||||||
|
{
|
||||||
|
return File.ReadAllBytes(Path.Combine(Common.CommandLineUtils.BloodDir, map));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new Exception($"Cannot download this map: {map}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool ContainsString(IEnumerable<string> list, string value) =>
|
||||||
|
list.Any(e => StringsAreSame(e, value));
|
||||||
|
|
||||||
|
private bool StringsAreSame(string left, string right) =>
|
||||||
|
string.Compare(left, right, StringComparison.OrdinalIgnoreCase) == 0;
|
||||||
|
}
|
||||||
|
}
|
13
WebInterface/Services/ICustomMapService.cs
Normal file
13
WebInterface/Services/ICustomMapService.cs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace WebInterface.Services
|
||||||
|
{
|
||||||
|
public interface ICustomMapService
|
||||||
|
{
|
||||||
|
IList<string> ListCustomMaps();
|
||||||
|
byte[] GetCustomMapBytes(string map);
|
||||||
|
}
|
||||||
|
}
|
@ -42,6 +42,7 @@ namespace WebInterface
|
|||||||
services.Add(new ServiceDescriptor(typeof(IStateService), typeof(StateService), ServiceLifetime.Singleton));
|
services.Add(new ServiceDescriptor(typeof(IStateService), typeof(StateService), ServiceLifetime.Singleton));
|
||||||
services.Add(new ServiceDescriptor(typeof(IPrivateServerService), typeof(PrivateServerService), ServiceLifetime.Transient));
|
services.Add(new ServiceDescriptor(typeof(IPrivateServerService), typeof(PrivateServerService), ServiceLifetime.Transient));
|
||||||
services.Add(new ServiceDescriptor(typeof(IRateLimiterService), typeof(RateLimiterService), ServiceLifetime.Singleton));
|
services.Add(new ServiceDescriptor(typeof(IRateLimiterService), typeof(RateLimiterService), ServiceLifetime.Singleton));
|
||||||
|
services.Add(new ServiceDescriptor(typeof(ICustomMapService), typeof(CustomMapService), ServiceLifetime.Transient));
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
|
14
WebInterface/Views/CustomMaps/Index.cshtml
Normal file
14
WebInterface/Views/CustomMaps/Index.cshtml
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
@model List<string>
|
||||||
|
@{
|
||||||
|
ViewData["Title"] = "Custom Maps";
|
||||||
|
Layout = "~/Views/Shared/_Layout.cshtml";
|
||||||
|
}
|
||||||
|
|
||||||
|
<h1>List of available custom maps</h1>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
@foreach (var map in Model)
|
||||||
|
{
|
||||||
|
<li><a href="@Url.Link("DownloadCustomMap", new { map = map })">@map</a></li>
|
||||||
|
}
|
||||||
|
</ul>
|
@ -35,8 +35,13 @@
|
|||||||
<body style="background-color: cornsilk">
|
<body style="background-color: cornsilk">
|
||||||
<div class="main">
|
<div class="main">
|
||||||
<header>
|
<header>
|
||||||
|
<div style="float: left; margin-right: 1em;">
|
||||||
|
<form method="get">
|
||||||
|
<button asp-route="Home">Home</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
<form method="get">
|
<form method="get">
|
||||||
<button asp-route="Home">Home</button>
|
<button asp-route="CustomMaps">Custom Maps</button>
|
||||||
</form>
|
</form>
|
||||||
<hr />
|
<hr />
|
||||||
<p>I live... Again!</p>
|
<p>I live... Again!</p>
|
||||||
|
Loading…
Reference in New Issue
Block a user