跳到正文
本頁目錄

Patch API

作用

Patch API 通過 SDK 讀取、寫入並回滾進程內存。

頭文件

C++
#include <pl/memory/Patch.hpp>

函數

C++
bool writeBytes(uintptr_t address, std::span<const uint8_t> bytes,
                std::string_view name);

bool writeBytes(uintptr_t address, std::string_view hexBytes,
                std::string_view name);

std::vector<uint8_t> readBytes(uintptr_t address, size_t length);

bool revertPatch(std::string_view name);
void revertAllPatches();

patch 名稱用於保存原始字節,之後由 revertPatch() 回滾。

示例

C++
#include <pl/memory/Patch.hpp>

bool installReturnZero(uintptr_t address) {
  return pl::memory::writeBytes(address, "00 00 80 D2 C0 03 5F D6",
                                "example.return_zero");
}

void removeReturnZero() {
  pl::memory::revertPatch("example.return_zero");
}

RAII 句柄

C++
class MyMod {
public:
  bool enable() {
    mPatch = pl::memory::PatchHandle(address, "00 00 80 D2 C0 03 5F D6",
                                     "example.return_zero");
    return mPatch.applied();
  }

  bool disable() {
    mPatch.reset();
    return true;
  }

private:
  uintptr_t address{};
  pl::memory::PatchHandle mPatch;
};

注意

  • 十六進制字符串用空白分隔字節,每個 token 必須是一位或兩位十六進制數。
  • 複用 patch 名稱會替換之前保存的記錄。
  • 錯誤指令或錯誤地址仍可能讓進程崩潰。