-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/ruffle' into develop
- Loading branch information
Showing
13 changed files
with
330 additions
and
50 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,139 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import * as flashpoint from 'flashpoint-launcher'; | ||
import { ConfigSchema, Game, GameLaunchInfo, GameMiddlewareConfig, GameMiddlewareDefaultConfig, IGameMiddleware } from 'flashpoint-launcher'; | ||
import { Readable } from 'stream'; | ||
import { buildBasicTemplate } from '../template/basic'; | ||
import * as path from 'path'; | ||
|
||
// Config Schema used to configure the middleware per game | ||
const schema: ConfigSchema = [ | ||
{ | ||
type: 'string', | ||
key: 'template', | ||
title: 'Template', | ||
description: 'Template to use when creating embed webpage', | ||
options: ['Automatic', 'Basic'], | ||
default: 'Automatic', | ||
}, | ||
{ | ||
type: 'string', | ||
key: 'url', | ||
title: 'Webpage URL', | ||
description: 'URL to serve the webpage from', | ||
optional: true, | ||
} | ||
]; | ||
|
||
// Stored config value map | ||
type RuffleEmbedConfig = { | ||
/** Player Options */ | ||
template: 'Automatic' | 'Basic'; | ||
url: string; | ||
}; | ||
|
||
const DEFAULT_CONFIG: Partial<RuffleEmbedConfig> = { | ||
template: 'Automatic' | ||
}; | ||
|
||
export class RuffleWebEmbedMiddleware implements IGameMiddleware { | ||
id = 'com.ruffle.middleware-embed'; | ||
name = 'Ruffle Web Embed'; | ||
|
||
constructor( | ||
private ruffleWebRoot: string | ||
) {} | ||
|
||
isValidVersion(version: string): boolean { | ||
return true; | ||
} | ||
|
||
async isValid(game: Game): Promise<boolean> { | ||
if (game.activeDataId && game.activeDataId >= 0) { | ||
const gameData = await flashpoint.gameData.findOne(game.activeDataId); | ||
if (gameData?.launchCommand.endsWith('.swf')) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
async execute(gameLaunchInfo: GameLaunchInfo, middlewareConfig: GameMiddlewareConfig): Promise<GameLaunchInfo> { | ||
// Cast our config values to the correct type | ||
const config = { | ||
...DEFAULT_CONFIG, | ||
...middlewareConfig.config | ||
} as Partial<RuffleEmbedConfig>; | ||
|
||
const launchArgs = coerceToStringArray(gameLaunchInfo.launchInfo.gameArgs); | ||
const sourceUrl = launchArgs[0]; | ||
|
||
// Generate a webpage for the given configuration | ||
let data: string | null = null; | ||
const pageUrl = config.url ? config.url : genEmbedUrl(launchArgs[0]); | ||
switch (config.template) { | ||
default: { | ||
// Save generated embed to file | ||
data = buildBasicTemplate(gameLaunchInfo.game, sourceUrl); | ||
break; | ||
} | ||
} | ||
|
||
if (!data ) { | ||
throw 'Webpage generated data was empty?'; | ||
} | ||
|
||
// Save generated webpage | ||
const rs = new Readable(); | ||
rs.push(data); | ||
rs.push(null); | ||
await flashpoint.middleware.writeGameFileByUrl(pageUrl, rs); | ||
|
||
// Replace launch arg | ||
launchArgs[0] = pageUrl; | ||
|
||
// Copy required ruffle script | ||
await flashpoint.middleware.copyGameFilesByUrl('http://ruffle.rs/', path.join(this.ruffleWebRoot, 'latest')); | ||
|
||
// Overwrite launch values | ||
gameLaunchInfo.launchInfo.gamePath = path.join(flashpoint.config.flashpointPath, 'FPSoftware\\StartChrome.bat'); | ||
gameLaunchInfo.launchInfo.gameArgs = launchArgs; | ||
|
||
return gameLaunchInfo; | ||
} | ||
|
||
getDefaultConfig(game: Game): GameMiddlewareDefaultConfig { | ||
return { | ||
version: 'latest', | ||
config: this.getConfigSchema('latest'), | ||
}; | ||
} | ||
|
||
getConfigSchema(version: string): ConfigSchema { | ||
// Only 1 kind of schema for now | ||
return schema; | ||
} | ||
|
||
upgradeConfig(version: string, config: any) { | ||
// UNIMPLEMENTED | ||
return config; | ||
} | ||
} | ||
|
||
function coerceToStringArray(arr: string[] | string): string[] { | ||
if (Array.isArray(arr)) { | ||
return arr; | ||
} else { | ||
return [arr]; | ||
} | ||
} | ||
|
||
// Creates an embed at the root of the current domain | ||
function genEmbedUrl(launchUrl: string): string { | ||
const url = new URL(launchUrl); | ||
if (url.pathname === '/embed.html') { | ||
url.pathname = '/embed_backup.html'; | ||
} else { | ||
url.pathname = '/embed.html'; | ||
} | ||
return url.href; | ||
} |
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,11 @@ | ||
import * as flashpoint from 'flashpoint-launcher'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import * as mustache from 'mustache'; | ||
|
||
export function buildBasicTemplate(game: flashpoint.Game, sourceUrl: string) { | ||
const templatesRoot = path.join(flashpoint.extensionPath, 'static', 'templates'); | ||
const data = fs.readFileSync(path.join(templatesRoot, 'basic.mustache'), 'utf8'); | ||
const styleData = fs.readFileSync(path.join(templatesRoot, 'basic.css'), 'utf8'); | ||
return mustache.render(data, { title: game.title, sourceUrl, styleData }); | ||
} |
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,32 @@ | ||
:root { | ||
font-family: Helvetica, Arial, sans-serif; | ||
} | ||
|
||
body { | ||
background-color: lightblue; | ||
} | ||
.page-content { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
} | ||
#embed-container { | ||
display: flex; | ||
max-width: 80%; | ||
max-height: 80%; | ||
border: 3px solid black; | ||
border-radius: 2px; | ||
} | ||
.game-title { | ||
font-size: 2rem; | ||
font-weight: bold; | ||
text-align: center; | ||
margin-top: 1rem; | ||
margin-bottom: 1rem; | ||
} | ||
.button-row { | ||
margin-top: 1rem; | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: left; | ||
} |
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,37 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>{{title}}</title> | ||
<style type="text/css"> | ||
{{{styleData}}} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="page-content"> | ||
<div class="game-title">{{title}}</div> | ||
<div id="embed-container"></div> | ||
<div class="button-row"> | ||
<button class="button-basic" onclick="(() => { | ||
window.player.enterFullscreen(); | ||
})()">Fullscreen</button> | ||
</div> | ||
|
||
<script> | ||
window.RufflePlayer = window.RufflePlayer || {}; | ||
window.RufflePlayer.config = { | ||
"allowScriptAccess": true, | ||
"autoplay": "on", | ||
} | ||
window.addEventListener("load", (event) => { | ||
const ruffle = window.RufflePlayer.newest(); | ||
const player = ruffle.createPlayer(); | ||
const container = document.getElementById("embed-container"); | ||
container.appendChild(player); | ||
player.load("{{{sourceUrl}}}"); | ||
window.player = player; | ||
}); | ||
</script> | ||
<script src="http://ruffle.rs/ruffle.js"></script> | ||
</div> | ||
</body> | ||
</html> |
Oops, something went wrong.