Skip to content

Commit

Permalink
Add Preference for Debug Loggin
Browse files Browse the repository at this point in the history
- Adds preference Admin_EnableDebugLogging (default: false) to toggle the additional debug logging messages to the logs and console
  • Loading branch information
data-bomb committed Jul 19, 2024
1 parent b56f301 commit 861e323
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 22 deletions.
5 changes: 4 additions & 1 deletion Si_AdminMod/AdminMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ public static void RegisterAdminCommand(String adminCommand, HelperMethods.Comma
if (addAdminConsoleCmd != null)
{
Dictionary<string, DebugConsole.ICommand> s_Commands = (Dictionary<string, DebugConsole.ICommand>)commandField.GetValue(null);
MelonLogger.Msg(addAdminConsoleCmd.Key.ToLower() + " console command registered.");
if (SiAdminMod.Pref_Admin_DebugLogMessages.Value)
{
MelonLogger.Msg(addAdminConsoleCmd.Key.ToLower() + " console command registered.");
}
s_Commands.Add(addAdminConsoleCmd.Key.ToLower(), addAdminConsoleCmd);
commandField.SetValue(null, s_Commands);
}
Expand Down
15 changes: 12 additions & 3 deletions Si_AdminMod/ChatVotes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ public static void Command_VoteChat(Player? callerPlayer, String args)
return;
}

MelonLogger.Msg("Reached vote callback for: " + args);
if (SiAdminMod.Pref_Admin_DebugLogMessages.Value)
{
MelonLogger.Msg("Reached vote callback for: " + args);
}

foreach (OptionVoteResult currentVoteResult in currentVoteResults.DetailedResults)
{
Expand Down Expand Up @@ -132,7 +135,10 @@ public static void HoldVote(ChatVoteBallot ballot)

private static void StartVoteDurationTimer()
{
MelonLogger.Msg("Starting vote timer with duration " + SiAdminMod.Pre_Admin_VoteDuration.Value.ToString());
if (SiAdminMod.Pref_Admin_DebugLogMessages.Value)
{
MelonLogger.Msg("Starting vote timer with duration " + SiAdminMod.Pre_Admin_VoteDuration.Value.ToString());
}
voteInProgress = true;
HelperMethods.StartTimer(ref Timer_TallyVote);
}
Expand Down Expand Up @@ -178,7 +184,10 @@ public static void Postfix(MusicJukeboxHandler __instance)
winningResult = currentVoteResult;
}

MelonLogger.Msg("Found command " + currentVoteResult.Command + " with votes " + currentVoteResult.Votes);
if (SiAdminMod.Pref_Admin_DebugLogMessages.Value)
{
MelonLogger.Msg("Found command " + currentVoteResult.Command + " with votes " + currentVoteResult.Votes);
}

// unregister commands for current vote
PlayerMethods.UnregisterPlayerPhrase(currentVoteResult.Command);
Expand Down
11 changes: 9 additions & 2 deletions Si_AdminMod/Event_Netcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,19 @@ static void Prefix(GameByteStreamReader __result, ref byte[] __0, int __1, uint
return;
}

MelonLogger.Msg("Firing OnRequestPlayerChatEvent for player (size=" + __1.ToString() + "): " + chatterPlayer.PlayerName);
if (SiAdminMod.Pref_Admin_DebugLogMessages.Value)
{
MelonLogger.Msg("Firing OnRequestPlayerChatEvent for player (size=" + __1.ToString() + "): " + chatterPlayer.PlayerName);
}

OnRequestPlayerChatArgs onRequestPlayerChatArgs = FireOnRequestPlayerChatEvent(chatterPlayer, chatText, chatTeamOnly);

if (onRequestPlayerChatArgs.Block)
{
MelonLogger.Msg("Blocking chat message for player: " + chatterPlayer.PlayerName);
if (SiAdminMod.Pref_Admin_DebugLogMessages.Value)
{
MelonLogger.Msg("Blocking chat message for player: " + chatterPlayer.PlayerName);
}
__0[1] = (byte)127;
}

