remotemono
RMonoAPIFunctionSimple_Def.h
1 /*
2  Copyright 2020 David "Alemarius Nexus" Lerch
3 
4  This file is part of RemoteMono.
5 
6  RemoteMono is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  RemoteMono is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with RemoteMono. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #pragma once
21 
22 #include "../config.h"
23 
24 #include <tuple>
25 #include <type_traits>
26 #include <BlackBone/Process/Process.h>
27 #include <BlackBone/Process/RPC/RemoteFunction.hpp>
28 
29 
30 
31 
32 namespace remotemono
33 {
34 
35 
36 
37 template <typename RetT, typename... ArgsT>
39 {
40 public:
41  typedef RetT RetType;
42  typedef std::tuple<ArgsT...> ArgsTuple;
43 
44  typedef RetT (*Func)(ArgsT...);
45  typedef blackbone::RemoteFunction<Func> RemoteFunc;
46 
47 public:
48  RMonoAPIFunctionSimple() : process(nullptr), addr(0), boundThread(nullptr), f(nullptr) {}
49  RMonoAPIFunctionSimple(blackbone::Process& proc, blackbone::ptr_t addr, blackbone::ThreadPtr boundThread = nullptr)
50  : process(&process), addr(addr), boundThread(boundThread), f(nullptr)
51  {
52  rebuildRemoteFunc();
53  }
55  {
56  delete f;
57  }
58 
59  void reset()
60  {
61  delete f;
62 
63  process = nullptr;
64  addr = 0;
65  boundThread = nullptr;
66  f = nullptr;
67  }
68 
69  void rebuild(blackbone::Process& proc, blackbone::ptr_t addr, blackbone::ThreadPtr boundThread = nullptr)
70  {
71  this->process = &proc;
72  this->addr = addr;
73  this->boundThread = boundThread;
74  rebuildRemoteFunc();
75  }
76 
77  blackbone::ptr_t getAddress() const { return addr; }
78 
79  operator bool() const { return f != nullptr; }
80 
81  RetT operator()(ArgsT... args)
82  {
83  assert(f);
84 
85  if constexpr(!std::is_same_v<RetT, void>) {
86  return *f->Call(typename RemoteFunc::CallArguments(args...), boundThread);
87  } else {
88  f->Call(typename RemoteFunc::CallArguments(args...), boundThread);
89  }
90  }
91 
92 private:
93  void rebuildRemoteFunc()
94  {
95  delete f;
96  f = new RemoteFunc(*process, addr);
97  }
98 
99 private:
100  blackbone::Process* process;
101  blackbone::ptr_t addr;
102  blackbone::ThreadPtr boundThread;
103  RemoteFunc* f;
104 };
105 
106 
107 
108 }
remotemono::RMonoAPIFunctionSimple
Definition: RMonoAPIFunctionSimple_Def.h:39