summary refs log tree commit diff stats
path: root/Source/Memory.h
blob: cce06ba4c27701511e7ccf863b5dc4a8046867f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#pragma once
#include <functional>
#include <map>
#include <vector>
#include <windows.h>

// #define GLOBALS 0x5B28C0
#define GLOBALS 0x62D0A0

// https://github.com/erayarslan/WriteProcessMemory-Example
// http://stackoverflow.com/q/32798185
// http://stackoverflow.com/q/36018838
// http://stackoverflow.com/q/1387064
class Memory
{
public:
	Memory(const std::string& processName);
	~Memory();

	Memory(const Memory& memory) = delete;
	Memory& operator=(const Memory& other) = delete;

	int GetCurrentFrame();

	template <class T>
	std::vector<T> ReadArray(int panel, int offset, int size) {
		return ReadData<T>({GLOBALS, 0x18, panel*8, offset, 0}, size);
	}

	template <class T>
	void WriteArray(int panel, int offset, const std::vector<T>& data) {
		WriteData<T>({GLOBALS, 0x18, panel*8, offset, 0}, data);
	}

	template <class T>
	std::vector<T> ReadPanelData(int panel, int offset, size_t size) {
		return ReadData<T>({GLOBALS, 0x18, panel*8, offset}, size);
	}

	template <class T>
	void WritePanelData(int panel, int offset, const std::vector<T>& data) {
		WriteData<T>({GLOBALS, 0x18, panel*8, offset}, data);
	}

	void AddSigScan(const std::vector<byte>& scanBytes, const std::function<void(int index)>& scanFunc);
	int ExecuteSigScans();

	void ClearOffsets() {_computedAddresses = std::map<uintptr_t, uintptr_t>();}

private:
	template<class T>
	std::vector<T> ReadData(const std::vector<int>& offsets, size_t numItems) {
		std::vector<T> data;
		data.resize(numItems);
		for (int i=0; i<5; i++) {
			if (ReadProcessMemory(_handle, ComputeOffset(offsets), &data[0], sizeof(T) * numItems, nullptr))
			{
				return data;
			}
		}
		ThrowError();
		return {};
	}

	template <class T>
	void WriteData(const std::vector<int>& offsets, const std::vector<T>& data) {
		for (int i=0; i<5; i++) {
			if (WriteProcessMemory(_handle, ComputeOffset(offsets), &data[0], sizeof(T) * data.size(), nullptr)) {
				return;
			}
		}
		ThrowError();
	}

	void ThrowError();

	void* ComputeOffset(std::vector<int> offsets);

	std::map<uintptr_t, uintptr_t> _computedAddresses;
	uintptr_t _baseAddress = 0;
	HANDLE _handle = nullptr;
	struct SigScan {
		std::function<void(int)> scanFunc;
		bool found;
	};
	std::map<std::vector<byte>, SigScan> _sigScans;

	friend class Temp;
	friend class ChallengeRandomizer;
	friend class Randomizer;
};