summaryrefslogtreecommitdiff
path: root/src/factories/bk64/BKResource.h
blob: f77ca6dc0987c3ec6a9718207a6a67536fb04b68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma once

#include "endianness.h"

#include <cstddef>
#include <cstdint>
#include <stdexcept>
#include <vector>

namespace BK64 {

// Reads the fields after a resource's 64-byte header, swapping when the header names the other byte order. Throws
// when it runs out.
class ResourceReader {
  public:
    explicit ResourceReader(const std::vector<uint8_t>& bytes) : mBytes(bytes) {
        if (bytes.size() < 0x40) {
            throw std::runtime_error("the resource is under 64 bytes");
        }
        mSwap = (bytes[0] == (uint8_t)Torch::Endianness::Big) != (Torch::Endianness::Native == Torch::Endianness::Big);
    }

    template <typename T> void Read(T& value) {
        if (mBytes.size() - mAt < sizeof(T)) {
            throw std::runtime_error("the resource ends early");
        }
        for (size_t i = 0; i < sizeof(T); i++) {
            ((uint8_t*)&value)[mSwap ? sizeof(T) - 1 - i : i] = mBytes[mAt + i];
        }
        mAt += sizeof(T);
    }

    template <typename T> T Read() {
        T value;
        Read(value);
        return value;
    }

    template <typename T> void ReadArray(T* values, size_t count) {
        for (size_t i = 0; i < count; i++) {
            Read(values[i]);
        }
    }

    std::vector<uint8_t> Bytes(size_t size) {
        if (mBytes.size() - mAt < size) {
            throw std::runtime_error("the resource ends early");
        }
        mAt += size;
        return std::vector<uint8_t>(mBytes.begin() + (mAt - size), mBytes.begin() + mAt);
    }

  private:
    const std::vector<uint8_t>& mBytes;
    size_t mAt = 0x40;
    bool mSwap = false;
};

} // namespace BK64