Skip to content

Commit e9e8619

Browse files
committed
feat: improve error messages
1 parent 4487768 commit e9e8619

File tree

6 files changed

+95
-21
lines changed

6 files changed

+95
-21
lines changed

include/REL/IDDB.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ namespace REL
7979
_stream(a_filename.data(), a_mode)
8080
{
8181
if (!_stream.is_open()) {
82-
stl::report_and_fail("failed to open address library file"sv);
82+
const auto str = std::format(
83+
L"Failed to open Address Library file!\nPath: {}"sv,
84+
a_filename);
85+
stl::report_and_fail(str);
8386
}
8487

8588
_stream.exceptions(std::ios::badbit | std::ios::failbit | std::ios::eofbit);

include/REL/Offset2ID.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace REL
3333
[[nodiscard]] std::uint64_t operator()(std::size_t a_offset) const
3434
{
3535
if (_offset2id.empty()) {
36-
stl::report_and_fail("No Address Library has been loaded."sv);
36+
stl::report_and_fail("No Address Library has been loaded!"sv);
3737
}
3838

3939
const IDDB::mapping_t elem{ 0, a_offset };
@@ -45,7 +45,14 @@ namespace REL
4545
return a_lhs.offset < a_rhs.offset;
4646
});
4747
if (it == _offset2id.end()) {
48-
stl::report_and_fail("offset not found"sv);
48+
const auto mod = Module::GetSingleton();
49+
const auto version = mod->version();
50+
const auto str = std::format(
51+
"Failed to find Address Library ID for offset!\n"
52+
"Invalid offset: 0x{:08X}\n"
53+
"Game Version: {}"sv,
54+
a_offset, version.string());
55+
stl::report_and_fail(str);
4956
}
5057

5158
return it->id;

include/REL/Pattern.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,15 @@ namespace REL
139139
const noexcept
140140
{
141141
if (!this->match(a_address)) {
142-
const auto version = Module::get().version();
142+
const auto mod = Module::GetSingleton();
143+
const auto version = mod->version();
143144
stl::report_and_fail(
144145
std::format(
145146
"A pattern has failed to match.\n"
146147
"This means the plugin is incompatible with either the "
147-
"current version of the game ({}.{}.{}), or another "
148+
"current version of the game ({}), or another "
148149
"installed mod."sv,
149-
version[0],
150-
version[1],
151-
version[2]),
150+
version.string()),
152151
a_loc);
153152
}
154153
}

include/REX/Impl/PCH.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,68 @@ namespace XSE
543543
REX::W32::TerminateProcess(REX::W32::GetCurrentProcess(), EXIT_FAILURE);
544544
}
545545

546+
[[noreturn]] inline void report_and_fail(std::wstring_view a_msg, std::source_location a_loc = std::source_location::current())
547+
{
548+
const auto body = [&]() {
549+
constexpr std::array directories{
550+
L"include/"sv,
551+
L"src/"sv,
552+
};
553+
554+
const std::filesystem::path p = a_loc.file_name();
555+
const auto filename = p.generic_wstring();
556+
std::wstring_view fileview = filename;
557+
558+
constexpr auto npos = std::wstring::npos;
559+
std::size_t pos = npos;
560+
std::size_t off = 0;
561+
for (const auto& dir : directories) {
562+
pos = fileview.find(dir);
563+
if (pos != npos) {
564+
off = dir.length();
565+
break;
566+
}
567+
}
568+
569+
if (pos != npos) {
570+
fileview = fileview.substr(pos + off);
571+
}
572+
573+
return std::format(L"{}({}): {}", fileview, a_loc.line(), a_msg);
574+
}();
575+
576+
const auto caption = []() -> std::wstring {
577+
std::vector<char> buf;
578+
buf.reserve(REX::W32::MAX_PATH);
579+
buf.resize(REX::W32::MAX_PATH / 2);
580+
std::uint32_t result = 0;
581+
do {
582+
buf.resize(buf.size() * 2);
583+
result = REX::W32::GetModuleFileNameA(
584+
REX::W32::GetCurrentModule(),
585+
buf.data(),
586+
static_cast<std::uint32_t>(buf.size()));
587+
} while (result && result == buf.size() && buf.size() <= std::numeric_limits<std::uint32_t>::max());
588+
589+
if (result && result != buf.size()) {
590+
std::filesystem::path p(buf.begin(), buf.begin() + result);
591+
return p.filename().wstring();
592+
} else {
593+
return {};
594+
}
595+
}();
596+
597+
spdlog::log(
598+
spdlog::source_loc{
599+
a_loc.file_name(),
600+
static_cast<int>(a_loc.line()),
601+
a_loc.function_name() },
602+
spdlog::level::critical,
603+
a_msg);
604+
REX::W32::MessageBoxW(nullptr, body.c_str(), (caption.empty() ? nullptr : caption.c_str()), 0);
605+
REX::W32::TerminateProcess(REX::W32::GetCurrentProcess(), EXIT_FAILURE);
606+
}
607+
546608
template <class To, class From>
547609
[[nodiscard]] To unrestricted_cast(From a_from)
548610
{

src/REL/IDDB.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace REL
2121
const auto mod = Module::GetSingleton();
2222
const auto version = mod->version();
2323
const auto str = std::format(
24-
"Failed to find Address Library ID!\n"
24+
"Failed to find offset for Address Library ID!\n"
2525
"Invalid ID: {}\n"
2626
"Game Version: {}"sv,
2727
a_id, version.string());
@@ -164,10 +164,9 @@ namespace REL
164164
if (header.version() != mod->version()) {
165165
const auto str = std::format(
166166
"Address Library version mismatch!\n"
167-
"Expected Version: {}\n",
167+
"Expected Version: {}\n"
168168
"Actual Version: {}"sv,
169-
mod->version().string(),
170-
header.version().string());
169+
mod->version().string(), header.version().string());
171170
stl::report_and_fail(str);
172171
}
173172

