Skip to content

Commit

Permalink
Allow admin commands to use ! and /
Browse files Browse the repository at this point in the history
- Allows each admin command to be issued with a !command and /command
- Using /command will prevent the command from being seen by other players
  • Loading branch information
data-bomb committed Mar 5, 2024
1 parent f4b7cc7 commit 14b84fd
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 80 deletions.
4 changes: 2 additions & 2 deletions Si_AFKManager/Si_AFK.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public override void OnLateInitializeMelon()

HelperMethods.CommandCallback kickCallback = Command_Kick;
HelperMethods.CommandCallback afkCallback = Command_AFK;
HelperMethods.RegisterAdminCommand("!kick", kickCallback, Power.Kick);
HelperMethods.RegisterAdminCommand("!afk", afkCallback, Power.Kick);
HelperMethods.RegisterAdminCommand("kick", kickCallback, Power.Kick);
HelperMethods.RegisterAdminCommand("afk", afkCallback, Power.Kick);

double interval = 60000.0f;
afkTimer = new System.Timers.Timer(interval);
Expand Down
5 changes: 5 additions & 0 deletions Si_AdminMod/Patch_ClientChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public static void OnRequestPlayerChat(object? sender, OnRequestPlayerChatArgs a

// run the callback
adminCommand.AdminCallback(args.Player, args.Text);

// block if it's a '/' but not a '!'
args.Block = (args.Text[0] == '/');
return;
}

Expand Down Expand Up @@ -105,6 +108,8 @@ public static bool IsValidCommandPrefix(string commandString)
public static AdminCommand? GetAdminCommand(string commandString)
{
String thisCommandText = commandString.Split(' ')[0];
// trim first character
thisCommandText = thisCommandText[1..];
return AdminMethods.FindAdminCommandFromString(thisCommandText);
}

