-
Notifications
You must be signed in to change notification settings - Fork 0
/
COD4_Compress.cs
210 lines (210 loc) · 7.99 KB
/
COD4_Compress.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
using System;
using System.Collections;
using System.Xml;
using System.Diagnostics;
using System.IO;
namespace ffManager
{
public class COD4_Compress
{
private ArrayList missing_files = new ArrayList();
private ArrayList overflow_files = new ArrayList();
private string fastfile;
private string console;
private string extractDir;
private string dumpDir;
private string hashDir;
private ArrayList process_files = new ArrayList();
private string DS = ffManager.MainClass.getOS() == "win32" ? @"\" : "/";
private XmlDocument offsets;
public COD4_Compress (string file, string console)
{
fastfile = file;
this.console = console;
}
public void compress(string dir, string xml)
{
offsets = new XmlDocument();
offsets.Load(xml);
extractDir = dir + DS + "scripts";
dumpDir = dir + DS + "raw";
hashDir = dir + DS + "hashes";
packData();
ArrayList process_files = new ArrayList();
Console.WriteLine("Compressing " + fastfile);
XmlNodeList files = offsets.GetElementsByTagName("file");
foreach(XmlNode file in files)
{
Console.WriteLine("Processing " + file.Attributes["name"].Value);
foreach(XmlNode part in file.ChildNodes)
{
if(!process_files.Contains(part.Attributes["name"].Value))
{
string source = locateDumpFile(part.Attributes["name"].Value);
if(source == "")
continue;
Process ps = new Process();
ps.StartInfo.CreateNoWindow = true;
ps.StartInfo.WindowStyle= ProcessWindowStyle.Hidden;
string comp = "";
if(ffManager.MainClass.console == "ps3")
{
comp = "-w -15";
}
else if(ffManager.MainClass.console == "xbox")
{
comp = "";
}
if(ffManager.MainClass.getOS() == "win32")
{
ps.StartInfo.FileName = MainClass.cwd + @"\packzip.exe";
ps.StartInfo.Arguments = "-o 0x" + part.Attributes["name"].Value + " " + comp + @" """ + dumpDir + DS + source + @""" " + @"""" + fastfile + @"""";
}
else if(ffManager.MainClass.getOS() == "unix")
{
ps.StartInfo.FileName = "wine";
ps.StartInfo.Arguments = @"""" + MainClass.cwd + @"/packzip.exe"" -o 0x" + part.Attributes["name"].Value + " " + comp + @" """ + dumpDir + DS + source + @""" " + @"""" + fastfile + @"""";
}
ps.Start();
ps.WaitForExit();
process_files.Add(part.Attributes["name"].Value);
}
}
}
}
private string locateDumpFile(string name)
{
DirectoryInfo files = new DirectoryInfo(dumpDir);
foreach(FileInfo finfo in files.GetFiles())
{
if(finfo.Name.Replace(finfo.Extension,"") == name)
return finfo.Name;
}
return "";
}
public ArrayList getMissingFiles()
{
return missing_files;
}
public ArrayList getOverflow()
{
return overflow_files;
}
private void packData()
{
XmlNodeList doc = offsets.GetElementsByTagName("file");
foreach(XmlNode file in doc)
{
long size = checkSize(file.Attributes["name"].Value,Convert.ToInt64(file.Attributes["size"].Value));
if(size != -1)
{
long pos = 0;
if(size > 0)
if(!hasChanged(file.Attributes["name"].Value))
continue;
fillPadding(file.Attributes["name"].Value,size);
foreach(XmlNode part in file.ChildNodes)
{
packPart(part,file.Attributes["name"].Value, pos);
pos += Convert.ToInt64(part.Attributes["endpos"].Value) - Convert.ToInt64(part.Attributes["startpos"].Value);
}
stripPadding(file.Attributes["name"].Value);
}
}
}
private void packPart(XmlNode part, string file, long foffset)
{
string source = locateDumpFile(part.Attributes["name"].Value);
if(source == "")
{
ArrayList data = new ArrayList();
data.Add(part.Attributes["name"].Value);
data.Add(file);
missing_files.Add(data);
return;
}
long spos = Convert.ToInt64(part.Attributes["startpos"].Value);
long epos = Convert.ToInt64(part.Attributes["endpos"].Value);
BinaryReader file_fhandle = new BinaryReader(File.OpenRead(extractDir + DS + file));
BinaryWriter source_fhandle = new BinaryWriter(File.OpenWrite(dumpDir + DS + source));
long size = epos - spos;
long len = 0;
try
{
source_fhandle.BaseStream.Seek(spos,SeekOrigin.Begin);
file_fhandle.BaseStream.Seek(foffset,SeekOrigin.Begin);
while(len <= size)
{
source_fhandle.BaseStream.WriteByte(file_fhandle.ReadByte());
len++;
}
}
catch(IOException ioex)
{
source_fhandle.Close();
file_fhandle.Close();
}
source_fhandle.Close();
file_fhandle.Close();
}
private long checkSize(string file, long size)
{
FileInfo finfo = new FileInfo(extractDir + DS + file);
if(finfo.Length > size)
{
ArrayList data = new ArrayList();
data.Add(file);
data.Add(size);
data.Add(finfo.Length - size);
overflow_files.Add(data);
return -1;
}
else
return size - finfo.Length;
}
private bool hasChanged(string file)
{
string md5hash = MainClass.GetMD5HashFromFile(extractDir + DS + file);
if(md5hash != File.ReadAllText(hashDir + DS + file + ".md5").Trim())
{
Console.WriteLine("File " + file + " has changed..");
File.WriteAllText(hashDir + DS + file + ".md5",md5hash);
return true;
}
else
{
Console.WriteLine("File " + file + " has NOT changed -- Skipping...");
return false;
}
}
private void fillPadding(string file, long num)
{
BinaryWriter fhandle= new BinaryWriter(File.Open(extractDir + DS + file,FileMode.Append,FileAccess.Write));
try
{
for(long i=0; i < num; i++)
{
fhandle.BaseStream.WriteByte(0x00);
}
}
catch(IOException ioex)
{
fhandle.Close();
}
fhandle.Close();
}
private void stripPadding(string file)
{
BinaryReader handler = new BinaryReader(File.Open(extractDir + DS + file, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite));
BinaryWriter handlew = new BinaryWriter(File.Open(extractDir + DS + file, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite));
while(handler.BaseStream.Position < handler.BaseStream.Length)
{
byte data = handler.ReadByte();
if(data != 0x00)
handlew.BaseStream.WriteByte(data);
}
handler.Close();
handlew.Close();
}
}
}