Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
IdDictionary.cs
Go to the documentation of this file.
1using System;
3using System.Linq;
5
7
8public class IdDictionary
9{
11
13
14 public readonly int Count;
15
17
18 private IdDictionary(int count)
19 {
20 Count = count;
21 }
22
23 public bool TryGetName(int id, out string name)
24 {
25 return _idToName.TryGetValue(id, out name);
26 }
27
28 public bool TryGetId(string name, out int id)
29 {
30 return _nameToId.TryGetValue(name, out id);
31 }
32
33 public bool ContainsName(string name)
34 {
35 return _nameToId.ContainsKey(name);
36 }
37
38 public bool ContainsId(int id)
39 {
40 return _idToName.ContainsKey(id);
41 }
42
43 public string GetName(int id)
44 {
45 return _idToName[id];
46 }
47
48 public int GetId(string name)
49 {
50 return _nameToId[name];
51 }
52
53 public void Add(string name, int id)
54 {
55 _idToName.Add(id, name);
56 _nameToId.Add(name, id);
57 }
58
59 public void Remove(string name)
60 {
62 _nameToId.Remove(name);
63 }
64
65 public void Remove(int id)
66 {
68 _idToName.Remove(id);
69 }
70
72 {
73 int num = int.MaxValue;
74 FieldInfo fieldInfo = idClass.GetFields().FirstOrDefault((FieldInfo field) => field.Name == "Count");
75 if (fieldInfo != null)
76 {
77 num = Convert.ToInt32(fieldInfo.GetValue(null));
78 if (num == 0)
79 {
80 throw new Exception("IdDictionary cannot be created before Count field is initialized. Move to bottom of static class");
81 }
82 }
84 (from f in idClass.GetFields(BindingFlags.Static | BindingFlags.Public)
85 where f.FieldType == idType
86 where f.GetCustomAttribute<ObsoleteAttribute>() == null
87 select f).ToList().ForEach(delegate(FieldInfo field)
88 {
89 int num2 = Convert.ToInt32(field.GetValue(null));
90 if (num2 < dictionary.Count)
91 {
92 dictionary._nameToId.Add(field.Name, num2);
93 }
94 });
95 dictionary._idToName = dictionary._nameToId.ToDictionary((KeyValuePair<string, int> kp) => kp.Value, (KeyValuePair<string, int> kp) => kp.Key);
96 return dictionary;
97 }
98
100 {
101 return Create(typeof(IdClass), typeof(IdType));
102 }
103}
readonly Dictionary< string, int > _nameToId
IEnumerable< string > Names
Dictionary< int, string > _idToName
bool TryGetId(string name, out int id)
bool TryGetName(int id, out string name)
static IdDictionary Create< IdClass, IdType >()
static IdDictionary Create(Type idClass, Type idType)
void Add(string name, int id)
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
void Add(TKey key, TValue value)
static int ToInt32(object? value)
Definition Convert.cs:1320