This repository was archived by the owner on Oct 14, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathvoid_drop_fix.cpp
More file actions
337 lines (287 loc) · 12.7 KB
/
Copy pathvoid_drop_fix.cpp
File metadata and controls
337 lines (287 loc) · 12.7 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <D2RLPlugin/api.h>
#include <D2RLPlugin/logging.h>
#include <algorithm>
#include <array>
#include <atomic>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <string>
#include <string_view>
namespace {
// ============================================================================
// Plugin Metadata
// ============================================================================
static constexpr D2RL::PluginInfo kPluginInfo{
.infoSize = D2RL::PluginInfoSize,
.abiVersion = D2RL_PLUGIN_ABI_VERSION,
.id = "d2rl-voiddropfix",
.name = "D2R Void Drop Fix",
.version = "1.0.0",
.author = "D2RLoader Community",
.description = "Prevents items and gold from being destroyed when monsters die over void/abyss tiles by finding the nearest solid ground.",
.flags = D2RL::PluginFlags::Shared | D2RL::PluginFlags::NativeHooks,
};
// ============================================================================
// Configuration & State
// ============================================================================
struct Config {
bool enabled = true;
bool logRecoveries = false;
int32_t searchRadius = 64; // Generous search radius (in tiles) to guarantee finding valid ground
};
static Config g_config;
static const D2RL::PluginContext* g_context = nullptr;
static std::atomic<uint64_t> g_totalCallsCount{0};
static std::atomic<uint64_t> g_groundDropsCount{0};
static std::atomic<uint64_t> g_voidShiftedDropsCount{0};
static std::atomic<uint64_t> g_deepVoidRescuesCount{0};
static std::atomic<int32_t> g_lastRadiusSeen{0};
static std::atomic<uint32_t> g_lastMaskSeen{0};
static std::atomic<int32_t> g_lastDistanceSeen{0};
static std::atomic<int32_t> g_maxDistanceSeen{0};
// Target RVA in Diablo II: Resurrected for COLLISION_GetFreeCoordinates
// Base address: 0x140000000 | Ghidra function: 0x140364e90
static constexpr uint64_t kCollisionGetFreeCoordsRva = 0x00364E90;
// Safety check bytes required by D2RLoader to verify the binary function prologue:
// 140364e90 48 8b c4 MOV RAX, RSP
// 140364e93 44 89 48 20 MOV dword ptr [RAX + 0x20], R9D (nMask)
// 140364e97 4c 89 40 18 MOV qword ptr [RAX + 0x18], R8 (pOriginCoords)
// 140364e9b 53 PUSH RBX
// 140364e9c 55 PUSH RBP
// 140364e9d 56 PUSH RSI
// 140364e9e 57 PUSH RDI
static constexpr uint8_t kExpectedGetFreeCoordsBytes[]{
0x48, 0x8B, 0xC4,
0x44, 0x89, 0x48, 0x20,
0x4C, 0x89, 0x40, 0x18,
0x53,
0x55,
0x56,
0x57
};
// Type definition matching the engine's COLLISION_GetFreeCoordinates signature
using COLLISION_GetFreeCoordinatesFn = int64_t(__fastcall*)(
int64_t pRoom,
int32_t* pTargetCoords,
int32_t* pOriginCoords,
uint32_t nMask,
uint32_t nField,
uint32_t bCheckLOS,
int32_t bIncludeOrigin,
int32_t nRadius,
int32_t nStep
);
static COLLISION_GetFreeCoordinatesFn g_originalGetFreeCoords = nullptr;
// ============================================================================
// Config File Helpers (TOML parsing conforming to D2RLoader guidelines)
// ============================================================================
static constexpr const char* kDefaultConfigToml =
"# D2RLoader Void Drop Fix Configuration\n"
"# Prevents items, runes, and gold from vanishing when monsters die over void/abyss tiles.\n\n"
"[general]\n"
"enabled = true\n"
"log_recoveries = false\n"
"search_radius = 64\n";
static std::string Trim(std::string_view sv) {
const auto first = sv.find_first_not_of(" \t\r\n");
if (first == std::string_view::npos) return {};
const auto last = sv.find_last_not_of(" \t\r\n");
return std::string(sv.substr(first, last - first + 1));
}
static std::string ToLower(std::string s) {
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) {
return static_cast<char>(std::tolower(c));
});
return s;
}
static void LoadConfiguration(const D2RL::PluginContext* context) {
if (!context) return;
if (context->EnsureConfig(kDefaultConfigToml)) {
std::array<char, 4096> tomlBuffer{};
uint32_t requiredSize = 0;
if (context->ReadConfig(tomlBuffer.data(), static_cast<uint32_t>(tomlBuffer.size()), &requiredSize) && requiredSize > 0) {
std::string_view content(tomlBuffer.data(), std::min<size_t>(requiredSize, tomlBuffer.size()));
size_t start = 0;
while (start < content.size()) {
size_t end = content.find('\n', start);
if (end == std::string_view::npos) end = content.size();
std::string line = Trim(content.substr(start, end - start));
start = end + 1;
if (line.empty() || line.starts_with('#') || line.starts_with('[')) {
continue;
}
const size_t eq = line.find('=');
if (eq == std::string_view::npos) continue;
std::string key = ToLower(Trim(line.substr(0, eq)));
std::string val = ToLower(Trim(line.substr(eq + 1)));
if (key == "enabled") {
g_config.enabled = (val == "true" || val == "1");
} else if (key == "log_recoveries" || key == "logging") {
g_config.logRecoveries = (val == "true" || val == "1");
} else if (key == "search_radius" || key == "wider_search_radius") {
try {
g_config.searchRadius = std::clamp(std::stoi(val), 8, 256);
} catch (...) {}
}
}
}
}
}
// ============================================================================
// The Core Hook: Safe Ground Search Recovery
// ============================================================================
static int64_t __fastcall HookCOLLISION_GetFreeCoordinates(
int64_t pRoom,
int32_t* pTargetCoords,
int32_t* pOriginCoords,
uint32_t nMask,
uint32_t nField,
uint32_t bCheckLOS,
int32_t bIncludeOrigin,
int32_t nRadius,
int32_t nStep
) {
const auto original = g_originalGetFreeCoords;
if (!original) {
return 0;
}
g_totalCallsCount.fetch_add(1, std::memory_order_relaxed);
g_lastRadiusSeen.store(nRadius, std::memory_order_relaxed);
g_lastMaskSeen.store(nMask, std::memory_order_relaxed);
// Step 1: Let the vanilla engine search with its standard radius.
// If the monster is already on or near valid walkable ground, this succeeds immediately.
int64_t resultRoom = original(
pRoom, pTargetCoords, pOriginCoords, nMask, nField, bCheckLOS, bIncludeOrigin, nRadius, nStep
);
// If a valid ground tile was found, check distance moved
if (resultRoom != 0) {
int32_t dist = 0;
if (pTargetCoords && pOriginCoords) {
dist = std::max(std::abs(pTargetCoords[0] - pOriginCoords[0]), std::abs(pTargetCoords[1] - pOriginCoords[1]));
g_lastDistanceSeen.store(dist, std::memory_order_relaxed);
int32_t currentMax = g_maxDistanceSeen.load(std::memory_order_relaxed);
while (dist > currentMax && !g_maxDistanceSeen.compare_exchange_weak(currentMax, dist, std::memory_order_relaxed)) {}
}
if (dist > 3) {
// Repositioned across void/abyss to the nearest solid ground/walkway (> 3 tiles)
g_voidShiftedDropsCount.fetch_add(1, std::memory_order_relaxed);
} else {
// Dropped directly on solid ground (including normal scatter up to 3 tiles)
g_groundDropsCount.fetch_add(1, std::memory_order_relaxed);
}
return resultRoom;
}
if (!g_config.enabled || !pTargetCoords) {
return 0;
}
// Step 2: Monster died out over deep void/abyss (Step 1 failed).
// Perform an expanded radius search to find the nearest walkable ground/ledge.
if (pRoom != 0) {
const int32_t expandedRadius = std::max(nRadius, g_config.searchRadius);
resultRoom = original(
pRoom, pTargetCoords, pOriginCoords, nMask, nField, bCheckLOS, bIncludeOrigin, expandedRadius, nStep
);
if (resultRoom != 0) {
g_deepVoidRescuesCount.fetch_add(1, std::memory_order_relaxed);
if (pTargetCoords && pOriginCoords) {
const int32_t dist = std::max(std::abs(pTargetCoords[0] - pOriginCoords[0]), std::abs(pTargetCoords[1] - pOriginCoords[1]));
g_lastDistanceSeen.store(dist, std::memory_order_relaxed);
int32_t currentMax = g_maxDistanceSeen.load(std::memory_order_relaxed);
while (dist > currentMax && !g_maxDistanceSeen.compare_exchange_weak(currentMax, dist, std::memory_order_relaxed)) {}
if (g_config.logRecoveries && g_context) {
char logBuf[128];
std::snprintf(logBuf, sizeof(logBuf), "[VoidDropFix] Rescued void drop to ledge (distance moved: %d tiles)", dist);
g_context->WriteConsoleMessage(logBuf);
}
}
return resultRoom;
}
}
return 0;
}
// ============================================================================
// In-Game Console Command Handler
// ============================================================================
static auto VoidDropFixCommand(
D2R::Game::Client* client,
const D2RL::ConsoleCommandContext* cmd,
void* userData
) noexcept -> D2RL::ConsoleCommandResult {
(void)client;
(void)userData;
if (!cmd || !cmd->plugin) {
return D2RL::ConsoleCommandResult::Failed;
}
char buffer[256];
std::snprintf(buffer, sizeof(buffer),
"[VoidDropFix] Status: %s | Logging: %s | Search Radius: %d tiles",
g_config.enabled ? "ENABLED" : "DISABLED",
g_config.logRecoveries ? "ENABLED" : "DISABLED",
g_config.searchRadius
);
cmd->plugin->WriteConsoleMessage(buffer);
std::snprintf(buffer, sizeof(buffer),
"[VoidDropFix] Total Calls: %llu | Ground: %llu | Void-Shifted: %llu | Deep-Rescued: %llu",
static_cast<unsigned long long>(g_totalCallsCount.load()),
static_cast<unsigned long long>(g_groundDropsCount.load()),
static_cast<unsigned long long>(g_voidShiftedDropsCount.load()),
static_cast<unsigned long long>(g_deepVoidRescuesCount.load())
);
cmd->plugin->WriteConsoleMessage(buffer);
std::snprintf(buffer, sizeof(buffer),
"[VoidDropFix] Last Seen: Radius=%d | Mask=0x%X | DistanceMoved=%d tiles (Max: %d)",
g_lastRadiusSeen.load(),
g_lastMaskSeen.load(),
g_lastDistanceSeen.load(),
g_maxDistanceSeen.load()
);
cmd->plugin->WriteConsoleMessage(buffer);
return D2RL::ConsoleCommandResult::Handled;
}
// ============================================================================
// Safe Hook Installation
// ============================================================================
static auto InstallVoidDropHook(const D2RL::PluginContext* context) noexcept -> bool {
if (!context) return false;
// Use D2RLoader's tracked inline hook installation with verified safety check bytes
const bool success = context->InstallInlineHook(
kCollisionGetFreeCoordsRva,
kExpectedGetFreeCoordsBytes,
static_cast<uint32_t>(sizeof(kExpectedGetFreeCoordsBytes)),
HookCOLLISION_GetFreeCoordinates,
&g_originalGetFreeCoords
);
if (success) {
context->LogInfo("[VoidDropFix] HookCOLLISION_GetFreeCoordinates installed successfully.");
} else {
context->LogError("[VoidDropFix] Failed to install inline hook on COLLISION_GetFreeCoordinates.");
}
return success;
}
} // namespace
// ============================================================================
// D2RLoader Export Functions
// ============================================================================
D2RL_PLUGIN_EXPORT auto D2RLoaderGetPluginInfo() noexcept -> const D2RL::PluginInfo* {
return &kPluginInfo;
}
D2RL_PLUGIN_EXPORT auto D2RLoaderLoadPlugin(const D2RL::PluginContext* context) noexcept -> bool {
if (!context) {
return false;
}
g_context = context;
LoadConfiguration(context);
if (!context->RegisterConsoleCommand("void-drop-fix", VoidDropFixCommand, "Displays Void Drop Fix status and recovery stats.")) {
context->LogWarn("[VoidDropFix] Console command 'void-drop-fix' could not be registered.");
}
if (!InstallVoidDropHook(context)) {
return false;
}
context->LogInfo("[VoidDropFix] D2R Void Drop Fix Plugin v1.0.0 successfully initialized.");
return true;
}
D2RL_PLUGIN_EXPORT void D2RLoaderUnloadPlugin() noexcept {
g_context = nullptr;
g_originalGetFreeCoords = nullptr;
}