[Refactor] Remove main.ts default export, clean up manifest code

https://github.com/pagefaultgames/pokerogue/pull/6768
This commit is contained in:
Bertie690 2025-11-29 17:42:23 -05:00 committed by GitHub
parent b6bd9566e2
commit a2b4727d63
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 68 additions and 74 deletions

View File

@ -720,12 +720,10 @@ export class BattleScene extends SceneBase {
}
cachedFetch(url: string, init?: RequestInit): Promise<Response> {
const manifest = this.game["manifest"];
if (manifest) {
const timestamp = manifest[`/${url.replace("./", "")}`];
if (timestamp) {
url += `?t=${timestamp}`;
}
const { manifest } = this.game;
const timestamp = manifest?.[`/${url.replace("./", "")}`];
if (timestamp) {
url += `?t=${timestamp}`;
}
return fetch(url, init);
}

View File

@ -25,7 +25,6 @@ export class LoadingScene extends SceneBase {
preload() {
localPing();
this.load["manifest"] = this.game["manifest"];
this.loadImage("loading_bg", "arenas");
this.loadImage("logo", "");

View File

@ -1,6 +1,7 @@
import "#app/polyfills";
// All polyfills MUST be loaded first for side effects
import "#app/polyfills"; // All polyfills MUST be loaded first for side effects
import { BattleScene } from "#app/battle-scene";
import { LoadingScene } from "#app/loading-scene";
import { InvertPostFX } from "#app/pipelines/invert";
import { initI18n } from "#app/plugins/i18n";
import { isBeta, isDev } from "#constants/app-constants";
@ -31,6 +32,11 @@ window.addEventListener("unhandledrejection", event => {
//alert(errorString);
});
interface GuideObject
extends Pick<Phaser.GameObjects.Components.ComputedSize, "width" | "height">,
Pick<Phaser.GameObjects.Components.Transform, "x" | "y">,
Pick<Phaser.GameObjects.Components.Origin, "originX" | "originY"> {}
/**
* Set this object's position relative to another object with a given offset.
* @param guideObject - The object to base this object's position off of; must have defined
@ -41,9 +47,7 @@ window.addEventListener("unhandledrejection", event => {
*/
function setPositionRelative<T extends Phaser.GameObjects.Components.Transform>(
this: T,
guideObject: Pick<Phaser.GameObjects.Components.ComputedSize, "width" | "height"> &
Pick<Phaser.GameObjects.Components.Transform, "x" | "y"> &
Pick<Phaser.GameObjects.Components.Origin, "originX" | "originY">,
guideObject: GuideObject,
x: number,
y: number,
): T {
@ -59,17 +63,9 @@ Phaser.GameObjects.NineSlice.prototype.setPositionRelative = setPositionRelative
Phaser.GameObjects.Text.prototype.setPositionRelative = setPositionRelative;
Phaser.GameObjects.Rectangle.prototype.setPositionRelative = setPositionRelative;
document.fonts.load("16px emerald").then(() => document.fonts.load("10px pkmnems"));
// biome-ignore lint: TODO
let game;
// biome-ignore lint: TODO
let manifest;
const startGame = async () => {
async function startGame(gameManifest?: Record<string, string>): Promise<void> {
await initI18n();
const LoadingScene = (await import("./loading-scene")).LoadingScene;
const BattleScene = (await import("./battle-scene")).BattleScene;
game = new Phaser.Game({
const game = new Phaser.Game({
type: Phaser.WEBGL,
parent: "app",
scale: {
@ -121,22 +117,18 @@ const startGame = async () => {
version,
});
game.sound.pauseOnBlur = false;
if (manifest) {
game["manifest"] = manifest;
}
};
game.manifest = gameManifest;
}
fetch("/manifest.json")
.then(res => res.json())
.then(jsonResponse => {
manifest = jsonResponse.manifest;
})
.catch(err => {
// Manifest not found (likely local build or path error on live)
console.log(`Manifest not found. ${err}`);
})
.finally(() => {
startGame();
});
export default game;
let manifest: Record<string, string> | undefined;
try {
const loadFonts = Promise.all([document.fonts.load("16px emerald"), document.fonts.load("10px pkmnems")]);
const [jsonResponse] = await Promise.all([fetch("/manifest.json").then(r => r.json()), loadFonts]);
manifest = jsonResponse.manifest;
} catch (err) {
// Manifest not found (likely local build or path error on live)
// TODO: Do we want actual error handling here?
console.log("Manifest not found:", err);
} finally {
await startGame(manifest);
}

View File

@ -1,28 +1,27 @@
import { globalScene } from "#app/global-scene";
import { coerceArray } from "#utils/array";
let manifest: object;
export class CacheBustedLoaderPlugin extends Phaser.Loader.LoaderPlugin {
get manifest() {
return manifest;
}
addFile(files: Phaser.Loader.File | Phaser.Loader.File[]): void {
files = coerceArray(files);
const { manifest } = globalScene.game;
set manifest(manifestObj: object) {
manifest = manifestObj;
}
if (!manifest) {
super.addFile(files);
return;
}
addFile(file): void {
file = coerceArray(file);
file.forEach(item => {
if (manifest) {
const timestamp = manifest[`/${item.url.replace(/\/\//g, "/")}`];
if (timestamp) {
item.url += `?t=${timestamp}`;
}
for (const item of files) {
if (typeof item.url !== "string") {
continue;
}
});
super.addFile(file);
const timestamp = manifest[`/${item.url.replace(/\/\//g, "/")}`];
if (timestamp) {
item.url += `?t=${timestamp}`;
}
}
super.addFile(files);
}
}

View File

@ -18,12 +18,16 @@ export class SceneBase extends Phaser.Scene {
};
getCachedUrl(url: string): string {
const manifest = this.game["manifest"];
if (manifest) {
const timestamp = manifest[`/${url}`];
if (timestamp) {
url += `?t=${timestamp}`;
}
const manifest = this.game.manifest;
if (!manifest) {
return url;
}
// TODO: This is inconsistent with how the battle scene cached fetch
// uses the manifest
const timestamp = manifest[`/${url}`];
if (timestamp) {
url += `?t=${timestamp}`;
}
return url;
}
@ -40,10 +44,7 @@ export class SceneBase extends Phaser.Scene {
}
}
loadSpritesheet(key: string, folder: string, size: number, filename?: string) {
if (!filename) {
filename = `${key}.png`;
}
loadSpritesheet(key: string, folder: string, size: number, filename = `${key}.png`) {
this.load.spritesheet(key, this.getCachedUrl(`images/${folder}/${filename}`), {
frameWidth: size,
frameHeight: size,
@ -58,10 +59,7 @@ export class SceneBase extends Phaser.Scene {
}
}
loadAtlas(key: string, folder: string, filenameRoot?: string) {
if (!filenameRoot) {
filenameRoot = key;
}
loadAtlas(key: string, folder: string, filenameRoot = key) {
if (folder) {
folder += "/";
}

View File

@ -51,4 +51,9 @@ declare module "phaser" {
}
}
}
interface Game {
/** A manifest used to cache various files requested from the server. */
manifest?: Record<string, string>;
}
}

View File

@ -1,8 +1,11 @@
import { isBeta } from "#constants/app-constants";
// 90 days
const COOKIE_EXPIRATION_BUFFER = 3600000 * 24 * 30 * 3;
export function setCookie(cName: string, cValue: string): void {
const expiration = new Date();
expiration.setTime(Date.now() + 3600000 * 24 * 30 * 3 /*7*/);
expiration.setTime(Date.now() + COOKIE_EXPIRATION_BUFFER);
document.cookie = `${cName}=${cValue};Secure;SameSite=Strict;Domain=${window.location.hostname};Path=/;Expires=${expiration.toUTCString()}`;
}