name: "Room" panels { name: "HORN" path: "Panels/entry_1" clue: "horn" answer: "thorn" symbols: SPARKLES } panels { name: "ION" path: "Panels/entry_2" clue: "ion" answer: "iron" symbols: SPARKLES } panels { name: "TON" path: "Panels/entry_3" clue: "ton" answer: "torn" symbols: SPARKLES } panels { name: "ROT" path: "Panels/entry_4" clue: "rot" answer: "riot" symbols: SPARKLES } panels { name: "IT" path: "Panels/entry_5" clue: "it" answer: "hit" symbols: SPARKLES } panels { name: "HIT" path: "Panels/entry_6" clue: "hit" answer: "hint" symbols: SPARKLES } panels { name: "INTO" path: "Panels/entry_7" clue: "into" answer: "intro" symbols: SPARKLES } panels { name: "NOR" path: "Panels/entry_8" clue: "nor" answer: "north" symbols: SPARKLES } ports { name: "GREAT" display_name: "Entrance" path: "Components/Warps/worldport" destination { x: 0 y: 0 z: 7.5 } rotation: 0 } /option> Mod for The Witness that makes every puzzle into a straight line
about summary refs log blame commit diff stats
path: root/WitnessRandomizer/Memory.cpp
blob: 0afededb41dc14462975f3633240f266c81b889f (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16















                                                                                              


                              
                       
                                                                                                     

                                   
















                                                                                                             
                                                                                    







                                   
                           
                                       
                                                                                                                        
                                                  


                           
                                                     
 


                                                                                                           
 
                                                   
                                          
                                            
 
                                                                               


                                                                               
                                                                                                                                                  


                                                                                
                 

                                                                          
         
                                                                         
 
#include "Memory.h"
#include <psapi.h>
#include <tlhelp32.h>
#include <iostream>

#undef PROCESSENTRY32
#undef Process32Next

Memory::Memory(const std::string& processName) {
	// First, get the handle of the process
	PROCESSENTRY32 entry;
	entry.dwSize = sizeof(entry);
	HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	while (Process32Next(snapshot, &entry)) {
		if (processName == entry.szExeFile) {
			_handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);
			break;
		}
	}
	if (!_handle) {
		std::cout << "Couldn't find " << processName.c_str() << ", is it open?" << std::endl;
		exit(EXIT_FAILURE);
	}

	// Next, get the process base address
	DWORD numModules;
	std::vector<HMODULE> moduleList(1024);
	EnumProcessModulesEx(_handle, &moduleList[0], static_cast<DWORD>(moduleList.size()), &numModules, 3);

	std::string name(64, 0);
	for (DWORD i = 0; i < numModules / sizeof(HMODULE); i++) {
		GetModuleBaseNameA(_handle, moduleList[i], &name[0], sizeof(name));

		// TODO: Filling with 0s still yeilds name.size() == 64...
		if (strcmp(processName.c_str(), name.c_str()) == 0) {
			_baseAddress = (uintptr_t)moduleList[i];
			break;
		}
	}
	if (_baseAddress == 0) {
		std::cout << "Couldn't find the base process address!" << std::endl;
		exit(EXIT_FAILURE);
	}
}

Memory::~Memory() {
	CloseHandle(_handle);
}

void Memory::ThrowError() {
	std::string message(256, '\0');
	FormatMessageA(4096, nullptr, GetLastError(), 1024, &message[0], static_cast<DWORD>(message.length()), nullptr);
	std::cout << message.c_str() << std::endl;
	exit(EXIT_FAILURE);
}

void* Memory::ComputeOffset(std::vector<int> offsets)
{
	// Leave off the last offset, since it will be either read/write, and may not be of type unitptr_t.
	int final_offset = offsets.back();
	offsets.pop_back();

	uintptr_t cumulativeAddress = _baseAddress;
	for (const int offset : offsets) {
		cumulativeAddress += offset;

		const auto search = _computedAddresses.find(cumulativeAddress);
		if (search == std::end(_computedAddresses)) {
			// If the address is not yet computed, then compute it.
			uintptr_t computedAddress = 0;
			if (!ReadProcessMemory(_handle, reinterpret_cast<LPVOID>(cumulativeAddress), &computedAddress, sizeof(uintptr_t), NULL)) {
				ThrowError();
			}
			_computedAddresses[cumulativeAddress] = computedAddress;
		}

		cumulativeAddress = _computedAddresses[cumulativeAddress];
	}
	return reinterpret_cast<void*>(cumulativeAddress + final_offset);
}