Skip to content

Commit

Permalink
1.1.1 - fix Mod usage
Browse files Browse the repository at this point in the history
  • Loading branch information
K0lb3 committed Nov 9, 2022
1 parent c26f1eb commit 82b8a25
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
15 changes: 9 additions & 6 deletions AssetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.IO.Compression;
using System.Xml;
using System.Linq;
using System.Xml.Linq;
using System.Drawing;

namespace TroubleTool
{
Expand Down Expand Up @@ -51,6 +53,11 @@ internal void SaveIndex()
IndexHelper.SaveIndex(indexXml, indexPath);
}

internal void SaveIndex(XmlDocument modIndexXml)
{
IndexHelper.SaveIndex(modIndexXml, indexPath);
}

internal XmlNodeList GetEntries()
{
XmlElement root = indexXml.DocumentElement;
Expand Down Expand Up @@ -87,18 +94,14 @@ internal void ExtractAllEntries()
{
foreach (XmlElement entry in GetEntries())
{
String src = Path.Combine(package, entry.GetAttribute("pack"));
String dst = Path.Combine(data, entry.GetAttribute("original"));
// TODO - check if it really has to be extracted
Console.WriteLine($"Extracting {src} to {dst}!");
Extract(src, dst, entry);
ExtractEntry(entry);
}
}

internal void ExtractEntry(XmlElement entry)
{
String src = Path.Combine(package, entry.GetAttribute("pack"));
String dst = Path.Combine(data, entry.GetAttribute("original"));
String dst = Path.Combine(data, entry.GetAttribute("original").Trim('\n'));
Extract(src, dst, entry);
}

Expand Down
29 changes: 26 additions & 3 deletions Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ private void ButtonApply_Click(object sender, EventArgs e)

foreach (FileInfo file in new DirectoryInfo(am.mods).GetFiles())
{
if (file.Extension.ToLower() != "zip")
{
AddToLog($"Ignoring {file.Name} as it's not a zip!", Color.Yellow);
continue;
}
AddToLog(file.Name, Color.White);
using (ZipArchive z = ZipFile.OpenRead(file.FullName))
{
Expand All @@ -146,8 +151,26 @@ private void ButtonApply_Click(object sender, EventArgs e)
}
}
}
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("index");
root.SetAttribute("mod", "true");
doc.AppendChild(root);
foreach (var entry in indexDict)
{
XmlElement child = doc.CreateElement("entry");
foreach (XmlAttribute attr in entry.Value.Attributes)
{
child.SetAttribute(attr.Name, attr.Value);
}

root.AppendChild(child);
}
am.SaveIndex(doc);
}
else
{
am.SaveIndex();
}
am.SaveIndex();
AddToLog("Done", Color.White);
}

Expand Down Expand Up @@ -261,7 +284,7 @@ private void ButtonImageSets_Click(object sender, EventArgs e)
{
if (!file.Name.EndsWith(".imageset"))
continue;
XmlDataDocument imageset = new XmlDataDocument();
XmlDocument imageset = new XmlDocument();
imageset.Load(file.FullName);

XmlElement root = imageset.DocumentElement;
Expand Down Expand Up @@ -296,7 +319,7 @@ private void ButtonImageSets_Click(object sender, EventArgs e)
cropRect,
GraphicsUnit.Pixel);
}
target.Save(Path.Combine(outPath, entry.GetAttribute("name") + ".png"));
target.Save(Path.Combine(outPath, entry.GetAttribute("name").Trim('\n') + ".png"));
}
}
AddToLog("Done", Color.White);
Expand Down
29 changes: 26 additions & 3 deletions IndexHelper.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Xml;

namespace TroubleTool
{
static class IndexHelper
{
static bool zipped;

public static XmlDocument LoadIndex(String path)
{
byte[] data = File.ReadAllBytes(path);
data = Crypt.Decrypt(data);
// check if the file is a zip file
if ((data[0] == 0x50) && (data[1] == 0x4b))
{
zipped = true;
byte[] tempData;
using (var stream = new MemoryStream(data))
{
Expand All @@ -30,6 +35,10 @@ public static XmlDocument LoadIndex(String path)
}
data = tempData;
}
else
{
zipped = false;
}

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

public static void SaveIndex(XmlDocument doc, String path)
{
// make sure that that index file is flagged as modded
doc.DocumentElement.SetAttribute("mod", "true");

byte[] data_enc = null;
using (var stream = new MemoryStream())
{
Expand All @@ -59,7 +65,24 @@ public static void SaveIndex(XmlDocument doc, String path)
}
data_enc = stream.ToArray();
}
if (zipped)
{
using (var stream = new MemoryStream())
{
using (ZipArchive z = new ZipArchive(stream, ZipArchiveMode.Create, true))
{
ZipArchiveEntry entry = z.CreateEntry("index");
using (var entryStream = entry.Open())
{
entryStream.Write(data_enc, 0, data_enc.Length);
}
}
data_enc = stream.ToArray();
}
}
data_enc = Crypt.Encrypt(data_enc);


File.WriteAllBytes(path, data_enc);
}
}
Expand Down

0 comments on commit 82b8a25

Please sign in to comment.