implement loading from save states

This commit is contained in:
Spencer Kuzara
2025-08-22 16:25:04 +09:00
parent 2d655bc33e
commit a5b3eea698
2 changed files with 162 additions and 10 deletions

View File

@@ -121,11 +121,54 @@
Details
</button>
</div>
<div
v-if="rom?.user_states?.length"
class="mt-4"
>
<h3 class="text-[10px] uppercase tracking-wider text-gray-400 font-semibold mb-2">
SAVE STATES
</h3>
<div
ref="statesRef"
class="w-full overflow-x-auto overflow-y-hidden no-scrollbar"
>
<div class="flex items-center gap-3 py-1 px-2 min-w-max">
<button
v-for="(st,i) in rom.user_states"
:key="st.id"
:ref="el => registerStateEl(el as HTMLElement|null, i)"
class="group relative rounded-md overflow-hidden flex flex-col bg-white/5 border border-white/10 hover:bg-white/10 transition-all w-[140px] h-[80px]"
:class="{ 'ring-2 ring-[var(--accent-2)] scale-[1.05] shadow-[0_0_0_2px_var(--accent-2)]': selectedZone==='states' && selectedStateIndex===i }"
:aria-label="'State from ' + relativeTime(st.updated_at)"
@click="startWithState(i)"
>
<div
v-if="!st.screenshot?.download_path"
class="absolute inset-0 flex items-center justify-center text-[10px] text-white/40 font-medium select-none"
>
STATE
</div>
<img
v-else
:src="st.screenshot.download_path"
:alt="'State screenshot ' + (i+1)"
class="w-full h-full object-cover select-none pointer-events-none"
draggable="false"
loading="lazy"
>
<div class="absolute bottom-0 left-0 right-0 bg-gradient-to-t from-black/70 to-black/0 px-2 pt-4 pb-1 text-[10px] text-white/80 tracking-wide flex justify-between items-end">
<span class="truncate max-w-[90%]">{{ relativeTime(st.updated_at) }}</span>
</div>
</button>
</div>
</div>
</div>
</div>
</div>
</section>
<!-- SCREENSHOTS (fixed bottom, dark background) -->
<!-- SCREENSHOTS -->
<section
v-if="screenshotUrls.length"
class="fixed bottom-0 inset-x-0 z-30 py-3 md:py-4 bg-black/40 backdrop-blur-md border-t border-white/10"
@@ -134,7 +177,10 @@
<h3 class="text-gray-300 text-xs md:text-sm font-semibold uppercase tracking-wide mb-4">
Screenshots
</h3>
<div ref="shotsRef" class="w-full overflow-x-auto overflow-y-hidden no-scrollbar">
<div
ref="shotsRef"
class="w-full overflow-x-auto overflow-y-hidden no-scrollbar"
>
<div class="flex items-center gap-3 md:gap-4 py-6 px-2 min-w-max">
<button
v-for="(src, idx) in screenshotUrls"
@@ -295,6 +341,7 @@
<script setup lang="ts">
import { computed, onMounted, onUnmounted, ref, watch, nextTick } from 'vue';
import { formatDistanceToNow } from 'date-fns';
import { useRoute, useRouter } from 'vue-router';
import romApi from '@/services/api/rom';
import type { DetailedRomSchema } from '@/__generated__/models/DetailedRomSchema';
@@ -311,14 +358,17 @@ const rom = ref<DetailedRomSchema>();
const loading = ref(true);
const error = ref('');
const unsupported = ref(false);
type FocusZone = 'play'|'description'|'details'|'shots'|'lightbox';
type FocusZone = 'play'|'description'|'details'|'shots'|'lightbox'|'states';
const selectedZone = ref<FocusZone>('play');
const selectedStateIndex = ref(0);
const showDescription = ref(false);
const showDetails = ref(false);
const showLightbox = ref(false);
const selectedShot = ref(0);
const shotsRef = ref<HTMLDivElement|null>(null);
const shotEls = ref<HTMLElement[]>([]);
const statesRef = ref<HTMLDivElement|null>(null);
const stateEls = ref<HTMLElement[]>([]);
const descOverlayRef = ref<HTMLElement|null>(null);
const detailsOverlayRef = ref<HTMLElement|null>(null);
@@ -375,10 +425,12 @@ function handleAction(action: InputAction): boolean {
// Main focus navigation
switch(selectedZone.value){
case 'play':
case 'play':
if(action==='moveUp' && rom.value?.summary) { selectedZone.value='description'; return true; }
if(action==='moveRight') { selectedZone.value='details'; return true; }
if(action==='moveDown') { if(screenshotUrls.value.length){ selectedZone.value='shots'; selectedShot.value = 0; nextTick(scrollShotsToSelected); } return true; }
if(action==='moveDown') {
if(rom.value?.user_states?.length){ selectedZone.value='states'; selectedStateIndex.value = 0; return true; }
if(screenshotUrls.value.length){ selectedZone.value='shots'; selectedShot.value = 0; nextTick(scrollShotsToSelected); } return true; }
if(action==='confirm') { play(); return true; }
if(action==='back') { router.back(); return true; }
return false;
@@ -389,14 +441,24 @@ function handleAction(action: InputAction): boolean {
return false;
case 'details':
if(action==='moveLeft') { selectedZone.value='play'; return true; }
if(action==='moveDown') { if(screenshotUrls.value.length){ selectedZone.value='shots'; selectedShot.value = 0; nextTick(scrollShotsToSelected); } return true; }
if(action==='moveDown') {
if(rom.value?.user_states?.length){ selectedZone.value='states'; selectedStateIndex.value = 0; return true; }
if(screenshotUrls.value.length){ selectedZone.value='shots'; selectedShot.value = 0; nextTick(scrollShotsToSelected); } return true; }
if(action==='confirm') { openDetails(); return true; }
if(action==='back') { router.back(); return true; }
return false;
case 'shots':
case 'states':
if(action==='moveUp') { selectedZone.value='play'; return true; }
if(action==='moveRight') { if(screenshotUrls.value.length){ selectedShot.value = (selectedShot.value + 1) % screenshotUrls.value.length; nextTick(scrollShotsToSelected); } return true; }
if(action==='moveLeft') { if(screenshotUrls.value.length){ selectedShot.value = (selectedShot.value - 1 + screenshotUrls.value.length) % screenshotUrls.value.length; nextTick(scrollShotsToSelected); } return true; }
if(action==='moveDown') { if(screenshotUrls.value.length){ selectedZone.value='shots'; selectedShot.value=0; nextTick(scrollShotsToSelected); } return true; }
if(action==='moveRight') { if(rom.value?.user_states){ selectedStateIndex.value = (selectedStateIndex.value + 1) % rom.value.user_states.length; nextTick(scrollStatesToSelected); return true; } }
if(action==='moveLeft') { if(rom.value?.user_states){ selectedStateIndex.value = (selectedStateIndex.value - 1 + rom.value.user_states.length) % rom.value.user_states.length; nextTick(scrollStatesToSelected); return true; } }
if(action==='confirm') { startWithState(selectedStateIndex.value); return true; }
if(action==='back') { router.back(); return true; }
return false;
case 'shots':
if(action==='moveUp') { selectedZone.value = rom.value?.user_states?.length ? 'states' : 'play'; return true; }
if(action==='moveRight') { if(screenshotUrls.value.length){ selectedShot.value = (selectedShot.value + 1) % screenshotUrls.value.length; nextTick(scrollShotsToSelected); } return true; }
if(action==='moveLeft') { if(screenshotUrls.value.length){ selectedShot.value = (selectedShot.value - 1 + screenshotUrls.value.length) % screenshotUrls.value.length; nextTick(scrollShotsToSelected); } return true; }
if(action==='confirm') { showLightbox.value = true; return true; }
if(action==='back') { router.back(); return true; }
return false;
@@ -407,7 +469,21 @@ function handleAction(action: InputAction): boolean {
function play(){
romApi.updateUserRomProps({ romId, data: {}, updateLastPlayed: true }).catch(()=>{});
router.push({ name: 'console-play', params: { rom: romId } });
const query: Record<string, number> = {};
if(currentStateId.value) query.state = currentStateId.value;
router.push({ name: 'console-play', params: { rom: romId }, query: Object.keys(query).length ? query : undefined });
}
const currentStateId = computed(() => rom.value?.user_states?.[selectedStateIndex.value]?.id);
function relativeTime(d: string | Date){
try{ return formatDistanceToNow(new Date(d), { addSuffix: true }); }catch{ return ''; }
}
function startWithState(i: number){
if(!rom.value?.user_states?.[i]) return;
selectedStateIndex.value = i;
play();
}
function registerShotEl(el: HTMLElement|null, idx: number){
@@ -415,6 +491,11 @@ function registerShotEl(el: HTMLElement|null, idx: number){
shotEls.value[idx] = el;
}
function registerStateEl(el: HTMLElement|null, idx: number){
if(!el) return;
stateEls.value[idx] = el;
}
function scrollShotsToSelected(){
const container = shotsRef.value;
const el = shotEls.value[selectedShot.value];
@@ -425,6 +506,16 @@ function scrollShotsToSelected(){
container.scrollTo({ left: desiredLeft, behavior: 'smooth' });
}
function scrollStatesToSelected(){
const container = statesRef.value;
const el = stateEls.value[selectedStateIndex.value];
if(!container || !el) return;
const cr = container.getBoundingClientRect();
const er = el.getBoundingClientRect();
const desiredLeft = el.offsetLeft - (cr.width/2) + (er.width/2);
container.scrollTo({ left: desiredLeft, behavior: 'smooth' });
}
// Screenshot URL helpers and error handler
function primaryScreenshotSrc(u: string): string {
// Use backend-provided URLs directly

View File

@@ -24,6 +24,8 @@ import firmwareApi from '@/services/api/firmware';
const route = useRoute();
const router = useRouter();
const romId = Number(route.params.rom);
const initialSaveId = route.query.save ? Number(route.query.save) : null;
const initialStateId = route.query.state ? Number(route.query.state) : null;
const showHint = ref(true);
let rafId = 0;
let lastPress: Record<number, number> = { 8: 0, 9: 0 };
@@ -66,6 +68,8 @@ async function boot(){
// Fetch rom details
const { data: rom } = await romApi.getRom({ romId });
const r = rom as DetailedRomSchema;
const selectedInitialSave = initialSaveId ? r.user_saves?.find(s => s.id === initialSaveId) : null;
const selectedInitialState = initialStateId ? r.user_states?.find(s => s.id === initialStateId) : null;
document.title = `${r.name} | Play`;
// Configure EmulatorJS globals
@@ -77,6 +81,13 @@ async function boot(){
w.EJS_controlScheme = getControlSchemeForPlatform(r.platform_slug);
w.EJS_threads = areThreadsRequiredForEJSCore(core);
w.EJS_gameID = r.id;
if(initialSaveId){
// Persist chosen save ID for later logic (EmulatorJS integration for loading server saves would go here)
try{ localStorage.setItem(`player:${r.id}:initial_save_id`, String(initialSaveId)); }catch{/* ignore */}
}
if(initialStateId){
try{ localStorage.setItem(`player:${r.id}:initial_state_id`, String(initialStateId)); }catch{/* ignore */}
}
// Disc selection persistence
const storedDisc = localStorage.getItem(`player:${r.id}:disc`);
const discId = storedDisc ? parseInt(storedDisc) : null;
@@ -105,6 +116,14 @@ async function boot(){
w.EJS_onGameStart = () => {
const e = (window as any).EJS_emulator;
if (!e) return;
const waitForGameManager = async () => {
const deadline = Date.now() + 5000; // 5s timeout
while(Date.now() < deadline){
if(e?.gameManager?.FS && e.gameManager.getSaveFilePath){ return true; }
await new Promise(r=>setTimeout(r,100));
}
return false;
};
const assignFirstPad = () => {
if (!e.gamepad) return;
if (!Array.isArray(e.gamepadSelection)) e.gamepadSelection = ['', '', '', ''];
@@ -121,6 +140,48 @@ async function boot(){
assignFirstPad();
// Also assign on future connections
try { e.gamepad?.on?.('connected', assignFirstPad); } catch { /* noop */ }
(async () => {
const ready = await waitForGameManager();
if(!ready){
console.warn('[ConsolePlay] Game manager not ready for save/state injection');
return;
}
const gm = e.gameManager;
// Load SAVE (battery / SRAM) if provided
if(selectedInitialSave?.download_path){
try {
const resp = await fetch(selectedInitialSave.download_path);
if(!resp.ok) throw new Error('Failed to fetch save');
const buf = new Uint8Array(await resp.arrayBuffer());
try {
const FS = gm.FS;
const path = gm.getSaveFilePath();
// Ensure dirs
const segs = path.split('/');
let accum = '';
for(let i=0;i<segs.length-1;i++){ if(!segs[i]) continue; accum += '/' + segs[i]; if(!FS.analyzePath(accum).exists) FS.mkdir(accum); }
if(FS.analyzePath(path).exists) FS.unlink(path);
FS.writeFile(path, buf);
gm.loadSaveFiles?.();
console.info('[ConsolePlay] Loaded server save into path', path);
} catch(err){ console.warn('[ConsolePlay] Failed writing save file', err); }
} catch(err){ console.warn('[ConsolePlay] Save download failed', err); }
}
// Load STATE if provided (fast-forward once core running)
if(selectedInitialState?.download_path){
try {
const resp = await fetch(selectedInitialState.download_path);
if(!resp.ok) throw new Error('Failed to fetch state');
const buf = new Uint8Array(await resp.arrayBuffer());
// Some cores need a couple frames; delay slightly
setTimeout(()=>{
try { gm.loadState?.(buf); console.info('[ConsolePlay] Applied server state'); }
catch(err){ console.warn('[ConsolePlay] Applying state failed', err); }
}, 500);
} catch(err){ console.warn('[ConsolePlay] State download failed', err); }
}
})();
};
// Load EmulatorJS loader