1313#ifdef WIN32
1414#include < Windows.h>
1515#else
16+ #include < cstring>
1617#include < dlfcn.h>
1718#include < unistd.h>
1819#endif
1920
2021#ifdef WIN32
21- #define DYNANAMIC_EXPORT _declspec (dllexport)
22+ #define DYNAMIC_EXPORT _declspec (dllexport)
23+ #define __T (x ) L ## x
2224#else
23- #define DYNANAMIC_EXPORT __attribute__ ((visibility(" default" )))
25+ #define DYNAMIC_EXPORT __attribute__ ((visibility(" default" )))
26+ #define __T (x ) x
2427#endif
2528
29+ #define _T (x ) __T(x)
30+
2631typedef int (*managed_delegate_executor_fn)(
2732 lua_State * luaState
2833);
@@ -36,19 +41,21 @@ typedef cleanup_function_fn(*managed_main_fn)(
3641 /* Out Param */ managed_delegate_executor_fn* managed_delegate_executor_ptr
3742 );
3843
44+ using tstring = std::basic_string<char_t >;
45+
3946std::ofstream error_log_file;
4047
4148managed_delegate_executor_fn managed_delegate_executor = nullptr ;
4249
4350managed_main_fn managed_main = nullptr ;
4451
45- std::string hosfxr_path = " garrysmod/lua/bin/dotnet/host/fxr/ " + std::string( NET_CORE_VERSION) ;
52+ const auto hosfxr_path = std::filesystem::path(_T( " garrysmod/lua/bin/dotnet/host/fxr" )) / NET_CORE_VERSION;
4653#ifdef WIN32
47- void * hostfxr_library_handle = LoadLibraryA ((hosfxr_path + " / hostfxr.dll" ).c_str());
54+ void * hostfxr_library_handle = LoadLibraryW ((hosfxr_path / _T( " hostfxr.dll" )).make_preferred().wstring( ).c_str());
4855#elif __APPLE__
49- void * hostfxr_library_handle = dlopen((hosfxr_path + " / libhostfxr.dylib" ).c_str(), RTLD_LAZY | RTLD_LOCAL);
56+ void * hostfxr_library_handle = dlopen((hosfxr_path / " libhostfxr.dylib" ).string( ).c_str(), RTLD_LAZY | RTLD_LOCAL);
5057#elif __gnu_linux__
51- void * hostfxr_library_handle = dlopen((hosfxr_path + " / libhostfxr.so" ).c_str(), RTLD_LAZY);
58+ void * hostfxr_library_handle = dlopen((hosfxr_path / " libhostfxr.so" ).string( ).c_str(), RTLD_LAZY);
5259#endif
5360
5461#ifdef WIN32
@@ -150,7 +157,7 @@ void * params_to_managed_code[] = {
150157 reinterpret_cast <void *>(export_push_c_function_safe)
151158};
152159
153- extern " C" DYNANAMIC_EXPORT cleanup_function_fn InitNetRuntime (GarrysMod::Lua::ILuaBase* lua)
160+ extern " C" DYNAMIC_EXPORT cleanup_function_fn InitNetRuntime (GarrysMod::Lua::ILuaBase* lua)
154161{
155162 if (!error_log_file.is_open ())
156163 {
@@ -169,27 +176,22 @@ extern "C" DYNANAMIC_EXPORT cleanup_function_fn InitNetRuntime(GarrysMod::Lua::I
169176
170177 hostfxr_set_error_writer (dotnet_error_writer);
171178
172- #ifdef WIN32
173- const char_t * dotnet_args[2 ] = {L" exec" , L" garrysmod/lua/bin/gmodnet/GmodNET.dll" };
174- #else
175- const char_t * dotnet_args[2 ] = {" exec" , " garrysmod/lua/bin/gmodnet/GmodNET.dll" };
176- #endif
179+ const char_t * dotnet_args[] = {_T (" exec" ), _T (" garrysmod/lua/bin/gmodnet/GmodNET.dll" )};
180+
177181 hostfxr_initialize_parameters dotnet_runtime_params;
178182 dotnet_runtime_params.size = sizeof (hostfxr_initialize_parameters);
183+ tstring game_exe_path (301 , _T (' \0 ' ));
179184#ifdef WIN32
180- char_t game_exe_path[301 ];
181- int game_exe_path_len = GetModuleFileNameW (nullptr , game_exe_path, 300 );
185+ GetModuleFileNameW (nullptr , game_exe_path.data (), game_exe_path.size () - 1 );
182186#else
183- char game_exe_path[301 ];
184- int game_exe_path_len = readlink (" /proc/self/exe" , game_exe_path, 300 );
185- game_exe_path[game_exe_path_len] = ' \0 ' ;
187+ readlink (" /proc/self/exe" , game_exe_path.data (), game_exe_path.size () - 1 );
186188#endif
187- dotnet_runtime_params.host_path = game_exe_path;
189+ dotnet_runtime_params.host_path = game_exe_path. c_str () ;
188190
189191 std::filesystem::path dotnet_root_path = std::filesystem::current_path () / " garrysmod" / " lua" / " bin" / " dotnet" ;
190192 dotnet_runtime_params.dotnet_root = dotnet_root_path.native ().c_str ();
191193
192- int init_success_code = hostfxr_initialize_for_dotnet_command_line (2 , dotnet_args, &dotnet_runtime_params, &runtime_environment_handle);
194+ int init_success_code = hostfxr_initialize_for_dotnet_command_line (std::size (dotnet_args) , dotnet_args, &dotnet_runtime_params, &runtime_environment_handle);
193195 if (init_success_code != 0 )
194196 {
195197 error_log_file << " Unable to initialize dotnet runtime. Error code: " << init_success_code << std::endl;
@@ -212,13 +214,9 @@ extern "C" DYNANAMIC_EXPORT cleanup_function_fn InitNetRuntime(GarrysMod::Lua::I
212214 error_log_file << " get_function_pointer is null" << std::endl;
213215 return nullptr ;
214216 }
215- #ifdef WIN32
216- int get_managed_main_success_code = get_function_pointer (L" GmodNET.Startup, GmodNET" , L" Main" , UNMANAGEDCALLERSONLY_METHOD,
217- nullptr , nullptr , reinterpret_cast <void **>(&managed_main));
218- #else
219- int get_managed_main_success_code = get_function_pointer (" GmodNET.Startup, GmodNET" , " Main" , UNMANAGEDCALLERSONLY_METHOD,
217+
218+ int get_managed_main_success_code = get_function_pointer (_T (" GmodNET.Startup, GmodNET" ), _T (" Main" ), UNMANAGEDCALLERSONLY_METHOD,
220219 nullptr , nullptr , reinterpret_cast <void **>(&managed_main));
221- #endif
222220 if (get_managed_main_success_code != 0 )
223221 {
224222 error_log_file << " Unable to load managed entry point: Error code: " << get_managed_main_success_code << std::endl;
@@ -230,7 +228,8 @@ extern "C" DYNANAMIC_EXPORT cleanup_function_fn InitNetRuntime(GarrysMod::Lua::I
230228 return nullptr ;
231229 }
232230 }
233- return managed_main (lua, std::string (SEM_VERSION).c_str (), std::string (SEM_VERSION).length (), params_to_managed_code,
231+
232+ return managed_main (lua, SEM_VERSION, std::strlen (SEM_VERSION), params_to_managed_code,
234233 native_delegate_executor, &managed_delegate_executor);
235234}
236235
0 commit comments