arma-server-web-admin/lib/mods.js

156 lines
3.7 KiB
JavaScript
Raw Normal View History

var async = require('async');
var events = require('events');
2015-02-04 23:42:18 +00:00
var filesize = require('filesize');
2015-07-22 18:16:30 +00:00
var fs = require('fs.extra');
2015-04-07 06:12:42 +00:00
var _ = require('lodash');
var path = require('path');
var playwithsix = require('playwithsix');
var traverse = require('./mods/traverse');
var Mods = function (config) {
this.config = config;
this.liteMods = true;
this.mods = [];
var self = this;
2015-02-14 18:42:18 +00:00
};
Mods.removeDuplicates = function (mods) {
return mods.reduce(function(a,b){
if (a.indexOf(b) < 0 ) a.push(b);
return a;
},[]);
};
Mods.prototype = new events.EventEmitter();
2015-07-22 18:16:30 +00:00
Mods.prototype.delete = function (mod, cb) {
var self = this;
fs.rmrf(path.join(this.config.path, mod), function (err) {
cb(err);
if (!err) {
self.updateMods();
}
2015-07-22 18:16:30 +00:00
});
};
Mods.prototype.download = function (mod, cb) {
var self = this;
2015-04-07 06:12:42 +00:00
var currentDownloadMod = null;
var currentDownloadProgress = 0;
playwithsix.downloadMod(this.config.path, mod, {lite: this.liteMods}, function(err, mods) {
2015-04-07 06:12:42 +00:00
if (currentDownloadMod) {
2015-04-17 18:06:48 +00:00
currentDownloadMod.progress = null;
self.emit('mods', self.mods);
2015-04-07 06:12:42 +00:00
}
self.updateMods();
if (cb) {
cb(err, mods);
}
2015-04-07 06:12:42 +00:00
}).on('progress', function (progress) {
var modName = progress.mod;
if (!currentDownloadMod || currentDownloadMod.name != modName) {
if (currentDownloadMod) {
delete currentDownloadMod.progress;
}
var mod = _.find(self.mods, {name: modName});
if (mod) {
currentDownloadMod = mod;
} else {
currentDownloadMod = {
name: modName,
outdated: false,
playWithSix: true,
};
2015-04-17 18:06:48 +00:00
self.mods.push(currentDownloadMod);
2015-04-07 06:12:42 +00:00
}
}
// Progress in whole percent
var newProgress = parseInt(progress.completed / progress.size * 100, 10);
if (newProgress != currentDownloadMod.progress) {
currentDownloadMod.progress = newProgress;
self.emit('mods', self.mods);
}
});
};
Mods.prototype.updateMods = function () {
var self = this;
fs.readdir(self.config.path, function (err, files) {
if (err) {
2015-02-13 16:14:12 +00:00
console.log(err);
} else {
var mods = files.filter(function (file) {
return file.charAt(0) == "@";
});
playwithsix.checkOutdated(self.config.path, function (err, outdatedMods) {
async.map(mods, function (mod, cb) {
var modPath = path.join(self.config.path, mod);
self.isPlayWithSixMod(modPath, function (isPlayWithSixMod) {
cb(null, {
name: mod,
outdated: outdatedMods && outdatedMods.indexOf(mod) >= 0,
2015-04-17 18:06:48 +00:00
progress: null,
playWithSix: isPlayWithSixMod,
});
});
}, function (err, mods) {
if (!err) {
self.mods = mods;
self.emit('mods', mods);
}
});
});
}
});
};
Mods.prototype.isPlayWithSixMod = function (modPath, cb) {
var pwsFile = path.join(modPath, '.synq.json');
fs.exists(pwsFile, function (exists) {
if (cb) {
cb(exists);
}
});
};
Mods.prototype.resolveMods = function (modsToResolve, cb) {
var self = this;
playwithsix.resolveDependencies(modsToResolve, {lite: this.liteMods}, function (err, mods) {
if (!err && mods) {
2015-02-14 18:42:18 +00:00
cb(null, Mods.removeDuplicates(modsToResolve.concat(mods)));
} else {
cb(err);
}
});
};
2015-02-01 04:01:50 +00:00
Mods.prototype.search = function (query, cb) {
2015-02-04 23:42:18 +00:00
playwithsix.search(query, function (err, mods) {
if (err) {
cb(err);
} else {
mods.map(function (mod) {
mod.formattedSize = filesize(mod.size);
});
cb(null, mods);
}
});
2015-02-01 04:01:50 +00:00
};
Mods.prototype.traverse = function (mod, cb) {
traverse(path.join(this.config.path, mod), cb);
};
module.exports = Mods;