Profectus/src/main.ts

109 lines
3.4 KiB
TypeScript
Raw Permalink Normal View History

import "@fontsource/material-icons";
2022-06-27 00:17:22 +00:00
import App from "App.vue";
import projInfo from "data/projInfo.json";
import "game/notifications";
2023-05-17 04:38:31 +00:00
import state from "game/state";
2022-06-27 00:17:22 +00:00
import { load } from "util/save";
2022-06-26 20:22:22 +00:00
import { useRegisterSW } from "virtual:pwa-register/vue";
2022-06-27 00:17:22 +00:00
import type { App as VueApp } from "vue";
import { createApp, nextTick } from "vue";
2022-06-26 20:22:22 +00:00
import { useToast } from "vue-toastification";
2022-01-14 04:25:47 +00:00
declare global {
2022-07-10 03:09:25 +00:00
/**
2022-07-10 05:43:52 +00:00
* Augment the window object so the vue app and project info can be accessed from the console.
2022-07-10 03:09:25 +00:00
*/
2022-01-14 04:25:47 +00:00
interface Window {
vue: VueApp;
projInfo: typeof projInfo;
2022-01-14 04:25:47 +00:00
}
2022-07-10 03:02:44 +00:00
2022-07-10 05:43:52 +00:00
/** Fix for typedoc treating import functions as taking AssertOptions instead of GlobOptions. */
2022-07-10 03:02:44 +00:00
interface AssertOptions {
as: string;
}
2022-01-14 04:25:47 +00:00
}
2023-05-17 04:38:31 +00:00
const error = console.error;
console.error = function (...args) {
if (import.meta.env.DEV) {
state.errors.push(new Error(args[0], { cause: args[1] }));
}
error(...args);
};
window.onerror = function (event, source, lineno, colno, err) {
state.errors.push(err instanceof Error ? err : new Error(JSON.stringify(err)));
error(err);
2023-05-17 04:38:31 +00:00
return true;
};
window.onunhandledrejection = function (event) {
state.errors.push(
event.reason instanceof Error ? event.reason : new Error(JSON.stringify(event.reason))
);
error(event.reason);
2023-05-17 04:38:31 +00:00
};
2022-07-10 03:09:25 +00:00
document.title = projInfo.title;
window.projInfo = projInfo;
if (projInfo.id === "") {
2023-05-17 04:38:31 +00:00
console.error(
"Project ID is empty!",
"Please select a unique ID for this project in /src/data/projInfo.json"
2023-03-24 01:22:03 +00:00
);
2022-07-10 03:09:25 +00:00
}
requestAnimationFrame(async () => {
console.log(
2022-02-27 20:24:48 +00:00
"%cMade in Profectus%c\nLearn more at www.moddingtree.com",
"font-weight: bold; font-size: 24px; color: #A3BE8C; background: #2E3440; padding: 4px 8px; border-radius: 8px;",
"padding: 4px;"
);
await load();
const { globalBus } = await import("./game/events");
const { startGameLoop } = await import("./game/gameLoop");
// Create Vue
2022-03-04 03:41:59 +00:00
const vue = (window.vue = createApp(App));
2023-05-17 04:38:31 +00:00
vue.config.errorHandler = function (err, instance, info) {
console.error(err, info, instance);
};
2022-01-14 04:25:47 +00:00
globalBus.emit("setupVue", vue);
vue.mount("#app");
2022-06-26 20:22:22 +00:00
// Setup PWA update prompt
nextTick(() => {
const toast = useToast();
const { updateServiceWorker } = useRegisterSW({
onNeedRefresh() {
2022-12-10 06:47:11 +00:00
toast.info("New content available, click here to update.", {
2022-06-26 20:22:22 +00:00
timeout: false,
closeOnClick: false,
draggable: false,
icon: {
iconClass: "material-icons",
iconChildren: "refresh",
iconTag: "i"
},
rtl: false,
onClick() {
updateServiceWorker();
}
});
},
onOfflineReady() {
toast.info("App ready to work offline");
},
onRegisterError: console.warn,
onRegistered(r) {
if (r) {
2022-12-09 01:31:36 +00:00
// https://stackoverflow.com/questions/65500916/typeerror-failed-to-execute-update-on-serviceworkerregistration-illegal-in
setInterval(() => r.update(), 60 * 60 * 1000);
2022-06-26 20:22:22 +00:00
}
}
});
});
2022-01-14 04:25:47 +00:00
startGameLoop();
});