Repository navigation
Expand file tree
/
Copy pathencrypter.js
More file actions
56 lines (44 loc) · 2.02 KB
/
Copy pathencrypter.js
File metadata and controls
56 lines (44 loc) · 2.02 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
const {
readFile, writeFileSync,
existsSync, mkdirSync
} = require("fs");
const path = require("path");
const Crypto = require("./src/crypto.js");
const { getTimestamp } = require("./src/misc.js");
const crypto = new Crypto();
const dir = `${__dirname.replace(/\\/g, "/")}/encrypted/`;
const argv = require("minimist")(process.argv.slice(2))._;
if (argv.length == 0) {
console.log("Encoder usage:");
console.log("\tnode ./encrypter.js [filepath] (encrypt_macos)");
console.log("\t\t[filepath]: custom path to a file ending in .xml");
console.log("\t\t(encrypt_macos): leave empty or false for normal, or true for MacOS encrypt");
process.exit(0);
}
let pathToFile = argv[0].replace(/\\/g, "/");
let type = (argv[1] ?? "").toLowerCase();
let parsedPath = path.parse(pathToFile);
if (parsedPath.ext != ".xml")
throw new Error("The file must be an XML file.");
if (parsedPath.name.match(/(.*)-\d{4}-\d{2}-\d{2} \d{2}-\d{2}-\d{2}/g))
parsedPath.name = parsedPath.name.replace(/(.*)-\d{4}-\d{2}-\d{2} \d{2}-\d{2}-\d{2}/g, "$1");
if (!type.match(/true|false/g))
throw new Error("Type must be true or false.");
readFile(pathToFile, "utf-8", (err, saveData) => {
if (err) throw new Error(`The file either doesn't exist or is being used by another process. (${err.code})`);
console.log("Encoding...");
// maybe change in future?
if (!saveData.includes("gv_0001"))
throw new Error("Cannot encrypt a save file that has it's keys replaced!");
if (saveData.startsWith("<!-- Prettified -->"))
saveData = saveData.replace("<!-- Prettified -->\n", "").replace(/\t/g, "").replace(/\n/g, "");
if (type == "true")
saveData = saveData.replace(
'<?xml version="1.0"?>',
"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"
);
let encrypted = crypto.encrypt(saveData, type == "true");
if (!existsSync(dir)) mkdirSync(dir);
writeFileSync(dir + `${parsedPath.name}-${getTimestamp()}.dat`, encrypted, "binary");
console.log("Saved!");
});