Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/steam workshop mods status #235

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var express = require('express')
var fs = require('fs')
var bodyParser = require('body-parser')
var morgan = require('morgan')
var path = require('path')
Expand All @@ -11,7 +12,7 @@ var webpackConfig = require('./webpack.config')
var setupBasicAuth = require('./lib/setup-basic-auth')
var Manager = require('./lib/manager')
var Missions = require('./lib/missions')
var Mods = require('./lib/mods')
var SteamMods = require('./lib/steam_mods')
var Logs = require('./lib/logs')
var Settings = require('./lib/settings')

Expand All @@ -29,17 +30,44 @@ app.use(morgan(config.logFormat || 'dev'))

app.use(serveStatic(path.join(__dirname, 'public')))

var logs = new Logs(config)
/*
Workaround for Steam Workshop with Linux Arma server

Absolute paths are not supported.
Create symlink in Arma folder to Workshop folder.
Rewrite Workshop mods to use relative path to symlinked folder instead.
*/
if (config.type === 'linux' && config.steam && config.steam.path) {
var tempWorkshopFolder = path.join(config.path, 'workshop')
try {
var stat = fs.lstatSync(tempWorkshopFolder)
if (!stat.isSymbolicLink()) {
console.error('Please remove workshop folder from Arma directory manually and restart application')
process.exit(1)
}
fs.unlinkSync(tempWorkshopFolder)
} catch (err) {
if (err.code !== 'ENOENT') {
console.error('Something went wrong when creating workaround for workshop')
console.error(err)
process.exit(1)
}
}

fs.symlinkSync(config.steam.path, tempWorkshopFolder)
}

var manager = new Manager(config, logs)
manager.load()
var logs = new Logs(config)

var missions = new Missions(config)
var mods = new Mods(config)
var mods = new SteamMods(config)
mods.updateMods()

var settings = new Settings(config)

var manager = new Manager(config, logs, mods)
manager.load()

