-
Notifications
You must be signed in to change notification settings - Fork 2
Extending Enums
MTG API v1.4.0 added the ETGModCompatibility class, which has a method named ExtendEnum.
To extend an enum, you can do one of the following:
You can manually extend any enum you want by calling ETGModCompatibility.ExtendEnum<YourEnumType>(YourPluginClass.GUID, "NameOfYourEnum")
You can either save the value returned or you can call ETGModCompatibility.ExtendEnum<YourEnumType>(YourPluginClass.GUID, "NameOfYourEnum") every time you want to get it (calling it with the same values after the first time will only return the saved result and will not add anything)
Firstly, you need to add this class to your project:
public class EnumExtensionAttribute : Attribute
{
public EnumExtensionAttribute(Type extensiontype)
{
type = extensiontype;
}
public Type type;
}Secondly, create a new class and name it {NameOfTheEnumYouWantToExtend}E.cs.
Create a public static NameOfTheEnumYouWantToExtend field for each new extended value you want.
Example of such class:
[EnumExtension(typeof(GungeonFlags))]
public static class GungeonFlagsE
{
public static GungeonFlags TEST_FLAG_1;
public static GungeonFlags TEST_FLAG_2;
public static GungeonFlags TEST_FLAG_3;
public static GungeonFlags TEST_FLAG_4;
}Then, add this code to your Plugin class's Awake() method:
var asm = Assembly.GetExecutingAssembly();
foreach(var type in asm.GetTypes())
{
var custom = type.GetCustomAttributes(false);
if (custom != null)
{
var extension = custom.OfType<EnumExtensionAttribute>().FirstOrDefault();
if(extension != null && extension.type != null && extension.type.IsEnum)
{
foreach(var f in type.GetFields(BindingFlags.Public | BindingFlags.Static))
{
f.SetValue(null, ETGModCompatibility.ExtendEnum(GUID, f.Name, extension.type));
}
}
}
}(Note: this code needs using System.Linq; and using System.Reflection;)
Then, when you want to use an extended enum value, you can do {NameOfTheEnumYouWantToExtend}E.ValueName