Skip to content

Commit

Permalink
Adds Bounds Checking for Anti-Grief
Browse files Browse the repository at this point in the history
- Generates an error log entry for when player indices being outside of expected bounds
- Avoids using an index that's outside of the expected bounds
  • Loading branch information
data-bomb committed Dec 29, 2024
1 parent 93743c3 commit 6e17744
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions Si_AntiGrief/Si_AntiGrief.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ You should have received a copy of the GNU General Public License
using System.Linq;
using UnityEngine;

[assembly: MelonInfo(typeof(AntiGrief), "Anti-Grief", "1.4.1", "databomb", "https://github.com/data-bomb/Silica")]
[assembly: MelonInfo(typeof(AntiGrief), "Anti-Grief", "1.4.2", "databomb", "https://github.com/data-bomb/Silica")]
[assembly: MelonGame("Bohemia Interactive", "Silica")]
[assembly: MelonOptionalDependencies("Admin Mod")]

Expand Down Expand Up @@ -174,7 +174,7 @@ public static void Postfix(Target __0, GameObject __1)
}

[HarmonyPatch(typeof(GameMode), nameof(GameMode.SpawnUnitForPlayer), new Type[] { typeof(Player), typeof(GameObject), typeof(Vector3), typeof(Quaternion) })]
private static class CommanderManager_Patch_GameMode_SpawnUnitForPlayer
private static class AntiGrief_Patch_GameMode_SpawnUnitForPlayer
{
public static void Postfix(GameMode __instance, Unit __result, Player __0, UnityEngine.GameObject __1, UnityEngine.Vector3 __2, UnityEngine.Quaternion __3)
{
Expand All @@ -185,6 +185,14 @@ public static void Postfix(GameMode __instance, Unit __result, Player __0, Unity
return;
}

// GetIndex() will return -1 if this is not a valid player on the list
int playerIndex = __0.GetIndex();
if (IsPlayerIndexValid(playerIndex))
{
MelonLogger.Error("player index found outside of bounds: " + playerIndex);
return;
}

playerTransferCount[__0.GetIndex()] = 0;
}
catch (Exception error)
Expand Down Expand Up @@ -236,7 +244,15 @@ public void OnRequestEnterUnit(object? sender, OnRequestEnterUnitArgs args)
DeletePriorUnit(player, player.ControlledUnit);
}

playerTransferCount[player.GetIndex()]++;
// GetIndex() will return -1 if this is not a valid player on the list
int playerIndex = player.GetIndex();
if (IsPlayerIndexValid(playerIndex))
{
MelonLogger.Error("Player index found outside of bounds: " + playerIndex);
return;
}

playerTransferCount[playerIndex]++;
}
catch (Exception error)
{
Expand Down Expand Up @@ -266,10 +282,18 @@ public static bool ShouldDeletePriorUnit(Player player)
return false;
}

// we don't want the user to be able to delete an endless supply of starting units
if (GameMode.CurrentGameMode.GetCanDeletePlayerControlledUnit(player, currentUnit))
{
// we don't want the user to be able to delete an endless supply of starting units
if (playerTransferCount[player.GetIndex()] >= 1)
// GetIndex() will return -1 if this is not a valid player on the list
int playerIndex = player.GetIndex();
if (IsPlayerIndexValid(playerIndex))
{
MelonLogger.Error("Player Index found outside of bounds: " + playerIndex);
return false;
}

if (playerTransferCount[playerIndex] >= 1)
{
return false;
}
Expand Down Expand Up @@ -387,6 +411,16 @@ public static bool IsHighValueAlienUnit(Unit unit)
return false;
}

private static bool IsPlayerIndexValid(int playerIndex)
{
if (playerIndex >= 0 && playerIndex <= NetworkGameServer.GetPlayersMax())
{
return true;
}

return false;
}

private static bool DisplayTeamKillForStructure(string structureName)
{
// has the server set it so Nodes should be ignored?
Expand Down

0 comments on commit 6e17744

Please sign in to comment.