plugify 1.0.0.0
Loading...
Searching...
No Matches
call.hpp
1#pragma once
2
3#include <asmjit/asmjit.h>
4#include <plugify/mem_addr.hpp>
5#include <plugify/method.hpp>
6#include <string_view>
7#include <utility>
8#include <memory>
9#include <vector>
10
11namespace plugify {
21 class JitCall {
22 public:
27 explicit JitCall(std::weak_ptr<asmjit::JitRuntime> rt);
28
33 JitCall(const JitCall& other) = delete;
34
39 JitCall(JitCall&& other) noexcept;
40
45
50 struct Parameters {
51 typedef const uint64_t* Data;
52
57 explicit Parameters(size_t count) {
58 arguments.reserve(count);
59 }
60
67 template<typename T>
68 void AddArgument(T val) {
69 uint64_t& arg = arguments.emplace_back(0);
70 *(T*) &arg = val;
71 }
72
77 Data GetDataPtr() const noexcept {
78 return arguments.data();
79 }
80
81 private:
82 std::vector<uint64_t> arguments;
83 };
84
85 struct Return {
90 template<typename T, typename...Args>
91 void ConstructAt(Args&&... args) const noexcept {
92 std::construct_at((T*) GetReturnPtr(), std::forward<Args>(args)...);
93 }
94
101 template<typename T>
102 void SetReturn(T val) const noexcept {
103 *(T*) GetReturnPtr() = val;
104 }
105
111 template<typename T>
112 T GetReturn() const noexcept {
113 return *(T*) GetReturnPtr();
114 }
115
120 int8_t* GetReturnPtr() const noexcept {
121 return (int8_t*) &ret[0];
122 }
123
124 private:
125 volatile uint64_t ret[2]{};
126 };
127
128 enum class WaitType {
129 None,
130 Breakpoint,
131 Wait_Keypress
132 };
133
134 using CallingFunc = void(*)(Parameters::Data params, const Return*); // Return can be null
135 using HiddenParam = bool(*)(ValueType);
136
145 MemAddr GetJitFunc(const asmjit::FuncSignature& sig, MemAddr target, WaitType waitType, bool hidden);
146
155 MemAddr GetJitFunc(MethodHandle method, MemAddr target, WaitType waitType = WaitType::None, HiddenParam hidden = &ValueUtils::IsHiddenParam);
156
162 MemAddr GetFunction() const noexcept { return _function; }
163
170 MemAddr GetTargetFunc() const noexcept { return _targetFunc; }
171
176 std::string_view GetError() noexcept { return !_function && _errorCode ? _errorCode : ""; }
177
184 JitCall& operator=(const JitCall& other) = delete;
185
193 JitCall& operator=(JitCall&& other) noexcept;
194
195 private:
196 std::weak_ptr<asmjit::JitRuntime> _rt;
197 MemAddr _function;
198 union {
199 MemAddr _targetFunc;
200 const char* _errorCode{};
201 };
202 };
203} // namespace plugify
Class encapsulates architecture-, OS- and compiler-specific function call semantics in a virtual "bin...
Definition call.hpp:21
MemAddr GetJitFunc(MethodHandle method, MemAddr target, WaitType waitType=WaitType::None, HiddenParam hidden=&ValueUtils::IsHiddenParam)
Get a dynamically created function based on the method reference.
JitCall(const JitCall &other)=delete
Copy constructor.
JitCall(JitCall &&other) noexcept
Move constructor.
JitCall & operator=(JitCall &&other) noexcept
Move assignment operator for JitCall.
MemAddr GetTargetFunc() const noexcept
Get the target associated with the object.
Definition call.hpp:170
MemAddr GetFunction() const noexcept
Get a dynamically created function.
Definition call.hpp:162
MemAddr GetJitFunc(const asmjit::FuncSignature &sig, MemAddr target, WaitType waitType, bool hidden)
Get a dynamically created function based on the raw signature.
JitCall(std::weak_ptr< asmjit::JitRuntime > rt)
Constructor.
~JitCall()
Destructor.
JitCall & operator=(const JitCall &other)=delete
Copy assignment operator for JitCall.
std::string_view GetError() noexcept
Get the error message, if any.
Definition call.hpp:176
A wrapper class for memory addresses, providing utility functions for pointer manipulation.
Definition mem_addr.hpp:11
A handle class for the Method structure.
Definition method.hpp:105
constexpr bool IsHiddenParam(ValueType type) noexcept
Checks if a given ValueType is considered a hidden object parameter.
Structure to represent function parameters.
Definition call.hpp:50
Data GetDataPtr() const noexcept
Get a pointer to the argument storage.
Definition call.hpp:77
Parameters(size_t count)
Constructor.
Definition call.hpp:57
void AddArgument(T val)
Set the value of the argument at next available position.
Definition call.hpp:68
void SetReturn(T val) const noexcept
Set the return value.
Definition call.hpp:102
int8_t * GetReturnPtr() const noexcept
Get a pointer to the return value.
Definition call.hpp:120
T GetReturn() const noexcept
Get the value of the return.
Definition call.hpp:112
void ConstructAt(Args &&... args) const noexcept
Constructs an object of type T at the memory location.
Definition call.hpp:91