From 8a995a7d48f5e46fa1454432d36bb1b47df7bd14 Mon Sep 17 00:00:00 2001 From: Anton Pupkov Date: Fri, 19 Jan 2024 16:56:57 -0800 Subject: [PATCH] Add example program --- .../CounterStrike2GSI Example Program.sln | 31 +++++ .../CounterStrike2GSI Example Program.csproj | 15 +++ .../Program.cs | 126 ++++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100644 CounterStrike2GSI Example Program/CounterStrike2GSI Example Program.sln create mode 100644 CounterStrike2GSI Example Program/CounterStrike2GSI Example Program/CounterStrike2GSI Example Program.csproj create mode 100644 CounterStrike2GSI Example Program/CounterStrike2GSI Example Program/Program.cs diff --git a/CounterStrike2GSI Example Program/CounterStrike2GSI Example Program.sln b/CounterStrike2GSI Example Program/CounterStrike2GSI Example Program.sln new file mode 100644 index 0000000..b95159e --- /dev/null +++ b/CounterStrike2GSI Example Program/CounterStrike2GSI Example Program.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34330.188 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CounterStrike2GSI Example Program", "CounterStrike2GSI Example Program\CounterStrike2GSI Example Program.csproj", "{5ABA3AEF-9C06-4353-A906-FFE8FA879DA5}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CounterStrike2GSI", "..\build\CounterStrike2GSI\CounterStrike2GSI.csproj", "{FD7228AA-698A-3716-8CFF-373A33438C95}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5ABA3AEF-9C06-4353-A906-FFE8FA879DA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5ABA3AEF-9C06-4353-A906-FFE8FA879DA5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5ABA3AEF-9C06-4353-A906-FFE8FA879DA5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5ABA3AEF-9C06-4353-A906-FFE8FA879DA5}.Release|Any CPU.Build.0 = Release|Any CPU + {FD7228AA-698A-3716-8CFF-373A33438C95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FD7228AA-698A-3716-8CFF-373A33438C95}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FD7228AA-698A-3716-8CFF-373A33438C95}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FD7228AA-698A-3716-8CFF-373A33438C95}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {9DCC2CC9-9B14-47CC-82B9-3A5573C884F6} + EndGlobalSection +EndGlobal diff --git a/CounterStrike2GSI Example Program/CounterStrike2GSI Example Program/CounterStrike2GSI Example Program.csproj b/CounterStrike2GSI Example Program/CounterStrike2GSI Example Program/CounterStrike2GSI Example Program.csproj new file mode 100644 index 0000000..3804bf4 --- /dev/null +++ b/CounterStrike2GSI Example Program/CounterStrike2GSI Example Program/CounterStrike2GSI Example Program.csproj @@ -0,0 +1,15 @@ + + + + Exe + net8.0 + CounterStrike2GSI_Example_Program + enable + enable + + + + + + + diff --git a/CounterStrike2GSI Example Program/CounterStrike2GSI Example Program/Program.cs b/CounterStrike2GSI Example Program/CounterStrike2GSI Example Program/Program.cs new file mode 100644 index 0000000..4760552 --- /dev/null +++ b/CounterStrike2GSI Example Program/CounterStrike2GSI Example Program/Program.cs @@ -0,0 +1,126 @@ +using CounterStrike2GSI; +using CounterStrike2GSI.EventMessages; + +namespace CounterStrike2GSI_Example_program +{ + class Program + { + static GameStateListener? _gsl; + + static void Main(string[] args) + { + _gsl = new GameStateListener(4000); + + if (!_gsl.GenerateGSIConfigFile("Example")) + { + Console.WriteLine("Could not generate GSI configuration file."); + } + + // There are many callbacks that can be subscribed. + // This example shows a few. + _gsl.NewGameState += OnNewGameState; + _gsl.GameEvent += OnGameEvent; + _gsl.BombStateUpdated += OnBombStateUpdated; + _gsl.PlayerGotKill += OnPlayerGotKill; + _gsl.PlayerDied += OnPlayerDied; + _gsl.KillFeed += OnKillFeed; + _gsl.PlayerWeaponsPickedUp += OnPlayerWeaponsPickedUp; + _gsl.PlayerWeaponsDropped += OnPlayerWeaponsDropped; + _gsl.RoundStarted += OnRoundStarted; + _gsl.RoundConcluded += OnRoundConcluded; + + if (!_gsl.Start()) + { + Console.WriteLine("GameStateListener could not start. Try running this program as Administrator. Exiting."); + Console.ReadLine(); + Environment.Exit(0); + } + Console.WriteLine("Listening for game integration calls..."); + + Console.WriteLine("Press ESC to quit"); + do + { + while (!Console.KeyAvailable) + { + Thread.Sleep(1000); + } + } while (Console.ReadKey(true).Key != ConsoleKey.Escape); + } + + private static void OnNewGameState(GameState gamestate) + { + // Guaranteed to fire before CS2GameEvent events. + } + + private static void OnGameEvent(CS2GameEvent game_event) + { + if (game_event is PlayerTookDamage player_took_damage) + { + Console.WriteLine($"The player {player_took_damage.Player.Name} took {player_took_damage.Previous - player_took_damage.New} damage!"); + } + else if (game_event is PlayerActiveWeaponChanged active_weapon_changed) + { + Console.WriteLine($"The player {active_weapon_changed.Player.Name} changed their active weapon to {active_weapon_changed.New.Name} from {active_weapon_changed.Previous.Name}!"); + } + } + + private static void OnBombStateUpdated(BombStateUpdated game_event) + { + Console.WriteLine($"The bomb is now {game_event.New}."); + } + + private static void OnPlayerGotKill(PlayerGotKill game_event) + { + Console.WriteLine($"The player {game_event.Player.Name} earned a {(game_event.IsHeadshot ? "headshot " : "")}kill with {game_event.Weapon.Name}!" + (game_event.IsAce ? " And it was an ACE!" : "")); + } + + private static void OnPlayerDied(PlayerDied game_event) + { + Console.WriteLine($"The player {game_event.Player.Name} died."); + } + + private static void OnKillFeed(KillFeed game_event) + { + Console.WriteLine($"{game_event.Killer.Name} killed {game_event.Victim.Name} with {game_event.Weapon.Name}{(game_event.IsHeadshot ? " as a headshot." : ".")}"); + } + + private static void OnPlayerWeaponsPickedUp(PlayerWeaponsPickedUp game_event) + { + Console.WriteLine($"The player {game_event.Player.Name} picked up the following weapons:"); + foreach (var weapon in game_event.Weapons) + { + Console.WriteLine($"\t{weapon.Name}"); + } + } + + private static void OnPlayerWeaponsDropped(PlayerWeaponsDropped game_event) + { + Console.WriteLine($"The player {game_event.Player.Name} dropped the following weapons:"); + foreach (var weapon in game_event.Weapons) + { + Console.WriteLine($"\t{weapon.Name}"); + } + } + + private static void OnRoundStarted(RoundStarted game_event) + { + if (game_event.IsFirstRound) + { + Console.WriteLine($"First round {game_event.Round} started."); + } + else if (game_event.IsLastRound) + { + Console.WriteLine($"Last round {game_event.Round} started."); + } + else + { + Console.WriteLine($"A new round {game_event.Round} started."); + } + } + + private static void OnRoundConcluded(RoundConcluded game_event) + { + Console.WriteLine($"Round {game_event.Round} concluded by {game_event.WinningTeam} for reason: {game_event.RoundConclusionReason}"); + } + } +}