Skip to content
A PHP library for reading and unpacking BZFlag network packets
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

BZFlag Networking

Packagist License

A PHP 7.1+ library for reading and handling BZFlag network packets.

BZFlag Replay files are simply the raw packets stored in a file together, so this library will let you read replay files and unpack them into PHP classes that can be serialized into JSON.


The recommended use of this library is to "stream" the replays in your code with the getPacketsIterable() method, which reads and yields a packet at a time. This allows for you to iterate through a replay file once and not have to store the entire replay in memory as you're working with it.

use allejo\bzflag\networking\Replay;

$replay = new Replay(__DIR__ . '/my-bz-replay.rec');

foreach ($replay->getPacketsIterable() as $packet) {
    // do something with $packet

If you'd like to get all of a replay file's packets as an array, use the getPacketsAsArray() method. Keep in mind, that this will incur high-memory usage as the entire replay will be stored in an expanded format in memory. Additionally, all of the packets are read and if your code would like to perform any type of analysis, you will need to iterate through all of the packets again.

use allejo\bzflag\networking\Packets\GamePacket;

/** @var GamePacket[] $packets */
$packets = $replay->getPacketsAsArray();

Exporting to JSON

⚠️ High Memory Usage

The built-in json_encode function PHP will not be able to handle writing large JSON files from replays. At this point, it's necessary to stream your JSON using something like streaming-json-encoder. See the PHP port of rrlog for sample usage.

use allejo\bzflag\networking\Replay;

$replay = new Replay(__DIR__ . '/my-bz-replay.rec');

$json = json_encode($replay, JSON_PRETTY_PRINT);
file_put_contents(__DIR__ . '/my-bz-replay.rec.json', $json);

In Other Languages

This is the third iteration of this library. It's available in other languages:



You can’t perform that action at this time.