Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rf scratch #3

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 0 additions & 161 deletions Src/2DOBB.hpp

This file was deleted.

22 changes: 15 additions & 7 deletions Src/GameConnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@
#include <memory>
#include <vector>
#include "Object.hpp"

enum class PlayerState:int
{
NOTCREATED=0,
NORMAL=1,
INVINCIBLE =2,
DYING=4
};
struct GameConnection {
virtual ~GameConnection() noexcept = default;
virtual void GetPowers(std::vector<Object> &powers) noexcept = 0;
virtual void GetEnemyData(std::vector<Object> &enemy) noexcept = 0;
virtual void GetEnemyBulletData(std::vector<Object> &bullet, const Player &player, double maxRange) noexcept = 0;
virtual void GetPlayerData(Player &self) noexcept = 0;
virtual void GetEnemyLaserData(std::vector<Laser> &laser) noexcept = 0;
virtual int getTimeline() noexcept = 0;
virtual void getPowers(std::vector<Object> &powers) noexcept = 0;
virtual void getEnemyData(std::vector<Object> &enemy) noexcept = 0;
virtual void getEnemyBulletData(std::vector<Object> &bullet, const Player &player, double maxRange) noexcept = 0;
virtual void getPlayerData(Player &self) noexcept = 0;
virtual void getEnemyLaserData(std::vector<Laser> &laser) noexcept = 0;
virtual void sendKeyInfo(int dir,bool shift,bool z,bool x) noexcept = 0;
virtual void getMousePosition(Vec2d& pos)noexcept = 0;
virtual PlayerState getPlayerStateInformation()noexcept = 0;
};

std::unique_ptr<GameConnection> createGameConnection();
96 changes: 60 additions & 36 deletions Src/GameConnectionTH10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,42 @@
#include "KeyboardManager.hpp"
#include "Windows.hpp"
#include <TlHelp32.h>

class GameConnectionTH10 : public GameConnection {
public:
GameConnectionTH10();
~GameConnectionTH10() noexcept override;
void GetPowers(std::vector<Object> &powers) noexcept override;
void GetEnemyData(std::vector<Object> &enemy) noexcept override;
void GetEnemyBulletData(std::vector<Object> &bullet, const Player &player, double maxRange) noexcept override;
void GetPlayerData(Player &self) noexcept override;
void GetEnemyLaserData(std::vector<Laser> &laser) noexcept override;
void sendKeyInfo(int dir, bool shift, bool z, bool x) noexcept override;
private:
HANDLE mHProcess{nullptr};
static bool GetProcessIdByName(const char *exeFileName, DWORD &pid) noexcept;
void readProcessRaw(intptr_t offset, size_t length, void *target) const noexcept {
static thread_local SIZE_T nbr;
ReadProcessMemory(mHProcess, reinterpret_cast<LPCVOID>(offset), target, length, &nbr);
}
template<class T>
T readProcess(intptr_t offset) const noexcept {
T result;
readProcessRaw(offset, sizeof(T), &result);
return result;
}
};
#include <stdexcept>

namespace {
char staticBuffer[0x7F0 * 2001];
template<class T>
T read(void *buffer) noexcept { return *reinterpret_cast<T *>(buffer); }
}

