From 8a16c69e3e3b43e76d91066e49fac21d35d6a776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Dahlgren?= Date: Sun, 18 Jul 2021 13:18:01 +0200 Subject: [PATCH] Add metadata about mods * Mod info * Size * Steam Workshop info --- lib/mods.js | 43 ---------- lib/mods/folderSize.js | 26 ++++++ lib/mods/index.js | 82 +++++++++++++++++++ lib/mods/modFile.js | 30 +++++++ lib/mods/steamMeta.js | 23 ++++++ package.json | 1 + public/js/app/views/mods/list_item.js | 27 ++++++ public/js/app/views/servers/mods/list_item.js | 26 +++++- public/js/tpl/mods/list.html | 3 +- public/js/tpl/mods/list_item.html | 12 ++- public/js/tpl/servers/mods/list_item.html | 7 ++ 11 files changed, 234 insertions(+), 46 deletions(-) delete mode 100644 lib/mods.js create mode 100644 lib/mods/folderSize.js create mode 100644 lib/mods/index.js create mode 100644 lib/mods/modFile.js create mode 100644 lib/mods/steamMeta.js diff --git a/lib/mods.js b/lib/mods.js deleted file mode 100644 index 736c891..0000000 --- a/lib/mods.js +++ /dev/null @@ -1,43 +0,0 @@ -var events = require('events') -var fs = require('fs.extra') -var glob = require('glob') -var path = require('path') - -var Mods = function (config) { - this.config = config - this.mods = [] -} - -Mods.prototype = new events.EventEmitter() - -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() - } - }) -} - -Mods.prototype.updateMods = function () { - var self = this - glob('**/{@*,csla,gm,vn,ws}/addons', { cwd: self.config.path }, function (err, files) { - if (err) { - console.log(err) - } else { - var mods = files.map(function (file) { - return { - // Find actual parent mod folder from addons folder - name: path.join(file, '..') - } - }) - - self.mods = mods - self.emit('mods', mods) - } - }) -} - -module.exports = Mods diff --git a/lib/mods/folderSize.js b/lib/mods/folderSize.js new file mode 100644 index 0000000..196073b --- /dev/null +++ b/lib/mods/folderSize.js @@ -0,0 +1,26 @@ +var async = require('async') +var fs = require('fs') +var glob = require('glob') +var path = require('path') + +module.exports = function (modPath, config, callback) { + var basePath = path.resolve(config.path, modPath) + var total = 0 + glob('**/*', { cwd: basePath, dot: true }, function (err, files) { + if (err) { + return callback(err, 0) + } + + async.forEach(files, function (file, cb) { + fs.stat(path.join(basePath, file), function stat (err, stats) { + if (!err && (stats.isFile() || stats.isSymbolicLink())) { + var size = stats.size || 0 + total += size + } + cb() + }) + }, function (err) { + callback(err, total) + }) + }) +} diff --git a/lib/mods/index.js b/lib/mods/index.js new file mode 100644 index 0000000..7d8fa50 --- /dev/null +++ b/lib/mods/index.js @@ -0,0 +1,82 @@ +var async = require('async') +var events = require('events') +var filesize = require('filesize') +var fs = require('fs.extra') +var glob = require('glob') +var path = require('path') + +var folderSize = require('./folderSize') +var modFile = require('./modFile') +var steamMeta = require('./steamMeta') + +var Mods = function (config) { + this.config = config + this.mods = [] +} + +Mods.prototype = new events.EventEmitter() + +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() + } + }) +} + +Mods.prototype.updateMods = function () { + var self = this + glob('**/{@*,csla,gm,vn}/addons', { cwd: self.config.path }, function (err, files) { + if (err) { + console.log(err) + return + } + + var mods = files.map(function (file) { + // Find actual parent mod folder from addons folder + return path.join(file, '..') + }) + + async.map(mods, self.resolveModData.bind(self), function (err, mods) { + if (err) { + console.log(err) + return + } + + self.mods = mods + self.emit('mods', mods) + }) + }) +} + +Mods.prototype.resolveModData = function (modPath, cb) { + var self = this + async.parallel({ + folderSize: function (cb) { + folderSize(modPath, self.config, cb) + }, + modFile: function (cb) { + modFile(modPath, self.config, cb) + }, + steamMeta: function (cb) { + steamMeta(modPath, self.config, cb) + } + }, function (err, results) { + if (err) { + return cb(err) + } + + cb(null, { + name: modPath, + size: results.folderSize, + formattedSize: filesize(results.folderSize), + modFile: results.modFile, + steamMeta: results.steamMeta + }) + }) +} + +module.exports = Mods diff --git a/lib/mods/modFile.js b/lib/mods/modFile.js new file mode 100644 index 0000000..a4ad252 --- /dev/null +++ b/lib/mods/modFile.js @@ -0,0 +1,30 @@ +var armaClassParser = require('arma-class-parser') +var fs = require('fs') +var path = require('path') + +function stripBOM (data) { + if (data.charCodeAt(0) === 0xFEFF) { + return data.slice(1) + } + + return data +} + +module.exports = function (modPath, config, callback) { + var modCpp = path.resolve(config.path, modPath, 'mod.cpp') + fs.readFile(modCpp, 'utf8', function (err, data) { + if (err) { + return callback(null, null) + } + + try { + var meta = armaClassParser.parse(stripBOM(data)) + callback(null, { + name: meta.name + }) + } catch (err) { + console.log('Error parsing mod.cpp for ' + modPath + ', ' + err) + callback(null, null) + } + }) +} diff --git a/lib/mods/steamMeta.js b/lib/mods/steamMeta.js new file mode 100644 index 0000000..4d5a423 --- /dev/null +++ b/lib/mods/steamMeta.js @@ -0,0 +1,23 @@ +var armaClassParser = require('arma-class-parser') +var fs = require('fs') +var path = require('path') + +module.exports = function (modPath, config, callback) { + var metaCpp = path.resolve(config.path, modPath, 'meta.cpp') + fs.readFile(metaCpp, 'utf8', function (err, data) { + if (err) { + return callback(null, null) + } + + try { + var meta = armaClassParser.parse(data) + callback(null, { + id: meta.publishedid, + name: meta.name + }) + } catch (err) { + console.log('Error parsing meta.cpp for ' + modPath + ', ' + err) + callback(null, null) + } + }) +} diff --git a/package.json b/package.json index f4f294b..7b64b46 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ ] }, "dependencies": { + "arma-class-parser": "^1.1.1", "arma-server": "0.0.10", "async": "^0.9.0", "backbone": "1.3.3", diff --git a/public/js/app/views/mods/list_item.js b/public/js/app/views/mods/list_item.js index 724e3a5..300dc44 100644 --- a/public/js/app/views/mods/list_item.js +++ b/public/js/app/views/mods/list_item.js @@ -14,6 +14,33 @@ module.exports = Marionette.ItemView.extend({ 'click .destroy': 'deleteMod' }, + templateHelpers: function () { + var modFile = this.model.get('modFile') + var steamMeta = this.model.get('steamMeta') + + var link = null + var title = null + + if (steamMeta && steamMeta.id) { + if (steamMeta.id) { + link = 'https://steamcommunity.com/sharedfiles/filedetails/?id=' + steamMeta.id + } + + if (steamMeta.name) { + title = steamMeta.name + } + } + + if (modFile && modFile.name) { + title = modFile.name + } + + return { + link: link, + title: title + } + }, + deleteMod: function (event) { var self = this sweetAlert({ diff --git a/public/js/app/views/servers/mods/list_item.js b/public/js/app/views/servers/mods/list_item.js index 3168672..944b4ba 100644 --- a/public/js/app/views/servers/mods/list_item.js +++ b/public/js/app/views/servers/mods/list_item.js @@ -10,8 +10,32 @@ module.exports = ModListItemView.extend({ template: template, templateHelpers: function () { + var modFile = this.model.get('modFile') + var name = this.model.get('name') + var steamMeta = this.model.get('steamMeta') + + var enabled = this.options.server.get('mods').indexOf(name) > -1 + var link = null + var title = null + + if (steamMeta && steamMeta.id) { + if (steamMeta.id) { + link = 'https://steamcommunity.com/sharedfiles/filedetails/?id=' + steamMeta.id + } + + if (steamMeta.name) { + title = steamMeta.name + } + } + + if (modFile && modFile.name) { + title = modFile.name + } + return { - enabled: this.options.server.get('mods').indexOf(this.model.get('name')) > -1 + enabled: enabled, + link: link, + title: title } } }) diff --git a/public/js/tpl/mods/list.html b/public/js/tpl/mods/list.html index bc65135..5fb6bfb 100644 --- a/public/js/tpl/mods/list.html +++ b/public/js/tpl/mods/list.html @@ -6,7 +6,8 @@ - + + diff --git a/public/js/tpl/mods/list_item.html b/public/js/tpl/mods/list_item.html index 4b5ab12..c120997 100644 --- a/public/js/tpl/mods/list_item.html +++ b/public/js/tpl/mods/list_item.html @@ -1,5 +1,15 @@ +
ModModSize
- <%-name%> + <%-name%> + <% if (link) { %> + + <%-title%> + + <% } else if (title) { %> + <%-title%> + <% } %> + + <%-formattedSize%> diff --git a/public/js/tpl/servers/mods/list_item.html b/public/js/tpl/servers/mods/list_item.html index 9c28210..9f17815 100644 --- a/public/js/tpl/servers/mods/list_item.html +++ b/public/js/tpl/servers/mods/list_item.html @@ -3,6 +3,13 @@ + <%-title%> + + <% } else if (title) { %> + <%-title%> + <% } %>