forked from atampy25/simple-mod-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpkg.js
77 lines (63 loc) · 2.31 KB
/
rpkg.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
const fs = require("fs")
const path = require("path")
const json5 = require("json5")
const child_process = require("child_process")
require("clarify")
const config = json5.parse(fs.readFileSync(path.join(process.cwd(), "config.json")))
class RPKGInstance {
constructor() {
this.rpkgProcess = child_process.spawn(path.join(process.cwd(), "Third-Party", "rpkg-cli"), ["-i"])
this.output = ""
this.previousOutput = ""
this.initialised = false
this.ready = false
this.rpkgProcess.stdout.on("data", (data) => {
this.output += String(data)
if (this.output.endsWith("RPKG> ")) {
if (!this.initialised) {
this.initialised = true
this.ready = false
this.output = ""
this.previousOutput = ""
return
}
this.previousOutput = this.output
this.output = ""
this.ready = true
}
})
}
async waitForInitialised() { // yes, bad, pls tell me how to make good
return new Promise(waitForInitialised.bind(this))
}
async callFunction(func) {
this.ready = false
await this.rpkgProcess.stdin.write(func)
await this.rpkgProcess.stdin.write("\n")
return new Promise(waitForReady.bind(this))
}
async getRPKGOfHash(hash) {
let result = [...(await this.callFunction("-hash_probe \"" + path.resolve(process.cwd(), config.runtimePath) + "\" -filter \"" + hash + "\"")).matchAll(/is in RPKG file: (chunk[0-9]*(?:patch[1-9])?)\.rpkg/g)]
return result[result.length - 1][result[result.length - 1].length - 1] // enjoy lmao
}
exit() {
this.rpkgProcess.kill()
}
}
function waitForInitialised(resolve) { // yes, bad, pls tell me how to make good
if (this.initialised) {
resolve(this.previousOutput)
} else {
setTimeout(waitForInitialised.bind(this, resolve), 100);
}
}
function waitForReady(resolve) { // yes, bad, pls tell me how to make good
if (this.ready) {
resolve(this.previousOutput.slice(0, -8).replace(/Running command: .*\r\n\r\n/g, ""))
} else {
setTimeout(waitForReady.bind(this, resolve), 100);
}
}
module.exports = {
RPKGInstance
}