Skip to content

Commit

Permalink
Add RegisterPlayerCommand
Browse files Browse the repository at this point in the history
- Change admin and player callbacks to use OnRequestPlayerChat event
- Adds new method to RegisterPlayerCommand (non-admin command)
- Minor code improvements
  • Loading branch information
data-bomb committed Mar 4, 2024
1 parent c3f8ed3 commit 5340701
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 59 deletions.
5 changes: 3 additions & 2 deletions Si_AdminMod/Event_Netcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static void Prefix(GameByteStreamReader __result, ref byte[] __0, int __1, bool
}

MelonLogger.Msg("Firing OnRequestPlayerChatEvent for player: " + chatterPlayer.PlayerName);
OnRequestPlayerChatArgs onRequestPlayerChatArgs = FireOnRequestPlayerChatEvent(chatterPlayer, chatText);
OnRequestPlayerChatArgs onRequestPlayerChatArgs = FireOnRequestPlayerChatEvent(chatterPlayer, chatText, chatTeamOnly);

if (onRequestPlayerChatArgs.Block)
{
Expand All @@ -100,12 +100,13 @@ static void Prefix(GameByteStreamReader __result, ref byte[] __0, int __1, bool
}
}

public static OnRequestPlayerChatArgs FireOnRequestPlayerChatEvent(Player player, string message)
public static OnRequestPlayerChatArgs FireOnRequestPlayerChatEvent(Player player, string message, bool teamOnly)
{
OnRequestPlayerChatArgs onRequestPlayerChatArgs = new OnRequestPlayerChatArgs
{
Player = player,
Text = message,
TeamOnly = teamOnly,
Block = false
};
OnRequestPlayerChat?.Invoke(null, onRequestPlayerChatArgs);
Expand Down
6 changes: 6 additions & 0 deletions Si_AdminMod/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ public string Text
set => _text = value ?? throw new ArgumentNullException("Text cannot be empty.");
}

public bool TeamOnly
{
get;
set;
}

public bool Block
{
get;
Expand Down
14 changes: 8 additions & 6 deletions Si_AdminMod/HostActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ public static bool AddAdmin(Player player, String powerText, byte level)
return false;
}

