Skip to content

Commit f5dec89

Browse files
authored
Merge pull request #102 from Stat1cV01D/cpp_unicode
Minor string-related and style fixes
2 parents bf5e6fd + 8e84395 commit f5dec89

File tree

3 files changed

+88
-132
lines changed

3 files changed

+88
-132
lines changed

gm_dotnet_native/dotnethelper-src/LuaAPIExposure.cpp

Lines changed: 30 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,7 @@ void export_set_metatable(ILuaBase * lua, int iStackPos)
5050

5151
int export_get_metatable(ILuaBase * lua, int iStackPos)
5252
{
53-
bool tmp_ret = lua->GetMetaTable(iStackPos);
54-
55-
if(tmp_ret)
56-
{
57-
return 1;
58-
}
59-
else
60-
{
61-
return 0;
62-
}
53+
return static_cast<int>(lua->GetMetaTable(iStackPos));
6354
}
6455

6556
void export_call(ILuaBase * lua, int IArgs, int iResults)
@@ -124,16 +115,7 @@ double export_get_number(ILuaBase * lua, int iStackPos)
124115

125116
int export_get_bool (ILuaBase * lua, int iStackPos)
126117
{
127-
bool tmp_ret = lua->GetBool(iStackPos);
128-
129-
if(tmp_ret)
130-
{
131-
return 1;
132-
}
133-
else
134-
{
135-
return 0;
136-
}
118+
return static_cast<int>(lua->GetBool(iStackPos));
137119
}
138120

139121
CFunc export_get_c_function(ILuaBase * lua, int iStackPos)
@@ -158,17 +140,7 @@ void export_push_number(ILuaBase * lua, double val)
158140

159141
void export_push_bool(ILuaBase * lua, int val)
160142
{
161-
bool tmp;
162-
if(val == 0)
163-
{
164-
tmp = false;
165-
}
166-
else
167-
{
168-
tmp = true;
169-
}
170-
171-
lua->PushBool(tmp);
143+
lua->PushBool(static_cast<bool>(val));
172144
}
173145

174146
void export_push_c_function(ILuaBase * lua, CFunc val)
@@ -203,16 +175,7 @@ void export_push_special(ILuaBase * lua, int table_type_number)
203175

204176
int export_is_type(ILuaBase * lua, int iStackPos, int iType)
205177
{
206-
bool tmp = lua->IsType(iStackPos, iType);
207-
208-
if(tmp)
209-
{
210-
return 1;
211-
}
212-
else
213-
{
214-
return 0;
215-
}
178+
return static_cast<int>(lua->IsType(iStackPos, iType));
216179
}
217180

218181
int export_get_type(ILuaBase * lua, int iStackPos)
@@ -222,10 +185,9 @@ int export_get_type(ILuaBase * lua, int iStackPos)
222185

223186
const char * export_get_type_name(ILuaBase * lua, int iType, int * out_name_len)
224187
{
225-
const char * ptr = lua->GetTypeName(iType);
226-
int tmp_len = strlen(ptr);
227-
*out_name_len = tmp_len;
228-
return ptr;
188+
const char * name = lua->GetTypeName(iType);
189+
*out_name_len = static_cast<int>(strlen(name));
190+
return name;
229191
}
230192

231193
int export_obj_len(ILuaBase * lua, int iStackPos)
@@ -235,42 +197,42 @@ int export_obj_len(ILuaBase * lua, int iStackPos)
235197

236198
void export_get_angle(ILuaBase * lua, float * out_angle_components, int iStackPos)
237199
{
238-
const QAngle& tmp = lua->GetAngle(iStackPos);
200+
const QAngle& angle = lua->GetAngle(iStackPos);
239201

240-
out_angle_components[0] = tmp.x;
241-
out_angle_components[1] = tmp.y;
242-
out_angle_components[2] = tmp.z;
202+
out_angle_components[0] = angle.x;
203+
out_angle_components[1] = angle.y;
204+
out_angle_components[2] = angle.z;
243205
}
244206

245207
void export_get_vector(ILuaBase * lua, float * out_vector_components, int iStackPos)
246208
{
247-
const Vector& tmp = lua->GetVector(iStackPos);
209+
const Vector& vector = lua->GetVector(iStackPos);
248210

249-
out_vector_components[0] = tmp.x;
250-
out_vector_components[1] = tmp.y;
251-
out_vector_components[2] = tmp.z;
211+
out_vector_components[0] = vector.x;
212+
out_vector_components[1] = vector.y;
213+
out_vector_components[2] = vector.z;
252214
}
253215

254216
void export_push_angle(ILuaBase * lua, float x, float y, float z)
255217
{
256-
QAngle tmp;
218+
QAngle angle;
257219

258-
tmp.x = x;
259-
tmp.y = y;
260-
tmp.z = z;
220+
angle.x = x;
221+
angle.y = y;
222+
angle.z = z;
261223

262-
lua->PushAngle(tmp);
224+
lua->PushAngle(angle);
263225
}
264226

