forked from lzup345/ManaLock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathefmod.cpp
More file actions
134 lines (113 loc) · 4.6 KB
/
efmod.cpp
File metadata and controls
134 lines (113 loc) · 4.6 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// Created by lzup on 2025/7/11.
//
#include "efmod_core.hpp"
#include "TEFMod.hpp"
#include "BaseType.hpp"
#include "Logger.hpp"
// 全局组件
TEFMod::Logger* g_log;
TEFMod::TEFModAPI* g_api;
// 魔力字段
struct ManaFields {
TEFMod::Field<int>* statMana; // 当前魔力值
TEFMod::Field<int>* statManaMax; // 最大魔力值
} g_mana;
// 原始函数声明
void (*original_ResetEffects)(TEFMod::TerrariaInstance);
// Hook转发函数
void ResetEffects_T(TEFMod::TerrariaInstance i);
// Hook模板定义
inline TEFMod::HookTemplate HookTemplate_ResetEffects {
reinterpret_cast<void*>(ResetEffects_T),
{}
};
// 转发函数实现
void ResetEffects_T(TEFMod::TerrariaInstance i) {
original_ResetEffects(i);
for (const auto fun : HookTemplate_ResetEffects.FunctionArray) {
if (fun) reinterpret_cast<void(*)(TEFMod::TerrariaInstance)>(fun)(i);
}
}
// 实际Hook逻辑
void Hook_ResetEffects(TEFMod::TerrariaInstance player) {
// 获取最大魔力值
const int maxMana = g_mana.statManaMax->Get(player);
// 将当前魔力值设置为最大值
g_mana.statMana->Set(maxMana, player);
//调试日志(可选)
g_log->d("魔力已锁定: ", maxMana);
}
class ManaLocker final : public EFMod {
public:
int Initialize(const std::string &path, MultiChannel *multiChannel) override {
return 0;
}
int UnLoad(const std::string &path, MultiChannel *multiChannel) override {
return 0;
}
int Load(const std::string &path, MultiChannel* channel) override {
// 初始化日志和API
g_log = channel->receive<TEFMod::Logger*(*)(const std::string&, const std::string&, const std::size_t)>(
"TEFMod::CreateLogger")("ManaLocker-lzup", "", 0);
g_api = channel->receive<TEFMod::TEFModAPI*>("TEFMod::TEFModAPI");
g_log->init();
g_log->i("魔力锁定Mod已加载");
return 0;
}
void Send(const std::string &path, MultiChannel* channel) override {
// 注册Hook
g_api->registerFunctionDescriptor({
"Terraria",
"Player",
"ResetEffects",
"hook>>void",
0, // 参数数量为0
&HookTemplate_ResetEffects,
{ reinterpret_cast<void*>(Hook_ResetEffects) }
});
// 注册需要使用的字段
g_api->registerApiDescriptor({"Terraria", "Player", "statMana", "Field"});
g_api->registerApiDescriptor({"Terraria", "Player", "statManaMax", "Field"});
}
void Receive(const std::string &path, MultiChannel* channel) override {
// 获取字段解析器
auto ParseIntField = channel->receive<TEFMod::Field<int>*(*)(void*)>(
"TEFMod::Field<Int>::ParseFromPointer");
// 获取原始函数指针
original_ResetEffects = g_api->GetAPI<void(*)(TEFMod::TerrariaInstance)>({
"Terraria",
"Player",
"ResetEffects",
"old_fun",
0
});
// 初始化魔力字段
g_mana.statMana = ParseIntField(g_api->GetAPI<void*>({
"Terraria",
"Player",
"statMana",
"Field"
}));
g_mana.statManaMax = ParseIntField(g_api->GetAPI<void*>({
"Terraria",
"Player",
"statManaMax",
"Field"
}));
}
Metadata GetMetadata() override {
return {
"ManaLocker", // Mod名称
"lzup", // 作者
"1.0.0", // 版本
20250711, // 日期
ModuleType::Game,
{ false }
};
}
};
EFMod* CreateMod() {
static ManaLocker instance;
return &instance;
}