mirror of
https://github.com/NuclearPowered/Reactor.git
synced 2026-10-20 01:46:21 +00:00
1 parent
5cc08363c7
commit
b7971d36a8
66 files changed
+652
-618
No files matched your search
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using BepInEx.Unity.IL2CPP;
|
||||
using HarmonyLib;
|
||||
using Reactor.Networking.Serialization;
|
||||
|
||||
namespace Reactor.Networking.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class MessageConverterAttribute : Attribute
|
||||
{
|
||||
private static readonly HashSet<Assembly> _registeredAssemblies = new();
|
||||
|
||||
public static void Register(Assembly assembly)
|
||||
{
|
||||
if (_registeredAssemblies.Contains(assembly)) return;
|
||||
_registeredAssemblies.Add(assembly);
|
||||
|
||||
foreach (var type in assembly.GetTypes())
|
||||
{
|
||||
var attribute = type.GetCustomAttribute<MessageConverterAttribute>();
|
||||
|
||||
if (attribute != null)
|
||||
{
|
||||
if (!type.IsSubclassOf(typeof(UnsafeMessageConverter)))
|
||||
{
|
||||
throw new InvalidOperationException($"Type {type.FullDescription()} has {nameof(MessageConverterAttribute)} but doesn't extend {nameof(UnsafeMessageConverter)}.");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var messageConverter = (UnsafeMessageConverter) Activator.CreateInstance(type)!;
|
||||
MessageSerializer.Register(messageConverter);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Warning($"Failed to register {type.FullDescription()}: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static void Initialize()
|
||||
{
|
||||
IL2CPPChainloader.Instance.PluginLoad += (_, assembly, _) => Register(assembly);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user