-
Notifications
You must be signed in to change notification settings - Fork 18
/
Main.cpp
367 lines (321 loc) · 7.75 KB
/
Main.cpp
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include "MemLeaks.h"
#include <assert.h>
#include <stdio.h>
#include <conio.h>
#include <locale.h>
#include "FileUt.h"
#include "WZ.h"
void DisplayData(WzNode * root)
{
if(WzProperty<WzNull> * pProp = dynamic_cast<WzProperty<WzNull>*>(root))
{
wprintf(L"** null\n");
}
else if(WzProperty<unsigned short> * pProp = dynamic_cast<WzProperty<unsigned short>*>(root))
{
wprintf(L"** ushort: [%u]\n", pProp->Get());
}
else if(WzProperty<int> * pProp = dynamic_cast<WzProperty<int>*>(root))
{
wprintf(L"** int: [%d]\n", pProp->Get());
}
else if(WzProperty<float> * pProp = dynamic_cast<WzProperty<float>*>(root))
{
wprintf(L"** float: [%f]\n", pProp->Get());
}
else if(WzProperty<double> * pProp = dynamic_cast<WzProperty<double>*>(root))
{
wprintf(L"** double: [%lf]\n", pProp->Get());
}
else if(WzProperty<std::wstring> * pProp = dynamic_cast<WzProperty<std::wstring>*>(root))
{
wprintf(L"** string: [%s]\n", pProp->Get().c_str());
}
else if(WzProperty<WzSubProp> * pProp = dynamic_cast<WzProperty<WzSubProp>*>(root))
{
wprintf(L"** sub property\n");
}
else if(WzProperty<WzCanvas> * pProp = dynamic_cast<WzProperty<WzCanvas>*>(root))
{
wprintf(L"** canvas: [%d, %d], fmt(%d) %s\n",
pProp->Get().m_Width, pProp->Get().m_Height,
pProp->Get().m_Format + pProp->Get().m_Format2,
pProp->Get().m_Encrypted ? L"Encrypted" : L"");
}
else if(WzProperty<WzVec2D> * pProp = dynamic_cast<WzProperty<WzVec2D>*>(root))
{
wprintf(L"** vector 2d: [%d, %d]\n", pProp->Get().m_X, pProp->Get().m_Y);
}
else if(WzProperty<WzConvex> * pProp = dynamic_cast<WzProperty<WzConvex>*>(root))
{
wprintf(L"** convex\n");
}
else if(WzProperty<WzSound> * pProp = dynamic_cast<WzProperty<WzSound>*>(root))
{
wprintf(L"** sound: [%d]\n", pProp->Get().m_Size);
}
else if(WzProperty<WzUOL> * pProp = dynamic_cast<WzProperty<WzUOL>*>(root))
{
wprintf(L"** link: [%s]\n", pProp->Get().m_UOL.c_str());
}
else if(WzDirectory * pProp = dynamic_cast<WzDirectory*>(root))
{
wprintf(L"** %s\n", pProp->IsImage() ? L"image" : L"directory");
}
else wprintf(L"** root\n");
}
void Win32_DrawPixels(HDC hDC,
const RECT & dstRC,
const POINT & srcPt,
void * pSrc, size_t len,
const POINT & szSrc,
WORD bitCount)
{
static BITMAPINFO s_origBMI =
{
{
sizeof(BITMAPINFO),
0, 0, 1, bitCount,
BI_RGB, 0, 0, 0, 0, 0
},
0
};
// 如果带 Alpha 通道, 则计算半透明值
if(bitCount == 32)
{
unsigned char * pBuf = (unsigned char*)pSrc;
for(size_t i = 0; i < len; i += 4)
{
// BGRA
float alp = pBuf[i + 3] / 255.f;
float inva = 1.0f - alp;
pBuf[i] *= alp;
pBuf[i + 1] *= alp;
pBuf[i + 2] *= alp;
// 混色
/*pBuf[i] += inva * 216.f;
pBuf[i + 1] += inva * 233.f;
pBuf[i + 2] += inva * 236.f;*/
}
}
BITMAPINFO bmi;
memmove(&bmi, &s_origBMI, sizeof(BITMAPINFO));
memmove(&bmi.bmiHeader.biWidth, &szSrc, sizeof(POINT));
SetDIBitsToDevice(hDC,
dstRC.left, dstRC.top, dstRC.right, dstRC.bottom,
srcPt.x, srcPt.y, 0, dstRC.bottom,
pSrc,
&bmi,
DIB_RGB_COLORS);
}
void DirLoop(BinReader & reader, WzNode * root)
{
bool bShowIdx = false;
int index = 0;
wchar_t strInput[256];
std::vector<std::wstring> strDirs;
strDirs.push_back(L"root");
while(1)
{
system("cls");
wprintf(L"-- [Dir]: ");
for(std::vector<std::wstring>::const_iterator dit = strDirs.begin();
dit != strDirs.end();
dit++)
{
wprintf(L"%s/", dit->c_str());
}
if(bShowIdx) wprintf(L"\tIndex: [%d]", index);
bShowIdx = false;
wprintf(L"\n");
const WzNodeMap & childs = root->GetChilds();
if(!childs.size())
{
WzDirectory * pdir = dynamic_cast<WzDirectory*>(root);
if(pdir && pdir->IsImage())
{
WzParseImage(reader, pdir, root);
}
}
DisplayData(root);
std::map<int, std::wstring> strIDmap;
int nid = 0;
if(!childs.size())
{
wprintf(L"-- [No child data, input \'/\' to back]\n");
}
else
{
for(WzNodeMap::const_iterator it = childs.begin();
it != childs.end();
it++)
{
wprintf(L"%d\t%s\n", nid, it->first.c_str());
strIDmap[nid] = it->first.c_str();
nid++;
}
}
wprintf(L"-- [Input]: ");
wscanf(L"%s", strInput);
_InputOK:
WzNodeMap::const_iterator fit = childs.find(strInput);
if(fit != childs.end())
{
if(!fit->second.size())
{
wprintf(L"-- [No any data]\n");
getch();
continue;
}
else if(fit->second.size() > 1)
{
wprintf(L"-- [Index (0~%d)]: ", fit->second.size() - 1);
wscanf(L"%d", &index);
if(index >= fit->second.size())
index = fit->second.size() - 1;
bShowIdx = true;
}
root = fit->second[index];
strDirs.push_back(strInput);
}
else if(!wcsicmp(strInput, L"/"))
{
WzNode * pnode = root->GetParent();
if(pnode)
{
WzDirectory * pdir = dynamic_cast<WzDirectory*>(root);
if(pdir && pdir->IsImage())
{
pdir->FreeChilds();
}
root = pnode;
strDirs.pop_back();
}
}
else if(!wcsicmp(strInput, L"/exit"))
{
#ifdef _DEBUG
printf("BinReader: %d, FileMapping: %d\n",
reader.m_ReadCount,
reader.GetFileMapping().m_ReadCount);
getch();
#endif
break;
}
else if(!wcsicmp(strInput, L"/d"))
{
WzProperty<WzCanvas> * pCanvas = dynamic_cast<WzProperty<WzCanvas>*>(root);
if(pCanvas)
{
FileMapping & fm = reader.GetFileMapping();
size_t szBuf = 0;
size_t bpp = 0;
void * pBuf = WzRebuildCanvas(fm, pCanvas->Get(), szBuf, bpp);
if(pBuf)
{
/*std::wstring strPath = L"./";
strPath += *strDirs.rbegin();
strPath += L".png";
file_save(strPath.c_str(), pBuf, szBuf);*/
POINT szSrc = { pCanvas->Get().m_Width, pCanvas->Get().m_Height };
RECT dstRc = { 0, 30, szSrc.x, szSrc.y };
POINT srcPt = { 0, 0 };
// 翻转图像
szSrc.y = -szSrc.y;
HWND hConWnd = GetConsoleWindow();
HDC hDC = GetDC(hConWnd);
system("cls");
printf("Press any key to back.");
Win32_DrawPixels(hDC, dstRc, srcPt, pBuf, szBuf, szSrc, bpp);
ReleaseDC(hConWnd, hDC);
free(pBuf);
getch();
}
}
else
{
WzProperty<WzSound> * pSound = dynamic_cast<WzProperty<WzSound>*>(root);
if(pSound)
{
FileMapping & fm = reader.GetFileMapping();
size_t rsize = 0;
void * pBuf = fm.Read(pSound->Get().m_Offset, pSound->Get().m_Size, &rsize);
if(pBuf)
{
std::wstring strPath = L"./";
strPath += *strDirs.rbegin();
strPath += L".mp3";
file_save(strPath.c_str(), pBuf, rsize);
}
}
}
}
else if(strInput[0] == L'/' && nid)
{
int tid = 0;
swscanf(strInput + 1, L"%d", &tid);
if(tid >= nid || tid < 0) goto _Error;
wcscpy(strInput, strIDmap[tid].c_str());
goto _InputOK;
}
else
{
_Error:
wprintf(L"-- [Error command]\n");
getch();
}
}
}
int wmain(int argc, wchar_t ** argv)
{
CHECK_MEM_LEAKS;
setlocale(LC_CTYPE, "chs");
wchar_t currDir[MAX_PATH];
size_t strDirLen = wcslen(argv[0]);
memmove(currDir, argv[0], (strDirLen + 1) * sizeof(wchar_t));
for(size_t i = strDirLen; i != 0; i--)
{
if(currDir[i] == L'\\' || currDir[i] == L'/')
{
currDir[i] = 0;
break;
}
}
SetCurrentDirectoryW(currDir);
#ifdef _DEBUG
GetCurrentDirectoryW(MAX_PATH, currDir);
wprintf(L"Current Dir: %s\n", currDir);
getch();
#endif
FileMapping fm;
BinReader reader(fm);
WzGenKeys(WzKMSKeyIV);
if(argc > 1) // 传入参数
{
if(fm.Open(argv[1]))
{
WzFileDesc fdesc;
WzNode * root = WzNode::New();
if(!WzParseFile(reader, fdesc, root))
{
std::vector<std::wstring> strList;
reader.Reset();
WzParseListFile(reader, strList);
for(std::vector<std::wstring>::const_iterator it = strList.begin();
it != strList.end();
it++)
{
wprintf(L"%s\n", it->c_str());
}
getch();
}
else
{
DirLoop(reader, root);
}
root->Free();
fm.Close();
}
}
return 0;
}