Expand Down
21 changes: 17 additions & 4 deletions Si_AdminMod/Event_Roles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,28 @@ public static bool Prefix(MP_Strategy __instance, ref GameByteStreamReader __0,

if (onRequestCommanderArgs.Block)
{
MelonLogger.Msg("Blocking commander role request for " + onRequestCommanderArgs.Requester.PlayerName);
if (SiAdminMod.Pref_Admin_DebugLogMessages.Value)
{
MelonLogger.Msg("Blocking commander role request for " + onRequestCommanderArgs.Requester.PlayerName);
}

if (!onRequestCommanderArgs.PreventSpawnWhenBlocked)
{
MelonLogger.Msg("Preventing Spawn");
if (SiAdminMod.Pref_Admin_DebugLogMessages.Value)
{
MelonLogger.Msg("Preventing Spawn");
}
__instance.SpawnUnitForPlayer(requestingPlayer, requestingPlayer.Team);
FireOnRoleChangedEvent(requestingPlayer, GameModeExt.ETeamRole.INFANTRY);
}

return false;
}

MelonLogger.Msg("Allowing to join commander");
if (SiAdminMod.Pref_Admin_DebugLogMessages.Value)
{
MelonLogger.Msg("Allowing to join commander");
}
#if NET6_0
__instance.SetCommander(baseTeamSetup.Team, requestingPlayer);
__instance.RPC_SynchCommander(baseTeamSetup.Team);
Expand Down Expand Up @@ -142,7 +152,10 @@ public static GameByteStreamReader RestoreRPC_RequestRoleReader(Player requestin

public static void FireOnRoleChangedEvent(Player player, GameModeExt.ETeamRole role)
{
MelonLogger.Msg("Firing Role Change Event for " + player.PlayerName + " to role " + role.ToString());
if (SiAdminMod.Pref_Admin_DebugLogMessages.Value)
{
MelonLogger.Msg("Firing Role Change Event for " + player.PlayerName + " to role " + role.ToString());
}

OnRoleChangedArgs onRoleChangedArgs = new OnRoleChangedArgs
{
Expand Down
10 changes: 8 additions & 2 deletions Si_AdminMod/Event_Units.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,17 @@ public static bool Prefix(UnitCompartment __instance, Unit __0)

if (onRequestEnterUnitArgs.Block)
{
MelonLogger.Msg("Blocking player " + __0.ControlledBy.PlayerName + " from entering unit " + __instance.OwnerUnit.ToString());
if (SiAdminMod.Pref_Admin_DebugLogMessages.Value)
{
MelonLogger.Msg("Blocking player " + __0.ControlledBy.PlayerName + " from entering unit " + __instance.OwnerUnit.ToString());
}
return false;
}

MelonLogger.Msg("Allowing player to enter unit's compartment");
if (SiAdminMod.Pref_Admin_DebugLogMessages.Value)
{
MelonLogger.Msg("Allowing player to enter unit's compartment");
}

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion Si_AdminMod/ModAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ You should have received a copy of the GNU General Public License
using SilicaAdminMod;
using System.Drawing;

[assembly: MelonInfo(typeof(SiAdminMod), "Admin Mod", "2.0.901", "databomb", "https://github.com/data-bomb/Silica")]
[assembly: MelonInfo(typeof(SiAdminMod), "Admin Mod", "2.0.902", "databomb", "https://github.com/data-bomb/Silica")]
[assembly: MelonGame("Bohemia Interactive", "Silica")]

// Color.Cyan
Expand Down
15 changes: 12 additions & 3 deletions Si_AdminMod/Patch_ClientChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ public static void OnRequestPlayerChat(object? sender, OnRequestPlayerChatArgs a
return;
}

MelonLogger.Msg("Processing admin or player command.");
if (SiAdminMod.Pref_Admin_DebugLogMessages.Value)
{
MelonLogger.Msg("Processing admin or player command.");
}

// ignore team chat if preference is set
if (args.TeamOnly && SiAdminMod.Pref_Admin_AcceptTeamChatCommands != null && !SiAdminMod.Pref_Admin_AcceptTeamChatCommands.Value)
Expand All @@ -64,7 +67,10 @@ public static void OnRequestPlayerChat(object? sender, OnRequestPlayerChatArgs a
AdminCommand? adminCommand = GetAdminCommand(args.Text);
if (adminCommand != null)
{
MelonLogger.Msg("Processing admin command: " + adminCommand.AdminCommandText);
if (SiAdminMod.Pref_Admin_DebugLogMessages.Value)
{
MelonLogger.Msg("Processing admin command: " + adminCommand.AdminCommandText);
}

// are they an admin?
if (!args.Player.IsAdmin())
Expand Down Expand Up @@ -97,7 +103,10 @@ public static void OnRequestPlayerChat(object? sender, OnRequestPlayerChatArgs a
return;
}

MelonLogger.Msg("Processing player command: " + playerCommand.CommandName);
if (SiAdminMod.Pref_Admin_DebugLogMessages.Value)
{
MelonLogger.Msg("Processing player command: " + playerCommand.CommandName);
}

// run the callback
playerCommand.PlayerCommandCallback(args.Player, args.Text);
Expand Down
21 changes: 15 additions & 6 deletions Si_AdminMod/Si_AdminMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ You should have received a copy of the GNU General Public License
using System.Collections.Generic;
using System.Data;
using UnityEngine;
using static MelonLoader.MelonLogger;

namespace SilicaAdminMod
{
Expand All @@ -46,14 +45,14 @@ public class SiAdminMod : MelonMod
public static MelonPreferences_Entry<int> Pre_Admin_VoteDuration = null!;
public static MelonPreferences_Entry<bool> Pref_Admin_StopNonAdminCheats = null!;
public static MelonPreferences_Entry<bool> Pref_Admin_ReplicateCheatsForPasswordedServers = null!;
public static MelonPreferences_Entry<bool> Pref_Admin_DebugLogMessages = null!;

public static List<Admin> AdminList = null!;

public override void OnInitializeMelon()
{
try
{

AdminMethods.AdminCommands = new List<AdminCommand>();
PlayerMethods.PlayerCommands = new List<PlayerCommand>();
PlayerMethods.PlayerPhrases = new List<PlayerCommand>();
Expand All @@ -65,7 +64,8 @@ public override void OnInitializeMelon()
Pre_Admin_VoteDuration ??= _modCategory.CreateEntry<int>("Admin_VoteDuration_Seconds", 30);
Pref_Admin_StopNonAdminCheats ??= _modCategory.CreateEntry<bool>("Admin_PreventNonAdminCheats", false);
Pref_Admin_ReplicateCheatsForPasswordedServers ??= _modCategory.CreateEntry<bool>("Admin_ReplicateCheatsForPrivateServers", true);

Pref_Admin_DebugLogMessages ??= _modCategory.CreateEntry<bool>("Admin_EnableDebugLogging", false);

#if !NET6_0
MelonLogger.Msg("Registering host console commands...");

Expand All @@ -75,21 +75,30 @@ public override void OnInitializeMelon()
DebugConsole.ICommand addAdminConsoleCmd = (DebugConsole.ICommand)Activator.CreateInstance(typeof(CSAM_AddAdmin));
if (addAdminConsoleCmd != null)
{
MelonLogger.Msg(addAdminConsoleCmd.Key.ToLower() + " registered.");
if (Pref_Admin_DebugLogMessages.Value)
{
MelonLogger.Msg(addAdminConsoleCmd.Key.ToLower() + " registered.");
}
s_Commands.Add(addAdminConsoleCmd.Key.ToLower(), addAdminConsoleCmd);
}

DebugConsole.ICommand addSayConsoleCmd = (DebugConsole.ICommand)Activator.CreateInstance(typeof(CSAM_Say));
if (addSayConsoleCmd != null)
{
MelonLogger.Msg(addSayConsoleCmd.Key.ToLower() + " registered.");
if (Pref_Admin_DebugLogMessages.Value)
{
MelonLogger.Msg(addSayConsoleCmd.Key.ToLower() + " registered.");
}
s_Commands.Add(addSayConsoleCmd.Key.ToLower(), addSayConsoleCmd);
}

DebugConsole.ICommand cvarConsoleCmd = (DebugConsole.ICommand)Activator.CreateInstance(typeof(CSAM_Cvar));
if (cvarConsoleCmd != null)
{
MelonLogger.Msg(cvarConsoleCmd.Key.ToLower() + " registered.");
if (Pref_Admin_DebugLogMessages.Value)
{
MelonLogger.Msg(cvarConsoleCmd.Key.ToLower() + " registered.");
}
s_Commands.Add(cvarConsoleCmd.Key.ToLower(), cvarConsoleCmd);
}

Expand Down

0 comments on commit 861e323

Please sign in to comment.