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 #31

Merged
merged 5 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
69 changes: 3 additions & 66 deletions Dota2GSI Example program/Dota2GSI Example program/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Dota2GSI;
using Dota2GSI.EventMessages;
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace Dota2GSI_Example_program
Expand All @@ -14,18 +11,13 @@ class Program

static void Main(string[] args)
{
CreateGsifile();
_gsl = new GameStateListener(4000);

Process[] pname = Process.GetProcessesByName("Dota2");
if (pname.Length == 0)
if (!_gsl.GenerateGSIConfigFile("Example"))
{
Console.WriteLine("Dota 2 is not running. Please start Dota 2.");
Console.ReadLine();
Environment.Exit(0);
Console.WriteLine("Could not generate GSI configuration file.");
}

_gsl = new GameStateListener(4000);

// There are many callbacks that can be subscribed.
// This example shows a few.
// _gsl.NewGameState += OnNewGameState; // `NewGameState` can be used alongside `GameEvent`. Just not in this example.
Expand Down Expand Up @@ -246,60 +238,5 @@ static void OnNewGameState(GameState gs)

Console.WriteLine("Press ESC to quit");
}

private static void CreateGsifile()
{
RegistryKey regKey = Registry.CurrentUser.OpenSubKey(@"Software\Valve\Steam");

if (regKey != null)
{
string gsifolder = regKey.GetValue("SteamPath") +
@"\steamapps\common\dota 2 beta\game\dota\cfg\gamestate_integration";
Directory.CreateDirectory(gsifolder);
string gsifile = gsifolder + @"\gamestate_integration_testGSI.cfg";
if (File.Exists(gsifile))
return;

string[] contentofgsifile =
{
"\"Dota 2 Integration Configuration\"",
"{",
" \"uri\" \"http://localhost:4000\"",
" \"timeout\" \"5.0\"",
" \"buffer\" \"0.1\"",
" \"throttle\" \"0.1\"",
" \"heartbeat\" \"30.0\"",
" \"data\"",
" {",
" \"auth\" \"1\"",
" \"provider\" \"1\"",
" \"map\" \"1\"",
" \"player\" \"1\"",
" \"hero\" \"1\"",
" \"abilities\" \"1\"",
" \"items\" \"1\"",
" \"events\" \"1\"",
" \"buildings\" \"1\"",
" \"league\" \"1\"",
" \"draft\" \"1\"",
" \"wearables\" \"1\"",
" \"minimap\" \"1\"",
" \"roshan\" \"1\"",
" \"couriers\" \"1\"",
" \"neutralitems\" \"1\"",
" }",
"}",

};

File.WriteAllLines(gsifile, contentofgsifile);
}
else
{
Console.WriteLine("Registry key for steam not found, cannot create Gamestate Integration file");
Console.ReadLine();
Environment.Exit(0);
}
}
}
}
2 changes: 2 additions & 0 deletions Dota2GSI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ INCLUDE(CSharpUtilities)

SET(SOURCES
Dota2EventsInterface.cs
Dota2GSIFile.cs
EventDispatcher.cs
EventHandler.cs
EventsInterface.cs
Expand Down Expand Up @@ -102,6 +103,7 @@ SET(SOURCES
StateHandlers/ProviderHandler.cs
StateHandlers/RoshanHandler.cs
StateHandlers/WearablesHandler.cs
Utils/SteamUtils.cs
)

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

namespace Dota2GSI
{
/// <summary>
/// Class handling Game State Integration configuration file generation.
/// </summary>
public class Dota2GSIFile
{
/// <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 game_path = SteamUtils.GetGamePath(570);

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

ACF provider_configuration = new ACF();
provider_configuration.Items["auth"] = "1";
provider_configuration.Items["provider"] = "1";
provider_configuration.Items["map"] = "1";
provider_configuration.Items["player"] = "1";
provider_configuration.Items["hero"] = "1";
provider_configuration.Items["abilities"] = "1";
provider_configuration.Items["items"] = "1";
provider_configuration.Items["events"] = "1";
provider_configuration.Items["buildings"] = "1";
provider_configuration.Items["league"] = "1";
provider_configuration.Items["draft"] = "1";
provider_configuration.Items["wearables"] = "1";
provider_configuration.Items["minimap"] = "1";
provider_configuration.Items["roshan"] = "1";
provider_configuration.Items["couriers"] = "1";
provider_configuration.Items["neutralitems"] = "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;
}
}
}
56 changes: 43 additions & 13 deletions Dota2GSI/GameStateListener.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Dota2GSI.EventMessages;
using Dota2GSI.EventMessages;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
Expand Down Expand Up @@ -43,20 +43,36 @@ public GameState CurrentGameState
{
get
{
return _current_game_state;
lock (gamestate_lock)
{
return _current_game_state;
}
}
private set
{
_previous_game_state = _current_game_state;
_current_game_state = value;
RaiseOnNewGameState(ref _current_game_state);
lock (gamestate_lock)
{
if (_current_game_state.Equals(value))
{
return;
}

_previous_game_state = _current_game_state;
_current_game_state = value;
RaiseOnNewGameState(ref _current_game_state);
}
}
}

/// <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 @@ -68,8 +84,11 @@ private set
/// </summary>
public event NewGameStateHandler NewGameState = delegate { };

private readonly object gamestate_lock = new object();

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 @@ -111,14 +130,15 @@ private GameStateListener()
}

/// <summary>
/// A GameStateListener that listens for connections on http://localhost:port/.
/// A GameStateListener that listens for connections on http://localhost:<c>port</c>/.
/// </summary>
/// <param name="Port">The port to listen on.</param>
public GameStateListener(int Port) : this()
/// <param name="port">The port to listen on.</param>
public GameStateListener(int port) : this()
{
_port = Port;
_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 @@ -141,11 +161,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 Dota2GSIFile.CreateFile(name, _uri);
}

/// <summary>
/// Starts listening for GameState requests.
/// </summary>
Expand Down Expand Up @@ -230,7 +260,7 @@ private void RaiseOnNewGameState(ref GameState game_state)
{
RaiseEvent(NewGameState, game_state);

_game_state_handler.OnNewGameState(CurrentGameState);
_game_state_handler.OnNewGameState(game_state);
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions Dota2GSI/Nodes/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ public override bool Equals(object obj)
}

return obj is Node other &&
_ParsedData != null &&
_ParsedData.Equals(other._ParsedData) &&
_successfully_retrieved_any_value.Equals(other._successfully_retrieved_any_value);
}
Expand Down
Loading