diff --git a/Fika.Core/Console/FikaCommands.cs b/Fika.Core/Console/FikaCommands.cs index 3da46551..ffe8b9b6 100644 --- a/Fika.Core/Console/FikaCommands.cs +++ b/Fika.Core/Console/FikaCommands.cs @@ -91,6 +91,26 @@ public static void God(bool state) } } + [ConsoleCommand("debug", "", null, "Toggle debug window", [])] + public static void Debug(bool state) + { + CoopGame coopGame = (CoopGame)Singleton.Instance; + + if (coopGame == null) + { + ConsoleScreen.LogWarning("You are not in a game."); + return; + } + + if (coopGame.Status != GameStatus.Started) + { + ConsoleScreen.LogWarning("Game is not running."); + return; + } + + coopGame.ToggleDebug(state); + } + [ConsoleCommand("clear", "", null, "Clears the console output", [])] public static void Clear() { diff --git a/Fika.Core/Coop/Custom/FikaDebug.cs b/Fika.Core/Coop/Custom/FikaDebug.cs new file mode 100644 index 00000000..802f3a4a --- /dev/null +++ b/Fika.Core/Coop/Custom/FikaDebug.cs @@ -0,0 +1,162 @@ +using Comfort.Common; +using Fika.Core.Coop.Components; +using Fika.Core.Coop.Matchmaker; +using Fika.Core.Coop.Players; +using Fika.Core.Networking; +using System.Collections.Generic; +using UnityEngine; + +namespace Fika.Core.Coop.Custom +{ + internal class FikaDebug : MonoBehaviour + { + private CoopHandler coopHandler; + private Rect windowRect = new(20, 20, 200, 200); + private int frameCounter = 0; + + private int Ping + { + get + { + return Singleton.Instance.Ping; + } + } + + private int RTT + { + get + { + return Singleton.Instance.NetClient.FirstPeer.RoundTripTime; + } + } + + private bool isServer = false; + + private List alivePlayers; + private List aliveBots; + + protected void Awake() + { + coopHandler = CoopHandler.GetCoopHandler(); + if (coopHandler == null) + { + FikaPlugin.Instance.FikaLogger.LogError("FikaDebug: CoopHandlera was null!"); + Destroy(this); + } + + if (MatchmakerAcceptPatches.IsServer) + { + isServer = true; + } + + alivePlayers = []; + aliveBots = []; + + enabled = false; + } + + protected void Update() + { + frameCounter++; + if (frameCounter % 300 == 0) + { + frameCounter = 0; + + CheckAndAdd(); + } + } + + private void CheckAndAdd() + { + foreach (CoopPlayer player in coopHandler.Players.Values) + { + if (player.gameObject.name.StartsWith("Player_") || player.IsYourPlayer) + { + if (!alivePlayers.Contains(player) && player.HealthController.IsAlive) + { + AddPlayer(player); + } + } + else + { + if (!aliveBots.Contains(player) && player.HealthController.IsAlive) + { + AddBot(player); + } + } + } + } + + protected void OnEnable() + { + CheckAndAdd(); + } + + protected void OnDisable() + { + foreach (CoopPlayer player in alivePlayers) + { + player.OnPlayerDead -= PlayerDied; + } + alivePlayers.Clear(); + + foreach (CoopPlayer bot in aliveBots) + { + bot.OnPlayerDead -= BotDied; + } + aliveBots.Clear(); + } + + private void AddPlayer(CoopPlayer player) + { + player.OnPlayerDead += PlayerDied; + alivePlayers.Add(player); + } + + private void PlayerDied(EFT.Player player, EFT.IPlayer lastAggressor, DamageInfo damageInfo, EBodyPart part) + { + player.OnPlayerDead -= PlayerDied; + alivePlayers.Remove((CoopPlayer)player); + } + + private void AddBot(CoopPlayer bot) + { + bot.OnPlayerDead += BotDied; + aliveBots.Add(bot); + } + + private void BotDied(EFT.Player player, EFT.IPlayer lastAggressor, DamageInfo damageInfo, EBodyPart part) + { + player.OnPlayerDead -= BotDied; + aliveBots.Remove((CoopPlayer)player); + } + + protected void OnGUI() + { + GUI.skin.label.alignment = TextAnchor.MiddleLeft; + GUI.skin.window.alignment = TextAnchor.UpperCenter; + + GUI.Window(0, windowRect, DrawWindow, "Fika Debug"); + } + + private void DrawWindow(int windowId) + { + Rect rect = new(5, 15, 150, 25); + GUI.Label(rect, $"Alive Players: {alivePlayers.Count}"); + rect.y += 15; + GUI.Label(rect, $"Alive Bots: {aliveBots.Count}"); + if (isServer) + { + rect.y += 15; + GUI.Label(rect, $"Clients: {Singleton.Instance.NetServer.ConnectedPeersCount}"); + } + else + { + rect.y += 15; + GUI.Label(rect, $"Ping: {Ping}"); + rect.y += 15; + GUI.Label(rect, $"RTT: {RTT}"); + } + } + } +} diff --git a/Fika.Core/Coop/GameMode/CoopGame.cs b/Fika.Core/Coop/GameMode/CoopGame.cs index 7ca31798..7a609825 100644 --- a/Fika.Core/Coop/GameMode/CoopGame.cs +++ b/Fika.Core/Coop/GameMode/CoopGame.cs @@ -76,6 +76,7 @@ internal sealed class CoopGame : BaseLocalGame, IBotGame, IF private bool hasSaved = false; private CoopExfilManager exfilManager; private CoopTimeManager timeManager; + private FikaDebug fikaDebug; private bool isServer; public FikaDynamicAI DynamicAI { get; private set; } @@ -865,6 +866,8 @@ public override async Task vmethod_2(int playerId, Vector3 position await WaitForPlayers(); + fikaDebug = gameObject.AddComponent(); + Destroy(customButton); /*if (fikaStartButton != null) { @@ -888,7 +891,7 @@ private void MainPlayerDied(EDamageType obj) } } - public async Task InitPlayer(BotControllerSettings botsSettings, string backendUrl, InventoryControllerClass inventoryController, Callback runCallback) + public async Task InitPlayer(BotControllerSettings botsSettings, string backendUrl, Callback runCallback) { Status = GameStatus.Running; UnityEngine.Random.InitState((int)GClass1304.Now.Ticks); @@ -1864,6 +1867,14 @@ private void StopFromError(string profileId, ExitStatus exitStatus) GClass548.Config.UseSpiritPlayer = false; } + public void ToggleDebug(bool enabled) + { + if (fikaDebug != null) + { + fikaDebug.enabled = enabled; + } + } + public override void CleanUp() { foreach (Player player in dictionary_0.Values)