[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> { cachedFetch(url: string, init?: RequestInit): Promise<Response> {
const manifest = this.game["manifest"]; const { manifest } = this.game;
if (manifest) { const timestamp = manifest?.[`/${url.replace("./", "")}`];
const timestamp = manifest[`/${url.replace("./", "")}`]; if (timestamp) {
if (timestamp) { url += `?t=${timestamp}`;
url += `?t=${timestamp}`;
}
} }
return fetch(url, init); return fetch(url, init);
} }

View File

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

View File

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

View File

@ -1,28 +1,27 @@
import { globalScene } from "#app/global-scene";
import { coerceArray } from "#utils/array"; import { coerceArray } from "#utils/array";
let manifest: object;
export class CacheBustedLoaderPlugin extends Phaser.Loader.LoaderPlugin { export class CacheBustedLoaderPlugin extends Phaser.Loader.LoaderPlugin {
get manifest() { addFile(files: Phaser.Loader.File | Phaser.Loader.File[]): void {
return manifest; files = coerceArray(files);
} const { manifest } = globalScene.game;
set manifest(manifestObj: object) { if (!manifest) {
manifest = manifestObj; super.addFile(files);
} return;
}
addFile(file): void { for (const item of files) {
file = coerceArray(file); if (typeof item.url !== "string") {
continue;
file.forEach(item => {
if (manifest) {
const timestamp = manifest[`/${item.url.replace(/\/\//g, "/")}`];
if (timestamp) {
item.url += `?t=${timestamp}`;
}
} }
});
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 { getCachedUrl(url: string): string {
const manifest = this.game["manifest"]; const manifest = this.game.manifest;
if (manifest) { if (!manifest) {
const timestamp = manifest[`/${url}`]; return url;
if (timestamp) { }
url += `?t=${timestamp}`;
} // 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; return url;
} }
@ -40,10 +44,7 @@ export class SceneBase extends Phaser.Scene {
} }
} }
loadSpritesheet(key: string, folder: string, size: number, filename?: string) { loadSpritesheet(key: string, folder: string, size: number, filename = `${key}.png`) {
if (!filename) {
filename = `${key}.png`;
}
this.load.spritesheet(key, this.getCachedUrl(`images/${folder}/${filename}`), { this.load.spritesheet(key, this.getCachedUrl(`images/${folder}/${filename}`), {
frameWidth: size, frameWidth: size,
frameHeight: size, frameHeight: size,
@ -58,10 +59,7 @@ export class SceneBase extends Phaser.Scene {
} }
} }
loadAtlas(key: string, folder: string, filenameRoot?: string) { loadAtlas(key: string, folder: string, filenameRoot = key) {
if (!filenameRoot) {
filenameRoot = key;
}
if (folder) { if (folder) {
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"; import { isBeta } from "#constants/app-constants";
// 90 days
const COOKIE_EXPIRATION_BUFFER = 3600000 * 24 * 30 * 3;
export function setCookie(cName: string, cValue: string): void { export function setCookie(cName: string, cValue: string): void {
const expiration = new Date(); 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()}`; document.cookie = `${cName}=${cValue};Secure;SameSite=Strict;Domain=${window.location.hostname};Path=/;Expires=${expiration.toUTCString()}`;
} }