I want to know how to read a struct within a struct via php’s unpack function. When I get an IS_MCI packet, I check it’s Type to make sure it’s equal to ISP_MCI, and then I check NumC to find out how many CompCar structs there are within this packet. The problem is trying to unpack these contents into an array via a single function. I always get a undefined offset. So, i’m looking for some fresh eyes on the matter.
How would you handle this packet?
The struct in question is this:
struct IS_MCI // Multi Car Info - if more than 8 in race then more than one of these is sent
{
byte Size; // 4 + NumC * 28
byte Type; // ISP_MCI
byte ReqI; // 0 unless this is a reply to an TINY_MCI request
byte NumC; // number of valid CompCar structs in this packet
CompCar Info[8]; // car info for each player, 1 to 8 of these (NumC)
};
struct CompCar // Car info in 28 bytes - there is an array of these in the MCI (below)
{
word Node; // current path node
word Lap; // current lap
byte PLID; // player's unique id
byte Position; // current race position : 0 = unknown, 1 = leader, etc...
byte Info; // flags and other info - see below
byte Sp3;
int X; // X map (65536 = 1 metre)
int Y; // Y map (65536 = 1 metre)
int Z; // Z alt (65536 = 1 metre)
word Speed; // speed (32768 = 100 m/s)
word Direction; // direction of car's motion : 0 = world y direction, 32768 = 180 deg
word Heading; // direction of forward axis : 0 = world y direction, 32768 = 180 deg
short AngVel; // signed, rate of change of heading : (16384 = 360 deg/s)
};
prints
Now, that code makes some assumptions that you might not want to take for granted (i.e. add a lot more error/read-data handling).
Sizemust be between 0…228, NumC must be between 0…8 and both values must fit together and so on.wordI’ve usedvwhich stands for “unsigned short (always 16 bit, little endian byte order). But forintI’ve usedl: “signed long (always 32 bit, machine byte order)”. That’s ok on my machine. But search the documentation of the protocol for the endianness of the data.The testdata in $msg has been taken from the result of