Skip to content

Commit

Permalink
feat: update default rom resolve function to use retrobrews
Browse files Browse the repository at this point in the history
  • Loading branch information
arianrhodsandlot committed Oct 6, 2023
1 parent d859ec5 commit 15e4951
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
5 changes: 5 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@ <h2>Example 2</h2>
<div>
<button id="run-example-2">run</button>
</div>

<h2>Example 3</h2>
<div>
<button id="run-example-3">run</button>
</div>
</body>
</html>
27 changes: 4 additions & 23 deletions examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,12 @@
import { Nostalgist } from '../src'

Nostalgist.configure({
resolveCoreJs({ core }) {
return `/cores/${core}_libretro.js`
},

resolveCoreWasm({ core }) {
return `/cores/${core}_libretro.wasm`
},

resolveRom({ options: { core }, file }) {
const directory = {
genesis_plus_gx: 'megadrive',
fceumm: 'nes',
}[`${core}`]
if (directory && typeof file === 'string') {
return `/roms/${encodeURIComponent(directory)}/${encodeURIComponent(file)}`
}
},
})

async function runExample1() {
await Nostalgist.nes('Alter Ego.nes')
await Nostalgist.nes('assimilate.nes')
}

async function runExample2() {
const nostalgist = await Nostalgist.launch({
rom: '30YearsOfNintendont.zip',
rom: 'astroperdido.bin',
core: 'genesis_plus_gx',
})
await new Promise((resolve) => setTimeout(resolve, 1000))
Expand All @@ -35,12 +15,13 @@ async function runExample2() {
}

async function runExample3() {
await Nostalgist.gba('super tile bros.nes')
await Nostalgist.gba('awerewolftale.gba')
}

function main() {
document.body.querySelector('#run-example-1').addEventListener('click', runExample1)
document.body.querySelector('#run-example-2').addEventListener('click', runExample2)
document.body.querySelector('#run-example-3').addEventListener('click', runExample3)
}

main()
33 changes: 30 additions & 3 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@ const defaultRetroarchConfig: RetroArchConfig = {
input_player4_analog_dpad_mode: 1,
}

const coreBaseUrl = 'https://cdn.jsdelivr.net/gh'
const cdnBaseUrl = 'https://cdn.jsdelivr.net/gh'

const coreRepo = 'arianrhodsandlot/retroarch-emscripten-build'
const coreVersion = 'v1.16.0'
const coreDirectory = 'retroarch'

function isUrl(string: string) {
return string.startsWith('http://') || string.startsWith('https://') || string.startsWith('//')

Check failure on line 47 in src/options.ts

View workflow job for this annotation

GitHub Actions / test

Replace `·string.startsWith('https://')·||·` with `string.startsWith('https://')·||`
}

export function getDefaultOptions() {
const defaultOptions: Omit<NostalgistOptions, 'core'> = {
element: '#canvas',
Expand All @@ -50,14 +55,36 @@ export function getDefaultOptions() {
retroarchCoreConfig: {},

resolveCoreJs({ core }) {
return `${coreBaseUrl}/${coreRepo}@${coreVersion}/${coreDirectory}/${core}_libretro.js`
return `${cdnBaseUrl}/${coreRepo}@${coreVersion}/${coreDirectory}/${core}_libretro.js`
},

resolveCoreWasm({ core }) {
return `${coreBaseUrl}/${coreRepo}@${coreVersion}/${coreDirectory}/${core}_libretro.wasm`
return `${cdnBaseUrl}/${coreRepo}@${coreVersion}/${coreDirectory}/${core}_libretro.wasm`
},

resolveRom({ file }) {

Check failure on line 65 in src/options.ts

View workflow job for this annotation

GitHub Actions / test

Refactor this function to reduce its Cognitive Complexity from 17 to the 15 allowed
if (typeof file === 'string') {

Check failure on line 66 in src/options.ts

View workflow job for this annotation

GitHub Actions / test

Merge this if statement with the nested one
if (!isUrl(file)) {

Check failure on line 67 in src/options.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected `if` as the only statement in a `if` block without `else`
let romRepo = ''
if (file.endsWith('.nes')) {
romRepo = 'retrobrews/nes-games'
} else if (file.endsWith('.sfc')) {
romRepo = 'retrobrews/snes-games'
} else if (file.endsWith('.gb') || file.endsWith('.gbc')) {
romRepo = 'retrobrews/gbc-games'
} else if (file.endsWith('.gba')) {
romRepo = 'retrobrews/gba-games'
} else if (file.endsWith('.sms')) {
romRepo = 'retrobrews/sms-games'
} else if (file.endsWith('.md') || file.endsWith('.bin')) {
romRepo = 'retrobrews/md-games'
}
if (romRepo) {
const encodedFile = encodeURIComponent(file)
return `${cdnBaseUrl}/${romRepo}@master/${encodedFile}`
}
}

Check failure on line 86 in src/options.ts

View workflow job for this annotation

GitHub Actions / test

Delete `·`
}
return file || []
},

Expand Down

0 comments on commit 15e4951

Please sign in to comment.