summaryrefslogtreecommitdiff
path: root/mm/2s2h/resource/importer/KeyFrameFactory.cpp
blob: b0ac93ffb471e4228493bc70b884fcee66ac8f44 (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
111
112
113
114
#include "2s2h/resource/importer/KeyFrameFactory.h"
#include "2s2h/resource/type/KeyFrame.h"
#include <ship/Context.h>
#include <ship/resource/ResourceManager.h>

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

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

    skel->skelData.limbCount = reader->ReadUByte();
    skel->skelData.dListCount = reader->ReadUByte();
    KeyframeSkelType skelType = (KeyframeSkelType)reader->ReadUByte();
    uint8_t numLimbs = reader->ReadUByte();

    if (skelType == KeyframeSkelType::Normal) {
        KeyFrameStandardLimb* limbs = new KeyFrameStandardLimb[numLimbs];

        for (uint32_t i = 0; i < numLimbs; i++) {
            std::string dlStr = reader->ReadString();
            auto dl = Ship::Context::GetInstance()->GetResourceManager()->LoadResourceProcess(dlStr.c_str());
            limbs[i].dList = nullptr;
            if (dl != nullptr) {
                limbs[i].dList = dl->GetRawPointer();
            }
            limbs[i].numChildren = reader->ReadUByte();
            limbs[i].flags = reader->ReadUByte();
            limbs[i].translation.x = reader->ReadInt16();
            limbs[i].translation.y = reader->ReadInt16();
            limbs[i].translation.z = reader->ReadInt16();
        }
        skel->skelData.limbsStandard = limbs;
    } else {
        KeyFrameFlexLimb* limbs = new KeyFrameFlexLimb[numLimbs];

        for (uint32_t i = 0; i < numLimbs; i++) {
            std::string dlStr = reader->ReadString();
            auto dl = Ship::Context::GetInstance()->GetResourceManager()->LoadResourceProcess(dlStr.c_str());
            limbs[i].dList = nullptr;
            if (dl != nullptr) {
                limbs[i].dList = dl->GetRawPointer();
            }
            limbs[i].numChildren = reader->ReadUByte();
            limbs[i].flags = reader->ReadUByte();
            limbs[i].callbackIndex = reader->ReadUByte();
        }
        skel->skelData.limbsFlex = limbs;
    }

    return skel;
}

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

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

    const KeyframeSkelType skelType = (KeyframeSkelType)reader->ReadUByte();
    const uint32_t numBitFlags = reader->ReadUInt32();

    if (skelType == KeyframeSkelType::Normal) {
        anim->animData.bitFlags = new u8[numBitFlags];
        for (uint32_t i = 0; i < numBitFlags; i++) {
            anim->animData.bitFlags[i] = reader->ReadUByte();
        }
    } else {
        anim->animData.bitFlagsFlex = new u16[numBitFlags];
        for (uint32_t i = 0; i < numBitFlags; i++) {
            anim->animData.bitFlagsFlex[i] = reader->ReadUInt16();
        }
    }

    uint32_t numKf = reader->ReadUInt32();
    anim->animData.keyFrames = new KeyFrame[numKf];

    for (uint32_t i = 0; i < numKf; i++) {
        anim->animData.keyFrames[i].frame = reader->ReadInt16();
        anim->animData.keyFrames[i].value = reader->ReadInt16();
        anim->animData.keyFrames[i].velocity = reader->ReadInt16();
    }

    uint32_t numKfNums = reader->ReadUInt32();
    anim->animData.kfNums = new s16[numKfNums];

    for (uint32_t i = 0; i < numKfNums; i++) {
        anim->animData.kfNums[i] = reader->ReadInt16();
    }

    uint32_t numPresetValues = reader->ReadUInt32();
    anim->animData.presetValues = new s16[numPresetValues];

    for (uint32_t i = 0; i < numPresetValues; i++) {
        anim->animData.presetValues[i] = reader->ReadInt16();
    }

    anim->animData.unk_10[0] = reader->ReadUByte();
    anim->animData.unk_10[0] = reader->ReadUByte();

    anim->animData.duration = reader->ReadUInt16();

    return anim;
}
} // namespace SOH