Need help on Reversing Packets


Hello all ,



I reversing game packets , Everything is good, but how do I save these values in the array or Vector [ValueFromRecv,id,x,y]
Note that it is not possible to know the size of the array ,
Because when the player hits the monster in the game, a lot of items are on the ground and randomly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
			constexpr auto ValueFromRecvOffset = 0x8;
			auto lpReadAddress = reinterpret_cast<LPVOID>((DWORD)lpPacket + ValueFromRecvOffset);
			auto ValueFromRecv = memory.Read<uint32_t>(lpReadAddress);

			constexpr auto IDOffset = 0xC;
			auto lpReadAddress = reinterpret_cast<LPVOID>((DWORD)lpPacket + IDOffset);


			auto ID = memory.Read<uint32_t>(lpReadAddress);
			constexpr auto XOffset = 0x10;
			auto lpReadAddress = reinterpret_cast<LPVOID>((DWORD)lpPacket + XOffset);
			auto X = memory.Read<uint16_t>(lpReadAddress);

			constexpr auto YOffset = 0x12;
			auto lpReadAddress = reinterpret_cast<LPVOID>((DWORD)lpPacket + YOffset);
			auto Y = memory.Read<uint16_t>(lpReadAddress);

Last edited on
You could use a struct for the values (ValueFromRecv, id, x, y) with each element of the appropriate type. Then have a std::vector of the struct. To add items to the vector, you just insert at the end. Unlike a std::array, the size of a std::vector can change at run-time when you insert/erase elements. Consider for a simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <vector>
#include <iostream>

struct Data {
	int ValueFromRecv {};
	int id {};
	int x {};
	int y {};
};

int main() {
	std::vector<Data> packets;

	packets.emplace_back(1, 2, 3, 4);
	packets.emplace_back(3, 4, 5, 6);

	for (const auto& [vfr, i, x, y] : packets)
		std::cout << vfr << "  " << i << "  " << x << "  " << y << '\n';
}


which displays:


1  2  3  4
3  4  5  6

Thanks so much seeplus ,

error :
1
2
Error	C2661	'Data::Data': no overloaded function takes 4 arguments	ConsoleApplication1	C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory	681	
		
Last edited on
I think
 
packets.emplace_back(1, 2, 3, 4);
requires C++20 or later.

Instead you could use
 
packets.push_back({1, 2, 3, 4});
or
 
packets.push_back(Data{1, 2, 3, 4});
or
1
2
Data data{1, 2, 3, 4};
packets.push_back(data);
or
1
2
3
4
5
6
Data data;
data.ValueFromRecv = 1;
data.id = 2;
data.x = 3;
data.y = 4;
packets.push_back(data);

(There are many ways to do this but they all accomplish the same thing)
Last edited on
Prior to C++20 you need an explicit struct constructor. This will work for C++14:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <vector>
#include <iostream>

struct Data {
	int ValueFromRecv {};
	int id {};
	int x {};
	int y {};

	Data(int v, int i, int x_, int y_) : ValueFromRecv(v), id(i), x(x_), y(y_) {}
};

int main() {
	std::vector<Data> packets;

	packets.emplace_back(1, 2, 3, 4);
	packets.emplace_back(3, 4, 5, 6);

	for (const auto& pack : packets)
		std::cout << pack.ValueFromRecv << "  " << pack.id << "  " << pack.x << "  " << pack.y << '\n';
}


What os/compiler are you using? For some compilers you need to explicitly specify the C++ version to use (eg MS VS defaults to C++14 unless changed). C++20 is the current standard with C++23 due later this year. If your current C++ complier doesn't support C++20 then I'd advise you really consider updating it.

Last edited on
@Hawlong,

VS2019 and VS2022 default the C++ language standard to C++14. C++20 should be your minimum default standard unless you have specific reasons not to.

C++23 is now the current approved standard, but no compilers are 100% compliant yet. VS can be set for the language standard to c++:latest that include what C++23 features have be added.

VS2019 is at "end of life" support now that VS2022 is available, newer language support isn't likely to happen. VS2019 has issues with some C++20 features. Consider upgrading to VS2022 if you can.
thanks so much George P, Peter87 , seeplus <3
the problem has been solved and I have more than one solution <3
Where possible use .emplace() as this mitigates the requirement to perform an object copy/move. See:

https://en.cppreference.com/w/cpp/container/vector/emplace_back

If you have to use .insert() then try to also use move semantics to avoid an unnecessary copy. See:

https://en.cppreference.com/w/cpp/utility/move
Last edited on
Topic archived. No new replies allowed.