66#include < iostream>
77#include < string>
88#include < GarrysMod/Lua/LuaBase.h>
9- #include < dynalo/dynalo.hpp>
10- #include < netcore/coreclr_delegates.h>
119#include " LuaAPIExposure.h"
1210#include " cleanup_function_type.h"
13- #include " hostfxr_loader .h"
14- #include " utils/get_exe_path .h"
11+ #include " hostfxr_interop .h"
12+ #include " utils/path .h"
1513#ifdef WIN32
1614#include < Windows.h>
1715#else
1816#include < cstring>
19- #include < dlfcn.h>
20- #include < unistd.h>
2117#endif
2218
2319#ifdef WIN32
2420#define DYNAMIC_EXPORT _declspec (dllexport)
25- #define __T (x ) L##x
2621#else
2722#define DYNAMIC_EXPORT __attribute__ ((visibility(" default" )))
28- #define __T (x ) x
2923#endif
3024
31- #define _T (x ) __T(x)
32-
3325typedef int (*managed_delegate_executor_fn)(lua_State* luaState);
3426
3527typedef cleanup_function_fn (*managed_main_fn)(GarrysMod::Lua::ILuaBase* lua,
@@ -46,9 +38,6 @@ std::ofstream error_log_file;
4638managed_delegate_executor_fn managed_delegate_executor = nullptr ;
4739managed_main_fn managed_main = nullptr ;
4840
49- const std::filesystem::path lua_bin_folder = _T(" garrysmod/lua/bin" );
50- const dynalo::library hostfxr_library (lua_bin_folder / _T(" dotnet/host/fxr" ) / NET_CORE_VERSION / dynalo::to_native_name(" hostfxr" ));
51-
5241void HOSTFXR_CALLTYPE dotnet_error_writer (const char_t * message)
5342{
5443 error_log_file << message << std::endl;
@@ -130,72 +119,6 @@ void* params_to_managed_code[] = {
130119};
131120// clang-format on
132121
133- hostfxr_handle get_runtime_environment_handle (const hostfxr_functions& hostfxr)
134- {
135- const auto dotnet_root_path = (std::filesystem::current_path () / lua_bin_folder / _T (" dotnet" )).make_preferred ();
136- std::filesystem::path game_exe_path = utils::get_exe_path ();
137-
138- hostfxr_initialize_parameters dotnet_runtime_params;
139- dotnet_runtime_params.size = sizeof (hostfxr_initialize_parameters);
140- dotnet_runtime_params.host_path = game_exe_path.c_str ();
141- dotnet_runtime_params.dotnet_root = dotnet_root_path.c_str ();
142-
143- const auto gmodnet_dll_relative_path = lua_bin_folder / _T (" gmodnet/GmodNET.dll" );
144- const char_t * dotnet_args[] = {_T (" exec" ), gmodnet_dll_relative_path.c_str ()};
145-
146- hostfxr_handle result = nullptr ;
147- int error_code = hostfxr.initialize_for_dotnet_command_line (static_cast <int >(std::size (dotnet_args)),
148- dotnet_args,
149- &dotnet_runtime_params,
150- &result);
151- if (error_code != 0 )
152- {
153- throw std::runtime_error (std::string (" Unable to initialize dotnet runtime. Error code: " ) + std::to_string (error_code));
154- }
155- if (result == nullptr )
156- {
157- throw std::runtime_error (" runtime_environment_handle is null" );
158- }
159-
160- return result;
161- }
162-
163- get_function_pointer_fn get_dotnet_runtime_delegate (const hostfxr_functions& hostfxr, hostfxr_handle runtime_environment_handle)
164- {
165- get_function_pointer_fn result = nullptr ;
166- int error_code = hostfxr.get_runtime_delegate (runtime_environment_handle, hdt_get_function_pointer, reinterpret_cast <void **>(&result));
167- if (error_code != 0 )
168- {
169- throw std::runtime_error (std::string (" Unable to get delegate of dotnet runtime. Error code: " ) + std::to_string (error_code));
170- }
171- if (result == nullptr )
172- {
173- throw std::runtime_error (" get_function_pointer is null" );
174- }
175-
176- return result;
177- }
178-
179- managed_main_fn load_gmodnet_main (get_function_pointer_fn get_function_pointer)
180- {
181- managed_main_fn result = nullptr ;
182- int error_code = get_function_pointer (_T (" GmodNET.Startup, GmodNET" ),
183- _T (" Main" ),
184- UNMANAGEDCALLERSONLY_METHOD,
185- nullptr ,
186- nullptr ,
187- reinterpret_cast <void **>(&result));
188- if (error_code != 0 )
189- {
190- throw std::runtime_error (std::string (" Unable to load managed entry point: Error code: " ) + std::to_string (error_code));
191- }
192- if (result == nullptr )
193- {
194- throw std::runtime_error (" Unable to load managed entry point: managed_main is null" );
195- }
196- return result;
197- }
198-
199122extern " C" DYNAMIC_EXPORT cleanup_function_fn InitNetRuntime (GarrysMod::Lua::ILuaBase* lua)
200123{
201124 if (!error_log_file.is_open ())
@@ -207,11 +130,9 @@ extern "C" DYNAMIC_EXPORT cleanup_function_fn InitNetRuntime(GarrysMod::Lua::ILu
207130 {
208131 try
209132 {
210- hostfxr_functions hostfxr (hostfxr_library) ;
133+ hostfxr_interop hostfxr{} ;
211134 hostfxr.set_error_writer (dotnet_error_writer);
212- hostfxr_handle runtime_environment_handle = get_runtime_environment_handle (hostfxr);
213- get_function_pointer_fn get_function_pointer = get_dotnet_runtime_delegate (hostfxr, runtime_environment_handle);
214- managed_main = load_gmodnet_main (get_function_pointer);
135+ managed_main = hostfxr.load_gmodnet_main <managed_main_fn>();
215136 }
216137 catch (const std::runtime_error& ex)
217138 {
0 commit comments