-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Decrypter.js
120 lines (101 loc) · 3.66 KB
/
Decrypter.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
function Decrypter() {
throw new Error('This is a static class');
}
Decrypter.hasEncryptedImages = false;
Decrypter.hasEncryptedAudio = false;
Decrypter._requestImgFile = [];
Decrypter._headerlength = 16;
Decrypter._xhrOk = 400;
Decrypter._encryptionKey = "";
Decrypter._ignoreList = [
"img/system/Window.png"
];
Decrypter.SIGNATURE = "5250474d56000000";
Decrypter.VER = "000301";
Decrypter.REMAIN = "0000000000";
Decrypter.checkImgIgnore = function(url){
for(var cnt = 0; cnt < this._ignoreList.length; cnt++) {
if(url === this._ignoreList[cnt]) return true;
}
return false;
};
Decrypter.decryptImg = function(url, bitmap) {
url = this.extToEncryptExt(url);
var requestFile = new XMLHttpRequest();
requestFile.open("GET", url);
requestFile.responseType = "arraybuffer";
requestFile.send();
requestFile.onload = function () {
if(this.status < Decrypter._xhrOk) {
var arrayBuffer = Decrypter.decryptArrayBuffer(requestFile.response);
bitmap._image.src = Decrypter.createBlobUrl(arrayBuffer);
bitmap._image.addEventListener('load', bitmap._loadListener = Bitmap.prototype._onLoad.bind(bitmap));
bitmap._image.addEventListener('error', bitmap._errorListener = bitmap._loader || Bitmap.prototype._onError.bind(bitmap));
}
};
requestFile.onerror = function () {
if (bitmap._loader) {
bitmap._loader();
} else {
bitmap._onError();
}
};
};
Decrypter.decryptHTML5Audio = function(url, bgm, pos) {
var requestFile = new XMLHttpRequest();
requestFile.open("GET", url);
requestFile.responseType = "arraybuffer";
requestFile.send();
requestFile.onload = function () {
if(this.status < Decrypter._xhrOk) {
var arrayBuffer = Decrypter.decryptArrayBuffer(requestFile.response);
var url = Decrypter.createBlobUrl(arrayBuffer);
AudioManager.createDecryptBuffer(url, bgm, pos);
}
};
};
Decrypter.cutArrayHeader = function(arrayBuffer, length) {
return arrayBuffer.slice(length);
};
Decrypter.decryptArrayBuffer = function(arrayBuffer) {
if (!arrayBuffer) return null;
var header = new Uint8Array(arrayBuffer, 0, this._headerlength);
var i;
var ref = this.SIGNATURE + this.VER + this.REMAIN;
var refBytes = new Uint8Array(16);
for (i = 0; i < this._headerlength; i++) {
refBytes[i] = parseInt("0x" + ref.substr(i * 2, 2), 16);
}
for (i = 0; i < this._headerlength; i++) {
if (header[i] !== refBytes[i]) {
throw new Error("Header is wrong");
}
}
arrayBuffer = this.cutArrayHeader(arrayBuffer, Decrypter._headerlength);
var view = new DataView(arrayBuffer);
this.readEncryptionkey();
if (arrayBuffer) {
var byteArray = new Uint8Array(arrayBuffer);
for (i = 0; i < this._headerlength; i++) {
byteArray[i] = byteArray[i] ^ parseInt(Decrypter._encryptionKey[i], 16);
view.setUint8(i, byteArray[i]);
}
}
return arrayBuffer;
};
Decrypter.createBlobUrl = function(arrayBuffer){
var blob = new Blob([arrayBuffer]);
return window.URL.createObjectURL(blob);
};
Decrypter.extToEncryptExt = function(url) {
var ext = url.split('.').pop();
var encryptedExt = ext;
if(ext === "ogg") encryptedExt = ".rpgmvo";
else if(ext === "m4a") encryptedExt = ".rpgmvm";
else if(ext === "png") encryptedExt = ".rpgmvp";
else encryptedExt = ext;
return url.slice(0, url.lastIndexOf(ext) - 1) + encryptedExt;
};
Decrypter.readEncryptionkey = function(){
this._encryptionKey = $dataSystem.encryptionKey.split(/(.{2})/).filter(Boolean);
};