-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathil2cpp_auto.hpp
More file actions
78 lines (65 loc) · 2.41 KB
/
il2cpp_auto.hpp
File metadata and controls
78 lines (65 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#pragma once
#include <Windows.h>
#include <string>
#include <unordered_map>
#include <shared_mutex>
#include <mutex>
#include <atomic>
namespace IL2CPP_Auto {
class Resolver {
private:
inline static std::atomic<HMODULE> m_IL2CPPModule{ nullptr };
inline static std::unordered_map<std::string, uintptr_t> m_Cache;
inline static std::shared_mutex m_Mutex;
static HMODULE GetModule() {
HMODULE mod = m_IL2CPPModule.load(std::memory_order_acquire);
if (mod) return mod;
static std::mutex init_mutex;
std::lock_guard<std::mutex> lock(init_mutex);
mod = m_IL2CPPModule.load(std::memory_order_relaxed);
if (mod) return mod;
mod = GetModuleHandleA("GameAssembly.dll");
if (!mod) mod = GetModuleHandleA("UnityPlayer.dll");
if (mod) m_IL2CPPModule.store(mod, std::memory_order_release);
return mod;
}
static uintptr_t ResolveExport(const std::string& name) {
HMODULE mod = GetModule();
if (!mod) return 0;
return reinterpret_cast<uintptr_t>(GetProcAddress(mod, name.c_str()));
}
public:
static bool Initialize() {
return GetModule() != nullptr;
}
static uintptr_t GetAddress(const std::string& name) {
{
std::shared_lock<std::shared_mutex> lock(m_Mutex);
auto it = m_Cache.find(name);
if (it != m_Cache.end()) return it->second;
}
uintptr_t addr = ResolveExport(name);
if (!addr) return 0;
{
std::unique_lock<std::shared_mutex> lock(m_Mutex);
auto it = m_Cache.find(name);
if (it == m_Cache.end()) {
m_Cache[name] = addr;
}
else {
addr = it->second;
}
}
return addr;
}
template<typename T = void*>
static T GetFunction(const std::string& name) {
return reinterpret_cast<T>(GetAddress(name));
}
static void ClearCache() {
std::unique_lock<std::shared_mutex> lock(m_Mutex);
m_Cache.clear();
m_IL2CPPModule.store(nullptr, std::memory_order_release);
}
};
}