mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-20 01:46:15 +00:00
it works?
This commit is contained in:
1 parent
9485099ab9
commit
ce81d8c1ff
3 files changed
+86
-5
No files matched your search
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
|
||||
namespace MiraAPI.Modifiers;
|
||||
|
||||
/// <summary>
|
||||
/// Modifier data for networking.
|
||||
/// </summary>
|
||||
/// <param name="type">The Type of the modifier.</param>
|
||||
/// <param name="args">Parameters for constructor.</param>
|
||||
public readonly struct ModifierData(Type type, object[] args)
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the type of the modifier.
|
||||
/// </summary>
|
||||
public Type Type { get; } = type;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the parameters for the constructor.
|
||||
/// </summary>
|
||||
public object[] Args { get; } = args;
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using MiraAPI.Networking;
|
||||
using Reactor.Networking.Attributes;
|
||||
using Reactor.Networking.Rpc;
|
||||
using Reactor.Utilities;
|
||||
|
||||
namespace MiraAPI.Modifiers;
|
||||
@@ -104,7 +105,7 @@ public static class ModifierExtensions
|
||||
/// </summary>
|
||||
/// <param name="target">The player to add the modifier to.</param>
|
||||
/// <param name="modifierId">The modifier ID.</param>
|
||||
[MethodRpc((uint)MiraRpc.AddModifier)]
|
||||
/// <param name="args">The arguments to initialize the modifier constructor with.</param>
|
||||
public static void RpcAddModifier(this PlayerControl target, uint modifierId, params object[] args)
|
||||
{
|
||||
var type = ModifierManager.GetModifierType(modifierId);
|
||||
@@ -114,8 +115,7 @@ public static class ModifierExtensions
|
||||
return;
|
||||
}
|
||||
|
||||
var modifier = ModifierFactory.CreateInstance(type, args);
|
||||
target.GetModifierComponent()?.AddModifier(modifier);
|
||||
Rpc<AddModifierRpc>.Instance.Send(target, new ModifierData(type, args));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -133,7 +133,6 @@ public static class ModifierExtensions
|
||||
return;
|
||||
}
|
||||
|
||||
var modifier = ModifierFactory<T>.CreateInstance(args);
|
||||
player.GetModifierComponent()!.AddModifier(modifier);
|
||||
player.RpcAddModifier(id.Value, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Hazel;
|
||||
using MiraAPI.Modifiers;
|
||||
using Reactor.Networking.Attributes;
|
||||
using Reactor.Networking.Rpc;
|
||||
using Reactor.Networking.Serialization;
|
||||
|
||||
namespace MiraAPI.Networking;
|
||||
|
||||
/// <summary>
|
||||
/// Remote procedure call for adding a modifier.
|
||||
/// </summary>
|
||||
/// <param name="plugin">Mira plugin.</param>
|
||||
/// <param name="id">RPC ID.</param>
|
||||
[RegisterCustomRpc((uint)MiraRpc.AddModifier)]
|
||||
public class AddModifierRpc(MiraApiPlugin plugin, uint id) : PlayerCustomRpc<MiraApiPlugin, ModifierData>(plugin, id)
|
||||
{
|
||||
private static readonly Dictionary<Type, ParameterInfo[]> ParameterCache = [];
|
||||
|
||||
/// <inheritdoc />
|
||||
public override RpcLocalHandling LocalHandling => RpcLocalHandling.Before;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Write(MessageWriter writer, ModifierData data)
|
||||
{
|
||||
var modId = ModifierManager.GetModifierId(data.Type);
|
||||
writer.WritePacked((uint)modId!);
|
||||
MessageSerializer.Serialize(writer, data.Args);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override ModifierData Read(MessageReader reader)
|
||||
{
|
||||
var modId = reader.ReadPackedUInt32();
|
||||
var modifier = ModifierManager.GetModifierType(modId)!;
|
||||
|
||||
if (!ParameterCache.TryGetValue(modifier, out var paramTypes))
|
||||
{
|
||||
paramTypes = modifier.GetConstructors().OrderBy(x => x.GetParameters().Length).First().GetParameters();
|
||||
ParameterCache[modifier] = paramTypes;
|
||||
}
|
||||
|
||||
var objects = new object[paramTypes.Length];
|
||||
foreach (var paramType in paramTypes)
|
||||
{
|
||||
objects[paramType.Position] = reader.Deserialize(paramType.ParameterType);
|
||||
}
|
||||
|
||||
return new ModifierData(modifier, objects);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Handle(PlayerControl innerNetObject, ModifierData data)
|
||||
{
|
||||
var modifier = ModifierFactory.CreateInstance(data.Type, data.Args);
|
||||
innerNetObject.GetModifierComponent()?.AddModifier(modifier);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user