-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLparser.cs
306 lines (244 loc) · 10 KB
/
XMLparser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
using System;
using System.Xml;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Threading;
public class Operator
{
public static string[] xmlFiles;
public static string filePathToRead;
public static string outputLocation;
// delimiter
public static string delim = ";";
public static char delimChar;
public static List<string> exclusions = new List<string>();
public static void Main()
{
//CREATE TEXT FILE
Console.WriteLine(" ");
Console.WriteLine("Rimworld XML Data Extractor");
Console.WriteLine("[@patchware] // 2023");
Console.WriteLine("----------------------------");
Console.WriteLine("https://github.com/jp-prfsn/Rimworld-XML-Data-Extractor");
Console.WriteLine("----------------------------");
delimChar = delim.ToCharArray()[0];
getMode();
}
public static void getMode() {
Console.WriteLine(" ");
Console.WriteLine("[1] run");
Console.WriteLine("[2] settings");
Console.WriteLine("[3] exit");
Console.WriteLine("----------------------------");
string opt = Console.ReadLine();
if (opt == "1") {
Console.WriteLine("Folder location of defs to read:");
Console.WriteLine("[ Submit blank to use default of ]");
Console.WriteLine(@"[ C:\Program Files (x86)\Steam\steamapps\workshop\content\ ]");
filePathToRead = getPath(@"C:\Program Files (x86)\Steam\steamapps\workshop\content\");
Console.WriteLine("Enter file output location: ");
Console.WriteLine("[ Submit blank to use default of Desktop ]");
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
outputLocation = getPath(desktop);
Console.WriteLine("Finding files...");
xmlFiles = Directory.GetFiles(filePathToRead, "*.xml", SearchOption.AllDirectories);
Console.WriteLine(xmlFiles.Length + " XML files found.");
pullIt();
} else if (opt == "2") {
Console.WriteLine(" ");
Console.WriteLine("[1] change delimiter");
Console.WriteLine("[2] add exclusions");
Console.WriteLine("[3] remove exclusions");
Console.WriteLine("[4] exit to menu");
Console.WriteLine("----------------------------");
string setter = Console.ReadLine();
if (setter == "1") {
// CHANGE A DELIMITER
Console.WriteLine("Preferred delimiter:");
delim = Console.ReadLine();
delimChar = delim.ToCharArray()[0];
} else if (setter == "2") {
// ADD AN EXCLUSION TERM
Console.WriteLine("Current exclusions:");
foreach (string e in exclusions) {
Console.WriteLine(e);
}
Console.WriteLine(" ");
Console.WriteLine("New exclusion:");
exclusions.Add(Console.ReadLine());
} else if (setter == "3") {
// ADD AN EXCLUSION TERM
Console.WriteLine("Current exclusions:");
foreach (string e in exclusions) {
Console.WriteLine(e);
}
Console.WriteLine(" ");
Console.WriteLine("New exclusion:");
exclusions.Remove(Console.ReadLine());
}
getMode();
} else if (opt == "3") {
System.Environment.Exit(1);
} else {
Console.WriteLine("No such command.");
getMode();
}
}
public static string getPath(string dflt = "") {
string path = Console.ReadLine();
if (path == "") {
return dflt;
}
else if (Directory.Exists(path))
{
Console.WriteLine("Path OK.");
return path;
} else {
Console.WriteLine("Path not found. Try again.");
return getPath();
}
}
public static void pullIt() {
Console.WriteLine("Which tag are you interested in?");
Console.WriteLine("[ e.g. ThingDef, ResearchProjectDef ]");
string tagLabel = Console.ReadLine();
Console.WriteLine("Which properties of that tag do you want? (comma separated values, no spaces)");
Console.WriteLine("[ e.g. label,baseCost,prequisites ]");
string propLong = Console.ReadLine();
string[] props = propLong.Split(',');
using (StreamWriter outputFile = File.CreateText(outputLocation + @"\" + tagLabel + ".txt"))
{
xmlFiles = Directory.GetFiles(filePathToRead, "*.xml", SearchOption.AllDirectories);
Console.WriteLine("Pulling " + tagLabel + "s");
Console.WriteLine("....................................");
int count = 1;
float perc = 0;
string titleLine = "";
foreach (string p in props)
{
titleLine += p + delimChar;
}
titleLine += "Mod Name (packageId)";
//titleLine = titleLine.TrimEnd(delimChar);
outputFile.WriteLine(titleLine);
foreach (string file in xmlFiles)
{
perc = ((float)count / xmlFiles.Length) * 100;
Console.Write("\r{0}% ", perc);
count++;
string thisModName = getModName(file);
// filter out files that give issues or are not important
foreach (string e in exclusions)
{
if (file.Contains(e))
{
continue;
}
}
try
{
// OPEN THE FILE
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(file);
// FOR EACH HEADNODE
XmlNodeList defs = xmlDoc.GetElementsByTagName(tagLabel);
if (defs.Count > 0)
{
for (int k = 0; k < defs.Count; k++)
{
XmlNode thisNode = defs[k];
string oneLine = "";
if (thisNode.HasChildNodes) // if this node has children
{
// for each property we mentioned
foreach (string p in props)
{
XmlNode property = thisNode[p];
if (property != null)
{
if (!property.HasChildNodes)
{
oneLine += "0" + delimChar;
}
else if (property.ChildNodes[0].NodeType != XmlNodeType.Text)
{
//list them separated by comma
foreach (XmlNode c in property)
{
oneLine += c.InnerText + ",";
}
oneLine += delimChar;
}
else
{
//else sparate by semicolon
oneLine += property.InnerText + delimChar;
}
}
else
{
//Handle unfound tags
oneLine += "0" + delimChar;
}
}
oneLine += thisModName;
}
string formatString = oneLine.TrimEnd(delimChar);
formatString = formatString.TrimEnd(',');
outputFile.WriteLine(formatString);
}
}
}
catch (Exception ex)
{
Console.WriteLine(file);
Console.WriteLine(ex);
continue;
}
}
outputFile.Close();
Console.WriteLine(" ");
}
Console.WriteLine("....................................");
Console.WriteLine("COMPLETED");
Console.WriteLine("....................................");
getMode();
}
public static string getModName(string fl){
FileInfo f = new FileInfo(fl);
string fullname = f.FullName;
string filePath = fullname;
string directoryName;
int i = 0;
string modName = "";
while (filePath != null)
{
directoryName = Path.GetDirectoryName(filePath);
filePath = directoryName;
if (i == 1)
{
filePath = directoryName + @"\";
}
if(directoryName == @"C:\"){
break;
}
string[] dirs = Directory.GetDirectories(filePath, "About", SearchOption.TopDirectoryOnly);
if(dirs.Length > 0){
XmlDocument doc = new XmlDocument();
doc.Load(dirs[0] + @"\About.xml");
XmlNodeList pkgIDs = doc.GetElementsByTagName("packageId");
if(pkgIDs.Count > 0){
modName = pkgIDs[0].InnerText;
}
break;
}
i++;
}
if(modName != ""){
return modName;
}else{
return null;
}
}
}