yukinovel
Version:
Yukinovel is a simple web visual novel engine.
99 lines (98 loc) • 2.59 kB
JavaScript
/**
* Helper function để tạo plugin dễ dàng hơn
*/
export function createPlugin(options) {
return {
metadata: {
priority: 0,
...options.metadata
},
hooks: options.hooks,
initialize: options.initialize,
dispose: options.dispose,
api: options.api
};
}
/**
* Decorator để đánh dấu plugin method hooks
*/
export function hook(eventType) {
return function (target, propertyKey, descriptor) {
if (!target._hooks) {
target._hooks = {};
}
target._hooks[eventType] = descriptor.value;
};
}
/**
* Class-based Plugin helper
*/
export class PluginBase {
constructor() {
this.collectHooks();
}
collectHooks() {
const proto = Object.getPrototypeOf(this);
if (proto._hooks) {
this.hooks = { ...proto._hooks };
if (this.hooks) {
for (const [eventType, method] of Object.entries(this.hooks)) {
if (typeof method === 'function') {
this.hooks[eventType] = method.bind(this);
}
}
}
}
}
}
/**
* Plugin Registry Builder để quản lý nhiều plugins :v
*/
export class PluginRegistryBuilder {
constructor() {
this.plugins = [];
}
add(plugin) {
this.plugins.push(plugin);
return this;
}
addMultiple(plugins) {
this.plugins.push(...plugins);
return this;
}
build() {
return [...this.plugins];
}
async registerAll(game) {
for (const plugin of this.plugins) {
await game.registerPlugin(plugin);
}
}
}
/**
* Validation helper cho plugin
*/
export function validatePlugin(plugin) {
const errors = [];
if (!plugin.metadata) {
errors.push('Plugin must have metadata');
}
else {
if (!plugin.metadata.name) {
errors.push('Plugin must have a name');
}
if (!plugin.metadata.version) {
errors.push('Plugin must have a version');
}
if (plugin.metadata.name && !/^[a-zA-Z0-9_-]+$/.test(plugin.metadata.name)) {
errors.push('Plugin name must contain only alphanumeric characters, hyphens, and underscores');
}
if (plugin.metadata.version && !/^\d+\.\d+\.\d+/.test(plugin.metadata.version)) {
errors.push('Plugin version must follow semantic versioning (x.y.z)');
}
}
return {
valid: errors.length === 0,
errors
};
}