-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix packet code #68
Comments
First of all I do not like the naming of your version specific sub classes. Second I would let those sub classes implement an common interface for example: This then leads to third: I would create a lookup dictionary in very packet base class that are ordered by the protocol version (key) and map to the versioned sub class' read delegate (the static method) (value). The Read method of the base packet class then gets the matching read delegate by "asking" with the current protocol version. The dict then returns the corresponding read delegate for the sub class. Finally all of these boilder plate stuff like adding the interfaces and creating the dict and registering all the sub clases can be done with a source generator. The ones that run directly with your dotnet build in one go. I would like to implement all this for a single packet to better demonstrate this with an example. But I do not know a packet that has more than 2 versions. If you know one please tell me. |
The packet
|
Oh I am sorry for falsely closing this issue (because of the commit message). Before you edited it it would have been resolved by the PR. |
I implemented the pattern I thought of in a branch of my forked repo. I did it for What do you think? |
Seems good to me, the version with the SourceGenerator is not too different to my approach. |
No. It is not even an improvement it is actually slightly slower. But with a dictionary you can query all versions at runtime. With a switch you can not. |
okay, lets try it out then |
Ok. I will start implementing the source generator tomorrow then |
The code of packets is super messy and weird. Lets standardize how packets should be structured, and handle differences in minecraft versions.
My suggestion is the following:
Have a base class for every packet in the
PacketType
enum implementing theIPacket
interface.If packets use enum's or have nested fields, create the new type nested within the packet class.
For nested classes: always implement the
ISerializable<T>
interface.If the packet has differences between versions:
In this case, the base class should be abstract.
The
PacketType
field is implemented as usual, since it should never change between versions.The
Write
method is declared as abstract.Create a new nested class for each version where the packet changed, deriving from the base class.
Every version specific packet class implements its required members, and the correct
Read
andWrite
methods.The base class then implements the read method, by reading the correct subclass for the current version.
Example:
Posted by @Klotzi111 in #71 (comment) :
Why do you want register all subclasses in a lookup table? The reading logic can deal with reading the right packet, and for writing, inheritance does the job.
The text was updated successfully, but these errors were encountered: