load gba rom as a url to let next handle cache invalidation

This commit is contained in:
Corwin 2024-04-20 16:19:07 +01:00
parent dc44d20627
commit 4022e8413e
No known key found for this signature in database
4 changed files with 17 additions and 13 deletions

View file

@ -91,10 +91,9 @@ build-mgba-wasm:
build-combo-rom-site: build-combo-rom-site:
just _build-rom "examples/combo" "AGBGAMES" just _build-rom "examples/combo" "AGBGAMES"
gzip -9 -c examples/target/examples/combo.gba > website/agb/src/app/combo.gba.gz
build-site-app: build-mgba-wasm build-combo-rom-site build-site-app: build-mgba-wasm build-combo-rom-site
mkdir -p website/agb/public
gzip -9 -c examples/target/examples/combo.gba > website/agb/public/combo.gba.gz
(cd website/agb && npm install --no-save --prefer-offline --no-audit) (cd website/agb && npm install --no-save --prefer-offline --no-audit)
(cd website/agb && npm run build) (cd website/agb && npm run build)

View file

@ -13,7 +13,7 @@ import { useController } from "./useController.hook";
import { useLocalStorage } from "./useLocalStorage.hook"; import { useLocalStorage } from "./useLocalStorage.hook";
interface MgbaProps { interface MgbaProps {
gameUrl: string; gameUrl: URL;
volume?: number; volume?: number;
controls: KeyBindings; controls: KeyBindings;
paused: boolean; paused: boolean;
@ -41,10 +41,12 @@ export interface MgbaHandle {
buttonRelease: (key: GbaKey) => void; buttonRelease: (key: GbaKey) => void;
} }
async function downloadGame(gameUrl: string): Promise<ArrayBuffer> { async function downloadGame(gameUrl: URL): Promise<ArrayBuffer> {
const game = await fetch(gameUrl); const game = await fetch(gameUrl);
if (gameUrl.endsWith(".gz")) { const gameUrlString = gameUrl.toString();
if (gameUrlString.endsWith(".gz")) {
const decompressedStream = (await game.blob()) const decompressedStream = (await game.blob())
.stream() .stream()
.pipeThrough(new DecompressionStream("gzip")); .pipeThrough(new DecompressionStream("gzip"));
@ -67,13 +69,14 @@ export const Mgba = forwardRef<MgbaHandle, MgbaProps>(
{}, {},
"agbrswebplayer/savegames" "agbrswebplayer/savegames"
); );
const gameUrlString = gameUrl.toString();
const [state, setState] = useState(MgbaState.Uninitialised); const [state, setState] = useState(MgbaState.Uninitialised);
const [gameLoaded, setGameLoaded] = useState(false); const [gameLoaded, setGameLoaded] = useState(false);
useEffect(() => { useEffect(() => {
function beforeUnload() { function beforeUnload() {
const gameSplit = gameUrl.split("/"); const gameSplit = gameUrlString.split("/");
const gameBaseName = gameSplit[gameSplit.length - 1]; const gameBaseName = gameSplit[gameSplit.length - 1];
const save = mgbaModule.current?.getSave(); const save = mgbaModule.current?.getSave();
@ -90,12 +93,12 @@ export const Mgba = forwardRef<MgbaHandle, MgbaProps>(
return () => { return () => {
window.removeEventListener("beforeunload", beforeUnload); window.removeEventListener("beforeunload", beforeUnload);
}; };
}, [gameUrl, saveGame, setSaveGame]); }, [gameUrlString, saveGame, setSaveGame]);
useEffect(() => { useEffect(() => {
if (state !== MgbaState.Initialised) return; if (state !== MgbaState.Initialised) return;
const gameSplit = gameUrl.split("/"); const gameSplit = gameUrlString.split("/");
const gameBaseName = gameSplit[gameSplit.length - 1]; const gameBaseName = gameSplit[gameSplit.length - 1];
const save = saveGame[gameBaseName]; const save = saveGame[gameBaseName];
@ -104,13 +107,13 @@ export const Mgba = forwardRef<MgbaHandle, MgbaProps>(
const savePath = `${MGBA_ROM_DIRECTORY}/${gameBaseName}.sav`; const savePath = `${MGBA_ROM_DIRECTORY}/${gameBaseName}.sav`;
mgbaModule.current?.FS.writeFile(savePath, new Uint8Array([0, 1, 2, 3])); mgbaModule.current?.FS.writeFile(savePath, new Uint8Array([0, 1, 2, 3]));
}, [gameUrl, saveGame, state]); }, [gameUrlString, saveGame, state]);
useEffect(() => { useEffect(() => {
if (state !== MgbaState.Initialised) return; if (state !== MgbaState.Initialised) return;
(async () => { (async () => {
const gameData = await downloadGame(gameUrl); const gameData = await downloadGame(gameUrl);
const gameSplit = gameUrl.split("/"); const gameSplit = gameUrlString.split("/");
const gameBaseName = gameSplit[gameSplit.length - 1]; const gameBaseName = gameSplit[gameSplit.length - 1];
const gamePath = `${MGBA_ROM_DIRECTORY}/${gameBaseName}`; const gamePath = `${MGBA_ROM_DIRECTORY}/${gameBaseName}`;
@ -119,7 +122,7 @@ export const Mgba = forwardRef<MgbaHandle, MgbaProps>(
mgbaModule.current?.setVolume(0.1); // for some reason you have to do this or you get no sound mgbaModule.current?.setVolume(0.1); // for some reason you have to do this or you get no sound
setGameLoaded(true); setGameLoaded(true);
})(); })();
}, [state, gameUrl]); }, [state, gameUrl, gameUrlString]);
// init mgba // init mgba
useEffect(() => { useEffect(() => {

View file

@ -60,7 +60,7 @@ const StartButtonWrapper = styled.button`
`; `;
interface MgbaWrapperProps { interface MgbaWrapperProps {
gameUrl: string; gameUrl: URL;
isPlaying?: boolean; isPlaying?: boolean;
setIsPlaying?: (isPlaying: boolean) => void; setIsPlaying?: (isPlaying: boolean) => void;
} }

View file

@ -78,6 +78,8 @@ function shouldStartPlaying(isTouchScreen: boolean | undefined) {
return !isTouchScreen; return !isTouchScreen;
} }
const COMBO_GAME = new URL("combo.gba.gz", import.meta.url);
function MgbaWithControllerSides() { function MgbaWithControllerSides() {
const mgba = useRef<MgbaHandle>(null); const mgba = useRef<MgbaHandle>(null);
@ -105,7 +107,7 @@ function MgbaWithControllerSides() {
</GameSide> </GameSide>
<GameDisplayWindow> <GameDisplayWindow>
<MgbaWrapper <MgbaWrapper
gameUrl="combo.gba.gz" gameUrl={COMBO_GAME}
ref={mgba} ref={mgba}
isPlaying={playEmulator} isPlaying={playEmulator}
setIsPlaying={setIsPlaying} setIsPlaying={setIsPlaying}