public PatchEventAttribute(Type classToPatch, string methodName, PatchEventType eventType, int priority = 500) { EventType = eventType; ClassToPatch = classToPatch; MethodName = methodName; Priority = priority; }
/// <summary> /// Generic method to add an event to an eventhandler inside of a harmony patch class /// </summary> /// <typeparam name="T">Type of Harmony Patch class</typeparam> /// <param name="method">Method to add to eventhandler</param> /// <param name="eventType">BlockingPrefix,Prefix or Postfix</param> private static void AddHarmonyEvent <T>(MethodInfo method, PatchEventType eventType) where T : class { try { EventInfo evt; // Get eventhandler according to PatchEventType if (eventType == PatchEventType.BlockingPrefix) { evt = typeof(T).GetEvents().First(x => x.Name == nameof(ZNet_Awake_Patch.BlockingPrefixEvent)); } else if (eventType == PatchEventType.Prefix) { evt = typeof(T).GetEvents().First(x => x.Name == nameof(ZNet_Awake_Patch.PrefixEvent)); } else { evt = typeof(T).GetEvents().First(x => x.Name == nameof(ZNet_Awake_Patch.PostfixEvent)); } // Just in case if the HarmonyPatch class has no eventhandler if (evt == null) { Logger.LogError($"{eventType} Event could not be found for class {method.DeclaringType}"); } var eventHandlerType = evt.EventHandlerType; // Create delegate var @delegate = Delegate.CreateDelegate(eventHandlerType, method); // Add to event handler var addMethod = evt.GetAddMethod(); addMethod.Invoke(null, new object[] { @delegate }); } // Will throw error if method parameters are not in line with the eventhandler's delegate catch (Exception ex) { Logger.LogError(ex.Message + Environment.NewLine + ex.StackTrace); } }