@@ -49,34 +49,29 @@ managed_delegate_executor_fn managed_delegate_executor = nullptr;
4949
5050managed_main_fn managed_main = nullptr ;
5151
52- const auto hosfxr_path = std::filesystem::path(_T(" garrysmod/lua/bin/dotnet/host/fxr" )) / NET_CORE_VERSION;
52+ const std::filesystem::path bin_folder = _T(" garrysmod/lua/bin" );
53+ const std::filesystem::path hostfxr_path = (bin_folder / _T(" dotnet/host/fxr" ) / NET_CORE_VERSION).make_preferred();
5354#ifdef WIN32
54- void * hostfxr_library_handle = LoadLibraryW((hosfxr_path / _T(" hostfxr.dll" )).make_preferred().wstring( ).c_str());
55+ HMODULE hostfxr_library_handle = LoadLibraryW((hostfxr_path / _T(" hostfxr.dll" )).c_str());
5556#elif __APPLE__
56- void * hostfxr_library_handle = dlopen((hosfxr_path / " libhostfxr.dylib" ).string( ).c_str(), RTLD_LAZY | RTLD_LOCAL);
57+ void * hostfxr_library_handle = dlopen((hostfxr_path / " libhostfxr.dylib" ).c_str(), RTLD_LAZY | RTLD_LOCAL);
5758#elif __gnu_linux__
58- void * hostfxr_library_handle = dlopen((hosfxr_path / " libhostfxr.so" ).string( ).c_str(), RTLD_LAZY);
59+ void * hostfxr_library_handle = dlopen((hostfxr_path / " libhostfxr.so" ).c_str(), RTLD_LAZY);
5960#endif
6061
62+ template <typename T>
63+ bool LoadFunction (const char * function_name, T& out_func)
64+ {
6165#ifdef WIN32
62- hostfxr_initialize_for_dotnet_command_line_fn hostfxr_initialize_for_dotnet_command_line =
63- reinterpret_cast <hostfxr_initialize_for_dotnet_command_line_fn>(GetProcAddress(static_cast <HMODULE>(hostfxr_library_handle),
64- " hostfxr_initialize_for_dotnet_command_line" ));
65- hostfxr_get_runtime_delegate_fn hostfxr_get_runtime_delegate =
66- reinterpret_cast <hostfxr_get_runtime_delegate_fn>(GetProcAddress(static_cast <HMODULE>(hostfxr_library_handle),
67- " hostfxr_get_runtime_delegate" ));
68-
69- hostfxr_set_error_writer_fn hostfxr_set_error_writer =
70- reinterpret_cast <hostfxr_set_error_writer_fn>(GetProcAddress(static_cast <HMODULE>(hostfxr_library_handle),
71- " hostfxr_set_error_writer" ));
66+ out_func = reinterpret_cast <T>(GetProcAddress (hostfxr_library_handle, function_name));
7267#else
73- hostfxr_initialize_for_dotnet_command_line_fn hostfxr_initialize_for_dotnet_command_line =
74- reinterpret_cast <hostfxr_initialize_for_dotnet_command_line_fn>(dlsym(hostfxr_library_handle, " hostfxr_initialize_for_dotnet_command_line" ));
75- hostfxr_get_runtime_delegate_fn hostfxr_get_runtime_delegate =
76- reinterpret_cast <hostfxr_get_runtime_delegate_fn>(dlsym(hostfxr_library_handle, " hostfxr_get_runtime_delegate" ));
77- hostfxr_set_error_writer_fn hostfxr_set_error_writer =
78- reinterpret_cast <hostfxr_set_error_writer_fn>(dlsym(hostfxr_library_handle, " hostfxr_set_error_writer" ));
68+ out_func = reinterpret_cast <T>(dlsym (hostfxr_library_handle, function_name));
7969#endif
70+ return (out_func != nullptr );
71+ }
72+ hostfxr_initialize_for_dotnet_command_line_fn hostfxr_initialize_for_dotnet_command_line = nullptr ;
73+ hostfxr_get_runtime_delegate_fn hostfxr_get_runtime_delegate = nullptr ;
74+ hostfxr_set_error_writer_fn hostfxr_set_error_writer = nullptr ;
8075
8176void HOSTFXR_CALLTYPE dotnet_error_writer (const char_t *message)
8277{
@@ -166,7 +161,9 @@ extern "C" DYNAMIC_EXPORT cleanup_function_fn InitNetRuntime(GarrysMod::Lua::ILu
166161
167162 if (managed_main == nullptr )
168163 {
169- if (hostfxr_initialize_for_dotnet_command_line == nullptr || hostfxr_get_runtime_delegate == nullptr || hostfxr_set_error_writer == nullptr )
164+ if (!(LoadFunction (" hostfxr_initialize_for_dotnet_command_line" , hostfxr_initialize_for_dotnet_command_line)
165+ && LoadFunction (" hostfxr_get_runtime_delegate" , hostfxr_get_runtime_delegate)
166+ && LoadFunction (" hostfxr_set_error_writer" , hostfxr_set_error_writer)))
170167 {
171168 error_log_file << " Unable to load hostfxr library" << std::endl;
172169 return nullptr ;
@@ -176,22 +173,24 @@ extern "C" DYNAMIC_EXPORT cleanup_function_fn InitNetRuntime(GarrysMod::Lua::ILu
176173
177174 hostfxr_set_error_writer (dotnet_error_writer);
178175
179- const char_t * dotnet_args[] = {_T (" exec" ), _T (" garrysmod/lua/bin/gmodnet/GmodNET.dll" )};
176+ const auto gmodnet_dll_relative_path = bin_folder / _T (" gmodnet/GmodNET.dll" );
177+ const auto dotnet_root_path = (std::filesystem::current_path () / bin_folder / " dotnet" ).make_preferred ();
180178
181- hostfxr_initialize_parameters dotnet_runtime_params ;
182- dotnet_runtime_params. size = sizeof (hostfxr_initialize_parameters);
179+ const char_t * dotnet_args[] = { _T ( " exec " ), gmodnet_dll_relative_path. c_str ()} ;
180+
183181 tstring game_exe_path (301 , _T (' \0 ' ));
184182#ifdef WIN32
185- GetModuleFileNameW (nullptr , game_exe_path.data (), game_exe_path.size () - 1 );
183+ GetModuleFileNameW (nullptr , game_exe_path.data (), static_cast <DWORD>( game_exe_path.size () ) - 1 );
186184#else
187185 readlink (" /proc/self/exe" , game_exe_path.data (), game_exe_path.size () - 1 );
188186#endif
187+ hostfxr_initialize_parameters dotnet_runtime_params;
188+ dotnet_runtime_params.size = sizeof (hostfxr_initialize_parameters);
189189 dotnet_runtime_params.host_path = game_exe_path.c_str ();
190+ dotnet_runtime_params.dotnet_root = dotnet_root_path.c_str ();
190191
191- std::filesystem::path dotnet_root_path = std::filesystem::current_path () / " garrysmod" / " lua" / " bin" / " dotnet" ;
192- dotnet_runtime_params.dotnet_root = dotnet_root_path.native ().c_str ();
193-
194- int init_success_code = hostfxr_initialize_for_dotnet_command_line (std::size (dotnet_args), dotnet_args, &dotnet_runtime_params, &runtime_environment_handle);
192+ int init_success_code = hostfxr_initialize_for_dotnet_command_line (static_cast <int >(std::size (dotnet_args)), dotnet_args,
193+ &dotnet_runtime_params, &runtime_environment_handle);
195194 if (init_success_code != 0 )
196195 {
197196 error_log_file << " Unable to initialize dotnet runtime. Error code: " << init_success_code << std::endl;
@@ -229,7 +228,7 @@ extern "C" DYNAMIC_EXPORT cleanup_function_fn InitNetRuntime(GarrysMod::Lua::ILu
229228 }
230229 }
231230
232- return managed_main (lua, SEM_VERSION, std::strlen (SEM_VERSION), params_to_managed_code,
231+ return managed_main (lua, SEM_VERSION, static_cast < int >( std::strlen (SEM_VERSION) ), params_to_managed_code,
233232 native_delegate_executor, &managed_delegate_executor);
234233}
235234
0 commit comments