Skip to content

Commit d9d3663

Browse files
authored
Add example program (#8)
1 parent 648987b commit d9d3663

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.8.34330.188
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CounterStrike2GSI Example Program", "CounterStrike2GSI Example Program\CounterStrike2GSI Example Program.csproj", "{5ABA3AEF-9C06-4353-A906-FFE8FA879DA5}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CounterStrike2GSI", "..\build\CounterStrike2GSI\CounterStrike2GSI.csproj", "{FD7228AA-698A-3716-8CFF-373A33438C95}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{5ABA3AEF-9C06-4353-A906-FFE8FA879DA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{5ABA3AEF-9C06-4353-A906-FFE8FA879DA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{5ABA3AEF-9C06-4353-A906-FFE8FA879DA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{5ABA3AEF-9C06-4353-A906-FFE8FA879DA5}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{FD7228AA-698A-3716-8CFF-373A33438C95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{FD7228AA-698A-3716-8CFF-373A33438C95}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{FD7228AA-698A-3716-8CFF-373A33438C95}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{FD7228AA-698A-3716-8CFF-373A33438C95}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {9DCC2CC9-9B14-47CC-82B9-3A5573C884F6}
30+
EndGlobalSection
31+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>CounterStrike2GSI_Example_Program</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\build\CounterStrike2GSI\CounterStrike2GSI.csproj" />
13+
</ItemGroup>
14+
15+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
using CounterStrike2GSI;
2+
using CounterStrike2GSI.EventMessages;
3+
4+
namespace CounterStrike2GSI_Example_program
5+
{
6+
class Program
7+
{
8+
static GameStateListener? _gsl;
9+
10+
static void Main(string[] args)
11+
{
12+
_gsl = new GameStateListener(4000);
13+
14+
if (!_gsl.GenerateGSIConfigFile("Example"))
15+
{
16+
Console.WriteLine("Could not generate GSI configuration file.");
17+
}
18+
19+
// There are many callbacks that can be subscribed.
20+
// This example shows a few.
21+
_gsl.NewGameState += OnNewGameState;
22+
_gsl.GameEvent += OnGameEvent;
23+
_gsl.BombStateUpdated += OnBombStateUpdated;
24+
_gsl.PlayerGotKill += OnPlayerGotKill;
25+
_gsl.PlayerDied += OnPlayerDied;
26+
_gsl.KillFeed += OnKillFeed;
27+
_gsl.PlayerWeaponsPickedUp += OnPlayerWeaponsPickedUp;
28+
_gsl.PlayerWeaponsDropped += OnPlayerWeaponsDropped;
29+
_gsl.RoundStarted += OnRoundStarted;
30+
_gsl.RoundConcluded += OnRoundConcluded;
31+
32+
if (!_gsl.Start())
33+
{
34+
Console.WriteLine("GameStateListener could not start. Try running this program as Administrator. Exiting.");
35+
Console.ReadLine();
36+
Environment.Exit(0);
37+
}
38+
Console.WriteLine("Listening for game integration calls...");
39+
40+
Console.WriteLine("Press ESC to quit");
41+
do
42+
{
43+
while (!Console.KeyAvailable)
44+
{
45+
Thread.Sleep(1000);
46+
}
47+
} while (Console.ReadKey(true).Key != ConsoleKey.Escape);
48+
}
49+
50+
private static void OnNewGameState(GameState gamestate)
51+
{
52+
// Guaranteed to fire before CS2GameEvent events.
53+
}
54+
55+
private static void OnGameEvent(CS2GameEvent game_event)
56+
{
57+
if (game_event is PlayerTookDamage player_took_damage)
58+
{
59+
Console.WriteLine($"The player {player_took_damage.Player.Name} took {player_took_damage.Previous - player_took_damage.New} damage!");
60+
}
61+
else if (game_event is PlayerActiveWeaponChanged active_weapon_changed)
62+
{
63+
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}!");
64+
}
65+
}
66+
67+
private static void OnBombStateUpdated(BombStateUpdated game_event)
68+
{
69+
Console.WriteLine($"The bomb is now {game_event.New}.");
70+
}
71+
72+
private static void OnPlayerGotKill(PlayerGotKill game_event)
73+
{
74+
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!" : ""));
75+
}
76+
77+
private static void OnPlayerDied(PlayerDied game_event)
78+
{
79+
Console.WriteLine($"The player {game_event.Player.Name} died.");
80+
}
81+
82+
private static void OnKillFeed(KillFeed game_event)
83+
{
84+
Console.WriteLine($"{game_event.Killer.Name} killed {game_event.Victim.Name} with {game_event.Weapon.Name}{(game_event.IsHeadshot ? " as a headshot." : ".")}");
85+
}
86+
87+
private static void OnPlayerWeaponsPickedUp(PlayerWeaponsPickedUp game_event)
88+
{
89+
Console.WriteLine($"The player {game_event.Player.Name} picked up the following weapons:");
90+
foreach (var weapon in game_event.Weapons)
91+
{
92+
Console.WriteLine($"\t{weapon.Name}");
93+
}
94+
}
95+
96+
private static void OnPlayerWeaponsDropped(PlayerWeaponsDropped game_event)
97+
{
98+
Console.WriteLine($"The player {game_event.Player.Name} dropped the following weapons:");
99+
foreach (var weapon in game_event.Weapons)
100+
{
101+
Console.WriteLine($"\t{weapon.Name}");
102+
}
103+
}
104+
105+
private static void OnRoundStarted(RoundStarted game_event)
106+
{
107+
if (game_event.IsFirstRound)
108+
{
109+
Console.WriteLine($"First round {game_event.Round} started.");
110+
}
111+
else if (game_event.IsLastRound)
112+
{
113+
Console.WriteLine($"Last round {game_event.Round} started.");
114+
}
115+
else
116+
{
117+
Console.WriteLine($"A new round {game_event.Round} started.");
118+
}
119+
}
120+
121+
private static void OnRoundConcluded(RoundConcluded game_event)
122+
{
123+
Console.WriteLine($"Round {game_event.Round} concluded by {game_event.WinningTeam} for reason: {game_event.RoundConclusionReason}");
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)