protected static void DoPostfix(object __instance, object __state, MeasurableObjectType type) { if (!ProfilerPlugin.IsProfiling) { return; } var sw = __state as Stopwatch; if (sw == null) { return; } long time = sw.ElapsedMilliseconds; sw.Reset(); MeasurableObject o = ProfilerPlugin.Instance.Assemblies[__instance.GetType().Assembly][__instance.GetType()].First(c => c.MeasureType == type); if (ProfilerPlugin.Instance != null && o.Measures.Count > ProfilerPlugin.Instance.Configuration.Instance.MaxFrameCount) { // we might do profiling during a reload, so we need to check if instance is null return; } o.Measures.Add(time); }
public sealed override void PatchObject(Type type, string target, params object[] args) { var targetMethod = type.GetMethod(target, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, CallingConventions.Any, args.Cast <Type>().ToArray(), null); MeasurableObject o = new MeasurableObject { Name = type.FullName, Type = type, MeasureType = MeasureType, Measures = new List <decimal>(), Method = targetMethod }; ProfilerPlugin.Instance.Assemblies[type.Assembly][type].Add(o); Logger.Log(o.MeasureType + " found: " + type.FullName + "." + targetMethod.Name); ProfilerPlugin.Instance.Harmony.Patch(targetMethod, Prefix, Postfix); }
private void PatchObject(object instance, Type type, string target) { Logger.Log("Patching event: " + target); var eventDelegate = (MulticastDelegate)type.GetField(target, BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic).GetValue(instance); if (eventDelegate == null) { Logger.LogWarning("eventDelegate null for " + target + ", will not be measured"); return; } foreach (var handler in eventDelegate.GetInvocationList()) { var method = handler.Method; if (method == null) { Logger.LogWarning("method null for: " + handler.GetType().Name); continue; } var dType = handler.Method.DeclaringType; if (dType == null) { Logger.LogWarning("dtype null for: " + method.Name); continue; } Logger.Log("Patching method: " + dType.FullName + "." + method.Name + " for event: " + target); MeasurableObject o = new MeasurableObject { Name = target, Type = dType, MeasureType = MeasureType, Measures = new List <decimal>(), Method = method }; var asm = dType.Assembly; if (!ProfilerPlugin.Instance.Assemblies.ContainsKey(asm)) { ProfilerPlugin.Instance.Assemblies.Add(asm, new Dictionary <Type, List <MeasurableObject> >()); } if (!ProfilerPlugin.Instance.Assemblies[asm].ContainsKey(dType)) { ProfilerPlugin.Instance.Assemblies[asm].Add(dType, new List <MeasurableObject>()); } ProfilerPlugin.Instance.Assemblies[asm][dType].Add(o); if (!ProfilerPlugin.Instance.Events.ContainsKey(target)) { ProfilerPlugin.Instance.Events.Add(target, new List <MeasurableObject>()); } ProfilerPlugin.Instance.Events[target].Add(o); ProfilerPlugin.Instance.Harmony.Patch(method, Prefix, Postfix); } }