From ea29b7369d9014f61b98569f324c12d5eec1a464 Mon Sep 17 00:00:00 2001 From: Gregg Tavares Date: Fri, 28 Aug 2015 18:23:00 +0100 Subject: [PATCH] add support for happyFunTimes.templateFileOptions --- lib/gameinfo.js | 33 ++++++++++++++++++++++----- server/hft-server.js | 30 ++++++++++++++++++++++-- templates/0.x.x/controller.index.html | 2 +- templates/0.x.x/game.gameview.html | 2 +- 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/lib/gameinfo.js b/lib/gameinfo.js index f6b1e479..74b9df7b 100644 --- a/lib/gameinfo.js +++ b/lib/gameinfo.js @@ -78,6 +78,11 @@ var faviconRE = /^favicon\.(jpg|png|gif)$/i; * @property {HappyFunTimes~Info} happyFunTimes */ +/** + * @typedef {Object} HFT~TemplateInfo + * @property {string} name name of template + */ + /** * @typedef {Object} HFT~RuntimeInfo * @property {NPM~Info} info @@ -90,8 +95,8 @@ var faviconRE = /^favicon\.(jpg|png|gif)$/i; * @property {string} gameExecutable the baseDir relative path * to the game's executable if it's a native * executable. Like a Unity game for example. - * @property {string?} screenshotUrl - * @property {string?} gameExecutable (not used?) + * @property {string} [screenshotUrl] + * @property {string} [gameExecutable] (not used?) * @property {string} htmlPath Path to html files (see rootPath) * @property {string} rootPath path to all files. * Originally htmlPath and rootPath where the same but @@ -100,6 +105,10 @@ var faviconRE = /^favicon\.(jpg|png|gif)$/i; * @property {string} packagePath. Path to package.json * Originally was /package.json but now can also be * /Assets/WebPlayerTemplates/HappyFunTimes/package.json + * @property {Object. * * @property {string[]} files added in addGamesByList. It's used * by uninstall @@ -536,10 +545,10 @@ GameInfo.prototype.parseGameInfo = function(contents, packagePath, rootPath) { var jsType = "hft-late"; if (features.useScriptTag) { - game.afterScripts.push(createScriptTag({src: "scripts/game.js"})); - controller.afterScripts.push(createScriptTag({src: "scripts/controller.js", type: jsType})); + game.afterScripts.push(createScriptTag({src: "scripts/%(filename)s.js"})); + controller.afterScripts.push(createScriptTag({src: "scripts/%(filename)s.js", type: jsType})); } else { - game.afterScripts.push(createScriptTag({src: "/3rdparty/require.js", "data-main": "scripts/game.js"})); + game.afterScripts.push(createScriptTag({src: "/3rdparty/require.js", "data-main": "scripts/%(filename)s.js"})); game.afterScripts.push(createScriptTag({}, [ "requirejs.config({", " paths: {", @@ -549,7 +558,7 @@ GameInfo.prototype.parseGameInfo = function(contents, packagePath, rootPath) { " },", "});", ].join("\n"))); - controller.afterScripts.push(createScriptTag({src: "/3rdparty/require.js", "data-main": "scripts/controller.js", type: jsType})); + controller.afterScripts.push(createScriptTag({src: "/3rdparty/require.js", "data-main": "scripts/%(filename)s.js", type: jsType})); controller.afterScripts.push(createScriptTag({type: jsType}, [ "requirejs.config({", " paths: {", @@ -606,6 +615,18 @@ GameInfo.prototype.parseGameInfo = function(contents, packagePath, rootPath) { runtimeInfo.templateUrls[fullUrl] = true; }); + runtimeInfo.templateFileOptions = {}; + if (hftInfo.templateFileOptions && !semver.gte(hftInfo.apiVersion, "1.15.0")) { + console.error("error: package.json apiVersion must be 1.15.0 or greater to use templateFileOptions"); + return; + } + (hftInfo.templateFileOptions || []).forEach(function(options) { + var info = { + urlInfo: options, + }; + runtimeInfo.templateFileOptions[options.filename] = info; + }); + } catch (e) { console.error("ERROR: Parsing " + packagePath); throw e; diff --git a/server/hft-server.js b/server/hft-server.js index 18d24ad3..5f2ec1f7 100644 --- a/server/hft-server.js +++ b/server/hft-server.js @@ -454,6 +454,7 @@ var HFTServer = function(options, startedCallback) { var addTemplateInsertedPath = function(theApp, pathRegex, templateName, contentPath) { theApp.get(pathRegex, function(req, res) { var gameId = req.params[0]; + var filename = req.params[1] || templateName; var runtimeInfo = g.gameDB.getGameById(gameId); if (!runtimeInfo) { var msg = [ @@ -465,6 +466,14 @@ var HFTServer = function(options, startedCallback) { return send404(res, msg); } + if (!templateName) { + contentPath = filename + ".html"; + var urlRuntimeInfo = runtimeInfo.templateFileOptions[contentPath]; + if (urlRuntimeInfo && urlRuntimeInfo.urlInfo) { + templateName = urlRuntimeInfo.urlInfo.template; + } + } + if (!runtimeInfo.useTemplate[templateName]) { return sendGameRequestedFile(req, res); } @@ -481,7 +490,23 @@ var HFTServer = function(options, startedCallback) { } sendFileResponse(req, res, contentFullPath, function(str) { debug("doing substitutions for:", contentPath); + var scriptParams = { + filename: filename, + }; var result = strings.replaceParams(templateData.toString(), [ + { + filename: filename, + pages: { + game: { + beforeScripts: strings.replaceParams(runtimeInfo.pages.game.beforeScripts, scriptParams), + afterScripts: strings.replaceParams(runtimeInfo.pages.game.afterScripts, scriptParams), + }, + controller: { + beforeScripts: strings.replaceParams(runtimeInfo.pages.controller.beforeScripts, scriptParams), + afterScripts: strings.replaceParams(runtimeInfo.pages.controller.afterScripts, scriptParams), + } + }, + }, runtimeInfo, { content: str, @@ -530,8 +555,9 @@ var HFTServer = function(options, startedCallback) { var src = "define([], function() { return " + JSON.stringify(data) + "; })\n"; sendStringResponse(res, src, "application/javascript"); }); - addTemplateInsertedPath(app, /^\/games\/(.*?)\/index.html$/, 'controller', 'controller.html'); - addTemplateInsertedPath(app, /^\/games\/(.*?)\/gameview.html$/, 'game', 'game.html'); + addTemplateInsertedPath(app, /^\/games\/(.*?)\/index\.html$/, 'controller', 'controller.html'); + addTemplateInsertedPath(app, /^\/games\/(.*?)\/gameview\.html$/, 'game', 'game.html'); + addTemplateInsertedPath(app, /^\/games\/(.*?)\/(.*?)\.html$/); app.get(/^\/games\/(.*?)\/runtime-scripts\/traceur-runtime.js$/, function(req, res) { //var gameId = req.params[0]; var fullPath = path.join(__dirname, '..', 'node_modules', 'traceur', 'bin', 'traceur-runtime.js'); diff --git a/templates/0.x.x/controller.index.html b/templates/0.x.x/controller.index.html index 12961afb..489ce739 100644 --- a/templates/0.x.x/controller.index.html +++ b/templates/0.x.x/controller.index.html @@ -43,7 +43,7 @@ %(info.name)s Controls - +