-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSurrealegg_LinuxPatch.js
More file actions
233 lines (215 loc) · 7.33 KB
/
Copy pathSurrealegg_LinuxPatch.js
File metadata and controls
233 lines (215 loc) · 7.33 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
226
227
228
229
230
231
232
233
if (require("os").platform() === "linux") {
const fs = require("fs");
const path = require("path");
const cwd = process.cwd();
// 1. The game expects this env to be set, but it's not on linux
process.env.LOCALAPPDATA = process.env.HOME + "/.local/share";
/**
* Returns an actual path ignoring the case sensitivity.
* Throws an error if it finds nothing.
*
* @param {string} target
* @returns
*/
function realpath(target) {
target = path.normalize(target);
const sections = target.split("/").filter((s) => s !== "");
const basePath = path.isAbsolute(target) ? "" : cwd;
/**
* Try getting actual file/folder name inside a directory.
* Throws an error if it finds nothing.
*
* @param {string} current a file or a direcorty to be checked
* @param {string} directory
* @returns {string}
*/
function tryMatchPaths(current, directory) {
const lowerCase = current.toLowerCase();
for (const file of directory) {
if (file.toLowerCase() === lowerCase) {
return (file);
}
}
throw new Error(`${current}: no such file or directory.`);
}
return (sections.reduce(
(previous, current) =>
previous + "/" + tryMatchPaths(current, oldReadDirSynx("/" + previous)),
basePath,
));
}
// 2. Wrapping all the fs methods that being used by the game
// to support case insesitivity.
const oldReadDirSynx = fs.readdirSync;
fs.readdirSync = (path, options) => {
return (oldReadDirSynx(realpath(path), options));
};
const oldStatSync = fs.statSync;
fs.statSync = (path, options) => {
return (oldStatSync(realpath(path), options));
};
const oldReadFileSync = fs.readFileSync;
fs.readFileSync = (path, options) => {
return (oldReadFileSync(realpath(path), options));
};
const oldExistsSync = fs.existsSync;
fs.existsSync = (path) => {
try {
return (oldExistsSync(realpath(path)));
} catch (e) {
return (false);
}
};
const oldBitmapLoad = Bitmap.load;
Bitmap.load = function (url) {
return (oldBitmapLoad(realpath(url).replace(cwd + "/", "")));
};
WebAudio = class extends WebAudio {
/**
* Converts Buffer into ArrayBuffer
*
* @param {Buffer} buffer
* @returns
*/
toArrayBuffer(buffer) {
const arrayBuffer = new ArrayBuffer(buffer.length);
const view = new Uint8Array(arrayBuffer);
for (let i = 0; i < buffer.length; ++i) {
view[i] = buffer[i];
}
return (arrayBuffer);
}
/**
* a function to load an audio.
*
* @param {string} url
* @returns
*/
_load(url) {
if (!WebAudio._context) {
return;
}
if (Decrypter.hasEncryptedAudio) {
url = Decrypter.extToEncryptExt(url);
}
const fs = require("fs");
const path = require("path");
const base = path.dirname(process.mainModule.filename);
const filename = decodeURI(url).replace(/\+/g, " ");
fs.readFile(realpath(base + "/" + filename), (err, array) => {
if (err) {
throw new Error(err);
}
array = this.toArrayBuffer(array);
if (Decrypter.hasEncryptedAudio) {
array = Decrypter.decryptArrayBuffer(array);
}
this._readLoopComments(new Uint8Array(array));
WebAudio._context.decodeAudioData(
array,
function (buffer) {
this._buffer = buffer;
this._totalTime = buffer.duration;
if (this._loopLength > 0 && this._sampleRate > 0) {
this._loopStart /= this._sampleRate;
this._loopLength /= this._sampleRate;
} else {
this._loopStart = 0;
this._loopLength = this._totalTime;
}
this._onLoad();
}.bind(this),
);
});
}
};
window.addEventListener("load", function () {
DataManager = class extends DataManager {
static loadTiledMapData(mapId) {
const path = require("path");
const fs = require("fs");
const base = path.dirname(process.mainModule.filename);
const mapName = `/maps/map${mapId}.json`;
this.unloadTiledMapData();
fs.readFile(base + mapName, (err, buffer) => {
if (err) {
console.error(err);
return;
}
DataManager._tempTiledData = JSON.parse(buffer.toString());
DataManager.loadTilesetData();
DataManager._tiledLoaded = true;
});
}
static loadTilesetData() {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
function _loop() {
var tileset = _step.value;
if (!tileset.source) {
return;
}
if (Utils.isOptionValid("test")) {
DataManager._tilesetToLoad++;
const fs = require("fs");
const path = require("path");
const base = path.dirname(process.mainModule.filename);
const filename = tileset.source.replace(/^.*[\\\/]/, "");
fs.readFile(realpath(base + "/maps/" + filename), (err, data) => {
if (err) {
console.error(err);
return;
}
Object.assign(
tileset,
JSON.parse(data.toString()),
);
DataManager._tilesetToLoad--;
});
} else {
DataManager._tilesetToLoad++;
const filename = tileset.source.replace(/^.*[\\\/]/, "");
const path = require("path");
const fs = require("fs");
const base = path.dirname(process.mainModule.filename);
filename = filename.replace(".json", ".AUBREY");
fs.readFile(realpath(base + "/maps/" + filename), (err, data) => {
if (err) {
console.error(err);
return;
}
data = Encryption.decrypt(data);
Object.assign(tileset, JSON.parse(data.toString()));
DataManager._tilesetToLoad--;
});
}
}
for (
var _iterator = DataManager._tempTiledData.tilesets
[Symbol.iterator](),
_step;
!(_iteratorNormalCompletion = (_step = _iterator.next()).done);
_iteratorNormalCompletion = true
) {
_loop();
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
};
});
}