toddw123 / RotMG_Clientless

Compatible with version X16.0.0
MIT License
11 stars 4 forks source link

Read packets from resources instead of hard code #41

Closed Zeroeh closed 7 years ago

Zeroeh commented 7 years ago

Deca updated packet ids again. Maybe it would be a good idea to read packets externally through an xml file :) After I update my own packets I'll work on a pull request.

toddw123 commented 7 years ago

Packets are already updated in the branches. Updated them as soon as the update was out.

toddw123 commented 7 years ago

And i wanted to use an xml for the packet id's for the longest time now, the problem with C++ is that enum's (which is what the packet types are right now) are defined at compile time. So there is no way to set the value of an enum after compiling.

I havent quite figured out how best to handle defining packet id's at run-time yet.

edit: i have been going over the idea the past few weeks, and today sparked me to try and figure this out again since there was an update, but i would have to have a global unordered_map<PacketType, int> and then i would be able to link the enum value to an int at run-time. Still not too sure yet, guess ill play around with some code.

Zeroeh commented 7 years ago

Mm, looks like krelay just makes a few dictionaries. (unordered maps) http://prntscr.com/ekvtxk

toddw123 commented 7 years ago

Yup. That is the basic idea outside of using an enum. The problem is comparing a string value (from the xml) to an enum. Otherwise i have to create a giant if/else statement going "if(stringvalue == "FAILURE") return PacketType::FAILURE;" etc etc etc.

But i think i almost have something almost similar working. Still trying to get the logic correct on it though.

toddw123 commented 7 years ago

cool i got it working. Im surprised it worked first try, thats extremely rare for me lol

anyways the way i did it is pretty different from k-relay. I had to change the macros i was using for creating the PacketType enums. You can see how the PacketTypes are defined in the file "packets/PacketTypes.h", and then you can see the macros that are being used in "utilites/EnumToString.h". I had to change the macros a bit, but the nice thing is i already had it so the enums got created as both an enum value and a string value. So i created an unordered_map<string, int> and filled that will the values from packets.xml. Then all the packet classes i had to change the packet->id to packet->type and had that variable be a PacketType. In the sendPacket function i simply call my function that gets the string version of the PacketType enum and then i can get the actual id value from the map. Working the other way was a bit tricky though. Having to convert an int into a string, and then a string into a PacketType was a bit weird, but luckily the idea i had worked.

So yeah, havent pushed this to the master branch yet (its pushed to the "packet-handler-test" branch). Im most-likely going to be merging the "packet-handler-test" branch with the "master" branch soon. I like the change from an if/else statement into a function handler. As well as a few other things i added to the packet-handler-test branch. So look/pull from that branch if you want to use the updated code. Ill probably do the merge later tonight after i verify that theres nothing i left out from the original master branch.