Skip to content

Commit 2e365b8

Browse files
authored
Add error messages in places where only error codes were previously logged (e.g. load a C++ mod) (#902)
1 parent 8e08d13 commit 2e365b8

10 files changed

Lines changed: 209 additions & 56 deletions

File tree

UE4SS/proxy_generator/main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <Windows.h>
1616
#include <ImageHlp.h>
1717
#include <tchar.h>
18+
#include <Helpers/String.hpp>
19+
#include <Helpers/Win32Error.hpp>
1820

1921
using namespace RC;
2022
namespace fs = std::filesystem;
@@ -49,7 +51,8 @@ std::vector<ExportFunction> DumpExports(const fs::path& dll_path)
4951

5052
if (export_directory == nullptr)
5153
{
52-
cerr << std::format("Failed to get export directory, reason: {:x}", GetLastError()) << endl;
54+
auto err_msg = to_string(Win32Error(GetLastError()));
55+
cerr << std::format("Failed to get export directory, reason: {}", err_msg) << '\n';
5356
return {};
5457
}
5558

UE4SS/src/CrashDumper.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
#include <string>
33
#include <format>
44
#include <bit>
5-
#include <fmt/chrono.h>
65
#include <UE4SSProgram.hpp>
76
#include <Unreal/Core/Windows/WindowsHWrapper.hpp>
87

98
#include <polyhook2/PE/IatHook.hpp>
109
#include <dbghelp.h>
11-
10+
#include <Helpers/Win32Error.hpp>
1211
#include <String/StringType.hpp>
1312

1413
namespace fs = std::filesystem;
@@ -34,7 +33,7 @@ namespace RC
3433

3534
if (file == INVALID_HANDLE_VALUE)
3635
{
37-
const StringType message = fmt::format(STR("Failed to create crashdump file, reason: {:x}"), GetLastError());
36+
const StringType message = fmt::format(STR("Failed to create crashdump file, reason: {}"), Win32Error(GetLastError()).c_str());
3837
MessageBoxW(NULL, FromCharTypePtr<wchar_t>(message.c_str()), L"Fatal Error!", MB_OK);
3938
return EXCEPTION_CONTINUE_SEARCH;
4039
}
@@ -56,7 +55,7 @@ namespace RC
5655

5756
if (!ok)
5857
{
59-
const StringType message = fmt::format(STR("Failed to write crashdump file, reason: {:x}"), GetLastError());
58+
const StringType message = fmt::format(STR("Failed to write crashdump file, reason: {}"), Win32Error(GetLastError()).c_str());
6059
MessageBoxW(NULL, FromCharTypePtr<wchar_t>(message.c_str()), L"Fatal Error!", MB_OK);
6160
return EXCEPTION_CONTINUE_SEARCH;
6261
}

UE4SS/src/Mod/CppMod.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#define NOMINMAX
22

33
#include <filesystem>
4+
#include <Windows.h>
45

56
#include <DynamicOutput/DynamicOutput.hpp>
7+
#include <Helpers/Win32Error.hpp>
68
#include <Helpers/String.hpp>
79
#include <Mod/CppMod.hpp>
810

9-
#include <Windows.h>
10-
1111
namespace RC
1212
{
1313
CppMod::CppMod(UE4SSProgram& program, StringType&& mod_name, StringType&& mod_path) : Mod(program, std::move(mod_name), std::move(mod_path))
@@ -28,7 +28,8 @@ namespace RC
2828

2929
if (!m_main_dll_module)
3030
{
31-
Output::send<LogLevel::Warning>(STR("Failed to load dll <{}> for mod {}, error code: 0x{:x}\n"), ensure_str(dll_path), m_mod_name, GetLastError());
31+
Output::send<LogLevel::Warning>(STR("Failed to load dll <{}> for mod {}, error: {}\n"),
32+
ensure_str(dll_path), m_mod_name, Win32Error(GetLastError()).c_str());
3233
set_installable(false);
3334
return;
3435
}

assets/Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Added override Lua files for ProcessLocalScriptFunction and ProcessInternal [UE4
4545

4646
Added override Lua files for CallFunctionByNameWithArguments [UE4SS #848](https://github.com/UE4SS-RE/RE-UE4SS/pull/848) - M3C3I
4747

48+
Add error messages in places where only error codes were previously logged (e.g. load a C++ mod) [UE4SS #902](https://github.com/UE4SS-RE/RE-UE4SS/pull/902)
49+
4850
### Live View
4951
Added search filter: `IncludeClassNames`. ([UE4SS #472](https://github.com/UE4SS-RE/RE-UE4SS/pull/472)) - Buckminsterfullerene
5052

deps/first/File/src/FileType/WinFile.cpp

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#define NOMINMAX
88
#include <Windows.h>
9+
#include <Helpers/String.hpp>
10+
#include <Helpers/Win32Error.hpp>
911
#ifdef TEXT
1012
#undef TEXT
1113
#endif
@@ -31,14 +33,16 @@ namespace RC::File
3133
{
3234
if (DeleteFileW(file_path_and_name.wstring().c_str()) == 0)
3335
{
34-
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::delete_file] Was unable to delete file, error: {}", GetLastError()))
36+
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::delete_file] Was unable to delete file, error: {}",
37+
to_string(Win32Error(GetLastError())).c_str()))
3538
}
3639
}
3740
else
3841
{
39-
if (DeleteFileA(file_path_and_name.string().c_str()) != 0)
42+
if (DeleteFileA(file_path_and_name.string().c_str()) == 0)
4043
{
41-
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::delete_file] Was unable to delete file, error: {}", GetLastError()))
44+
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::delete_file] Was unable to delete file, error: {}",
45+
to_string(Win32Error(GetLastError())).c_str()))
4246
}
4347
}
4448
}
@@ -70,7 +74,7 @@ namespace RC::File
7074

