summaryrefslogtreecommitdiff
path: root/src/d/snd/d_snd_file_mgr.cpp
blob: fdbd560b35117c940a7c45c6e386d7c28597ed76 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "d/snd/d_snd_file_mgr.h"

#include "common.h"
#include "nw4r/snd/snd_DisposeCallbackManager.h"
#include "nw4r/snd/snd_SoundArchive.h"
#include "nw4r/snd/snd_SoundArchiveLoader.h"
#include "nw4r/snd/snd_SoundArchivePlayer.h"

dSndFileManager::dSndFileManagerGlob dSndFileManager::sGlob;

static dSndFileManager sFileManager;

dSndFileManager *dSndFileManager::create(nw4r::snd::SoundArchive *pArchive, nw4r::snd::SoundHeap *pHeap) {
    sGlob.numFiles = pArchive->detail_GetFileCount();
    sGlob.pData = (const void **)pHeap->Alloc(sGlob.numFiles * sizeof(const void *));
    sGlob.pWaveData = (const void **)pHeap->Alloc(sGlob.numFiles * sizeof(const void *));
    clearGlob();
    nw4r::snd::detail::DisposeCallbackManager::GetInstance().RegisterDisposeCallback(&sFileManager);
    return &sFileManager;
}

void dSndFileManager::clearGlob() {
    for (u32 i = 0; i < sGlob.numFiles; i++) {
        sGlob.pData[i] = nullptr;
        sGlob.pWaveData[i] = nullptr;
    }
}

bool dSndFileManager::loadFileForSound(const nw4r::snd::SoundArchive &pArchive, u32 soundId, nw4r::snd::SoundHeap *pHeap) {
    nw4r::snd::SoundArchive::SoundInfo info;
    if (!pArchive.ReadSoundInfo(soundId, &info)) {
        return false;
    }

    if (sGlob.pData[info.fileId] == nullptr) {
        nw4r::snd::detail::SoundArchiveLoader loader(pArchive);
        const void *data = loader.LoadFile(info.fileId, pHeap);
        if (data == nullptr) {
            return false;
        }
        sGlob.pData[info.fileId] = data;
    }
    return true;
}

bool dSndFileManager::isLoadedFileForSound(nw4r::snd::SoundArchivePlayer *pPlayer, u32 soundId) {
    const nw4r::snd::SoundArchive &pArchive = pPlayer->GetSoundArchive();
    nw4r::snd::SoundArchive::SoundInfo info;
    if (!pArchive.ReadSoundInfo(soundId, &info)) {
        return false;
    }
    
    if (pPlayer->detail_GetFileAddress(info.fileId) == nullptr) {
        return false;
    }

    return true;
}

bool dSndFileManager::isLoadedFileAndWaveForBank(nw4r::snd::SoundArchivePlayer *pPlayer, u32 soundId) {
    const nw4r::snd::SoundArchive &pArchive = pPlayer->GetSoundArchive();
    nw4r::snd::SoundArchive::BankInfo info;
    if (!pArchive.ReadBankInfo(soundId, &info)) {
        return false;
    }
    
    if (pPlayer->detail_GetFileAddress(info.fileId) == nullptr) {
        return false;
    }

    if (pPlayer->detail_GetFileWaveDataAddress(info.fileId) == nullptr) {
        return false;
    }

    return true;
}

bool dSndFileManager::isLoadedFileAndWaveForSound(nw4r::snd::SoundArchivePlayer *pPlayer, u32 soundId) {
    const nw4r::snd::SoundArchive &pArchive = pPlayer->GetSoundArchive();
    nw4r::snd::SoundArchive::SoundInfo info;
    if (!pArchive.ReadSoundInfo(soundId, &info)) {
        return false;
    }
    
    if (pPlayer->detail_GetFileAddress(info.fileId) == nullptr) {
        return false;
    }

    if (pPlayer->detail_GetFileWaveDataAddress(info.fileId) == nullptr) {
        return false;
    }

    return true;
}


void dSndFileManager::InvalidateData(void const *pStart, void const *pEnd) {
    for (u32 i = 0; i < sGlob.numFiles; i++) {
        if (pStart <= sGlob.pData[i] && sGlob.pData[i] <= pEnd) {
            sGlob.pData[i] = nullptr;
        }
    }
}

void dSndFileManager::InvalidateWaveData(void const *pStart, void const *pEnd) {
    for (u32 i = 0; i < sGlob.numFiles; i++) {
        if (pStart <= sGlob.pWaveData[i] && sGlob.pWaveData[i] <= pEnd) {
            sGlob.pWaveData[i] = nullptr;
        }
    }
}

// These two might be inline given that the order is reversed

void const *dSndFileManager::GetFileWaveDataAddress(u32 id) {
    if (id >= mpGlob->numFiles) {
        return nullptr;
    }
    return mpGlob->pWaveData[id];
}

void const *dSndFileManager::GetFileAddress(u32 id) {
    if (id >= mpGlob->numFiles) {
        return nullptr;
    }
    return mpGlob->pData[id];
}