@@ -187,7 +186,10 @@ namespace REL
187186
stl::report_and_fail("Failed to create shared mapping!"sv);
188187
}
189188
} catch (const std::system_error&) {
190-
stl::report_and_fail("Failed to find Address Library file!"sv);
189+
const auto str = std::format(
190+
L"Failed to open Address Library file!\nPath: {}"sv,
191+
filename);
192+
stl::report_and_fail(str);
191193
}
192194
}
193195

@@ -229,7 +231,7 @@ namespace REL
229231
id = a_in.readout<std::uint32_t>();
230232
break;
231233
default:
232-
stl::report_and_fail("Unhandled type while loading Address Library."sv);
234+
stl::report_and_fail("Unhandled type while loading Address Library!"sv);
233235
}
234236

235237
const std::uint64_t tmp = (hi & 8) != 0 ? (prevOffset / a_header.pointer_size()) : prevOffset;
@@ -260,7 +262,7 @@ namespace REL
260262
offset = a_in.readout<std::uint32_t>();
261263
break;
262264
default:
263-
stl::report_and_fail("Unhandled type while loading Address Library."sv);
265+
stl::report_and_fail("Unhandled type while loading Address Library!"sv);
264266
}
265267

266268
if ((hi & 8) != 0) {
@@ -285,7 +287,8 @@ namespace REL
285287
const auto version = mod->version().string("-"sv);
286288
const auto path = std::vformat(a_filename, std::make_format_args(version));
287289
if (!_mmap.open(path)) {
288-
stl::report_and_fail(std::format("failed to open: {}", path));
290+
const auto str = std::format("Failed to open Address Library file!\nPath: {}"sv, path);
291+
stl::report_and_fail(str);
289292
}
290293

291294
_id2offset = std::span{

xmake.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,31 @@ add_rules("mode.debug", "mode.releasedbg")
1313
-- add options
1414
option("rex_ini", function()
1515
set_default(false)
16-
set_description("enable ini config support for REX")
16+
set_description("enable REX::INI settings support")
1717
add_defines("REX_OPTION_INI=1")
1818
end)
1919

2020
option("rex_json", function()
2121
set_default(false)
22-
set_description("enable json config support for REX")
22+
set_description("enable REX::JSON settings support")
2323
add_defines("REX_OPTION_JSON=1")
2424
end)
2525

2626
option("rex_toml", function()
2727
set_default(false)
28-
set_description("enable toml config support for REX")
28+
set_description("enable REX::TOML settings support")
2929
add_defines("REX_OPTION_TOML=1")
3030
end)
3131

3232
option("xse_mmio", function()
3333
set_default(false)
34-
set_description("enable mmio based address library support")
34+
set_description("enable mmio based Address Library support")
3535
add_defines("XSE_MMIO_ADDRESSLIB=1")
3636
end)
3737

3838
option("xse_xbyak", function()
3939
set_default(false)
40-
set_description("enable xbyak support for trampoline")
40+
set_description("enable xbyak support for Trampoline")
4141
add_defines("XSE_SUPPORT_XBYAK=1")
4242
end)
4343

0 commit comments

Comments
 (0)