From 810b678c1e4ad16d1385fa6fa4ef5a37f27704cf Mon Sep 17 00:00:00 2001 From: harbingerofme Date: Sat, 19 Oct 2019 12:54:23 +0200 Subject: [PATCH 1/3] Add RegisterConvars --- R2API/Utils/CommandHelper.cs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/R2API/Utils/CommandHelper.cs b/R2API/Utils/CommandHelper.cs index be15cbe7..532a924d 100644 --- a/R2API/Utils/CommandHelper.cs +++ b/R2API/Utils/CommandHelper.cs @@ -3,8 +3,10 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; -using System.Text; +using System.Runtime.CompilerServices; using RoR2; +using RoR2.ConVar; +using UnityEngine; namespace R2API.Utils { /* @@ -38,5 +40,36 @@ public static void RegisterCommands(RoR2.Console self) { } } } + + public static void RegisterConVars(RoR2.Console self) { + var assembly = Assembly.GetCallingAssembly(); + if(assembly==null) { + return; + } + + if (self.allConVars == null) { + Debug.LogErrorFormat("Can't register the convars from mod {0} before the game does. Try doing it after initConvars!",assembly.FullName); + } + + List customVars = new List(); + foreach (Type type in assembly.GetTypes()) { + foreach (FieldInfo field in type.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)) { + if (field.FieldType.IsSubclassOf(typeof(BaseConVar))) { + if (field.IsStatic) { + self.RegisterConVarInternal((BaseConVar)field.GetValue(null)); + customVars.Add((BaseConVar) field.GetValue(null)); + } + else if (CustomAttributeExtensions.GetCustomAttribute(type) == null) + Debug.LogErrorFormat("ConVar defined as {0} in {1}. {2} could not be registered. ConVars must be static fields.", type.Name, assembly.FullName, field.Name); + } + } + } + foreach (BaseConVar baseConVar in customVars) { + if ((baseConVar.flags & ConVarFlags.Engine) != ConVarFlags.None) + baseConVar.defaultValue = baseConVar.GetString(); + else if (baseConVar.defaultValue != null) + baseConVar.SetString(baseConVar.defaultValue); + } + } } } From 811264dce9c977da1bce47d707132c53ab5d985b Mon Sep 17 00:00:00 2001 From: harbingerofme Date: Sat, 19 Oct 2019 13:15:04 +0200 Subject: [PATCH 2/3] Bettter errorhandling --- R2API/Utils/CommandHelper.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R2API/Utils/CommandHelper.cs b/R2API/Utils/CommandHelper.cs index 532a924d..3b02d809 100644 --- a/R2API/Utils/CommandHelper.cs +++ b/R2API/Utils/CommandHelper.cs @@ -48,7 +48,8 @@ public static void RegisterConVars(RoR2.Console self) { } if (self.allConVars == null) { - Debug.LogErrorFormat("Can't register the convars from mod {0} before the game does. Try doing it after initConvars!",assembly.FullName); + Debug.LogErrorFormat("Can't register the convars from mod {0} before the game does. Try doing it after initConvars!",assembly.GetName().Name); + return; } List customVars = new List(); From 7c637d64697755416c2e47df943754d6c329f43a Mon Sep 17 00:00:00 2001 From: harbingerofme Date: Sat, 19 Oct 2019 13:15:39 +0200 Subject: [PATCH 3/3] Attribution Wildbook didn't write the method provided by me. --- R2API/Utils/CommandHelper.cs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/R2API/Utils/CommandHelper.cs b/R2API/Utils/CommandHelper.cs index 3b02d809..4eef19e8 100644 --- a/R2API/Utils/CommandHelper.cs +++ b/R2API/Utils/CommandHelper.cs @@ -9,14 +9,13 @@ using UnityEngine; namespace R2API.Utils { - /* - This code belongs to Wildbook. - https://github.com/wildbook/R2Mods/blob/develop/Utilities/CommandHelper.cs - Credit goes to Wildbook. - */ - public class CommandHelper { public static void RegisterCommands(RoR2.Console self) { + /* + This code belongs to Wildbook. + https://github.com/wildbook/R2Mods/blob/develop/Utilities/CommandHelper.cs + Credit goes to Wildbook. + */ var types = Assembly.GetCallingAssembly()?.GetTypes(); if (types == null) { return;