Commit 16240ff1 authored by Cobra_Fast's avatar Cobra_Fast

Convert indentation to Tabs. Add transient cache.

parent 729e24fb
......@@ -2,64 +2,64 @@
class SimpleXMLHelper
{
public static function ToArray(SimpleXMLElement $sxmle)
{
return self::ToArray_Worker($sxmle);
}
private static function ToArray_Worker(SimpleXMLElement $parent, $recur = 0)
{
$tmp = array();
$ns = $parent->getNamespaces(true);
$ns[''] = '';
foreach ($ns as $prefix => $ns)
{
foreach ($parent->children($ns) as $child)
{
$new = array(
'name' => (strlen($ns) > 0 ? $prefix . ':' : '') . $child->getName(),
'text' => trim((string)$child)
);
$attributes = self::extractAttributes($child);
if (count($attributes) > 0)
$new['attributes'] = $attributes;
$children = self::ToArray_Worker($child, $recur + 1);
if (count($children) > 0)
$new['children'] = $children;
$tmp[] = $new;
}
}
public static function ToArray(SimpleXMLElement $sxmle)
{
return self::ToArray_Worker($sxmle);
}
private static function ToArray_Worker(SimpleXMLElement $parent, $recur = 0)
{
$tmp = array();
$ns = $parent->getNamespaces(true);
$ns[''] = '';
foreach ($ns as $prefix => $ns)
{
foreach ($parent->children($ns) as $child)
{
$new = array(
'name' => (strlen($ns) > 0 ? $prefix . ':' : '') . $child->getName(),
'text' => trim((string)$child)
);
$attributes = self::extractAttributes($child);
if (count($attributes) > 0)
$new['attributes'] = $attributes;
$children = self::ToArray_Worker($child, $recur + 1);
if (count($children) > 0)
$new['children'] = $children;
$tmp[] = $new;
}
}
if ($recur == 0)
{
$tmp2 = array();
$tmp2['name'] = $parent->getName();
$tmp2['attributes'] = self::extractAttributes($parent);
if (count($tmp2['attributes']) <= 0) unset($tmp2['attributes']);
$tmp2['children'] = $tmp;
if (count($tmp2['children']) <= 0) unset($tmp2['children']);
$tmp = $tmp2;
}
if ($recur == 0)
{
$tmp2 = array();
$tmp2['name'] = $parent->getName();
$tmp2['attributes'] = self::extractAttributes($parent);
if (count($tmp2['attributes']) <= 0) unset($tmp2['attributes']);
$tmp2['children'] = $tmp;
if (count($tmp2['children']) <= 0) unset($tmp2['children']);
$tmp = $tmp2;
}
return $tmp;
}
private static function extractAttributes(SimpleXMLElement $sxmle)
{
$result = array();
$ns = $sxmle->getNamespaces(true);
$ns[''] = '';
foreach ($ns as $prefix => $ns)
foreach ($sxmle->attributes($ns) as $key => $val)
$result[(strlen($prefix) > 0 ? $prefix . ':' : '') . $key] = (string)$val;
return $result;
}
return $tmp;
}
private static function extractAttributes(SimpleXMLElement $sxmle)
{
$result = array();
$ns = $sxmle->getNamespaces(true);
$ns[''] = '';
foreach ($ns as $prefix => $ns)
foreach ($sxmle->attributes($ns) as $key => $val)
$result[(strlen($prefix) > 0 ? $prefix . ':' : '') . $key] = (string)$val;
return $result;
}
}
\ No newline at end of file
......@@ -2,52 +2,52 @@
class Debug
{
private static function Connect()
{
if (!defined('DEBUG_SERVER_ADDRESS'))
return false;
$fp = stream_socket_client(DEBUG_SERVER_ADDRESS, $errno, $errstr);
if (!$fp)
{
//trigger_error($errno . ': ' . $errstr);
return false;
}
return $fp;
}
public static function Write($object, $appendln = true, $fancy = true)
{
$fp = Debug::Connect();
if (!$fp) return;
if ($fancy && count($bt = debug_backtrace()) > 1)
{
fwrite($fp, "[");
if (isset($bt[1]['class']))
{
$fc = (strlen($bt[1]['class']) % 10);
fwrite($fp, "\x1b[" . (($fc > 4 ? $fc/2 : $fc) + 32) . ($fc > 4 ? ';1' : '') . "m" . $bt[1]['class'] . "\x1b[0m::");
}
$fc = (strlen($bt[1]['function']) % 10);
fwrite($fp, "\x1b[" . (($fc > 4 ? $fc/2 : $fc) + 32) . ($fc > 4 ? ';1' : '') . "m" . $bt[1]['function'] . "\x1b[0m");
fwrite($fp, "]: ");
}
if (is_string($object))
{
fwrite($fp, $object . ($appendln ? "\n" : ''));
}
else if (is_array($object) || is_object($object))
{
fwrite($fp, print_r($object, true) . ($appendln ? "\n" : ''));
}
else if (is_resource($object))
{
fwrite($fp, 'Resource' . ($appendln ? "\n" : ''));
}
fclose($fp);
}
private static function Connect()
{
if (!defined('DEBUG_SERVER_ADDRESS'))
return false;
$fp = stream_socket_client(DEBUG_SERVER_ADDRESS, $errno, $errstr);
if (!$fp)
{
//trigger_error($errno . ': ' . $errstr);
return false;
}
return $fp;
}
public static function Write($object, $appendln = true, $fancy = true)
{
$fp = Debug::Connect();
if (!$fp) return;
if ($fancy && count($bt = debug_backtrace()) > 1)
{
fwrite($fp, "[");
if (isset($bt[1]['class']))
{
$fc = (strlen($bt[1]['class']) % 10);
fwrite($fp, "\x1b[" . (($fc > 4 ? $fc/2 : $fc) + 32) . ($fc > 4 ? ';1' : '') . "m" . $bt[1]['class'] . "\x1b[0m::");
}
$fc = (strlen($bt[1]['function']) % 10);
fwrite($fp, "\x1b[" . (($fc > 4 ? $fc/2 : $fc) + 32) . ($fc > 4 ? ';1' : '') . "m" . $bt[1]['function'] . "\x1b[0m");
fwrite($fp, "]: ");
}
if (is_string($object))
{
fwrite($fp, $object . ($appendln ? "\n" : ''));
}
else if (is_array($object) || is_object($object))
{
fwrite($fp, print_r($object, true) . ($appendln ? "\n" : ''));
}
else if (is_resource($object))
{
fwrite($fp, 'Resource' . ($appendln ? "\n" : ''));
}
fclose($fp);
}
}
......@@ -5,17 +5,17 @@ $port = 50252;
$socket = stream_socket_server("tcp://0.0.0.0:$port", $errno, $errstr);
if (!$socket)
die($errno . ': ' . $errstr . ' @' . __LINE__);
die($errno . ': ' . $errstr . ' @' . __LINE__);
else
echo 'Socket opened, listening for requests.' . "\n\n";
echo 'Socket opened, listening for requests.' . "\n\n";
while (true)
{
if (is_resource($conn = @stream_socket_accept($socket)))
{
echo '[' . date('r') . ']: ';
while (!feof($conn))
echo fread($conn, 4096);
fclose($conn);
}
if (is_resource($conn = @stream_socket_accept($socket)))
{
echo '[' . date('r') . ']: ';
while (!feof($conn))
echo fread($conn, 4096);
fclose($conn);
}
}
\ No newline at end of file
......@@ -3,65 +3,65 @@ require_once(dirname(__FILE__) . '/vector3.class.php');
class Color3 extends Vector3
{
public function Normalize()
{
$this->x = min(array(1, max(array(0, abs($this->x)))));
$this->y = min(array(1, max(array(0, abs($this->y)))));
$this->z = min(array(1, max(array(0, abs($this->z)))));
return $this;
}
public function To255()
{
return array(
'r' => round($this->x * 255.0),
'g' => round($this->y * 255.0),
'b' => round($this->z * 255.0)
);
}
public function To255Hex()
{
return '#'
. str_pad(dechex(round($this->x * 255.0)), 2, '0', STR_PAD_LEFT)
. str_pad(dechex(round($this->y * 255.0)), 2, '0', STR_PAD_LEFT)
. str_pad(dechex(round($this->z * 255.0)), 2, '0', STR_PAD_LEFT);
}
public static function FromHex($hex)
{
if ($hex[0] == '#')
$hex = substr($hex, 1);
$r = 0;
$g = 0;
$b = 0;
if (strlen($hex) == 3)
{
$r = hexdec($hex[0]) / 15.0;
$g = hexdec($hex[1]) / 15.0;
$b = hexdec($hex[2]) / 15.0;
}
else if (strlen($hex) == 6)
{
$r = hexdec(substr($hex, 0, 2)) / 255.0;
$g = hexdec(substr($hex, 2, 2)) / 255.0;
$b = hexdec(substr($hex, 4, 2)) / 255.0;
}
else
trigger_error('Unsupported format/data passed to FromHexRGB().', E_USER_ERROR);
return new Color3($r, $g, $b);
}
public static function FromByte($r, $g, $b)
{
return new Color3($r / 255.0, $g / 255.0, $b / 255.0);
}
public static function FromFloat($r, $g, $b)
{
return new Color3($r, $g, $b);
}
public function Normalize()
{
$this->x = min(array(1, max(array(0, abs($this->x)))));
$this->y = min(array(1, max(array(0, abs($this->y)))));
$this->z = min(array(1, max(array(0, abs($this->z)))));
return $this;
}
public function To255()
{
return array(
'r' => round($this->x * 255.0),
'g' => round($this->y * 255.0),
'b' => round($this->z * 255.0)
);
}
public function To255Hex()
{
return '#'
. str_pad(dechex(round($this->x * 255.0)), 2, '0', STR_PAD_LEFT)
. str_pad(dechex(round($this->y * 255.0)), 2, '0', STR_PAD_LEFT)
. str_pad(dechex(round($this->z * 255.0)), 2, '0', STR_PAD_LEFT);
}
public static function FromHex($hex)
{
if ($hex[0] == '#')
$hex = substr($hex, 1);
$r = 0;
$g = 0;
$b = 0;
if (strlen($hex) == 3)
{
$r = hexdec($hex[0]) / 15.0;
$g = hexdec($hex[1]) / 15.0;
$b = hexdec($hex[2]) / 15.0;
}
else if (strlen($hex) == 6)
{
$r = hexdec(substr($hex, 0, 2)) / 255.0;
$g = hexdec(substr($hex, 2, 2)) / 255.0;
$b = hexdec(substr($hex, 4, 2)) / 255.0;
}
else
trigger_error('Unsupported format/data passed to FromHexRGB().', E_USER_ERROR);
return new Color3($r, $g, $b);
}
public static function FromByte($r, $g, $b)
{
return new Color3($r / 255.0, $g / 255.0, $b / 255.0);
}
public static function FromFloat($r, $g, $b)
{
return new Color3($r, $g, $b);
}
}
\ No newline at end of file
......@@ -2,148 +2,148 @@
class Vector2
{
public $x;
public $y;
public function __construct($x, $y = false)
{
if ($x instanceof Vector2)
{
$this->x = $x->x;
$this->y = $x->y;
}
else if (is_numeric($x))
{
$this->x = $x;
if (is_numeric($y))
$this->y = $y;
else
$this->y = $x;
}
else
trigger_error('Unsupported construction types.', E_USER_ERROR);
}
public function Length()
{
return sqrt($this->x * $this->x + $this->y * $this->y);
}
public function IsZero()
{
return ($this->x == 0 && $this->y == 0);
}
public function Dot(Vector2 $v)
{
return $this->x * $v->x + $this->y * $v->y;
}
public function Multiply($n)
{
if ($n instanceof Vector2)
{
$this->x = $this->x * $n->x;
$this->y = $this->y * $n->y;
}
else if (is_numeric($n))
{
$this->x *= $n;
$this->y *= $n;
}
else
trigger_error('Unsupported operand type for vector multiplication.', E_USER_ERROR);
return $this;
}
public function Divide($n)
{
if ($n instanceof Vector2)
{
$this->x = $this->x / $n->x;
$this->y = $this->y / $n->y;
}
else if (is_numeric($n))
{
$this->x /= $n;
$this->y /= $n;
}
else
trigger_error('Unsupported operand type for vector multiplication.', E_USER_ERROR);
return $this;
}
public function Add($n)
{
if ($n instanceof Vector2)
{
$this->x = $this->x + $n->x;
$this->y = $this->y + $n->y;
}
else if (is_numeric($n))
{
$this->x += $n;
$this->y += $n;
}
else
trigger_error('Unsupported operand type for vector addition.', E_USER_ERROR);
return $this;
}
public function Substract($n)
{
if ($n instanceof Vector2)
{
$this->x = $this->x - $n->x;
$this->y = $this->y - $n->y;
}
else if (is_numeric($n))
{
$this->x -= $n;
$this->y -= $n;
}
else
trigger_error('Unsupported operand type for vector substraction.', E_USER_ERROR);
return $this;
}
public function Inverse()
{
$this->x = 1.0 / $this->x;
$this->y = 1.0 / $this->y;
}
public function Negate()
{
$this->x = -$this->x;
$this->y = -$this->y;
}
public static function sMultiply(Vector2 $vec, $n)
{
$result = clone $vec;
return $result->Multiply($n);
}
public static function sDivide(Vector2 $vec, $n)
{
$result = clone $vec;
return $result->Divide($n);
}
public static function sAdd(Vector2 $vec, $n)
{
$result = clone $vec;
return $result->Add($n);
}
public static function sSubstract(Vector2 $vec, $n)
{
$result = clone $vec;
return $result->Substract($n);
}
public $x;
public $y;
public function __construct($x, $y = false)
{
if ($x instanceof Vector2)
{
$this->x = $x->x;
$this->y = $x->y;
}
else if (is_numeric($x))
{
$this->x = $x;
if (is_numeric($y))
$this->y = $y;
else
$this->y = $x;
}
else
trigger_error('Unsupported construction types.', E_USER_ERROR);
}
public function Length()
{
return sqrt($this->x * $this->x + $this->y * $this->y);
}
public function IsZero()
{
return ($this->x == 0 && $this->y == 0);
}
public function Dot(Vector2 $v)
{
return $this->x * $v->x + $this->y * $v->y;
}
public function Multiply($n)
{
if ($n instanceof Vector2)
{
$this->x = $this->x * $n->x;
$this->y = $this->y * $n->y;
}
else if (is_numeric($n))
{
$this->x *= $n;
$this->y *= $n;
}
else
trigger_error('Unsupported operand type for vector multiplication.', E_USER_ERROR);
return $this;
}
public function Divide($n)
{
if ($n instanceof Vector2)
{
$this->x = $this->x / $n->x;
$this->y = $this->y / $n->y;
}
else if (is_numeric($n))
{
$this->x /= $n;
$this->y /= $n;
}
else
trigger_error('Unsupported operand type for vector multiplication.', E_USER_ERROR);
return $this;
}
public function Add($n)
{
if ($n instanceof Vector2)
{
$this->x = $this->x + $n->x;
$this->y = $this->y + $n->y;
}
else if (is_numeric($n))
{
$this->x += $n;
$this->y += $n;
}
else
trigger_error('Unsupported operand type for vector addition.', E_USER_ERROR);
return $this;
}
public function Substract($n)
{
if ($n instanceof Vector2)
{
$this->x = $this->x - $n->x;
$this->y = $this->y - $n->y;
}
else if (is_numeric($n))
{
$this->x -= $n;
$this->y -= $n;
}
else
trigger_error('Unsupported operand type for vector substraction.', E_USER_ERROR);
return $this;
}
public function Inverse()
{
$this->x = 1.0 / $this->x;
$this->y = 1.0 / $this->y;
}
public function Negate()
{
$this->x = -$this->x;
$this->y = -$this->y;
}
public static function sMultiply(Vector2 $vec, $n)
{
$result = clone $vec;
return $result->Multiply($n);
}
public static function sDivide(Vector2 $vec, $n)
{
$result = clone $vec;
return $result->Divide($n);
}
public static function sAdd(Vector2 $vec, $n)
{
$result = clone $vec;
return $result->Add($n);
}
public static function sSubstract(Vector2 $vec, $n)
{
$result = clone $vec;
return $result->Substract($n);
}
}
\ No newline at end of file
......@@ -2,170 +2,170 @@
class Vector3
{
public $x;
public $y;
public $z;
public function __construct($x, $y = false, $z = false)
{
if ($x instanceof Vector3)
{
$this->x = $x->x;
$this->y = $x->y;
$this->z = $x->z;
}
else if (is_numeric($x))
{
$this->x = $x;
if (is_numeric($y))
$this->y = $y;
else
$this->y = $x;
if (is_numeric($z))
$this->z = $z;