Skip to content

Commit b811ca2

Browse files
committed
Minor refactoring
1 parent 759eee6 commit b811ca2

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

gm_dotnet_native/src/gm_dotnet.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//
44
#include <GarrysMod/Lua/Interface.h>
55
#include <codecvt>
6+
#include <filesystem>
67
#ifdef WIN32
78
#include <Windows.h>
89
#else
@@ -18,9 +19,11 @@ cleanup_function_fn cleanup_function = nullptr;
1819
//Invoked by Garry's Mod on module load
1920
GMOD_MODULE_OPEN()
2021
{
22+
const std::filesystem::path bin_folder("garrysmod/lua/bin");
23+
2124
// On Linux, modify SIGSEGV handling
2225
#ifdef __gnu_linux__
23-
void *linux_helper_handle = dlopen("garrysmod/lua/bin/liblinuxhelper.so", RTLD_LAZY);
26+
void *linux_helper_handle = dlopen((bin_folder / "liblinuxhelper.so").c_str(), RTLD_LAZY);
2427
void (*pointer_to_install_sigsegv)(void);
2528
pointer_to_install_sigsegv = (void(*)())dlsym(linux_helper_handle, "install_sigsegv_handler");
2629
pointer_to_install_sigsegv();
@@ -29,21 +32,25 @@ GMOD_MODULE_OPEN()
2932
// Native welcome message
3033
LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
3134
LUA->GetField(-1, "print");
32-
LUA->PushString((std::string("Gmod dotnet loader ") + std::string(SEM_VERSION)).c_str());
35+
LUA->PushString("Gmod dotnet loader " SEM_VERSION);
3336
LUA->Call(1, 0);
3437
LUA->Pop(1);
3538

39+
InitNetRuntime_fn InitNetRuntime = nullptr;
40+
const char InitNetRuntime_fn_name[] = "InitNetRuntime";
41+
3642
#ifdef WIN32
37-
HMODULE dotnethelper_handle = LoadLibraryA("garrysmod/lua/bin/dotnethelper.dll");
38-
InitNetRuntime_fn InitNetRuntime = reinterpret_cast<InitNetRuntime_fn>(GetProcAddress(dotnethelper_handle, "InitNetRuntime"));
43+
HMODULE dotnethelper_handle = LoadLibraryW((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));
3946
#elif __APPLE__
40-
void* dotnethelper_handle = dlopen("garrysmod/lua/bin/libdotnethelper.dylib", RTLD_LAZY);
47+
void* dotnethelper_handle = dlopen((bin_folder / "libdotnethelper.dylib").c_str(), RTLD_LAZY);
4148
#elif __gnu_linux__
42-
void* dotnethelper_handle = dlopen("garrysmod/lua/bin/libdotnethelper.so", RTLD_LAZY);
49+
void* dotnethelper_handle = dlopen((bin_folder / "libdotnethelper.so").c_str(), RTLD_LAZY);
4350
#endif
4451

4552
#ifndef WIN32
46-
InitNetRuntime_fn InitNetRuntime = reinterpret_cast<InitNetRuntime_fn>(dlsym(dotnethelper_handle, "InitNetRuntime"));
53+
InitNetRuntime = reinterpret_cast<InitNetRuntime_fn>(dlsym(dotnethelper_handle, InitNetRuntime_fn_name));
4754
#endif
4855

4956
if(InitNetRuntime == nullptr)

0 commit comments

Comments
 (0)