-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (60 loc) · 2.13 KB
/
index.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
const path = require("path");
const fs = require("fs");
if (!fs.existsSync(path.join(__dirname, "saved"))) {
fs.mkdirSync(path.join(__dirname, "saved"));
}
module.exports = function saver(mod) {
const dataToSave = {};
mod.hook("S_LOAD_CLIENT_ACCOUNT_SETTING", "raw", sLoadClientSetting.bind(null, "accountSettings"));
mod.hook("S_REPLY_CLIENT_CHAT_OPTION_SETTING", "raw", sLoadClientSetting.bind(null, "chatSettings"));
mod.hook("C_SAVE_CLIENT_ACCOUNT_SETTING", "raw", cSaveClientSetting.bind(null, "accountSettings"));
mod.hook("C_SAVE_CLIENT_CHAT_OPTION_SETTING", "raw", cSaveClientSetting.bind(null, "chatSettings"));
mod.hook("S_EXIT", "raw", { "filter": { "fake": null } }, sExit.bind(null));
mod.hook("S_DECO_UI_INFO", "raw", sExit.bind(null));
mod.command.add("setlock", () => {
const jsonData = getData();
jsonData.lock = !jsonData.lock;
saveData(jsonData);
mod.command.message(`Your settings is ${jsonData.lock ? "locked" : "unlocked"}.`);
});
function sLoadClientSetting(key, opcode, payload, incoming, fake) {
const jsonData = getData();
if (jsonData[key] && !fake) {
mod.toClient(Buffer.from(`${jsonData[key].length}${
getPacketInfo(payload).opcode}${jsonData[key].payload}`, "hex"));
return false;
}
}
function cSaveClientSetting(key, opcode, payload, incoming, fake) {
dataToSave[key] = getPacketInfo(payload);
}
function sExit() {
const jsonData = getData();
if (Object.keys(dataToSave).length !== 0) {
saveData(jsonData.lock ?
{ ...dataToSave, ...jsonData, "lock": jsonData.lock } :
{ ...jsonData, ...dataToSave, "lock": jsonData.lock }
);
}
}
function getData() {
try {
return JSON.parse(fs.readFileSync(path.join(__dirname, "saved", `${
mod.game.serverId}-${mod.game.accountId}.json`)));
} catch (_) {
return {};
}
}
function saveData(data) {
fs.writeFileSync(path.join(__dirname, "saved", `${
mod.game.serverId}-${mod.game.accountId}.json`), JSON.stringify(data, null, 4));
}
function getPacketInfo(payload) {
const data = payload.toString("hex");
return {
"length": data.slice(0, 4),
"opcode": data.slice(4, 8),
"payload": data.slice(8)
};
}
};