|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- using System;
- using System.Reflection;
- using System.Text;
- using GameNetworkLayer.Shared;
- using HarmonyLib;
- using NetworkFramework.Shared;
-
- namespace CLre_server.API.Tools
- {
- public class NetServerSender
- {
- private struct DummyNetDataStruct : ISerializedNetData
- {
- public byte[] Serialize()
- {
- return new byte[0];
- }
-
- public void Deserialize(byte[] data)
- {
- }
- }
-
- private static readonly MethodInfo _genericSendMessage = AccessTools.Method("GameNetworkLayer.Server.NetMessageServerSender:SendMessage");
-
- private static readonly MethodInfo _genericGetSendMessageMethod =
- AccessTools.Method(typeof(NetServerSender), "GetSendMessageMethod",parameters: new Type[0]);/*
- /*((Func<MethodInfo>) GetSendMessageMethod<DummyNetDataStruct>).Method
- .GetBaseDefinition()
- .GetGenericMethodDefinition();*/
-
- private static readonly MethodInfo _genericLog =
- AccessTools.Method(typeof(NetServerSender), "Log");/*
- ((Action<NetworkDispatcherCode, DummyNetDataStruct>) Log<DummyNetDataStruct>).Method
- .GetBaseDefinition()
- .GetGenericMethodDefinition();*/
-
- private static readonly MethodInfo _genericGetLogMethod =
- AccessTools.Method(typeof(NetServerSender), "GetLogMethod", new Type[0]);/*
- ((Func<MethodInfo>) GetLogMethod<DummyNetDataStruct>).Method
- .GetBaseDefinition()
- .GetGenericMethodDefinition();*/
-
- public static MethodInfo GetSendMessageMethod(Type t)
- {
- return (MethodInfo) _genericGetSendMessageMethod.MakeGenericMethod(t)
- .Invoke(null, new object[0]);
- }
-
- public static MethodInfo GetSendMessageMethod<T>() where T : struct, ISerializedNetData
- {
- return _genericSendMessage.MakeGenericMethod(typeof(T));
- }
-
- public static MethodInfo DebugSendMessage<T>(Harmony instance = null, MethodInfo before = null, MethodInfo after = null, MethodInfo transpiler = null, MethodInfo finalizer = null) where T : struct, ISerializedNetData
- {
- return DebugSendMessage(typeof(T), instance, before, after, transpiler, finalizer);
- }
-
- public static MethodInfo DebugSendMessage(Type generic, Harmony instance = null, MethodInfo before = null, MethodInfo after = null, MethodInfo transpiler = null, MethodInfo finalizer = null)
- {
- return DebugSendMessage(
- generic, instance,
- before == null ? null : new HarmonyMethod(before),
- after == null ? null : new HarmonyMethod(after),
- transpiler == null ? null : new HarmonyMethod(transpiler),
- finalizer == null ? null : new HarmonyMethod(finalizer));
- }
-
- public static MethodInfo DebugSendMessage<T>(Harmony instance = null, HarmonyMethod before = null, HarmonyMethod after = null, HarmonyMethod transpiler = null, HarmonyMethod finalizer = null) where T : struct, ISerializedNetData
- {
- return DebugSendMessage(typeof(T), instance, before, after, transpiler, finalizer);
- }
-
- public static MethodInfo DebugSendMessage(Type generic, Harmony instance = null, HarmonyMethod before = null, HarmonyMethod after = null, HarmonyMethod transpiler = null, HarmonyMethod finalizer = null)
- {
- if (instance == null) instance = CLre.harmonyInstance;
- MethodInfo target = GetSendMessageMethod(generic);
- return instance.Patch(target,
- before,
- after,
- transpiler,
- finalizer);
- }
-
- public static MethodInfo GetLogMethod(Type t)
- {
- return (MethodInfo) _genericGetLogMethod.MakeGenericMethod(t)
- .Invoke(null, new object[0]);
- }
-
- public static MethodInfo GetLogMethod<T>() where T : struct, ISerializedNetData
- {
- return _genericLog.MakeGenericMethod(typeof(T));
- }
-
- private static void Log<T>(NetworkDispatcherCode code, ref T data) where T : struct, ISerializedNetData
- {
- //Utility.Logging.Log($"Sending ISerializedNetData {data.GetType().FullName} (code: {code.ToString()})");
- Traverse d = Traverse.Create(data);
- string codeName = (short) code > 217 ? "CUSTOM" : code.ToString();
- StringBuilder sb = new StringBuilder($"Sending ISerializedNetData {data.GetType().FullName} (code: {codeName} {(short)code})");
- foreach (string fieldName in d.Fields())
- {
- Traverse field = d.Field(fieldName);
- sb.Append("\n");
- sb.Append("\"");
- int start = fieldName.IndexOf('<');
- int len = fieldName.LastIndexOf('>');
- if (start != -1 && len > 0)
- {
- sb.Append(fieldName.Substring(start+1, len-1));
- }
- else
- {
- sb.Append(fieldName);
- }
- sb.Append("\": ");
- sb.Append(field.GetValue());
- }
- Utility.Logging.Log(sb.ToString());
- }
- }
- }
|