-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
416 lines (353 loc) · 16.8 KB
/
Program.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using Verse;
/*
* Notes: Map size is hardcoded to 250,250
* This could use error checking. Assuming input vals are correct for file and args.
*/
// USAGE: EXE inputfile(savegame) outputfile(mapgen XML) defnameForMap
// USAGE: C:\Users\johnn\source\repos\RimWorldMaps\RimWorldMaps\bin\Debug\RimWorldMaps.exe E:\Modding\RimWorld\_Templates\Dwarfs\_CustomMap\OasisshawDirty.rws E:\Modding\RimWorld\_Templates\Dwarfs\_CustomMap\OUTPUT.xml RH_TET_Dwarfs_CapturedHoldMap
// USAGE: RimWorldMaps.exe E:\Modding\RimWorld\_Templates\Dwarfs\_CustomMap\YT-DwarfUpdatesFilthy.rws E:\Modding\RimWorld\_Templates\Dwarfs\_CustomMap\OUTPUT.xml RH_TET_Dwarfs_CapturedHoldMap
namespace RimWorldMaps
{
class Program
{
public static int MAP_SIZE = 250;
static List<IntVec3> RoofConstructed = new List<IntVec3>();
static List<IntVec3> RoofRockThick = new List<IntVec3>();
static List<IntVec3> RoofRockThin = new List<IntVec3>();
static Dictionary<string, List<IntVec3>> TerrainData = new Dictionary<string, List<IntVec3>>();
static Dictionary<string, List<IntVec3>> UnderTerrainData = new Dictionary<string, List<IntVec3>>();
static Dictionary<string, List<IntVec3>> CompressedThingData = new Dictionary<string, List<IntVec3>>();
static List<ThingHolder> UncompressedThingData = new List<ThingHolder>();
//static Dictionary<string, ThingHolder> UncompressedThingData = new Dictionary<string, ThingHolder>();
static void Main(string[] args)
{
StringBuilder result = new StringBuilder();
System.IO.StreamWriter outputFile = new System.IO.StreamWriter(args[1]);
XElement savegame = XElement.Load(args[0]);
XElement game = savegame.Element("game");
XElement maps = game.Element("maps");
XElement li = maps.Element("li");
IEnumerable<XElement> things = li.Elements();
outputFile.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
outputFile.WriteLine("<Defs>");
outputFile.WriteLine(" <TheEndTimes_Dwarfs.MapGenDef>");
outputFile.WriteLine(" <defName>" + args[2] + "</defName>");
outputFile.WriteLine(" <size>(250, 250)</size>");
outputFile.WriteLine(" <defogPosition>(125,0,0)</defogPosition>");
LoadMapDataToObjects(things, ref result);
outputFile.WriteLine(" <MapData>");
OutputSimpleData(outputFile, "Terrain", TerrainData);
OutputSimpleData(outputFile, "Thing", CompressedThingData);
OutputComplexData(outputFile, UncompressedThingData);
outputFile.WriteLine(" </MapData>");
outputFile.WriteLine(" <UnderTerrainData>");
OutputSimpleData(outputFile, "Terrain", UnderTerrainData);
outputFile.WriteLine(" </UnderTerrainData>");
outputFile.WriteLine(" <RoofData>");
OutputRoofData(outputFile, "RoofConstructed", RoofConstructed);
OutputRoofData(outputFile, "RoofRockThick", RoofRockThick);
OutputRoofData(outputFile, "RoofRockThin", RoofRockThin);
outputFile.WriteLine(" </RoofData>");
outputFile.WriteLine(" </TheEndTimes_Dwarfs.MapGenDef>");
outputFile.WriteLine("</Defs>");
outputFile.Close();
}
private static void OutputComplexData(StreamWriter outputFile, List<ThingHolder> uncompressedThingData)
{
foreach (ThingHolder thing in uncompressedThingData)
{
if (!thing.defName.Equals("HeronInvisibleDoor"))
{
outputFile.WriteLine(" <li>");
outputFile.WriteLine(" <key>");
if (thing.kind == null)
outputFile.WriteLine(" <Thing>" + thing.defName + "</Thing>");
else
outputFile.WriteLine(" <Kind>" + thing.kind + "</Kind>");
if (!thing.stuff.NullOrEmpty())
outputFile.WriteLine(" <Stuff>" + thing.stuff + "</Stuff>");
if (thing.count == 0)
outputFile.WriteLine(" <Rotate>" + thing.rot + "</Rotate>");
if (thing.count > 0)
outputFile.WriteLine(" <Count>" + thing.count + "</Count>");
outputFile.WriteLine(" </key>");
outputFile.WriteLine(" <value>");
foreach (IntVec3 currPos in thing.positions)
outputFile.WriteLine(" <li>" + currPos.ToString() + "</li>");
outputFile.WriteLine(" </value>");
outputFile.WriteLine(" </li>");
}
}
}
private static void OutputSimpleData(StreamWriter outputFile, string thingType, Dictionary<string, List<IntVec3>> data)
{
foreach (string key in data.Keys)
{
outputFile.WriteLine(" <li>");
outputFile.WriteLine(" <key>");
outputFile.WriteLine(" <" + thingType + ">" + key + "</" + thingType + ">");
outputFile.WriteLine(" </key>");
outputFile.WriteLine(" <value>");
foreach (IntVec3 position in data.TryGetValue(key))
{
outputFile.WriteLine(" <li>" + position.ToString() + "</li>");
}
outputFile.WriteLine(" </value>");
outputFile.WriteLine(" </li>");
}
}
private static void OutputRoofData(StreamWriter outputFile, string defName, List<IntVec3> positions)
{
outputFile.WriteLine(" <li>");
outputFile.WriteLine(" <RoofDef>" + defName + "</RoofDef>");
outputFile.WriteLine(" <Positions>");
foreach (IntVec3 position in positions)
{
outputFile.WriteLine(" <li>" + position.ToString() + "</li>");
}
outputFile.WriteLine(" </Positions>");
outputFile.WriteLine(" </li>");
}
private static void LoadMapDataToObjects(IEnumerable<XElement> things, ref StringBuilder result)
{
foreach (XElement thing in things)
{
if (thing.Name.ToString().Equals("roofGrid"))
{
XElement roofsDeflate = thing.Element("roofsDeflate");
string str = roofsDeflate.Value;
byte[] arr = (byte[])null;
if (str != null)
{
arr = CompressUtility.Decompress(Convert.FromBase64String(DataExposeUtility.RemoveLineBreaks(str)));
for (int index = 0; index < (MAP_SIZE * MAP_SIZE); ++index)
{
roofWriter(index, (ushort)((int)arr[index * 2] << 0 | (int)arr[index * 2 + 1] << 8));
}
}
}
else if (thing.Name.ToString().Equals("terrainGrid"))
{
XElement underGridDeflate = thing.Element("underGridDeflate");
string underGridDeflateValue = underGridDeflate.Value;
byte[] underGridDeflated = (byte[])null;
if (underGridDeflateValue != null)
{
underGridDeflated = CompressUtility.Decompress(Convert.FromBase64String(DataExposeUtility.RemoveLineBreaks(underGridDeflateValue)));
for (int index = 0; index < (MAP_SIZE * MAP_SIZE); ++index)
{
underTerrainWriter(index, (ushort)((int)underGridDeflated[index * 2] << 0 | (int)underGridDeflated[index * 2 + 1] << 8));
}
}
XElement topGridDeflate = thing.Element("topGridDeflate");
string topGridDeflateValue = topGridDeflate.Value;
byte[] topGridDeflated = (byte[])null;
if (topGridDeflateValue != null)
{
topGridDeflated = CompressUtility.Decompress(Convert.FromBase64String(DataExposeUtility.RemoveLineBreaks(topGridDeflateValue)));
for (int index = 0; index < (MAP_SIZE * MAP_SIZE); ++index)
{
terrainWriter(index, (ushort)((int)topGridDeflated[index * 2] << 0 | (int)topGridDeflated[index * 2 + 1] << 8));
}
}
}
else if (thing.Name.ToString().Equals("compressedThingMapDeflate"))
{
string compressedThingMapValue = thing.Value;
byte[] compressedThingMapDeflated = (byte[])null;
if (compressedThingMapValue != null)
{
compressedThingMapDeflated = CompressUtility.Decompress(Convert.FromBase64String(DataExposeUtility.RemoveLineBreaks(compressedThingMapValue)));
for (int index = 0; index < (MAP_SIZE * MAP_SIZE); ++index)
{
compressedThingWriter(index, (ushort)((int)compressedThingMapDeflated[index * 2] << 0 | (int)compressedThingMapDeflated[index * 2 + 1] << 8));
}
}
}
else if (thing.Name.ToString().Equals("things"))
{
IEnumerable<XElement> thingsEnum = thing.Elements("thing");
char[] delimiterChars = {','};
foreach (XElement currentThing in thingsEnum)
{
XElement def = currentThing.Element("def");
XElement pos = currentThing.Element("pos");
XElement rot = currentThing.Element("rot");
XElement count = currentThing.Element("stackCount");
XElement stuff = currentThing.Element("stuff");
XElement kind = currentThing.Element("kindDef");
string defValue = def.Value;
string stuffValue = null;
int rotValue = 0;
int countValue = 0;
string kindValue = null;
if (rot != null)
{
rotValue = Int32.Parse(rot.Value);
}
else
{
rotValue = 0;
}
if (count != null)
{
countValue = Int32.Parse(count.Value);
}
if (stuff != null)
{
stuffValue = stuff.Value;
}
if (kind != null)
{
kindValue = kind.Value;
}
string posVal = pos.Value;
posVal = posVal.Replace("(", "").Replace(")", "");
string[] posValues = posVal.Split(delimiterChars);
IntVec3 position = new IntVec3(Int32.Parse(posValues[0]), Int32.Parse(posValues[1]), Int32.Parse(posValues[2]));
ThingHolder thingCurr = FindThingByDefName(defValue);
if (thingCurr != null && MeetsCombinationRequirements(rot, count, stuff, kind, thingCurr))
{
thingCurr.AddPos(position);
}
else
UncompressedThingData.Add(new ThingHolder(defValue, rotValue, position, countValue, stuffValue, kindValue));
}
}
}
}
private static ThingHolder FindThingByDefName(string defValue)
{
foreach(ThingHolder currThing in UncompressedThingData)
{
if (currThing.defName.Equals(defValue))
return currThing;
}
return null;
}
// Determine whether currThing matches the item we're attempting to store. Will determine whether a new entry is made in the OUTPUT
// If we made it here, we know the def matches.
private static bool MeetsCombinationRequirements(XElement rot, XElement count, XElement stuff, XElement kind, ThingHolder currThing)
{
int currRot = currThing.rot;
int newRot = (rot == null ? 0 : Int32.Parse(rot.Value));
int currCount = currThing.count;
int newCount = (count == null ? 0 : Int32.Parse(count.Value));
string currStuff = currThing.stuff;
string newStuff = (stuff == null ? null : stuff.Value);
string currKind = currThing.kind;
string newKind = (kind == null ? null : kind.Value);
if (currRot == newRot
&& currCount == newCount
&& currStuff == newStuff
&& currKind == newKind)
{
return true;
}
//if (kind != null && rot != null && count == null && stuff == null)//
// return true;
//else if (kind == null && rot == null && count == null && stuff == null)//
// return true;
//else if (stuff != null && rot == null && count == null && kind == null)//
// return true;
return false;
}
public static void roofShortWriter(IntVec3 c, ushort val)
{
if (val == 5133)
{
RoofConstructed.Add(c);
}
else if (val == 10820)
{
RoofRockThick.Add(c);
}
else if (val == 6699)
{
RoofRockThin.Add(c);
}
}
public static void roofWriter (int idx, ushort data)
{
roofShortWriter(IndexToCell(idx), data);
}
public static void underTerrainWriter(int idx, ushort val)
{
IntVec3 c = IndexToCell(idx);
String lookUpVal = TerrainKeyValues.TERRAIN_KEY_VALUE_USHORT.TryGetValue(val);
if (!lookUpVal.NullOrEmpty())
{
if (!UnderTerrainData.ContainsKey(lookUpVal))
{
List<IntVec3> locations = new List<IntVec3>();
locations.Add(c);
UnderTerrainData.Add(lookUpVal, locations);
}
else
{
UnderTerrainData.TryGetValue(lookUpVal).Add(c);
}
}
else
{
// Toss an error.
}
}
public static void terrainWriter(int idx, ushort val)
{
IntVec3 c = IndexToCell(idx);
String lookUpVal = TerrainKeyValues.TERRAIN_KEY_VALUE_USHORT.TryGetValue(val);
if (!lookUpVal.NullOrEmpty())
{
if (!TerrainData.ContainsKey(lookUpVal))
{
List<IntVec3> locations = new List<IntVec3>();
locations.Add(c);
TerrainData.Add(lookUpVal, locations);
}
else
{
TerrainData.TryGetValue(lookUpVal).Add(c);
}
}
else
{
// Toss an error.
}
}
public static void compressedThingWriter(int idx, ushort data)
{
IntVec3 c = IndexToCell(idx);
String lookUpVal = ThingKeyValues.THING_KEY_VALUE_USHORT.TryGetValue(data);
if (!lookUpVal.NullOrEmpty())
{
if (!CompressedThingData.ContainsKey(lookUpVal))
{
List<IntVec3> locations = new List<IntVec3>();
locations.Add(c);
CompressedThingData.Add(lookUpVal, locations);
}
else
{
CompressedThingData.TryGetValue(lookUpVal).Add(c);
}
}
else
{
// Toss an error.
}
}
public static IntVec3 IndexToCell(int ind)
{
return CellIndicesUtility.IndexToCell(ind, MAP_SIZE);
}
}
}