diff --git a/Application/IW4MServer.cs b/Application/IW4MServer.cs index 4594adfc..7d4f88ed 100644 --- a/Application/IW4MServer.cs +++ b/Application/IW4MServer.cs @@ -1009,7 +1009,16 @@ namespace IW4MAdmin else { - LogPath = GenerateLogPath(basegame.Value, basepath.Value, EventParser.Configuration.GameDirectory, game, logfile.Value); + var logInfo = new LogPathGeneratorInfo() + { + BaseGameDirectory = basegame.Value, + BasePathDirectory = basepath.Value, + GameDirectory = EventParser.Configuration.GameDirectory ?? "", + ModDirectory = game ?? "", + LogFile = logfile.Value, + IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) + }; + LogPath = GenerateLogPath(logInfo); if (!File.Exists(LogPath) && ServerConfig.GameLogServerUrl == null) { @@ -1027,37 +1036,37 @@ namespace IW4MAdmin #endif } - public static string GenerateLogPath(string baseGameDirectory, string basePathDirectory, string gameDirectory, string modDirectory, string logFile) + public static string GenerateLogPath(LogPathGeneratorInfo logInfo) { string logPath; - string workingDirectory = basePathDirectory; + string workingDirectory = logInfo.BasePathDirectory; - bool baseGameIsDirectory = !string.IsNullOrWhiteSpace(baseGameDirectory) && - baseGameDirectory.IndexOfAny(Utilities.DirectorySeparatorChars) != -1; + bool baseGameIsDirectory = !string.IsNullOrWhiteSpace(logInfo.BaseGameDirectory) && + logInfo.BaseGameDirectory.IndexOfAny(Utilities.DirectorySeparatorChars) != -1; - bool baseGameIsRelative = baseGameDirectory.FixDirectoryCharacters() - .Equals(gameDirectory?.FixDirectoryCharacters() ?? "", StringComparison.InvariantCultureIgnoreCase); + bool baseGameIsRelative = logInfo.BaseGameDirectory.FixDirectoryCharacters() + .Equals(logInfo.GameDirectory.FixDirectoryCharacters(), StringComparison.InvariantCultureIgnoreCase); // we want to see if base game is provided and it 'looks' like a directory if (baseGameIsDirectory && !baseGameIsRelative) { - workingDirectory = baseGameDirectory; + workingDirectory = logInfo.BaseGameDirectory; } - if (string.IsNullOrWhiteSpace(modDirectory)) + if (string.IsNullOrWhiteSpace(logInfo.ModDirectory)) { - logPath = Path.Combine(workingDirectory, gameDirectory ?? "", logFile); + logPath = Path.Combine(workingDirectory, logInfo.GameDirectory, logInfo.LogFile); } else { - logPath = Path.Combine(workingDirectory, modDirectory, logFile); + logPath = Path.Combine(workingDirectory, logInfo.ModDirectory, logInfo.LogFile); } // fix wine drive name mangling - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (!logInfo.IsWindows) { - logPath = Regex.Replace($"{Path.DirectorySeparatorChar}{logPath}", @"[A-Z]:/", ""); + logPath = $"{Path.DirectorySeparatorChar}{Regex.Replace(logPath, @"[A-Z]:(\/|\\)", "")}"; } return logPath.FixDirectoryCharacters(); diff --git a/Application/Misc/LogPathGeneratorInfo.cs b/Application/Misc/LogPathGeneratorInfo.cs new file mode 100644 index 00000000..d2cc778f --- /dev/null +++ b/Application/Misc/LogPathGeneratorInfo.cs @@ -0,0 +1,45 @@ +using System.Runtime.InteropServices; + +namespace IW4MAdmin.Application.Misc +{ + /// + /// dto class for handling log path generation + /// + public class LogPathGeneratorInfo + { + /// + /// directory under the paths where data comes from by default + /// fs_basegame + /// + public string BaseGameDirectory { get; set; } = ""; + + /// + /// base game root path + /// fs_basepath + /// + public string BasePathDirectory { get; set; } = ""; + + /// + /// overide game directory + /// plugin driven + /// + public string GameDirectory { get; set; } = ""; + + /// + /// game director + /// fs_game + /// + public string ModDirectory { get; set; } = ""; + + /// + /// log file name + /// g_log + /// + public string LogFile { get; set; } = ""; + + /// + /// indicates if running on windows + /// + public bool IsWindows { get; set; } = true; + } +} diff --git a/Tests/ApplicationTests/IW4MServerTests.cs b/Tests/ApplicationTests/IW4MServerTests.cs index b52f709d..e6607a31 100644 --- a/Tests/ApplicationTests/IW4MServerTests.cs +++ b/Tests/ApplicationTests/IW4MServerTests.cs @@ -1,4 +1,5 @@ using IW4MAdmin; +using IW4MAdmin.Application.Misc; using NUnit.Framework; namespace ApplicationTests @@ -10,7 +11,13 @@ namespace ApplicationTests public void Test_GenerateLogPath_Basic() { string expected = "C:\\Game\\main\\log.log"; - string generated = IW4MServer.GenerateLogPath("", "C:\\Game", "main", null, "log.log"); + var info = new LogPathGeneratorInfo() + { + BasePathDirectory = "C:\\Game", + GameDirectory = "main", + LogFile = "log.log" + }; + string generated = IW4MServer.GenerateLogPath(info); Assert.AreEqual(expected, generated); } @@ -19,25 +26,47 @@ namespace ApplicationTests public void Test_GenerateLogPath_WithMod() { string expected = "C:\\Game\\mods\\mod\\log.log"; - string generated = IW4MServer.GenerateLogPath("", "C:\\Game", "main", "mods\\mod", "log.log"); + var info = new LogPathGeneratorInfo() + { + BasePathDirectory = "C:\\Game", + GameDirectory = "main", + ModDirectory = "mods\\mod", + LogFile = "log.log" + }; + string generated = IW4MServer.GenerateLogPath(info); Assert.AreEqual(expected, generated); } [Test] - public void Test_GenerateLogPath_WithBasePath() + public void Test_GenerateLogPath_WithBaseGame() { string expected = "C:\\GameAlt\\main\\log.log"; - string generated = IW4MServer.GenerateLogPath("C:\\GameAlt", "C:\\Game", "main", null, "log.log"); + var info = new LogPathGeneratorInfo() + { + BaseGameDirectory = "C:\\GameAlt", + BasePathDirectory = "C:\\Game", + GameDirectory = "main", + LogFile = "log.log" + }; + string generated = IW4MServer.GenerateLogPath(info); Assert.AreEqual(expected, generated); } [Test] - public void Test_GenerateLogPath_WithBasePathAndMod() + public void Test_GenerateLogPath_WithBaseGameAndMod() { string expected = "C:\\GameAlt\\mods\\mod\\log.log"; - string generated = IW4MServer.GenerateLogPath("C:\\GameAlt", "C:\\Game", "main", "mods\\mod", "log.log"); + var info = new LogPathGeneratorInfo() + { + BaseGameDirectory = "C:\\GameAlt", + BasePathDirectory = "C:\\Game", + GameDirectory = "main", + ModDirectory = "mods\\mod", + LogFile = "log.log" + }; + string generated = IW4MServer.GenerateLogPath(info); Assert.AreEqual(expected, generated); } @@ -46,7 +75,14 @@ namespace ApplicationTests public void Test_GenerateLogPath_InvalidBasePath() { string expected = "C:\\Game\\main\\log.log"; - string generated = IW4MServer.GenerateLogPath("game", "C:\\Game", "main", null, "log.log"); + var info = new LogPathGeneratorInfo() + { + BaseGameDirectory = "game", + BasePathDirectory = "C:\\Game", + GameDirectory = "main", + LogFile = "log.log" + }; + string generated = IW4MServer.GenerateLogPath(info); Assert.AreEqual(expected, generated); } @@ -55,7 +91,13 @@ namespace ApplicationTests public void Test_GenerateLogPath_BadSeparators() { string expected = "C:\\Game\\main\\folder\\log.log"; - string generated = IW4MServer.GenerateLogPath("", "C:/Game", "main/folder", null, "log.log"); + var info = new LogPathGeneratorInfo() + { + BasePathDirectory = "C:/Game", + GameDirectory = "main/folder", + LogFile = "log.log" + }; + string generated = IW4MServer.GenerateLogPath(info); Assert.AreEqual(expected, generated); } @@ -64,7 +106,30 @@ namespace ApplicationTests public void Test_GenerateLogPath_RelativeBasePath() { string expected = "C:\\Game\\main\\folder\\log.log"; - string generated = IW4MServer.GenerateLogPath("main\\folder", "C:\\Game", "main\\folder", null, "log.log"); + var info = new LogPathGeneratorInfo() + { + BaseGameDirectory = "main\\folder", + BasePathDirectory = "C:\\Game", + GameDirectory = "main\\folder", + LogFile = "log.log" + }; + string generated = IW4MServer.GenerateLogPath(info); + + Assert.AreEqual(expected, generated); + } + + [Test] + public void Test_GenerateLogPath_FixWineDriveMangling() + { + string expected = "/opt/server/game/log.log"; + var info = new LogPathGeneratorInfo() + { + BasePathDirectory = "Z:\\opt\\server", + GameDirectory = "game", + LogFile = "log.log", + IsWindows = false + }; + string generated = IW4MServer.GenerateLogPath(info).Replace('\\', '/'); Assert.AreEqual(expected, generated); }