Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automatic GSI configuration file generation #1

Merged
merged 4 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CounterStrike2GSI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ INCLUDE(CSharpUtilities)

SET(SOURCES
CS2EventsInterface.cs
CS2GSIFile.cs
EventDispatcher.cs
EventHandler.cs
EventsInterface.cs
Expand Down Expand Up @@ -64,6 +65,7 @@ SET(SOURCES
StateHandlers/PlayerHandler.cs
StateHandlers/ProviderHandler.cs
StateHandlers/RoundHandler.cs
Utils/SteamUtils.cs
)

SET(README "${CMAKE_SOURCE_DIR}/README.md")
Expand Down
88 changes: 88 additions & 0 deletions CounterStrike2GSI/CS2GSIFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using CounterStrike2GSI.Utils;
using System;
using System.IO;

namespace CounterStrike2GSI
{
/// <summary>
/// Class handling Game State Integration configuration file generation.
/// </summary>
public class CS2GSIFile
{
/// <summary>
/// Attempts to create a Game State Integration configuraion file.<br/>
/// The configuration will target <c>http://localhost:{port}/</c> address.<br/>
/// Returns true on success, false otherwise.
/// </summary>
/// <param name="name">The name of your integration.</param>
/// <param name="port">The port for your integration.</param>
/// <returns>Returns true on success, false otherwise.</returns>
public static bool CreateFile(string name, int port)
{
return CreateFile(name, $"http://localhost:{port}/");
}

/// <summary>
/// Attempts to create a Game State Integration configuraion file.<br/>
/// The configuration will target the specified URI address.<br/>
/// Returns true on success, false otherwise.
/// </summary>
/// <param name="name">The name of your integration.</param>
/// <param name="uri">The URI for your integration.</param>
/// <returns>Returns true on success, false otherwise.</returns>
public static bool CreateFile(string name, string uri)
{
string csgo_path = SteamUtils.GetGamePath(730);

try
{
if (!string.IsNullOrWhiteSpace(csgo_path))
{
string gsifolder = csgo_path + @"\game\csgo\cfg\";
Directory.CreateDirectory(gsifolder);
string gsifile = gsifolder + @$"gamestate_integration_{name}.cfg";

ACF provider_configuration = new ACF();
provider_configuration.Items["provider"] = "1";
provider_configuration.Items["tournamentdraft"] = "1";
provider_configuration.Items["map"] = "1";
provider_configuration.Items["map_round_wins"] = "1";
provider_configuration.Items["round"] = "1";
provider_configuration.Items["player_id"] = "1";
provider_configuration.Items["player_state"] = "1";
provider_configuration.Items["player_weapons"] = "1";
provider_configuration.Items["player_match_stats"] = "1";
provider_configuration.Items["player_position"] = "1";
provider_configuration.Items["phase_countdowns"] = "1";
provider_configuration.Items["allplayers_id"] = "1";
provider_configuration.Items["allplayers_state"] = "1";
provider_configuration.Items["allplayers_match_stats"] = "1";
provider_configuration.Items["allplayers_weapons"] = "1";
provider_configuration.Items["allplayers_position"] = "1";
provider_configuration.Items["allgrenades"] = "1";
provider_configuration.Items["bomb"] = "1";

ACF gsi_configuration = new ACF();
gsi_configuration.Items["uri"] = uri;
gsi_configuration.Items["timeout"] = "5.0";
gsi_configuration.Items["buffer"] = "0.1";
gsi_configuration.Items["throttle"] = "0.1";
gsi_configuration.Items["heartbeat"] = "10.0";
gsi_configuration.Children["data"] = provider_configuration;

ACF gsi = new ACF();
gsi.Children[$"{name} Integration Configuration"] = gsi_configuration;

File.WriteAllText(gsifile, gsi.ToString());

return true;
}
}
catch (Exception)
{
}

return false;
}
}
}
23 changes: 20 additions & 3 deletions CounterStrike2GSI/GameStateListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ private set
/// <summary>
/// Gets the port that is being listened.
/// </summary>
public int Port { get { return _port; } }
public int Port => _port;

/// <summary>
/// Gets the URI that is being listened.
/// </summary>
public string URI => _uri;

/// <summary>
/// Returns whether or not the listener is running.
Expand All @@ -83,6 +88,7 @@ private set

private bool _is_running = false;
private int _port;
private string _uri;
private HttpListener _http_listener;
private AutoResetEvent _wait_for_connection = new AutoResetEvent(false);
private GameState _previous_game_state = new GameState();
Expand Down Expand Up @@ -123,8 +129,9 @@ private GameStateListener()
public GameStateListener(int port) : this()
{
_port = port;
_uri = $"http://localhost:{_port}/";
_http_listener = new HttpListener();
_http_listener.Prefixes.Add("http://localhost:" + port + "/");
_http_listener.Prefixes.Add(_uri);
}

/// <summary>
Expand All @@ -147,11 +154,21 @@ public GameStateListener(string URI) : this()
}

_port = Convert.ToInt32(PortMatch.Groups[1].Value);

_uri = URI;
_http_listener = new HttpListener();
_http_listener.Prefixes.Add(URI);
}

/// <summary>
/// Attempts to create a Game State Integration configuraion file.
/// </summary>
/// <param name="name">The name of your integration.</param>
/// <returns>Returns true on success, false otherwise.</returns>
public bool GenerateGSIConfigFile(string name)
{
return CS2GSIFile.CreateFile(name, _uri);
}

/// <summary>
/// Starts listening for GameState requests.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion CounterStrike2GSI/StateHandlers/PlayerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private void OnProviderUpdated(CS2GameEvent e)
_provider_cache = evt.New;
}

private void OnPlayerUpdated(CS2GameEvent e)
private void OnPlayerUpdated(CS2GameEvent e)
{
PlayerUpdated evt = (e as PlayerUpdated);

Expand Down
Loading