-
Notifications
You must be signed in to change notification settings - Fork 2
/
PFile.cs
202 lines (195 loc) · 7.22 KB
/
PFile.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
using JpmodLib.Utils;
using System;
using System.IO;
using zlib;
namespace DoubleFine
{
public class PFile
{
private const int ZINBUFSIZ = 32768;
private const int ZOUTBUFSIZ = 32768;
private HFile header;
public PFile(HFile header)
{
this.header = header;
}
public void Unpack(string hFilePath, string unpackPath)
{
using (BinaryReader binaryReader = new BinaryReader((Stream) new FileStream(this.getPath(Path.GetDirectoryName(hFilePath), Path.GetFileNameWithoutExtension(hFilePath) + ".~p"), FileMode.Open)))
{
ConsoleProgressBar consoleProgressBar = new ConsoleProgressBar(this.header.Header.FileCount);
try
{
consoleProgressBar.Start();
foreach (FileIndex file in this.header.Files)
{
binaryReader.BaseStream.Seek(file.Offset, SeekOrigin.Begin);
consoleProgressBar.PerformStep();
Directory.CreateDirectory(Path.GetDirectoryName(this.getPath(unpackPath, file.GetFilePath())));
using (MemoryStream memoryStream = new MemoryStream())
{
using (ZOutputStream zoutputStream = new ZOutputStream((Stream) memoryStream))
{
if (file.IsCompressed())
zoutputStream.Write(binaryReader.ReadBytes(file.Size), 0, file.Size);
else
memoryStream.Write(binaryReader.ReadBytes(file.Size), 0, file.Size);
memoryStream.Seek(0L, SeekOrigin.Begin);
if ((long) file.ContentSize < memoryStream.Length)
{
using (BinaryWriter binaryWriter = new BinaryWriter((Stream) new FileStream(this.getPath(unpackPath, file.GetFilePath() + ".header"), FileMode.Create)))
{
byte[] buffer = new byte[memoryStream.Length - (long) file.ContentSize];
memoryStream.Read(buffer, 0, buffer.Length);
binaryWriter.Write(buffer);
}
}
using (BinaryWriter binaryWriter = new BinaryWriter((Stream) new FileStream(this.getPath(unpackPath, file.GetFilePath()), FileMode.Create)))
{
int contentSize = file.ContentSize;
if (file.FileType.HasSizeHeader)
contentSize -= 4;
byte[] buffer = new byte[contentSize];
if (file.FileType.HasSizeHeader)
memoryStream.Read(buffer, 0, 4);
memoryStream.Read(buffer, 0, contentSize);
binaryWriter.Write(buffer);
}
}
}
}
}
finally
{
consoleProgressBar.End();
}
}
}
public void Pack(string path, string unpackPath, HFile hFile)
{
using (FileStream fileStream1 = new FileStream(path + ".~p", FileMode.Create))
{
ConsoleProgressBar consoleProgressBar = new ConsoleProgressBar(hFile.Header.FileCount);
try
{
consoleProgressBar.Start();
for (int index = 0; index < hFile.Files.Length; ++index)
{
consoleProgressBar.PerformStep();
using (MemoryStream memoryStream = new MemoryStream())
{
string path1 = unpackPath + "\\" + hFile.Files[index].GetFilePath();
if (File.Exists(path1 + ".header"))
{
using (FileStream fileStream2 = new FileStream(path1 + ".header", FileMode.Open))
this.CopyStream((Stream) fileStream2, (Stream) memoryStream);
}
using (FileStream fileStream2 = new FileStream(path1, FileMode.Open))
{
hFile.Files[index].ContentSize = (int) fileStream2.Length;
if (hFile.Files[index].FileType.HasSizeHeader)
{
memoryStream.Write(BitConverter.GetBytes(hFile.Files[index].ContentSize), 0, 4);
hFile.Files[index].ContentSize += 4;
}
this.CopyStream((Stream) fileStream2, (Stream) memoryStream);
}
memoryStream.Seek(0L, SeekOrigin.Begin);
hFile.Files[index].Offset = fileStream1.Position;
if (hFile.Files[index].IsCompressed())
{
int num = this.compress((Stream) memoryStream, (Stream) fileStream1, 9);
if (num < 0)
throw new Exception();
hFile.Files[index].Size = num;
}
else
{
hFile.Files[index].Size = (int) memoryStream.Length;
this.CopyStream((Stream) memoryStream, (Stream) fileStream1);
}
if ((int) hFile.Header.Version[1] == 1)
{
if (fileStream1.Length % 4L != 0L)
fileStream1.Write(new byte[4L - fileStream1.Length % 4L], 0, (int) (4L - fileStream1.Length % 4L));
}
else if (fileStream1.Length % 2048L != 0L)
{
int count = (int) (2048L * (fileStream1.Length / 2048L + 1L) - fileStream1.Length);
fileStream1.Write(new byte[count], 0, count);
}
}
}
hFile.Header.DataFooterOffset = (ulong) fileStream1.Position;
int num1 = (int) ushort.MaxValue - (int) (fileStream1.Length & (long) ushort.MaxValue);
for (int index = 0; index <= num1; ++index)
fileStream1.WriteByte((byte) 122);
}
finally
{
consoleProgressBar.End();
}
}
}
private int compress(Stream sin, Stream sout, int level)
{
ZStream zstream = new ZStream();
int length = (int) sin.Length;
byte[] buffer1 = new byte[length];
byte[] buffer2 = new byte[32768];
int num1 = 0;
int num2 = zstream.deflateInit(level);
if (num2 != 0)
return num2;
zstream.avail_in = 0;
zstream.avail_out = 32768;
zstream.next_out = buffer2;
int flush = 0;
int num3;
do
{
if (zstream.avail_in == 0)
{
int num4 = sin.Read(buffer1, 0, length);
zstream.next_in = buffer1;
zstream.avail_in = num4;
zstream.next_in_index = 0;
if (num4 < length)
flush = 4;
}
num3 = zstream.deflate(flush);
if (num3 < 0)
return num3;
if (zstream.avail_out == 0 || num3 == 1)
{
int count = 32768 - zstream.avail_out;
sout.Write(buffer2, 0, count);
num1 += count;
zstream.next_out = buffer2;
zstream.avail_out = 32768;
zstream.next_out_index = 0;
}
}
while (num3 != 1);
int num5 = zstream.deflateEnd();
if (num5 != 0)
return num5;
zstream.free();
return num1;
}
private string getPath(string basepath, string path)
{
if (basepath == null || basepath.Length == 0)
return path;
return basepath + "\\" + path;
}
private void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[32768];
int count;
while ((count = input.Read(buffer, 0, 32768)) > 0)
output.Write(buffer, 0, count);
output.Flush();
}
}
}