2015-01-21 03:00:09 +00:00
|
|
|
var async = require('async');
|
2015-02-01 04:27:12 +00:00
|
|
|
var events = require('events');
|
2015-02-04 23:42:18 +00:00
|
|
|
var filesize = require('filesize');
|
2015-01-21 03:00:09 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
var playwithsix = require('playwithsix');
|
|
|
|
|
|
|
|
var traverse = require('./mods/traverse');
|
|
|
|
|
|
|
|
var Mods = function (config) {
|
|
|
|
this.config = config;
|
2015-02-01 01:43:52 +00:00
|
|
|
this.liteMods = true;
|
2015-02-01 04:27:12 +00:00
|
|
|
this.mods = [];
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
this.updateMods();
|
2015-01-21 03:00:09 +00:00
|
|
|
};
|
|
|
|
|
2015-02-01 04:27:12 +00:00
|
|
|
Mods.prototype = new events.EventEmitter();
|
|
|
|
|
2015-01-21 03:00:09 +00:00
|
|
|
Mods.prototype.download = function (mod, cb) {
|
2015-02-01 04:27:12 +00:00
|
|
|
var self = this;
|
|
|
|
playwithsix.downloadMod(this.config.path, mod, {lite: this.liteMods}, function(err, mods) {
|
|
|
|
self.updateMods();
|
|
|
|
cb(err, mods);
|
|
|
|
});
|
2015-01-21 03:00:09 +00:00
|
|
|
};
|
|
|
|
|
2015-02-01 04:27:12 +00:00
|
|
|
Mods.prototype.updateMods = function () {
|
2015-01-21 03:00:09 +00:00
|
|
|
var self = this;
|
|
|
|
fs.readdir(self.config.path, function (err, files) {
|
|
|
|
if (err) {
|
2015-02-13 16:14:12 +00:00
|
|
|
console.log(err);
|
2015-01-21 03:00:09 +00:00
|
|
|
} 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);
|
2015-02-01 01:43:52 +00:00
|
|
|
self.isPlayWithSixMod(modPath, function (isPlayWithSixMod) {
|
2015-01-21 03:00:09 +00:00
|
|
|
cb(null, {
|
|
|
|
name: mod,
|
|
|
|
outdated: outdatedMods && outdatedMods.indexOf(mod) >= 0,
|
|
|
|
playWithSix: isPlayWithSixMod,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, function (err, mods) {
|
2015-02-01 04:27:12 +00:00
|
|
|
if (!err) {
|
|
|
|
self.mods = mods;
|
|
|
|
self.emit('mods', mods);
|
|
|
|
}
|
2015-01-21 03:00:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-02-01 01:43:52 +00:00
|
|
|
Mods.prototype.isPlayWithSixMod = function (modPath, cb) {
|
|
|
|
var pwsFile = path.join(modPath, '.synq.json');
|
|
|
|
fs.exists(pwsFile, function (exists) {
|
|
|
|
if (cb) {
|
|
|
|
cb(exists);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Mods.prototype.removeDuplicates = function (mods) {
|
|
|
|
return mods.reduce(function(a,b){
|
|
|
|
if (a.indexOf(b) < 0 ) a.push(b);
|
|
|
|
return a;
|
|
|
|
},[]);
|
|
|
|
};
|
|
|
|
|
|
|
|
Mods.prototype.resolveMods = function (modsToResolve, cb) {
|
|
|
|
var self = this;
|
|
|
|
playwithsix.resolveDependencies(modsToResolve, {lite: this.liteMods}, function (err, mods) {
|
|
|
|
if (!err && mods) {
|
|
|
|
cb(null, self.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
|
|
|
};
|
|
|
|
|
2015-01-21 03:00:09 +00:00
|
|
|
Mods.prototype.traverse = function (mod, cb) {
|
|
|
|
traverse(path.join(this.config.path, mod), cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Mods;
|