summaryrefslogtreecommitdiff
path: root/src/KingSystem/Resource/Actor/resResourceAttClient.cpp
blob: 070c55616e7ac5c451465dfe3b7ab4b78d39701c (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
#include "resResourceAttClient.h"
#include <container/seadSafeArray.h>
#include "resResourceAttCheck.h"

namespace ksys::res {

AttClientList::~AttClientList() {
    mClients.freeBuffer();
}

void AttClientList::doCreate_(u8* buffer, u32 buffer_size, sead::Heap* heap) {}

bool AttClientList::parse_(u8* data, size_t size, sead::Heap* heap) {
    if (!data)
        return false;

    agl::utl::ResParameterArchive archive{data};
    const auto root = archive.getRootList();

    mAttPos.init(&mAttPosObj);
    mForceEdit.init(false, "ForceEdit", "強制編集", "", &mAttPosObj);
    addObj(&mAttPosObj, "AttPos");

    const auto AttClients = agl::utl::getResParameterList(root, "AttClients");
    if (int num; AttClients && (num = AttClients.getResParameterObjNum()) != 0) {
        mClients.allocBufferAssert(num, heap);
        for (auto it = mClients.begin(), end = mClients.end(); it != end; ++it) {
            it->client = nullptr;
            it->name.init("", "Name", "クライアントのキー名", "", &it->obj);
            it->file_name.init("", "FileName", "クライアントのデータファイル名", "", &it->obj);
            it->is_valid.init(true, "IsValid", "デフォルトの有効・無効状態", "", &it->obj);

            mAttClientsList.addObj(
                &it->obj, sead::FormatFixedSafeString<32>("%s%d", "AttClient_", it.getIndex()));
        }
    }

    addList(&mAttClientsList, "AttClients");

    applyResParameterArchive(archive);
    return true;
}

bool AttClientList::finishParsing_() {
    return true;
}

bool AttClientList::m7_() {
    for (auto& client : mClients)
        client.client = nullptr;
    return true;
}

bool AttClientList::isForceEdit() const {
    return mForceEdit.ref();
}

AttClient::~AttClient() {
    for (int i = 0; i < mChecks.size(); ++i) {
        if (mChecks[i]) {
            delete mChecks[i];
            mChecks[i] = nullptr;
        }
    }
    mChecks.freeBuffer();
}

void AttClient::doCreate_(u8*, u32, sead::Heap*) {}

namespace {

// Keep this in sync with ksys::act::AttType!
sead::SafeArray<const char*, 8> sAttTypes = {{
    "Action",
    "Lock",
    "SwordSearch",
    "Attack",
    "Appeal",
    "JumpRide",
    "NameBalloon",
    "LookOnly",
}};

void parseAttType(const agl::utl::ResParameterObj& AttClientParams, act::AttType* type) {
    const sead::SafeString AttType =
        agl::utl::getResParameter(AttClientParams, "AttType").getData<char>();

    *type = act::AttType::Invalid;

    for (int i = 0; i < sAttTypes.size(); ++i) {
        if (AttType == sAttTypes[i]) {
            *type = static_cast<act::AttType>(i);
            break;
        }
    }

    if (*type == act::AttType::Invalid)
        *type = act::AttType::Action;
}

void parseActionType(const agl::utl::ResParameterObj& AttClientParams, act::AttActionCode* code) {
    const sead::SafeString ActionType =
        agl::utl::getResParameter(AttClientParams, "ActionType").getData<char>();

    *code = act::AttActionCode::Dummy;

    for (int i = int(act::AttActionCode::None); i < int(act::AttActionCode::Dummy); ++i) {
        if (ActionType == act::AttActionType::text(i - int(act::AttActionCode::None))) {
            *code = static_cast<act::AttActionCode>(i);
            break;
        }
    }

    if (*code == act::AttActionCode::Dummy)
        *code = act::AttActionCode::None;
}

}  // namespace

bool AttClient::parse_(u8* data, size_t size, sead::Heap* heap) {
    if (!data)
        return false;

    agl::utl::ResParameterArchive archive{data};
    const auto root = archive.getRootList();
    const auto AttClientParams = root.getResParameterObj(0);

    parseAttType(AttClientParams, &mAttType);
    parseActionType(AttClientParams, &mActionCode);
    addObj(&mAttClientParamsObj, "AttClientParams");

    mAttTypeParam.init({sAttTypes[0]}, "AttType", "", &mAttClientParamsObj);
    mActionTypeParam.init({act::AttActionType(act::AttActionType::None).text()}, "ActionType", "",
                          &mAttClientParamsObj);
    mPriorityTypeParam.init({act::AttPriorityType(act::AttPriorityType::Obj).text()},
                            "PriorityType", "", &mAttClientParamsObj);

    const int num_checks = root.getResParameterListNum();
    if (num_checks != 0) {
        mChecks.allocBufferAssert(num_checks, heap);
        for (int i = 0; i < num_checks; ++i)
            mChecks[i] = nullptr;

        AttCheck::CreateArg arg{};
        arg.heap = heap;
        arg.client = this;

        auto it = mChecks.begin(), end = mChecks.end();
        auto res_it = root.listBegin(), res_end = root.listEnd();
        for (; it != end && res_it != res_end; ++res_it, ++it) {
            arg.res_list = res_it.getList();

            auto* check = *it = AttCheck::make(arg);
            if (check == nullptr)
                return false;

            addList(&check->getList_(),
                    sead::FormatFixedSafeString<32>("%s%d", "Check_", it.getIndex()));
        }
    }

    applyResParameterArchive(archive);

    const sead::SafeString PriorityType = mPriorityTypeParam.ref();
    for (auto priority : act::AttPriorityType{}) {
        if (PriorityType == priority.text()) {
            mPriorityType = priority;
            mPriorityTypeStr = PriorityType;
            return true;
        }
    }
    mPriorityType = act::AttPriorityType::Obj;
    mPriorityTypeStr = mPriorityType.text();
    return true;
}

int AttClient::getNumChecks() const {
    return mChecks.size();
}

void AttClient::appendPriority(sead::BufferedSafeString* str) {
    str->append(mPriorityTypeStr);
}

}  // namespace ksys::res