forked from alainbryden/bitburner-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenCache.js
More file actions
164 lines (145 loc) · 5.14 KB
/
Copy pathopenCache.js
File metadata and controls
164 lines (145 loc) · 5.14 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
/**
* openCache.js — 暗网缓存管理器 v2.0
*
* 功能:
* 移植自 dnet-worm.js 的 openCache API 功能
* 扫描本机的 .cache / .d.cache 文件并逐一打开
*
* 两种运行模式:
* 1. 一次性模式(默认): 扫描并打开所有缓存文件后自动退出
* 2. 监视模式(--watch): 持续循环,发现新缓存立即打开
*
* 被 dnet-worm.js 管理生命周期:
* - worm 发现存在缓存文件时执行本脚本(--watch 模式)
* - worm 发现缓存文件消失时 kill 本进程
*
* RAM: ~3 GB (ls 0.2G + openCache 2G + base)
*
* 用法:
* run openCache.js ← 一次性打开所有缓存后退出
* run openCache.js --watch ← 持续监视并打开新增缓存
*
* @param {NS} ns
*/
export async function main(ns) {
const HOST = ns.getHostname();
const WATCH_MODE = ns.args.includes("--watch");
// ======================== 配置 ========================
const CHECK_INTERVAL_MS = 30000; // 监视模式下每 30 秒扫描一次
const REPORT_BASE = "/Temp/dnet-worm-";
// 已打开的缓存文件集合(避免重复打开)
const openedSet = new Set();
const openedLogFile = "/Temp/openCache-opened.txt";
// 启动时加载已打开记录
try {
const raw = ns.read(openedLogFile);
if (raw) JSON.parse(raw).forEach((f) => openedSet.add(f));
} catch { /* 首次运行 */ }
/** 持久化已打开列表 */
function saveOpened() {
ns.write(openedLogFile, JSON.stringify([...openedSet]), "w");
}
/** 向控制中枢发送报告 */
async function reportToController(type, data) {
try {
const reportFile = REPORT_BASE + type + "-" + HOST.replace(/[^a-zA-Z0-9]/g, "_") + ".txt";
ns.write(reportFile, JSON.stringify(data), "w");
// ⚠️ SCP 到 home!否则中枢在 home 上读不到
await ns.scp(reportFile, "home");
} catch (e) {
ns.print(`[${HOST}] 报告失败: ${e}`);
}
}
/** 扫描并打开本机的缓存文件 */
async function scanAndOpen() {
let cacheFiles = [];
try {
const allFiles = ns.ls(HOST);
cacheFiles = allFiles.filter(
(f) => f.endsWith(".cache") || f.endsWith(".d.cache")
);
} catch (e) {
ns.print(`[${HOST}] 读取文件列表失败: ${e}`);
return { opened: 0, total: 0 };
}
if (cacheFiles.length === 0) {
return { opened: 0, total: 0 };
}
// 过滤出未打开的缓存
const newCaches = cacheFiles.filter((f) => !openedSet.has(f));
if (newCaches.length === 0) {
return { opened: 0, total: cacheFiles.length };
}
ns.print(`[${HOST}] 发现 ${newCaches.length} 个新缓存文件, 开始打开...`);
let opened = 0;
for (const cacheFile of newCaches) {
try {
const r = await ns.dnet.openCache(cacheFile, true); // suppressToast=true
if (r && r.success) {
ns.tprint(`🎁 [${HOST}] 打开缓存 ${cacheFile}: ${r.message}`);
openedSet.add(cacheFile);
opened++;
} else {
ns.print(`[${HOST}] 打开缓存 ${cacheFile} 失败: ${r?.message}`);
}
} catch (e) {
ns.print(`[${HOST}] 打开缓存 ${cacheFile} 异常: ${e}`);
}
await ns.sleep(100);
}
// 持久化已打开记录
saveOpened();
// 向控制中枢报告
if (opened > 0) {
reportToController("cache", {
host: HOST,
opened,
total: cacheFiles.length,
files: newCaches,
});
}
return { opened, total: cacheFiles.length };
}
// ======================== 主逻辑 ========================
ns.print(`[${HOST}] openCache.js v2.0 启动, 模式: ${WATCH_MODE ? "监视" : "一次性"}`);
// 告知 worm 本脚本已启动(用于同步)
reportToController("openCache-status", {
host: HOST,
status: "started",
mode: WATCH_MODE ? "watch" : "oneshot",
});
if (WATCH_MODE) {
// ----- 监视模式:检查一遍,没活就退出,让 watch 后续再唤我 -----
ns.print(`[${HOST}] 进入监视模式,每 ${CHECK_INTERVAL_MS / 1000} 秒扫描一次缓存`);
let idleCycles = 0;
while (true) {
const result = await scanAndOpen();
// 没有新缓存可打开 → 计数,连续 2 次就退出
if (result.opened === 0) {
idleCycles++;
ns.print(`[${HOST}] 空闲 #${idleCycles},${idleCycles >= 2 ? "退出等待" : "再扫一轮"}`);
if (idleCycles >= 2) {
reportToController("openCache-done", {
host: HOST, opened: 0, total: result.total, status: "idle_exit",
});
ns.print(`[${HOST}] 缓存已全部打开,退出等待`);
return; // 退出,watch 会在新缓存出现时重新启动
}
} else {
idleCycles = 0; // 打开了新缓存,重置计数器
}
await ns.sleep(CHECK_INTERVAL_MS);
}
} else {
// ----- 一次性模式:打开后退出 -----
const result = await scanAndOpen();
ns.print(
`[${HOST}] 完成: 打开 ${result.opened}/${result.total} 个缓存`
);
reportToController("openCache-done", {
host: HOST,
opened: result.opened,
total: result.total,
});
}
}