2017-08-24 09:15:06 +00:00
|
|
|
var async = require('async')
|
|
|
|
var events = require('events')
|
|
|
|
var filesize = require('filesize')
|
|
|
|
var fs = require('fs.extra')
|
|
|
|
var _ = require('lodash')
|
|
|
|
var path = require('path')
|
|
|
|
var playwithsix = require('playwithsix')
|
2015-01-21 03:00:09 +00:00
|
|
|
|
|
|
|
var Mods = function (config) {
|
2017-08-24 09:15:06 +00:00
|
|
|
this.config = config
|
|
|
|
this.liteMods = true
|
|
|
|
this.mods = []
|
|
|
|
}
|
2015-02-01 04:27:12 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
Mods.prototype = new events.EventEmitter()
|
2015-02-01 04:27:12 +00:00
|
|
|
|
2015-07-22 18:16:30 +00:00
|
|
|
Mods.prototype.delete = function (mod, cb) {
|
2017-08-24 09:15:06 +00:00
|
|
|
var self = this
|
2015-07-22 18:16:30 +00:00
|
|
|
fs.rmrf(path.join(this.config.path, mod), function (err) {
|
2017-08-24 09:15:06 +00:00
|
|
|
cb(err)
|
2015-07-22 19:20:05 +00:00
|
|
|
|
|
|
|
if (!err) {
|
2017-08-24 09:15:06 +00:00
|
|
|
self.updateMods()
|
2015-07-22 19:20:05 +00:00
|
|
|
}
|
2017-08-24 09:15:06 +00:00
|
|
|
})
|
|
|
|
}
|
2015-07-22 18:16:30 +00:00
|
|
|
|
2015-01-21 03:00:09 +00:00
|
|
|
Mods.prototype.download = function (mod, cb) {
|
2017-08-24 09:15:06 +00:00
|
|
|
var self = this
|
|
|
|
var currentDownloadMod = null
|
2015-04-07 06:12:42 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
playwithsix.downloadMod(this.config.path, mod, {lite: this.liteMods}, function (err, mods) {
|
2015-04-07 06:12:42 +00:00
|
|
|
if (currentDownloadMod) {
|
2017-08-24 09:15:06 +00:00
|
|
|
currentDownloadMod.progress = null
|
|
|
|
self.emit('mods', self.mods)
|
2015-04-07 06:12:42 +00:00
|
|
|
}
|
2017-08-24 09:15:06 +00:00
|
|
|
self.updateMods()
|
2015-12-29 15:19:55 +00:00
|
|
|
|
|
|
|
if (cb) {
|
2017-08-24 09:15:06 +00:00
|
|
|
cb(err, mods)
|
2015-12-29 15:19:55 +00:00
|
|
|
}
|
2015-04-07 06:12:42 +00:00
|
|
|
}).on('progress', function (progress) {
|
2017-08-24 09:15:06 +00:00
|
|
|
var modName = progress.mod
|
2015-04-07 06:12:42 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
if (!currentDownloadMod || currentDownloadMod.name !== modName) {
|
2015-04-07 06:12:42 +00:00
|
|
|
if (currentDownloadMod) {
|
2017-08-24 09:15:06 +00:00
|
|
|
currentDownloadMod.progress = null
|
2015-04-07 06:12:42 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
var mod = _.find(self.mods, {name: modName})
|
2015-04-07 06:12:42 +00:00
|
|
|
|
|
|
|
if (mod) {
|
2017-08-24 09:15:06 +00:00
|
|
|
currentDownloadMod = mod
|
2015-04-07 06:12:42 +00:00
|
|
|
} else {
|
|
|
|
currentDownloadMod = {
|
|
|
|
name: modName,
|
|
|
|
outdated: false,
|
2017-08-24 09:15:06 +00:00
|
|
|
playWithSix: true
|
|
|
|
}
|
|
|
|
self.mods.push(currentDownloadMod)
|
2015-04-07 06:12:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Progress in whole percent
|
2017-08-24 09:15:06 +00:00
|
|
|
var newProgress = parseInt(progress.completed / progress.size * 100, 10)
|
2015-04-07 06:12:42 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
if (newProgress !== currentDownloadMod.progress) {
|
|
|
|
currentDownloadMod.progress = newProgress
|
|
|
|
self.emit('mods', self.mods)
|
2015-04-07 06:12:42 +00:00
|
|
|
}
|
2017-08-24 09:15:06 +00:00
|
|
|
})
|
|
|
|
}
|
2015-01-21 03:00:09 +00:00
|
|
|
|
2015-02-01 04:27:12 +00:00
|
|
|
Mods.prototype.updateMods = function () {
|
2017-08-24 09:15:06 +00:00
|
|
|
var self = this
|
2015-01-21 03:00:09 +00:00
|
|
|
fs.readdir(self.config.path, function (err, files) {
|
|
|
|
if (err) {
|
2017-08-24 09:15:06 +00:00
|
|
|
console.log(err)
|
2015-01-21 03:00:09 +00:00
|
|
|
} else {
|
|
|
|
var mods = files.filter(function (file) {
|
2017-08-24 09:15:06 +00:00
|
|
|
return file.charAt(0) === '@'
|
|
|
|
})
|
2015-01-21 03:00:09 +00:00
|
|
|
|
|
|
|
playwithsix.checkOutdated(self.config.path, function (err, outdatedMods) {
|
2017-08-24 09:15:06 +00:00
|
|
|
if (err) {
|
|
|
|
console.log('Error checking for outdated mods: ' + err)
|
|
|
|
}
|
|
|
|
|
2015-01-21 03:00:09 +00:00
|
|
|
async.map(mods, function (mod, cb) {
|
2017-08-24 09:15:06 +00:00
|
|
|
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,
|
2017-08-24 09:15:06 +00:00
|
|
|
playWithSix: isPlayWithSixMod
|
|
|
|
})
|
|
|
|
})
|
2015-01-21 03:00:09 +00:00
|
|
|
}, function (err, mods) {
|
2015-02-01 04:27:12 +00:00
|
|
|
if (!err) {
|
2017-08-24 09:15:06 +00:00
|
|
|
self.mods = mods
|
|
|
|
self.emit('mods', mods)
|
2015-02-01 04:27:12 +00:00
|
|
|
}
|
2017-08-24 09:15:06 +00:00
|
|
|
})
|
|
|
|
})
|
2015-01-21 03:00:09 +00:00
|
|
|
}
|
2017-08-24 09:15:06 +00:00
|
|
|
})
|
|
|
|
}
|
2015-01-21 03:00:09 +00:00
|
|
|
|
2015-02-01 01:43:52 +00:00
|
|
|
Mods.prototype.isPlayWithSixMod = function (modPath, cb) {
|
2017-08-24 09:15:06 +00:00
|
|
|
var pwsFile = path.join(modPath, '.synq.json')
|
2015-02-01 01:43:52 +00:00
|
|
|
fs.exists(pwsFile, function (exists) {
|
|
|
|
if (cb) {
|
2017-08-24 09:15:06 +00:00
|
|
|
cb(exists)
|
2015-02-01 01:43:52 +00:00
|
|
|
}
|
2017-08-24 09:15:06 +00:00
|
|
|
})
|
|
|
|
}
|
2015-02-01 01:43:52 +00:00
|
|
|
|
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) {
|
2017-08-24 09:15:06 +00:00
|
|
|
cb(err)
|
2015-02-04 23:42:18 +00:00
|
|
|
} else {
|
|
|
|
mods.map(function (mod) {
|
2017-08-24 09:15:06 +00:00
|
|
|
mod.formattedSize = filesize(mod.size)
|
|
|
|
})
|
|
|
|
cb(null, mods)
|
2015-02-04 23:42:18 +00:00
|
|
|
}
|
2017-08-24 09:15:06 +00:00
|
|
|
})
|
|
|
|
}
|
2015-02-01 04:01:50 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
module.exports = Mods
|