summaryrefslogtreecommitdiff
path: root/src/KingSystem/Resource/resCache.cpp
blob: d3e3195c54a90f5dbc3b412b712cce9202197b04 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "KingSystem/Resource/resCache.h"
#include <prim/seadScopedLock.h>
#include "KingSystem/Resource/resCacheCriticalSection.h"
#include "KingSystem/Resource/resControlTask.h"
#include "KingSystem/Resource/resResourceMgrTask.h"
#include "KingSystem/Resource/resSystem.h"
#include "KingSystem/Utils/Debug.h"

namespace ksys::res {

Cache::Cache() = default;

void Cache::init() {}

ResourceUnit* Cache::findUnit(const util::StrTreeMapNode::KeyType& key) const {
    auto lock = sead::makeScopedLock(gCacheCriticalSection);
    ResourceUnitMapNode* node = mMap.find(key);
    return node ? node->getUnit() : nullptr;
}

Handle::Status Cache::loadResource(const ControlTaskData& data) {
    auto* handle = data.mResHandle;
    if (handle->isLinked()) {
        stubbedLogFunction();
        return Handle::Status::_8;
    }

    {
        const auto path = data.mResLoadReq.mPath;
        ResourceUnit* unit = data.mPackResUnit;
        auto lock = sead::makeScopedLock(gCacheCriticalSection);
        bool remove_from_cache_if_needed = true;
        if (!unit) {
            unit = findUnit(path);
            remove_from_cache_if_needed = [&] {
                if (!unit)
                    return false;

                if (unit->mStatusFlags.isOn(ResourceUnit::StatusFlag::_80)) {
                    unit->removeFromCache();
                    if (returnFalse())
                        stubbedLogFunction();
                    return false;
                }

                if (unit->isStatusFlag10000Set()) {
                    unit->removeFromCache();
                    return false;
                }

                if (unit->isStatus0()) {
                    sead::FormatFixedSafeString<256> message("↓↓↓\nリソース名 : %s\n↑↑↑\n",
                                                             path.cstr());
                    util::PrintDebug(message);
                    return false;
                }

                return true;
            }();
        }

        if (remove_from_cache_if_needed) {
            const bool removed = unit->removeTask3FromQueue();
            if (unit->isLinkedToResourceMgr()) {
                unit->removeFromCache();
            } else if (removed) {
                unit->updateStatus();
                unit->attachHandle(handle);
                return unit->getRefCount() == 1 ? Handle::Status::_6 : Handle::Status::_5;
            }
        }
    }

    if (data.mHasResLoadReq)
        return Handle::Status::_8;

    ResourceUnit* result;

    {
        sead::FixedSafeString<128 + 7> new_path;
        sead::SafeString path = data.mResLoadReq.mPath;

        bool set_flag_4 = false;
        if (data.mResLoadReq.mLoadCompressed) {
            auto it = data.mResLoadReq.mPath.rfindIterator(".");
            if (it.getIndex() == -1)
                return Handle::Status::_1;

            ++it;
            sead::SafeString extension;
            extension = &*it;
            set_flag_4 = ResourceMgrTask::instance()->dropSFromExtensionIfNeeded(
                data.mResLoadReq.mPath, new_path, it.getIndex() - 1, extension);
        }

        if (!new_path.isEmpty())
            path = new_path;

        ResourceUnit::InitArg init_arg(false, false, set_flag_4, data.mResLoadReq._26,
                                       data.mResLoadReq._28, handle, this, nullptr, nullptr,
                                       &data.mResLoadReq, data.mResLoadReq.mArena,
                                       data.mResLoadReq.mBufferSize, path);

        ResourceMgrTask::GetUnitArg arg;
        arg.unit_init_arg = &init_arg;
        arg.arena = data.mResLoadReq.mArena;

        result = ResourceMgrTask::instance()->clearCachesAndGetUnit(arg);
    }

    if (!result)
        return Handle::Status::_3;

    {
        auto lock = sead::makeScopedLock(gCacheCriticalSection);
        mMap.insert(&result->mMapNode);
        result->setIsLinkedToCache(true);
    }

    u8 lane_id = 0xff;
    if (data.mResLoadReq.mLaneId <= 2) {
        const bool x = result->mStatusFlags.isOn(ResourceUnit::StatusFlag::LoadFromArchive);
#ifdef MATCHING_HACK_NX_CLANG
        // This makes absolutely no sense at all, but this prevents InstCombine from
        // turning (x & 0x20) >> 5 into (x >> 5) & 1 by adding a fake-use.
        // LLVM (4.0.1 at least) doesn't do anything with this piece of information
        // so the conditional still works fine.
        __builtin_assume(x);
#endif
        lane_id = 2 * data.mResLoadReq.mLaneId + (x ? 1 : 2);
    }

    ResourceUnit::RequestInitLoadArg load_arg;
    load_arg.lane_id = lane_id;
    load_arg.has_handle = data.mResLoadReq._8;
    result->requestInitLoad(load_arg);
    return Handle::Status::_7;
}

void Cache::eraseUnit(ResourceUnit* unit) {
    auto lock = sead::makeScopedLock(gCacheCriticalSection);
    if (unit->isLinkedToCache()) {
        mMap.erase(unit->getCacheKey());
        unit->setIsLinkedToCache(false);
    }
}

void Cache::removeUnitAndClearCache_(ResourceUnit* unit) {
    unit->mStatusFlags.reset(ResourceUnit::StatusFlag::_20000);
    if (unit->isStatusFlag8000Set()) {
        ResourceMgrTask::instance()->deregisterUnit(unit);
        ResourceMgrTask::instance()->requestClearCache(&unit);
    }
}

namespace detail::cache {
struct ForEachContextData {
    ForEachContextData() = default;
    virtual ~ForEachContextData() = default;
    virtual void invoke(const util::StrTreeMapKey&, ResourceUnit*& value) {
        (value->getCache()->*fn)(value);
    }

    void (Cache::*fn0)(ResourceUnit* unit);
    void (Cache::*fn)(ResourceUnit* unit);
};
KSYS_CHECK_SIZE_NX150(ForEachContextData, 0x28);

struct ForEachContext {
    void deleteUnit(util::StrTreeMapNode* node_) {
        auto* node = static_cast<ResourceUnitMapNode*>(node_);
        data->invoke(node->key(), node->getUnit());
    }

    ForEachContextData* data;
};
KSYS_CHECK_SIZE_NX150(ForEachContext, 0x8);
}  // namespace detail::cache

void Cache::eraseUnits() {
    using namespace detail::cache;
    ForEachContextData data{};
    data.fn = &Cache::removeUnitAndClearCache_;
    auto lock = sead::makeScopedLock(gCacheCriticalSection);
    ForEachContext context{&data};
    sead::Delegate1<ForEachContext, util::StrTreeMapNode*> delegate{&context,
                                                                    &ForEachContext::deleteUnit};
    mMap.forEach(delegate);
}

}  // namespace ksys::res