Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: abstract manual rom file path construction #238

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions gbajs3/src/components/modals/load-local-rom.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ describe('<LoadLocalRomModal />', () => {
vi.spyOn(contextHooks, 'useEmulatorContext').mockImplementation(() => ({
...originalEmulator(),
emulator: {
listRoms: () => ['rom1.gba'],
filePaths: () => ({
gamePath: '/games'
})
listRoms: () => ['rom1.gba']
} as GBAEmulator
}));

Expand All @@ -74,7 +71,7 @@ describe('<LoadLocalRomModal />', () => {
await userEvent.click(localRom);

expect(runGameSpy).toHaveBeenCalledOnce();
expect(runGameSpy).toHaveBeenCalledWith('/games/rom1.gba');
expect(runGameSpy).toHaveBeenCalledWith('rom1.gba');
expect(setIsModalOpenSpy).toHaveBeenCalledWith(false);
});

Expand Down
2 changes: 1 addition & 1 deletion gbajs3/src/components/modals/load-local-rom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const LoadLocalRomModal = () => {
<StyledLi key={`${romName}_${idx}`}>
<LoadRomButton
onClick={() => {
runGame(emulator?.filePaths().gamePath + '/' + romName);
runGame(romName);
setIsModalOpen(false);
}}
>
Expand Down
7 changes: 2 additions & 5 deletions gbajs3/src/components/modals/load-rom.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ describe('<LoadRomModal />', () => {
vi.spyOn(contextHooks, 'useEmulatorContext').mockImplementation(() => ({
...originalEmulator(),
emulator: {
uploadRom: uploadRomSpy,
filePaths: () => ({
gamePath: '/games'
})
uploadRom: uploadRomSpy
} as GBAEmulator
}));

Expand All @@ -66,7 +63,7 @@ describe('<LoadRomModal />', () => {
expect(uploadRomSpy).toHaveBeenCalledOnce();
expect(syncActionIfEnabledSpy).toHaveBeenCalledOnce();
expect(runGameSpy).toHaveBeenCalledOnce();
expect(runGameSpy).toHaveBeenCalledWith('/games/rom1.gba');
expect(runGameSpy).toHaveBeenCalledWith('rom1.gba');
});

it('renders message when there are no roms', () => {
Expand Down
2 changes: 1 addition & 1 deletion gbajs3/src/components/modals/load-rom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const LoadRomModal = () => {
if (shouldUploadRom) {
const runCallback = () => {
syncActionIfEnabled();
runGame(emulator?.filePaths().gamePath + '/' + romFile.name);
runGame(romFile.name);
};

emulator?.uploadRom(romFile, runCallback);
Expand Down
5 changes: 1 addition & 4 deletions gbajs3/src/components/modals/load-save.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ describe('<LoadSaveModal />', () => {
vi.spyOn(contextHooks, 'useEmulatorContext').mockImplementation(() => ({
...originalEmulator(),
emulator: {
uploadSaveOrSaveState: uploadSaveOrSaveStateSpy,
filePaths: () => ({
savePath: '/saves'
})
uploadSaveOrSaveState: uploadSaveOrSaveStateSpy
} as GBAEmulator
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ describe('<UploadPublicExternalRomsModal />', () => {
vi.spyOn(contextHooks, 'useEmulatorContext').mockImplementation(() => ({
...originalEmulator(),
emulator: {
uploadRom: uploadRomSpy,
filePaths: () => ({
gamePath: '/games'
})
uploadRom: uploadRomSpy
} as GBAEmulator
}));

Expand Down Expand Up @@ -79,7 +76,7 @@ describe('<UploadPublicExternalRomsModal />', () => {
expect(uploadRomSpy).toHaveBeenCalledOnce();
expect(syncActionIfEnabledSpy).toHaveBeenCalledOnce();
expect(runGameSpy).toHaveBeenCalledOnce();
expect(runGameSpy).toHaveBeenCalledWith('/games/good_rom.gba');
expect(runGameSpy).toHaveBeenCalledWith('good_rom.gba');

expect(onLoadOrDismissSpy).toHaveBeenCalledOnce();
expect(onLoadOrDismissSpy).toHaveBeenCalledWith('loaded');
Expand All @@ -100,10 +97,7 @@ describe('<UploadPublicExternalRomsModal />', () => {
vi.spyOn(contextHooks, 'useEmulatorContext').mockImplementation(() => ({
...originalEmulator(),
emulator: {
uploadRom: uploadRomSpy,
filePaths: () => ({
gamePath: '/games'
})
uploadRom: uploadRomSpy
} as GBAEmulator
}));

Expand Down
4 changes: 1 addition & 3 deletions gbajs3/src/components/modals/upload-public-external-roms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ export const UploadPublicExternalRomsModal = ({
if (!isExternalRomLoading && externalRomFile && currentRomURL) {
const runCallback = () => {
syncActionIfEnabled();
const hasSucceeded = runGame(
emulator?.filePaths().gamePath + '/' + externalRomFile.name
);
const hasSucceeded = runGame(externalRomFile.name);
if (hasSucceeded) {
onLoadOrDismiss('loaded');
setIsModalOpen(false);
Expand Down
20 changes: 6 additions & 14 deletions gbajs3/src/components/modals/upload-rom.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ describe('<UploadRomModal />', () => {
vi.spyOn(contextHooks, 'useEmulatorContext').mockImplementation(() => ({
...originalEmulator(),
emulator: {
uploadRom: uploadRomSpy,
filePaths: () => ({
gamePath: '/games'
})
uploadRom: uploadRomSpy
} as GBAEmulator
}));

Expand All @@ -67,11 +64,12 @@ describe('<UploadRomModal />', () => {

await userEvent.click(screen.getByRole('button', { name: 'Upload' }));

expect(uploadRomSpy).toHaveBeenCalledOnce();
expect(uploadRomSpy).toHaveBeenCalledWith(testRom, expect.anything());

expect(syncActionIfEnabledSpy).toHaveBeenCalledOnce();
expect(runGameSpy).toHaveBeenCalledOnce();
expect(runGameSpy).toHaveBeenCalledWith('/games/rom1.gba');
expect(runGameSpy).toHaveBeenCalledWith('rom1.gba');
expect(setIsModalOpenSpy).toHaveBeenCalledWith(false);
});

Expand All @@ -94,10 +92,7 @@ describe('<UploadRomModal />', () => {

// needs to be a consistent object
const testEmu = {
uploadRom: uploadRomSpy,
filePaths: () => ({
gamePath: '/games'
})
uploadRom: uploadRomSpy
} as GBAEmulator;

vi.spyOn(contextHooks, 'useModalContext').mockImplementation(() => ({
Expand Down Expand Up @@ -140,7 +135,7 @@ describe('<UploadRomModal />', () => {

expect(syncActionIfEnabledSpy).toHaveBeenCalledOnce();
expect(runGameSpy).toHaveBeenCalledOnce();
expect(runGameSpy).toHaveBeenCalledWith('/games/good_rom.gba');
expect(runGameSpy).toHaveBeenCalledWith('good_rom.gba');
expect(setIsModalOpenSpy).toHaveBeenCalledWith(false);
});

Expand All @@ -158,10 +153,7 @@ describe('<UploadRomModal />', () => {
...originalEmulator(),
emulator: {
uploadRom: uploadRomSpy,
run: emulatorRunSpy,
filePaths: () => ({
gamePath: '/games'
})
run: emulatorRunSpy
} as GBAEmulator
}));

Expand Down
8 changes: 2 additions & 6 deletions gbajs3/src/components/modals/upload-rom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ export const UploadRomModal = () => {
if (shouldUploadExternalRom) {
const runCallback = () => {
syncActionIfEnabled();
const hasSucceeded = runGame(
emulator?.filePaths().gamePath + '/' + externalRomFile.name
);
const hasSucceeded = runGame(externalRomFile.name);
if (hasSucceeded) {
setIsModalOpen(false);
}
Expand Down Expand Up @@ -96,9 +94,7 @@ export const UploadRomModal = () => {

const runCallback = () => {
syncActionIfEnabled();
const hasSucceeded = runGame(
emulator?.filePaths().gamePath + '/' + romFile.name
);
const hasSucceeded = runGame(romFile.name);
if (hasSucceeded) {
setIsModalOpen(false);
}
Expand Down
13 changes: 4 additions & 9 deletions gbajs3/src/hooks/emulator/use-quick-reload.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('useQuickReload hook', () => {
setCanvas: vi.fn(),
canvas: null,
emulator: {
getCurrentGameName: () => undefined,
quickReload: emulatorQuickReloadSpy
} as GBAEmulator
}));
Expand Down Expand Up @@ -46,9 +47,6 @@ describe('useQuickReload hook', () => {
setCanvas: vi.fn(),
canvas: null,
emulator: {
filePaths: () => ({
gamePath: '/games'
}),
getCurrentGameName: () => 'some_rom.gba'
} as GBAEmulator
}));
Expand All @@ -67,7 +65,7 @@ describe('useQuickReload hook', () => {
});

expect(runGameSpy).toHaveBeenCalledOnce();
expect(runGameSpy).toHaveBeenCalledWith('/games/some_rom.gba');
expect(runGameSpy).toHaveBeenCalledWith('some_rom.gba');
expect(emulatorQuickReloadSpy).not.toHaveBeenCalled();

expect(setIsRunningSpy).toHaveBeenCalledOnce();
Expand All @@ -79,10 +77,7 @@ describe('useQuickReload hook', () => {
const runGameSpy = vi.fn(() => true);
const setIsRunningSpy = vi.fn();

localStorage.setItem(
emulatorGameNameLocalStorageKey,
'"/games/some_rom_2.gba"'
);
localStorage.setItem(emulatorGameNameLocalStorageKey, '"some_rom_2.gba"');

vi.spyOn(contextHooks, 'useEmulatorContext').mockImplementation(() => ({
setCanvas: vi.fn(),
Expand All @@ -106,7 +101,7 @@ describe('useQuickReload hook', () => {
});

expect(runGameSpy).toHaveBeenCalledOnce();
expect(runGameSpy).toHaveBeenCalledWith('/games/some_rom_2.gba');
expect(runGameSpy).toHaveBeenCalledWith('some_rom_2.gba');
expect(emulatorQuickReloadSpy).not.toHaveBeenCalled();

expect(setIsRunningSpy).toHaveBeenCalledOnce();
Expand Down
13 changes: 5 additions & 8 deletions gbajs3/src/hooks/emulator/use-quick-reload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ export const useQuickReload = () => {
);

const quickReload = useCallback(() => {
const currentGameName = emulator?.getCurrentGameName();

if (isRunning) {
emulator?.quickReload();
} else if (emulator?.getCurrentGameName()) {
const isSuccessfulRun = runGame(
emulator.filePaths().gamePath + '/' + emulator.getCurrentGameName()
);
setIsRunning(!!isSuccessfulRun);
} else if (storedGameName) {
const isSuccessfulRun = runGame(storedGameName);
setIsRunning(!!isSuccessfulRun);
} else {
const gameName = currentGameName ?? storedGameName;
if (gameName) setIsRunning(runGame(gameName));
}
}, [emulator, isRunning, setIsRunning, runGame, storedGameName]);

Expand Down
18 changes: 12 additions & 6 deletions gbajs3/src/hooks/emulator/use-run-game.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ describe('useRunGame hook', () => {
canvas: null,
emulator: {
run: emulatorRunSpy,
setVolume: emulatorSetVolumeSpy
setVolume: emulatorSetVolumeSpy,
filePaths: () => ({
gamePath: '/data/games'
})
} as GBAEmulator
}));

Expand All @@ -49,19 +52,19 @@ describe('useRunGame hook', () => {
const { result } = renderHookWithContext(() => useRunGame());

act(() => {
expect(result.current('/games/some_rom.gba')).toBeTruthy();
expect(result.current('some_rom.gba')).toBeTruthy();
});

expect(emulatorRunSpy).toHaveBeenCalledOnce();
expect(emulatorRunSpy).toHaveBeenCalledWith('/games/some_rom.gba');
expect(emulatorRunSpy).toHaveBeenCalledWith('/data/games/some_rom.gba');

expect(setIsRunningSpy).toHaveBeenCalledOnce();
expect(setIsRunningSpy).toHaveBeenCalledWith(true);

// set stored game name
expect(setItemSpy).toHaveBeenCalledWith(
emulatorGameNameLocalStorageKey,
'"/games/some_rom.gba"'
'"some_rom.gba"'
);

// set volume
Expand Down Expand Up @@ -97,7 +100,10 @@ describe('useRunGame hook', () => {
isFastForwardEnabled: () => false,
setVolume: vi.fn() as (v: number) => void,
remapKeyBindings: emulatorRemapKeyBindingsSpy,
setFastForwardMultiplier: emulatorSetFastForwardMultiplierSpy
setFastForwardMultiplier: emulatorSetFastForwardMultiplierSpy,
filePaths: () => ({
gamePath: '/data/games'
})
} as GBAEmulator
}));

Expand All @@ -115,7 +121,7 @@ describe('useRunGame hook', () => {
const { result } = renderHookWithContext(() => useRunGame());

act(() => {
expect(result.current('/games/some_rom.gba')).toBeTruthy();
expect(result.current('some_rom.gba')).toBeTruthy();
});

expect(emulatorRemapKeyBindingsSpy).toHaveBeenCalledOnce();
Expand Down
8 changes: 5 additions & 3 deletions gbajs3/src/hooks/emulator/use-run-game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ export const useRunGame = () => {
const { addCallbacks } = useAddCallbacks();

const run = useCallback(
(romPath: string) => {
(romName: string) => {
const romPath = `${emulator?.filePaths().gamePath}/${romName}`;
const isSuccessfulRun = emulator?.run(romPath);
setIsRunning(!!isSuccessfulRun);
setStoredGameName(romPath);
emulator?.setVolume(currentEmulatorVolume);
setStoredGameName(romName);

if (isSuccessfulRun) {
emulator?.setVolume(currentEmulatorVolume);

if (currentKeyBindings) emulator?.remapKeyBindings(currentKeyBindings);

if (fastForwardMultiplier > 1 && !emulator?.isFastForwardEnabled())
Expand Down
Loading