summaryrefslogtreecommitdiff
path: root/src/KingSystem/ActorSystem/actActorParam.cpp
blob: bcc3a045ed319c236b202e110c1e822c2254b112 (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
#include "KingSystem/ActorSystem/actActorParam.h"
#include <basis/seadRawPrint.h>
#include <prim/seadScopedLock.h>

namespace ksys::act {

ActorParam::Resources ActorParam::sDummyResources;

void ActorParam::resetDummyResources() {
    sDummyResources = {};
}

ActorParam::ActorParam() {
    mRes = {};
    mNumHandles = {};
    mEvent.resetSignal();
}

ActorParam::~ActorParam() {
    deleteData();
}

void ActorParam::deleteData() {
    auto lock = sead::makeScopedLock(mCS);

    if (mActorName.isEmpty())
        return;

    for (size_t i = 0; i < mNumHandles.size(); ++i) {
        for (s32 handle_idx = 0; handle_idx < mNumHandles[i]; ++handle_idx)
            mHandles[i][handle_idx].requestUnload();
        mNumHandles[i] = 0;
    }

    deleteResHandles();

    mActorName = "";
    mRefCount = 0;
    _a = 0;
    mRes = {};
    mEvent.resetSignal();
}

void ActorParam::deleteResHandles() {
    for (auto& handles : mHandles)
        handles.freeBuffer();
}

bool ActorParam::isDummyParam(res::ActorLink::Users::User user) const {
    return mRes.mActorLink->getUsers().getUserName(user) == "Dummy";
}

void ActorParam::allocResHandles(sead::Heap* heap, u32 buffer_idx, s32 count) {
    SEAD_ASSERT(buffer_idx == 0 || buffer_idx == 1);
    mHandles[buffer_idx].allocBufferAssert(count, heap);
    mNumHandles[buffer_idx] = 0;
}

s32 ActorParam::incrementRef() {
    auto lock = sead::makeScopedLock(mCS);
    return ++mRefCount;
}

s32 ActorParam::decrementRef() {
    auto lock = sead::makeScopedLock(mCS);

    if (mActorName.isEmpty())
        return 0;

    if (--mRefCount == 0)
        deleteData();

    return mRefCount;
}

void ActorParam::setEventSignal() {
    mEvent.setSignal();
}

void ActorParam::waitForEvent() {
    mEvent.wait();
}

bool ActorParam::isSignalSet() const {
    return mEvent.isSignalSet();
}

res::Handle* ActorParam::allocHandle() {
    const auto idx = mNumHandles[mActiveBufferIdx];
    ++mNumHandles[mActiveBufferIdx];
    return &mHandles[mActiveBufferIdx][idx];
}

void ActorParam::freeLastHandle() {
    --mNumHandles[mActiveBufferIdx];
}

void ActorParam::setResource(ResourceType type, ParamIO* param_io) {
    const auto idx = u32(type);
    mRes.mArray[idx] = param_io;
    param_io->setIndex(idx);
}

bool ActorParam::setPriority(const sead::SafeString& priority) {
    if (priority == "PlayerBefore") {
        mPriority = Priority::PlayerBefore;
        return true;
    }

    if (priority == "Player") {
        mPriority = Priority::Player;
        return true;
    }

    if (priority == "PlayerAfter") {
        mPriority = Priority::PlayerAfter;
        return true;
    }

    if (priority == "AllAfter") {
        mPriority = Priority::AllAfter;
        return true;
    }

    return false;
}

}  // namespace ksys::act