Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ValuesDictionary.cs
浏览该文件的文档.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Xml.Linq;
6using XmlUtilities;
7
8namespace TemplatesDatabase {
9 public class ValuesDictionary : IEnumerable<KeyValuePair<string, object>> {
10 Dictionary<string, object> m_dictionary = [];
11
13
14 public int Count => m_dictionary.Count;
15
16 public IEnumerable<string> Keys => m_dictionary.Keys;
17
18 public IEnumerable<object> Values => m_dictionary.Values;
19
21 get => m_databaseObject;
22 set => m_databaseObject = value;
23 }
24
25 public object this[string key] {
26 get => GetValue<object>(key);
27 set => SetValue(key, value);
28 }
29
30 public bool ContainsKey(string key) => m_dictionary.ContainsKey(key);
31
32 public bool ContainsValue(object value) => m_dictionary.ContainsValue(value);
33
34 public void EnsureCapacity(int capacity) {
35 m_dictionary.EnsureCapacity(capacity);
36 }
37
38 public T GetValue<T>(string key) {
39 if (m_dictionary.TryGetValue(key, out object value)) {
40 return (T)value;
41 }
42 throw new InvalidOperationException($"Required value \"{key}\" not found in values dictionary");
43 }
44
45 public T GetValue<T>(string key, T defaultValue) {
46 if (m_dictionary.TryGetValue(key, out object value)) {
47 return (T)value;
48 }
49 return defaultValue;
50 }
51
52 public void SetValue<T>(string key, T value) {
53 m_dictionary[key] = value;
54 }
55
56 public void Add<T>(string key, T value) {
57 m_dictionary.Add(key, value);
58 }
59
60 public void Clear() {
61 m_dictionary.Clear();
62 }
63
64 public void Remove(string key) {
65 m_dictionary.Remove(key);
66 }
67
68 public void Remove(string key, out object value) {
69 m_dictionary.Remove(key, out value);
70 }
71
72 public void TrimExcess() {
73 m_dictionary.TrimExcess();
74 }
75
76 public void TrimExcess(int capacity) {
77 m_dictionary.TrimExcess(capacity);
78 }
79
80 public bool TryAdd<T>(string key, T value) => m_dictionary.TryAdd(key, value);
81
82 public bool TryGetValue<T>(string key, out T value) {
83 if (m_dictionary.TryGetValue(key, out object value2)) {
84 value = (T)value2;
85 return true;
86 }
87 value = default;
88 return false;
89 }
90
91 public void Save(XElement node) {
92 foreach (KeyValuePair<string, object> item in m_dictionary) {
93 if (item.Value is ValuesDictionary valuesDictionary) {
94 XElement node2 = XmlUtils.AddElement(node, "Values");
95 XmlUtils.SetAttributeValue(node2, "Name", item.Key);
96 valuesDictionary.Save(node2);
97 }
98 else if (item.Value != null) {
99 XElement node3 = XmlUtils.AddElement(node, "Value");
100 XmlUtils.SetAttributeValue(node3, "Name", item.Key);
101 XmlUtils.SetAttributeValue(node3, "Type", TypeCache.GetShortTypeName(item.Value.GetType().FullName));
102 XmlUtils.SetAttributeValue(node3, "Value", item.Value);
103 }
104 }
105 }
106
107 public void PopulateFromDatabaseObject(DatabaseObject databaseObject) {
108 m_databaseObject = databaseObject;
109 foreach (DatabaseObject effectiveNestingChild in databaseObject.GetEffectiveNestingChildren(null, true)) {
110 if (effectiveNestingChild.Type.SupportsValue) {
111 if (effectiveNestingChild.Value is ProceduralValue proceduralValue) {
112 object value = proceduralValue.Parse(databaseObject);
113 SetValue(effectiveNestingChild.Name, value);
114 }
115 else {
116 SetValue(effectiveNestingChild.Name, effectiveNestingChild.Value);
117 }
118 }
119 else {
120 ValuesDictionary valuesDictionary = [];
121 valuesDictionary.PopulateFromDatabaseObject(effectiveNestingChild);
122 SetValue(effectiveNestingChild.Name, valuesDictionary);
123 }
124 }
125 }
126
127 public void ApplyOverrides(ValuesDictionary overridesValuesDictionary) {
128 foreach (KeyValuePair<string, object> item in overridesValuesDictionary) {
129 if (item.Value is ValuesDictionary valuesDictionary) {
130 if (GetValue<object>(item.Key, null) is not ValuesDictionary valuesDictionary2) {
131 valuesDictionary2 = [];
132 SetValue(item.Key, valuesDictionary2);
133 }
134 valuesDictionary2.ApplyOverrides(valuesDictionary);
135 }
136 else {
137 SetValue(item.Key, item.Value);
138 }
139 }
140 }
141
142 public void ApplyOverrides(XElement overridesNode) {
143 ApplyOverrides(overridesNode, false);
144 }
145
146 public void ApplyOverrides(XElement overridesNode, bool overrideExistOnly) {
147 foreach (XElement item in overridesNode.Elements()) {
148 if (item.Name == "Value") {
149 string key = XmlUtils.GetAttributeValue<string>(item, "Name");
150 if (overrideExistOnly && !m_dictionary.ContainsKey(key))
151 continue;
152 string typeName = XmlUtils.GetAttributeValue<string>(item, "Type", null);
153 Type type;
154 if (typeName == null) {
155 object value = GetValue<object>(key, null);
156 if (value == null)
157 throw new InvalidOperationException($"Type of override \"{key}\" cannot be determined.");
158 type = value.GetType();
159 }
160 else {
161 type = TypeCache.FindType(typeName, false, true);
162 }
163 object valueObj = XmlUtils.GetAttributeValue(item, "Value", type);
164 SetValue(key, valueObj);
165 }
166 else if (item.Name == "Values") {
167 string key = XmlUtils.GetAttributeValue<string>(item, "Name");
168 if (overrideExistOnly && !m_dictionary.ContainsKey(key))
169 continue;
170 ValuesDictionary valuesDictionary;
171 if (GetValue<object>(key, null) is ValuesDictionary vd) {
172 valuesDictionary = vd;
173 }
174 else {
175 valuesDictionary = new ValuesDictionary();
176 SetValue(key, valuesDictionary);
177 }
178 valuesDictionary.ApplyOverrides(item, overrideExistOnly);
179 }
180 else {
181 throw new InvalidOperationException($"Unrecognized element \"{item.Name}\" in values dictionary overrides XML.");
182 }
183 }
184 }
185 public IEnumerator<KeyValuePair<string, object>> GetEnumerator() => m_dictionary.GetEnumerator();
186
187 IEnumerator IEnumerable.GetEnumerator() => m_dictionary.GetEnumerator();
188 }
189}
static Type FindType(string typeName, bool skipSystemAssemblies, bool throwIfNotFound)
static string GetShortTypeName(string longTypeName)
Dictionary< string, object > m_dictionary
void ApplyOverrides(XElement overridesNode, bool overrideExistOnly)
void ApplyOverrides(XElement overridesNode)
bool TryGetValue< T >(string key, out T value)
void Remove(string key, out object value)
IEnumerator< KeyValuePair< string, object > > GetEnumerator()
void PopulateFromDatabaseObject(DatabaseObject databaseObject)
void ApplyOverrides(ValuesDictionary overridesValuesDictionary)
static void SetAttributeValue(XElement node, string attributeName, object value)
static object GetAttributeValue(XElement node, string attributeName, Type type)
static XElement AddElement(XElement parentNode, string name)