neptools

Modding tools to Neptunia games
git clone https://git.neptards.moe/neptards/neptools.git
Log | Files | Refs | Submodules | README | LICENSE

hook.hpp (1159B)


      1 #ifndef UUID_310CE7F9_73D3_4140_BA43_9DA7CB4136D1
      2 #define UUID_310CE7F9_73D3_4140_BA43_9DA7CB4136D1
      3 #pragma once
      4 
      5 #include <cstddef>
      6 #include "../pattern.hpp"
      7 
      8 namespace Neptools
      9 {
     10 
     11   extern Byte* image_base;
     12   size_t GetImageSize() noexcept;
     13   Byte* GetEntryPoint() noexcept;
     14 
     15   inline Byte* MaybeFindImage(const Pattern& pat) noexcept
     16   { return const_cast<Byte*>(pat.MaybeFind({image_base, GetImageSize()})); }
     17   inline Byte* FindImage(const Pattern& pat)
     18   { return const_cast<Byte*>(pat.Find({image_base, GetImageSize()})); }
     19 
     20   void* Hook(void* hook, void* dst, size_t copy);
     21 
     22   template <typename T>
     23   inline T Hook(void* hook, T dst, size_t copy)
     24   {
     25     static_assert(sizeof(T) == 4);
     26     union
     27     {
     28       T t;
     29       void* vd;
     30     } u;
     31     u.t = dst;
     32     u.vd = Hook(hook, u.vd, copy);
     33     return u.t;
     34   }
     35 
     36   template <typename T>
     37   T& As(void* ptr) { return *static_cast<T*>(ptr); }
     38 
     39   class Unprotect
     40   {
     41   public:
     42     Unprotect(void* ptr, size_t len);
     43     ~Unprotect();
     44     Unprotect(const Unprotect&) = delete;
     45     void operator=(const Unprotect&) = delete;
     46 
     47   private:
     48     void* ptr;
     49     size_t len;
     50     unsigned long orig_prot;
     51   };
     52 
     53 }
     54 #endif