-
Notifications
You must be signed in to change notification settings - Fork 5
/
AssetManager.cs
183 lines (163 loc) · 6.22 KB
/
AssetManager.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
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Xml;
using System.Linq;
using System.Xml.Linq;
using System.Drawing;
namespace TroubleTool
{
class AssetManager
{
internal XmlDocument indexXml;
internal String root;
internal String package;
internal String data;
internal String mods;
internal String backup;
internal String indexPath;
internal String indexBackUpPath;
public AssetManager(String gameRoot)
{
root = gameRoot;
package = Path.Combine(root, "Package");
package = Path.Combine(root, "Package");
data = Path.Combine(root, "Data");
mods = Path.Combine(root, "Mods");
indexPath = Path.Combine(package, "index");
indexBackUpPath = Path.Combine(package, "index_backup");
//LoadIndex();
}
internal void LoadIndex()
{
// load original index
indexXml = IndexHelper.LoadIndex(indexPath);
// check if the index file is modded, if so, load the backup
if (indexXml.DocumentElement.HasAttribute("mod"))
{
indexXml = IndexHelper.LoadIndex(indexBackUpPath);
}
else
{
if ((!File.Exists(indexBackUpPath)) || (new FileInfo(indexPath).Length != new FileInfo(indexBackUpPath).Length))
File.Copy(indexPath, indexBackUpPath, true);
};
}
internal void SaveIndex()
{
IndexHelper.SaveIndex(indexXml, indexPath);
}
internal void SaveIndex(XmlDocument modIndexXml)
{
IndexHelper.SaveIndex(modIndexXml, indexPath);
}
internal XmlNodeList GetEntries()
{
XmlElement root = indexXml.DocumentElement;
return root.ChildNodes;
}
internal List<XmlElement> FindUnExtractedAndUpdatedEntries()
{
List<XmlElement> toExtract = new List<XmlElement>();
foreach (XmlElement entry in GetEntries())
{
String src = Path.Combine(package, entry.GetAttribute("pack"));
String dst = Path.Combine(data, entry.GetAttribute("original"));
if (((!File.Exists(dst)) || (Int32.Parse(entry.GetAttribute("size")) != new FileInfo(dst).Length)) && (Int32.Parse(entry.GetAttribute("csize")) == new FileInfo(src).Length))
{
toExtract.Add(entry);
}
}
return toExtract;
}
internal List<XmlElement> BackUpPackageFiles(List<String> files)
{
return (from String file in files
let entry = indexXml.DocumentElement
let src = Path.Combine(package, entry.GetAttribute("pack"))
let dst = Path.Combine(data, entry.GetAttribute("original"))
where ((!File.Exists(dst)) || (Int32.Parse(entry.GetAttribute("size")) != new FileInfo(dst).Length)) && (Int32.Parse(entry.GetAttribute("csize")) == new FileInfo(src).Length)
select entry).ToList();
}
internal void ExtractAllEntries()
{
foreach (XmlElement entry in GetEntries())
{
ExtractEntry(entry);
}
}
internal void ExtractEntry(XmlElement entry)
{
String src = Path.Combine(package, entry.GetAttribute("pack"));
String dst = Path.Combine(data, entry.GetAttribute("original").Trim('\n'));
Extract(src, dst, entry);
}
public static void Extract(string src, String dst, XmlElement entry)
{
Directory.CreateDirectory(Path.GetDirectoryName(dst));
String method = entry.GetAttribute("method");
switch (method)
{
case "raw":
File.Copy(src, dst, true);
break;
case "zip":
using (ZipArchive z = ZipFile.OpenRead(src))
{
z.GetEntry((String)entry.GetAttribute("virtual")).ExtractToFile(dst, true);
}
break;
case "encrypted_zip":
byte[] data = File.ReadAllBytes(src);
data = Crypt.Decrypt(data);
using (var stream = new MemoryStream(data))
{
using (ZipArchive z = new ZipArchive(stream))
{
z.GetEntry((String)entry.GetAttribute("virtual")).ExtractToFile(dst, true);
}
}
break;
default:
break;
}
}
public static void Pack(string src, String dst, XmlElement entry)
{
String method = entry.GetAttribute("method");
switch (method)
{
case "raw":
File.Copy(src, dst, true);
break;
case "zip":
using (ZipArchive z = ZipFile.Open(dst, ZipArchiveMode.Create))
{
z.CreateEntryFromFile(src, (String)entry.GetAttribute("virtual"));
}
break;
case "encrypted_zip":
byte[] data;
using (var stream = new MemoryStream())
{
using (ZipArchive z = new ZipArchive(stream))
{
z.CreateEntryFromFile(src, (String)entry.GetAttribute("virtual"));
}
int pad = (int)(16 - (stream.Length % 16));
if (pad != 16)
{
stream.Write(new byte[pad], 0, pad);
}
data = stream.ToArray();
}
data = Crypt.Encrypt(data);
File.WriteAllBytes(dst, data);
break;
default:
break;
}
}
}
}