Skip to content

Commit 82b8a25

Browse files
committed
1.1.1 - fix Mod usage
1 parent c26f1eb commit 82b8a25

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed

AssetManager.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System.IO.Compression;
55
using System.Xml;
66
using System.Linq;
7+
using System.Xml.Linq;
8+
using System.Drawing;
79

810
namespace TroubleTool
911
{
@@ -51,6 +53,11 @@ internal void SaveIndex()
5153
IndexHelper.SaveIndex(indexXml, indexPath);
5254
}
5355

56+
internal void SaveIndex(XmlDocument modIndexXml)
57+
{
58+
IndexHelper.SaveIndex(modIndexXml, indexPath);
59+
}
60+
5461
internal XmlNodeList GetEntries()
5562
{
5663
XmlElement root = indexXml.DocumentElement;
@@ -87,18 +94,14 @@ internal void ExtractAllEntries()
8794
{
8895
foreach (XmlElement entry in GetEntries())
8996
{
90-
String src = Path.Combine(package, entry.GetAttribute("pack"));
91-
String dst = Path.Combine(data, entry.GetAttribute("original"));
92-
// TODO - check if it really has to be extracted
93-
Console.WriteLine($"Extracting {src} to {dst}!");
94-
Extract(src, dst, entry);
97+
ExtractEntry(entry);
9598
}
9699
}
97100

98101
internal void ExtractEntry(XmlElement entry)
99102
{
100103
String src = Path.Combine(package, entry.GetAttribute("pack"));
101-
String dst = Path.Combine(data, entry.GetAttribute("original"));
104+
String dst = Path.Combine(data, entry.GetAttribute("original").Trim('\n'));
102105
Extract(src, dst, entry);
103106
}
104107

Form1.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ private void ButtonApply_Click(object sender, EventArgs e)
127127

128128
foreach (FileInfo file in new DirectoryInfo(am.mods).GetFiles())
129129
{
130+
if (file.Extension.ToLower() != "zip")
131+
{
132+
AddToLog($"Ignoring {file.Name} as it's not a zip!", Color.Yellow);
133+
continue;
134+
}
130135
AddToLog(file.Name, Color.White);
131136
using (ZipArchive z = ZipFile.OpenRead(file.FullName))
132137
{
@@ -146,8 +151,26 @@ private void ButtonApply_Click(object sender, EventArgs e)
146151
}
147152
}
148153
}
154+
XmlDocument doc = new XmlDocument();
155+
XmlElement root = doc.CreateElement("index");
156+
root.SetAttribute("mod", "true");
157+
doc.AppendChild(root);
158+
foreach (var entry in indexDict)
159+
{
160+
XmlElement child = doc.CreateElement("entry");
161+
foreach (XmlAttribute attr in entry.Value.Attributes)
162+
{
163+
child.SetAttribute(attr.Name, attr.Value);
164+
}
165+
166+
root.AppendChild(child);
167+
}
168+
am.SaveIndex(doc);
169+
}
170+
else
171+
{
172+
am.SaveIndex();
149173
}
150-
am.SaveIndex();
151174
AddToLog("Done", Color.White);
152175
}
153176

@@ -261,7 +284,7 @@ private void ButtonImageSets_Click(object sender, EventArgs e)
261284
{
262285
if (!file.Name.EndsWith(".imageset"))
263286
continue;
264-
XmlDataDocument imageset = new XmlDataDocument();
287+
XmlDocument imageset = new XmlDocument();
265288
imageset.Load(file.FullName);
266289

267290
XmlElement root = imageset.DocumentElement;
@@ -296,7 +319,7 @@ private void ButtonImageSets_Click(object sender, EventArgs e)
296319
cropRect,
297320
GraphicsUnit.Pixel);
298321
}
299-
target.Save(Path.Combine(outPath, entry.GetAttribute("name") + ".png"));
322+
target.Save(Path.Combine(outPath, entry.GetAttribute("name").Trim('\n') + ".png"));
300323
}
301324
}
302325
AddToLog("Done", Color.White);

IndexHelper.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
using System;
22
using System.IO;
33
using System.IO.Compression;
4+
using System.Runtime.InteropServices;
5+
using System.Runtime.InteropServices.ComTypes;
46
using System.Text;
57
using System.Xml;
68

79
namespace TroubleTool
810
{
911
static class IndexHelper
1012
{
13+
static bool zipped;
14+
1115
public static XmlDocument LoadIndex(String path)
1216
{
1317
byte[] data = File.ReadAllBytes(path);
1418
data = Crypt.Decrypt(data);
1519
// check if the file is a zip file
1620
if ((data[0] == 0x50) && (data[1] == 0x4b))
1721
{
22+
zipped = true;
1823
byte[] tempData;
1924
using (var stream = new MemoryStream(data))
2025
{
@@ -30,6 +35,10 @@ public static XmlDocument LoadIndex(String path)
3035
}
3136
data = tempData;
3237
}
38+
else
39+
{
40+
zipped = false;
41+
}
3342

3443
int i = data.Length - 1;
3544
while (data[i] == 0)
@@ -45,9 +54,6 @@ public static XmlDocument LoadIndex(String path)
4554

4655
public static void SaveIndex(XmlDocument doc, String path)
4756
{
48-
// make sure that that index file is flagged as modded
49-
doc.DocumentElement.SetAttribute("mod", "true");
50-
5157
byte[] data_enc = null;
5258
using (var stream = new MemoryStream())
5359
{
@@ -59,7 +65,24 @@ public static void SaveIndex(XmlDocument doc, String path)
5965
}
6066
data_enc = stream.ToArray();
6167
}
68+
if (zipped)
69+
{
70+
using (var stream = new MemoryStream())
71+
{
72+
using (ZipArchive z = new ZipArchive(stream, ZipArchiveMode.Create, true))
73+
{
74+
ZipArchiveEntry entry = z.CreateEntry("index");
75+
using (var entryStream = entry.Open())
76+
{
77+
entryStream.Write(data_enc, 0, data_enc.Length);
78+
}
79+
}
80+
data_enc = stream.ToArray();
81+
}
82+
}
6283
data_enc = Crypt.Encrypt(data_enc);
84+
85+
6386
File.WriteAllBytes(path, data_enc);
6487
}
6588
}

0 commit comments

Comments
 (0)