- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
import { checkVersion } from "./../utils/utils.js";
import { game, version } from "./../index.js";
import { warning } from "../lang/console.js";
/**
* Contains all registered plugins.
* @name cache
* @memberof plugin
*/
export let cache = {};
/**
* @namespace plugin
*/
/**
* a base Object class for plugin
* (plugin must be installed using the register function)
* @class
* @name BasePlugin
* @memberof plugin
*/
export class BasePlugin {
/**
* @param {Application} [app] - a reference to the app/game that registered this plugin
*/
constructor(app = game) {
/**
* define the minimum required version of melonJS<br>
* this can be overridden by the plugin
* @type {string}
* @default "__VERSION__"
*/
this.version = "__VERSION__";
/**
* a reference to the app/game that registered this plugin
* @type {Application}
*/
this.app = app;
}
}
/**
* @class
* @name Base
* @memberof plugin
* @deprecated since 15.1.6, see {@link plugin.BasePlugin}
*/
export class Base extends BasePlugin {
constructor() {
warning("plugin.Base", "plugin.BasePlugin", "15.1.6");
super();
}
}
/**
* patch a melonJS function
* @name patch
* @memberof plugin
* @param {object} proto - target object
* @param {string} name - target function
* @param {Function} fn - replacement function
* @example
* // redefine the me.game.update function with a new one
* me.plugin.patch(me.game, "update", function () {
* // display something in the console
* console.log("duh");
* // call the original me.game.update function
* this._patched();
* });
*/
export function patch(proto, name, fn) {
// use the object prototype if possible
if (typeof proto.prototype !== "undefined") {
proto = proto.prototype;
}
// reuse the logic behind object extends
if (typeof(proto[name]) === "function") {
// save the original function
let _parent = proto[name];
// override the function with the new one
Object.defineProperty(proto, name, {
"configurable" : true,
"value" : (function (name, fn) {
return function () {
this._patched = _parent;
let ret = fn.apply(this, arguments);
this._patched = null;
return ret;
};
})(name, fn)
});
}
else {
throw new Error(name + " is not an existing function");
}
}
/**
* Register a plugin.
* @name register
* @memberof plugin
* @param {plugin.BasePlugin} plugin - Plugin object to instantiate and register
* @param {string} [name=plugin.constructor.name] - a unique name for this plugin
* @param {object} [...arguments] - all extra parameters will be passed to the plugin constructor
* @example
* // register a new plugin
* me.plugin.register(TestPlugin, "testPlugin");
* // the `testPlugin` class instance can also be accessed through me.plugin.cache
* me.plugin.cache.testPlugin.myfunction ();
*/
export function register(plugin, name = plugin.toString().match(/ (\w+)/)[1]) {
// ensure me.plugins[name] is not already "used"
if (cache[name]) {
throw new Error("plugin " + name + " already registered");
}
// get extra arguments
let _args = [];
if (arguments.length > 2) {
// store extra arguments if any
_args = Array.prototype.slice.call(arguments, 1);
}
// try to instantiate the plugin
_args[0] = plugin;
let instance = new (plugin.bind.apply(plugin, _args))();
// inheritance check
if (typeof instance === "undefined" || !(instance instanceof BasePlugin)) {
throw new Error("Plugin should extend the BasePlugin Class !");
}
// compatibility testing
if (checkVersion(instance.version, version) > 0) {
throw new Error("Plugin version mismatch, expected: " + instance.version + ", got: " + version);
}
// create a reference to the new plugin
cache[name] = instance;
}
/**
* returns the the plugin instance with the specified class type or registered name
* @name get
* @memberof plugin
* @param {object|string} classType - the Class Object or registered name of the plugin to retreive
* @returns {BasePlugin} a plugin instance or undefined
*/
export function get(classType) {
for (const name in cache) {
if ((typeof classType === "string" && classType === name) || cache[name] instanceof classType) {
return cache[name];
}
}
}