Admin admin = new Admin();
admin.Name = player.PlayerName;
admin.SteamId = playerSteamId;
admin.Level = level;
admin.Powers = HelperMethods.PowerTextToPower(powerText);
admin.CreatedOn = (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
Admin admin = new Admin
{
Name = player.PlayerName,
SteamId = playerSteamId,
Level = level,
Powers = HelperMethods.PowerTextToPower(powerText),
CreatedOn = (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds
};
admin.LastModifiedOn = admin.CreatedOn;

SiAdminMod.AdminList.Add(admin);
Expand Down
126 changes: 81 additions & 45 deletions Si_AdminMod/Patch_ClientChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,77 +18,113 @@ You should have received a copy of the GNU General Public License
*/

using HarmonyLib;
using MelonLoader;
using System;

namespace SilicaAdminMod
{
#if NET6_0
[HarmonyPatch(typeof(Il2CppSilica.UI.Chat), nameof(Il2CppSilica.UI.Chat.MessageReceived))]
#else
[HarmonyPatch(typeof(Silica.UI.Chat), "MessageReceived")]
#endif
static class Patch_MessageReceived_AdminCommands
public static class ClientChatHandler
{
public static void OnRequestPlayerChat(object? sender, OnRequestPlayerChatArgs args)
{
#if NET6_0
public static void Postfix(Il2CppSilica.UI.Chat __instance, Il2Cpp.Player __0, string __1, bool __2)
#else
public static void Postfix(Silica.UI.Chat __instance, Player __0, string __1, bool __2)
#endif
{
try
try
{
if (args.Player == null)
{
// check if this even has a '!' or '/' as the command prefix
if (__1[0] != '!' && __1[0] != '/')
{
return;
}

// ignore team chat if preference is set
if (__2 && SiAdminMod.Pref_Admin_AcceptTeamChatCommands != null && !SiAdminMod.Pref_Admin_AcceptTeamChatCommands.Value)
{
return;
}
return;
}

// 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"))
{
return;
}
// check if this even starts with a character that indicates it's a command
if (!IsValidCommandPrefix(args.Text))
{
return;
}

// check if the first portion matches an admin command
String thisCommandText = __1.Split(' ')[0];
AdminCommand? checkCommand = AdminMethods.FindAdminCommandFromString(thisCommandText);
MelonLogger.Msg("Processing admin or player command.");

if (checkCommand == null)
{
return;
}
// ignore team chat if preference is set
if (args.TeamOnly && SiAdminMod.Pref_Admin_AcceptTeamChatCommands != null && !SiAdminMod.Pref_Admin_AcceptTeamChatCommands.Value)
{
return;
}

// check if the first portion matches an admin command
AdminCommand? adminCommand = GetAdminCommand(args.Text);
if (adminCommand != null)
{
// are they an admin?
if (!__0.IsAdmin())
if (!args.Player.IsAdmin())
{
HelperMethods.ReplyToCommand_Player(__0, "is not an admin");
HelperMethods.ReplyToCommand_Player(args.Player, "is not an admin");
return;
}

// do they have the matching power?
Power callerPowers = __0.GetAdminPowers();
Power callerPowers = args.Player.GetAdminPowers();

if (!AdminMethods.PowerInPowers(checkCommand.AdminPower, callerPowers))
if (!AdminMethods.PowerInPowers(adminCommand.AdminPower, callerPowers))
{
HelperMethods.ReplyToCommand_Player(__0, "unauthorized command");
HelperMethods.ReplyToCommand_Player(args.Player, "unauthorized command");
return;
}

// run the callback
checkCommand.AdminCallback(__0, __1);
adminCommand.AdminCallback(args.Player, args.Text);
return;
}
catch (Exception error)

// check if the first portion matches a player command
PlayerCommand? playerCommand = GetPlayerCommand(args.Text);
if (playerCommand == null)
{
return;
}

// run the callback
playerCommand.PlayerCommandCallback(args.Player, args.Text);

// let the command registrant decide whether the chat goes through
args.Block = playerCommand.HideChatMessage;
}
catch (Exception error)
{
HelperMethods.PrintError(error, "Failed to run AdminMod::OnRequestPlayerChat");
}
}

public static bool IsValidCommandPrefix(string commandString)
{
if (commandString[0] == '!' || commandString[0] == '/' || commandString[0] == '.')
{
return true;
}

return false;
}

public static AdminCommand? GetAdminCommand(string commandString)
{
String thisCommandText = commandString.Split(' ')[0];
return AdminMethods.FindAdminCommandFromString(thisCommandText);
}

public static PlayerCommand? GetPlayerCommand(string commandString)
{
String thisCommandText = commandString.Split(' ')[0];
return FindPlayerCommandFromString(thisCommandText);
}

public static PlayerCommand? FindPlayerCommandFromString(String commandText)
{
foreach (PlayerCommand command in SiAdminMod.PlayerCommands)
{
if (command.CommandName == commandText)
{
HelperMethods.PrintError(error, "Failed to run MessageReceived");
return command;
}
}

return;
return null;
}
}
}
48 changes: 48 additions & 0 deletions Si_AdminMod/PlayerCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Silica Admin Mod
Copyright (C) 2024 by databomb
* License *
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

using System;

namespace SilicaAdminMod
{
public class PlayerCommand
{
private string _commandName = null!;

public String CommandName
{
get => _commandName;
set => _commandName = value ?? throw new ArgumentNullException("Command name is required.");
}

private HelperMethods.CommandCallback _commandCallback = null!;

public HelperMethods.CommandCallback PlayerCommandCallback
{
get => _commandCallback;
set => _commandCallback = value ?? throw new ArgumentNullException("Command callback is required.");
}

public bool HideChatMessage
{
get;
set;
}
}
}
26 changes: 21 additions & 5 deletions Si_AdminMod/Si_AdminMod.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Silica Admin Mod
Copyright (C) 2024 by databomb
Copyright (C) 2023-2024 by databomb
* Description *
Provides basic admin mod system to allow additional admins beyond
Expand Down Expand Up @@ -46,12 +46,14 @@ public class SiAdminMod : MelonMod

public static List<Admin> AdminList = null!;
public static List<AdminCommand> AdminCommands = null!;
public static List<PlayerCommand> PlayerCommands = null!;

public override void OnInitializeMelon()
{
try
{
AdminCommands = new List<AdminCommand>();
PlayerCommands = new List<PlayerCommand>();
AdminList = AdminFile.Initialize();

_modCategory ??= MelonPreferences.CreateCategory("Silica");
Expand Down Expand Up @@ -79,12 +81,26 @@ public override void OnInitializeMelon()

public static void RegisterAdminCommand(String adminCommand, HelperMethods.CommandCallback adminCallback, Power adminPower)
{
AdminCommand thisCommand = new AdminCommand();
thisCommand.AdminCommandText = adminCommand;
thisCommand.AdminCallback = adminCallback;
thisCommand.AdminPower = adminPower;
AdminCommand thisCommand = new AdminCommand
{
AdminCommandText = adminCommand,
AdminCallback = adminCallback,
AdminPower = adminPower
};

AdminCommands.Add(thisCommand);
}

public static void RegisterPlayerCommand(String playerCommand, HelperMethods.CommandCallback playerCallback, bool hideFromChat)
{
PlayerCommand thisCommand = new PlayerCommand
{
CommandName = playerCommand,
PlayerCommandCallback = playerCallback,
HideChatMessage = hideFromChat
};

PlayerCommands.Add(thisCommand);
}
}
}
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion Si_ChatSilence/obj/project.nuget.cache
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"expectedPackageFiles": [
"C:\\Users\\veronica\\.nuget\\packages\\lavagang.melonloader\\0.6.1\\lavagang.melonloader.0.6.1.nupkg.sha512",
"C:\\Users\\veronica\\.nuget\\packages\\newtonsoft.json\\13.0.3\\newtonsoft.json.13.0.3.nupkg.sha512",
"C:\\Users\\veronica\\.nuget\\packages\\silica-melonloader-adminmod\\2.0.642\\silica-melonloader-adminmod.2.0.642.nupkg.sha512",
"C:\\Users\\veronica\\.nuget\\packages\\silica-melonloader-adminmod\\2.0.663\\silica-melonloader-adminmod.2.0.663.nupkg.sha512",
"C:\\Users\\veronica\\.nuget\\packages\\silica-melonloader-dependencies\\0.8.19\\silica-melonloader-dependencies.0.8.19.nupkg.sha512"
],
"logs": []
Expand Down

0 comments on commit 5340701

Please sign in to comment.