private static bool TryRegisterConsoleVariables(RoR2.Console self) { const string registerConVarMethod = "RegisterConVarInternal"; var registerConVarInternal = typeof(RoR2.Console).GetMethods(BindingFlags.Instance | BindingFlags.NonPublic) .FirstOrDefault(m => m.Name == registerConVarMethod); if (registerConVarInternal == null) { Chat.AddMessage($"Could not find {registerConVarMethod}. Unloading RoR2Pro."); return(false); } else { var consoleVariables = Reflection.GetImplementingTypes <RoR2Pro, BaseConVar>().ToList(); Chat.AddMessage($"Registering console variables [{string.Join(", ", consoleVariables.Select(v => v.Name))}]"); foreach (var conVar in consoleVariables) { var instance = (BaseConVar)Activator.CreateInstance(conVar); registerConVarInternal.Invoke(self, new object[] { instance }); } Chat.AddMessage($"Registering console variables [{string.Join(", ", consoleVariables.Select(v => v.Name))}]"); } return(true); }
private bool TryRegisterConsoleCommands(RoR2.Console self) { const string commandCatalogField = "concommandCatalog"; var types = typeof(RoR2Pro).Assembly.GetTypes(); var catalog = self.GetFieldValue <IDictionary>(commandCatalogField); if (catalog == null) { Chat.AddMessage($"Could not find command catalog {commandCatalogField}. Unloading RoR2Pro."); return(false); } 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 = R2API.Utils.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; Chat.AddMessage($"Registered command {attribute.commandName}"); } } return(true); }