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.

124 lines
5.2KB

  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. string codeName = (short) code > 217 ? "CUSTOM" : code.ToString();
  87. StringBuilder sb = new StringBuilder($"Sending ISerializedNetData {data.GetType().FullName} (code: {codeName} {(short)code})");
  88. foreach (string fieldName in d.Fields())
  89. {
  90. Traverse field = d.Field(fieldName);
  91. sb.Append("\n");
  92. sb.Append("\"");
  93. int start = fieldName.IndexOf('<');
  94. int len = fieldName.LastIndexOf('>');
  95. if (start != -1 && len > 0)
  96. {
  97. sb.Append(fieldName.Substring(start+1, len-1));
  98. }
  99. else
  100. {
  101. sb.Append(fieldName);
  102. }
  103. sb.Append("\": ");
  104. sb.Append(field.GetValue());
  105. }
  106. Utility.Logging.Log(sb.ToString());
  107. }
  108. }
  109. }