summaryrefslogtreecommitdiff
path: root/src/KingSystem/ActorSystem/actBaseProcUnit.cpp
blob: cac5c8643b637a7c66ce8cd8f5030f0eed0cc335 (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
#include "KingSystem/ActorSystem/actBaseProcUnit.h"
#include <prim/seadSafeString.h>
#include <prim/seadScopedLock.h>
#include "KingSystem/ActorSystem/actActorLinkConstDataAccess.h"
#include "KingSystem/ActorSystem/actBaseProc.h"
#include "KingSystem/ActorSystem/actBaseProcHandle.h"
#include "KingSystem/Utils/Debug.h"

namespace ksys::act {

bool BaseProcUnit::deleteProc(u32, BaseProcHandle* handle) {
    ActorLinkConstDataAccess accessor;

    {
        const auto lock = sead::makeScopedLock(mCS);
        BaseProcHandle* current_handle = mHandle;
        if (current_handle == handle) {
            if (mProc)
                accessor.acquire(mProc);

            if (mStatus == Status::_1 || mProc) {
                mHandle.compareExchange(handle, &BaseProcHandle::sDummyHandle);
            } else {
                mStatus = Status::Unused;
                mHandle = nullptr;
            }
        } else {
            sead::FixedSafeString<256> message;
            message.format("BaseProcUnit:(%p, %p), 呼び出し(%p)", this, current_handle, handle);
            util::PrintDebug(message);
        }
    }

    if (accessor.hasProc())
        accessor.mProc->deleteLater(BaseProc::DeleteReason::_2);

    return true;
}

bool BaseProcUnit::setProc(BaseProc* proc) {
    static constexpr const char* sStateNames[] = {"Init", "Calc", "Sleep", "Delete"};

    auto lock = sead::makeScopedLock(mCS);

    if (mProc)
        mProc = nullptr;

    if (isParentHandleDefault())
        return false;

    const auto print_info = [&] {
        sead::FixedSafeString<64> message;
        if (proc) {
            message.format("%s, %d, %d, %s, ( %p:%p )", proc->getName().cstr(), u32(mStatus.load()),
                           proc->isInitialized(), sStateNames[u8(proc->getState())], this,
                           mHandle.load());
        } else {
            message.format("なし, %d, ?, ?, ( %p:%p )", u32(mStatus.load()), this, mHandle.load());
        }
        util::PrintDebug(message);
    };

    if (!mHandle) {
        print_info();
        return false;
    }

    if (mStatus != Status::_1)
        print_info();

    mProc = proc;
    mStatus = Status::Ready;
    return true;
}

void BaseProcUnit::reset() {
    auto* handle = getHandle();
    if (handle != &BaseProcHandle::sDummyHandle) {
        sead::FixedSafeString<256> message;
        message.format("BaseProcUnit:%p, %p", this, handle);
        util::PrintDebug(message);
    }
    mProc = nullptr;
    mStatus = Status::Unused;
    mHandle = nullptr;
}

void BaseProcUnit::cleanUp(BaseProc* proc, bool set_status_5) {
    const auto lock = sead::makeScopedLock(mCS);

    mProc = nullptr;

    const auto print_info = [&] {
        sead::FixedSafeString<64> message;
        message.format("%d, ( %p:%p )", u32(mStatus.load()), this, mHandle.load());
        util::PrintDebug(message);
    };

    if (isParentHandleDefault()) {
        reset();
    } else if (!mHandle) {
        print_info();
    } else {
        const auto status = mStatus.load();
        if (status == Status::Unused || (status > Status::_3 && status != Status::_5))
            print_info();
        mStatus = set_status_5 ? Status::_5 : Status::_3;
    }
}

void BaseProcUnit::unlinkProc(BaseProc* proc) {
    const auto lock = sead::makeScopedLock(mCS);

    if (mProc != proc && mProc != nullptr) {
        sead::FixedSafeString<256> message;
        message.format("BaseProcUnit:(%p:%p), BaseProc:(%s:%p), 残りBaseProc(%s:%p)", this,
                       mHandle.load(), proc->getName().cstr(), proc, mProc->getName().cstr(),
                       mProc);
        util::PrintDebug(message);
    }

    reset();
}

bool BaseProcUnit::isParentHandleDefault() const {
    return mHandle == &BaseProcHandle::sDummyHandle;
}

}  // namespace ksys::act