summaryrefslogtreecommitdiff
path: root/src/port/resource/importers/GenericArrayFactory.cpp
blob: 7389c0600d1163762a6f7f97e9a5b4bc7e977590 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "GenericArrayFactory.h"
#include "../type/GenericArray.h"
#include "spdlog/spdlog.h"

namespace SF64 {
std::shared_ptr<Ship::IResource> ResourceFactoryBinaryGenericArrayV0::ReadResource(std::shared_ptr<Ship::File> file,
                                                                                   std::shared_ptr<Ship::ResourceInitData> initData) {
    if (!FileHasValidFormatAndReader(file, initData)) {
        return nullptr;
    }

    auto arr = std::make_shared<GenericArray>(initData);
    auto reader = std::get<std::shared_ptr<Ship::BinaryReader>>(file->Reader);

    auto type = reader->ReadUInt32();

    SPDLOG_INFO("GenericArray Type Num: {}", type);

    auto count = reader->ReadUInt32();

    SPDLOG_INFO("GenericArray Count: {}", count);

    for (uint32_t i = 0; i < count; i++) {
        switch (static_cast<ArrayType>(type)) {
            case ArrayType::u8: {
                auto x = reader->ReadUByte();
                arr->mData.push_back(x);
                break;
            }
            case ArrayType::s8: {
                auto x = reader->ReadInt8();
                arr->mData.push_back(x);
                break;
            }
            case ArrayType::u16: {
                auto x = reader->ReadUInt16();
                std::copy_n(reinterpret_cast<uint8_t*>(&x), 2, std::back_inserter(arr->mData));
                break;
            }
            case ArrayType::s16: {
                auto x = reader->ReadInt16();
                std::copy_n(reinterpret_cast<uint8_t*>(&x), 2, std::back_inserter(arr->mData));
                break;
            }
            case ArrayType::u32: {
                auto x = reader->ReadUInt32();
                std::copy_n(reinterpret_cast<uint8_t*>(&x), 4, std::back_inserter(arr->mData));
                break;
            }
            case ArrayType::s32: {
                auto x = reader->ReadInt32();
                std::copy_n(reinterpret_cast<uint8_t*>(&x), 4, std::back_inserter(arr->mData));
                break;
            }
            case ArrayType::u64: {
                auto x = reader->ReadUInt64();
                std::copy_n(reinterpret_cast<uint8_t*>(&x), 8, std::back_inserter(arr->mData));
                break;
            }
            case ArrayType::f32: {
                auto x = reader->ReadFloat();
                std::copy_n(reinterpret_cast<uint8_t*>(&x), 4, std::back_inserter(arr->mData));
                break;
            }
            case ArrayType::f64: {
                auto x = reader->ReadDouble();
                std::copy_n(reinterpret_cast<uint8_t*>(&x), 8, std::back_inserter(arr->mData));
                break;
            }
            case ArrayType::Vec2f: {
                Vec2f vec(reader->ReadFloat(), reader->ReadFloat());
                std::copy_n(reinterpret_cast<uint8_t*>(&vec), sizeof(Vec2f), std::back_inserter(arr->mData));
                break;
            }
            case ArrayType::Vec3f: {
                Vec3f vec(reader->ReadFloat(), reader->ReadFloat(), reader->ReadFloat());
                std::copy_n(reinterpret_cast<uint8_t*>(&vec), sizeof(Vec3f), std::back_inserter(arr->mData));
                break;
            }
            case ArrayType::Vec3s: {
                Vec3s vec(reader->ReadInt16(), reader->ReadInt16(), reader->ReadInt16());
                std::copy_n(reinterpret_cast<uint8_t*>(&vec), sizeof(Vec3s), std::back_inserter(arr->mData));
                break;
            }
            case ArrayType::Vec3i: {
                Vec3i vec(reader->ReadInt32(), reader->ReadInt32(), reader->ReadInt32());
                std::copy_n(reinterpret_cast<uint8_t*>(&vec), sizeof(Vec3i), std::back_inserter(arr->mData));
                break;
            }
            case ArrayType::Vec3iu: {
                Vec3iu vec(reader->ReadUInt32(), reader->ReadUInt32(), reader->ReadUInt32());
                std::copy_n(reinterpret_cast<uint8_t*>(&vec), sizeof(Vec3iu), std::back_inserter(arr->mData));
                break;
            }
            case ArrayType::Vec4f: {
                Vec4f vec(reader->ReadFloat(), reader->ReadFloat(), reader->ReadFloat(), reader->ReadFloat());
                std::copy_n(reinterpret_cast<uint8_t*>(&vec), sizeof(Vec4f), std::back_inserter(arr->mData));
                break;
            }
            case ArrayType::Vec4s: {
                Vec4s vec(reader->ReadInt16(), reader->ReadInt16(), reader->ReadInt16(), reader->ReadInt16());
                std::copy_n(reinterpret_cast<uint8_t*>(&vec), sizeof(Vec4s), std::back_inserter(arr->mData));
                break;
            }
        }
    }

    return arr;
}
} // namespace LUS