app.use('/api/logs', require('./routes/logs')(logs))
app.use('/api/missions', require('./routes/missions')(missions))
app.use('/api/mods', require('./routes/mods')(mods))
Expand Down
6 changes: 6 additions & 0 deletions config.js.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ module.exports = {
'@mod1',
'@mod2',
],
steam: {
apiKey: '1234567890ABCDE', // https://steamcommunity.com/dev/apikey
path: 'path-to-main-steam-folder',
username: 'steam_username',
password: 'steam_password',
},
admins: [], // add steam IDs here to enable #login without password
auth: { // If both username and password is set, HTTP Basic Auth will be used. You may use an array to specify more than one user.
username: '', // Username for HTTP Basic Auth
Expand Down
5 changes: 3 additions & 2 deletions lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ var Server = require('./server')

var filePath = 'servers.json'

var Manager = function (config, logs) {
var Manager = function (config, logs, mods) {
this.config = config
this.logs = logs
this.mods = mods
this.serversArr = []
this.serversHash = {}
}
Expand Down Expand Up @@ -41,7 +42,7 @@ Manager.prototype.removeServer = function (id) {
}

Manager.prototype._addServer = function (data) {
var server = new Server(this.config, this.logs, data)
var server = new Server(this.config, this.logs, this.mods, data)
this.serversArr.push(server)
this.serversArr.sort(function (a, b) {
return a.title.localeCompare(b.title)
Expand Down
10 changes: 9 additions & 1 deletion lib/mods.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,24 @@ Mods.prototype.delete = function (mod, cb) {
})
}

Mods.prototype.find = function (id) {
this.mods.find(function (mod) {
return mod.id === id
})
}

Mods.prototype.updateMods = function () {
var self = this
glob('**/{@*,csla,gm,vn}/addons', { cwd: self.config.path }, function (err, files) {
if (err) {
console.log(err)
} else {
var mods = files.map(function (file) {
var name = path.join(file, '..')
return {
// Find actual parent mod folder from addons folder
name: path.join(file, '..')
id: name,
name: name
}
})

Expand Down
24 changes: 21 additions & 3 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ var queryTypes = {
ofpresistance: 'operationflashpoint'
}

var Server = function (config, logs, options) {
var Server = function (config, logs, mods, options) {
this.config = config
this.logs = logs
this.modsManager = mods
this.update(options)
}

Expand Down Expand Up @@ -95,6 +96,21 @@ Server.prototype.queryStatus = function () {
)
}

Server.prototype.getMods = function () {
var self = this
return this.mods.map(function (mod) {
return self.modsManager.find(mod)
}).filter(function (mod) {
return mod
}).map(function (mod) {
if (self.config.type === 'linux' && self.config.steam && self.config.steam.path) {
return mod.path.replace(self.config.steam.path, 'workshop/')
}

return mod.path
})
}

Server.prototype.getParameters = function () {
var parameters = []

Expand Down Expand Up @@ -132,6 +148,7 @@ Server.prototype.start = function () {
return this
}

var mods = this.getMods()
var parameters = this.getParameters()
var server = new ArmaServer.Server({
additionalConfigurationOptions: this.getAdditionalConfigurationOptions(),
Expand All @@ -147,7 +164,7 @@ Server.prototype.start = function () {
hostname: this.createServerTitle(this.title),
localClient: this.number_of_headless_clients > 0 ? ['127.0.0.1'] : null,
missions: this.missions,
mods: this.mods,
mods: mods,
motd: (this.motd && this.motd.split('\n')) || null,
parameters: parameters,
password: this.password,
Expand Down Expand Up @@ -197,14 +214,15 @@ Server.prototype.startHeadlessClientsIfNeeded = function () {
}

Server.prototype.startHeadlessClients = function () {
var mods = this.getMods()
var parameters = this.getParameters()
var self = this
var headlessClientInstances = _.times(this.number_of_headless_clients, function (i) {
var headless = new ArmaServer.Headless({
filePatching: self.file_patching,
game: self.config.game,
host: '127.0.0.1',
mods: self.mods,
mods: mods,
parameters: parameters,
password: self.password,
path: self.config.path,
Expand Down
61 changes: 61 additions & 0 deletions lib/steam_mods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
var events = require('events')
var ArmaSteamWorkshop = require('arma-steam-workshop')

var SteamMods = function (config) {
this.config = config
this.armaSteamWorkshop = new ArmaSteamWorkshop(this.config.steam)
this.mods = []
}

SteamMods.prototype = new events.EventEmitter()

SteamMods.prototype.delete = function (mod, cb) {
var self = this
this.armaSteamWorkshop.deleteMod(mod, function (err) {
if (err) {
console.log(err)
} else {
self.updateMods()
}

if (cb) {
cb(err)
}
})
}

SteamMods.prototype.find = function (id) {
return this.mods.find(function (mod) {
return mod.id === id
})
}

SteamMods.prototype.download = function (workshopId, cb) {
var self = this
this.armaSteamWorkshop.downloadMod(workshopId, function (err) {
self.updateMods()

if (cb) {
cb(err)
}
})
self.updateMods()
}

SteamMods.prototype.search = function (query, cb) {
this.armaSteamWorkshop.search(query, cb)
}

SteamMods.prototype.updateMods = function () {
var self = this
this.armaSteamWorkshop.mods(function (err, mods) {
if (err) {
console.log(err)
} else {
self.mods = mods
self.emit('mods', mods)
}
})
}

module.exports = SteamMods
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"dependencies": {
"arma-server": "0.0.10",
"arma-steam-workshop": "https://github.com/Dahlgren/node-arma-steam-workshop/tarball/57f11ee85ccac0a18a74c5acb5645c9af78ec5c0",
"async": "^0.9.0",
"backbone": "1.3.3",
"backbone.bootstrap-modal": "https://github.com/powmedia/backbone.bootstrap-modal/archive/632210077c2424be2ee6ea2aafe0d3fe016ae524.tar.gz",
Expand Down
7 changes: 5 additions & 2 deletions public/js/app/models/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ var Backbone = require('backbone')

module.exports = Backbone.Model.extend({
defaults: {
name: ''
id: '',
downloading: false,
name: '',
path: ''
},
idAttribute: 'name',
idAttribute: 'id',
urlRoot: '/api/mods/'
})
82 changes: 82 additions & 0 deletions public/js/app/views/mods/download/form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
var $ = require('jquery')
var _ = require('underscore')
var Marionette = require('marionette')
var Ladda = require('ladda')
var Mods = require('app/collections/mods')
var tpl = require('tpl/mods/download/form.html')
var sweetAlert = require('sweet-alert')

module.exports = Marionette.ItemView.extend({

events: {
submit: 'beforeSubmit'
},

template: _.template(tpl),

initialize: function (options) {
this.mods = options.mods
this.collection = new Mods()
this.bind('ok', this.submit)
this.bind('shown', this.shown)

var self = this
this.listenTo(this.mods, 'change reset add remove', function () {
self.collection.trigger('reset')
})
},

beforeSubmit: function (e) {
e.preventDefault()
this.submit()
},

shown: function (modal) {
var $okBtn = modal.$el.find('.btn.ok')
$okBtn.addClass('ladda-button').attr('data-style', 'expand-left')

this.laddaBtn = Ladda.create($okBtn.get(0))

this.$el.find('form .query').focus()
},

submit: function (modal) {
var self = this
var $form = this.$el.find('form')

if (modal) {
self.modal.preventClose()
}

$form.find('.form-group').removeClass('has-error')
$form.find('.help-block').text('')

this.laddaBtn.start()
self.modal.$el.find('.btn.cancel').addClass('disabled')

$.ajax({
url: '/api/mods/',
type: 'POST',
data: {
id: $form.find('.query').val()
},
dataType: 'json',
success: function (resp) {
self.laddaBtn.stop()
self.modal.$el.find('.ladda-button').removeClass('disabled')
self.modal.close()
},
error: function (resp) {
self.laddaBtn.stop()
self.modal.$el.find('.ladda-button').removeClass('disabled')
self.modal.close()

sweetAlert({
title: 'Error',
text: 'An error occurred, please consult the logs',
type: 'error'
})
}
})
}
})
Loading