management commands

This commit is contained in:
Spirtix 2024-04-07 15:42:28 +02:00
parent 146b366e44
commit 854d1c0b0a
7 changed files with 90 additions and 9 deletions

View File

@ -0,0 +1,14 @@
using sodoffmmo.Management;
namespace sodoffmmo.Attributes;
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public class ManagementCommandAttribute : Attribute {
public string Name { get; set; }
public Role Role { get; set; }
public ManagementCommandAttribute(string name, Role role) {
Name = name;
Role = role;
}
}

View File

@ -1,21 +1,25 @@
using sodoffmmo.Attributes; using sodoffmmo.Attributes;
using sodoffmmo.Core; using sodoffmmo.Core;
using sodoffmmo.Data; using sodoffmmo.Data;
using sodoffmmo.Management;
namespace sodoffmmo.CommandHandlers; namespace sodoffmmo.CommandHandlers;
[ExtensionCommandHandler("SCM")] [ExtensionCommandHandler("SCM")]
class ChatMessageHandler : CommandHandler { class ChatMessageHandler : CommandHandler {
public override Task Handle(Client client, NetworkObject receivedObject) { public override Task Handle(Client client, NetworkObject receivedObject) {
string message = receivedObject.Get<NetworkObject>("p").Get<string>("chm");
if (ManagementCommandProcessor.ProcessCommand(message, client))
return Task.CompletedTask;
if (!Configuration.ServerConfiguration.EnableChat) { if (!Configuration.ServerConfiguration.EnableChat) {
ChatDisabled(client, receivedObject); ChatDisabled(client);
} else { } else {
Chat(client, receivedObject); Chat(client, message);
} }
return Task.CompletedTask; return Task.CompletedTask;
} }
public void ChatDisabled(Client client, NetworkObject receivedObject) { public void ChatDisabled(Client client) {
NetworkObject cmd = new(); NetworkObject cmd = new();
NetworkObject data = new(); NetworkObject data = new();
data.Add("arr", new string[] { "CMR", "-1", "-1", "1", "Unfortunately, chat has been disabled by server administrators", "", "1", "Server" }); data.Add("arr", new string[] { "CMR", "-1", "-1", "1", "Unfortunately, chat has been disabled by server administrators", "", "1", "Server" });
@ -26,10 +30,9 @@ class ChatMessageHandler : CommandHandler {
client.Send(packet); client.Send(packet);
} }
public void Chat(Client client, NetworkObject receivedObject) { public void Chat(Client client, string message) {
if (client.PlayerData.DiplayName == "") if (client.PlayerData.DiplayName == "")
return; return;
string message = receivedObject.Get<NetworkObject>("p").Get<string>("chm");
NetworkObject cmd = new(); NetworkObject cmd = new();
NetworkObject data = new(); NetworkObject data = new();

View File

@ -33,6 +33,9 @@ public class PlayerData {
// animation bitfield (animations used by avatar, e.g. mounted, swim, ...) // animation bitfield (animations used by avatar, e.g. mounted, swim, ...)
public int Mbf { get; set; } public int Mbf { get; set; }
public string DiplayName { get; set; } = "";
public Role Role { get; set; } = Role.User;
public static readonly string[] SupportedVariables = { public static readonly string[] SupportedVariables = {
"A", // avatar data "A", // avatar data
"FP", // raised pet data "FP", // raised pet data
@ -94,9 +97,6 @@ public class PlayerData {
IsValid = true; IsValid = true;
} }
public string DiplayName { get; set; } = "";
public Role Role { get; set; } = Role.User;
public NetworkArray GetNetworkData(int clientID, out NetworkArray paramArr) { public NetworkArray GetNetworkData(int clientID, out NetworkArray paramArr) {
NetworkArray arr = new(); NetworkArray arr = new();
arr.Add(clientID); arr.Add(clientID);

View File

@ -16,5 +16,5 @@ public class AuthenticationInfo {
[Serializable] [Serializable]
public enum Role { public enum Role {
User, Admin, Moderator User = 0, Moderator = 1, Admin = 2
} }

View File

@ -0,0 +1,11 @@
using sodoffmmo.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace sodoffmmo.Management;
public interface IManagementCommand {
public void Handle(Client client, string[] arguments);
}

View File

@ -0,0 +1,51 @@
using sodoffmmo.Attributes;
using sodoffmmo.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace sodoffmmo.Management;
public class ManagementCommandProcessor {
static Dictionary<Tuple<string, Role>, Type> commands = new();
static bool initialized = false;
public static void Initialize() {
if (!Configuration.ServerConfiguration.Authentication)
return;
commands.Clear();
var handlerTypes = Assembly.GetExecutingAssembly().GetTypes()
.Where(type => typeof(IManagementCommand).IsAssignableFrom(type))
.Where(type => type.GetCustomAttribute<ManagementCommandAttribute>() != null);
foreach (var handlerType in handlerTypes) {
ManagementCommandAttribute attrib = (handlerType.GetCustomAttribute(typeof(ManagementCommandAttribute)) as ManagementCommandAttribute)!;
commands[new Tuple<string, Role>(attrib.Name, attrib.Role)] = handlerType;
}
initialized = true;
}
public static bool ProcessCommand(string message, Client client) {
if (!Configuration.ServerConfiguration.Authentication || !initialized)
return false;
if (!message.StartsWith("//") && message.Length < 3)
return false;
string[] parts = message.Split(' ');
string commandName = parts[0].Substring(2);
string[] arguments = parts.Skip(1).ToArray();
for (int i = (int)client.PlayerData.Role; i >= 0; --i) {
Role currentRole = (Role)i;
if (commands.TryGetValue(new Tuple<string, Role>(commandName, currentRole), out Type? commandType)) {
IManagementCommand command = (IManagementCommand)Activator.CreateInstance(commandType)!;
command.Handle(client, arguments);
return true;
}
}
return false;
}
}

View File

@ -1,5 +1,6 @@
using sodoffmmo.Core; using sodoffmmo.Core;
using sodoffmmo.Data; using sodoffmmo.Data;
using sodoffmmo.Management;
using System; using System;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
@ -20,6 +21,7 @@ public class Server {
public async Task Run() { public async Task Run() {
moduleManager.RegisterModules(); moduleManager.RegisterModules();
ManagementCommandProcessor.Initialize();
using Socket listener = new(ipAddress.AddressFamily, using Socket listener = new(ipAddress.AddressFamily,
SocketType.Stream, SocketType.Stream,
ProtocolType.Tcp); ProtocolType.Tcp);