summaryrefslogtreecommitdiff
path: root/src/nw4r/snd/snd_AnimSoundReader.cpp
blob: a003139d3190277e587433611af91cd32c54c3ae (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
/* Only implemented to the extent necessary to match data sections. */

#include "nw4r/snd/snd_AnimSound.h"

#include "common.h" // nullptr
#include "nw4r/snd/snd_Util.h"
#include "nw4r/ut/ut_binaryFileFormat.h"

namespace nw4r {
namespace snd {
namespace detail {

bool IsValidFileHeader(const void *data) {
    ut::BinaryFileHeader const *fileHeader = static_cast<ut::BinaryFileHeader const *>(data);

    if (fileHeader->signature != AnimSoundFile::SIGNATURE_FILE) {
        return false;
    }

    if (fileHeader->version < AnimSoundFile::FILE_VERSION) {
        return false;
    }

    if (fileHeader->version > AnimSoundFile::FILE_VERSION) {
        return false;
    }

    return true;
}

AnimSoundFileReader::AnimSoundFileReader() : mpHeader(NULL) {}

bool AnimSoundFileReader::Setup(const void *buf) {
    if (!IsValidFileHeader(buf)) {
        return false;
    }
    mpHeader = static_cast<const AnimSoundFile::Header *>(buf);
    mpBlock = static_cast<const AnimSoundFile::Block *>(ut::AddOffsetToPtr(buf, mpHeader->blockOffset));
    return true;
}

void AnimSoundFileReader::Shutdown() {
    mpHeader = NULL;
    mpBlock = NULL;
}

u32 AnimSoundFileReader::GetEventCount() const {
    const AnimSoundFile::EventTable *event = GetEventTable();
    return event->eventRef.count;
}

const AnimEventRef *AnimSoundFileReader::GetEventRef(u32 id) const {
    const AnimSoundFile::EventTable *event = GetEventTable();
    return &event->eventRef.item[id];
}

const AnimSoundFile::EventTable *AnimSoundFileReader::GetEventTable() const {
    if (mpBlock == NULL) {
        return NULL;
    }
    return Util::GetDataRefAddress0(mpBlock->eventTable, &mpBlock->duration);
}

const AnimEvent *AnimSoundFileReader::GetEvent(const AnimEventRef *ref) const {
    if (ref == NULL) {
        return NULL;
    }
    const AnimSoundFile::EventTable *event = GetEventTable(); // unused
    return Util::GetDataRefAddress0(ref->event, &mpBlock->duration);
}

} // namespace detail
} // namespace snd
} // namespace nw4r