refactor and test log path generation to support pluto IW5 better

This commit is contained in:
RaidMax 2020-04-13 16:16:31 -05:00
parent b63d2995ed
commit be8041b868
4 changed files with 118 additions and 21 deletions

View File

@ -1,7 +1,5 @@
using IW4MAdmin.Application.EventParsers; using IW4MAdmin.Application.IO;
using IW4MAdmin.Application.IO;
using IW4MAdmin.Application.Misc; using IW4MAdmin.Application.Misc;
using IW4MAdmin.Application.RconParsers;
using SharedLibraryCore; using SharedLibraryCore;
using SharedLibraryCore.Configuration; using SharedLibraryCore.Configuration;
using SharedLibraryCore.Database.Models; using SharedLibraryCore.Database.Models;
@ -945,10 +943,6 @@ namespace IW4MAdmin
var logsync = await this.GetDvarAsync<int>("g_logsync"); var logsync = await this.GetDvarAsync<int>("g_logsync");
var ip = await this.GetDvarAsync<string>("net_ip"); var ip = await this.GetDvarAsync<string>("net_ip");
WorkingDirectory = Directory.Exists(basegame?.Value ?? "") ?
basegame.Value :
basepath.Value;
try try
{ {
var website = await this.GetDvarAsync<string>("_website"); var website = await this.GetDvarAsync<string>("_website");
@ -970,6 +964,7 @@ namespace IW4MAdmin
InitializeMaps(); InitializeMaps();
WorkingDirectory = basepath.Value;
this.Hostname = hostname; this.Hostname = hostname;
this.MaxClients = maxplayers; this.MaxClients = maxplayers;
this.FSGame = game; this.FSGame = game;
@ -1014,17 +1009,7 @@ namespace IW4MAdmin
else else
{ {
string mainPath = EventParser.Configuration.GameDirectory; LogPath = GenerateLogPath(basegame.Value, basepath.Value, EventParser.Configuration.GameDirectory, game, logfile.Value);
LogPath = string.IsNullOrEmpty(game) ?
$"{WorkingDirectory.Replace('\\', Path.DirectorySeparatorChar)}{Path.DirectorySeparatorChar}{mainPath}{Path.DirectorySeparatorChar}{logfile?.Value}" :
$"{WorkingDirectory.Replace('\\', Path.DirectorySeparatorChar)}{Path.DirectorySeparatorChar}{game?.Replace('/', Path.DirectorySeparatorChar)}{Path.DirectorySeparatorChar}{logfile?.Value}";
// fix wine drive name mangling
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
LogPath = Regex.Replace($"{Path.DirectorySeparatorChar}{LogPath}", @"[A-Z]:/", "");
}
if (!File.Exists(LogPath) && ServerConfig.GameLogServerUrl == null) if (!File.Exists(LogPath) && ServerConfig.GameLogServerUrl == null)
{ {
@ -1042,6 +1027,37 @@ namespace IW4MAdmin
#endif #endif
} }
public static string GenerateLogPath(string baseGameDirectory, string basePathDirectory, string gameDirectory, string modDirectory, string logFile)
{
string logPath;
string workingDirectory = basePathDirectory;
// we want to see if base game is provided and it 'looks' like a directory
if (!string.IsNullOrWhiteSpace(baseGameDirectory) &&
baseGameDirectory.IndexOfAny(Utilities.DirectorySeparatorChars) != -1)
{
workingDirectory = baseGameDirectory;
}
if (string.IsNullOrWhiteSpace(modDirectory))
{
logPath = Path.Combine(workingDirectory, gameDirectory, logFile);
}
else
{
logPath = Path.Combine(workingDirectory, modDirectory, logFile);
}
// fix wine drive name mangling
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
logPath = Regex.Replace($"{Path.DirectorySeparatorChar}{logPath}", @"[A-Z]:/", "");
}
return logPath.FixDirectoryCharacters();
}
protected override async Task Warn(String Reason, EFClient Target, EFClient Origin) protected override async Task Warn(String Reason, EFClient Target, EFClient Origin)
{ {
// ensure player gets warned if command not performed on them in game // ensure player gets warned if command not performed on them in game

View File

@ -25,7 +25,7 @@ var plugin = {
rconParser.Configuration.Dvar.AddMapping(106, 2); rconParser.Configuration.Dvar.AddMapping(106, 2);
rconParser.Configuration.Dvar.AddMapping(107, 3); rconParser.Configuration.Dvar.AddMapping(107, 3);
rconParser.Configuration.WaitForResponse = false; rconParser.Configuration.WaitForResponse = false;
rconParser.Configuration.CanGenerateLogPath = true; rconParser.Configuration.CanGenerateLogPath = true;
rconParser.Configuration.Status.Pattern = '^ *([0-9]+) +([0-9]+) +(?:[0-1]{1}) +([0-9]+) +([A-F0-9]+) +(.+?) +(?:[0-9]+) +(\\d+\\.\\d+\\.\\d+\\.\\d+\\:-?\\d{1,5}|0+\\.0+:-?\\d{1,5}|loopback) +(?:-?[0-9]+) +(?:[0-9]+) *$'; rconParser.Configuration.Status.Pattern = '^ *([0-9]+) +([0-9]+) +(?:[0-1]{1}) +([0-9]+) +([A-F0-9]+) +(.+?) +(?:[0-9]+) +(\\d+\\.\\d+\\.\\d+\\.\\d+\\:-?\\d{1,5}|0+\\.0+:-?\\d{1,5}|loopback) +(?:-?[0-9]+) +(?:[0-9]+) *$';
rconParser.Configuration.Status.AddMapping(100, 1); rconParser.Configuration.Status.AddMapping(100, 1);
@ -36,9 +36,11 @@ var plugin = {
rconParser.Configuration.Status.AddMapping(105, 6); rconParser.Configuration.Status.AddMapping(105, 6);
rconParser.Version = 'IW5 MP 1.9 build 388110 Fri Sep 14 00:04:28 2012 win-x86'; rconParser.Version = 'IW5 MP 1.9 build 388110 Fri Sep 14 00:04:28 2012 win-x86';
rconParser.GameName = 3; // T5 rconParser.GameName = 3; // IW5
eventParser.Version = 'IW5 MP 1.9 build 388110 Fri Sep 14 00:04:28 2012 win-x86'; eventParser.Version = 'IW5 MP 1.9 build 388110 Fri Sep 14 00:04:28 2012 win-x86';
eventParser.GameName = 3; // T5 eventParser.GameName = 3; // IW5
eventParser.Configuration.GameDirectory = '';
}, },
onUnloadAsync: function () { onUnloadAsync: function () {

View File

@ -32,6 +32,7 @@ namespace SharedLibraryCore
public static Encoding EncodingType; public static Encoding EncodingType;
public static Localization.Layout CurrentLocalization = new Localization.Layout(new Dictionary<string, string>()); public static Localization.Layout CurrentLocalization = new Localization.Layout(new Dictionary<string, string>());
public static TimeSpan DefaultCommandTimeout = new TimeSpan(0, 0, 25); public static TimeSpan DefaultCommandTimeout = new TimeSpan(0, 0, 25);
public static char[] DirectorySeparatorChars = new[] { '\\', '/' };
public static EFClient IW4MAdminClient(Server server = null) public static EFClient IW4MAdminClient(Server server = null)
{ {
@ -873,6 +874,21 @@ namespace SharedLibraryCore
public static bool ShouldHideLevel(this Permission perm) => perm == Permission.Flagged; public static bool ShouldHideLevel(this Permission perm) => perm == Permission.Flagged;
/// <summary>
/// replaces any directory separator chars with the platform specific character
/// </summary>
/// <param name="path">original file path</param>
/// <returns></returns>
public static string FixDirectoryCharacters(this string path)
{
foreach (char separator in DirectorySeparatorChars)
{
path = path.Replace(separator, Path.DirectorySeparatorChar);
}
return path;
}
#if DEBUG == true #if DEBUG == true
public static string ToSql<TEntity>(this IQueryable<TEntity> query) where TEntity : class public static string ToSql<TEntity>(this IQueryable<TEntity> query) where TEntity : class
{ {

View File

@ -0,0 +1,63 @@
using IW4MAdmin;
using NUnit.Framework;
namespace ApplicationTests
{
[TestFixture]
public class IW4MServerTests
{
[Test]
public void Test_GenerateLogPath_Basic()
{
string expected = "C:\\Game\\main\\log.log";
string generated = IW4MServer.GenerateLogPath("", "C:\\Game", "main", null, "log.log");
Assert.AreEqual(expected, generated);
}
[Test]
public void Test_GenerateLogPath_WithMod()
{
string expected = "C:\\Game\\mods\\mod\\log.log";
string generated = IW4MServer.GenerateLogPath("", "C:\\Game", "main", "mods\\mod", "log.log");
Assert.AreEqual(expected, generated);
}
[Test]
public void Test_GenerateLogPath_WithBasePath()
{
string expected = "C:\\GameAlt\\main\\log.log";
string generated = IW4MServer.GenerateLogPath("C:\\GameAlt", "C:\\Game", "main", null, "log.log");
Assert.AreEqual(expected, generated);
}
[Test]
public void Test_GenerateLogPath_WithBasePathAndMod()
{
string expected = "C:\\GameAlt\\mods\\mod\\log.log";
string generated = IW4MServer.GenerateLogPath("C:\\GameAlt", "C:\\Game", "main", "mods\\mod", "log.log");
Assert.AreEqual(expected, generated);
}
[Test]
public void Test_GenerateLogPath_InvalidBasePath()
{
string expected = "C:\\Game\\main\\log.log";
string generated = IW4MServer.GenerateLogPath("game", "C:\\Game", "main", null, "log.log");
Assert.AreEqual(expected, generated);
}
[Test]
public void Test_GenerateLogPath_BadSeparators()
{
string expected = "C:\\Game\\main\\folder\\log.log";
string generated = IW4MServer.GenerateLogPath("", "C:/Game", "main/folder", null, "log.log");
Assert.AreEqual(expected, generated);
}
}
}