Last active
November 12, 2016 20:59
-
-
Save AlinaNova21/1690177425e43bae4a85857d28084a0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(config) { | |
if(config.cli){ | |
/** Current Way: | |
let onPlayerSandbox = config.engine.onPlayerSandbox; | |
config.engine.onPlayerSandbox = function(sandbox, userId) { | |
sandbox.test = function(){ return 'Hello World!' } | |
return onPlayerSandbox(sandbox,userId) | |
} | |
**/ | |
// Proposed way | |
config.cli.on('createSandbox',(event,sandbox)=>{ | |
sandbox.test = function(){ return 'Hello World!' } | |
}) | |
} | |
if(config.engine){ | |
// Normal way: (Not multimod firendly) | |
/** | |
config.engine.onProcessObject = function(object) { | |
if(object.type == 'keeperLair') { | |
return false; // This object will not be processed | |
} | |
}**/ | |
// Event based way: (More multimod friendly) | |
config.engine.on('processObject',(event, object, roomObjects, roomTerrain, gameTime, roomInfo, objectsUpdate, usersUpdate)=>{ | |
if(object.type == 'keeperLair'){ | |
event.preventDefault() // Prevents default handler from running | |
event.returnValue = false | |
} | |
}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { EventEmitter } = require('events') | |
module.exports = function(config) { | |
if(config.__eventmod_hooked) return | |
config.__eventmod_hooked = true | |
for (let modName in config) { | |
let mod = config[modName] | |
let ee = new EventEmitter() | |
config[modName] = new Proxy(mod,{ | |
get: (target,name)=>ee[name] || target[name] | |
}) | |
let hooked = {} | |
ee.on('newListener',(eventName,listener)=>{ | |
// This allows us to hook the event on first listener | |
if(hooked[eventName]) return | |
hooked[eventName] = true | |
console.log('Hooking event',eventName) | |
let name = `on${eventName.slice(0,1).toUpperCase}${eventName.slice(1)}` | |
let orig = mod[name] | |
mod[name] = function(...args) { | |
let def = true | |
let event = { returnValue: null, preventDefault: ()=>def=false } | |
ee.emit(event,eventName, ...args) | |
if(def) orig(...args) | |
return event.returnValue | |
} | |
}) | |
} | |
} | |
/* | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment