<?php
php
/*
xergio - [email protected]
http://en.lfsmanual.net/wiki/User:xergio
http://en.lfsmanual.net/wiki/File_Formats#SET
http://www.lfsforum.net/showthread.php?t=14477
http://www.lfsforum.net/showthread.php?t=27054
*/
class PHPSET {
public $file;
public $headers;
public $fields;
function __construct($file) {
$this->file = trim($file);
$this->header_len = 12;
$this->body_len = 132 - $this->header_len; //132 char == \0
$this->parse();
}
public function parse($file = null) {
$this->stime = microtime(true);
if ($file) {
$this->file = trim($file);
}
if (!file_exists($this->file) or !is_file($this->file) or !preg_match('#\.set$#i', $this->file))
throw new Exception("SET not exists: ".basename($this->file)."");
if (!($f = fopen($this->file, 'rb')))
throw new Exception("Error opening ".basename($this->file)."");
if (!($header = fread($f, $this->header_len)))
throw new Exception("Error reading header");
$this->headers = new PHPSET_Header($header);
if (!($data = fread($f, $this->body_len)))
throw new Exception("Error reading data");
$this->fields = new PHPSET_Fields($data);
$this->etime = microtime(true);
$this->ptime = $this->etime - $this->stime;
if ($file) return $this;
}
}
class PHPSET_Data {
protected $data;
protected $value;
public function __set($name, $value) {
$class_name = 'PHPSET_'. $name;
if (class_exists($class_name))
$this->$name = new $class_name($value);
else if (is_string($value))
$this->$name = trim($value);
else
$this->$name = $value;
}
function __toString() {
if ($this->value) return $this->value;
return $this->data;
}
function translate($translations) {
$this->value = $translations[$this->data];
}
}
class PHPSET_Header extends PHPSET_Data {
function __construct($bin) {
$headers = unpack("A6SRSETT/x1/C1lfsInternalVersion/C1fileFormatVersion/x3", $bin);
foreach ((array)$headers as $header => $value) {
$this->$header = $value;
}
if ($this->SRSETT != 'SRSETT')
throw new Exception("File has not a valid SET format");
}
}
class PHPSET_Fields extends PHPSET_Data {
function __construct($bin) {
$fields = unpack("C1misc/C1unknown/C1handicapMassPosition/C1tyreBrand/f1brakeStrength/C1rearWingAngle/C1frontWingAngle/C1voluntaryHandicapMass/C1voluntaryIntakeRestriction/C1maxSteeringLock/C1parallelSteering/C1brakeBalance/C1engineBrakeReduction/C1centreDiffType/C1centreDiffViscousTorque/x1/C1centreDiffTorqueSplit/S1gearRatio7/S1gearRatioFinal/S1gearRatio1/S1gearRatio2/S1gearRatio3/S1gearRatio4/S1gearRatio5/S1gearRatio6/C1passenger/C1carConfig/C1tractionControlSlip/C1tractionControlEngageSpeed/frearTravel/frearSpring/frearComp/frearRebound/frearAR/x4/C1rearToe/C1rearCaster/C1rearTyreType/x1/C1LRcamberAdjust/C1RRcamberAdjust/x1/C1rearDiffClutchPack/C1rearDiffType/C1rearViscousTorque/C1rearPowerLocking/C1rearCoastLocking/S1LRtyrePressure/S1RRtyrePressure/ffrontTravel/ffrontSpring/ffrontComp/ffrontRebound/ffrontAR/x4/C1frontToe/C1frontCaster/C1frontTyreType/x1/C1LFcamberAdjust/C1RFcamberAdjust/x1/C1frontDiffClutchPack/C1frontDiffType/C1frontViscousTorque/C1frontPowerLocking/C1frontCoastLocking/S1LFtyrePressure/S1RFtyrePressure", $bin);
foreach ((array)$fields as $field => $value) {
$this->$field = $value;
}
}
}
class PHPSET_misc extends PHPSET_Data {
function __construct($data) {
$this->data = decbin($data);
}
}
class PHPSET_passenger extends PHPSET_Data {
function __construct($data) {
$this->data = decbin($data);
}
}
class PHPSET_tyreBrand extends PHPSET_Data {
function __construct($data) {
$this->data = $data;
$this->translate(array('Cromo Plain', 'Cromo', 'Torro', 'Michelin', 'Evostar'));
}
}
class PHPSET_centreDiffType extends PHPSET_Data {
function __construct($data) {
$this->data = $data;
$this->translate(array('Open', 'Viscous'));
}
}
class PHPSET_rearDiffType extends PHPSET_Data {
function __construct($data) {
$this->data = $data;
$this->translate(array('Open', 'Locked', 'Viscous', 'Clutch Pack'));
}
}
class PHPSET_frontDiffType extends PHPSET_rearDiffType {}
?>
<?php
php
require('phpset.php');
$set = new PHPSET('RB4_FE5r_test.set');
print_r($set);
?>
PHPSET Object
(
[file] => /home/salvarez/mch.lfsteam.es/forum/setups/store/RB4/FE5R/RB4_FE5r zarugor.set
[headers] => PHPSET_Header Object
(
[data:protected] =>
[value:protected] =>
[SRSETT] => SRSETT
[lfsInternalVersion] => 251
[fileFormatVersion] => 2
)
[fields] => PHPSET_Fields Object
(
[data:protected] =>
[value:protected] =>
[misc] => PHPSET_misc Object
(
[data:protected] => 10000100
[value:protected] =>
)
[unknown] => 0
[handicapMassPosition] => 50
[tyreBrand] => PHPSET_tyreBrand Object
(
[data:protected] => 3
[value:protected] => Michelin
)
[brakeStrength] => 920
[rearWingAngle] => 0
[frontWingAngle] => 0
[voluntaryHandicapMass] => 0
[voluntaryIntakeRestriction] => 0
[maxSteeringLock] => 30
[parallelSteering] => 0
[brakeBalance] => 72
[engineBrakeReduction] => 0
[centreDiffType] => PHPSET_centreDiffType Object
(
[data:protected] => 1
[value:protected] => Viscous
)
[centreDiffViscousTorque] => 20
[centreDiffTorqueSplit] => 39
[gearRatio7] => 0
[gearRatioFinal] => 32767
[gearRatio1] => 26214
[gearRatio2] => 16852
[gearRatio3] => 11047
[gearRatio4] => 7958
[gearRatio5] => 5243
[gearRatio6] => 3253
[passenger] => PHPSET_passenger Object
(
[data:protected] => 0
[value:protected] =>
)
[carConfig] => 0
[tractionControlSlip] => 20
[tractionControlEngageSpeed] => 5
[rearTravel] => 0.274268299341
[rearSpring] => 40000
[rearComp] => 4497.56103516
[rearRebound] => 5126.82910156
[rearAR] => 37000
[rearToe] => 6
[rearCaster] => 0
[rearTyreType] => 6
[LRcamberAdjust] => 20
[RRcamberAdjust] => 36
[rearDiffClutchPack] => 20
[rearDiffType] => PHPSET_rearDiffType Object
(
[data:protected] => 1
[value:protected] => Locked
)
[rearViscousTorque] => 20
[rearPowerLocking] => 80
[rearCoastLocking] => 80
[LRtyrePressure] => 162
[RRtyrePressure] => 165
[frontTravel] => 0.236869946122
[frontSpring] => 45000
[frontComp] => 4785.36572266
[frontRebound] => 6341.46337891
[frontAR] => 18000
[frontToe] => 10
[frontCaster] => 0
[frontTyreType] => 6
[LFcamberAdjust] => 29
[RFcamberAdjust] => 39
[frontDiffClutchPack] => 16
[frontDiffType] => PHPSET_frontDiffType Object
(
[data:protected] => 2
[value:protected] => Viscous
)
[frontViscousTorque] => 20
[frontPowerLocking] => 60
[frontCoastLocking] => 80
[LFtyrePressure] => 170
[RFtyrePressure] => 170
)
[header_len] => 12
[body_len] => 120
[stime] => 1266580405.69
[etime] => 1266580405.69
[ptime] => 0.000797033309937
)
byte Prefix; // Special host message prefix character
import sys
import os
import time
VERSION = '1.6.3'
try:
import pyinsim
if not pyinsim.version(VERSION):
raise ImportError
except ImportError:
print 'You must install pyinsim %s or better' % VERSION
sys.exit(1)
HEARTBEAT_INTERVAL = 10
PATH_CACHE = './cache.txt'
insim = pyinsim.InSim()
insim.message_cache = []
def heartbeat(insim):
save_cache(insim)
insim.timer(heartbeat, HEARTBEAT_INTERVAL)
def load_cache(insim):
if not os.path.isfile(PATH_CACHE):
open(PATH_CACHE, 'a').close()
p = open(PATH_CACHE, 'r')
lines = p.readlines()
for line in lines:
if len(line.strip()) > 0:
insim.message_cache.append(line.strip())
p.close()
def save_cache(insim):
lines = insim.message_cache[-100:]
p = open(PATH_CACHE, 'w')
for line in lines:
p.write("%s\n" % line.strip().replace("\r", "").replace("\n", ""))
p.close()
def message_out(insim, mso):
insim.message_cache.append(str(int(time.time())) +";"+ mso.Msg.strip())
if __name__ == '__main__':
try:
insim.bind(pyinsim.ISP_MSO, message_out)
insim.init('localhost', 29999, Admin='admin', IName='logexport', Flags=pyinsim.ISF_MSO_COLS, Interval=500)
insim.timer(load_cache, 1)
insim.timer(heartbeat, HEARTBEAT_INTERVAL)
except pyinsim.InSimError as err:
print 'InSim Error:', err
sys.exit(1)
except:
print 'Unexpected error:', sys.exc_info()[0]
sys.exit(1)
timestamp;message
<?php
$logexport_lines = file("cache.txt");
foreach ($logexport_lines as $logexport_line) {
list($timestamp, $message) = split(";", $logexport_line, 2);
// do something here
}
?>
<?php
require_once('phpmpr.php');
// parse the file
$o = new PHPMPR('some-mpr-file.mpr');
// see the content
var_dump($o);
?>
Traceback (most recent call last):
File "insim_buttons.py", line 40, in <module>
insim.send(pyinsim.ISP_ISI, Admin='Pass', IName='^3pyinsim', Flags=pyinsim.ISF_LOCAL)
File "/home/salvarez/_/_p/salvarez/pylfs/pyinsim_1.1.4/examples/pyinsim.py", line 1478, in send
self.sendB(Packet(packetType, **values).pack())
File "/home/salvarez/_/_p/salvarez/pylfs/pyinsim_1.1.4/examples/pyinsim.py", line 1748, in pack
return struct.pack(self.__packStr, [values.append(self[p[0]]) for p in _PACKET_DEFS[self['Type']]])
UnboundLocalError: local variable 'values' referenced before assignment
def pack(self):
"""Pack the current packet values into a binary string, which can then
be sent to InSim.
Returns:
A binary formatted string.
"""
[COLOR=Red] return struct.pack(self.__packStr, [values.append(self[p[0]]) for p in _PACKET_DEFS[self['Type']]]) [/COLOR]
[COLOR=SeaGreen]# values = []
# for packetDef in _PACKET_DEFS[self['Type']]:
# values.append(self[packetDef[0]])
# return struct.pack(self.__packStr, *values)[/COLOR]
$ cat deb.log
May 26 16:42:16 LFS DEDICATED HOST : 0.5Z13
May 26 16:42:16 ?
May 26 16:42:16 ?
May 26 16:42:16 ?
May 26 16:42:16 ?
May 26 16:42:16 ?
May 26 16:42:16 ?
May 26 16:42:16 ?
May 26 16:42:16 ?
May 26 16:42:16 ?
May 26 16:42:16 ?
May 26 16:42:16 ?
May 26 16:42:16 ?
May 26 16:42:16 load bans
May 26 16:42:16 load font
May 26 16:42:16 Could not get device context of LFS window
May 26 16:42:16 create english file
May 26 16:42:16 initialisations
May 26 16:42:16 tables
May 26 16:42:16 load objects
May 26 16:42:16 start intro
May 26 16:42:16 Kyoto Ring
May 26 16:42:16 end of initialisation
May 26 16:42:17 Track loaded
May 26 16:42:24 Send Track : 212.163.4.98
May 26 16:42:24 Send Track : 212.163.4.98
May 26 16:42:24 Connect : 212.163.4.98
May 26 16:42:24 A new guest is connecting
May 26 16:42:25 [MCH]BrEaThE^L connected (BriZz^L)
May 26 16:42:29 Host will restart in 3 seconds
May 26 16:42:32 TCP Socket : bind failed
May 26 16:42:32
May 26 16:42:32 Track loaded