Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the possibility for mods to register convars #68

Merged
merged 3 commits into from
Oct 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions R2API/Utils/CommandHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
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 {
/*
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;
Expand All @@ -38,5 +39,37 @@ 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.GetName().Name);
return;
}

List<BaseConVar> customVars = new List<BaseConVar>();
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<CompilerGeneratedAttribute>(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);
}
}
}
}