Modular Support

Started by PSL, Today at 11:07 AM

Previous topic - Next topic

PSL

This is a lightweight, modular management framework designed specifically for the Squirrel language. With zero dependencies and ready to use right out of the box, it comprehensively resolves the issues inherent in traditional single-script architectures—such as disorganization, difficulty in maintenance, lack of hot-updating capabilities, and event conflicts.

Through a unified module manager, the framework facilitates module loading, unloading, dependency management, lifecycle control, automatic global event dispatch, and secure timer isolation—thereby enabling your server scripts to truly achieve an engineered, modular, and extensible architecture.

  • Each module resides in a separate file with its own isolated scope, ensuring no mutual interference and resulting in a clean, easily maintainable code structure.
  • Supports single-module loading, batch loading, and dependency-aware loading; automatically detects whether a module has already been loaded to prevent redundant execution.
  • Features built-in `onScriptLoad` and `onScriptUnload` lifecycle hooks, which execute automatically whenever a module is loaded or unloaded.
  • Covers events across all scenarios—including server, player, vehicle, item, command, and chat interactions—with all modules automatically listening for these events, eliminating the need for manual hook binding.
  • Includes a dedicated `newTimer` function that binds timers directly to their respective modules; timers are automatically destroyed when a module is unloaded, effectively preventing memory leaks.
  • Enables cross-module function calls and variable access via `getModule()`, facilitating seamless functional interoperability between modules.
  • Supports the unloading of individual modules as well as a one-click option to clear all modules, allowing for hot-reloading without requiring a server restart.
  • Avoids polluting the global scope and leaves native logic unmodified; integration is straightforward and compatible with all standard Squirrel environments.

Here is a simple example.

function onPlayerJoin(player) {
MessagePlayer("[#FFFF00]Hello " + player.Name, player);
}

function onScriptLoad() {
print("onScriptLoad: " + moduleName);
}

function onScriptUnload() {
print("onScriptUnload: " + moduleName);

a.Delete();
}

function hello(value) {
print("hello " + value);
}

a <- newTimer("hello", 1000, 0);

Place the script file into the "module" folder, then load the filename to add the script's functionality to the server.

ModuleManager.load("testModule");