void GameConnectionTH10::GetPowers(std::vector<Object> &powers) noexcept {
class GameConnectionTH10 : public GameConnection {
public:
GameConnectionTH10();
~GameConnectionTH10() noexcept override;
void getPowers(std::vector<Object>& powers) noexcept override;
void getEnemyData(std::vector<Object>& enemy) noexcept override;
void getEnemyBulletData(std::vector<Object>& bullet, const Player& player, double maxRange) noexcept override;
void getPlayerData(Player& self) noexcept override;
void getEnemyLaserData(std::vector<Laser>& laser) noexcept override;
void sendKeyInfo(int dir, bool shift, bool z, bool x) noexcept override;
void getMousePosition(Vec2d& pos)noexcept override;
int getTimeline() noexcept override;
PlayerState getPlayerStateInformation()noexcept override;
private:
HWND mWindow{ nullptr };
HANDLE mHProcess{ nullptr };
static bool GetProcessIdByName(const char* exeFileName, DWORD& pid) noexcept;
void readProcessRaw(intptr_t offset, size_t length, void* target) const noexcept {
static thread_local SIZE_T nbr;
ReadProcessMemory(mHProcess, reinterpret_cast<LPCVOID>(offset), target, length, &nbr);
}
template<class T>
T readProcess(intptr_t offset) const noexcept {
T result;
readProcessRaw(offset, sizeof(T), &result);
return result;
}
};
void GameConnectionTH10::getPowers(std::vector<Object> &powers) noexcept {
powers.clear();
auto ebp = staticBuffer;
const auto base = readProcess<int32_t>(0x00477818);
Expand All @@ -50,7 +53,7 @@ void GameConnectionTH10::GetPowers(std::vector<Object> &powers) noexcept {
}
}

void GameConnectionTH10::GetEnemyData(std::vector<Object> &enemy) noexcept {
void GameConnectionTH10::getEnemyData(std::vector<Object> &enemy) noexcept {
enemy.clear();
const auto base = readProcess<int32_t>(0x00477704);
if (!base) return;
Expand All @@ -70,9 +73,17 @@ void GameConnectionTH10::GetEnemyData(std::vector<Object> &enemy) noexcept {
}
}
}

void GameConnectionTH10::getMousePosition(Vec2d& pos)noexcept{
const Vec2d orgdelta = Vec2d(28, 45);
POINT m;
GetCursorPos(&m);
RECT rect;
GetWindowRect(mWindow, &rect);
Vec2d org = Vec2d(rect.left + orgdelta.x + 200.0, rect.top + orgdelta.y);
pos = Vec2d(m.x - org.x, m.y - org.y);
}
void
GameConnectionTH10::GetEnemyBulletData(std::vector<Object> &bullet, const Player &player, double maxRange) noexcept {
GameConnectionTH10::getEnemyBulletData(std::vector<Object> &bullet, const Player &player, double maxRange) noexcept {
bullet.clear();
auto ebx = staticBuffer;
const auto base = readProcess<int32_t>(0x004776F0);
Expand All @@ -89,22 +100,23 @@ GameConnectionTH10::GetEnemyBulletData(std::vector<Object> &bullet, const Player
const auto x = read<float>(ebx + 0x3B4), y = read<float>(ebx + 0x3B8),
w = read<float>(ebx + 0x3F0), h = read<float>(ebx + 0x3F4),
dx = read<float>(ebx + 0x3C0), dy = read<float>(ebx + 0x3C4);
//为了效率,只考虑可能会碰到的子弹
if (distanceSqr(Vec2d(x, y), player.pos) <= maxRange * maxRange)
bullet.emplace_back(x, y, w, h, dx / 2.0f, dy / 2.0f);
bullet.emplace_back(x, y, w, h, dx, dy);
}
ebx += 0x7F0;
}
}

void GameConnectionTH10::GetPlayerData(Player &self) noexcept {
void GameConnectionTH10::getPlayerData(Player &self) noexcept {
const auto obj_base = readProcess<int32_t>(0x00477834);
if (!obj_base) return;
self.pos.x = readProcess<float>(obj_base + 0x3C0);
self.pos.y = readProcess<float>(obj_base + 0x3C4);
self.size.x = 2.0;
self.size.y = 2.0;
}

void GameConnectionTH10::GetEnemyLaserData(std::vector<Laser> &laser) noexcept {
void GameConnectionTH10::getEnemyLaserData(std::vector<Laser> &laser) noexcept {
laser.clear();
const auto base = readProcess<int32_t>(0x0047781C);
if (!base) return;
Expand All @@ -115,13 +127,22 @@ void GameConnectionTH10::GetEnemyLaserData(std::vector<Laser> &laser) noexcept {
const auto x = readProcess<float>(esi + 0x24), y = readProcess<float>(esi + 0x28),
w = readProcess<float>(esi + 0x44), h = readProcess<float>(esi + 0x40),
arc = readProcess<float>(esi + 0x3C);
laser.emplace_back(x, y, w / 2.0f, h, arc);
laser.emplace_back(x, y, w, h, arc);
if (!ebx) break;
esi = ebx;
}
}
}

PlayerState GameConnectionTH10::getPlayerStateInformation()noexcept {
int base_addr = readProcess<int32_t>(0x00477834);
if (!base_addr){
return (PlayerState)0;
}
return (PlayerState)readProcess<int32_t>(base_addr + 0x458);
}
int GameConnectionTH10::getTimeline() noexcept {
return readProcess<int32_t>(0x00474C88);
}
void GameConnectionTH10::sendKeyInfo(int32_t dir, bool shift, bool z, bool x) noexcept {
KeyboardManager::sendKeyInfo(dir, shift, z, x);
}
Expand All @@ -138,6 +159,9 @@ GameConnectionTH10::GameConnectionTH10() {
mHProcess = OpenProcess(PROCESS_VM_READ, true, pid);
if (!mHProcess)
throw std::runtime_error("cannot open th10 process!");
mWindow = FindWindow(0, "�������_�^�@�` Mountain of Faith. ver 1.00a");
if (!mWindow)
throw std::runtime_error("cannot open th10 window!");
}

bool GameConnectionTH10::GetProcessIdByName(const char *exeFileName, DWORD &pid) noexcept {
Expand Down
Loading