Skip to content
This repository was archived by the owner on Apr 10, 2021. It is now read-only.

Commit 31fe04d

Browse files
base for proxy objects
1 parent fd38103 commit 31fe04d

File tree

7 files changed

+71
-31
lines changed

7 files changed

+71
-31
lines changed

byond-extools/src/core/core.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "core.h"
22
#include "find_functions.h"
33
#include "../tffi/tffi.h"
4+
#include "../proxy/proxy_object.h"
45

56
CrashProcPtr CrashProc;
67
SuspendPtr Suspend;
@@ -17,10 +18,16 @@ ProcSetupEntry** Core::proc_setup_table;
1718
std::map<unsigned int, opcode_handler> Core::opcode_handlers;
1819
std::map<std::string, unsigned int> Core::name_to_opcode;
1920
unsigned int next_opcode_id = 0x1337;
21+
bool Core::initialized = false;
2022

2123
bool Core::initialize()
2224
{
23-
return find_functions() && populate_proc_list();
25+
if (initialized)
26+
{
27+
return true;
28+
}
29+
initialized = find_functions() && populate_proc_list() && hook_custom_opcodes();
30+
return initialized;
2431
}
2532

2633
void Core::Alert(const char* what) {
@@ -49,17 +56,19 @@ extern "C" EXPORT const char* core_initialize(int n_args, const char* args)
4956
Core::Alert("Core init failed!");
5057
return bad;
5158
}
52-
if (!Core::hook_em())
53-
{
54-
Core::Alert("Hooking failed!");
55-
return bad;
56-
}
5759
return good;
5860
}
5961

6062
extern "C" EXPORT const char* tffi_initialize(int n_args, const char* args)
6163
{
62-
if (!TFFI::initialize())
64+
if (!(Core::initialize() && TFFI::initialize()))
65+
return bad;
66+
return good;
67+
}
68+
69+
extern "C" EXPORT const char* proxy_initialize(int n_args, const char* args)
70+
{
71+
if (!(Core::initialize() && Proxy::initialize()))
6372
return bad;
6473
return good;
6574
}

byond-extools/src/core/core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ namespace Core
2525
unsigned int register_opcode(std::string name, opcode_handler handler);
2626
void Alert(const char* what);
2727
bool initialize();
28+
extern bool initialized;
2829
}

byond-extools/src/core/hooking.cpp

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#include "hooking.h"
22

33
#ifdef _WIN32
4-
PLH::CapstoneDisassembler* disassembler;
5-
PLH::x86Detour* CrashProcDetour;
4+
static PLH::CapstoneDisassembler* disassembler;
65
#else
76
urmem::hook CrashProcDetour;
87
#endif
@@ -22,37 +21,47 @@ void hCrashProc(char* error, int argument)
2221
#endif
2322
}
2423

25-
bool Core::hook_em()
24+
25+
void* Core::install_hook(void* original, void* hook)
2626
{
2727
#ifdef _WIN32
2828
disassembler = new PLH::CapstoneDisassembler(PLH::Mode::x86);
2929
if (!disassembler)
3030
{
31-
return false;
31+
return nullptr;
3232
}
33-
std::uint64_t sendMapsTrampoline;
34-
CrashProcDetour = new PLH::x86Detour((char*)CrashProc, (char*)&hCrashProc, &sendMapsTrampoline, *disassembler);
35-
if (!CrashProcDetour)
33+
std::uint64_t trampoline;
34+
PLH::x86Detour* detour = new PLH::x86Detour((char*)original, (char*)hook, &trampoline, *disassembler);
35+
if (!detour)
3636
{
37-
return false;
37+
Core::Alert("No detour");
38+
return nullptr;
3839
}
39-
if (!CrashProcDetour->hook())
40+
if (!detour->hook())
4041
{
41-
return false;
42+
Core::Alert("hook failed");
43+
return nullptr;
4244
}
43-
oCrashProc = PLH::FnCast(sendMapsTrampoline, oCrashProc);
44-
if (!oCrashProc)
45+
return (void*)trampoline;
46+
#else
47+
detour.install(original, hook);
48+
detour.enable();
49+
if (!detour.is_enabled())
4550
{
46-
CrashProcDetour->unHook();
47-
return false;
51+
detour.disable();
52+
return nullptr;
4853
}
54+
return (void*)detour;
55+
#endif
56+
}
57+
58+
bool Core::hook_custom_opcodes()
59+
{
60+
#ifdef _WIN32
61+
oCrashProc = (CrashProcPtr)install_hook(CrashProc, hCrashProc);
62+
return oCrashProc;
4963
#else
50-
CrashProcDetour.install(urmem::get_func_addr(CrashProc), urmem::get_func_addr(hCrashProc));
51-
CrashProcDetour.enable();
52-
if(!CrashProcDetour.is_enabled()) {
53-
CrashProcDetour.disable();
54-
return false;
55-
}
64+
CrashProcDetour = (CrashProcDetour)install_hook(CrashProc, hCrashProc);
65+
return CrashProcDetour;
5666
#endif
57-
return true;
5867
}

byond-extools/src/core/hooking.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111

1212
namespace Core
1313
{
14-
bool hook_em();
14+
void* install_hook(void* original, void* hook);
15+
bool hook_custom_opcodes();
1516
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "proxy_object.h"
2+
3+
GetVariablePtr oGetVariable;
4+
5+
Value hGetVariable(Value datum, unsigned int name_id)
6+
{
7+
return oGetVariable(datum, name_id);
8+
}
9+
10+
bool Proxy::initialize()
11+
{
12+
oGetVariable = (GetVariablePtr)Core::install_hook(GetVariable, hGetVariable);
13+
return true;
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "../core/core.h"
2+
3+
namespace Proxy
4+
{
5+
bool initialize();
6+
}

byond-extools/src/tffi/tffi.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ void tffi_suspend(ExecutionContext* ctx)
2626

2727
bool TFFI::initialize()
2828
{
29+
// the CrashProc hook is currently super broken on Linux. It hooks but doesn't stop the crash.
30+
#ifdef _WIN32
2931
int suspension_opcode = Core::register_opcode("TFFI_SUSPEND", tffi_suspend);
3032
Core::Proc internal_resolve = Core::get_proc("/datum/promise/proc/__internal_resolve");
31-
#ifdef _WIN32
32-
// the CrashProc hook is currently super broken on Linux. It hooks but doesn't stop the crash.
3333
internal_resolve.set_bytecode(new std::vector<int>({ suspension_opcode, 0, 0, 0 }));
3434
#endif
3535
result_string_id = GetStringTableIndex("result", 0, 1);

0 commit comments

Comments
 (0)