-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EnumPatcher.cs
112 lines (95 loc) · 5.41 KB
/
EnumPatcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.Collections;
using System.Globalization;
using System.Linq;
using System.Text;
using UnityEngine;
using System.Reflection;
using HarmonyLib;
using RealEnumPatcher = EnumPatcher;
namespace SALT
{
using Registries;
// /// <summary>
// /// Allows adding values to any Enum
// /// </summary>
public static class EnumPatcher
{
internal static void RegisterAlternate(Type type, RealEnumPatcher.AlternateEnumRegister del) => RealEnumPatcher.RegisterAlternate(type, del);
internal static void RegisterAlternate<TEnum>(RealEnumPatcher.AlternateEnumRegister del) where TEnum : Enum => RealEnumPatcher.RegisterAlternate<TEnum>(del);
static EnumPatcher()
{
RealEnumPatcher.RegisterAlternate<Character>((x, y) => CharacterRegistry.CreateCharacterId(x, y));
RealEnumPatcher.RegisterAlternate<Level>((x, y) => LevelRegistry.CreateLevelId(x, y));
}
/// <summary>
/// Add a new enum value to the given <typeparamref name="TEnum"/> with the first free value
/// </summary>
/// <typeparam name="TEnum">Type of enum to add the value to</typeparam>
/// <param name="name">Name of the new enum value</param>
/// <returns>The new enum value</returns>
public static TEnum AddEnumValue<TEnum>(string name) where TEnum : Enum => (TEnum)AddEnumValue(typeof(TEnum), name);
/// <summary>
/// Add a new enum value to the given <paramref name="enumType"/> with the first free value
/// </summary>
/// <param name="enumType">Type of enum to add the value to</param>
/// <param name="name">Name of the new enum value</param>
/// <returns>The new enum value</returns>
public static object AddEnumValue(Type enumType, string name) => AddEnumValue(enumType, name);
/// <summary>
/// Add a new value to the given <typeparamref name="TEnum"/>
/// </summary>
/// <typeparam name="TEnum">Type of enum to add the value to</typeparam>
/// <param name="value">Value to add to the enum</param>
/// <param name="name">The name of the new value</param>
public static void AddEnumValue<TEnum>(object value, string name) where TEnum : Enum => AddEnumValue(typeof(TEnum), value, name);
/// <summary>
/// Add a new value to the given <paramref name="enumType"/>
/// </summary>
/// <param name="enumType">Enum to add the new value to</param>
/// <param name="value">Value to add to the enum</param>
/// <param name="name">The name of the new value</param>
public static void AddEnumValue(Type enumType, object value, string name)
{
var calling = Assembly.GetCallingAssembly();
if (calling != Assembly.GetExecutingAssembly() && ModLoader.GetModForAssembly(calling) != null) throw new Exception($"Patching {enumType} through EnumPatcher is not supported!");
RealEnumPatcher.AddEnumValue(enumType, value, name);
}
internal static void AddEnumValueInternal(Type enumType, object value, string name) => RealEnumPatcher.AddEnumValue(enumType, value, name, true);
internal static void AddPartialEnumValue<TEnum>(object value, string name) where TEnum : Enum => AddPartialEnumValue(typeof(TEnum), value, name);
internal static void AddPartialEnumValue(Type enumType, object value, string name) => RealEnumPatcher.AddPartialEnumValue(enumType, value, name);
internal static void AddEnumValueWithAlternatives<TEnum>(object value, string name) where TEnum : Enum => AddEnumValueWithAlternatives(typeof(TEnum), value, name);
internal static void AddEnumValueWithAlternatives(Type enumType, object value, string name)
{
if (RealEnumPatcher.AlternateEnumRegistries.TryGetValue(enumType, out var alternate)) alternate(value, name);
else AddEnumValue(enumType, value, name);
}
/// <summary>
/// Get first undefined value in an enum
/// </summary>
/// <typeparam name="TEnum"></typeparam>
/// <returns>The first undefined enum value</returns>
public static object GetFirstFreeValue<TEnum>() where TEnum : Enum => GetFirstFreeValue(typeof(TEnum));
/// <summary>
/// Get first undefined value in an enum
/// </summary>
/// <param name="enumType"></param>
/// <returns>The first undefined enum value</returns>
public static object GetFirstFreeValue(Type enumType) => RealEnumPatcher.GetFirstFreeValue(enumType);
/// <summary>
/// Get first undefined value in an enum
/// </summary>
/// <typeparam name="TEnum"></typeparam>
/// <returns>The first undefined enum value</returns>
internal static object GetFirstFreeValue<TEnum>(int skip) where TEnum : Enum => GetFirstFreeValue(typeof(TEnum), skip);
/// <summary>
/// Get first undefined value in an enum
/// </summary>
/// <param name="enumType"></param>
/// <param name="skip">Value to skip</param>
/// <returns>The first undefined enum value</returns>
internal static object GetFirstFreeValue(Type enumType, int skip) => RealEnumPatcher.GetFirstFreeValue(enumType, skip);
internal static bool TryGetRawPatch(Type enumType, out RealEnumPatcher.EnumPatch patch) => RealEnumPatcher.TryGetRawPatch(enumType, out patch);
}
}