Unofficial CardLife revival project, pronounced like "celery"
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
4.9KB

  1. using System;
  2. using System.Reflection;
  3. using System.Text;
  4. using GameNetworkLayer.Shared;
  5. using HarmonyLib;
  6. using NetworkFramework.Shared;
  7. namespace CLre_server.API.Tools
  8. {
  9. public class NetServerSender
  10. {
  11. private struct DummyNetDataStruct : ISerializedNetData
  12. {
  13. public byte[] Serialize()
  14. {
  15. return new byte[0];
  16. }
  17. public void Deserialize(byte[] data)
  18. {
  19. }
  20. }
  21. private static readonly MethodInfo _genericSendMessage = AccessTools.Method("GameNetworkLayer.Server.NetMessageServerSender:SendMessage");
  22. private static readonly MethodInfo _genericGetSendMessageMethod =
  23. AccessTools.Method(typeof(NetServerSender), "GetSendMessageMethod",parameters: new Type[0]);/*
  24. /*((Func<MethodInfo>) GetSendMessageMethod<DummyNetDataStruct>).Method
  25. .GetBaseDefinition()
  26. .GetGenericMethodDefinition();*/
  27. private static readonly MethodInfo _genericLog =
  28. AccessTools.Method(typeof(NetServerSender), "Log");/*
  29. ((Action<NetworkDispatcherCode, DummyNetDataStruct>) Log<DummyNetDataStruct>).Method
  30. .GetBaseDefinition()
  31. .GetGenericMethodDefinition();*/
  32. private static readonly MethodInfo _genericGetLogMethod =
  33. AccessTools.Method(typeof(NetServerSender), "GetLogMethod", new Type[0]);/*
  34. ((Func<MethodInfo>) GetLogMethod<DummyNetDataStruct>).Method
  35. .GetBaseDefinition()
  36. .GetGenericMethodDefinition();*/
  37. public static MethodInfo GetSendMessageMethod(Type t)
  38. {
  39. return (MethodInfo) _genericGetSendMessageMethod.MakeGenericMethod(t)
  40. .Invoke(null, new object[0]);
  41. }
  42. public static MethodInfo GetSendMessageMethod<T>() where T : struct, ISerializedNetData
  43. {
  44. return _genericSendMessage.MakeGenericMethod(typeof(T));
  45. }
  46. public static MethodInfo DebugSendMessage<T>(Harmony instance = null, MethodInfo before = null, MethodInfo after = null, MethodInfo transpiler = null, MethodInfo finalizer = null) where T : struct, ISerializedNetData
  47. {
  48. return DebugSendMessage(typeof(T), instance, before, after, transpiler, finalizer);
  49. }
  50. public static MethodInfo DebugSendMessage(Type generic, Harmony instance = null, MethodInfo before = null, MethodInfo after = null, MethodInfo transpiler = null, MethodInfo finalizer = null)
  51. {
  52. return DebugSendMessage(
  53. generic, instance,
  54. before == null ? null : new HarmonyMethod(before),
  55. after == null ? null : new HarmonyMethod(after),
  56. transpiler == null ? null : new HarmonyMethod(transpiler),
  57. finalizer == null ? null : new HarmonyMethod(finalizer));
  58. }
  59. public static MethodInfo DebugSendMessage<T>(Harmony instance = null, HarmonyMethod before = null, HarmonyMethod after = null, HarmonyMethod transpiler = null, HarmonyMethod finalizer = null) where T : struct, ISerializedNetData
  60. {
  61. return DebugSendMessage(typeof(T), instance, before, after, transpiler, finalizer);
  62. }
  63. public static MethodInfo DebugSendMessage(Type generic, Harmony instance = null, HarmonyMethod before = null, HarmonyMethod after = null, HarmonyMethod transpiler = null, HarmonyMethod finalizer = null)
  64. {
  65. if (instance == null) instance = CLre.harmonyInstance;
  66. MethodInfo target = GetSendMessageMethod(generic);
  67. return instance.Patch(target,
  68. before,
  69. after,
  70. transpiler,
  71. finalizer);
  72. }
  73. public static MethodInfo GetLogMethod(Type t)
  74. {
  75. return (MethodInfo) _genericGetLogMethod.MakeGenericMethod(t)
  76. .Invoke(null, new object[0]);
  77. }
  78. public static MethodInfo GetLogMethod<T>() where T : struct, ISerializedNetData
  79. {
  80. return _genericLog.MakeGenericMethod(typeof(T));
  81. }
  82. private static void Log<T>(NetworkDispatcherCode code, ref T data) where T : struct, ISerializedNetData
  83. {
  84. //Utility.Logging.Log($"Sending ISerializedNetData {data.GetType().FullName} (code: {code.ToString()})");
  85. Traverse d = Traverse.Create(data);
  86. StringBuilder sb = new StringBuilder($"Sending ISerializedNetData {data.GetType().FullName} (code: {code.ToString()})");
  87. foreach (string fieldName in d.Fields())
  88. {
  89. Traverse field = d.Field(fieldName);
  90. sb.Append("\n");
  91. sb.Append("\"");
  92. sb.Append(fieldName.Substring(fieldName.IndexOf('<')+1, fieldName.LastIndexOf('>')-1));
  93. sb.Append("\": ");
  94. sb.Append(field.GetValue());
  95. }
  96. Utility.Logging.Log(sb.ToString());
  97. }
  98. }
  99. }