-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwz_lib-20240701.js
1014 lines (989 loc) · 26.6 KB
/
wz_lib-20240701.js
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const WzCrypto = (function(){
const AES_KEY ='130000000800000006000000B40000001B0000000F0000003300000052000000';
const IV = 'B97D63E9B97D63E9B97D63E9B97D63E9';
const aesData = {
lastHexStr: "",
cipherMask: [],
};
return {
/**
Field
*/
getCipherMask:function(){
return aesData.cipherMask;
},
setIV:function(iv){
if(iv == undefined || iv == "" || iv.length != 16){
console.warn(`iv must be exist and iv.length == 16`);
return ;
}
IV = iv;
return ;
},
/**
Methods
*/
encryptAes256Ecb: function (hexStr) {
const key = CryptoJS.enc.Hex.parse(AES_KEY);
const dataWordArray = CryptoJS.enc.Hex.parse(hexStr);
const encrypted = CryptoJS.AES.encrypt(
dataWordArray,
key,
{
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.NoPadding
});
return CryptoJS.enc.Hex.stringify(encrypted.ciphertext);
//return this.wordArrayToUint8Array(encrypted.ciphertext);
},
wordArrayToUint8Array:function(wordArray) {
let len = wordArray.sigBytes;
let u8Array = new Uint8Array(len);
for (let i = 0; i < len; i++) {
u8Array[i] = wordArray.words[i >>> 2] >>> ((i % 4) * 8) & 0xff;
}
return u8Array;
},
ensureSize:function(len){
if(len <= 0){
console.warn(`ensureSize len must be > 0, len:${len}`);
return;
}
const curLen = aesData.cipherMask.length;
if(curLen >= len){
return;
}
let hexStr = IV;
if(curLen > 0){
hexStr = aesData.lastHexStr;
}
let n = Math.ceil(len / 16) * 16;
for(let i = curLen; i < n; i+=16){
hexStr = this.encryptAes256Ecb(hexStr);
for(let j = 0; j < hexStr.length; j+=2){
let d = parseInt(hexStr.charAt(j),16) * 16 + parseInt(hexStr.charAt(j+1),16);
aesData.cipherMask.push(d);
}
}
aesData.lastHexStr = hexStr;
return ;
},
decrypt:function(arr){
if(arr.length <= 0){
console.warn(`decrypto arr is empty, arr.length:${arr.length}`);
return;
}
this.ensureSize(arr.length);
const cipherMask = aesData.cipherMask;
for(let i = 0; i < arr.length; i++){
arr[i] ^= cipherMask[i];
}
return ;
},
xorAA: function(arr){
if(arr.length <= 0){
console.warn(`xorAA arr is empty, arr.length:${arr.length}`);
return;
}
this.ensureSize(arr.length);
const cipherMask = aesData.cipherMask;
let a = 0xAA;
for(let i = 0; i < arr.length; i++){
arr[i] = a ^ arr[i] ^ cipherMask[i];
a = (a + 1) & 0xFF;
}
return ;
},
xorAAAA: function(arr){
if(arr.length <= 0){
console.warn(`xorAAAA arr is empty, arr.length:${arr.length}`);
return;
}
this.ensureSize(arr.length);
const cipherMask = aesData.cipherMask;
let a = 0xAAAA;
for(let i = 0; i < arr.length; i+=2){
arr[i] = ((a & 0xFF) ^ arr[i] ^ cipherMask[i]) & 0xFF;
arr[i + 1] = (((a >> 8) & 0xFF) ^ arr[i + 1] ^ cipherMask[i + 1]) & 0xFF;
a = (a + 1) & 0xFFFF;
}
return ;
},
};
})();
const WzFile = (function(){
/**
wzDataMap['Base.wz']={
data:[],
pos:0,
};
*/
let wzDataMap = {};
let curWzData;
let curWzName;
let VERSION = 79;
let HASH_VERSION;
let soundMap = {};
let pngMap = {};
return {
/**
Fields
*/
getCurWzData:function(){
return curWzData;
},
getWzDataMap:function(){
return wzDataMap;
},
getSoundMap:function(offset){
if(offset){
return soundMap[offset];
}
return soundMap;
},
getPngMap:function(offset){
if(offset){
return pngMap[offset];
}
return pngMap;
},
getImgProps: function(name){
if(!name.endsWith(".img")){
console.log("不是img文件",name);
return;
}
const idx = name.lastIndexOf("/");
if(idx == -1)return;
let preName = name.substr(0, idx);
let sufName = name.substr(idx + 1);
return this.getLayerDataMap(preName)["img"][sufName].props;
},
/**
Base.wz:{
dir:{
Map:{
dir:{
Map0:{
dir
img
offset
...
}
}
}
}
@param Base.wz/Map/Map0
@return Map0;
*/
getLayerDataMap:function(name){
let arr = name.split("/");
let len = arr.length;
let data = wzDataMap[curWzName];
for(let i = 1; i < len; i++){
data = data.dir[arr[i]];
}
return data;
},
getImgDataMap: function(name){
return this.getLayerDataMap(name).img;
},
getDirDataMap: function(name){
return this.getLayerDataMap(name).dir;
},
setCurWzPos:function(pos){
if(typeof(pos) != "number"){
return false;
}
curWzData.pos = pos;
return true;
},
setCurWzData:function(wzName){
if(wzName == undefined || wzName == ""){
console.warn(`wzName is empty. wzName: ${wzName}`);
return false;
}
curWzData = wzDataMap[wzName];
curWzName = wzName;
return true;
},
setVersion:function(v){
if(typeof(v) != "number" || v <= 0){
console.warn(`v is must be number and v > 0.`);
return false;
}
VERSION = v;
return true;
},
/**
Methods
*/
readFile: async function(fileHandle){
async function readFileInChunks(fileHandle, chunkSize = 1024 * 1024) { // 1MB chunk size by default
const file = await fileHandle.getFile();
const chunks = [];
const readChunk = async (offset) => {
const reader = new FileReader();
const blob = file.slice(offset, offset + chunkSize);
return new Promise((resolve, reject) => {
reader.onloadend = () => {
const chunkArrayBuffer = reader.result;
const u8Array = new Uint8Array(chunkArrayBuffer);
//const dataView = new DataView(chunkArrayBuffer);
//console.log(dataView);
chunks.push(u8Array);
resolve();
};
reader.onerror = reject;
reader.readAsArrayBuffer(blob);
});
};
for (let offset = 0; offset < file.size; offset += chunkSize) {
await readChunk(offset);
}
return chunks;
}
const res = await readFileInChunks(fileHandle);
wzDataMap[fileHandle.name] = {
data: res,
pos: 0
};
if(res.length > 0){
return true;
}
return false;
},
calVersion: function(){
let hash = BigInt(0);
const versionStr = VERSION.toString();
for (const digitChar of versionStr) {
const digit = parseInt(digitChar, 10);
hash = (hash * BigInt(32)) + BigInt(digit) + BigInt(49);
}
let hashBytes = [];
while (hash > 0n) {
hashBytes.unshift(hash & 0xffn);
hash >>= 8n;
}
while (hashBytes.length < 4) {
hashBytes.unshift(0n);
}
hash = 0n;
for (const b of hashBytes) {
hash = (hash << 8n) | b;
}
return Number(hash);
},
calOffset: function(offset, hash){
let imgOffset = BigInt(offset);
let hashVersion = BigInt(HASH_VERSION);
imgOffset = (imgOffset ^ 0xFFFFFFFFn) & 0xFFFFFFFFn;
imgOffset = (imgOffset * hashVersion) & 0xFFFFFFFFn;
imgOffset = (imgOffset - 0x581C3F6Dn) & 0xFFFFFFFFn;
let s = imgOffset & 0x1Fn;
imgOffset = (((imgOffset << s) & 0xFFFFFFFFn) | (imgOffset >> (32n - s))) & 0xFFFFFFFFn;
imgOffset = (imgOffset ^ BigInt(hash)) & 0xFFFFFFFFn;
imgOffset = (imgOffset + 120n) & 0xFFFFFFFFn;
return Number(imgOffset);
},
/**
string
*/
readStringAt: function(offset){
let oldPos = curWzData.pos;
curWzData.pos = offset;
let str = this.readWzString();
//stringTable[offset] = str;
curWzData.pos = oldPos;
return str;
},
readStringByOffset: function(offset){
let b = this.readInt8();
switch (b)
{
case 0x00:
case 0x73:
return this.readWzString();
case 0x01:
case 0x1B:
return this.readStringAt(offset + Number(this.readInt32()));
case 0x04:
curWzData.pos += 8;
break;
default:
console.log("读取字符串错误 在:" + curWzName + " " + curWzData.pos);
}
return "";
},
readStringAscii: function(len){
let arr = this.readByte(len);
return String.fromCodePoint(...arr);
},
readStringU8Arr: function(u8Arr){
return String.fromCodePoint(...u8Arr);
},
readStringU16Arr: function(u8Arr){
let u16Arr = [];
for(let i = 0; i < u8Arr.length; i+= 2){
let d = u8Arr[i] + (u8Arr[i + 1] << 8);
u16Arr.push(d);
}
return String.fromCodePoint(...u16Arr)
},
/**
wz type
*/
readWzString: function(){
let len = this.readInt8();
return this.readWzStringByLen(len);
},
readWzStringByLen: function(len){
if(len == 0){
return "";
}
let l = 0, arr, res, str;
if(len > 0 && len <= 127){
if(len < 127){
l = BigInt(len);
}else{
l = this.readInt32();
}
arr = this.readByte(l * 2n);
WzCrypto.xorAAAA(arr);
str = this.readStringU16Arr(arr);
}else{
if(len == 128){
l = this.readInt32();
}else{
l = 256 - len;
}
arr = this.readByte(l);
WzCrypto.xorAA(arr);
str = this.readStringU8Arr(arr);
}
return str;
},
readWzInt64: function(){
let len = this.readInt8();
if(len == 128){
return this.readInt64();
}
return len < 128? len: len - 256;
},
readWzInt32: function(){
let len = this.readInt8();
if(len == 128){
return this.readInt32();
}
return len < 128? len: len - 256;
},
readWzInt16: function(){
let len = this.readInt8();
if(len == 128){
return this.readInt16();
}
return len < 128? len: len - 256;
},
readWzInt8: function(){
let len = this.readInt8();
return len < 128? len: len - 256;
},
readWzFloat32: function(){
let len = this.readInt8();
if(len == 128){
return this.readFloat32();
}
return len < 128? len: len - 256;
},
/**
file stream flow
*/
readByte: function(len){
if(curWzName == undefined){
console.log("The wz file has not been read yet.");
return;
}
let l = Number(len);
if(l < 1){
console.log(`len: ${len}, offset: ${curWzData.pos}`);
return ;
}
const data = curWzData.data;
let m = data.length;
let n = data[0].length;
let res = [];
let i = Math.floor(curWzData.pos / n),j = curWzData.pos - i * n;
for(; i < m; i++){
for(; j < data[i].length; j++){
if(l == 0)break;
l--;
curWzData.pos++;
res.push(data[i][j]);
}
j = 0;
if(l == 0)break;
}
return res;
},
readInt64: function(){
return this.readInt(8);
},
readInt32: function(){
return Number(this.readInt(4));
},
readInt16: function(){
return Number(this.readInt(2));
},
readInt8: function(){
return Number(this.readByte(1)[0]);
},
readInt: function(len){
let arr = this.readByte(len);
let res = BigInt(arr[0]);
for(let i = 1; i < arr.length; i++){
res += BigInt(arr[i] << (i * 8));
}
return res;
},
readFloat64: function(){
let arr = this.readByte(8);
let intValue = 0n;
for(let i = 0; i < 8; i++){
intValue |= (BigInt(arr[i]) << (56n - BigInt(i * 8)));
}
const buffer = new ArrayBuffer(8);
const view = new DataView(buffer);
view.setBigUint64(0, intValue, true);// true小端
return view.getFloat64(0).toFixed(2);
},
readFloat32:function(){
let arr = this.readByte(4);
let intValue = 0n;
for(let i = 0; i < 4; i++){
intValue |= (BigInt(arr[i]) << (24n - BigInt(i * 8)));
}
const buffer = new ArrayBuffer(4);
const view = new DataView(buffer);
view.setUint32(0, Number(intValue), true);// true小端
return view.getFloat32(0).toFixed(2);
},
/**
wz file operate
*/
initData: function(name, handleFile){
let _this = this;
if(!wzDataMap){
wzDataMap = {};
}
return new Promise((resolve, reject) => {
// 加载wz
if(name && wzDataMap[name]){
_this.setCurWzData(name);
_this.getHeader();
resolve(1);
return;
}
const start = Date.now();
const promise = this.readFile(handleFile);
promise.then((res)=>{
console.log(`initData name:${name}, res: ${res}, time: ${(Date.now() - start)} ms`);
_this.setCurWzData(name);
_this.getHeader();
resolve(2);
});
});
},
destroy:function(){
wzDataMap = null;
},
getHeader: function(){
curWzData.pos = 0;
let signature = this.readStringAscii(4);
if(signature !== 'PKG1'){
console.log('This is not header of wz file.');
return;
}
let dataSize = this.readInt64();
let headerSize = this.readInt32();
let copyright = this.readStringAscii(headerSize - curWzData.pos);
this.readByte(2);//C2 00
HASH_VERSION = this.calVersion();
let headerData = {
signature: signature,
dataSize: dataSize,
headerSize: headerSize,
copyright: copyright,
};
curWzData.headerData = headerData;
let dir = {}, img = {};
curWzData.dir = dir;
curWzData.img = img;
this.getDirList(dir, img, curWzName, headerData.headerSize + 2);
//console.log(headerData);
},
getDirList(dir, img, name, pos){
let headerData = curWzData.headerData;
curWzData.pos = pos;
let subCount = this.readWzInt32();// 62
for(let i = 0; i < subCount; i++){
let type = this.readInt8();
let dirName;
switch(type){
case 0x02:
dirName = this.readStringAt(Number(headerData.headerSize) + 1 + Number(this.readInt32()));
case 0x03:
case 0x04:
if(dirName == undefined){
dirName = this.readWzString();
}
let imgSize = this.readWzInt32();
let checkSumSize = this.readWzInt32();
let imgOffset = curWzData.pos - Number(headerData.headerSize);
let hashOffset = this.readInt32();
let offset = this.calOffset(imgOffset, hashOffset);
if(dirName.endsWith(".img")){
img[dirName] = {
name: dirName,
size: imgSize,
checkSumSize: checkSumSize,
offset: offset,
};
}else{
dir[dirName] = {
name: dirName,
size: imgSize,
checkSumSize: checkSumSize,
offset: offset,
};
}
break;
default:
console.log("没有满足条件的类型");
}
}
},
// Base.wz/Etc
getDir: function(name){
let idx = name.lastIndexOf("/");
if(idx == -1){
console.log(`This is not subdir. ${name}`);
return;
}
let preName = name.substr(0, idx);
let sufName = name.substr(idx + 1);
let _this = this;
return new Promise((resolve, reject) => {
let data = _this.getLayerDataMap(preName)["dir"][sufName];
let offset = data.offset;
let dir = {}, img = {};
data.dir = dir;
data.img = img;
_this.getDirList(dir, img, name, offset);
resolve(1);
});
},
// Base.wz/smap.img
getImg: function(name){
return new Promise((resolve, reject) => {
let idx = name.lastIndexOf("/");
if(idx == -1){
console.log("不是img文件", name);
reject();
return;
}
let preName = name.substr(0, idx);
let sufName = name.substr(idx + 1);
let _this = this;
const data = _this.getLayerDataMap(preName)["img"][sufName];
if(data == undefined || data.size == undefined || !data.name.endsWith(".img")){
reject();
return;
}
//console.log(data,wzData);
// 保存原先的位置
let pos = curWzData.pos;
let n = BigInt(curWzData.data[0].length);
// 跳转到指定位置读取
curWzData.pos = data.offset;
let ppos = curWzData.pos;
const type = _this.readInt8();// 73
switch(type){
case 0x01:// lua img : 01 XX 00 00 00 00 00 OFFSET
_this.readInt32();
_this.readInt16();
break;
case 0x73:// img without offset
case 0x1B:
curWzData.pos = ppos;
data.props = [];
_this.extractImg(ppos, data.props, 0);
break;
}
// 还原
curWzData.pos = pos;
resolve(1);
return true;
});
},
extractImg:function(offset, parentNode, eob){
let entries;
let prop = this.readStringByOffset(offset);
//console.log(wzDataMap[curWzName].pos, offset, prop);
switch(prop){
case "Property":
let val = this.readInt16();//跳过 00 00
entries = this.readWzInt32();// 06
//console.log("Property entries",wzDataMap[curWzName].pos,entries);
parentNode.count = entries;
for(let i = 0; i < entries; i++){
let children = {};
parentNode.push(children);
this.extractValue(offset, children);
}
break;
case "Shape2D#Vector2D":
//new Wz_Vector(this.WzFile.ReadInt32(), this.WzFile.ReadInt32());
parentNode.value = {x: this.readWzInt32(), y: this.readWzInt32()};
break;
case "Canvas":
curWzData.pos++;
let t = this.readInt8();
if(t == 0x01){
curWzData.pos+=2;
entries = this.readWzInt32();
//console.log("Canvas entries", wzDataMap[curWzName].pos, entries);
for (let i = 0; i < entries; i++){
let children = [];
parentNode.push(children);
this.extractValue(offset, children);
}
}
let w = this.readWzInt32();
let h = this.readWzInt32();
let format = this.readWzInt32() + this.readInt8();
curWzData.pos+=4;
let bufSize = Number(this.readInt32());
let offs = curWzData.pos + 1;
parentNode.value = {width: w, height: h,format: format, bufSize: bufSize - 1, offset: offs };
curWzData.pos += bufSize;
pngMap[offs] = parentNode.value;
break;
case "Shape2D#Convex2D":
entries = this.readWzInt32();
//console.log("Shape2D#Convex2D entries", wzDataMap[curWzName].pos, entries);
for(let i = 0; i < entries; i++){
let children = [];
parentNode.push(children);
this.extractImg(offset, children, 0);
}
break;
case "Sound_DX8":
wzDataMap[curWzName].pos++;
let len = Number(this.readWzInt32());
let ms = this.readWzInt32();
let headerLen = eob - len - wzDataMap[curWzName].pos;
let header = this.readByte(headerLen);
parentNode.value = {offset: (eob - len), length: len, header: header, ms: ms};
curWzData.pos = eob;
soundMap[(eob-len)] = parentNode.value;
break;
case "UOL":
wzDataMap[curWzName].pos++;
//new Wz_Uol(this.WzFile.ReadString(offset, this.EncKeys));
parentNode.value = this.readStringByOffset(offset);
break;
default:
console.warn("不知道prop类型");
}
},
extractValue: function(offset, parentNode){
let temp = {};
temp.key = this.readStringByOffset(offset);//00 FF 31
//console.log("parentStr",wzDataMap[curWzName].pos, offset, temp.key);
let type = this.readInt8();//09
//console.log("type",wzDataMap[curWzName].pos,type);
switch(type){
case 0x00:
temp.value = null;
temp.type = "";
break;
case 0x02:
case 0x0B:
temp.value = this.readInt16();
temp.type = "Int16";
break;
case 0x03:
case 0x13:
// case 0x14:
temp.value = this.readWzInt32();
temp.type = "WzInt32";
break;
case 0x14:
temp.value = this.readWzInt64();
temp.type = "WzInt64";
break;
case 0x04:
temp.value = this.readWzFloat32();
temp.type = "WzFloat32";
break;
case 0x05:
temp.value = this.readFloat64();
temp.type = "Float64";
break;
case 0x08:
temp.value = this.readStringByOffset(offset);
temp.type = "String";
break;
case 0x09:
parentNode.children = [];
let eob = this.readInt32() + curWzData.pos;
this.extractImg(offset, parentNode.children, eob);
break;
default:
throw new Error("读取值错误." + type + " at Offset: " + wzDataMap[curWzName].pos);
}
parentNode.key = temp.key;
parentNode.value = temp.value;
parentNode.type = temp.type;
//console.log("temp",temp);
},
};
})();
const WzSound = (function(){
return {
extractSound:function(offset){
let curWzData = WzFile.getCurWzData();
let sound = WzFile.getSoundMap(offset);
if(!offset || !sound ||
!sound.header ||
sound.header.length <= 51){
console.log("The sound data is invaild!");
return;
}
this.tryDecryptHeader(offset);
this.trySoundType(offset);
curWzData.pos = offset;
switch(sound.type){
case "Mp3":
sound.data = WzFile.readByte(sound.length);
break;
case "WavRaw":
console.log("WavRaw: Waiting for development!");
break;
}
},
trySoundType:function(offset){
let sound = WzFile.getSoundMap(offset);
if(sound.header == undefined){
sound.type = "Mp3";
}else{
switch(sound.header.length){
default:
case 0x52:
sound.type = "Mp3";
break;
case 0x46:
if(this.getFrequency(offset) == sound.length && sound.ms == 1000){
sound.type = "Binary";
}else{
sound.type = "WavRaw";
}
break;
}
}
},
getFrequency:function(offset){
let sound = WzFile.getSoundMap(offset);
if(sound.header == undefined || sound.header.length < 0x3c){
return 0;
}
let ans = 0;
for(let i = 56; i < 59; i++){
ans = ((ans + sound.header[i]) << 8);
}
ans += sound.header[59];
return ans;
},
tryDecryptHeader:function(offset){
let sound = WzFile.getSoundMap(offset);
let waveFormatLen = sound.header[51];
if(sound.header.length != 52 + waveFormatLen){
console.log("The sound data length is error!");
return ;
}
let cbSize = (sound.header[69] << 8) + sound.header[68];
if(cbSize + 18 != waveFormatLen){
let tempHeader = [];
for(let i = 0; i < waveFormatLen; i++){
tempHeader.push(sound.header[i + 52]);
}
this.ensureSize(waveFormatLen);
const cipherMask = WzCrypto.getCipherMask();
for(let i = 0; i < waveFormatLen; i++){
tempHeader[i] ^= cipherMask[i];
}
cbSize = (tempHeader[17] << 8) + tempHeader[16];
if(cbSize + 18 == waveFormatLen){
for(let i = 0; i < waveFormatLen; i++){
sound.header[i + 52] = tempHeader[i];
}
}
}
},
playSound:function(offset){
let sound = WzFile.getSoundMap(offset);
const typedArray = new Uint8Array(sound.data);
const blob = new Blob([typedArray.buffer], { type: 'audio/mpeg' });
const url = URL.createObjectURL(blob);
const audioPlayer = document.getElementById('audioPlayer');
audioPlayer.src = url;
audioPlayer.play().catch(error => {
console.error('Error playing audio:', error);
});
// 清理工作:在不再需要URL时释放它,避免内存泄漏
audioPlayer.addEventListener('ended', () => {
URL.revokeObjectURL(url);
});
},
};
})();
const WzPng = (function(){
return {
getImgRawData:function(offset){
let png = WzFile.getPngMap(offset);
if(!offset || !png){
return;
}
let curWzData = WzFile.getCurWzData();
curWzData.pos = offset;
let type = WzFile.readInt16();
let inflator;
if(type == 0x9C78){
let arr = WzFile.readByte(png.bufSize - 2);
arr.unshift(0x9C);arr.unshift(0x78);
inflator = new pako.Inflate();
inflator.push(new Uint8Array(arr));
}else{
curWzData.pos-=2;
let arr = [];
let endPosition = png.bufSize + curWzData.pos;
while(curWzData.pos < endPosition){
let blockSize = Number(WzFile.readInt32());
if(curWzData.pos + blockSize > endPosition){
throw new Error(`Wz_Png exceeds the declared data size. (data length: ${imgData.bufSize}, readed bytes: ${startIndex}, next block: ${blockSize})`);
}
let tArr = WzFile.readByte(blockSize);
WzCrypto.decrypt(tArr);
Array.prototype.push.apply(arr, tArr);
}
inflator = new pako.Inflate();
inflator.push(new Uint8Array(arr));
}
//console.log(inflator);
let code = inflator.chunks.push(inflator.strm.output);
let outArr = inflator.result;
/*let size = inflator.strm.total_out;
let outArr = new Uint8Array(size);
let i = 0, k = 0;
for(; i < inflator.chunks.length; i++){
for(let j = 0; j < inflator.chunks[i].length; j++){
if(k == size)break;
outArr[k] = inflator.chunks[i][j];
k++;
}
}*/
//格式
let res;
switch(png.format){
case 1:
res = this.getPixelDataBgra4444(outArr);
break;
case 3:
res = this.getPixelDataForm3(outArr, width, height);
break;
}
if(res){
png.data = res;
}
//this.displayImageOnCanvas(res, Number(png.width), Number(png.height));
},
showImgByRawData:function(u8Arr){
let blob = new Blob([u8Arr], {type: "image/png"});
let reader = new FileReader();
reader.onload = function(e) {
let imgElement = document.querySelector("#wzPng");
imgElement.src = e.target.result;
};
reader.readAsDataURL(blob);
},
displayImageOnCanvas: function(offset) {
let png = WzFile.getPngMap(offset);
let argb8888Data = png.data;
let width = Number(png.width), height = Number(png.height);
const canvas = document.getElementById('myCanvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
const imageData = ctx.createImageData(width, height);
for (let i = 0, j = 0; i < argb8888Data.length; i += 4, j += 4) {
imageData.data[j + 3] = argb8888Data[i + 3];
imageData.data[j + 0] = argb8888Data[i + 2];
imageData.data[j + 1] = argb8888Data[i + 1];
imageData.data[j + 2] = argb8888Data[i + 0];
}
ctx.putImageData(imageData, 0, 0, 0, 0, width, height);
},
//png format
getPixelDataBgra4444:function(u8Arr){
let u8argb = new Uint8Array(u8Arr.length * 2);
let p;
for(let i = 0; i < u8Arr.length; i++){
p = u8Arr[i] & 0x0F; p |= (p << 4); u8argb[i * 2] = (p & 0xFF);
p = u8Arr[i] & 0xF0; p |= (p >> 4); u8argb[i * 2 + 1] = (p & 0xFF);
}
return u8argb;
},
getPixelDataForm3:function(rawData, width, height){
let pixel = new Uint8Array(rawData.length * 4);
let w = Math.ceil(width / 4);
let h = Math.ceil(height / 4);
for(let y = 0; y < 0; y++){
for(let x = 0; x < w; x++){
let index = (x + y * w) * 2; //原像素索引
let index2 = x * 4 + y * width * 4; //目标像素索引
let p = (rawData[index] & 0x0F) | ((rawData[index] & 0x0F) << 4)
| ((rawData[index] & 0xF0) | ((rawData[index] & 0xF0) >> 4)) << 8
| ((rawData[index + 1] & 0x0F) | ((rawData[index + 1] & 0x0F) << 4)) << 16
| ((rawData[index + 1] & 0xF0) | ((rawData[index + 1] & 0xF0) >> 4)) << 24;
for (let i = 0; i < 4; i++){
if (x * 4 + i < width)
{
pixel[index2 + i] = p;
}
else
{
break;
}
}
}
//复制行
let srcIndex = y * width * 4 * 4;
let dstIndex = srcIndex + width * 4;
for (let j = 1; j < 4; j++)
{
if (y * 4 + j < height)
{
for(let u = 0; u < width * 4; u++){