Elin Decompiled Documentation EA 23.343.5 Nightly
Loading...
Searching...
No Matches
ModXml.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Xml;
6using System.Xml.Linq;
7using UnityEngine;
8
9public static class ModXml
10{
11 private static readonly string[] _validXml = new string[14]
12 {
13 "title", "author", "id", "description", "version", "builtin", "tag", "tags", "loadPriority", "visibility",
14 "dependency", "incompatible", "loadAfter", "loadBefore"
15 };
16
17 public static string ValidXml(string name)
18 {
19 return _validXml.FirstOrDefault((string known) => string.Equals(name, known, StringComparison.OrdinalIgnoreCase));
20 }
21
22 public static string Text(XElement e)
23 {
24 return e.Value.Trim();
25 }
26
27 public static string[] SplitTags(string value)
28 {
29 if (value.IsEmpty())
30 {
31 return null;
32 }
33 List<string> list = new List<string>();
34 string[] array = value.Split(',');
35 for (int i = 0; i < array.Length; i++)
36 {
37 string text = array[i].Trim();
38 if (!text.IsEmpty() && !list.Contains(text))
39 {
40 list.Add(text);
41 }
42 }
43 if (list.Count != 0)
44 {
45 return list.ToArray();
46 }
47 return null;
48 }
49
50 public static void ReadIdRow(XElement e, ref List<string[]> rows, string path)
51 {
52 try
53 {
54 string text = Attribute(e, "id");
55 if (text.IsEmpty())
56 {
57 Debug.LogWarning("#mod package.xml <" + e.Name.LocalName + "> missing id, ignored: " + path);
58 return;
59 }
60 List<string> list = null;
61 string[] array = text.Split(',');
62 for (int i = 0; i < array.Length; i++)
63 {
64 string text2 = BaseModPackage.NormalizeId(array[i]);
65 if (!text2.IsEmpty())
66 {
67 if (list == null)
68 {
69 list = new List<string>();
70 }
71 if (!list.Contains(text2))
72 {
73 list.Add(text2);
74 }
75 }
76 }
77 if (list != null)
78 {
79 if (rows == null)
80 {
81 rows = new List<string[]>();
82 }
83 rows.Add(list.ToArray());
84 }
85 }
86 catch (Exception arg)
87 {
88 Debug.LogWarning($"#mod package.xml <{e.Name.LocalName}> is ill-formatted, ignored: {path}\n{arg}");
89 }
90 }
91
92 private static string Attribute(XElement e, string name)
93 {
94 return e.Attributes().FirstOrDefault((XAttribute a) => string.Equals(a.Name.LocalName, name, StringComparison.OrdinalIgnoreCase))?.Value;
95 }
96
97 public static XDocument Load(byte[] bytes, out string error)
98 {
99 error = null;
100 XmlReaderSettings settings = new XmlReaderSettings
101 {
102 IgnoreComments = true,
103 IgnoreWhitespace = true,
104 IgnoreProcessingInstructions = true,
105 DtdProcessing = DtdProcessing.Prohibit,
106 XmlResolver = null
107 };
108 try
109 {
110 using MemoryStream input = new MemoryStream(bytes, writable: false);
111 using XmlReader reader = XmlReader.Create(input, settings);
112 return XDocument.Load(reader);
113 }
114 catch (Exception ex)
115 {
116 error = ex.Message;
117 return null;
118 }
119 }
120}
static string NormalizeId(string s)
Definition: ModXml.cs:10
static XDocument Load(byte[] bytes, out string error)
Definition: ModXml.cs:97
static string Attribute(XElement e, string name)
Definition: ModXml.cs:92
static string[] SplitTags(string value)
Definition: ModXml.cs:27
static string ValidXml(string name)
Definition: ModXml.cs:17
static string Text(XElement e)
Definition: ModXml.cs:22
static void ReadIdRow(XElement e, ref List< string[]> rows, string path)
Definition: ModXml.cs:50
static readonly string[] _validXml
Definition: ModXml.cs:11