265227
void export_push_vector(ILuaBase * lua, float x, float y, float z)
266228
{
267-
Vector tmp;
229+
Vector vector;
268230

269-
tmp.x = x;
270-
tmp.y = y;
271-
tmp.z = z;
231+
vector.x = x;
232+
vector.y = y;
233+
vector.z = z;
272234

273-
lua->PushVector(tmp);
235+
lua->PushVector(vector);
274236
}
275237

276238
void export_set_state(ILuaBase * lua, lua_State * state)
@@ -285,16 +247,7 @@ int export_create_metatable(ILuaBase * lua, const char * name)
285247

286248
int export_push_metatable(ILuaBase * lua, int iType)
287249
{
288-
bool tmp = lua->PushMetaTable(iType);
289-
290-
if(tmp)
291-
{
292-
return 1;
293-
}
294-
else
295-
{
296-
return 0;
297-
}
250+
return static_cast<int>(lua->PushMetaTable(iType));
298251
}
299252

300253
void export_push_user_type(ILuaBase * lua, void * data, int iType)
@@ -344,16 +297,14 @@ void export_push_user_data(ILuaBase * lua, void * data)
344297

345298
const char * export_check_string(ILuaBase * lua, int iStackPos, int * output_string_length)
346299
{
347-
const char * tmp = lua->CheckString(iStackPos);
300+
const char * str = lua->CheckString(iStackPos);
348301

349-
if(tmp == nullptr)
302+
if(str != nullptr)
350303
{
351-
return nullptr;
304+
*output_string_length = static_cast<int>(strlen(str));
352305
}
353306

354-
*output_string_length = strlen(tmp);
355-
356-
return tmp;
307+
return str;
357308
}
358309

359310
double export_check_number(ILuaBase * lua, int iStackPos)

gm_dotnet_native/dotnethelper-src/dotnethelper.cpp

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,21 @@
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+
2631
typedef int (*managed_delegate_executor_fn)(
2732
lua_State * luaState
2833
);
@@ -36,40 +41,37 @@ 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+
3946
std::ofstream error_log_file;
4047

4148
managed_delegate_executor_fn managed_delegate_executor = nullptr;
4249

4350
managed_main_fn managed_main = nullptr;
4451

45-
std::string hosfxr_path = "garrysmod/lua/bin/dotnet/host/fxr/" + std::string(NET_CORE_VERSION);
52+
const std::filesystem::path lua_bin_folder = _T("garrysmod/lua/bin");
53+
const std::filesystem::path hostfxr_path = (lua_bin_folder / _T("dotnet/host/fxr") / NET_CORE_VERSION).make_preferred();
4654
#ifdef WIN32
47-
void* hostfxr_library_handle = LoadLibraryA((hosfxr_path + "/hostfxr.dll").c_str());
55+
HMODULE hostfxr_library_handle = LoadLibraryW((hostfxr_path / _T("hostfxr.dll")).c_str());
4856
#elif __APPLE__
49-
void* hostfxr_library_handle = dlopen((hosfxr_path + "/libhostfxr.dylib").c_str(), RTLD_LAZY | RTLD_LOCAL);
57+
void* hostfxr_library_handle = dlopen((hostfxr_path / "libhostfxr.dylib").c_str(), RTLD_LAZY | RTLD_LOCAL);
5058
#elif __gnu_linux__
51-
void* hostfxr_library_handle = dlopen((hosfxr_path + "/libhostfxr.so").c_str(), RTLD_LAZY);
59+
void* hostfxr_library_handle = dlopen((hostfxr_path / "libhostfxr.so").c_str(), RTLD_LAZY);
5260
#endif
5361

62+
template<typename T>
63+
bool LoadFunction(const char* function_name, T& out_func)
64+
{
5465
#ifdef WIN32
55-
hostfxr_initialize_for_dotnet_command_line_fn hostfxr_initialize_for_dotnet_command_line =
56-
reinterpret_cast<hostfxr_initialize_for_dotnet_command_line_fn>(GetProcAddress(static_cast<HMODULE>(hostfxr_library_handle),
57-
"hostfxr_initialize_for_dotnet_command_line"));
58-
hostfxr_get_runtime_delegate_fn hostfxr_get_runtime_delegate =
59-
reinterpret_cast<hostfxr_get_runtime_delegate_fn>(GetProcAddress(static_cast<HMODULE>(hostfxr_library_handle),
60-
"hostfxr_get_runtime_delegate"));
61-
62-
hostfxr_set_error_writer_fn hostfxr_set_error_writer =
63-
reinterpret_cast<hostfxr_set_error_writer_fn>(GetProcAddress(static_cast<HMODULE>(hostfxr_library_handle),
64-
"hostfxr_set_error_writer"));
66+
out_func = reinterpret_cast<T>(GetProcAddress(hostfxr_library_handle, function_name));
6567
#else
66-
hostfxr_initialize_for_dotnet_command_line_fn hostfxr_initialize_for_dotnet_command_line =
67-
reinterpret_cast<hostfxr_initialize_for_dotnet_command_line_fn>(dlsym(hostfxr_library_handle, "hostfxr_initialize_for_dotnet_command_line"));
68-
hostfxr_get_runtime_delegate_fn hostfxr_get_runtime_delegate =
69-
reinterpret_cast<hostfxr_get_runtime_delegate_fn>(dlsym(hostfxr_library_handle, "hostfxr_get_runtime_delegate"));
70-
hostfxr_set_error_writer_fn hostfxr_set_error_writer =
71-
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));
7269
#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;
7375

