forked from YSRKEN/HSP-Decompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HspDecoder.cs
214 lines (200 loc) · 6.5 KB
/
HspDecoder.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
211
212
213
214
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using KttK.HspDecompiler.DpmToAx;
using KttK.HspDecompiler.Ax2ToAs;
using KttK.HspDecompiler.Ax3ToAs;
namespace KttK.HspDecompiler
{
internal sealed class HspDecoder
{
private const string dictionaryFileName = "Dictionary.csv";
private static Hsp3Dictionary dictionary = null;
internal static bool Initialize()
{
string dictionaryPath = Program.ExeDir + dictionaryFileName;
try
{
dictionary = Hsp3Dictionary.FromFile(dictionaryPath);
}
catch
{
goto err;
}
if (dictionary != null)
{
HspConsole.WriteLog("load " + dictionaryFileName + ":succeeded");
return true;
}
err:
HspConsole.WriteLog("load " + dictionaryFileName + ":failed");
System.Windows.Forms.MessageBox.Show(dictionaryFileName + "の読込に失敗しました");
dictionary = null;
return false;
}
internal void DecompressDpm(BinaryReader reader, ListView dpmFileListView, string outputDir)
{
if (reader == null)
throw new ArgumentNullException();
if (dpmFileListView == null)
throw new ArgumentNullException();
dpmFileListView.Items.Clear();
global::KttK.HspDecompiler.HspConsole.Write("DPMヘッダーの開始位置を検索中...");
Undpm undpm = Undpm.FromBinaryReader(reader);
if (undpm == null)
throw new HspDecoderException("DPMヘッダーが見つかりません(HSPの実行ファイルではありません)");
if ((undpm.FileList == null) || (undpm.FileList.Count == 0))
throw new HspDecoderException("DPMにファイルが含まれていません");
int encryptCount = 0;
int fileCount = undpm.FileList.Count;
foreach (DpmFileState file in undpm.FileList)
{
string[] itemParams = new string[4];
itemParams[0] = file.FileName;
if (file.IsEncrypted)
{
itemParams[1] = "有";
#if AllowDecryption
//deHSP100 start.axは暗号ファイルに数えないことにする
if (!file.FileName.Equals("start.ax", StringComparison.OrdinalIgnoreCase))
#endif
encryptCount++;
}
else
itemParams[1] = "-";
itemParams[2] = string.Format("0x{0:X08}",file.FileOffset);
itemParams[3] = string.Format("0x{0:X08}",file.FileSize);
dpmFileListView.Items.Add(new ListViewItem(itemParams));
}
Thread.Sleep(0);
if ((fileCount - encryptCount) <= 0)
{
MessageBox.Show("すべてのファイルが暗号化されています", fileCount.ToString() + "ファイル中、" + encryptCount.ToString() + "ファイルが暗号化されています。", MessageBoxButtons.OK);
global::KttK.HspDecompiler.HspConsole.Write("展開中断");
return;
}
if (encryptCount > 0)
{
DialogResult result = MessageBox.Show("暗号化されたファイルがあります。" + Environment.NewLine + "暗号化されたファイルを無視して展開を続けますか?", fileCount.ToString() + "ファイル中、" + encryptCount.ToString() + "ファイルが暗号化されています。", MessageBoxButtons.YesNo);
if (result != DialogResult.Yes)
{
global::KttK.HspDecompiler.HspConsole.Write("展開中断");
return;
}
}
if (!Directory.Exists(outputDir))
{
try
{
Directory.CreateDirectory(outputDir);
}
catch
{
throw new HspDecoderException("ディレクトリ" + outputDir + "の作成に失敗しました");
}
}
byte[] buffer = null;
FileStream saveStream = null;
foreach (DpmFileState file in undpm.FileList)
{
if (file.IsEncrypted)
{
#if AllowDecryption
if (!file.FileName.Equals("start.ax", StringComparison.OrdinalIgnoreCase))
#endif
{
global::KttK.HspDecompiler.HspConsole.Write(file.FileName + "は暗号化されています");
continue;
}
}
string outputPath = outputDir + file.FileName;
if (File.Exists(outputPath))
{
global::KttK.HspDecompiler.HspConsole.Write(file.FileName + "と同名のファイルが既に存在します");
continue;
}
if (!undpm.Seek(file))
{
global::KttK.HspDecompiler.HspConsole.Write(file.FileName + "の開始位置に移動できませんでした");
continue;
}
buffer = reader.ReadBytes(file.FileSize);
#if AllowDecryption
if (file.IsEncrypted)
{
global::KttK.HspDecompiler.HspConsole.Write(file.FileName + "の復号中...");
KttK.HspDecompiler.DpmToAx.HspCrypto.HspCryptoTransform decrypter = KttK.HspDecompiler.DpmToAx.HspCrypto.HspCryptoTransform.CrackEncryption(buffer, dictionary, outputPath); // 変更
if (decrypter == null){
global::KttK.HspDecompiler.HspConsole.Write(file.FileName + "の復号に失敗しました");
continue;
}
buffer = decrypter.Decryption(buffer);
}
#endif
try{
saveStream = new FileStream(outputPath, FileMode.CreateNew, FileAccess.Write);
saveStream.Write(buffer,0,buffer.Length);
}
catch
{
global::KttK.HspDecompiler.HspConsole.Warning(file.FileName + "の保存に失敗しました");
}
finally{
if(saveStream != null)
saveStream.Close();
}
}
global::KttK.HspDecompiler.HspConsole.Write("展開終了");
}
internal void Decode(BinaryReader reader,string outputPath)
{
if (reader == null)
throw new ArgumentNullException();
if (dictionary == null)
throw new InvalidOperationException();
global::KttK.HspDecompiler.HspConsole.StartParagraph();
List<string> lines = null;
global::KttK.HspDecompiler.HspConsole.Write("逆コンパイル中...");
global::KttK.HspDecompiler.HspConsole.StartParagraph();
lines = getDecoder(reader).Decode(reader);
global::KttK.HspDecompiler.HspConsole.EndParagraph();
global::KttK.HspDecompiler.HspConsole.Write("逆コンパイル終了");
global::KttK.HspDecompiler.HspConsole.EndParagraph();
global::KttK.HspDecompiler.HspConsole.Write(Path.GetFileName(outputPath) + "に出力");
StreamWriter writer = null;
try
{
writer = new StreamWriter(outputPath, false, Encoding.GetEncoding("SHIFT-JIS"));
foreach (string line in lines)
writer.WriteLine(line);
global::KttK.HspDecompiler.HspConsole.Write("解析終了");
}
finally
{
if (writer != null)
writer.Close();
}
}
AbstractAxDecoder getDecoder(BinaryReader reader)
{
long startPosition = reader.BaseStream.Position;
char[] buffer = reader.ReadChars(4);
string bufStr = new string(buffer);
reader.BaseStream.Seek(startPosition, SeekOrigin.Begin);
if (bufStr.Equals("HSP2", StringComparison.Ordinal))
{
return new Ax2Decoder();
}
else if (bufStr.Equals("HSP3", StringComparison.Ordinal))
{
Ax3Decoder decoder = new Ax3Decoder();
decoder.Dictionary = dictionary;
return decoder;
}
throw new HspDecoderException("HSP2でもHSP3でもない形式");
}
}
}