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

Commit 38ef248

Browse files
DM api
1 parent 6bc2453 commit 38ef248

File tree

6 files changed

+117
-18
lines changed

6 files changed

+117
-18
lines changed

byond-extools/byond-extools.vcxproj

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@
170170
</ItemDefinitionGroup>
171171
<ItemGroup>
172172
<ClInclude Include="framework.h" />
173-
<ClInclude Include="pch.h" />
174173
<ClInclude Include="src\core\core.h" />
175174
<ClInclude Include="src\core\find_functions.h" />
176175
<ClInclude Include="src\core\hooking.h" />
@@ -182,12 +181,6 @@
182181
</ItemGroup>
183182
<ItemGroup>
184183
<ClCompile Include="dllmain.cpp" />
185-
<ClCompile Include="pch.cpp">
186-
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
187-
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
188-
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
189-
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
190-
</ClCompile>
191184
<ClCompile Include="src\core\core.cpp" />
192185
<ClCompile Include="src\core\find_functions.cpp" />
193186
<ClCompile Include="src\core\hooking.cpp" />

byond-extools/byond-extools.vcxproj.filters

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636
<ClInclude Include="framework.h">
3737
<Filter>Header Files</Filter>
3838
</ClInclude>
39-
<ClInclude Include="pch.h">
40-
<Filter>Header Files</Filter>
41-
</ClInclude>
4239
<ClInclude Include="src\core\core.h">
4340
<Filter>Header Files\core</Filter>
4441
</ClInclude>
@@ -68,9 +65,6 @@
6865
<ClCompile Include="dllmain.cpp">
6966
<Filter>Source Files</Filter>
7067
</ClCompile>
71-
<ClCompile Include="pch.cpp">
72-
<Filter>Source Files</Filter>
73-
</ClCompile>
7468
<ClCompile Include="src\core\core.cpp">
7569
<Filter>Source Files\core</Filter>
7670
</ClCompile>

byond-extools/dllmain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// dllmain.cpp : Defines the entry point for the DLL application.
2-
#include "pch.h"
2+
#include <Windows.h>
33

44
BOOL APIENTRY DllMain( HMODULE hModule,
55
DWORD ul_reason_for_call,

byond-extools/src/core/core.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,24 @@ unsigned int Core::register_opcode(std::string name, opcode_handler handler)
3434
const char* good = "gucci";
3535
const char* bad = "pain";
3636

37-
extern "C" __declspec(dllexport) const char* initialize(int n_args, const char* args)
37+
extern "C" __declspec(dllexport) const char* core_initialize(int n_args, const char* args)
3838
{
3939
if (!Core::initialize())
4040
{
41+
MessageBoxA(NULL, "Core init failed!", "damn it", NULL);
4142
return bad;
4243
}
4344
if (!Core::hook_em())
4445
{
46+
MessageBoxA(NULL, "Hooking failed!", "damn it", NULL);
4547
return bad;
4648
}
47-
TFFI::initialize();
49+
return good;
50+
}
51+
52+
extern "C" __declspec(dllexport) const char* tffi_initialize(int n_args, const char* args)
53+
{
54+
if (!TFFI::initialize())
55+
return bad;
4856
return good;
4957
}

byond-extools/src/core/proc_management.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ int Core::Proc::get_local_varcount()
4242
return setup_entry_varcount->local_var_count;
4343
}
4444

45-
Core::Proc get_proc(std::string name)
45+
Core::Proc Core::get_proc(std::string name)
4646
{
4747
return procs_by_name[name];
4848
}
4949

