From 8371f119d5fcddba47322d1f0c74f66a5b1e794f Mon Sep 17 00:00:00 2001 From: glax Date: Thu, 16 Jan 2025 19:43:22 +0100 Subject: [PATCH] Accessor Changes, GSIServer Instantiated --- CS2GSI/CS2GSI.cs | 53 ++++++++++++++++++++++++++++++--------------- CS2GSI/GSIServer.cs | 16 +++++++++++--- 2 files changed, 49 insertions(+), 20 deletions(-) diff --git a/CS2GSI/CS2GSI.cs b/CS2GSI/CS2GSI.cs index 4bcdea9..d8ebf25 100644 --- a/CS2GSI/CS2GSI.cs +++ b/CS2GSI/CS2GSI.cs @@ -13,8 +13,14 @@ public class CS2GSI private readonly ILogger? _logger; public bool IsRunning => this._gsiServer.IsRunning; public CS2GameState? CurrentGameState => _lastLocalGameState; + + private const string DebugDirectory = "Debug"; + public static string StatesDirectory => Path.Join(DebugDirectory, "States"); + public static string MessagesDirectory => Path.Join(DebugDirectory, "Messages"); + public static string EventsDirectory => Path.Join(DebugDirectory, "Messages"); + public static bool DebugEnabled = false; - public CS2GSI(ILogger? logger = null) + public CS2GSI(ILogger? logger = null, bool debugEnabled = false) { this._logger = logger; this._logger?.Log(LogLevel.Information, Resources.Installing_GSI_File); @@ -28,7 +34,9 @@ public class CS2GSI this._logger?.Log(LogLevel.Critical, Resources.Installing_GSI_File_Failed); return; } - this._gsiServer = new GSIServer(3000, logger); + + DebugEnabled = debugEnabled; + this._gsiServer = GSIServer.Create(3000, logger); this._gsiServer.OnMessage += GsiServerOnOnMessage; } @@ -37,18 +45,32 @@ public class CS2GSI JObject jsonObject = JObject.Parse(messageJson); CS2GameState newState = CS2GameState.ParseFromJObject(jsonObject); this._logger?.Log(LogLevel.Debug, $"{Resources.Received_State}:\n{newState.ToString()}"); -#if DEBUG - long time = DateTime.Now.ToFileTime(); - Directory.CreateDirectory("states"); - File.WriteAllText(Path.Join("states", $"{time}.json"), JsonConvert.SerializeObject(newState, Formatting.Indented, new Newtonsoft.Json.Converters.StringEnumConverter())); - Directory.CreateDirectory("messages"); - File.WriteAllText(Path.Join("messages", $"{time}.json"), messageJson); -#endif + + double time = DateTime.UtcNow.Subtract( + new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc) + ).TotalMilliseconds; + string timeString = $"{time:N0}.json"; + + if (DebugEnabled) + { + Directory.CreateDirectory(StatesDirectory); + Directory.CreateDirectory(MessagesDirectory); + File.WriteAllText(Path.Join(StatesDirectory, timeString), + JsonConvert.SerializeObject(newState, Formatting.Indented, new Newtonsoft.Json.Converters.StringEnumConverter())); + File.WriteAllText(Path.Join(MessagesDirectory, timeString), messageJson); + } if (_lastLocalGameState is not null && _allGameStates.Count > 0) { List> generatedEvents = CS2EventGenerator.GenerateEvents(_lastLocalGameState, newState, _allGameStates.Last()); this._logger?.Log(LogLevel.Information, $"Generated {generatedEvents.Count} event{(generatedEvents.Count > 1 ? 's' : null)}:\n- {string.Join("\n- ", generatedEvents)}"); + if (DebugEnabled) + { + Directory.CreateDirectory(EventsDirectory); + File.WriteAllText(Path.Join(StatesDirectory, EventsDirectory), + JsonConvert.SerializeObject(generatedEvents, Formatting.Indented, new Newtonsoft.Json.Converters.StringEnumConverter())); + } + InvokeEvents(generatedEvents); } this._lastLocalGameState = newState.UpdateGameStateForLocal(_lastLocalGameState); @@ -68,7 +90,7 @@ public class CS2GSI GetEventHandlerForEvent(cs2Event.Item1)?.Invoke(cs2Event.Item2); } - public CS2EventHandler? GetEventHandlerForEvent(CS2Event cs2Event) + private CS2EventHandler? GetEventHandlerForEvent(CS2Event cs2Event) { return cs2Event switch { @@ -97,15 +119,13 @@ public class CS2GSI CS2Event.OnBombPlanted => this.OnBombPlanted, CS2Event.OnBombDefused => this.OnBombDefused, CS2Event.OnBombExploded => this.OnBombExploded, - CS2Event.AnyEvent => this.AnyEvent, - CS2Event.AnyMessage => this.AnyMessage, + CS2Event.AnyEvent => this.OnAnyEvent, + CS2Event.AnyMessage => this.OnAnyMessage, CS2Event.OnActivityChange => this.OnActivityChange, _ => throw new ArgumentException(Resources.Unknown_Event, nameof(cs2Event)) }; } - - public delegate void CS2EventHandler(CS2EventArgs eventArgs); public event CS2EventHandler? OnKill, @@ -133,8 +153,7 @@ public class CS2GSI OnBombPlanted, OnBombDefused, OnBombExploded, - AnyEvent, - AnyMessage, + OnAnyEvent, + OnAnyMessage, OnActivityChange; - } \ No newline at end of file diff --git a/CS2GSI/GSIServer.cs b/CS2GSI/GSIServer.cs index da2e24b..e0b0fe9 100644 --- a/CS2GSI/GSIServer.cs +++ b/CS2GSI/GSIServer.cs @@ -8,14 +8,24 @@ namespace CS2GSI; internal class GSIServer { + public static GSIServer? Instance { get; private set; } private HttpListener HttpListener { get; init; } - internal delegate void OnMessageEventHandler(string content); - internal event OnMessageEventHandler? OnMessage; private bool _keepRunning = true; internal bool IsRunning { get; private set; } private ILogger? logger; + + internal delegate void OnMessageEventHandler(string content); + internal event OnMessageEventHandler? OnMessage; - internal GSIServer(int port, ILogger? logger = null) + public static GSIServer Create(int port, ILogger? logger = null) + { + Instance = new GSIServer(port, logger); + return Instance; + } + + private GSIServer(){} + + private GSIServer(int port, ILogger? logger = null) { this.logger = logger; string prefix = $"http://127.0.0.1:{port}/";