Compare commits

..

6 Commits

Author SHA1 Message Date
4ca3a8b464 V 1.1.0 Update to .net9.0 standard 2025-01-16 20:13:59 +01:00
11c8a4f38f No License URL I guess 2025-01-16 19:46:22 +01:00
a11f50b6d8 V 1.0.9 2025-01-16 19:45:23 +01:00
8371f119d5 Accessor Changes, GSIServer Instantiated 2025-01-16 19:43:22 +01:00
386c211de5 Activity Change Event 2024-11-02 23:51:59 +01:00
eda66a2334 Add debug output 2024-11-02 23:32:39 +01:00
7 changed files with 66 additions and 18 deletions

View File

@ -28,4 +28,5 @@ public enum CS2Event : byte {
OnBombExploded = 24,
AnyEvent = 25,
AnyMessage = 26,
OnActivityChange = 27
}

View File

@ -16,6 +16,9 @@ internal static class CS2EventGenerator
if(events.Count > 0)
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.AnyEvent, new CS2EventArgs(events.Count)));
if (lastGameState.Player?.Activity != newGameState.Player?.Activity)
events.Add(new(CS2Event.OnActivityChange, new CS2EventArgs(newGameState.Player?.Activity)));
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.AnyMessage, new CS2EventArgs()));
return events;
}

View File

@ -1,5 +1,6 @@
using CS2GSI.GameState;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace CS2GSI;
@ -13,7 +14,13 @@ public class CS2GSI
public bool IsRunning => this._gsiServer.IsRunning;
public CS2GameState? CurrentGameState => _lastLocalGameState;
public CS2GSI(ILogger? logger = null)
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, bool debugEnabled = false)
{
this._logger = logger;
this._logger?.Log(LogLevel.Information, Resources.Installing_GSI_File);
@ -27,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,10 +46,31 @@ public class CS2GSI
CS2GameState newState = CS2GameState.ParseFromJObject(jsonObject);
this._logger?.Log(LogLevel.Debug, $"{Resources.Received_State}:\n{newState.ToString()}");
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<ValueTuple<CS2Event, CS2EventArgs>> 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);
@ -60,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
{
@ -89,14 +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,
@ -124,7 +153,7 @@ public class CS2GSI
OnBombPlanted,
OnBombDefused,
OnBombExploded,
AnyEvent,
AnyMessage;
OnAnyEvent,
OnAnyMessage,
OnActivityChange;
}

View File

@ -1,14 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.8</Version>
<Version>1.1.0</Version>
<Title>CS2GSI</Title>
<Authors>Glax</Authors>
<RepositoryUrl>https://github.com/C9Glax/CS2GSI</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageProjectUrl>https://github.com/C9Glax/CS2GSI</PackageProjectUrl>
<PackageLicenseUrl></PackageLicenseUrl>
<TargetFrameworks>net8.0;net9.0;net7.0</TargetFrameworks>
<LangVersion>latestmajor</LangVersion>
</PropertyGroup>
<ItemGroup>

View File

@ -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 GSIServer(int port, ILogger? logger = null)
internal delegate void OnMessageEventHandler(string content);
internal event OnMessageEventHandler? OnMessage;
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}/";

View File

@ -4,7 +4,7 @@
[![Github](https://img.shields.io/badge/Github-8A2BE2)](https://github.com/C9Glax/CS2GSI)
[![GitHub Release](https://img.shields.io/github/v/release/c9glax/CS2GSI)](https://github.com/C9Glax/CS2GSI/releases/latest)
## .net7.0, .net8.0, .net9.0
## Example Usage
@ -52,3 +52,4 @@ All Events with IDs here: https://github.com/C9Glax/CS2GSI/blob/master/CS2GSI/CS
* `OnBombExploded`
* `AnyEvent`
* `AnyMessage`
* `OnActivityChange` (_string_) Activity

View File

@ -2,9 +2,10 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TargetFrameworks>net7.0;net8.0;net9.0</TargetFrameworks>
<LangVersion>latestmajor</LangVersion>
</PropertyGroup>
<ItemGroup>