mirror of
https://github.com/CommonLoon102/NBloodServerSupervisor.git
synced 2024-12-22 18:52:44 +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.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace Common
|
||||
{
|
||||
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) =>
|
||||
$"nblood -client {host} -port {port} {modCommandLine}";
|
||||
}
|
||||
|
@ -2,8 +2,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
@ -18,7 +16,6 @@ namespace Common
|
||||
private const int maximumServers = 80;
|
||||
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();
|
||||
|
||||
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}")
|
||||
{
|
||||
UseShellExecute = true,
|
||||
WorkingDirectory = workingDir
|
||||
WorkingDirectory = CommandLineUtils.BloodDir
|
||||
};
|
||||
|
||||
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(IPrivateServerService), typeof(PrivateServerService), ServiceLifetime.Transient));
|
||||
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.
|
||||
|
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">
|
||||
<div class="main">
|
||||
<header>
|
||||
<div style="float: left; margin-right: 1em;">
|
||||
<form method="get">
|
||||
<button asp-route="Home">Home</button>
|
||||
</form>
|
||||
</div>
|
||||
<form method="get">
|
||||
<button asp-route="Home">Home</button>
|
||||
<button asp-route="CustomMaps">Custom Maps</button>
|
||||
</form>
|
||||
<hr />
|
||||
<p>I live... Again!</p>
|
||||
|
Loading…
Reference in New Issue
Block a user