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