Skip to content

Commit 759eee6

Browse files
committed
Warning fixes, refactoring
1 parent 76762d7 commit 759eee6

File tree

2 files changed

+59
-109
lines changed

2 files changed

+59
-109
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: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -49,34 +49,29 @@ managed_delegate_executor_fn managed_delegate_executor = nullptr;
4949

5050
managed_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

8176
void 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

Comments
 (0)