1010#include < dlfcn.h>
1111#endif // WIN32
1212#include < string>
13- #include " ../dotnethelper-src/cleanup_function_type.h"
14-
15- typedef cleanup_function_fn (*InitNetRuntime_fn)(GarrysMod::Lua::ILuaBase* lua);
13+ #include " dotnethelper-src/cleanup_function_type.h"
14+ #include < dynalo/dynalo.hpp>
1615
1716cleanup_function_fn cleanup_function = nullptr ;
1817
18+ const std::filesystem::path lua_bin_folder (" garrysmod/lua/bin" );
19+
20+ #ifdef __gnu_linux__
21+ const dynalo::library liblinuxhelper (lua_bin_folder / " liblinuxhelper.so" );
22+ #endif
23+
24+ const dynalo::library dotnethelper (lua_bin_folder / dynalo::to_native_name(" dotnethelper" ));
25+
1926// Invoked by Garry's Mod on module load
2027GMOD_MODULE_OPEN ()
2128{
22- const std::filesystem::path lua_bin_folder (" garrysmod/lua/bin" );
23-
2429 // On Linux, modify SIGSEGV handling
2530#ifdef __gnu_linux__
26- void *linux_helper_handle = dlopen ((lua_bin_folder / " liblinuxhelper.so" ).c_str (), RTLD_LAZY);
27- void (*pointer_to_install_sigsegv)(void );
28- pointer_to_install_sigsegv = (void (*)())dlsym (linux_helper_handle, " install_sigsegv_handler" );
31+ auto pointer_to_install_sigsegv = liblinuxhelper.get_function <void ()>(" install_sigsegv_handler" );
2932 pointer_to_install_sigsegv ();
3033#endif
3134
@@ -36,36 +39,22 @@ GMOD_MODULE_OPEN()
3639 LUA->Call (1 , 0 );
3740 LUA->Pop (1 );
3841
39- InitNetRuntime_fn InitNetRuntime = nullptr ;
40- const char InitNetRuntime_fn_name[] = " InitNetRuntime" ;
41-
42- #ifdef WIN32
43- HMODULE dotnethelper_handle = LoadLibraryW ((lua_bin_folder / " dotnethelper.dll" ).make_preferred ().c_str ());
44- if (dotnethelper_handle != nullptr )
45- InitNetRuntime = reinterpret_cast <InitNetRuntime_fn>(GetProcAddress (dotnethelper_handle, InitNetRuntime_fn_name));
46- #elif __APPLE__
47- void * dotnethelper_handle = dlopen ((lua_bin_folder / " libdotnethelper.dylib" ).c_str (), RTLD_LAZY);
48- #elif __gnu_linux__
49- void * dotnethelper_handle = dlopen ((lua_bin_folder / " libdotnethelper.so" ).c_str (), RTLD_LAZY);
50- #endif
51-
52- #ifndef WIN32
53- InitNetRuntime = reinterpret_cast <InitNetRuntime_fn>(dlsym (dotnethelper_handle, InitNetRuntime_fn_name));
54- #endif
55-
56- if (InitNetRuntime == nullptr )
42+ try
5743 {
44+ auto InitNetRuntime = dotnethelper.get_function <cleanup_function_fn (GarrysMod::Lua::ILuaBase*)>(" InitNetRuntime" );
45+ cleanup_function = InitNetRuntime (LUA);
46+ }
47+ catch (std::runtime_error e)
48+ {
49+ auto error_msg = std::string (" ::error::Unable to load dotnet helper library. " ) + e.what ();
5850 LUA->PushSpecial (GarrysMod::Lua::SPECIAL_GLOB);
5951 LUA->GetField (-1 , " print" );
60- LUA->PushString (" ::error::Unable to load dotnet helper library. " );
52+ LUA->PushString (error_msg. c_str () );
6153 LUA->Call (1 , 0 );
6254 LUA->Pop (1 );
63-
6455 return 0 ;
6556 }
6657
67- cleanup_function = InitNetRuntime (LUA);
68-
6958 if (cleanup_function == nullptr )
7059 {
7160 LUA->PushSpecial (GarrysMod::Lua::SPECIAL_GLOB);
0 commit comments