public static void RegisterCommands(RoR2.Console self) { var types = typeof(CommandHelper).Assembly.GetTypes(); var catalog = self.GetFieldValue <IDictionary>("concommandCatalog"); foreach (var methodInfo in types.SelectMany(x => x.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))) { var customAttributes = methodInfo.GetCustomAttributes(false); foreach (var attribute in customAttributes.OfType <ConCommandAttribute>()) { var conCommand = Reflection.GetNestedType <RoR2.Console>("ConCommand").Instantiate(); if (!DropInMultiplayer.SpawnAsEnabled.Value && attribute.commandName.Equals("spawn_as", StringComparison.CurrentCultureIgnoreCase)) { return; } conCommand.SetFieldValue("flags", attribute.flags); conCommand.SetFieldValue("helpText", attribute.helpText); conCommand.SetFieldValue("action", (RoR2.Console.ConCommandDelegate)Delegate.CreateDelegate(typeof(RoR2.Console.ConCommandDelegate), methodInfo)); catalog[attribute.commandName.ToLower()] = conCommand; } } }
public static void RegisterCommands(RoR2.Console self) { var types = Assembly.GetCallingAssembly()?.GetTypes(); if (types == null) { return; } var catalog = self.GetFieldValue <IDictionary>("concommandCatalog"); const BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; var methods = types.SelectMany(x => x.GetMethods(flags).Where(m => m.GetCustomAttribute <ConCommandAttribute>() != null)); foreach (var methodInfo in methods) { var attributes = methodInfo.GetCustomAttributes <ConCommandAttribute>(); foreach (var attribute in attributes) { var conCommand = Reflection.GetNestedType <RoR2.Console>("ConCommand").Instantiate(); conCommand.SetFieldValue("flags", attribute.flags); conCommand.SetFieldValue("helpText", attribute.helpText); conCommand.SetFieldValue("action", (RoR2.Console.ConCommandDelegate)Delegate.CreateDelegate(typeof(RoR2.Console.ConCommandDelegate), methodInfo)); catalog[attribute.commandName.ToLower()] = conCommand; } } }
private static void RegisterCommands(Assembly assembly) { /* * This code belongs to Wildbook. * https://github.com/wildbook/R2Mods/blob/develop/Utilities/CommandHelper.cs * Credit goes to Wildbook. */ var types = assembly?.GetTypes(); if (types == null) { return; } var catalog = console.GetFieldValue <IDictionary>("concommandCatalog"); const BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; var methods = types.SelectMany(x => x.GetMethods(flags).Where(m => m.GetCustomAttribute <ConCommandAttribute>() != null)); foreach (var methodInfo in methods) { var attributes = methodInfo.GetCustomAttributes <ConCommandAttribute>(); foreach (var attribute in attributes) { var conCommand = Reflection.GetNestedType <RoR2.Console>("ConCommand").Instantiate(); conCommand.SetFieldValue("flags", attribute.flags); conCommand.SetFieldValue("helpText", attribute.helpText); conCommand.SetFieldValue("action", (RoR2.Console.ConCommandDelegate)Delegate.CreateDelegate(typeof(RoR2.Console.ConCommandDelegate), methodInfo)); catalog[attribute.commandName.ToLower()] = conCommand; } } }
public static void RegisterCommands(RoR2.Console self) { var types = typeof(CommandHelper).Assembly.GetTypes(); var catalog = self.GetFieldValue <IDictionary>("concommandCatalog"); foreach (var methodInfo in types.SelectMany(x => x.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))) { var customAttributes = methodInfo.GetCustomAttributes(false); foreach (var attribute in customAttributes.OfType <ConCommandAttribute>()) { var conCommand = Reflection.GetNestedType <RoR2.Console>("ConCommand").Instantiate(); conCommand.SetFieldValue("flags", attribute.flags); conCommand.SetFieldValue("helpText", attribute.helpText); conCommand.SetFieldValue("action", (RoR2.Console.ConCommandDelegate)Delegate.CreateDelegate(typeof(RoR2.Console.ConCommandDelegate), methodInfo)); catalog[attribute.commandName.ToLower()] = conCommand; } } }