7476
void HOSTFXR_CALLTYPE dotnet_error_writer(const char_t *message)
7577
{
@@ -150,7 +152,7 @@ void * params_to_managed_code[] = {
150152
reinterpret_cast<void*>(export_push_c_function_safe)
151153
};
152154

153-
extern "C" DYNANAMIC_EXPORT cleanup_function_fn InitNetRuntime(GarrysMod::Lua::ILuaBase* lua)
155+
extern "C" DYNAMIC_EXPORT cleanup_function_fn InitNetRuntime(GarrysMod::Lua::ILuaBase* lua)
154156
{
155157
if(!error_log_file.is_open())
156158
{
@@ -159,7 +161,9 @@ extern "C" DYNANAMIC_EXPORT cleanup_function_fn InitNetRuntime(GarrysMod::Lua::I
159161

160162
if(managed_main == nullptr)
161163
{
162-
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)))
163167
{
164168
error_log_file << "Unable to load hostfxr library" << std::endl;
165169
return nullptr;
@@ -169,27 +173,24 @@ extern "C" DYNANAMIC_EXPORT cleanup_function_fn InitNetRuntime(GarrysMod::Lua::I
169173

170174
hostfxr_set_error_writer(dotnet_error_writer);
171175

176+
const auto gmodnet_dll_relative_path = lua_bin_folder / _T("gmodnet/GmodNET.dll");
177+
const auto dotnet_root_path = (std::filesystem::current_path() / lua_bin_folder / "dotnet").make_preferred();
178+
179+
const char_t* dotnet_args[] = {_T("exec"), gmodnet_dll_relative_path.c_str()};
180+
181+
tstring game_exe_path(301, _T('\0'));
172182
#ifdef WIN32
173-
const char_t* dotnet_args[2] = {L"exec", L"garrysmod/lua/bin/gmodnet/GmodNET.dll"};
183+
GetModuleFileNameW(nullptr, game_exe_path.data(), static_cast<DWORD>(game_exe_path.size()) - 1);
174184
#else
175-
const char_t* dotnet_args[2] = {"exec", "garrysmod/lua/bin/gmodnet/GmodNET.dll"};
185+
readlink("/proc/self/exe", game_exe_path.data(), game_exe_path.size() - 1);
176186
#endif
177187
hostfxr_initialize_parameters dotnet_runtime_params;
178188
dotnet_runtime_params.size = sizeof(hostfxr_initialize_parameters);
179-
#ifdef WIN32
180-
char_t game_exe_path[301];
181-
int game_exe_path_len = GetModuleFileNameW(nullptr, game_exe_path, 300);
182-
#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';
186-
#endif
187-
dotnet_runtime_params.host_path = game_exe_path;
189+
dotnet_runtime_params.host_path = game_exe_path.c_str();
190+
dotnet_runtime_params.dotnet_root = dotnet_root_path.c_str();
188191

189-
std::filesystem::path dotnet_root_path = std::filesystem::current_path() / "garrysmod" / "lua" / "bin" / "dotnet";
190-
dotnet_runtime_params.dotnet_root = dotnet_root_path.native().c_str();
191-
192-
int init_success_code = hostfxr_initialize_for_dotnet_command_line(2, 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);
193194
if(init_success_code != 0)
194195
{
195196
error_log_file << "Unable to initialize dotnet runtime. Error code: " << init_success_code << std::endl;
@@ -212,13 +213,9 @@ extern "C" DYNANAMIC_EXPORT cleanup_function_fn InitNetRuntime(GarrysMod::Lua::I
212213
error_log_file << "get_function_pointer is null" << std::endl;
213214
return nullptr;
214215
}
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,
216+
217+
int get_managed_main_success_code = get_function_pointer(_T("GmodNET.Startup, GmodNET"), _T("Main"), UNMANAGEDCALLERSONLY_METHOD,
220218
nullptr, nullptr, reinterpret_cast<void**>(&managed_main));
221-
#endif
222219
if(get_managed_main_success_code != 0)
223220
{
224221
error_log_file << "Unable to load managed entry point: Error code: " << get_managed_main_success_code << std::endl;
@@ -230,7 +227,8 @@ extern "C" DYNANAMIC_EXPORT cleanup_function_fn InitNetRuntime(GarrysMod::Lua::I
230227
return nullptr;
231228
}
232229
}
233-
return managed_main(lua, std::string(SEM_VERSION).c_str(), std::string(SEM_VERSION).length(), params_to_managed_code,
230+
231+
return managed_main(lua, SEM_VERSION, static_cast<int>(std::strlen(SEM_VERSION)), params_to_managed_code,
234232
native_delegate_executor, &managed_delegate_executor);
235233
}
236234

0 commit comments

Comments
 (0)