Expand Down
2 changes: 1 addition & 1 deletion Si_AutoTeamsSelect/Si_AutoTeamsSelect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public override void OnInitializeMelon()
public override void OnLateInitializeMelon()
{
HelperMethods.CommandCallback changeNextModeCallback = Command_ChangeNextMode;
HelperMethods.RegisterAdminCommand("!nextmode", changeNextModeCallback, Power.Map);
HelperMethods.RegisterAdminCommand("nextmode", changeNextModeCallback, Power.Map);

#if NET6_0
bool QListLoaded = RegisteredMelons.Any(m => m.Info.Name == "QList");
Expand Down
6 changes: 3 additions & 3 deletions Si_BasicBanlist/Si_BasicBans.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ public override void OnInitializeMelon()
public override void OnLateInitializeMelon()
{
HelperMethods.CommandCallback banCallback = Command_Ban;
HelperMethods.RegisterAdminCommand("!ban", banCallback, Power.Ban);
HelperMethods.RegisterAdminCommand("!kickban", banCallback, Power.Ban);
HelperMethods.RegisterAdminCommand("ban", banCallback, Power.Ban);
HelperMethods.RegisterAdminCommand("kickban", banCallback, Power.Ban);

HelperMethods.CommandCallback unbanCallback = Command_Unban;
HelperMethods.RegisterAdminCommand("!unban", unbanCallback, Power.Unban);
HelperMethods.RegisterAdminCommand("unban", unbanCallback, Power.Unban);

#if NET6_0
bool QListLoaded = RegisteredMelons.Any(m => m.Info.Name == "QList");
Expand Down
4 changes: 2 additions & 2 deletions Si_ChatSilence/Si_Silence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public override void OnLateInitializeMelon()
{
// register commands
HelperMethods.CommandCallback silenceCallback = Command_Silence;
HelperMethods.RegisterAdminCommand("!silence", silenceCallback, Power.Mute);
HelperMethods.RegisterAdminCommand("silence", silenceCallback, Power.Mute);

HelperMethods.CommandCallback unSilenceCallback = Command_UnSilence;
HelperMethods.RegisterAdminCommand("!unsilence", unSilenceCallback, Power.Mute);
HelperMethods.RegisterAdminCommand("unsilence", unSilenceCallback, Power.Mute);


// subscribe to the OnRequestPlayerChat event
Expand Down
134 changes: 71 additions & 63 deletions Si_CommManagement/Si_CmdrMgr.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Silica Commander Management Mod
Copyright (C) 2023-2024 by databomb
Copyright (C) 2024 by databomb
* Description *
For Silica servers, establishes a random selection for commander at the
Expand Down Expand Up @@ -58,7 +58,7 @@ public class CommanderManager : MelonMod

public class BanEntry
{
public ulong OffenderSteamId
public long OffenderSteamId
{
get;
set;
Expand Down Expand Up @@ -170,19 +170,16 @@ public override void OnLateInitializeMelon()
{
// register commands
HelperMethods.CommandCallback commanderBanCallback = Command_CommanderBan;
HelperMethods.RegisterAdminCommand("!cmdrban", commanderBanCallback, Power.Commander);
HelperMethods.RegisterAdminCommand("!commanderban", commanderBanCallback, Power.Commander);
HelperMethods.RegisterAdminCommand("!cban", commanderBanCallback, Power.Commander);
HelperMethods.RegisterAdminCommand("cmdrban", commanderBanCallback, Power.Commander);
HelperMethods.RegisterAdminCommand("commanderban", commanderBanCallback, Power.Commander);
HelperMethods.RegisterAdminCommand("cban", commanderBanCallback, Power.Commander);

HelperMethods.CommandCallback commanderUnbanCallback = Command_CommanderUnban;
HelperMethods.RegisterAdminCommand("!removecommanderban", commanderUnbanCallback, Power.Commander);
HelperMethods.RegisterAdminCommand("!uncban", commanderUnbanCallback, Power.Commander);
HelperMethods.RegisterAdminCommand("removecommanderban", commanderUnbanCallback, Power.Commander);
HelperMethods.RegisterAdminCommand("uncban", commanderUnbanCallback, Power.Commander);

HelperMethods.CommandCallback commanderDemoteCallback = Command_CommanderDemote;
HelperMethods.RegisterAdminCommand("!demote", commanderDemoteCallback, Power.Commander);

HelperMethods.CommandCallback commanderCommandCallback = Command_Commander;
HelperMethods.RegisterPlayerCommand("!commander", commanderCommandCallback, false);
HelperMethods.RegisterAdminCommand("demote", commanderDemoteCallback, Power.Commander);

// subscribe to the OnRequestCommander event
Event_Roles.OnRequestCommander += OnRequestCommander;
Expand Down Expand Up @@ -425,7 +422,7 @@ public static bool RemoveCommanderBan(Player playerToCmdrUnban)
}

BanEntry? matchingCmdrBan;
matchingCmdrBan = MasterBanList.Find(i => i.OffenderSteamId == playerToCmdrUnban.PlayerID.m_SteamID);
matchingCmdrBan = MasterBanList.Find(i => i.OffenderSteamId == (long)playerToCmdrUnban.PlayerID.m_SteamID);

if (matchingCmdrBan == null)
{
Expand All @@ -450,7 +447,7 @@ public static void AddCommanderBan(Player playerToCmdrBan)
Player serverPlayer = NetworkGameServer.GetServerPlayer();
BanEntry thisBan = new BanEntry()
{
OffenderSteamId = playerToCmdrBan.PlayerID.m_SteamID,
OffenderSteamId = long.Parse(playerToCmdrBan.ToString().Split('_')[1]),
OffenderName = playerToCmdrBan.PlayerName,
UnixBanTime = (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds,
Comments = "banned from playing commander by " + serverPlayer.PlayerName
Expand All @@ -467,7 +464,7 @@ public static void AddCommanderBan(Player playerToCmdrBan)
}

// are we already banned?
if (IsPlayerCommanderBanned(playerToCmdrBan))
if (MasterBanList.Find(i => i.OffenderSteamId == thisBan.OffenderSteamId) != null)
{
MelonLogger.Warning("Player name (" + thisBan.OffenderName + ") SteamID (" + thisBan.OffenderSteamId.ToString() + ") already on commander banlist.");
}
Expand Down Expand Up @@ -523,17 +520,17 @@ public static void DemoteTeamsCommander(MP_Strategy strategyInstance, Team Targe
{
Player DemotedCommander = strategyInstance.GetCommanderForTeam(TargetTeam);

#if NET6_0
#if NET6_0
strategyInstance.SetCommander(TargetTeam, null);
strategyInstance.RPC_SynchCommander(TargetTeam);
#else
#else
Type strategyType = typeof(MP_Strategy);
MethodInfo setCommanderMethod = strategyType.GetMethod("SetCommander", BindingFlags.Instance | BindingFlags.NonPublic);
setCommanderMethod.Invoke(strategyInstance, parameters: new object?[] { TargetTeam, null });

MethodInfo synchCommanderMethod = strategyType.GetMethod("RPC_SynchCommander", BindingFlags.Instance | BindingFlags.NonPublic);
synchCommanderMethod.Invoke(strategyInstance, new object[] { TargetTeam });
#endif
#endif

// need to get the player back to Infantry and not stuck in no-clip
SendToRole(DemotedCommander, MP_Strategy.ETeamRole.INFANTRY);
Expand Down Expand Up @@ -721,6 +718,7 @@ public static void Command_CommanderBan(Player callerPlayer, String args)
}
}

#if false
[HarmonyPatch(typeof(GameMode), nameof(GameMode.CreateRPCPacket))]
private static class CommanderManager_Patch_GameMode_GameByteStreamWriter
{
Expand Down Expand Up @@ -759,6 +757,7 @@ static void Postfix(GameMode __instance, GameByteStreamWriter __result, byte __0
}
}
}
#endif

[HarmonyPatch(typeof(GameMode), nameof(GameMode.OnPlayerLeftBase))]
private static class CommanderManager_Patch_GameMode_OnPlayerLeftBase
Expand Down Expand Up @@ -846,60 +845,69 @@ public static void Postfix(GameMode __instance, Unit __result, Player __0, Unity
}
}

public static void Command_Commander(Player callerPlayer, String args)
#if NET6_0
[HarmonyPatch(typeof(Il2CppSilica.UI.Chat), nameof(Il2CppSilica.UI.Chat.MessageReceived))]
#else
[HarmonyPatch(typeof(Silica.UI.Chat), "MessageReceived")]
#endif
private static class CommanderManager_Patch_Chat_MessageReceived
{
try
#if NET6_0
public static void Postfix(Il2CppSilica.UI.Chat __instance, Player __0, string __1, bool __2)
#else
public static void Postfix(Silica.UI.Chat __instance, Player __0, string __1, bool __2)
#endif
{
// count number of arguments
int argumentCount = args.Split(' ').Length - 1;
if (argumentCount > 0)
{
HelperMethods.ReplyToCommand(args.Split(' ')[0] + ": Too many arguments");
return;
}

if (commanderApplicants == null)
{
return;
}

if (callerPlayer.Team == null)
{
HelperMethods.ReplyToCommand_Player(callerPlayer, "is not on a valid team");
return;
}

// check if they're trying to apply for commander before the 30 second countdown expires and the game begins
if (GameMode.CurrentGameMode.Started && !GameMode.CurrentGameMode.GameBegun)
try
{
// check if we are already on the commander applicant list
bool hasApplied = commanderApplicants[callerPlayer.Team.Index].Any(k => k == callerPlayer);

if (hasApplied)
if (commanderApplicants == null)
{
commanderApplicants[callerPlayer.Team.Index].Remove(callerPlayer);
HelperMethods.ReplyToCommand_Player(callerPlayer, "removed themselves from commander lottery");
return;
}

if (IsPlayerCommanderBanned(callerPlayer))
// each faction has its own chat manager but by looking at alien and only global messages this catches commands only once
if (__instance.ToString().Contains("alien") && __2 == false)
{
HelperMethods.ReplyToCommand_Player(callerPlayer, "cannot apply (restricted to infantry)");
return;
}
bool isCommanderCommand = String.Equals(__1, "!commander", StringComparison.OrdinalIgnoreCase);
if (isCommanderCommand)
{
if (__0.Team == null)
{
HelperMethods.ReplyToCommand_Player(__0, "is not on a valid team");
return;
}

commanderApplicants[callerPlayer.Team.Index].Add(callerPlayer);
HelperMethods.ReplyToCommand_Player(callerPlayer, "applied for commander");
// check if they're trying to apply for commander before the 30 second countdown expires and the game begins
if (GameMode.CurrentGameMode.Started && !GameMode.CurrentGameMode.GameBegun)
{

// check if we are already on the commander applicant list
bool hasApplied = commanderApplicants[__0.Team.Index].Any(k => k == __0);

if (!hasApplied)
{

commanderApplicants[__0.Team.Index].Add(__0);
HelperMethods.ReplyToCommand_Player(__0, "applied for commander");
}
else
{
commanderApplicants[__0.Team.Index].Remove(__0);
HelperMethods.ReplyToCommand_Player(__0, "removed themselves from commander lottery");
}
}
else
{
HelperMethods.ReplyToCommand_Player(__0, "cannot apply for commander during the game");
}
}
}
}
else
catch (Exception error)
{
HelperMethods.ReplyToCommand_Player(callerPlayer, "cannot apply for commander during the game");
HelperMethods.PrintError(error, "Failed to run Chat::MessageReceived");
}
}
catch (Exception error)
{
HelperMethods.PrintError(error, "Failed to run Command_Commander");
}
}

public static bool AllTeamsHaveCommanderApplicants()
Expand Down Expand Up @@ -937,13 +945,12 @@ public void OnRequestCommander(object? sender, OnRequestCommanderArgs args)
}

// check if player is allowed to be commander
BanEntry? banEntry = MasterBanList.Find(i => i.OffenderSteamId == args.Requester.PlayerID.m_SteamID);
long requestingPlayerSteamId = long.Parse(args.Requester.ToString().Split('_')[1]);
BanEntry? banEntry = MasterBanList.Find(i => i.OffenderSteamId == requestingPlayerSteamId);
if (banEntry != null)
{
MelonLogger.Msg("Preventing " + banEntry.OffenderName + " from playing as commander.");
args.Block = true;

return;
}

// check if they're trying to join before the 30 second countdown expires and the game begins
Expand All @@ -962,15 +969,16 @@ public void OnRequestCommander(object? sender, OnRequestCommanderArgs args)
}
}

public static bool IsPlayerCommanderBanned(Player player)
public static bool IsCommanderBanned(Player player)
{
if (MasterBanList == null)
{
return false;
}

// check if player is allowed to be commander
BanEntry? banEntry = MasterBanList.Find(i => i.OffenderSteamId == player.PlayerID.m_SteamID);
long playerSteamId = long.Parse(player.ToString().Split('_')[1]);
BanEntry? banEntry = MasterBanList.Find(i => i.OffenderSteamId == playerSteamId);
if (banEntry != null)
{
return true;
Expand Down
2 changes: 1 addition & 1 deletion Si_Eject/Si_Eject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class Eject : MelonMod
public override void OnLateInitializeMelon()
{
HelperMethods.CommandCallback ejectCallback = Command_Eject;
HelperMethods.RegisterAdminCommand("!eject", ejectCallback, Power.Eject);
HelperMethods.RegisterAdminCommand("eject", ejectCallback, Power.Eject);
}
public static void Command_Eject(Player callerPlayer, String args)
{
Expand Down
4 changes: 2 additions & 2 deletions Si_EndRound/Si_EndRound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public class EndRound : MelonMod
public override void OnLateInitializeMelon()
{
HelperMethods.CommandCallback roundEndCallback = Command_EndRound;
HelperMethods.RegisterAdminCommand("!endround", roundEndCallback, Power.End);
HelperMethods.RegisterAdminCommand("!endgame", roundEndCallback, Power.End);
HelperMethods.RegisterAdminCommand("endround", roundEndCallback, Power.End);
HelperMethods.RegisterAdminCommand("endgame", roundEndCallback, Power.End);
}

public static void Command_EndRound(Player callerPlayer, String args)
Expand Down
2 changes: 1 addition & 1 deletion Si_Mapcycle/Si_Mapcycle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class MapCycleMod : MelonMod
public override void OnLateInitializeMelon()
{
HelperMethods.CommandCallback mapCallback = Command_ChangeMap;
HelperMethods.RegisterAdminCommand("!map", mapCallback, Power.Map);
HelperMethods.RegisterAdminCommand("map", mapCallback, Power.Map);
}
public static void Command_ChangeMap(Player callerPlayer, String args)
{
Expand Down
10 changes: 5 additions & 5 deletions Si_SpawnConfigs/Si_SpawnConfigs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,19 @@ public override void OnLateInitializeMelon()
if (AdminModAvailable)
{
HelperMethods.CommandCallback spawnCallback = Command_Spawn;
HelperMethods.RegisterAdminCommand("!spawn", spawnCallback, Power.Cheat);
HelperMethods.RegisterAdminCommand("spawn", spawnCallback, Power.Cheat);

HelperMethods.CommandCallback undoSpawnCallback = Command_UndoSpawn;
HelperMethods.RegisterAdminCommand("!undospawn", undoSpawnCallback, Power.Cheat);
HelperMethods.RegisterAdminCommand("undospawn", undoSpawnCallback, Power.Cheat);

HelperMethods.CommandCallback saveCallback = Command_SaveSetup;
HelperMethods.RegisterAdminCommand("!saveconfig", saveCallback, Power.Cheat);
HelperMethods.RegisterAdminCommand("saveconfig", saveCallback, Power.Cheat);

HelperMethods.CommandCallback loadCallback = Command_LoadSetup;
HelperMethods.RegisterAdminCommand("!loadconfig", loadCallback, Power.Cheat);
HelperMethods.RegisterAdminCommand("loadconfig", loadCallback, Power.Cheat);

HelperMethods.CommandCallback addCallback = Command_AddSetup;
HelperMethods.RegisterAdminCommand("!addconfig", addCallback, Power.Cheat);
HelperMethods.RegisterAdminCommand("addconfig", addCallback, Power.Cheat);
}
else
{
Expand Down

0 comments on commit 14b84fd

Please sign in to comment.