-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
225 lines (200 loc) · 4.93 KB
/
main.cpp
File metadata and controls
225 lines (200 loc) · 4.93 KB
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
215
216
217
218
219
220
221
222
223
224
225
// HCA → WAV
// hcadec -d -o "出力ファイル名" "入力ファイル名"
// WAV → HCA (未実装) (Not yet implemented)
// hcadec -o "出力ファイル名" "入力ファイル名"
//--------------------------------------------------
// インクルード
//--------------------------------------------------
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#else
#include <string.h>
#endif
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include "clHCA.h"
#include "Path.h"
//--------------------------------------------------
// ~~~~~~~~~~~~~SCREW THE RULES~~~~~~~~~~~~~~~~~~~
//--------------------------------------------------
using namespace std;
bool isList = false;
bool deleteSource = false;
inline bool StringCompare(const char* str1, const char* str2) { return (strcmp(str1, str2) == 0); }
uint32_t ciphKey1 = 0x30DBE1AB;
uint32_t ciphKey2 = 0xCC554639;
//--------------------------------------------------
// 文字列を16進数とみなして数値に変換
//--------------------------------------------------
int32_t atoi16(const char *s)
{
int32_t r = 0;
while (*s)
{
if (*s >= '0' && *s <= '9')
{
r <<= 4;
r |= *s - '0';
}
else if (*s >= 'A' && *s <= 'F')
{
r <<= 4;
r |= *s - 'A' + 10;
}
else if (*s >= 'a' && *s <= 'f')
{
r <<= 4;
r |= *s - 'a' + 10;
}
else
{
break;
}
s++;
}
return r;
}
//--------------------------------------------------
// HCA → WAV
//--------------------------------------------------
bool HCAtoWAV(const char *filenameIn, const char *filenameOut, uint32_t pciphKey1, uint32_t pciphKey2)
{
// HCAファイルをデコード
clHCA hca(pciphKey1, pciphKey2);
if (!hca.Decode(filenameIn, filenameOut))
return false;
return true;
}
void Decode(const std::string& filenameIn, const std::string& filenameOut, bool list)
{
if (list)
{
ifstream listFile(filenameIn);
if (listFile.is_open())
{
for (string s; getline(listFile, s);)
Decode(s, filenameOut, false);
listFile.close();
}
else
return;
}
else
{
// 入力チェック
if (filenameIn.empty())
{
//wprintf(L"Error: 入力ファイルを指定してください。");
cout << "Error: Invalid input filename." << endl;
#ifdef _WIN32
system("pause");
#endif
return;
}
std::string fileOut;
// デフォルト出力ファイル名
if (filenameOut.empty())
{
fileOut = Path::Directory(filenameIn) + Path::Filename(filenameIn, false) + ".wav";
}
else
fileOut = filenameOut;
cout << "Decoding: " << Path::Filename(filenameIn) << " as " << Path::Filename(fileOut) << endl;
// デコード
if (!HCAtoWAV(filenameIn.c_str(), fileOut.c_str(), ciphKey1, ciphKey2))
{
//wprintf(L"Error: HCAファイルのデコードに失敗しました。");
cout << "Error: HCA file decode has failed." << endl << endl;
return;
}
else if (deleteSource)
{
cout << "Deleting " << filenameIn << "..." << endl;
remove(filenameIn.c_str());
}
cout << endl;
}
}
//--------------------------------------------------
// メイン
//--------------------------------------------------
int32_t main(int32_t argc, char* argv[])
{
int32_t result = 0;
// This stuff speeds up std::cout by quite a bit
if (setvbuf(stdout, 0, _IOLBF, 4096) != 0)
abort();
if (setvbuf(stderr, 0, _IOLBF, 4096) != 0)
abort();
// コマンドライン解析
std::string filenameIn;
std::string filenameOut;
if (argc > 1)
{
for (int32_t i = 1; i < argc; i++)
{
if (StringCompare(argv[i], "--out") || StringCompare(argv[i], "-o"))
{
filenameOut = argv[++i];
continue;
}
else if (StringCompare(argv[i], "--list") || StringCompare(argv[i], "-l"))
{
isList = !isList;
cout << "List mode " << ((isList) ? "enabled." : "disabled.") << endl;
continue;
}
else if (StringCompare(argv[i], "--delete") || StringCompare(argv[i], "-d"))
{
deleteSource = !deleteSource;
cout << "Delete mode " << ((deleteSource) ? "enabled." : "disabled.") << endl;
continue;
}
else if (StringCompare(argv[i], "--ciphA"), StringCompare(argv[i], "-a"))
{
ciphKey1 = atoi16(argv[++i]);
continue;
}
else if (StringCompare(argv[i], "--ciphB"), StringCompare(argv[i], "-b"))
{
ciphKey2 = atoi16(argv[++i]);
continue;
}
else
{
filenameIn = argv[i];
Decode(filenameIn, filenameOut, isList);
filenameOut = "";
}
}
}
else
{
cout << "Usage:" << endl;
cout << '\t' << Path::Filename(argv[0]) << " [parameters] file [parameters] file2 [...]" << endl;
cout << "\nParameters:" << endl;
cout << "\t-o"
<< "\tSets a custom output filename for the next file."
<< endl;
cout << "\t-d"
<< "\tDelete the source file after successful conversion."
<< endl;
cout << "\t-l"
<< "\tTreats the next file as a list of files."
<< endl;
#ifdef _WIN32
system("pause");
#endif
return result;
}
#ifdef _DEBUG
#ifdef _WIN32
system("pause");
#endif
#endif
return result;
}