Skip to content

Commit

Permalink
Merge pull request #248 from ninjadev/add-metadata
Browse files Browse the repository at this point in the history
Add metadata to compiled nin executables
  • Loading branch information
sigvef authored Jul 18, 2016
2 parents 27275c5 + 53638ef commit fd23b9d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
3 changes: 2 additions & 1 deletion nin/backend/blank-project/nin.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "My project",
"title": "My project",
"authors": ["Your demoscene handle"],
"music": {
"bpm": 125,
"subdivision": 4
Expand Down
21 changes: 20 additions & 1 deletion nin/backend/compile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var chalk = require('chalk');
var compress = require('./compress').compress;
var exec = require('child_process').exec;
var execSync = require('child_process').execSync;
var fs = require('fs');
var mkdirp = require('mkdirp');
var p = require('path');
Expand Down Expand Up @@ -63,9 +64,21 @@ var compile = function(projectPath, options) {
fs.writeFileSync(projectPath + '/bin/' + filename, data);
});
}

var settings = projectSettings.load(projectPath);
var metadata = [
['Title', settings.title],
['Author', settings.authors.join(', ')],
['Creation time', '' + new Date()],
['Software', 'NIN'],
['Description',
"Git revision: " + execSync('git rev-parse HEAD') +
'Git origin: ' + execSync('git config --get remote.origin.url')]
];

if(options.pngCompress) {
process.stdout.write(chalk.yellow('\nCompressing demo to .png.html'));
compress(projectPath, data, function(data) {
compress(projectPath, data, metadata, function(data) {
renderOK();
writeDemoToFile(data, 'demo.png.html');
console.log(chalk.white('\n★ ---------------------------------------- ★'));
Expand All @@ -83,7 +96,13 @@ var compile = function(projectPath, options) {
} catch(e) {
customHtml = fs.readFileSync(__dirname + '/index.html', {encoding: 'utf8'});
}

var parsedMetadata = metadata.map(function(data) {
return '<!-- ' + data[0] + ': ' + data[1] + ' -->';
}).join('\n') + '\n';

var html =
parsedMetadata +
customHtml +
'<script>' +
'GU=1;' + /* hack to make sure GU exisits from the get-go */
Expand Down
14 changes: 11 additions & 3 deletions nin/backend/compress.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var crc = require('crc').crc32;
var fs = require('fs');
var zlib = require('zlib');
var execSync = require('child_process').execSync;


function positiveNumberToBytes(number, bytes) {
Expand All @@ -23,8 +24,7 @@ function chunk(type, data) {
]);
}


function compress(projectPath, payload, callback) {
function compress(projectPath, payload, metadata, callback) {

payload = new Buffer(payload, 'binary');

Expand Down Expand Up @@ -57,6 +57,7 @@ function compress(projectPath, payload, callback) {
} catch(e) {
customHtml = fs.readFileSync(__dirname + '/index.html', {encoding: 'utf8'});
}

var html =
customHtml +
'<script>' +
Expand Down Expand Up @@ -92,7 +93,13 @@ function compress(projectPath, payload, callback) {
'demo=bootstrap({layers:layers, camerapaths:camerapaths, onprogress: ONPROGRESS, oncomplete: ONCOMPLETE});' +
'}' +
'}' +
'}</script><canvas class=hide height='+ height + ' width=' + width + '></canvas><img src=# onload=z()><!--';
'}</script>' +
'<canvas class=hide height='+ height + ' width=' + width + '></canvas><img src=# onload=z()><!--';


var metadataChunks = Buffer.concat(metadata.map(function(data) {
return chunk('tEXt', new Buffer(data[0] + '\0' + data[1]));
}));
var htMlChunk = chunk('htMl', new Buffer(html));
var IENDChunk = chunk('IEND', new Buffer(''));

Expand All @@ -117,6 +124,7 @@ function compress(projectPath, payload, callback) {
callback(Buffer.concat([
fileSignature,
IHDRChunk,
metadataChunks,
htMlChunk,
IDATChunk,
IENDChunk
Expand Down
4 changes: 3 additions & 1 deletion nin/backend/projectSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ var fs = require('fs');


var defaultSettings = {
title: "Untitled",
authors: ["Anonymous"],
music: {
path: "res/music.mp3",
bpm: 110,
Expand Down Expand Up @@ -31,7 +33,7 @@ function traverse(input, defaults) {
var output = {};
for (var key in defaults) {
if (key in input) {
if (typeof input[key] === 'object') {
if (input[key].constructor === Object) {
output[key] = traverse(input[key], defaults[key]);
} else {
output[key] = input[key];
Expand Down

0 comments on commit fd23b9d

Please sign in to comment.