Torch
Loading...
Searching...
No Matches
BKAssetTable.h
1#pragma once
2
3#include <algorithm>
4#include <cstddef>
5#include <cstdint>
6#include <vector>
7
8namespace BK64 {
9
10class SlotSizer {
11 public:
12 SlotSizer(const std::vector<uint32_t>& offsets, uint32_t dataStart, size_t romSize)
13 : mStray(offsets.size(), false), mDataStart(dataStart), mRomSize(romSize) {
14 mBoundaries.reserve(offsets.size());
15 uint32_t high = 0;
16 for (size_t i = 0; i < offsets.size(); i++) {
17 if (offsets[i] < high) {
18 mStray[i] = true;
19 mStrayCount++;
20 continue;
21 }
22 high = offsets[i];
23 mBoundaries.push_back(offsets[i]);
24 }
25 std::sort(mBoundaries.begin(), mBoundaries.end());
26 mBoundaries.erase(std::unique(mBoundaries.begin(), mBoundaries.end()), mBoundaries.end());
27 mRegionEnd = mBoundaries.empty() ? 0 : mBoundaries.back();
28 }
29
30 uint32_t operator()(uint32_t off) const {
31 if (off >= mRegionEnd) {
32 return 0;
33 }
34 const auto next = std::upper_bound(mBoundaries.begin(), mBoundaries.end(), off);
35 const uint32_t end = std::min(next != mBoundaries.end() ? *next : mRegionEnd, mRegionEnd);
36 if (end <= off) {
37 return 0;
38 }
39 const uint64_t start = static_cast<uint64_t>(mDataStart) + off;
40 if (start >= mRomSize) {
41 return 0;
42 }
43 return static_cast<uint32_t>(std::min<uint64_t>(end - off, mRomSize - start));
44 }
45
46 bool IsStray(size_t index) const {
47 return index < mStray.size() && mStray[index];
48 }
49
50 size_t StrayCount() const {
51 return mStrayCount;
52 }
53
54 uint32_t RegionEnd() const {
55 return mRegionEnd;
56 }
57
58 private:
59 std::vector<uint32_t> mBoundaries;
60 std::vector<bool> mStray;
61 size_t mStrayCount = 0;
62 uint32_t mRegionEnd = 0;
63 uint32_t mDataStart;
64 size_t mRomSize;
65};
66
67} // namespace BK64