7175
auto WinFile::get_raw_handle() noexcept -> void*
7276
{
73-
return static_cast<void*>(m_file);
77+
return m_file;
7478
}
7579

7680
auto WinFile::get_file_path() const noexcept -> const std::filesystem::path&
@@ -99,7 +103,8 @@ namespace RC::File
99103
DWORD bytes_written{};
100104
if (!WriteFile(file.get_file(), data, num_bytes_to_write, &bytes_written, nullptr))
101105
{
102-
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::write_to_file] Tried writing to file but was unable to complete operation. Error: {}", GetLastError()))
106+
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::write_to_file] Tried writing to file but was unable to complete operation. error: {}",
107+
to_string(Win32Error(GetLastError())).c_str()))
103108
}
104109
}
105110

@@ -168,10 +173,8 @@ namespace RC::File
168173
{
169174
return false;
170175
}
171-
else
172-
{
173-
deserialize_identifying_properties();
174-
}
176+
177+
deserialize_identifying_properties();
175178
}
176179

177180
BY_HANDLE_FILE_INFORMATION live_info{};
@@ -292,8 +295,8 @@ namespace RC::File
292295
auto res = ReadFile(cache_file.get_raw_handle(), &m_cache, cache_size, &bytes_read, nullptr);
293296
if (res == 0)
294297
{
295-
THROW_INTERNAL_FILE_ERROR(
296-
fmt::format("[WinFile::get_serialized_item] Tried deserializing file but was unable to complete operation. Error: {}", GetLastError()))
298+
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::get_serialized_item] Tried deserializing file but was unable to complete operation. error: {}",
299+
to_string(Win32Error(GetLastError())).c_str()))
297300
}
298301

