Skip to content

Commit

Permalink
add support for happyFunTimes.templateFileOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Aug 28, 2015
1 parent 5911724 commit ea29b73
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 10 deletions.
33 changes: 27 additions & 6 deletions lib/gameinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -100,6 +105,10 @@ var faviconRE = /^favicon\.(jpg|png|gif)$/i;
* @property {string} packagePath. Path to package.json
* Originally was <rootPath>/package.json but now can also be
* <rootPath>/Assets/WebPlayerTemplates/HappyFunTimes/package.json
* @property {Object.<string, boolean} templateUrls which urls need substitutions.
* This is poorly named. It means treat this URL like template by calling string.replaceParams
* on it.
* @property {Object.<string>
*
* @property {string[]} files added in addGamesByList. It's used
* by uninstall
Expand Down Expand Up @@ -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: {",
Expand All @@ -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: {",
Expand Down Expand Up @@ -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;
Expand Down
30 changes: 28 additions & 2 deletions server/hft-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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);
}
Expand All @@ -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,
Expand Down Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion templates/0.x.x/controller.index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<meta name="format-detection" content="telephone=no" />
<title>%(info.name)s Controls</title>
<link rel="stylesheet" href="../../css/controllers.css">
<link rel="stylesheet" href="css/controller.css">
<link rel="stylesheet" href="css/%(filename)s.css">
<link rel="shortcut icon" href="%(urls.favicon)s" type="image/png">
<link rel="apple-touch-icon" href="%(urls.favicon)s">
<script>
Expand Down
2 changes: 1 addition & 1 deletion templates/0.x.x/game.gameview.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<title>%(info.name)s</title>
<link href="%(urls.favicon)s" rel="shortcut icon" type="image/png">
<link rel="stylesheet" href="../../css/games.css">
<link rel="stylesheet" href="css/game.css">
<link rel="stylesheet" href="css/%(filename)s.css">
<script>
%(hftSettings)s
</script>
Expand Down

0 comments on commit ea29b73

Please sign in to comment.