Skip to content

Commit

Permalink
don't allow games for a different platform to be installed
Browse files Browse the repository at this point in the history
So can't install a Unity3D for Windows game on OSX for example.

Added this because drag and drop might make it too easy to try
to install the wrong file
  • Loading branch information
greggman committed Aug 30, 2015
1 parent 39044f3 commit d641c54
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 36 deletions.
4 changes: 4 additions & 0 deletions docs/changelist.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelist
==========

* 0.0.40

* don't allow games for a different platform to be installed.

* 0.0.39

* Games have context menu with "GetInfo", "Uninstall" and/or "Remove"
Expand Down
4 changes: 2 additions & 2 deletions lib/gameinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ GameInfo.prototype.checkRequiredFiles = (function() {
].concat(controllerChecks),
};

return function(runtimeInfo) {
return function(runtimeInfo, fileNames) {
var info = runtimeInfo.info;
var found = [];
var foundCount = 0;
Expand All @@ -298,7 +298,7 @@ GameInfo.prototype.checkRequiredFiles = (function() {
checks = checks.concat(gameChecks);
}

var fileNames = readdirtree.sync(runtimeInfo.htmlPath);
var fileNames = fileNames || readdirtree.sync(runtimeInfo.htmlPath);
for (var ii = 0; ii < fileNames.length && foundCount < checks.length; ++ii) {
var fileName = fileNames[ii].replace(/\\/g, '/');
checks.forEach(function(check, ndx) { // eslint-disable-line
Expand Down
2 changes: 1 addition & 1 deletion lib/games.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ var InstalledGamesList = function() {
if (!info) {
throw "";
}
gameInfo.checkRequiredFiles(info, info.rootPath);
gameInfo.checkRequiredFiles(info);

getInstalledGames();
var index = indexByPath(gamePath);
Expand Down
105 changes: 74 additions & 31 deletions management/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ var install = function(releasePath, opt_destPath, opt_options) {
});
var runtimeInfo;
var zipRootPath;
var zipPackagePath;

var packageLocations = gameInfo.getPackageLocations();
var checkPackageLocations = function(entry) {
Expand Down Expand Up @@ -106,18 +107,50 @@ var install = function(releasePath, opt_destPath, opt_options) {
if (!packageEntry) {
return reject("no package.json found in " + releasePath);
}
zipRootPath = packageEntry.name.replace(/\\/g, "/");
zipRootPath = zipRootPath.substring(0, zipRootPath.indexOf("/"));
zipPackagePath = packageEntry.name.replace(/\\/g, "/");
zipRootPath = zipPackagePath.substring(0, zipPackagePath.indexOf("/"));
runtimeInfo = gameInfo.parseGameInfo(packageEntry.asText(), packageEntry.name, zipRootPath);
} catch (e) {
return reject("could not parse package.json. Maybe this is not a HappyFunTimes game?");
}

// Check it's got the required files
try {
var zipPackageDir = path.dirname(zipPackagePath);
var fileNames = entries.map(function(entry) {
return path.relative(zipPackageDir, entry.name.replace(/[\\/]/g, path.sep)).replace(/\\/g, "/");
});
gameInfo.checkRequiredFiles(runtimeInfo, fileNames);
} catch (e) {
return reject(e);
}

var info = runtimeInfo.info;
var hftInfo = info.happyFunTimes;
var gameId = runtimeInfo.originalGameId;
var safeGameId = releaseUtils.safeishName(gameId);
var destBasePath;

var fileExists = {};
entries.forEach(function(entry) {
var filename = entry.name.replace(/\\/g, "/").replace(/.*?\//, "");
fileExists[filename] = true;
});

// Check for more files that should exist.
// Games that are "added" don't need this but games
// that are "installed" do.
log("checking gametype: " + hftInfo.gameType);
if (hftInfo.gameType.toLowerCase() === "unity3d") {
var exePath = platformInfo.exePath;
if (exePath) {
exePath = strings.replaceParams(exePath, { gameId: safeGameId });
if (!fileExists[exePath]) {
return reject("path not found: " + exePath + "\nIs this the correct zip file for " + platformInfo.id + "?");
}
}
}

// is it already installed?
var installedGame = gameDB.getGameById(gameId);
if (installedGame) {
Expand All @@ -130,55 +163,66 @@ var install = function(releasePath, opt_destPath, opt_options) {
}
}

var safeGameId = releaseUtils.safeishName(gameId);

if (!destBasePath) {
// make the dir after we're sure we're ready to install
destBasePath = path.join(config.getConfig().gamesDir, safeGameId);
}

destBasePath = opt_destPath ? opt_destPath : destBasePath;

// Check for bad paths
var bad = false;
var errors = [];
entries.forEach(function(entry) {
if (bad) {
return;
}
var filePath = entry.name.substring(zipRootPath.length + 1);
var destPath = path.resolve(path.join(destBasePath, filePath));
if (destPath.substring(0, destBasePath.length) !== destBasePath) {
errors.push("ERROR: bad zip file. Path would write outside game folder");
bad = true;
}
});

if (bad) {
// Should delete all work here?
return reject(errors.join("\n"));
}

console.log("installing to: " + destBasePath);
if (!options.dryRun) {
mkdirp.sync(destBasePath);
}

var files = [];
var errors = [];
var bad = false;
entries.forEach(function(entry) {
if (bad) {
return;
}
var filePath = entry.name.substring(zipRootPath.length + 1);
files.push(filePath);
var destPath = path.resolve(path.join(destBasePath, filePath));
if (destPath.substring(0, destBasePath.length) !== destBasePath) {
errors.push("ERROR: bad zip file. Path would write outside game folder");
bad = true;
} else {
var isDir = entry.dir;
if (destPath.substr(-1) === "/" || destPath.substr(-1) === "\\") {
destPath = destPath.substr(0, destPath.length - 1);
isDir = true;
var isDir = entry.dir;
if (destPath.substr(-1) === "/" || destPath.substr(-1) === "\\") {
destPath = destPath.substr(0, destPath.length - 1);
isDir = true;
}
//??
if (isDir) {
log("mkdir : " + destPath);
if (!options.dryRun) {
mkdirp.sync(destPath);
}
//??
if (isDir) {
log("mkdir : " + destPath);
if (!options.dryRun) {
mkdirp.sync(destPath);
}
} else {
log("install: " + entry.name + " -> " + destPath);
if (!options.dryRun) {
var dirPath = path.dirname(destPath);
if (!fs.existsSync(dirPath)) {
log("mkdir : " + dirPath);
mkdirp.sync(dirPath);
}
fs.writeFileSync(destPath, entry.asNodeBuffer());
} else {
log("install: " + entry.name + " -> " + destPath);
if (!options.dryRun) {
var dirPath = path.dirname(destPath);
if (!fs.existsSync(dirPath)) {
log("mkdir : " + dirPath);
mkdirp.sync(dirPath);
}
fs.writeFileSync(destPath, entry.asNodeBuffer());
}
}
});
Expand All @@ -188,8 +232,7 @@ var install = function(releasePath, opt_destPath, opt_options) {
return reject(errors.join("\n"));
}

// Should this be in the zip?
log("checking gametype: " + hftInfo.gameType);
// Set the executable bit
if (hftInfo.gameType.toLowerCase() === "unity3d") {
var exePath = platformInfo.exePath;
if (exePath) {
Expand Down
2 changes: 1 addition & 1 deletion management/make-controller-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ var make = function(gamePath, destFolder, options) {
}

try {
gameInfo.checkRequiredFiles(runtimeInfo, gamePath);
gameInfo.checkRequiredFiles(runtimeInfo);
if (!fs.existsSync(destFolder)) {
fs.mkdirSync(destFolder);
}
Expand Down
2 changes: 1 addition & 1 deletion management/make.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ var make = function(gamePath, destFolder, options) {
}

try {
gameInfo.checkRequiredFiles(runtimeInfo, gamePath);
gameInfo.checkRequiredFiles(runtimeInfo);
if (!fs.existsSync(destFolder)) {
fs.mkdirSync(destFolder);
}
Expand Down

0 comments on commit d641c54

Please sign in to comment.