TrackManiaControl/application/core/Libs/FML/Script/Script.php

314 lines
7.3 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
*
* @author steeffeen
2014-04-13 18:21:40 +02:00
* @copyright FancyManiaLinks Copyright © 2014 Steffen Schröder
* @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-04-27 14:44:40 +02:00
const TICKINTERVAL = 250;
const VAR_ScriptStart = 'FML_ScriptStart';
const VAR_LoopCounter = 'FML_LoopCounter';
const VAR_LastTick = 'FML_LastTick';
2013-12-31 11:19:19 +01:00
2014-01-21 20:30:40 +01:00
/*
2013-12-31 11:19:19 +01:00
* Protected Properties
*/
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-04-27 14:44:40 +02:00
* @param string $file Include File
* @param string $namespace Include Namespace
2014-01-19 19:30:21 +01:00
* @return \FML\Script\Script
*/
2014-04-27 14:44:40 +02:00
public function setScriptInclude($file, $namespace = null) {
if (is_object($file) && ($file instanceof ScriptInclude)) {
$scriptInclude = $file;
}
else {
$scriptInclude = new ScriptInclude($file, $namespace);
}
$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-04-27 14:44:40 +02:00
* @param string $name Constant Name
2014-01-12 00:51:46 +01:00
* @param string $value Constant Value
2014-01-03 17:18:14 +01:00
* @return \FML\Script\Script
*/
2014-04-27 14:44:40 +02:00
public function addScriptConstant($name, $value = null) {
if (is_object($name) && ($name instanceof ScriptConstant)) {
$scriptConstant = $name;
}
else {
$scriptConstant = new ScriptConstant($name, $value);
}
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-01-12 00:51:46 +01:00
* @param string $name Function Name
2014-04-27 14:44:40 +02:00
* @param string $text Function Text
2014-01-03 17:18:14 +01:00
* @return \FML\Script\Script
*/
2014-04-27 14:44:40 +02:00
public function addScriptFunction($name, $text = null) {
if (is_object($name) && ($name instanceof ScriptFunction)) {
$scriptFunction = $name;
2013-12-31 11:19:19 +01:00
}
2014-04-27 14:44:40 +02:00
else {
$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-04-27 14:44:40 +02:00
* Add a custom Script Text
2013-12-31 11:19:19 +01:00
*
2014-04-27 14:44:40 +02:00
* @param string $name Label Name
* @param string $text Script Text
2013-12-31 11:19:19 +01:00
* @return \FML\Script\Script
*/
2014-04-27 14:44:40 +02:00
public function addCustomScriptLabel($name, $text = null) {
if (is_object($name) && ($name instanceof ScriptLabel)) {
$scriptLabel = $name;
2013-12-31 11:19:19 +01:00
}
2014-04-27 14:44:40 +02:00
else {
$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-04-27 14:44:40 +02:00
* Append a generic Script Text for the next Rendering
2013-12-31 11:19:19 +01:00
*
2014-04-27 14:44:40 +02:00
* @param string $name Label Name
* @param string $text Script Text
* @param bool $isolated (optional) Whether to isolate the Label Script
2013-12-31 11:19:19 +01:00
* @return \FML\Script\Script
*/
2014-04-27 14:44:40 +02:00
public function appendGenericScriptLabel($name, $text = null, $isolated = false) {
if (is_object($name) && ($name instanceof ScriptLabel)) {
$scriptLabel = $name;
2013-12-31 11:19:19 +01:00
}
2014-04-27 14:44:40 +02:00
else {
$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-04-27 14:44:40 +02:00
* Remove all generic Script Texts
2013-12-31 11:19:19 +01:00
*
* @return \FML\Script\Script
*/
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-04-27 14:44:40 +02:00
* Add an own Script Feature
2014-01-05 16:04:55 +01:00
*
2014-04-27 14:44:40 +02:00
* @param ScriptFeature $feature Script Feature
2014-01-05 16:04:55 +01:00
* @return \FML\Script\Script
*/
2014-04-27 14:44:40 +02:00
public function addFeature(ScriptFeature $feature) {
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-01-05 17:19:44 +01:00
* @return \FML\Script\Script
*/
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-04-27 14:44:40 +02:00
* @param array $scriptFeatures Script Features to load
2014-01-12 00:51:46 +01:00
* @return \FML\Script\Script
*/
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-04-27 14:44:40 +02:00
* Check if the Script has Stuff 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-04-27 14:44:40 +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
/**
* Create the Script XML Tag
*
2014-01-12 00:51:46 +01: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);
2013-12-31 11:19:19 +01:00
$scriptXml = $domDocument->createElement($this->tagName);
$scriptText = $this->buildScriptText();
$scriptComment = $domDocument->createComment($scriptText);
$scriptXml->appendChild($scriptComment);
return $scriptXml;
}
/**
* Get the Header Comment
*
* @return string
*/
2014-04-27 14:44:40 +02:00
protected function getHeaderComment() {
$headerComment = '/****************************************************
* FancyManiaLinks v' . FML_VERSION . ' by steeffeen *
* http://github.com/steeffeen/FancyManiaLinks *
****************************************************/
';
2013-12-31 11:19:19 +01:00
return $headerComment;
}
/**
* Get the Includes
*
* @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
/**
* Get the Constants
*
* @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;
}
/**
* Get the Functions
*
* @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-04-27 14:44:40 +02:00
* Get the Labels
2014-01-12 01:49:43 +01:00
*
* @return string
*/
2014-04-27 14:44:40 +02:00
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
}
2013-12-31 11:19:19 +01:00
/**
2014-04-27 14:44:40 +02:00
* Get the Main Function
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
}