299302
cache_file.close();
@@ -329,7 +332,7 @@ namespace RC::File
329332
}
330333
catch (const std::filesystem::filesystem_error& e)
331334
{
332-
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::create_all_directories] Tried creating directories '{}' but encountered an error. Error: {}",
335+
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::create_all_directories] Tried creating directories '{}' but encountered an error. error: {}",
333336
file_name_and_path.string(),
334337
e.what()))
335338
}
@@ -341,7 +344,7 @@ namespace RC::File
341344
{
342345
if (UnmapViewOfFile(m_memory_map) == 0)
343346
{
344-
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::close_file] Was unable to unmap file, error: {}", GetLastError()))
347+
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::close_file] Was unable to unmap file, error: {}", to_string(Win32Error(GetLastError())).c_str()))
345348
}
346349
else
347350
{
@@ -353,7 +356,7 @@ namespace RC::File
353356
{
354357
if (CloseHandle(m_map_handle) == 0)
355358
{
356-
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::close_file] Was unable to close map handle, error: {}", GetLastError()))
359+
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::close_file] Was unable to close map handle, {}", to_string(Win32Error(GetLastError())).c_str()))
357360
}
358361
else
359362
{
@@ -368,7 +371,7 @@ namespace RC::File
368371

369372
if (CloseHandle(m_file) == 0)
370373
{
371-
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::close_file] Was unable to close file, error: {}", GetLastError()))
374+
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::close_file] Was unable to close file, {}", to_string(Win32Error(GetLastError())).c_str()))
372375
}
373376
else
374377
{
@@ -387,7 +390,8 @@ namespace RC::File
387390
WideCharToMultiByte(CP_UTF8, 0, FromCharTypePtr<wchar_t>(string_to_write.data()), static_cast<int>(string_to_write.size()), NULL, 0, NULL, NULL);
388391
if (string_size == 0)
389392
{
390-
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::write_string_to_file] Tried writing string to file but string_size was 0. Error: {}", GetLastError()))
393+
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::write_string_to_file] Tried writing string to file but string_size was 0. {}",
394+
to_string(Win32Error(GetLastError())).c_str()))
391395
}
392396

393397
std::string string_converted_to_utf8(string_size, 0);
@@ -401,7 +405,8 @@ namespace RC::File
401405
NULL) == 0)
402406
{
403407
THROW_INTERNAL_FILE_ERROR(
404-
fmt::format("[WinFile::write_string_to_file] Tried writing string to file but could not convert to utf-8. Error: {}", GetLastError()))
408+
fmt::format("[WinFile::write_string_to_file] Tried writing string to file but could not convert to utf-8. {}",
409+
to_string(Win32Error(GetLastError())).c_str()))
405410
}
406411

407412
write_to_file(*this, string_converted_to_utf8.c_str(), string_size);
@@ -412,13 +417,13 @@ namespace RC::File
412417
BY_HANDLE_FILE_INFORMATION file_info{};
413418
if (GetFileInformationByHandle(m_file, &file_info) == 0)
414419
{
415-
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::is_same_as] Tried retrieving file information by handle. Error: {}", GetLastError()))
420+
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::is_same_as] Tried retrieving file information by handle. {}", to_string(Win32Error(GetLastError())).c_str()))
416421
}
417422

418423
BY_HANDLE_FILE_INFORMATION other_file_info{};
419424
if (GetFileInformationByHandle(other_file.get_file(), &other_file_info) == 0)
420425
{
421-
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::is_same_as] Tried retrieving file information by handle. Error: {}", GetLastError()))
426+
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::is_same_as] Tried retrieving file information by handle. {}", to_string(Win32Error(GetLastError())).c_str()))
422427
}
423428

424429
if (file_info.dwVolumeSerialNumber != other_file_info.dwVolumeSerialNumber)
@@ -496,8 +501,8 @@ namespace RC::File
496501

497502
auto WinFile::memory_map() -> std::span<uint8_t>
498503
{
499-
DWORD handle_desired_access{};
500-
DWORD mapping_desired_access{};
504+
DWORD handle_desired_access, mapping_desired_access;
505+
501506
switch (m_open_properties.open_for)
502507
{
503508
case OpenFor::Writing:
@@ -517,13 +522,15 @@ namespace RC::File
517522
m_map_handle = CreateFileMapping(get_raw_handle(), nullptr, handle_desired_access, 0, 0, nullptr);
518523
if (!m_map_handle)
519524
{
520-
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::memory_map] Tried to memory map file but 'CreateFileMapping' returned error: {}", GetLastError()))
525+
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::memory_map] Tried to memory map file but 'CreateFileMapping' returned {}",
526+
to_string(Win32Error(GetLastError())).c_str()))
521527
}
522528

523529
m_memory_map = static_cast<uint8_t*>(MapViewOfFile(m_map_handle, mapping_desired_access, 0, 0, 0));
524530
if (!m_memory_map)
525531
{
526-
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::memory_map] Tried to memory map file but 'MapViewOfFile' returned error: {}", GetLastError()))
532+
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::memory_map] Tried to memory map file but 'MapViewOfFile' returned {}",
533+
to_string(Win32Error(GetLastError())).c_str()))
527534
}
528535

