GBSU/Patches/MultiplayerPatches.cs
2025-03-14 21:37:52 +01:00

98 lines
3 KiB
C#

using HarmonyLib;
using GB.Game;
using System.Collections.Generic;
using GB.Networking.Objects;
using System;
using UnityEngine;
using CoreNet.Objects;
using CoreNet.Components.Server;
using CoreNet.Model;
#pragma warning disable IDE0051 // Private member is unused
namespace GBSU.Patches;
[HarmonyPatch(typeof(GameMode), "HandleScore")]
class HandleScorePatch
{
static bool Prefix(GameMode __instance)
{
Plugin.Log.LogDebug("HandleScorePatch postfix called");
if (__instance.IsGameOver())
{
List<NetGang> gameWinners = __instance.GetGameWinners();
int gameWinnersCount = gameWinners.Count;
Plugin.Log.LogDebug($"{gameWinnersCount} player(s) won.");
foreach (var winner in gameWinners)
{
string gangColor = ColorUtility.ToHtmlStringRGBA(winner.GangColor);
Plugin.Log.LogDebug($"Processing {gangColor} score.");
// Handling Score dict
if (!Plugin.GameScore.ContainsKey(gangColor))
{
Plugin.Log.LogDebug($"{gangColor} doesn't have a key yet. Creating one and adding +1 to their score!");
Plugin.GameScore.Add(gangColor, 1);
}
else
{
Plugin.Log.LogDebug($"{gangColor} has a key. +1 to their score!");
Plugin.GameScore[gangColor]++;
}
Plugin.Log.LogDebug($"Finished processing {gangColor} score.");
}
Plugin.Log.LogDebug("Done processing all scores!");
}
Plugin.Log.LogInfo("Not letting HandleScore run...");
return false;
}
}
[HarmonyPatch]
class GetNumRemainingGangsAlivePatch
{
[HarmonyReversePatch]
[HarmonyPatch(typeof(GameMode), "GetNumRemainingGangsAlive")]
public static int GetNumRemainingGangsAlive(object instance)
{
// its a stub so it has no initial content
throw new NotImplementedException("It's a stub");
}
}
[HarmonyPatch]
class GameLogicPatches
{
public static int playercount; // number of players connected
[HarmonyPrefix, HarmonyPatch(typeof(NetServerSceneManager), "IsWaitingPlayersReady")]
private static void Postfix(NetModel ___Model)
{
playercount = ___Model.GetCollection<NetMember>("NET_MEMBERS").Count;
}
[HarmonyPrefix, HarmonyPatch(typeof(GameMode), nameof(GameMode.GameModeUpdate))]
private static void Prefix(GameMode __instance, ref float ___timer)
{
//Plugin.Log.LogDebug($"Manually checking if round ended...");
int remaining = GetNumRemainingGangsAlivePatch.GetNumRemainingGangsAlive(__instance); // gangs alive
//Plugin.Log.LogDebug($"{remaining} remaining players alive");
if (remaining <= 1 && playercount != 1)
{
Plugin.Log.LogDebug("That's one gang remaining! Ending round.");
Plugin.Log.LogDebug("Trying to set game timer to 0...");
___timer = 0f; // setting the timer to 0 will end the game on next frame
}
}
}