Skip to content

Instantly share code, notes, and snippets.

@ossra

ossra/Ossra_Framework-Base.js Secret

Created Aug 9, 2020
Embed
What would you like to do?
RPG Maker MV/MZ Plugin Framework
// +====================================================================================+
// ||| Base | Framework
// +====================================================================================+
/*:
* @target MZ
* @plugindesc [1.00] Plugin framework.
* @author Ossra
*
* @help
* ==| Plugin Information |=================================================
*
* - Author : Ossra
* - Contact : garden.of.ossra [at] gmail
* - Version : 1.00 | RPG Maker MZ 0.9.5
* - Release : 9th August 2020
* - Updated : 9th August 2020
* - License : MIT [https://opensource.org/licenses/MIT]
*
* @param pluginProperties
* @text Plugin Properties
* @type text
* @default ------------------------------------
*
* @param gid
* @parent pluginProperties
* @text Global Identifier
* @desc Global identification tag for internal use only. Do not change.
* @default ossra-OojTChE3vep9fHc3F
*/
// +====================================================================================+
// +===================================================| Namespace |
// | [Global] Namespace
// +====================================================================================+
var Imported = Imported || {};
Imported.Ossra = Imported.Ossra || {};
var Ossra = Ossra || {};
Ossra.Util = Ossra.Util || {};
Ossra.Share = Ossra.Share || {};
Ossra.Plugin = Ossra.Plugin || {};
(function (_) { // {
'use strict';
// +=================================================| Util |
// | [Global] Util
// +==================================================================================+
// +----------------------------------------------------------------------------------+
// | [Method] getPlugin
// +----------------------------------------------------------------------------------+
_.getPlugin = function (namestring) {
const [category, name] = namestring.split('.');
try {
const plugin = Ossra.Plugin[category][name];
const version = Imported.Ossra[category][name];
if ([plugin, version].every((v) => typeof v === 'undefined')) {
throw new Error();
}
return { name, category, version, plugin };
} catch (error) {
return null;
};
}; // Util << getPlugin
})(Ossra.Util); // }
(function(_) { // {
'use strict';
class Plugin {
constructor (namestring, version) {
this.namestring = namestring;
this.version = version;
this.file = this.getFile();
this.name = this.getName();
this.data = new Object();
this.config = new Config();
this.command = new Command();
this.namespace = new Namespace(namestring, version);
this.namespace.add('Data', this.data);
this.namespace.add('Config', this.config);
this.namespace.add('Command', this.command);
}
getName () {
const name = this.namestring.split('.')[1];
return name.match(/[A-Z]*[^A-Z]+/g).join(' ');
}
getFile () {
const script = document.currentScript.src;
return script.match(/^.*\/(.*).js$/)[1];
}
}
class Namespace {
constructor (namestring, version) {
this.imported = this.set(Imported.Ossra, namestring, version);
this.base = this.set(Ossra.Plugin, namestring);
}
set (namespace, namestring, value) {
return namestring.split('.').reduce((parent, child, index, array) => {
if (typeof parent[child] === 'undefined') {
const length = index + 1 === array.length;
const result = typeof value !== 'undefined';
parent[child] = length && result ? value : { };
}
return parent[child];
}, namespace);
}
add (namestring, value) {
return this.set(this.base, namestring, value);
}
}
class Container {
constructor () {
this.data = new Array();
}
find (key) {
return this.data.find((element) => element.key === key);
}
get (key) {
const data = this.find(key);
return typeof data === 'undefined' ? data : data.value;
}
add (key, value) {
const data = this.find(key);
if (typeof data !== 'undefined') {
data.value = value;
} else {
this.data.push({ key, value });
}
}
}
class Parser {
constructor () {
this.helpers = new Container();
this.defaults = new Container();
}
parse (json, defaults) {
const self = this;
try {
return JSON.parse(json, function(key, value) {
let [_key, _type, _fnc] = key.split('__');
let _def = defaults[_key];
let _val = value === '' ? _def : value;
if (_key && _type) {
switch (_type) {
case 'function':
_val = self.helpers.get(_fnc).call(this, _val);
break;
case 'array':
_val = value === '' ? [] : JSON.parse(_val);
for (let i = 0; i < _val.length; i++) {
_val[i] = self.parse(_val[i], _def[0]);
}
break;
case 'string':
break;
default:
_val = JSON.parse(_val);
break;
};
this[_key] = _val;
} else {
return _key !== '' ? undefined : _val;
}
});
} catch (error) {
console.log(error);
return defaults;
}
}
}
class Config extends Parser {
constructor () {
super();
this.data = new Object();
}
parameters (gid) {
return JSON.stringify($plugins.find(function(plugin) {
return plugin.parameters['gid'] === gid;
})['parameters']);
}
setup (gid) {
const parameters = this.parameters(gid);
const defaults = this.defaults.get('plugin');
this.data = this.parse(parameters, defaults);
}
}
class Command extends Parser {
constructor () {
super();
}
add (name, command, func) {
const self = this;
PluginManager.registerCommand(name, command, function (args) {
const defaults = self.defaults.get(command);
const json = JSON.stringify(args);
const parsed = self.parse(json, defaults);
func.call(this, parsed);
});
}
}
_.Plugin = Plugin;
})(Ossra.Share); // }
// |///////////////////////////////////| End of File |//////////////////////////////////|
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.