Skip to content

Commit ea29b73

Browse files
committed
add support for happyFunTimes.templateFileOptions
1 parent 5911724 commit ea29b73

File tree

4 files changed

+57
-10
lines changed

4 files changed

+57
-10
lines changed

lib/gameinfo.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ var faviconRE = /^favicon\.(jpg|png|gif)$/i;
7878
* @property {HappyFunTimes~Info} happyFunTimes
7979
*/
8080

81+
/**
82+
* @typedef {Object} HFT~TemplateInfo
83+
* @property {string} name name of template
84+
*/
85+
8186
/**
8287
* @typedef {Object} HFT~RuntimeInfo
8388
* @property {NPM~Info} info
@@ -90,8 +95,8 @@ var faviconRE = /^favicon\.(jpg|png|gif)$/i;
9095
* @property {string} gameExecutable the baseDir relative path
9196
* to the game's executable if it's a native
9297
* executable. Like a Unity game for example.
93-
* @property {string?} screenshotUrl
94-
* @property {string?} gameExecutable (not used?)
98+
* @property {string} [screenshotUrl]
99+
* @property {string} [gameExecutable] (not used?)
95100
* @property {string} htmlPath Path to html files (see rootPath)
96101
* @property {string} rootPath path to all files.
97102
* Originally htmlPath and rootPath where the same but
@@ -100,6 +105,10 @@ var faviconRE = /^favicon\.(jpg|png|gif)$/i;
100105
* @property {string} packagePath. Path to package.json
101106
* Originally was <rootPath>/package.json but now can also be
102107
* <rootPath>/Assets/WebPlayerTemplates/HappyFunTimes/package.json
108+
* @property {Object.<string, boolean} templateUrls which urls need substitutions.
109+
* This is poorly named. It means treat this URL like template by calling string.replaceParams
110+
* on it.
111+
* @property {Object.<string>
103112
*
104113
* @property {string[]} files added in addGamesByList. It's used
105114
* by uninstall
@@ -536,10 +545,10 @@ GameInfo.prototype.parseGameInfo = function(contents, packagePath, rootPath) {
536545

537546
var jsType = "hft-late";
538547
if (features.useScriptTag) {
539-
game.afterScripts.push(createScriptTag({src: "scripts/game.js"}));
540-
controller.afterScripts.push(createScriptTag({src: "scripts/controller.js", type: jsType}));
548+
game.afterScripts.push(createScriptTag({src: "scripts/%(filename)s.js"}));
549+
controller.afterScripts.push(createScriptTag({src: "scripts/%(filename)s.js", type: jsType}));
541550
} else {
542-
game.afterScripts.push(createScriptTag({src: "/3rdparty/require.js", "data-main": "scripts/game.js"}));
551+
game.afterScripts.push(createScriptTag({src: "/3rdparty/require.js", "data-main": "scripts/%(filename)s.js"}));
543552
game.afterScripts.push(createScriptTag({}, [
544553
"requirejs.config({",
545554
" paths: {",
@@ -549,7 +558,7 @@ GameInfo.prototype.parseGameInfo = function(contents, packagePath, rootPath) {
549558
" },",
550559
"});",
551560
].join("\n")));
552-
controller.afterScripts.push(createScriptTag({src: "/3rdparty/require.js", "data-main": "scripts/controller.js", type: jsType}));
561+
controller.afterScripts.push(createScriptTag({src: "/3rdparty/require.js", "data-main": "scripts/%(filename)s.js", type: jsType}));
553562
controller.afterScripts.push(createScriptTag({type: jsType}, [
554563
"requirejs.config({",
555564
" paths: {",
@@ -606,6 +615,18 @@ GameInfo.prototype.parseGameInfo = function(contents, packagePath, rootPath) {
606615
runtimeInfo.templateUrls[fullUrl] = true;
607616
});
608617

618+
runtimeInfo.templateFileOptions = {};
619+
if (hftInfo.templateFileOptions && !semver.gte(hftInfo.apiVersion, "1.15.0")) {
620+
console.error("error: package.json apiVersion must be 1.15.0 or greater to use templateFileOptions");
621+
return;
622+
}
623+
(hftInfo.templateFileOptions || []).forEach(function(options) {
624+
var info = {
625+
urlInfo: options,
626+
};
627+
runtimeInfo.templateFileOptions[options.filename] = info;
628+
});
629+
609630
} catch (e) {
610631
console.error("ERROR: Parsing " + packagePath);
611632
throw e;

server/hft-server.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ var HFTServer = function(options, startedCallback) {
454454
var addTemplateInsertedPath = function(theApp, pathRegex, templateName, contentPath) {
455455
theApp.get(pathRegex, function(req, res) {
456456
var gameId = req.params[0];
457+
var filename = req.params[1] || templateName;
457458
var runtimeInfo = g.gameDB.getGameById(gameId);
458459
if (!runtimeInfo) {
459460
var msg = [
@@ -465,6 +466,14 @@ var HFTServer = function(options, startedCallback) {
465466
return send404(res, msg);
466467
}
467468

469+
if (!templateName) {
470+
contentPath = filename + ".html";
471+
var urlRuntimeInfo = runtimeInfo.templateFileOptions[contentPath];
472+
if (urlRuntimeInfo && urlRuntimeInfo.urlInfo) {
473+
templateName = urlRuntimeInfo.urlInfo.template;
474+
}
475+
}
476+
468477
if (!runtimeInfo.useTemplate[templateName]) {
469478
return sendGameRequestedFile(req, res);
470479
}
@@ -481,7 +490,23 @@ var HFTServer = function(options, startedCallback) {
481490
}
482491
sendFileResponse(req, res, contentFullPath, function(str) {
483492
debug("doing substitutions for:", contentPath);
493+
var scriptParams = {
494+
filename: filename,
495+
};
484496
var result = strings.replaceParams(templateData.toString(), [
497+
{
498+
filename: filename,
499+
pages: {
500+
game: {
501+
beforeScripts: strings.replaceParams(runtimeInfo.pages.game.beforeScripts, scriptParams),
502+
afterScripts: strings.replaceParams(runtimeInfo.pages.game.afterScripts, scriptParams),
503+
},
504+
controller: {
505+
beforeScripts: strings.replaceParams(runtimeInfo.pages.controller.beforeScripts, scriptParams),
506+
afterScripts: strings.replaceParams(runtimeInfo.pages.controller.afterScripts, scriptParams),
507+
}
508+
},
509+
},
485510
runtimeInfo,
486511
{
487512
content: str,
@@ -530,8 +555,9 @@ var HFTServer = function(options, startedCallback) {
530555
var src = "define([], function() { return " + JSON.stringify(data) + "; })\n";
531556
sendStringResponse(res, src, "application/javascript");
532557
});
533-
addTemplateInsertedPath(app, /^\/games\/(.*?)\/index.html$/, 'controller', 'controller.html');
534-
addTemplateInsertedPath(app, /^\/games\/(.*?)\/gameview.html$/, 'game', 'game.html');
558+
addTemplateInsertedPath(app, /^\/games\/(.*?)\/index\.html$/, 'controller', 'controller.html');
559+
addTemplateInsertedPath(app, /^\/games\/(.*?)\/gameview\.html$/, 'game', 'game.html');
560+
addTemplateInsertedPath(app, /^\/games\/(.*?)\/(.*?)\.html$/);
535561
app.get(/^\/games\/(.*?)\/runtime-scripts\/traceur-runtime.js$/, function(req, res) {
536562
//var gameId = req.params[0];
537563
var fullPath = path.join(__dirname, '..', 'node_modules', 'traceur', 'bin', 'traceur-runtime.js');

templates/0.x.x/controller.index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<meta name="format-detection" content="telephone=no" />
4444
<title>%(info.name)s Controls</title>
4545
<link rel="stylesheet" href="../../css/controllers.css">
46-
<link rel="stylesheet" href="css/controller.css">
46+
<link rel="stylesheet" href="css/%(filename)s.css">
4747
<link rel="shortcut icon" href="%(urls.favicon)s" type="image/png">
4848
<link rel="apple-touch-icon" href="%(urls.favicon)s">
4949
<script>

templates/0.x.x/game.gameview.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<title>%(info.name)s</title>
4343
<link href="%(urls.favicon)s" rel="shortcut icon" type="image/png">
4444
<link rel="stylesheet" href="../../css/games.css">
45-
<link rel="stylesheet" href="css/game.css">
45+
<link rel="stylesheet" href="css/%(filename)s.css">
4646
<script>
4747
%(hftSettings)s
4848
</script>

0 commit comments

Comments
 (0)