Skip to content

Commit

Permalink
Add debug screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Lacyway committed Jun 2, 2024
1 parent 1845186 commit 9ac4a6e
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 1 deletion.
20 changes: 20 additions & 0 deletions Fika.Core/Console/FikaCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IFikaGame>.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()
{
Expand Down
162 changes: 162 additions & 0 deletions Fika.Core/Coop/Custom/FikaDebug.cs
Original file line number Diff line number Diff line change
@@ -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<FikaClient>.Instance.Ping;
}
}

private int RTT
{
get
{
return Singleton<FikaClient>.Instance.NetClient.FirstPeer.RoundTripTime;
}
}

private bool isServer = false;

private List<CoopPlayer> alivePlayers;
private List<CoopPlayer> 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<FikaServer>.Instance.NetServer.ConnectedPeersCount}");
}
else
{
rect.y += 15;
GUI.Label(rect, $"Ping: {Ping}");
rect.y += 15;
GUI.Label(rect, $"RTT: {RTT}");
}
}
}
}
13 changes: 12 additions & 1 deletion Fika.Core/Coop/GameMode/CoopGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ internal sealed class CoopGame : BaseLocalGame<EftGamePlayerOwner>, IBotGame, IF
private bool hasSaved = false;
private CoopExfilManager exfilManager;
private CoopTimeManager timeManager;
private FikaDebug fikaDebug;
private bool isServer;

public FikaDynamicAI DynamicAI { get; private set; }
Expand Down Expand Up @@ -865,6 +866,8 @@ public override async Task<LocalPlayer> vmethod_2(int playerId, Vector3 position

await WaitForPlayers();

fikaDebug = gameObject.AddComponent<FikaDebug>();

Destroy(customButton);
/*if (fikaStartButton != null)
{
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9ac4a6e

Please sign in to comment.