TrackManiaControl/libs/FML/Script/Script.php

318 lines
7.4 KiB
PHP
Raw Normal View History

2013-12-31 11:19:19 +01:00
<?php
namespace FML\Script;
2014-04-27 14:44:40 +02:00
use FML\Script\Features\ScriptFeature;
2013-12-31 11:19:19 +01:00
/**
* Class representing the ManiaLink Script
*
2014-05-20 15:44:45 +02:00
* @author steeffeen <mail@steeffeen.com>
2014-04-13 18:21:40 +02:00
* @copyright FancyManiaLinks Copyright © 2014 Steffen Schröder
2014-05-14 23:24:00 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
2013-12-31 11:19:19 +01:00
*/
class Script {
2014-01-21 20:30:40 +01:00
/*
2013-12-31 11:19:19 +01:00
* Constants
*/
2014-05-14 23:24:00 +02:00
const TICKINTERVAL = 250;
2014-04-27 14:44:40 +02:00
const VAR_ScriptStart = 'FML_ScriptStart';
const VAR_LoopCounter = 'FML_LoopCounter';
2014-05-14 23:24:00 +02:00
const VAR_LastTick = 'FML_LastTick';
2014-01-21 20:30:40 +01:00
/*
2014-06-21 03:18:21 +02:00
* Protected properties
2013-12-31 11:19:19 +01:00
*/
protected $tagName = 'script';
2014-04-27 14:44:40 +02:00
protected $features = array();
2013-12-31 11:19:19 +01:00
protected $includes = array();
2014-01-03 17:18:14 +01:00
protected $constants = array();
protected $functions = array();
2014-04-27 14:44:40 +02:00
protected $customLabels = array();
protected $genericLabels = array();
2013-12-31 11:19:19 +01:00
2014-01-19 19:30:21 +01:00
/**
2014-04-27 14:44:40 +02:00
* Set a Script Include
2014-01-19 19:30:21 +01:00
*
2014-06-21 03:18:21 +02:00
* @param string $file Include file
* @param string $namespace Include namespace
2014-07-03 22:34:47 +02:00
* @return static
2014-01-19 19:30:21 +01:00
*/
2014-04-27 14:44:40 +02:00
public function setScriptInclude($file, $namespace = null) {
2014-08-11 23:15:53 +02:00
if ($file instanceof ScriptInclude) {
2014-04-27 14:44:40 +02:00
$scriptInclude = $file;
2014-05-14 23:24:00 +02:00
} else {
2014-04-27 14:44:40 +02:00
$scriptInclude = new ScriptInclude($file, $namespace);
}
2014-06-21 03:18:21 +02:00
$this->includes[$scriptInclude->getNamespace()] = $scriptInclude;
2013-12-31 11:19:19 +01:00
return $this;
}
2014-01-03 17:18:14 +01:00
/**
2014-04-27 14:44:40 +02:00
* Add a Script Constant
2014-01-03 17:18:14 +01:00
*
2014-06-21 03:18:21 +02:00
* @param string $name Constant name
* @param string $value Constant value
2014-07-03 22:34:47 +02:00
* @return static
2014-01-03 17:18:14 +01:00
*/
2014-04-27 14:44:40 +02:00
public function addScriptConstant($name, $value = null) {
2014-08-11 23:15:53 +02:00
if ($name instanceof ScriptConstant) {
2014-04-27 14:44:40 +02:00
$scriptConstant = $name;
2014-05-14 23:24:00 +02:00
} else {
2014-04-27 14:44:40 +02:00
$scriptConstant = new ScriptConstant($name, $value);
}
2014-06-21 03:18:21 +02:00
if (!in_array($scriptConstant, $this->constants)) {
array_push($this->constants, $scriptConstant);
}
2014-01-03 17:18:14 +01:00
return $this;
}
/**
2014-04-27 14:44:40 +02:00
* Add a Script Function
2014-01-03 17:18:14 +01:00
*
2014-06-21 03:18:21 +02:00
* @param string $name Function name
* @param string $text Function text
2014-07-03 22:34:47 +02:00
* @return static
2014-01-03 17:18:14 +01:00
*/
2014-04-27 14:44:40 +02:00
public function addScriptFunction($name, $text = null) {
2014-08-11 23:15:53 +02:00
if ($name instanceof ScriptFunction) {
2014-04-27 14:44:40 +02:00
$scriptFunction = $name;
2014-05-14 23:24:00 +02:00
} else {
2014-04-27 14:44:40 +02:00
$scriptFunction = new ScriptFunction($name, $text);
2014-01-03 17:18:14 +01:00
}
2014-04-28 16:59:55 +02:00
if (!in_array($scriptFunction, $this->functions)) {
array_push($this->functions, $scriptFunction);
}
2013-12-31 11:19:19 +01:00
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Add a custom Script text
2013-12-31 11:19:19 +01:00
*
2014-06-21 03:18:21 +02:00
* @param string $name Label name
* @param string $text Script text
2014-07-03 22:34:47 +02:00
* @return static
2013-12-31 11:19:19 +01:00
*/
2014-04-27 14:44:40 +02:00
public function addCustomScriptLabel($name, $text = null) {
2014-08-11 23:15:53 +02:00
if ($name instanceof ScriptLabel) {
2014-04-27 14:44:40 +02:00
$scriptLabel = $name;
2014-05-14 23:24:00 +02:00
} else {
2014-04-27 14:44:40 +02:00
$scriptLabel = new ScriptLabel($name, $text);
2013-12-31 11:19:19 +01:00
}
2014-04-27 14:44:40 +02:00
array_push($this->customLabels, $scriptLabel);
2013-12-31 11:19:19 +01:00
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Append a generic Script text for the next rendering
2013-12-31 11:19:19 +01:00
*
2014-06-21 03:18:21 +02:00
* @param string $name Label name
* @param string $text Script text
2014-05-14 23:24:00 +02:00
* @param bool $isolated (optional) Whether to isolate the Label Script
2014-07-03 22:34:47 +02:00
* @return static
2013-12-31 11:19:19 +01:00
*/
2014-04-27 14:44:40 +02:00
public function appendGenericScriptLabel($name, $text = null, $isolated = false) {
2014-08-11 23:15:53 +02:00
if ($name instanceof ScriptLabel) {
2014-04-27 14:44:40 +02:00
$scriptLabel = $name;
2014-05-14 23:24:00 +02:00
} else {
2014-04-27 14:44:40 +02:00
$scriptLabel = new ScriptLabel($name, $text, $isolated);
2014-01-19 19:30:21 +01:00
}
2014-04-27 14:44:40 +02:00
array_push($this->genericLabels, $scriptLabel);
2013-12-31 11:19:19 +01:00
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Remove all generic Script texts
2013-12-31 11:19:19 +01:00
*
2014-07-03 22:34:47 +02:00
* @return static
2013-12-31 11:19:19 +01:00
*/
2014-04-27 14:44:40 +02:00
public function resetGenericScriptLabels() {
$this->genericLabels = array();
2013-12-31 11:19:19 +01:00
return $this;
}
2014-01-05 16:04:55 +01:00
/**
2014-06-21 03:18:21 +02:00
* Add a Script Feature
2014-01-05 16:04:55 +01:00
*
2014-04-27 14:44:40 +02:00
* @param ScriptFeature $feature Script Feature
2014-07-03 22:34:47 +02:00
* @return static
2014-01-05 16:04:55 +01:00
*/
2014-04-27 14:44:40 +02:00
public function addFeature(ScriptFeature $feature) {
2014-06-21 03:18:21 +02:00
if (!in_array($feature, $this->features, true)) {
array_push($this->features, $feature);
}
2014-01-05 16:04:55 +01:00
return $this;
}
2014-01-05 17:19:44 +01:00
/**
2014-04-27 14:44:40 +02:00
* Load the given Script Feature
2014-01-05 17:19:44 +01:00
*
2014-04-27 14:44:40 +02:00
* @param ScriptFeature $scriptFeature Script Feature to load
2014-07-03 22:34:47 +02:00
* @return static
2014-01-05 17:19:44 +01:00
*/
2014-04-27 14:44:40 +02:00
public function loadFeature(ScriptFeature $scriptFeature) {
$scriptFeature->prepare($this);
2014-01-05 17:19:44 +01:00
return $this;
}
2014-01-12 00:51:46 +01:00
/**
2014-04-27 14:44:40 +02:00
* Load the given Script Features
2014-01-12 00:51:46 +01:00
*
2014-06-21 03:18:21 +02:00
* @param ScriptFeature[] $scriptFeatures Script Features to load
2014-07-03 22:34:47 +02:00
* @return static
2014-01-12 00:51:46 +01:00
*/
2014-04-27 14:44:40 +02:00
public function loadFeatures(array $scriptFeatures) {
foreach ($scriptFeatures as $scriptFeature) {
$this->loadFeature($scriptFeature);
2014-01-12 00:51:46 +01:00
}
return $this;
}
2014-01-21 20:30:40 +01:00
/**
2014-06-21 03:18:21 +02:00
* Check if the Script has content so that it needs to be rendered
2014-01-21 20:30:40 +01:00
*
2014-04-27 14:44:40 +02:00
* @return bool
2014-01-21 20:30:40 +01:00
*/
2014-04-27 14:44:40 +02:00
public function needsRendering() {
if ($this->features || $this->customLabels || $this->genericLabels) {
return true;
2014-01-21 20:30:40 +01:00
}
2014-04-27 14:44:40 +02:00
return false;
2014-01-21 20:30:40 +01:00
}
2014-02-16 13:59:28 +01:00
/**
2014-06-21 03:18:21 +02:00
* Build the complete Script text
2014-02-16 13:59:28 +01:00
*
2014-04-27 14:44:40 +02:00
* @return string
2014-02-16 13:59:28 +01:00
*/
2014-04-27 14:44:40 +02:00
public function buildScriptText() {
$scriptText = PHP_EOL;
$scriptText .= $this->getHeaderComment();
$scriptText .= $this->getIncludes();
$scriptText .= $this->getConstants();
$scriptText .= $this->getFunctions();
$scriptText .= $this->getLabels();
$scriptText .= $this->getMainFunction();
return $scriptText;
2014-02-16 13:59:28 +01:00
}
2013-12-31 11:19:19 +01:00
/**
2014-06-21 03:18:21 +02:00
* Build the Script XML element
2013-12-31 11:19:19 +01:00
*
2014-06-21 03:18:21 +02:00
* @param \DOMDocument $domDocument DOMDocument for which the XML element should be created
2013-12-31 11:19:19 +01:00
* @return \DOMElement
*/
public function render(\DOMDocument $domDocument) {
2014-04-27 14:44:40 +02:00
$this->loadFeatures($this->features);
2014-05-14 23:24:00 +02:00
$scriptXml = $domDocument->createElement($this->tagName);
$scriptText = $this->buildScriptText();
2013-12-31 11:19:19 +01:00
$scriptComment = $domDocument->createComment($scriptText);
$scriptXml->appendChild($scriptComment);
return $scriptXml;
}
/**
2014-06-21 03:18:21 +02:00
* Get the header comment
2013-12-31 11:19:19 +01:00
*
* @return string
*/
2014-04-27 14:44:40 +02:00
protected function getHeaderComment() {
$headerComment = '/****************************************************
2014-08-11 23:15:53 +02:00
* FancyManiaLinks';
if (defined('FML_VERSION')) {
$headerComment .= ' v' . FML_VERSION;
}
$headerComment .= ' by steeffeen *
2014-04-27 14:44:40 +02:00
* http://github.com/steeffeen/FancyManiaLinks *
****************************************************/
2014-05-14 23:24:00 +02:00
2014-04-27 14:44:40 +02:00
';
2013-12-31 11:19:19 +01:00
return $headerComment;
}
/**
2014-06-21 03:18:21 +02:00
* Get the Includes text
2013-12-31 11:19:19 +01:00
*
* @return string
*/
2014-04-27 14:44:40 +02:00
protected function getIncludes() {
$includesText = implode(PHP_EOL, $this->includes);
2013-12-31 11:19:19 +01:00
return $includesText;
}
2014-01-03 17:18:14 +01:00
/**
2014-06-21 03:18:21 +02:00
* Get the Constants text
2014-01-03 17:18:14 +01:00
*
* @return string
*/
2014-04-27 14:44:40 +02:00
protected function getConstants() {
$constantsText = implode(PHP_EOL, $this->constants);
2014-01-03 17:18:14 +01:00
return $constantsText;
}
/**
2014-06-21 03:18:21 +02:00
* Get the Functions text
2014-01-03 17:18:14 +01:00
*
* @return string
*/
2014-04-27 14:44:40 +02:00
protected function getFunctions() {
$functionsText = implode(PHP_EOL, $this->functions);
2014-01-03 17:18:14 +01:00
return $functionsText;
}
/**
2014-06-21 03:18:21 +02:00
* Get the Labels text
2014-01-12 01:49:43 +01:00
*
* @return string
*/
2014-04-27 14:44:40 +02:00
protected function getLabels() {
2014-05-14 23:24:00 +02:00
$customLabelsText = implode(PHP_EOL, $this->customLabels);
2014-04-27 14:44:40 +02:00
$genericLabelsText = implode(PHP_EOL, $this->genericLabels);
return $customLabelsText . $genericLabelsText;
2014-01-12 01:49:43 +01:00
}
2013-12-31 11:19:19 +01:00
/**
2014-06-21 03:18:21 +02:00
* Get the main function text
2013-12-31 11:19:19 +01:00
*
* @return string
*/
2014-04-27 14:44:40 +02:00
protected function getMainFunction() {
$mainFunction = '
Void FML_Dummy() {}
main() {
declare ' . self::VAR_ScriptStart . ' = Now;
+++' . ScriptLabel::ONINIT . '+++
declare ' . self::VAR_LoopCounter . ' = 0;
declare ' . self::VAR_LastTick . ' = 0;
while (True) {
yield;
foreach (Event in PendingEvents) {
switch (Event.Type) {
case CMlEvent::Type::EntrySubmit: {
+++' . ScriptLabel::ENTRYSUBMIT . '+++
2014-04-15 15:47:57 +02:00
}
2014-04-27 14:44:40 +02:00
case CMlEvent::Type::KeyPress: {
+++' . ScriptLabel::KEYPRESS . '+++
}
case CMlEvent::Type::MouseClick: {
+++' . ScriptLabel::MOUSECLICK . '+++
}
case CMlEvent::Type::MouseOut: {
+++' . ScriptLabel::MOUSEOUT . '+++
}
case CMlEvent::Type::MouseOver: {
+++' . ScriptLabel::MOUSEOVER . '+++
2014-04-15 15:47:57 +02:00
}
2014-01-05 16:04:55 +01:00
}
}
2014-04-27 14:44:40 +02:00
+++' . ScriptLabel::LOOP . '+++
' . self::VAR_LoopCounter . ' += 1;
if (' . self::VAR_LastTick . ' + ' . self::TICKINTERVAL . ' > Now) continue;
+++' . ScriptLabel::TICK . '+++
' . self::VAR_LastTick . ' = Now;
2014-01-05 16:04:55 +01:00
}
2014-04-27 14:44:40 +02:00
}';
2013-12-31 11:19:19 +01:00
return $mainFunction;
}
2014-01-03 17:18:14 +01:00
}