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-07-22 18:16:30 +00:00
|
|
|
var fs = require('fs.extra');
|
2015-04-07 06:12:42 +00:00
|
|
|
var _ = require('lodash');
|
2015-01-21 03:00:09 +00:00
|
|
|
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;
|
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;
|
|
|
|
},[]);
|
2015-01-21 03:00:09 +00:00
|
|
|
};
|
|
|
|
|
2015-02-01 04:27:12 +00:00
|
|
|
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);
|
2015-07-22 19:20:05 +00:00
|
|
|
|
|
|
|
if (!err) {
|
|
|
|
self.updateMods();
|
|
|
|
}
|
2015-07-22 18:16:30 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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;
|
2015-04-07 06:12:42 +00:00
|
|
|
var currentDownloadMod = null;
|
|
|
|
var currentDownloadProgress = 0;
|
|
|
|
|
2015-02-01 04:27:12 +00:00
|
|
|
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
|
|
|
}
|
2015-02-01 04:27:12 +00:00
|
|
|
self.updateMods();
|
2015-12-29 15:19:55 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2015-02-01 04:27:12 +00:00
|
|
|
});
|
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,
|
2015-04-17 18:06:48 +00:00
|
|
|
progress: null,
|
2015-01-21 03:00:09 +00:00
|
|
|
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.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)));
|
2015-02-01 01:43:52 +00:00
|
|
|
} 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;
|