529536
MEMORY_BASIC_INFORMATION buffer{};
@@ -605,23 +612,8 @@ namespace RC::File
605612
{
606613
std::string_view open_type = open_properties.open_for == OpenFor::Writing || open_properties.open_for == OpenFor::Appending ? "writing" : "reading";
607614

608-
DWORD error = GetLastError();
609-
if (error == 2)
610-
{
611-
throw FileNotFoundException{fmt::format("File not found: {}", file_name_and_path.filename().string())};
612-
}
613-
else if (error == 3)
614-
{
615-
throw FileNotFoundException{fmt::format("Path not found: {}", file_name_and_path.filename().string())};
616-
}
617-
else
618-
{
619-
THROW_INTERNAL_FILE_ERROR(
620-
fmt::format("[WinFile::open_file] Tried opening file for {} but encountered an error. Path & File: {} | GetLastError() = {}\n",
621-
open_type,
622-
file_name_and_path.string(),
623-
error))
624-
}
615+
THROW_INTERNAL_FILE_ERROR(fmt::format("[WinFile::open_file] Tried opening file for {} but encountered an error. Path & File: {} | error: {}\n",
616+
open_type, file_name_and_path.string(), to_string(Win32Error(GetLastError())).c_str()))
625617
}
626618

627619
file.m_file_path_and_name = file_name_and_path;

deps/first/File/xmake.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ target(projectName)
66
set_exceptions("cxx")
77
add_rules("ue4ss.dependency")
88

9-
add_deps("String")
9+
add_deps("String", "Helpers")
1010

1111
add_includedirs("include", { public = true })
1212
add_headerfiles("include/**.hpp")

deps/first/Helpers/include/Helpers/String.hpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,20 @@ namespace RC
175175
}
176176
/* explode_by_occurrence -> END */
177177

178-
auto inline to_wstring(std::string& input) -> std::wstring
178+
auto inline to_wstring(const std::string& input) -> std::wstring
179179
{
180180
#pragma warning(disable : 4996)
181181
static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter{};
182182
return converter.from_bytes(input);
183183
#pragma warning(default : 4996)
184184
}
185185

186+
auto inline to_wstring(const char* pInput)
187+
{
188+
auto temp_input = std::string{pInput};
189+
return to_wstring(temp_input);
190+
}
191+
186192
auto inline to_wstring(std::string_view input) -> std::wstring
187193
{
188194
#ifdef PLATFORM_WINDOWS
@@ -208,12 +214,12 @@ namespace RC
208214
return std::wstring{input};
209215
}
210216

211-
auto inline to_wstring(std::wstring& input) -> std::wstring
217+
auto inline to_wstring(const std::wstring& input) -> std::wstring
212218
{
213219
return std::wstring{input};
214220
}
215221

216-
auto inline to_wstring(std::u16string& input) -> std::wstring
222+
auto inline to_wstring(const std::u16string& input) -> std::wstring
217223
{
218224
#ifdef PLATFORM_WINDOWS
219225
return {input.begin(), input.end()};
@@ -231,14 +237,20 @@ namespace RC
231237
#endif
232238
}
233239

234-
auto inline to_string(std::wstring& input) -> std::string
240+
auto inline to_string(const std::wstring& input) -> std::string
235241
{
236242
#pragma warning(disable : 4996)
237243
static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter{};
238244
return converter.to_bytes(input);
239245
#pragma warning(default : 4996)
240246
}
241247

248+
auto inline to_string(const wchar_t* pInput)
249+
{
250+
auto temp_input = std::wstring{pInput};
251+
return to_string(temp_input);
252+
}
253+
242254
auto inline to_string(std::wstring_view input) -> std::string
243255
{
244256
auto temp_input = std::wstring{input};
@@ -260,7 +272,7 @@ namespace RC
260272
#endif
261273
}
262274

263-
auto inline to_u16string(std::wstring& input) -> std::u16string
275+
auto inline to_u16string(const std::wstring& input) -> std::u16string
264276
{
265277
return {input.begin(), input.end()};
266278
}
@@ -271,7 +283,7 @@ namespace RC
271283
return to_u16string(temp_input);
272284
}
273285

274-
auto inline to_u16string(std::string& input) -> std::u16string
286+
auto inline to_u16string(const std::string& input) -> std::u16string
275287
{
276288
return {input.begin(), input.end()};
277289
}

0 commit comments

Comments
 (0)