TrackManiaControl/libs/FML/Script/Script.php

383 lines
9.6 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>
2017-03-25 18:40:15 +01:00
* @copyright FancyManiaLinks Copyright © 2017 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
*/
2017-03-25 18:40:15 +01:00
class Script
{
2014-05-14 23:24:00 +02:00
2017-03-25 18:40:15 +01:00
/*
* Constants
*/
const TICKINTERVAL = 250;
2017-04-02 16:32:57 +02:00
const VAR_ScriptStart = "FML_ScriptStart";
const VAR_LoopCounter = "FML_LoopCounter";
const VAR_LastTick = "FML_LastTick";
2013-12-31 11:19:19 +01:00
2017-04-02 16:32:57 +02:00
/**
* @var ScriptFeature[] $features Script Features
2017-03-25 18:40:15 +01:00
*/
protected $features = array();
2017-04-02 16:32:57 +02:00
/**
* @var ScriptInclude[] $includes Script Includes
*/
2017-03-25 18:40:15 +01:00
protected $includes = array();
2017-04-02 16:32:57 +02:00
/**
* @var ScriptConstant[] $constants Script Constants
*/
2017-03-25 18:40:15 +01:00
protected $constants = array();
2017-04-02 16:32:57 +02:00
/**
* @var ScriptFunction[] $functions Script Functions
*/
2017-03-25 18:40:15 +01:00
protected $functions = array();
2017-04-02 16:32:57 +02:00
/**
* @var ScriptLabel[] $customLabels Custom Script Labels
*/
2017-03-25 18:40:15 +01:00
protected $customLabels = array();
2017-04-02 16:32:57 +02:00
/**
* @var ScriptLabel[] $genericLabels Generic Script Labels
*/
2017-03-25 18:40:15 +01:00
protected $genericLabels = array();
2013-12-31 11:19:19 +01:00
2017-03-25 18:40:15 +01:00
/**
* Set a Script Include
*
* @api
* @param string|ScriptInclude $file Include file or ScriptInclude
* @param string $namespace Include namespace
* @return static
*/
public function setScriptInclude($file, $namespace = null)
{
if ($file instanceof ScriptInclude) {
$scriptInclude = $file;
} else {
$scriptInclude = new ScriptInclude($file, $namespace);
}
$this->includes[$scriptInclude->getNamespace()] = $scriptInclude;
return $this;
}
2014-01-03 17:18:14 +01:00
2017-03-25 18:40:15 +01:00
/**
* Add a Script Constant
*
* @api
* @param string|ScriptConstant $name Constant name or ScriptConstant
* @param string $value Constant value
* @return static
*/
public function addScriptConstant($name, $value = null)
{
if ($name instanceof ScriptConstant) {
$scriptConstant = $name;
} else {
$scriptConstant = new ScriptConstant($name, $value);
}
if (!in_array($scriptConstant, $this->constants)) {
array_push($this->constants, $scriptConstant);
}
return $this;
}
2013-12-31 11:19:19 +01:00
2017-03-25 18:40:15 +01:00
/**
* Add a Script Function
*
* @api
* @param string|ScriptFunction $name Function name or ScriptFunction
* @param string $text Function text
* @return static
*/
public function addScriptFunction($name, $text = null)
{
if ($name instanceof ScriptFunction) {
$scriptFunction = $name;
} else {
$scriptFunction = new ScriptFunction($name, $text);
}
if (!in_array($scriptFunction, $this->functions)) {
array_push($this->functions, $scriptFunction);
}
return $this;
}
2013-12-31 11:19:19 +01:00
2017-03-25 18:40:15 +01:00
/**
* Add a custom Script text
*
* @api
* @param string|ScriptLabel $name Label name or ScriptLabel
* @param string $text Script text
* @return static
*/
public function addCustomScriptLabel($name, $text = null)
{
if ($name instanceof ScriptLabel) {
$scriptLabel = $name;
} else {
$scriptLabel = new ScriptLabel($name, $text);
}
if (!in_array($scriptLabel, $this->customLabels)) {
array_push($this->customLabels, $scriptLabel);
}
return $this;
}
2013-12-31 11:19:19 +01:00
2017-03-25 18:40:15 +01:00
/**
* Append a generic Script text for the next rendering
*
* @TODO: get rid of generic script labels approach
* @param string|ScriptLabel $name Label name or ScriptLabel
* @param string $text Script text
* @param bool $isolated (optional) Whether to isolate the Label Script
* @return static
*/
public function appendGenericScriptLabel($name, $text = null, $isolated = false)
{
if ($name instanceof ScriptLabel) {
$scriptLabel = $name;
} else {
$scriptLabel = new ScriptLabel($name, $text, $isolated);
}
if (!in_array($scriptLabel, $this->genericLabels)) {
array_push($this->genericLabels, $scriptLabel);
}
return $this;
}
2013-12-31 11:19:19 +01:00
2017-03-25 18:40:15 +01:00
/**
* Remove all generic Script texts
*
* @TODO: get rid of generic script labels approach
* @return static
*/
public function resetGenericScriptLabels()
{
$this->genericLabels = array();
return $this;
}
2014-01-05 16:04:55 +01:00
2017-03-25 18:40:15 +01:00
/**
* Add a Script Feature
*
* @api
* @param ScriptFeature $feature Script Feature
* @return static
*/
public function addFeature(ScriptFeature $feature)
{
if (!in_array($feature, $this->features, true)) {
array_push($this->features, $feature);
}
return $this;
}
2014-01-05 17:19:44 +01:00
2017-03-25 18:40:15 +01:00
/**
* Load the given Script Feature
*
* @param ScriptFeature $scriptFeature Script Feature to load
* @return static
*/
public function loadFeature(ScriptFeature $scriptFeature)
{
$scriptFeature->prepare($this);
return $this;
}
2014-01-12 00:51:46 +01:00
2017-03-25 18:40:15 +01:00
/**
* Load the given Script Features
*
* @param ScriptFeature[] $scriptFeatures Script Features to load
* @return static
*/
public function loadFeatures(array $scriptFeatures)
{
foreach ($scriptFeatures as $scriptFeature) {
$this->loadFeature($scriptFeature);
}
return $this;
}
2014-01-21 20:30:40 +01:00
2017-03-25 18:40:15 +01:00
/**
* Check if the Script has content so that it needs to be rendered
*
* @return bool
*/
public function needsRendering()
{
if ($this->features || $this->customLabels || $this->genericLabels) {
return true;
}
return false;
}
2014-02-16 13:59:28 +01:00
2017-03-25 18:40:15 +01:00
/**
* Build the complete Script text
*
* @return string
*/
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;
}
2013-12-31 11:19:19 +01:00
2017-03-25 18:40:15 +01:00
/**
* Render the Script
*
* @param \DOMDocument $domDocument DOMDocument for which the Script should be created
* @return \DOMElement
*/
public function render(\DOMDocument $domDocument)
{
$this->loadFeatures($this->features);
2017-04-02 16:32:57 +02:00
$scriptXml = $domDocument->createElement("script");
$scriptText = $this->buildScriptText();
2017-03-25 18:40:15 +01:00
$scriptComment = $domDocument->createComment($scriptText);
$scriptXml->appendChild($scriptComment);
2017-04-02 16:32:57 +02:00
2017-03-25 18:40:15 +01:00
return $scriptXml;
}
/**
* Get the header comment
*
* @return string
*/
protected function getHeaderComment()
{
$headerComment = '/**************************************************
* FancyManiaLinks';
2017-03-25 18:40:15 +01:00
if (defined('FML_VERSION')) {
$headerComment .= ' v' . FML_VERSION;
}
$headerComment .= ' by steeffeen *
* http://github.com/steeffeen/FancyManiaLinks *
**************************************************/
2014-05-14 23:24:00 +02:00
2014-04-27 14:44:40 +02:00
';
2017-03-25 18:40:15 +01:00
return $headerComment;
}
2013-12-31 11:19:19 +01:00
2017-03-25 18:40:15 +01:00
/**
* Get the Includes text
*
* @return string
*/
protected function getIncludes()
{
$includesText = implode(PHP_EOL, $this->includes);
return $includesText;
}
2013-12-31 11:19:19 +01:00
2017-03-25 18:40:15 +01:00
/**
* Get the Constants text
*
* @return string
*/
protected function getConstants()
{
$constantsText = implode(PHP_EOL, $this->constants);
return $constantsText;
}
2014-01-03 17:18:14 +01:00
2017-03-25 18:40:15 +01:00
/**
* Get the Functions text
*
* @return string
*/
protected function getFunctions()
{
$functionsText = implode(PHP_EOL, $this->functions);
return $functionsText;
}
2014-01-03 17:18:14 +01:00
2017-03-25 18:40:15 +01:00
/**
* Get the Labels text
*
* @return string
*/
protected function getLabels()
{
$customLabelsText = implode(PHP_EOL, $this->customLabels);
$genericLabelsText = implode(PHP_EOL, $this->genericLabels);
return $customLabelsText . $genericLabelsText;
}
2014-01-12 01:49:43 +01:00
2017-03-25 18:40:15 +01:00
/**
* Get the main function text
*
* @return string
*/
protected function getMainFunction()
{
$mainFunction = '
2014-04-27 14:44:40 +02:00
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 . '+++
+++' . ScriptLabel::MOUSECLICK2 . '+++
2014-04-27 14:44:40 +02:00
}
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
}';
2017-03-25 18:40:15 +01:00
return $mainFunction;
}
/**
* Get the string representation
*
* @return string
*/
public function __toString()
{
return $this->buildScriptText();
}
2014-01-03 17:18:14 +01:00
}