Create a parser
To create a parser, you must implement the IParser<T> interface.
We will be using the WallItem
model we created from create a model.
Implement the IParser interface
Add the IParser<T>
interface and its Parse
method implementation to your model.
In Parse
you have access to the PacketReader
that allows you to read primitive types from the
packet. This is where you read each value, apply them to an object and return the result.
public class WallItem : IParser<WallItem>
{
// Define properties here...
public static WallItem Parse(in PacketReader p)
{
// Create a new WallItem.
WallItem item = new();
// We read a string and cast it to an Id so it is in the correct type for our model.
item.Id = (Id)p.ReadString();
// Read the rest of the fields from the PacketReader and set them on the new WallItem.
item.Kind = p.ReadInt();
item.Location = p.ReadString();
item.Data = p.ReadString();
item.SecondsUntilExpiration = p.ReadInt();
item.UsagePolicy = p.ReadInt();
item.OwnerId = p.ReadId();
item.OwnerName = p.ReadString();
// Return the parsed item.
return item;
}
}
In action
Now that we have created our own parser, we are able to read it from a packet.
Let's intercept the ItemAdd
packet and print to the console whenever someone places a wall item:
ext.Intercept(In.ItemAdd, e => {
var item = e.Packet.Read<WallItem>();
Console.WriteLine($"{item.OwnerName} placed a wall item with ID {item.Id} of kind {item.Kind} at {item.Location}");
});
When you run the extension and place some wall items, you should see the information printed to the console:
❯ dotnet run
(name) placed a wall item with ID 2147418119 of kind 4395 at :w=6,1 l=29,54 r
(name) placed a wall item with ID 2147418120 of kind 4395 at :w=8,1 l=3,60 r
(name) placed a wall item with ID 2147418121 of kind 4395 at :w=7,1 l=26,53 r
(name) placed a wall item with ID 2147418122 of kind 4395 at :w=9,1 l=2,33 r
ADDITIONAL INFO
If you removed the IParser<T>
interface from your class and attempted to compile the
above example, the xabbo analyzer would emit an error because it cannot be read from a packet:
❯ dotnet build
MSBuild version 17.8.5+b5265ef37 for .NET
Determining projects to restore...
All projects are up-to-date for restore.
/tmp/parser/Program.cs(15,30): error XABBO011: 'Examples.Parser.WallItem' is not a packet primitive or IParser<T> implementation [/tmp/parser/Examples.Parser.csproj]
Build FAILED.
/tmp/parser/Program.cs(15,30): error XABBO011: 'Examples.Parser.WallItem' is not a packet primitive or IParser<T> implementation [/tmp/parser/Examples.Parser.csproj]
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:00.99