mirror of
https://github.com/italicsjenga/agb.git
synced 2024-12-23 08:11:33 +11:00
load gba rom as a url to let next handle cache invalidation
This commit is contained in:
parent
dc44d20627
commit
4022e8413e
3
justfile
3
justfile
|
@ -91,10 +91,9 @@ build-mgba-wasm:
|
|||
|
||||
build-combo-rom-site:
|
||||
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
|
||||
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 run build)
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ import { useController } from "./useController.hook";
|
|||
import { useLocalStorage } from "./useLocalStorage.hook";
|
||||
|
||||
interface MgbaProps {
|
||||
gameUrl: string;
|
||||
gameUrl: URL;
|
||||
volume?: number;
|
||||
controls: KeyBindings;
|
||||
paused: boolean;
|
||||
|
@ -41,10 +41,12 @@ export interface MgbaHandle {
|
|||
buttonRelease: (key: GbaKey) => void;
|
||||
}
|
||||
|
||||
async function downloadGame(gameUrl: string): Promise<ArrayBuffer> {
|
||||
async function downloadGame(gameUrl: URL): Promise<ArrayBuffer> {
|
||||
const game = await fetch(gameUrl);
|
||||
|
||||
if (gameUrl.endsWith(".gz")) {
|
||||
const gameUrlString = gameUrl.toString();
|
||||
|
||||
if (gameUrlString.endsWith(".gz")) {
|
||||
const decompressedStream = (await game.blob())
|
||||
.stream()
|
||||
.pipeThrough(new DecompressionStream("gzip"));
|
||||
|
@ -67,13 +69,14 @@ export const Mgba = forwardRef<MgbaHandle, MgbaProps>(
|
|||
{},
|
||||
"agbrswebplayer/savegames"
|
||||
);
|
||||
const gameUrlString = gameUrl.toString();
|
||||
|
||||
const [state, setState] = useState(MgbaState.Uninitialised);
|
||||
const [gameLoaded, setGameLoaded] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
function beforeUnload() {
|
||||
const gameSplit = gameUrl.split("/");
|
||||
const gameSplit = gameUrlString.split("/");
|
||||
const gameBaseName = gameSplit[gameSplit.length - 1];
|
||||
|
||||
const save = mgbaModule.current?.getSave();
|
||||
|
@ -90,12 +93,12 @@ export const Mgba = forwardRef<MgbaHandle, MgbaProps>(
|
|||
return () => {
|
||||
window.removeEventListener("beforeunload", beforeUnload);
|
||||
};
|
||||
}, [gameUrl, saveGame, setSaveGame]);
|
||||
}, [gameUrlString, saveGame, setSaveGame]);
|
||||
|
||||
useEffect(() => {
|
||||
if (state !== MgbaState.Initialised) return;
|
||||
|
||||
const gameSplit = gameUrl.split("/");
|
||||
const gameSplit = gameUrlString.split("/");
|
||||
const gameBaseName = gameSplit[gameSplit.length - 1];
|
||||
|
||||
const save = saveGame[gameBaseName];
|
||||
|
@ -104,13 +107,13 @@ export const Mgba = forwardRef<MgbaHandle, MgbaProps>(
|
|||
const savePath = `${MGBA_ROM_DIRECTORY}/${gameBaseName}.sav`;
|
||||
|
||||
mgbaModule.current?.FS.writeFile(savePath, new Uint8Array([0, 1, 2, 3]));
|
||||
}, [gameUrl, saveGame, state]);
|
||||
}, [gameUrlString, saveGame, state]);
|
||||
|
||||
useEffect(() => {
|
||||
if (state !== MgbaState.Initialised) return;
|
||||
(async () => {
|
||||
const gameData = await downloadGame(gameUrl);
|
||||
const gameSplit = gameUrl.split("/");
|
||||
const gameSplit = gameUrlString.split("/");
|
||||
const gameBaseName = gameSplit[gameSplit.length - 1];
|
||||
|
||||
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
|
||||
setGameLoaded(true);
|
||||
})();
|
||||
}, [state, gameUrl]);
|
||||
}, [state, gameUrl, gameUrlString]);
|
||||
|
||||
// init mgba
|
||||
useEffect(() => {
|
||||
|
|
|
@ -60,7 +60,7 @@ const StartButtonWrapper = styled.button`
|
|||
`;
|
||||
|
||||
interface MgbaWrapperProps {
|
||||
gameUrl: string;
|
||||
gameUrl: URL;
|
||||
isPlaying?: boolean;
|
||||
setIsPlaying?: (isPlaying: boolean) => void;
|
||||
}
|
||||
|
|
|
@ -78,6 +78,8 @@ function shouldStartPlaying(isTouchScreen: boolean | undefined) {
|
|||
return !isTouchScreen;
|
||||
}
|
||||
|
||||
const COMBO_GAME = new URL("combo.gba.gz", import.meta.url);
|
||||
|
||||
function MgbaWithControllerSides() {
|
||||
const mgba = useRef<MgbaHandle>(null);
|
||||
|
||||
|
@ -105,7 +107,7 @@ function MgbaWithControllerSides() {
|
|||
</GameSide>
|
||||
<GameDisplayWindow>
|
||||
<MgbaWrapper
|
||||
gameUrl="combo.gba.gz"
|
||||
gameUrl={COMBO_GAME}
|
||||
ref={mgba}
|
||||
isPlaying={playEmulator}
|
||||
setIsPlaying={setIsPlaying}
|
||||
|
|
Loading…
Reference in a new issue