diff --git a/NydusNetwork/GameClient.cs b/NydusNetwork/GameClient.cs index 9d05a20..c6e2cda 100644 --- a/NydusNetwork/GameClient.cs +++ b/NydusNetwork/GameClient.cs @@ -33,7 +33,12 @@ public void Initialize(bool asHost) { public void LaunchClient(bool asHost) { _isHost = asHost; - Process.Start(new ProcessStartInfo { FileName = _settings.ExecutableClientPath(),Arguments = _settings.ToArguments(_isHost),WorkingDirectory = _settings.WorkingDirectory() }); + Process.Start( + new ProcessStartInfo { + FileName = _settings.ExecutableClientPath(), + Arguments = _settings.ToArguments(_isHost), + WorkingDirectory = _settings.WorkingDirectory(), + }); } public bool ConnectToClient() => _connection.Connect(_settings.GetUri(_isHost)); diff --git a/NydusNetwork/Services/GameSettingsService.cs b/NydusNetwork/Services/GameSettingsService.cs index be7f75a..984addb 100644 --- a/NydusNetwork/Services/GameSettingsService.cs +++ b/NydusNetwork/Services/GameSettingsService.cs @@ -64,7 +64,7 @@ public static Request CreateGameRequest(this GameSettings gs) { PlayerSetup = { gs.Opponents,new PlayerSetup { Type = PlayerType.Participant,Race = gs.ParticipantRace } } } }; - if(File.Exists(gs.GameMap) || File.Exists($"{gs.FolderPath}\\Maps\\{gs.GameMap}")) + if (File.Exists(gs.GameMap) || File.Exists(Path.Combine(gs.FolderPath, "Maps", gs.GameMap))) r.CreateGame.LocalMap = new LocalMap { MapPath = gs.GameMap }; else r.CreateGame.BattlenetMapName = gs.GameMap; @@ -74,11 +74,11 @@ public static Request CreateGameRequest(this GameSettings gs) { public static Uri GetUri(this GameSettings gs,bool IsHost) => IsHost ? new Uri($"ws://{gs.ConnectionAddress}:{gs.ConnectionServerPort}/sc2api") : new Uri($"ws://{gs.ConnectionAddress}:{gs.ConnectionClientPort}/sc2api"); - public static string WorkingDirectory(this GameSettings gs) => $"{gs.FolderPath}\\Support"; + public static string WorkingDirectory(this GameSettings gs) => Path.Combine(gs.FolderPath, "Support"); public static string ExecutableClientPath(this GameSettings gs) { if(System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX)) - return Directory.GetDirectories(gs.FolderPath + @"\Versions\",@"Base*")[0] + @"\SC2.app"; + return Directory.GetDirectories(gs.FolderPath + @"/Versions/",@"Base*")[0] + @"/SC2.app/Contents/MacOS/SC2"; else return Directory.GetDirectories(gs.FolderPath + @"\Versions\",@"Base*")[0] + @"\SC2.exe"; } diff --git a/SC2Abathur/Program.cs b/SC2Abathur/Program.cs index 13661cf..9314370 100644 --- a/SC2Abathur/Program.cs +++ b/SC2Abathur/Program.cs @@ -38,8 +38,8 @@ class Program { /// First argument will be used as datapath, second as logpath static void Main(string[] args) { Console.Title = $"Abathur Framework v{AbathurVersion} and NydusNetwork v{NydusNetworkVersion}"; - var dataPath = args.Length > 0 ? args[0] : Directory.GetCurrentDirectory() + "\\data\\"; - var logPath = args.Length > 1 ? args[0] : Directory.GetCurrentDirectory() + "\\log\\"; + var dataPath = args.Length > 0 ? args[0] : Path.Combine(Directory.GetCurrentDirectory(), "data"); + var logPath = args.Length > 1 ? args[0] : Path.Combine(Directory.GetCurrentDirectory(), "log"); new Program().Run(dataPath, logPath); } @@ -58,16 +58,16 @@ public void Run(string dataPath,string logPath = null) { // Check that the game settings file exist, or create it a default template. log?.LogMessage("Checking game settings file:"); - FileService.ValidateOrCreateJsonFile(dataPath + "gamesettings.json",Defaults.GameSettings,log); - GameSettings gameSettings = FileService.ReadFromJson(dataPath + "gamesettings.json",log); + FileService.ValidateOrCreateJsonFile(Path.Combine(dataPath, "gamesettings.json") ,Defaults.GameSettings,log); + GameSettings gameSettings = FileService.ReadFromJson(Path.Combine(dataPath, "gamesettings.json"), log); // Load the Abathur setup file, this file is used to add/remove modules to the framework log?.LogMessage("Checking setup file:"); - FileService.ValidateOrCreateJsonFile(dataPath + "setup.json",Defaults.AbathurSetup,log); - AbathurSetup abathurSetup = FileService.ReadFromJson(dataPath + "setup.json",log); + FileService.ValidateOrCreateJsonFile(Path.Combine(dataPath, "setup.json"), Defaults.AbathurSetup,log); + AbathurSetup abathurSetup = FileService.ReadFromJson(Path.Combine(dataPath, "setup.json"), log); // Load or create the 'essence file' - a file containing UnitTypeData, AbilityTypeData, BuffData, UpgradeData and manually coded tech-trees for each race. - var essence = EssenceService.LoadOrCreate(dataPath,log); + var essence = EssenceService.LoadOrCreate(dataPath,log,gameSettings); // If a log path have been specified, check the directory and change add a filelogger (writes to files) if(logPath != null) { diff --git a/SC2Abathur/Services/EssenceService.cs b/SC2Abathur/Services/EssenceService.cs index 33c7111..dfcbc79 100644 --- a/SC2Abathur/Services/EssenceService.cs +++ b/SC2Abathur/Services/EssenceService.cs @@ -2,6 +2,8 @@ using Google.Protobuf; using NydusNetwork.API.Protocol; using NydusNetwork.Logging; +using NydusNetwork.Model; +using SC2Abathur.Settings; using System; using System.IO; @@ -18,10 +20,10 @@ public class EssenceService { /// Valid path for data directory used by Abathur /// Optional log /// - public static Essence LoadOrCreate(string dataPath, ILogger log = null) { + public static Essence LoadOrCreate(string dataPath, ILogger log = null, GameSettings gs = null) { log?.LogMessage("Checking binary essence file:"); - ValidateOrCreateBinaryFile(dataPath + "essence.data",() => FetchDataFromClient(log),log); - return Load(dataPath + "essence.data",log); + ValidateOrCreateBinaryFile(Path.Combine(dataPath, "essence.data"), gs ?? Defaults.GameSettings, log); + return Load(Path.Combine(dataPath, "essence.data"), log); } /// @@ -45,15 +47,14 @@ public static Essence Load(string path,ILogger log = null) { /// Path to validate /// Function to get content in case the file does not exist /// Optional log - private static void ValidateOrCreateBinaryFile(string path,Func content,ILogger log = null) where T : IMessage { + private static void ValidateOrCreateBinaryFile(string path, GameSettings gs, ILogger log = null) { try { if(File.Exists(path)) { log?.LogSuccess($"\tFOUND: {path}"); } else { - var msg = content.Invoke(); - FileStream stream = File.Create(path); + var msg = FetchDataFromClient(gs, log); + using FileStream stream = File.Create(path); msg.WriteTo(stream); - stream.Close(); log?.LogWarning($"\tCREATED: {path}"); } } catch(Exception e) { log.LogError($"\tFAILED: {e.Message}"); } @@ -64,9 +65,9 @@ private static void ValidateOrCreateBinaryFile(string path,Func content,IL /// /// Optional log /// - private static Essence FetchDataFromClient(ILogger log = null) { + private static Essence FetchDataFromClient(GameSettings gs, ILogger log = null) { var factory = new EssenceFactory(log); - return factory.FetchFromClient(Settings.Defaults.GameSettings); + return factory.FetchFromClient(gs); } } } diff --git a/SC2Abathur/Settings/Defaults.cs b/SC2Abathur/Settings/Defaults.cs index 585b1ac..f497c88 100644 --- a/SC2Abathur/Settings/Defaults.cs +++ b/SC2Abathur/Settings/Defaults.cs @@ -1,6 +1,4 @@ -using Abathur.Constants; -using SC2Abathur.Settings; -using NydusNetwork.API.Protocol; +using NydusNetwork.API.Protocol; using NydusNetwork.Model; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -16,12 +14,22 @@ public static class Defaults { /// /// Used for setting up the modules in the Abathur framework. /// - public static AbathurSetup AbathurSetup => new AbathurSetup { IsParallelized = false, Modules = new List{ "RandomDemo", "AutoHarvestGather", "AutoSupply" } }; + public static AbathurSetup AbathurSetup => new AbathurSetup { + IsParallelized = false, + Modules = new List{ + "RandomDemo", + "AutoHarvestGather", + "AutoSupply" + } + }; + /// /// Stores game related settings used by the StarCraft II client. /// public static GameSettings GameSettings => new GameSettings { - FolderPath = @"C:\Program Files (x86)\StarCraft II", + FolderPath = + System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX) + ? @"/Applications/StarCraft II" : @"C:\Program Files (x86)\StarCraft II", ConnectionAddress = IPAddress.Loopback.ToString(), ConnectionServerPort = 8165, ConnectionClientPort = 8170,