ManiacontrolPlugins/Beu/ReloadDevTool.php

184 lines
5.9 KiB
PHP
Raw Normal View History

2022-02-14 11:39:38 +01:00
<?php
namespace Beu;
use ManiaControl\ManiaControl;
use ManiaControl\Plugins\Plugin;
use \ManiaControl\Logger;
use ManiaControl\Players\PlayerManager;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Players\Player;
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
use \Exception;
/**
2022-02-16 00:20:20 +01:00
* ReloadDevTool
2022-02-14 11:39:38 +01:00
*
* @author Beu
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
2022-02-16 00:20:20 +01:00
class ReloadDevTool implements ManialinkPageAnswerListener, CallbackListener, Plugin {
2022-02-14 11:39:38 +01:00
/*
* Constants
*/
const PLUGIN_ID = 165;
2022-11-15 09:36:25 +01:00
const PLUGIN_VERSION = 1.1;
2022-02-16 00:20:20 +01:00
const PLUGIN_NAME = 'ReloadDevTool';
2022-02-14 11:39:38 +01:00
const PLUGIN_AUTHOR = 'Beu';
2022-02-16 00:20:20 +01:00
const SETTING_RELOAD_GAMEMODE = 'Reload Gamemode';
const SETTING_GAMEMODE_TO_LOAD = 'Gamemode to load';
2022-11-15 09:36:25 +01:00
const SETTING_RELOAD_PLUGIN = 'Reload Server Plugin';
const SETTING_PLUGIN_TO_LOAD = 'Server Plugin to load';
2022-02-16 00:20:20 +01:00
const SETTING_RESTART_MANIACONTROL = 'Restart Maniacontrol';
2022-02-14 11:39:38 +01:00
/*
* Private properties
*/
/** @var ManiaControl $maniaControl */
private $maniaControl = null;
private $manialink = <<<'EOD'
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2022-02-16 00:20:20 +01:00
<manialink id="ReloadDevTool" version="3">
2022-02-14 11:39:38 +01:00
<script><!--
main () {
log("DEBUG_ReloadGamemode loaded");
while(True) {
yield;
foreach(Event in PendingEvents) {
if (Event.Type == CMlScriptEvent::Type::KeyPress && Event.KeyName == "F5") {
2022-02-16 00:20:20 +01:00
TriggerPageAction("ReloadDevTool_Reload");
2022-02-14 11:39:38 +01:00
}
}
}
}
--></script>
</manialink>
EOD;
/**
* @see \ManiaControl\Plugins\Plugin::prepare()
*/
public static function prepare(ManiaControl $maniaControl) {
}
/**
* @see \ManiaControl\Plugins\Plugin::getId()
*/
public static function getId() {
return self::PLUGIN_ID;
}
/**
* @see \ManiaControl\Plugins\Plugin::getName()
*/
public static function getName() {
return self::PLUGIN_NAME;
}
/**
* @see \ManiaControl\Plugins\Plugin::getVersion()
*/
public static function getVersion() {
return self::PLUGIN_VERSION;
}
/**
* @see \ManiaControl\Plugins\Plugin::getAuthor()
*/
public static function getAuthor() {
return self::PLUGIN_AUTHOR;
}
/**
* @see \ManiaControl\Plugins\Plugin::getDescription()
*/
public static function getDescription() {
2022-02-16 00:20:20 +01:00
return "Simple plugin to reload gamemode or maniacontrol with F5";
2022-02-14 11:39:38 +01:00
}
/**
* @see \ManiaControl\Plugins\Plugin::load()
*/
public function load(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2022-11-15 09:36:25 +01:00
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_RELOAD_GAMEMODE, false, "", 10);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_GAMEMODE_TO_LOAD, "", 'File to load in UserData/Scripts/Modes/', 11);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_RELOAD_PLUGIN, false, "", 20);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_PLUGIN_TO_LOAD, "", 'File to load in UserData/Scripts', 21);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_RESTART_MANIACONTROL, false, "", 30);
2022-02-16 00:20:20 +01:00
2022-02-14 11:39:38 +01:00
$this->maniaControl->getCallbackManager()->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerConnect');
2022-02-16 00:20:20 +01:00
$this->maniaControl->getManialinkManager()->registerManialinkPageAnswerListener("ReloadDevTool_Reload", $this, 'handleReload');
2022-02-14 11:39:38 +01:00
2022-02-16 00:20:20 +01:00
$admins = $this->maniaControl->getAuthenticationManager()->getAdmins();
if (!empty($admins)) {
$this->maniaControl->getManialinkManager()->sendManialink($this->manialink,$admins);
2022-02-14 11:39:38 +01:00
}
2022-02-16 00:20:20 +01:00
2022-02-14 11:39:38 +01:00
}
2022-02-16 00:20:20 +01:00
/**
2022-02-14 11:39:38 +01:00
* Handle when a player connects
*
* @param Player $player
*/
public function handlePlayerConnect(Player $player) {
2022-02-16 00:20:20 +01:00
if ($player->authLevel > 0) {
$this->maniaControl->getManialinkManager()->sendManialink($this->manialink,$player->login);
}
2022-02-14 11:39:38 +01:00
}
2022-02-16 00:20:20 +01:00
public function handleReload(array $callback, Player $player) {
if ($player->authLevel <= 0) {
Logger::logError('Wrong authlevel');
return;
}
Logger::log('handleReload');
$this->maniaControl->getChat()->sendSuccess("Handle Reload");
if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_RELOAD_GAMEMODE)) {
$file = $this->maniaControl->getServer()->getDirectory()->getUserDataFolder() . "Scripts" . DIRECTORY_SEPARATOR . "Modes" . DIRECTORY_SEPARATOR . $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_GAMEMODE_TO_LOAD);
if ($file && is_file($file)) {
try {
$this->maniaControl->getChat()->sendSuccess("Loading In-Dev Script");
$this->maniaControl->getClient()->setModeScriptText(file_get_contents($file));
} catch (\Exception $e) {
Logger::logError($e->getMessage());
$this->maniaControl->getChat()->sendErrorToAdmins($e->getMessage());
}
} else {
Logger::logError('No Game mode to load');
$this->maniaControl->getChat()->sendErrorToAdmins("No Game mode to load");
2022-02-14 11:39:38 +01:00
}
2022-02-16 00:20:20 +01:00
}
2022-11-15 09:36:25 +01:00
if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_RELOAD_PLUGIN)) {
$file = $this->maniaControl->getServer()->getDirectory()->getUserDataFolder() . "Scripts" . DIRECTORY_SEPARATOR . $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_PLUGIN_TO_LOAD);
if ($file && is_file($file)) {
try {
$this->maniaControl->getChat()->sendSuccess("Loading Script Plugin");
$this->maniaControl->getClient()->execute("SetServerPlugin", [true, $file]);
} catch (\Exception $e) {
Logger::logError($e->getMessage());
$this->maniaControl->getChat()->sendErrorToAdmins($e->getMessage());
}
} else {
Logger::logError('No Script Plugin to load');
$this->maniaControl->getChat()->sendErrorToAdmins("No Script Plugin to load");
}
}
2022-02-16 00:20:20 +01:00
if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_RESTART_MANIACONTROL)) {
$this->maniaControl->reboot();
2022-02-14 11:39:38 +01:00
}
}
/**
* Unload the plugin and its Resources
*/
public function unload() {
}
}