-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate ROM hacks with modified palettes 💥.
- Loading branch information
Showing
20 changed files
with
381 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { useRef, useState } from 'react'; | ||
import { useRom } from '../contexts/RomContext'; | ||
import { prependNesHeader, inject, debugArrayBuffer } from '../lib/romUtils'; | ||
import serialisePalette from '../lib/serialiser/serialisePalette'; | ||
|
||
const type = 'application/x-nes-rom'; | ||
const fileName = 'mm-hack.nes'; | ||
|
||
const DownloadRom = ({ children }) => { | ||
const { prg, resources } = useRom(); | ||
const [aHref, setAHref] = useState(null); | ||
const aRef = useRef(null); | ||
|
||
if (prg === null) { | ||
return ( | ||
<button | ||
className="text-slate-500 opacity-50" | ||
disabled> | ||
{children} | ||
</button> | ||
); | ||
} | ||
|
||
// Inject the new content into the PRG and return a NES ROM buffer. | ||
const generateRomHackFile = () => { | ||
const screens = [...resources.titles, ...resources.rooms]; | ||
|
||
// Update screen titles and rooms palette. | ||
for (let i = 0; i < screens.length; i++) { | ||
const screen = screens[i]; | ||
const { offset, size, id } = screen.metadata; | ||
if (!size) { | ||
continue; | ||
} | ||
|
||
const { from, to } = screen.map.find(({ type }) => type === 'palette'); | ||
const buffer = serialisePalette(screen.palette); | ||
|
||
const overwrites = inject(prg, buffer, offset + from, to - from); | ||
if (overwrites) { | ||
console.log('Injecting screen \x1b[33m%i\x1b[0m palette', id); | ||
} | ||
} | ||
|
||
// Add the iNES header to make it playable on emulators. | ||
return prependNesHeader(prg); | ||
}; | ||
|
||
return ( | ||
<> | ||
<button | ||
className="text-slate-500 transition hover:text-slate-800 hover:dark:text-slate-200" | ||
onClick={() => { | ||
const rom = generateRomHackFile(); | ||
setAHref(window.URL.createObjectURL(new Blob([rom], { type }))); | ||
|
||
// Needs to render first. | ||
setTimeout(() => { | ||
// Trigger the download by simulating a click. | ||
aRef.current.click(); | ||
window.URL.revokeObjectURL(aHref); | ||
}); | ||
}}> | ||
{children} | ||
</button> | ||
{/* eslint-disable-next-line jsx-a11y/anchor-has-content */} | ||
<a | ||
className="hidden" | ||
href={aHref} | ||
download={fileName} | ||
ref={aRef} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
export default DownloadRom; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import Header from './Header'; | ||
import Footer from './Footer'; | ||
|
||
export default function Layout({ children }) { | ||
return ( | ||
<div className="flex h-dvh flex-col"> | ||
<Header /> | ||
<div className="mx-auto flex w-full max-w-7xl grow items-stretch divide-x divide-slate-500 overflow-y-auto"> | ||
{children} | ||
</div> | ||
<Footer /> | ||
const Layout = ({ children }) => ( | ||
<div className="flex h-dvh flex-col"> | ||
<Header /> | ||
<div className="mx-auto flex w-full max-w-7xl grow items-stretch divide-x divide-slate-500 overflow-y-auto"> | ||
{children} | ||
</div> | ||
); | ||
} | ||
<Footer /> | ||
</div> | ||
); | ||
|
||
export default Layout; |
Oops, something went wrong.