50-
Core::Proc get_proc(unsigned int id)
50+
Core::Proc Core::get_proc(unsigned int id)
5151
{
5252
return procs_by_id[id];
5353
}
@@ -72,4 +72,5 @@ bool Core::populate_proc_list()
7272
procs_by_name[p.name] = p;
7373
i++;
7474
}
75+
return true;
7576
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#define EXTOOLS_SUCCESS "gucci"
2+
#define EXTOOLS_FAILED "pain"
3+
4+
/*
5+
Core - Provides necessary functionality for other modules.
6+
7+
Must be initialized first before initializing any modules!
8+
9+
*/
10+
11+
/proc/extools_initialize()
12+
return call("byond-extools.dll", "core_initialize")() == EXTOOLS_SUCCESS
13+
14+
/*
15+
TFFI - Threaded FFI
16+
17+
All DLL calls are automatically threaded off.
18+
Black magic is used to suspend (sleep) the currently executing proc, allowing non-blocking FFI.
19+
20+
You may call a DLL function and sleep until it returns, pass a callback to be called with the result,
21+
or call resolve() on the /datum/promise to receive the return value at any time.
22+
23+
Example:
24+
25+
26+
var/x = call_wait("sample.dll", "do_work", "arg1", "arg2", "arg3")
27+
28+
- Calls the do_work function from sample.dll with 3 arguments. The proc sleeps until do_work returns.
29+
30+
31+
32+
var/datum/promise/P = call_async("sample.dll", "do_work", "arg1")
33+
... do something else ...
34+
var/result = P.resolve()
35+
36+
- Calls do_work with 1 argument. Returns a promise object. Runs some other code before calling P.resolve() to obtain the result.
37+
38+
39+
40+
/proc/print_result(result)
41+
world << result
42+
43+
call_cb("sample.dll", "do_work", /proc/print_result, "arg1", "arg2")
44+
45+
- Calls do_work with 2 arguments. The callback is invoked with the result as the single argument. Execution resumes immediately.
46+
47+
48+
*/
49+
50+
/proc/tffi_initialize()
51+
call("byond-extools.dll", "tffi_initialize")() == EXTOOLS_SUCCESS
52+
53+
var/fallback_alerted = FALSE
54+
var/next_promise_id = 0
55+
56+
/datum/promise
57+
var/completed = FALSE
58+
var/result = ""
59+
var/cb = null
60+
var/__id = 0
61+
62+
/datum/promise/New()
63+
__id = next_promise_id++ //please don't create more than 10^38 promises in a single tick
64+
65+
//This proc's bytecode is overwritten to allow suspending and resuming on demand.
66+
//None of the code here should run.
67+
/datum/promise/proc/__internal_resolve(ref, id)
68+
if(!fallback_alerted)
69+
world << "<b>TFFI: __internal_resolve has not been rewritten, the TFFI DLL was not loaded correctly.</b>"
70+
world.log << "<b>TFFI: __internal_resolve has not been rewritten, the TFFI DLL was not loaded correctly.</b>"
71+
fallback_alerted = TRUE
72+
while(!completed)
73+
sleep(1)
74+
//It might be better to just fail and notify the user that something went wrong.
75+
76+
/datum/promise/proc/__resolve_callback()
77+
__internal_resolve("\ref[src]", __id)
78+
call(cb)(result)
79+
80+
/datum/promise/proc/resolve()
81+
__internal_resolve("\ref[src]", __id)
82+
return result
83+
84+
/proc/call_async()
85+
var/list/arguments = args.Copy()
86+
var/datum/promise/P = new
87+
arguments.Insert(1, "\ref[P]")
88+
call("byond-extools.dll", "call_async")(arglist(arguments))
89+
return P
90+
91+
/proc/call_cb()
92+
var/list/arguments = args.Copy()
93+
var/callback = arguments[3]
94+
arguments.Cut(3, 4)
95+
var/datum/promise/P = new
96+
P.cb = callback
97+
arguments.Insert(1, "\ref[P]")
98+
call("byond-extools.dll", "call_async")(arglist(arguments))
99+
spawn(0)
100+
P.__resolve_callback()
101+
102+
/proc/call_wait()
103+
return call_async(arglist(args)).resolve()

0 commit comments

Comments
 (0)