Skip to content

Commit

Permalink
feat: improve REL::ID not found fail message
Browse files Browse the repository at this point in the history
  • Loading branch information
qudix committed Jun 15, 2024
1 parent b5ad8b8 commit dc0c335
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
21 changes: 1 addition & 20 deletions CommonLibF4/include/REL/IDDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,7 @@ namespace REL
return singleton;
}

[[nodiscard]] std::size_t id2offset(std::uint64_t a_id) const
{
if (_id2offset.empty()) {
stl::report_and_fail("data is empty"sv);
}

const mapping_t elem{ a_id, 0 };
const auto it = std::lower_bound(
_id2offset.begin(),
_id2offset.end(),
elem,
[](auto&& a_lhs, auto&& a_rhs) {
return a_lhs.id < a_rhs.id;
});
if (it == _id2offset.end()) {
stl::report_and_fail("id not found"sv);
}

return static_cast<std::size_t>(it->offset);
}
[[nodiscard]] std::size_t id2offset(std::uint64_t a_id) const;

protected:
friend class Offset2ID;
Expand Down
24 changes: 24 additions & 0 deletions CommonLibF4/src/REL/IDDB.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "REL/IDDB.h"
#include "REL/Module.h"
#include "REL/Version.h"

#include "REX/W32/BCRYPT.h"
Expand Down Expand Up @@ -105,4 +106,27 @@ namespace REL
*reinterpret_cast<const std::uint64_t*>(_mmap.data())
};
}

std::size_t IDDB::id2offset(std::uint64_t a_id) const
{
if (_id2offset.empty()) {
stl::report_and_fail("data is empty"sv);
}

const mapping_t elem{ a_id, 0 };
const auto it = std::lower_bound(
_id2offset.begin(),
_id2offset.end(),
elem,
[](auto&& a_lhs, auto&& a_rhs) {
return a_lhs.id < a_rhs.id;
});
if (it == _id2offset.end()) {
const auto version = Module::get().version();
const auto str = std::format("id {} not found!\ngame version: {}"sv, a_id, version.string());
stl::report_and_fail(str);
}

return static_cast<std::size_t>(it->offset);
}
}

0 comments on commit dc0c335

Please sign in to comment.