forked from MercuryWorkshop/anuraOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoot.tsx
459 lines (415 loc) · 15.3 KB
/
Boot.tsx
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
const channel = new BroadcastChannel("tab");
// send message to all tabs, after a new tab
channel.postMessage("newtab");
let activetab = true;
channel.addEventListener("message", (msg) => {
if (msg.data === "newtab" && activetab) {
// if there's a previously registered tab that can read the message, tell the other tab to kill itself
channel.postMessage("blackmanthunderstorm");
}
if (msg.data === "blackmanthunderstorm") {
activetab = false;
//@ts-ignore
for (const elm of [...document.children]) {
elm.remove();
}
document.open();
document.write("you already have an anura tab open");
document.close();
}
});
const taskbar = new Taskbar();
const launcher = new Launcher();
const contextMenu = new ContextMenu();
const oobeview = new OobeView();
const alttab = new AltTabView();
let anura: Anura;
// global
window.addEventListener("load", async () => {
document.body.appendChild(bootsplash);
await navigator.serviceWorker.register("/anura-sw.js");
let conf, milestone, instancemilestone;
try {
conf = await (await fetch("/config.json")).json();
milestone = await (await fetch("/MILESTONE")).text();
console.log("writing config??");
Filer.fs.writeFile("/config_cached.json", JSON.stringify(conf));
} catch (e) {
conf = JSON.parse(
new TextDecoder().decode(
await Filer.fs.promises.readFile("/config_cached.json"),
),
);
}
anura = await Anura.new(conf);
// TODO: Serialize state in a way that nested statefuls are preserved
function $store<T>(
target: T,
ident: string,
backing:
| string
| ((
ident: string,
target: T,
) => {
get: () => T;
set: (val: T) => void;
}) = "anura",
): Stateful<T> {
let get: () => T;
let set: (val: T) => void;
if (typeof backing === "function") {
const { get: g, set: s } = backing(ident, target);
get = g;
set = s;
} else {
switch (backing) {
case "anura":
get = () =>
anura.settings.get(`dreamland.${ident}`) || target;
set = (val) =>
anura.settings.set(`dreamland.${ident}`, val);
break;
case "localstorage":
get = () => {
const stored = localStorage.getItem(ident);
if (stored === null) return target;
return JSON.parse(stored);
};
set = (val) => {
localStorage.setItem(ident, JSON.stringify(val));
};
break;
default:
throw new Error("invalid backing");
}
}
target = get();
addEventListener("beforeunload", () => {
console.info("[dreamland.js]: saving " + ident);
set(target);
});
return stateful(target);
}
if (milestone) {
const stored = anura.settings.get("milestone");
if (!stored) await anura.settings.set("milestone", milestone);
else if (stored != milestone) {
await anura.settings.set("milestone", milestone);
if (anura.settings.get("use-sw-cache")) {
const tracker = document.getElementById("systemstatus")!;
const tracker_br = document.getElementById("systemstatus-br")!;
tracker.style.display = "unset";
tracker_br.style.display = "unset";
tracker.innerText = "Anura is updating your system...";
try {
await new Filer.fs.Shell().promises.rm("/anura_files", {
recursive: true,
});
} catch {
console.log("cache already invalidated");
}
try {
const list = await (await fetch("cache-load.json")).json();
/*
* The list has a few items that aren't exactly real
* as a result of the developers schizophrenia.
* Because of this, there will be a few errors on the fetch.
* These can safely be ignored, just like the voices in
* the developers head.
*/
const chunkSize = 10;
const promises = [];
let i = 0;
for (const item in list) {
promises.push(fetch(list[item]));
if (Number(item) % chunkSize === chunkSize - 1) {
await Promise.all(promises);
}
tracker!.innerText = `Downloading anura system files, chunk ${i}/${list.length}`;
i++;
}
await Promise.all(promises);
} catch (e) {
console.warn("error durring oobe preload", e);
}
}
console.log("invalidated cache");
window.location.reload();
}
}
Object.assign(window, {
$store,
anura,
});
anura.ui.init();
if (!anura.settings.get("oobe-complete")) {
// This is a new install, so an old version containing the old extension
// handler system can't be installed. We can skip the migration.
anura.settings.set("handler-migration-complete", true);
}
if (!anura.settings.get("handler-migration-complete")) {
// Convert legacy file handlers
// This is a one-time migration
const extHandlers = anura.settings.get("FileExts") || {};
console.log("migrating file handlers");
console.log(extHandlers);
for (const ext in extHandlers) {
const handler = extHandlers[ext];
if (handler.handler_type === "module") continue;
if (handler.handler_type === "cjs") continue;
if (typeof handler === "string") {
if (handler === "/apps/libfileview.app/fileHandler.js") {
extHandlers[ext] = {
handler_type: "module",
id: "anura.fileviewer",
};
continue;
}
extHandlers[ext] = {
handler_type: "cjs",
path: handler,
};
}
}
anura.settings.set("FileExts", extHandlers);
anura.settings.set("handler-migration-complete", true);
}
setTimeout(
() => {
bootsplash.remove();
anura.logger.debug("boot completed");
document.dispatchEvent(new Event("anura-boot-completed"));
},
anura.settings.get("oobe-complete") ? 1000 : 2000,
);
});
document.addEventListener("anura-boot-completed", async () => {
if (anura.settings.get("oobe-complete")) {
document.dispatchEvent(new Event("anura-login-completed"));
} else {
document.body.appendChild(oobeview.element);
}
});
document.addEventListener("anura-login-completed", async () => {
const generic = new GenericApp();
anura.registerApp(generic);
const browser = new BrowserApp();
anura.registerApp(browser);
const settings = new SettingsApp();
anura.registerApp(settings);
const about = new AboutApp();
anura.registerApp(about);
const wallpaper = new WallpaperSelector();
anura.registerApp(wallpaper);
wallpaper.setWallpaper(
anura.settings.get("wallpaper") ||
"/assets/wallpaper/bundled_wallpapers/Default.jpg",
);
anura.registerLib(new NodeFS());
anura.registerLib(new NodePrelude());
for (const lib of anura.config.libs) {
anura.registerExternalLib(lib);
}
for (const app of anura.config.apps) {
anura.registerExternalApp(app);
}
/**
* These directories are used to load user apps and libs from
* the filesystem, along with folder shortcuts and other things.
*
*
*/
let directories = anura.settings.get("directories");
if (!directories) {
anura.settings.set(
"directories",
(directories = {
apps: "/userApps",
libs: "/userLibs",
init: "/userInit",
opt: "/opt",
}),
);
}
/**
* These directories are required for Anura to function
* properly, and are automatically created if they
* don't exist.
*
* This is a setting so that it can be changed by applications
* that heavily modify the system. This will also be respected by
* the file manager and other system utilities to prevent the user
* from removing the shortcuts.
*/
let requiredDirectories = anura.settings.get("requiredDirectories");
if (!requiredDirectories) {
anura.settings.set(
"requiredDirectories",
(requiredDirectories = ["apps", "libs", "init", "opt"]),
);
}
requiredDirectories.forEach((k: string) => {
anura.fs.exists(directories[k], (exists: boolean) => {
if (!exists) {
anura.fs.mkdir(directories[k]);
}
});
});
// Load all persistent sideloaded libs
try {
anura.fs.readdir(directories["libs"], (err: Error, files: string[]) => {
if (files == undefined) return;
console.log(files);
files.forEach((file) => {
try {
anura.registerExternalLib(
`/fs/${directories["libs"]}/${file}/`,
);
} catch (e) {
anura.logger.error("Anura failed to load a lib " + e);
}
});
});
} catch (e) {
anura.logger.error(e);
}
if ((await fetch("/fs/")).status === 404) {
// Safe mode
// Register recovery helper app
const recovery = new RecoveryApp();
anura.registerApp(recovery);
anura.notifications.add({
title: "Anura Error",
description:
"Anura has detected a system fault and booted in safe mode. Click this notification to enter the recovery app.",
timeout: "never",
callback: () => anura.apps["anura.recovery"].open(),
});
} else {
// Not in safe mode
// Load all user provided init scripts
try {
anura.fs.readdir(
directories["init"],
(err: Error, files: string[]) => {
// Fixes a weird edgecase that I was facing where no user apps are installed, nothing breaks it just throws an error which I would like to mitigate.
if (files == undefined) return;
files.forEach((file) => {
try {
anura.fs.readFile(
directories["init"] + "/" + file,
function (err: Error, data: Uint8Array) {
if (err) throw "Failed to read file";
try {
eval(
new TextDecoder("utf-8").decode(
data,
),
);
} catch (e) {
console.error(e);
}
},
);
} catch (e) {
anura.logger.error(
"Anura failed to load an app " + e,
);
}
});
},
);
} catch (e) {
anura.logger.error(e);
}
}
// Load all persistent sideloaded apps
try {
anura.fs.readdir(directories["apps"], (err: Error, files: string[]) => {
// Fixes a weird edgecase that I was facing where no user apps are installed, nothing breaks it just throws an error which I would like to mitigate.
if (files == undefined) return;
files.forEach((file) => {
try {
anura.registerExternalApp(
`/fs/${directories["apps"]}/${file}`,
);
} catch (e) {
anura.logger.error("Anura failed to load an app " + e);
}
});
});
} catch (e) {
anura.logger.error(e);
}
if (!anura.settings.get("x86-disabled")) {
await bootx86();
}
document.body.appendChild(contextMenu.element);
document.body.appendChild(launcher.element);
document.body.appendChild(launcher.clickoffChecker);
document.body.appendChild(taskbar.element);
document.body.appendChild(alttab.element);
(window as any).taskbar = taskbar;
if (anura.settings.get("kiosk-mode")) {
taskbar.element.remove();
// There is a race condition here, but it doesn't matter
// because this feature is a joke
await sleep(1000);
anura.settings.get("kiosk-apps").forEach((app: string) => {
anura.apps[app].open();
});
}
document.addEventListener("contextmenu", function (e) {
if (e.shiftKey) return;
e.preventDefault();
// const menu: any = document.querySelector(".custom-menu");
// menu.style.removeProperty("display");
// menu.style.top = `${e.clientY}px`;
// menu.style.left = `${e.clientX}px`;
});
//
// document.addEventListener("click", (e) => {
// if (e.button != 0) return;
// (
// document.querySelector(".custom-menu")! as HTMLElement
// ).style.setProperty("display", "none");
// });
document.addEventListener("keydown", (e) => {
if (e.shiftKey && e.key.toLowerCase() == "tab") {
e.preventDefault();
alttab.onComboPress();
}
if (
e.key.toLowerCase() === "meta" &&
anura.settings.get("launcher-keybind")
) {
launcher.toggleVisible();
return;
}
});
document.addEventListener("keyup", (e) => {
// console.log("keyup", e);
if (e.key.toLowerCase() === "shift") {
alttab.onModRelease();
return;
}
});
// This feels wrong but it works and makes TSC happy
launcher.clickoffChecker?.addEventListener("click", () => {
launcher.toggleVisible();
});
anura.initComplete = true;
taskbar.updateTaskbar();
alttab.update();
});
async function bootx86() {
const mgr = new x86MgrApp();
await anura.registerApp(mgr);
await anura.registerApp(new XFrogApp());
await anura.registerApp(
new XAppStub("X Calculator", "anura.xcalc", "", "xcalc"),
);
await anura.registerApp(new XAppStub("XTerm", "anura.xterm", "", "xterm"));
anura.x86 = new V86Backend(anura.x86hdd);
}