Skip to content

Commit

Permalink
[ROMM-1484] Manually set control scheme for sega games
Browse files Browse the repository at this point in the history
  • Loading branch information
gantoine committed Jan 14, 2025
1 parent 6e3d5d6 commit 9319e3d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
12 changes: 12 additions & 0 deletions backend/endpoints/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,18 @@ async def get_rom_content(
log.info(f"User {current_username} is downloading {rom.file_name}")

if not rom.multi:
# Serve the file directly in development mode for emulatorjs
if DEV_MODE:
return FileResponse(
path=rom_path,
filename=rom.file_name,
headers={
"Content-Disposition": f'attachment; filename="{quote(rom.file_name)}"',
"Content-Type": "application/octet-stream",
"Content-Length": str(rom.file_size_bytes),
},
)

return FileRedirectResponse(
download_path=Path(f"/library/{rom.full_path}"),
filename=rom.file_name,
Expand Down
37 changes: 36 additions & 1 deletion frontend/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ const _EJS_CORES_MAP = {
ps: ["pcsx_rearmed", "mednafen_psx_hw"],
psp: ["ppsspp"],
segacd: ["genesis_plus_gx", "picodrive"],
// sega32: ["picodrive"], // Broken: https://github.com/EmulatorJS/EmulatorJS/issues/579
sega32: ["picodrive"],
gamegear: ["genesis_plus_gx"],
sms: ["genesis_plus_gx"],
"sega-mark-iii": ["genesis_plus_gx"],
Expand Down Expand Up @@ -453,6 +453,41 @@ export function isEJSThreadsSupported(): boolean {
return typeof SharedArrayBuffer !== "undefined";
}

// This is a workaround to set the control scheme for Sega systems using the same cores
const _EJS_CONTROL_SCHEMES = {
segacd: "segaCD",
sega32: "sega32x",
gamegear: "segaGG",
sms: "segaMS",
"sega-mark-iii": "segaMS",
"sega-master-system-ii": "segaMS",
"master-system-super-compact": "segaMS",
"master-system-girl": "segaMS",
"genesis-slash-megadrive": "segaMD",
"sega-mega-drive-2-slash-genesis": "segaMD",
"sega-mega-jet": "segaMD",
"mega-pc": "segaMD",
"tera-drive": "segaMD",
"sega-nomad": "segaMD",
saturn: "segaSaturn",
};

type EJSControlSlug = keyof typeof _EJS_CONTROL_SCHEMES;

/**
* Get the control scheme for a given platform.
*
* @param platformSlug The platform slug.
* @returns The control scheme.
*/
export function getControlSchemeForPlatform(
platformSlug: string,
): string | null {
return platformSlug in _EJS_CONTROL_SCHEMES
? _EJS_CONTROL_SCHEMES[platformSlug as EJSControlSlug]
: null;
}

/**
* Check if Ruffle emulation is supported for a given platform.
*
Expand Down
10 changes: 9 additions & 1 deletion frontend/src/views/Player/EmulatorJS/Player.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import saveApi, { saveApi as api } from "@/services/api/save";
import screenshotApi from "@/services/api/screenshot";
import stateApi from "@/services/api/state";
import type { DetailedRom } from "@/stores/roms";
import { areThreadsRequiredForEJSCore, getSupportedEJSCores } from "@/utils";
import {
areThreadsRequiredForEJSCore,
getSupportedEJSCores,
getControlSchemeForPlatform,
} from "@/utils";
import { onBeforeUnmount, onMounted, ref } from "vue";
const props = defineProps<{
Expand Down Expand Up @@ -39,6 +43,7 @@ declare global {
EJS_startOnLoaded: boolean;
EJS_fullscreenOnLoaded: boolean;
EJS_threads: boolean;
EJS_controlScheme: string | null;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
EJS_emulator: any;
EJS_onGameStart: () => void;
Expand All @@ -52,6 +57,9 @@ declare global {
const supportedCores = getSupportedEJSCores(romRef.value.platform_slug);
window.EJS_core =
supportedCores.find((core) => core === props.core) ?? supportedCores[0];
window.EJS_controlScheme = getControlSchemeForPlatform(
romRef.value.platform_slug,
);
window.EJS_threads = areThreadsRequiredForEJSCore(window.EJS_core);
window.EJS_gameID = romRef.value.id;
window.EJS_gameUrl = `/api/roms/${romRef.value.id}/content/${romRef.value.file_name}`;
Expand Down

0 comments on commit 9319e3d

Please sign in to comment.