Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
XDRSchema.cs
Go to the documentation of this file.
5using System.Xml;
6
7namespace System.Data;
8
9internal sealed class XDRSchema : XMLSchema
10{
11 private sealed class NameType : IComparable
12 {
13 public string name;
14
16 public Type type;
17
19 {
20 name = n;
21 type = t;
22 }
23
24 public int CompareTo(object obj)
25 {
26 return string.Compare(name, (string)obj, StringComparison.Ordinal);
27 }
28 }
29
30 internal string _schemaName;
31
32 internal string _schemaUri;
33
35
36 internal DataSet _ds;
37
38 private static readonly NameType[] s_mapNameTypeXdr = new NameType[36]
39 {
40 new NameType("bin.base64", typeof(byte[])),
41 new NameType("bin.hex", typeof(byte[])),
42 new NameType("boolean", typeof(bool)),
43 new NameType("byte", typeof(sbyte)),
44 new NameType("char", typeof(char)),
45 new NameType("date", typeof(DateTime)),
46 new NameType("dateTime", typeof(DateTime)),
47 new NameType("dateTime.tz", typeof(DateTime)),
48 new NameType("entities", typeof(string)),
49 new NameType("entity", typeof(string)),
50 new NameType("enumeration", typeof(string)),
51 new NameType("fixed.14.4", typeof(decimal)),
52 new NameType("float", typeof(double)),
53 new NameType("i1", typeof(sbyte)),
54 new NameType("i2", typeof(short)),
55 new NameType("i4", typeof(int)),
56 new NameType("i8", typeof(long)),
57 new NameType("id", typeof(string)),
58 new NameType("idref", typeof(string)),
59 new NameType("idrefs", typeof(string)),
60 new NameType("int", typeof(int)),
61 new NameType("nmtoken", typeof(string)),
62 new NameType("nmtokens", typeof(string)),
63 new NameType("notation", typeof(string)),
64 new NameType("number", typeof(decimal)),
65 new NameType("r4", typeof(float)),
66 new NameType("r8", typeof(double)),
67 new NameType("string", typeof(string)),
68 new NameType("time", typeof(DateTime)),
69 new NameType("time.tz", typeof(DateTime)),
70 new NameType("ui1", typeof(byte)),
71 new NameType("ui2", typeof(ushort)),
72 new NameType("ui4", typeof(uint)),
73 new NameType("ui8", typeof(ulong)),
74 new NameType("uri", typeof(string)),
75 new NameType("uuid", typeof(Guid))
76 };
77
78 private static readonly NameType s_enumerationNameType = FindNameType("enumeration");
79
80 internal XDRSchema(DataSet ds, bool fInline)
81 {
82 _schemaUri = string.Empty;
83 _schemaName = string.Empty;
84 _schemaRoot = null;
85 _ds = ds;
86 }
87
88 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
90 {
91 if (schemaRoot == null)
92 {
93 return;
94 }
96 _ds = ds;
97 _schemaName = schemaRoot.GetAttribute("name");
98 _schemaUri = string.Empty;
99 if (_schemaName == null || _schemaName.Length == 0)
100 {
101 _schemaName = "NewDataSet";
102 }
104 for (XmlNode xmlNode = schemaRoot.FirstChild; xmlNode != null; xmlNode = xmlNode.NextSibling)
105 {
107 {
109 if (XMLSchema.FEqualIdentity(node, "ElementType", "urn:schemas-microsoft-com:xml-data"))
110 {
112 }
113 }
114 }
116 if (ds.Tables[_schemaName] == null)
117 {
119 }
120 }
121
123 {
124 if (XMLSchema.FEqualIdentity(node, "ElementType", "urn:schemas-microsoft-com:xml-data"))
125 {
126 return node;
127 }
128 string attribute = node.GetAttribute("type");
129 if (XMLSchema.FEqualIdentity(node, "element", "urn:schemas-microsoft-com:xml-data") || XMLSchema.FEqualIdentity(node, "attribute", "urn:schemas-microsoft-com:xml-data"))
130 {
131 if (attribute == null || attribute.Length == 0)
132 {
133 return null;
134 }
137 while (xmlNode != ownerDocument)
138 {
139 if (((XMLSchema.FEqualIdentity(xmlNode, "ElementType", "urn:schemas-microsoft-com:xml-data") && XMLSchema.FEqualIdentity(node, "element", "urn:schemas-microsoft-com:xml-data")) || (XMLSchema.FEqualIdentity(xmlNode, "AttributeType", "urn:schemas-microsoft-com:xml-data") && XMLSchema.FEqualIdentity(node, "attribute", "urn:schemas-microsoft-com:xml-data"))) && xmlNode is XmlElement && ((XmlElement)xmlNode).GetAttribute("name") == attribute)
140 {
141 return (XmlElement)xmlNode;
142 }
143 if (xmlNode.FirstChild != null)
144 {
146 continue;
147 }
148 if (xmlNode.NextSibling != null)
149 {
151 continue;
152 }
153 while (xmlNode != ownerDocument)
154 {
156 if (xmlNode.NextSibling != null)
157 {
159 break;
160 }
161 }
162 }
163 return null;
164 }
165 return null;
166 }
167
169 {
170 string attribute = node.GetAttribute("content");
171 if (attribute == null || attribute.Length == 0)
172 {
173 string attribute2 = node.GetAttribute("type", "urn:schemas-microsoft-com:datatypes");
174 return !string.IsNullOrEmpty(attribute2);
175 }
176 switch (attribute)
177 {
178 case "empty":
179 case "eltOnly":
180 case "elementOnly":
181 case "mixed":
182 return false;
183 case "textOnly":
184 return true;
185 default:
187 }
188 }
189
191 {
192 int minOccurs = 1;
193 int maxOccurs = 1;
195 {
196 return false;
197 }
198 for (XmlNode xmlNode = typeNode.FirstChild; xmlNode != null; xmlNode = xmlNode.NextSibling)
199 {
200 if (XMLSchema.FEqualIdentity(xmlNode, "element", "urn:schemas-microsoft-com:xml-data") || XMLSchema.FEqualIdentity(xmlNode, "attribute", "urn:schemas-microsoft-com:xml-data"))
201 {
202 return false;
203 }
204 }
205 if (XMLSchema.FEqualIdentity(node, "element", "urn:schemas-microsoft-com:xml-data"))
206 {
208 if (maxOccurs == -1 || maxOccurs > 1)
209 {
210 return false;
211 }
212 }
213 return true;
214 }
215
216 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
218 {
220 string attribute = node.GetAttribute("minOccurs");
221 if (attribute != null && attribute.Length > 0 && Convert.ToInt32(attribute, CultureInfo.InvariantCulture) > 1 && xmlElement == null)
222 {
224 }
225 attribute = node.GetAttribute("maxOccurs");
226 if (attribute != null && attribute.Length > 0 && !string.Equals(attribute, "1", StringComparison.Ordinal) && xmlElement == null)
227 {
229 }
230 if (xmlElement == null)
231 {
232 return null;
233 }
235 {
236 return null;
237 }
239 }
240
241 private static NameType FindNameType(string name)
242 {
243 int num = Array.BinarySearch(s_mapNameTypeXdr, name);
244 if (num < 0)
245 {
247 }
248 return s_mapNameTypeXdr[num];
249 }
250
252 private Type ParseDataType(string dt, string dtValues)
253 {
254 string name = dt;
255 string[] array = dt.Split(':');
256 if (array.Length > 2)
257 {
259 }
260 if (array.Length == 2)
261 {
262 name = array[1];
263 }
265 if (nameType == s_enumerationNameType && (dtValues == null || dtValues.Length == 0))
266 {
267 throw ExceptionBuilder.MissingAttribute("type", "values");
268 }
269 return nameType.type;
270 }
271
273 {
274 string attribute;
275 if (XMLSchema.FEqualIdentity(node, "ElementType", "urn:schemas-microsoft-com:xml-data") || XMLSchema.FEqualIdentity(node, "AttributeType", "urn:schemas-microsoft-com:xml-data"))
276 {
277 attribute = node.GetAttribute("name");
278 if (attribute == null || attribute.Length == 0)
279 {
280 throw ExceptionBuilder.MissingAttribute("Element", "name");
281 }
282 }
283 else
284 {
285 attribute = node.GetAttribute("type");
286 if (attribute == null || attribute.Length == 0)
287 {
288 throw ExceptionBuilder.MissingAttribute("Element", "type");
289 }
290 }
291 return attribute;
292 }
293
294 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
295 internal void HandleColumn(XmlElement node, DataTable table)
296 {
297 int minOccurs = 0;
298 int maxOccurs = 1;
299 string name;
301 if (node.Attributes.Count > 0)
302 {
303 string attribute = node.GetAttribute("ref");
304 if (attribute != null && attribute.Length > 0)
305 {
306 return;
307 }
308 string text = (name = GetInstanceName(node));
309 dataColumn = table.Columns[name, _schemaUri];
310 if (dataColumn != null)
311 {
312 if (dataColumn.ColumnMapping == MappingType.Attribute)
313 {
314 if (XMLSchema.FEqualIdentity(node, "attribute", "urn:schemas-microsoft-com:xml-data"))
315 {
317 }
318 }
319 else if (XMLSchema.FEqualIdentity(node, "element", "urn:schemas-microsoft-com:xml-data"))
320 {
322 }
323 name = XMLSchema.GenUniqueColumnName(text, table);
324 }
325 }
326 else
327 {
328 name = string.Empty;
329 }
331 SimpleType simpleType = null;
332 string attribute2;
333 if (xmlElement == null)
334 {
335 attribute2 = node.GetAttribute("type");
337 }
338 attribute2 = xmlElement.GetAttribute("type", "urn:schemas-microsoft-com:datatypes");
339 string attribute3 = xmlElement.GetAttribute("values", "urn:schemas-microsoft-com:datatypes");
340 Type type;
341 if (attribute2 == null || attribute2.Length == 0)
342 {
343 attribute2 = string.Empty;
344 type = typeof(string);
345 }
346 else
347 {
349 if (attribute2 == "float")
350 {
351 attribute2 = string.Empty;
352 }
353 if (attribute2 == "char")
354 {
355 attribute2 = string.Empty;
357 }
358 if (attribute2 == "enumeration")
359 {
360 attribute2 = string.Empty;
362 }
363 if (attribute2 == "bin.base64")
364 {
365 attribute2 = string.Empty;
367 }
368 if (attribute2 == "bin.hex")
369 {
370 attribute2 = string.Empty;
372 }
373 }
374 bool flag = XMLSchema.FEqualIdentity(node, "attribute", "urn:schemas-microsoft-com:xml-data");
376 string text2 = null;
377 text2 = node.GetAttribute("default");
378 bool flag2 = false;
379 dataColumn = new DataColumn(XmlConvert.DecodeName(name), type, null, (!flag) ? MappingType.Element : MappingType.Attribute);
385 if (node.Attributes != null)
386 {
387 for (int i = 0; i < node.Attributes.Count; i++)
388 {
389 if (node.Attributes[i].NamespaceURI == "urn:schemas-microsoft-com:xml-msdata" && node.Attributes[i].LocalName == "Expression")
390 {
391 dataColumn.Expression = node.Attributes[i].Value;
392 break;
393 }
394 }
395 }
396 string attribute4 = node.GetAttribute("targetNamespace");
397 if (attribute4 != null && attribute4.Length > 0)
398 {
400 }
401 table.Columns.Add(dataColumn);
402 if (text2 != null && text2.Length != 0)
403 {
404 try
405 {
407 }
408 catch (FormatException)
409 {
410 throw ExceptionBuilder.CannotConvert(text2, type.FullName);
411 }
412 }
413 }
414
419
421 {
422 string attribute = elNode.GetAttribute("minOccurs");
423 if (attribute != null && attribute.Length > 0)
424 {
425 try
426 {
428 }
430 {
431 throw ExceptionBuilder.AttributeValues("minOccurs", "0", "1");
432 }
433 }
434 attribute = elNode.GetAttribute("maxOccurs");
435 if (attribute == null || attribute.Length <= 0)
436 {
437 return;
438 }
439 if (string.Compare(attribute, "*", StringComparison.Ordinal) == 0)
440 {
441 maxOccurs = -1;
442 return;
443 }
444 try
445 {
447 }
449 {
450 throw ExceptionBuilder.AttributeValues("maxOccurs", "1", "*");
451 }
452 if (maxOccurs == 1)
453 {
454 return;
455 }
456 throw ExceptionBuilder.AttributeValues("maxOccurs", "1", "*");
457 }
458
459 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
461 {
462 for (XmlNode xmlNode = typeNode.FirstChild; xmlNode != null; xmlNode = xmlNode.NextSibling)
463 {
464 if (!(xmlNode is XmlElement))
465 {
466 continue;
467 }
468 if (XMLSchema.FEqualIdentity(xmlNode, "element", "urn:schemas-microsoft-com:xml-data"))
469 {
471 if (dataTable != null)
472 {
474 continue;
475 }
476 }
477 if (XMLSchema.FEqualIdentity(xmlNode, "attribute", "urn:schemas-microsoft-com:xml-data") || XMLSchema.FEqualIdentity(xmlNode, "element", "urn:schemas-microsoft-com:xml-data"))
478 {
480 }
481 }
482 }
483
484 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
486 {
487 string name = string.Empty;
488 XmlAttributeCollection attributes = node.Attributes;
489 int minOccurs = 1;
490 int maxOccurs = 1;
491 string text = null;
493 DataTable table;
494 if (attributes.Count > 0)
495 {
496 name = GetInstanceName(node);
497 table = dataSet.Tables.GetTable(name, _schemaUri);
498 if (table != null)
499 {
500 return table;
501 }
502 }
503 table = new DataTable(XmlConvert.DecodeName(name));
508 _ds.Tables.Add(table);
510 XMLSchema.SetProperties(table, attributes);
511 if (text != null)
512 {
513 string[] array = text.TrimEnd(null).Split((char[]?)null);
514 int num = array.Length;
515 DataColumn[] array2 = new DataColumn[num];
516 for (int i = 0; i < num; i++)
517 {
519 if (dataColumn == null)
520 {
522 }
523 array2[i] = dataColumn;
524 }
526 }
527 foreach (DataTable item in arrayList)
528 {
530 DataRelationCollection childRelations = table.ChildRelations;
531 for (int j = 0; j < childRelations.Count; j++)
532 {
533 if (childRelations[j].Nested && item == childRelations[j].ChildTable)
534 {
536 }
537 }
538 if (dataRelation == null)
539 {
540 DataColumn dataColumn2 = table.AddUniqueKey();
541 DataColumn childColumn = item.AddForeignKey(dataColumn2);
542 dataRelation = new DataRelation(table.TableName + "_" + item.TableName, dataColumn2, childColumn, createConstraints: true);
544 dataRelation.Nested = true;
547 }
548 }
549 return table;
550 }
551
552 [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly.")]
554 {
555 XmlAttributeCollection attributes = node.Attributes;
556 int minOccurs = 1;
557 int maxOccurs = 1;
559 DataTable table = dataSet.Tables.GetTable(instanceName, _schemaUri);
560 if (table != null)
561 {
563 }
565 table = new DataTable(text);
570 XMLSchema.SetProperties(table, attributes);
572 HandleColumn(node, table);
573 table.Columns[0].ColumnName = text + "_Column";
574 _ds.Tables.Add(table);
575 return table;
576 }
577}
static int BinarySearch(Array array, object? value)
Definition Array.cs:1320
static int ToInt32(object? value)
Definition Convert.cs:1320
static bool IsCatchableExceptionType(Exception e)
Definition ADP.cs:790
static object ChangeTypeForXML(object value, Type type)
virtual ? DataSet DataSet
DataRelationCollection Relations
Definition DataSet.cs:393
DataTableCollection Tables
Definition DataSet.cs:396
DataColumnCollection Columns
Definition DataTable.cs:327
static Exception MissingAttribute(string attribute)
static Exception AttributeValues(string name, string value1, string value2)
static Exception UndefinedDatatype(string name)
static Exception InvalidAttributeValue(string name, string value)
static Exception ElementTypeNotFound(string name)
static Exception DuplicateDeclaration(string name)
static Exception CannotConvert(string name, string type)
static SimpleType CreateEnumeratedType(string values)
static SimpleType CreateSimpleType(StorageType typeCode, Type type)
static SimpleType CreateByteArrayType(string encoding)
NameType(string n, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields|DynamicallyAccessedMemberTypes.PublicProperties)] Type t)
Definition XDRSchema.cs:18
string GetInstanceName(XmlElement node)
Definition XDRSchema.cs:272
bool IsXDRField(XmlElement node, XmlElement typeNode)
Definition XDRSchema.cs:190
XmlElement FindTypeNode(XmlElement node)
Definition XDRSchema.cs:122
static readonly NameType s_enumerationNameType
Definition XDRSchema.cs:78
XDRSchema(DataSet ds, bool fInline)
Definition XDRSchema.cs:80
void GetMinMax(XmlElement elNode, bool isAttribute, ref int minOccurs, ref int maxOccurs)
Definition XDRSchema.cs:420
bool IsTextOnlyContent(XmlElement node)
Definition XDRSchema.cs:168
Type ParseDataType(string dt, string dtValues)
Definition XDRSchema.cs:252
void HandleTypeNode(XmlElement typeNode, DataTable table, ArrayList tableChildren)
Definition XDRSchema.cs:460
DataTable InstantiateTable(DataSet dataSet, XmlElement node, XmlElement typeNode)
Definition XDRSchema.cs:485
DataTable HandleTable(XmlElement node)
Definition XDRSchema.cs:217
DataTable InstantiateSimpleTable(DataSet dataSet, XmlElement node)
Definition XDRSchema.cs:553
void GetMinMax(XmlElement elNode, ref int minOccurs, ref int maxOccurs)
Definition XDRSchema.cs:415
void HandleColumn(XmlElement node, DataTable table)
Definition XDRSchema.cs:295
static NameType FindNameType(string name)
Definition XDRSchema.cs:241
XmlElement _schemaRoot
Definition XDRSchema.cs:34
void LoadSchema(XmlElement schemaRoot, DataSet ds)
Definition XDRSchema.cs:89
static readonly NameType[] s_mapNameTypeXdr
Definition XDRSchema.cs:38
static void SetProperties(object instance, XmlAttributeCollection attrs)
Definition XMLSchema.cs:18
static bool FEqualIdentity(XmlNode node, string name, string ns)
Definition XMLSchema.cs:68
static string GenUniqueColumnName(string proposedName, DataTable table)
Definition XMLSchema.cs:97
static CultureInfo InvariantCulture
static ? string DecodeName(string? name)
Definition XmlConvert.cs:55
virtual string GetAttribute(string name)
virtual ? XmlNode ParentNode
Definition XmlNode.cs:76
virtual ? XmlNode NextSibling
Definition XmlNode.cs:104
virtual ? XmlDocument OwnerDocument
Definition XmlNode.cs:109
virtual ? XmlNode FirstChild
Definition XmlNode.cs:120