Compare commits
6 Commits
04be9d6fab
...
master
Author | SHA1 | Date | |
---|---|---|---|
4ca3a8b464 | |||
11c8a4f38f | |||
a11f50b6d8 | |||
8371f119d5 | |||
386c211de5 | |||
eda66a2334 |
@ -28,4 +28,5 @@ public enum CS2Event : byte {
|
|||||||
OnBombExploded = 24,
|
OnBombExploded = 24,
|
||||||
AnyEvent = 25,
|
AnyEvent = 25,
|
||||||
AnyMessage = 26,
|
AnyMessage = 26,
|
||||||
|
OnActivityChange = 27
|
||||||
}
|
}
|
@ -15,6 +15,9 @@ internal static class CS2EventGenerator
|
|||||||
|
|
||||||
if(events.Count > 0)
|
if(events.Count > 0)
|
||||||
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.AnyEvent, new CS2EventArgs(events.Count)));
|
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()));
|
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.AnyMessage, new CS2EventArgs()));
|
||||||
return events;
|
return events;
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using CS2GSI.GameState;
|
using CS2GSI.GameState;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
namespace CS2GSI;
|
namespace CS2GSI;
|
||||||
@ -12,8 +13,14 @@ public class CS2GSI
|
|||||||
private readonly ILogger? _logger;
|
private readonly ILogger? _logger;
|
||||||
public bool IsRunning => this._gsiServer.IsRunning;
|
public bool IsRunning => this._gsiServer.IsRunning;
|
||||||
public CS2GameState? CurrentGameState => _lastLocalGameState;
|
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 = logger;
|
||||||
this._logger?.Log(LogLevel.Information, Resources.Installing_GSI_File);
|
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);
|
this._logger?.Log(LogLevel.Critical, Resources.Installing_GSI_File_Failed);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this._gsiServer = new GSIServer(3000, logger);
|
|
||||||
|
DebugEnabled = debugEnabled;
|
||||||
|
this._gsiServer = GSIServer.Create(3000, logger);
|
||||||
this._gsiServer.OnMessage += GsiServerOnOnMessage;
|
this._gsiServer.OnMessage += GsiServerOnOnMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,10 +46,31 @@ public class CS2GSI
|
|||||||
CS2GameState newState = CS2GameState.ParseFromJObject(jsonObject);
|
CS2GameState newState = CS2GameState.ParseFromJObject(jsonObject);
|
||||||
this._logger?.Log(LogLevel.Debug, $"{Resources.Received_State}:\n{newState.ToString()}");
|
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)
|
if (_lastLocalGameState is not null && _allGameStates.Count > 0)
|
||||||
{
|
{
|
||||||
List<ValueTuple<CS2Event, CS2EventArgs>> generatedEvents = CS2EventGenerator.GenerateEvents(_lastLocalGameState, newState, _allGameStates.Last());
|
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)}");
|
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);
|
InvokeEvents(generatedEvents);
|
||||||
}
|
}
|
||||||
this._lastLocalGameState = newState.UpdateGameStateForLocal(_lastLocalGameState);
|
this._lastLocalGameState = newState.UpdateGameStateForLocal(_lastLocalGameState);
|
||||||
@ -60,7 +90,7 @@ public class CS2GSI
|
|||||||
GetEventHandlerForEvent(cs2Event.Item1)?.Invoke(cs2Event.Item2);
|
GetEventHandlerForEvent(cs2Event.Item1)?.Invoke(cs2Event.Item2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CS2EventHandler? GetEventHandlerForEvent(CS2Event cs2Event)
|
private CS2EventHandler? GetEventHandlerForEvent(CS2Event cs2Event)
|
||||||
{
|
{
|
||||||
return cs2Event switch
|
return cs2Event switch
|
||||||
{
|
{
|
||||||
@ -89,14 +119,13 @@ public class CS2GSI
|
|||||||
CS2Event.OnBombPlanted => this.OnBombPlanted,
|
CS2Event.OnBombPlanted => this.OnBombPlanted,
|
||||||
CS2Event.OnBombDefused => this.OnBombDefused,
|
CS2Event.OnBombDefused => this.OnBombDefused,
|
||||||
CS2Event.OnBombExploded => this.OnBombExploded,
|
CS2Event.OnBombExploded => this.OnBombExploded,
|
||||||
CS2Event.AnyEvent => this.AnyEvent,
|
CS2Event.AnyEvent => this.OnAnyEvent,
|
||||||
CS2Event.AnyMessage => this.AnyMessage,
|
CS2Event.AnyMessage => this.OnAnyMessage,
|
||||||
|
CS2Event.OnActivityChange => this.OnActivityChange,
|
||||||
_ => throw new ArgumentException(Resources.Unknown_Event, nameof(cs2Event))
|
_ => throw new ArgumentException(Resources.Unknown_Event, nameof(cs2Event))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public delegate void CS2EventHandler(CS2EventArgs eventArgs);
|
public delegate void CS2EventHandler(CS2EventArgs eventArgs);
|
||||||
|
|
||||||
public event CS2EventHandler? OnKill,
|
public event CS2EventHandler? OnKill,
|
||||||
@ -124,7 +153,7 @@ public class CS2GSI
|
|||||||
OnBombPlanted,
|
OnBombPlanted,
|
||||||
OnBombDefused,
|
OnBombDefused,
|
||||||
OnBombExploded,
|
OnBombExploded,
|
||||||
AnyEvent,
|
OnAnyEvent,
|
||||||
AnyMessage;
|
OnAnyMessage,
|
||||||
|
OnActivityChange;
|
||||||
}
|
}
|
@ -1,14 +1,17 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net7.0</TargetFramework>
|
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<Version>1.0.8</Version>
|
<Version>1.1.0</Version>
|
||||||
<Title>CS2GSI</Title>
|
<Title>CS2GSI</Title>
|
||||||
<Authors>Glax</Authors>
|
<Authors>Glax</Authors>
|
||||||
<RepositoryUrl>https://github.com/C9Glax/CS2GSI</RepositoryUrl>
|
<RepositoryUrl>https://github.com/C9Glax/CS2GSI</RepositoryUrl>
|
||||||
<RepositoryType>git</RepositoryType>
|
<RepositoryType>git</RepositoryType>
|
||||||
|
<PackageProjectUrl>https://github.com/C9Glax/CS2GSI</PackageProjectUrl>
|
||||||
|
<PackageLicenseUrl></PackageLicenseUrl>
|
||||||
|
<TargetFrameworks>net8.0;net9.0;net7.0</TargetFrameworks>
|
||||||
|
<LangVersion>latestmajor</LangVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -8,14 +8,24 @@ namespace CS2GSI;
|
|||||||
|
|
||||||
internal class GSIServer
|
internal class GSIServer
|
||||||
{
|
{
|
||||||
|
public static GSIServer? Instance { get; private set; }
|
||||||
private HttpListener HttpListener { get; init; }
|
private HttpListener HttpListener { get; init; }
|
||||||
internal delegate void OnMessageEventHandler(string content);
|
|
||||||
internal event OnMessageEventHandler? OnMessage;
|
|
||||||
private bool _keepRunning = true;
|
private bool _keepRunning = true;
|
||||||
internal bool IsRunning { get; private set; }
|
internal bool IsRunning { get; private set; }
|
||||||
private ILogger? logger;
|
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;
|
this.logger = logger;
|
||||||
string prefix = $"http://127.0.0.1:{port}/";
|
string prefix = $"http://127.0.0.1:{port}/";
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
[](https://github.com/C9Glax/CS2GSI)
|
[](https://github.com/C9Glax/CS2GSI)
|
||||||
[](https://github.com/C9Glax/CS2GSI/releases/latest)
|
[](https://github.com/C9Glax/CS2GSI/releases/latest)
|
||||||
|
|
||||||
|
## .net7.0, .net8.0, .net9.0
|
||||||
|
|
||||||
|
|
||||||
## Example Usage
|
## Example Usage
|
||||||
@ -51,4 +51,5 @@ All Events with IDs here: https://github.com/C9Glax/CS2GSI/blob/master/CS2GSI/CS
|
|||||||
* `OnBombDefused`
|
* `OnBombDefused`
|
||||||
* `OnBombExploded`
|
* `OnBombExploded`
|
||||||
* `AnyEvent`
|
* `AnyEvent`
|
||||||
* `AnyMessage`
|
* `AnyMessage`
|
||||||
|
* `OnActivityChange` (_string_) Activity
|
@ -2,9 +2,10 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net7.0</TargetFramework>
|
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
|
<TargetFrameworks>net7.0;net8.0;net9.0</TargetFrameworks>
|
||||||
|
<LangVersion>latestmajor</LangVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
Reference in New Issue
Block a user