Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
EnumUtils.cs
浏览该文件的文档.
1using Engine;
2
3namespace Game {
4 public static class EnumUtils {
5 public struct NamesValues {
6 public ReadOnlyList<string> Names;
7
8 public ReadOnlyList<int> Values;
9 }
10
11 public static class Cache {
12 public static Dictionary<Type, NamesValues> m_namesValuesByType = new();
13
14 [Obsolete("Use Query<T> instead.")]
15 public static NamesValues Query(Type type) {
16 lock (m_namesValuesByType) {
17 if (!m_namesValuesByType.TryGetValue(type, out NamesValues namesValues)) {
18 namesValues = default;
19 namesValues.Names = new ReadOnlyList<string>(new List<string>(Enum.GetNames(type)));
20#pragma warning disable IL3050
21 namesValues.Values = new ReadOnlyList<int>(new List<int>(Enum.GetValues(type).Cast<int>()));
22#pragma warning restore IL3050
23 m_namesValuesByType.Add(type, namesValues);
24 }
25 return namesValues;
26 }
27 }
28
29 public static NamesValues Query<T>() where T : struct, Enum {
30 lock (m_namesValuesByType) {
31 Type type = typeof(T);
32 if (!m_namesValuesByType.TryGetValue(type, out NamesValues namesValues)) {
33 namesValues = default;
34 namesValues.Names = new ReadOnlyList<string>(Enum.GetNames<T>());
35 namesValues.Values = new ReadOnlyList<int>(Enum.GetValues<T>().Select(x => Convert.ToInt32(x)).ToArray());
36 m_namesValuesByType.Add(type, namesValues);
37 }
38 return namesValues;
39 }
40 }
41 }
42
43 [Obsolete("Use GetEnumName<T> instead.")]
44 public static string GetEnumName(Type type, int value) {
45 int num = GetEnumValues(type).IndexOf(value);
46 return num >= 0 ? GetEnumNames(type)[num] : "<invalid enum>";
47 }
48
49 public static string GetEnumName<T>(T value) where T : struct, Enum {
50 int num = GetEnumValues<T>().IndexOf(Convert.ToInt32(value));
51 return num >= 0 ? GetEnumNames<T>()[num] : "<invalid enum>";
52 }
53
54 [Obsolete("Use GetEnumNames<T> instead.")]
55 public static IList<string> GetEnumNames(Type type) => Cache.Query(type).Names;
56
57 public static IList<string> GetEnumNames<T>() where T : struct, Enum => Cache.Query<T>().Names;
58
59 [Obsolete("Use GetEnumValues<T> instead.")]
60 public static IList<int> GetEnumValues(Type type) => Cache.Query(type).Values;
61
62 public static IList<int> GetEnumValues<T>() where T : struct, Enum => Cache.Query<T>().Values;
63 }
64}
static NamesValues Query(Type type)
static NamesValues Query< T >()
static Dictionary< Type, NamesValues > m_namesValuesByType
static IList< string > GetEnumNames< T >()
static IList< string > GetEnumNames(Type type)
static string GetEnumName< T >(T value)
static IList< int > GetEnumValues< T >()
static string GetEnumName(Type type, int value)
static IList< int > GetEnumValues(Type type)
ReadOnlyList< string > Names
ReadOnlyList< int > Values