nabucco-server/lib/mods.php

57 lines
No EOL
2 KiB
PHP

<?php
namespace sockchat;
class Modules {
protected static $mods = [];
protected static $cmds = [];
public static function Load() {
require_once("./mods/generic_mod.php");
$mods = glob("./mods/*", GLOB_ONLYDIR);
foreach($mods as $mod) {
$name = substr($mod, strrpos($mod, "/")+1);
$fileName = "{$mod}/{$name}.php";
if(file_exists($fileName)) {
include($fileName);
$className = "\\sockchat\\mods\\{$name}\\Main";
if(class_exists($className)) {
Modules::$mods[$name] = $name;
call_user_func([$className, 'Init']);
$cmds = call_user_func([$className, 'GetCommands']);
foreach($cmds as $cmd) {
if(array_key_exists($cmd, self::$cmds))
echo "Error loading module $name: Command $cmd has already been defined by module ". self::$cmds[$cmd] ."!\n";
else self::$cmds[$cmd] = $name;
}
}
}
}
//var_dump(self::$cmds);
}
public static function DoesModExist($mod) {
return array_key_exists($mod, self::$mods);
}
public static function DoesCommandExist($cmd) {
return array_key_exists($cmd, self::$cmds);
}
public static function ExecuteRoutine($routine, $args, $exitAfterFirstReturn = false) {
$ret = true;
foreach(Modules::$mods as $mod) {
$ret &= call_user_func_array("\\sockchat\\mods\\{$mod}\\Main::{$routine}", $args) === null;
if(!$ret && $exitAfterFirstReturn) break;
}
return $ret;
}
public static function ExecuteCommand($cmd, $user, $args) {
if(!array_key_exists($cmd, self::$cmds)) return false;
else {
call_user_func_array("\\sockchat\\mods\\". self::$cmds[$cmd] ."\\Main::ExecuteCommand", [$cmd, $args, $user, "\\sockchat\\mods\\". self::$cmds[$cmd] ."\\Main"]);
return true;
}
}
}