Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[c++] Case sorting and Variable name printing problem

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
ulysse31
Master Cheater
Reputation: 2

Joined: 19 Mar 2015
Posts: 324
Location: Paris

PostPosted: Tue Nov 10, 2015 11:38 am    Post subject: [c++] Case sorting and Variable name printing problem Reply with quote

Hi, I am coding a packet program for an online game.
After decryption, I analyze the decrypted packets and the program takes action accordingly, I do this with a switch..case.

Simply put, I injected a dll into the game that retrieved me all the packet names and their corresponding hexa number, they are defined as follow in my c++:

Quote:
const int S_PLAYER_CHANGE_PROF = 0xbb32;
const int C_CREST_APPLY_LIST = 0xbb79;
const int C_ADMIN_LOBBY = 0xbbce;
const int S_MOUNT_VEHICLE = 0xbbf1;


The way i filter packets that matter is this one :

Quote:
void ProcessUndecryptedPacketData(boost::asio::ip::tcp::socket &socket, uint16_t& size, uint16_t& title, std::vector <unsigned char> &vectArgument, int &length)
{
switch (title)
{
case S_PLAYER_CHANGE_PROF:
//do something..
break;
case S_MOUNT_VEHICLE
//Do somth
break;
}
}


There are over 2000 packets, a switch case code cannot go over 128 possible cases or else i get a compiler limit error.
Each time a packet is processed, I log its entirety with its hexa number.


First problem :
I know there is a way better way to do this, can someone point me in the right direction ? (switch case like i did is simply bad)


I need to log the title of the packet ie S_MOUNT_VEHICLE and not only its hexa value, I came across a macro code but it prints the variable name and here the variable name is title (because switch(title)) and what i want is to print the name of the variable to which the value of title equals the hexas..I need to log the S_MOUNT_VEHICLE string and i don't see how to do this, I've thought of a function returning a string but then i'd have too many nested cases in the function.

Any help would be greatly appreciated
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Tue Nov 10, 2015 2:23 pm    Post subject: This post has 1 review(s) Reply with quote

Use a functional lookup table for the packet types. Something on the line of:
Code:

bool packet_0x1234_handler()
{
    return true;
}

std::map<int, bool (*)() const> packetHandlers =
{
    { 0x1234, packet_0x1234_handler },
    { 0x1235, packet_0x1235_handler }
};


Adjust the handler function as needed for any arguments you may want etc.

You could also use std::function if need be.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
ulysse31
Master Cheater
Reputation: 2

Joined: 19 Mar 2015
Posts: 324
Location: Paris

PostPosted: Wed Nov 11, 2015 3:35 pm    Post subject: Reply with quote

Thanks, I haven't had the chance to come across those c ++ tools till now, it looks pretty good.

edit : Made it work, turns out to be just perfect


Last edited by ulysse31 on Wed Nov 11, 2015 7:33 pm; edited 1 time in total
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Wed Nov 11, 2015 6:40 pm    Post subject: Reply with quote

Shrooms wrote:
oh "smartone", difference in speed between map and other methods?


Performance depends on use. If you need the utmost speed out of an STL container, use unordered_map. But keep in mind being an STL container it will come at a cost with overhead and specific setups pertaining to how the STL feels things should be handled.

In most cases its best to roll your own container to get the maximum performance out of things, but if you are just looking for a quick and dirty pre-made solution, map and unordered_map from the STL are fine.

If you want to see some testings, there are various blogs that cover it:
http://www.joshbarczak.com/blog/?p=525
http://www.codeproject.com/Articles/866996/Fast-Implementations-of-Maps-with-Integer-Keys-in
http://scottmeyers.blogspot.com/2015/09/should-you-be-using-something-instead.html


Keep in mind, results will vary based on usage, data size being stored etc. as well as your system specs.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites