PDA

View Full Version : Read mpr data in php


ESPAŅA[XTM]
18th August 2007, 19:30
Hi

Iīm going to try do a php script which can read all the data from mpr.

WeatherWind, LapsPlayerFast Lap, End places, etc..

And I would get a bit help for this..

**Note, I know that would be more easy in C but must be in php..**

Thx

DarkTimes
18th August 2007, 20:03
Well you just need to open the file and read out the bytes, it's farily simple in both C and PHP. The way the data is stored is actually pleasingly straight-forward, you can see what each byte means on the LFS site: http://www.lfs.net/?page=MPR.

ESPAŅA[XTM]
18th August 2007, 20:06
Already I can open the replay file, with fopen, with I donīt know how do it by bites :S

DarkTimes
18th August 2007, 20:15
OK - It's been a long time since I did any PHP, and I never really worked with binary data much, but if I remember rightly you just need to read the data out as strings and then use the unpack() (http://www.php.net/unpack) function to convert the bytes from their binary representation into all the nice numbers and stuff that you need. If you look round the forum, or search on google, you'll find a few example of how to work it.

ESPAŅA[XTM]
18th August 2007, 20:29
Aja, ok thx, I will try and Iīll post again here with the results.

Thx u again

ESPAŅA[XTM]
19th August 2007, 14:14
Iīve tried unpack() but It donīt work, but very posible because me.. Could someone do a little example using unpack for read the mpr?

Dygear
20th August 2007, 00:37
A good example could be found in my signature, It contains code that uses the unpack function to read data from LFSWorld.


function get_hosts() {
$string = $this->make_query("&action=hosts");
for ($pointer = 0, $i = 0; $pointer <= strlen($string); $i++) {
$NumberOfRacers = @unpack("c", substr($string, $pointer + 52, 1));
$NumberOfRacers = $NumberOfRacers[1];
$NumberOfRacersLen = $NumberOfRacers * 24;
$PointerPast = $NumberOfRacersLen + 53;
$result[$i] = array();
if (($result[$i] = @unpack("a32hostname/c4tmlt/a4tcrm/icars/irules/claps/cqual/cspare1/cspare2/cnrofracers/a{$NumberOfRacersLen}racernames", substr($string, $pointer, $PointerPast)))) {
$result[$i]['racernames'] = preg_split("/\\0/", $result[$i]['racernames'], -1, PREG_SPLIT_NO_EMPTY);
$result[$i]['tmlt'] = unpack("ctype/cmain/cletter/ctestId", $result[$i]['tmlt']);
$result[$i]['tcrm'] = unpack("ctrack/cconfig/creversed/cmax", $result[$i]['tcrm']);
} else {
unset($result[$i]);
}
$pointer += $PointerPast;
}
return $result;
}