internal static void DoDetours() { // provide some info Verse.Log.Message("FluffyLib :: Detours :: Scanning mod assemblies for detour requests..."); // loop over all methods and properties in all mod's assemblies foreach (ModContentPack mod in LoadedModManager.RunningMods) { #if DEBUG_SPAMMY_DETOURS Verse.Log.Message(mod.Name); #endif foreach (Assembly assembly in mod.assemblies.loadedAssemblies) { #if DEBUG_SPAMMY_DETOURS Verse.Log.Message("\t" + assembly.FullName); #endif foreach (Type type in assembly.GetTypes()) { #if DEBUG_SPAMMY_DETOURS Verse.Log.Message("\t\t" + type.FullName); #endif foreach (MethodInfo methodInfo in type.GetMethods(AllBindingFlags)) { #if DEBUG_SPAMMY_DETOURS Verse.Log.Message("\t\t\t" + methodInfo.Name); #endif if (methodInfo.HasAttribute <DetourMethodAttribute>()) { #if DEBUG_SPAMMY_DETOURS Verse.Log.Message("\t\t\t\t" + "DING!"); #endif // if attribute is defined, do the detour DetourMethodAttribute detourAttribute = methodInfo.GetCustomAttributes(typeof(DetourMethodAttribute), false).First() as DetourMethodAttribute; HandleDetour(detourAttribute, methodInfo); } } foreach (PropertyInfo propertyInfo in type.GetProperties((AllBindingFlags))) { #if DEBUG_SPAMMY_DETOURS Verse.Log.Message("\t\t\t" + propertyInfo.Name); #endif if (propertyInfo.HasAttribute <DetourPropertyAttribute>()) { #if DEBUG_SPAMMY_DETOURS Verse.Log.Message("\t\t\t\t" + "DING!"); #endif // if attribute is defined, do the detour DetourPropertyAttribute detourAttribute = propertyInfo.GetCustomAttributes(typeof(DetourPropertyAttribute), false).First() as DetourPropertyAttribute; HandleDetour(detourAttribute, propertyInfo); } } } } } #if DEBUG DetourTest.RunTests(); #endif }
private static void HandleDetour(DetourPropertyAttribute sourceAttribute, PropertyInfo targetInfo) { // first, lets get the source propertyInfo - there's no ambiguity here. PropertyInfo sourceInfo = sourceAttribute.sourcePropertyInfo; // do our detours // if getter was flagged (so Getter | Both ) if ((sourceAttribute.detourProperty & DetourProperty.Getter) == DetourProperty.Getter) { TryDetourFromTo(sourceInfo.GetGetMethod(true), targetInfo.GetGetMethod(true)); } // if setter was flagged if ((sourceAttribute.detourProperty & DetourProperty.Setter) == DetourProperty.Setter) { TryDetourFromTo(sourceInfo.GetSetMethod(true), targetInfo.GetSetMethod(true)); } }