-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInstallDroidScripts.js
More file actions
97 lines (82 loc) · 2.66 KB
/
InstallDroidScripts.js
File metadata and controls
97 lines (82 loc) · 2.66 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
let baseUrl = 'https://raw.githubusercontent.com/TheDroidYourLookingFor/BitBurner-Scripts/main/';
const usrDirectory = "/TheDroid/";
let json_filename = usrDirectory + 'install_files_json.txt';
/** @param {NS} ns */
export async function main(ns) {
let {
welcomeLabel,
filesToDownload
} = await fetchConfig(ns)
ns.tprintf("%s", welcomeLabel)
let hostname = ns.getHostname()
if (hostname !== 'home') {
throw 'Run the script from home'
}
await clean(ns, filesToDownload);
let count = 0;
for (let filename of filesToDownload) {
const path = baseUrl + filename
const save_filename = (!filename.startsWith('/') && filename.includes('/')) ? usrDirectory + filename : filename;
try {
ns.scriptKill(save_filename, 'home')
ns.rm(save_filename)
await ns.sleep(20)
await ns.wget(path + '?ts=' + new Date().getTime(), save_filename)
if (++count % 5 == 0) {
ns.tprintf(`Installing... [${(count + '').padStart(2)}/${filesToDownload.length}]`);
}
} catch (e) {
ns.tprint(`ERROR (tried to download ${path})`)
throw e;
}
}
terminalCommand('alias -g Droid="run /TheDroid/Manager-Startup.js"')
ns.tprintf("Install complete! To start, type: Droid")
}
async function clean(ns, filesToDownload) {
let filesRaw = filesToDownload.map(file => file.substr(file.lastIndexOf('/') + 1))
let allFiles = ns.ls("home");
let toDelete = [];
allFiles.forEach(_file => {
let file = (_file.startsWith('/')) ? _file.substr(1) : _file;
if (file.startsWith('TheDroid/')) {
let file_raw = file.substr(file.lastIndexOf('/') + 1);
if (filesRaw.includes(file_raw)) {
if (!filesToDownload.includes(file)) {
toDelete.push(_file);
}
} else {
console.log("Install-clean: unidentified file", file);
}
}
})
if (toDelete.length) {
if (await ns.prompt("Files have moved. Installer will clean old files. Confirm? [recommended] " + toDelete.join(", "))) {
toDelete.forEach(file => ns.rm(file));
}
}
}
async function fetchConfig(ns) {
try {
let local_filename = json_filename;
await ns.rm(local_filename)
await ns.wget(baseUrl + json_filename + '?ts=' + new Date().getTime(), local_filename)
return JSON.parse(ns.read(local_filename));
} catch (e) {
ns.tprint(`ERROR: Downloading and reading config file failed ${json_filename}`);
throw e;
}
}
function terminalCommand(message) {
const docs = globalThis['document']
const terminalInput = /** @type {HTMLInputElement} */ (docs.getElementById("terminal-input"));
terminalInput.value = message;
const handler = Object.keys(terminalInput)[1];
terminalInput[handler].onChange({
target: terminalInput
});
terminalInput[handler].onKeyDown({
keyCode: 13,
preventDefault: () => null
});
}