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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
#include "KingSystem/Event/evtResourceFlowchart.h"
#include <array>
#include <evfl/Flowchart.h>
#include <evfl/ResEventFlowFile.h>
#include "KingSystem/Event/evtActorBindings.h"
#include "KingSystem/Event/evtEventResource.h"
#include "KingSystem/Event/evtInfoData.h"
#include "KingSystem/Resource/Event/resEventFlowBinder.h"
#include "KingSystem/Resource/Event/resResourceEventFlow.h"
#include "KingSystem/Resource/resLoadRequest.h"
#include "KingSystem/Resource/resResourceMgrTask.h"
#include "KingSystem/Utils/Byaml/Byaml.h"
#include "KingSystem/Utils/HeapUtil.h"
namespace ksys::evt {
ResourceFlowchart::ResourceFlowchart() {
mLoadFailed = false;
}
ResourceFlowchart::~ResourceFlowchart() {
for (int i = 0; i < mFlowcharts.size(); ++i)
mFlowcharts[i].handle.requestUnload2();
mFlowcharts.freeBuffer();
if (mMissingFlowcharts.isBufferReady()) {
for (int i = 0; i < mFlowcharts.size(); ++i)
mMissingFlowcharts[i].handle.requestUnload2();
mMissingFlowcharts.freeBuffer();
}
}
void ResourceFlowchart::loadEventFlow(sead::Heap* heap, res::Handle* pack_handle) {
al::ByamlIter event_info;
al::ByamlIter subfiles;
int num_flowcharts = 1;
if (InfoData::instance()->getEntry(&event_info, mName, mEntryPoint) &&
event_info.tryGetIterByKey(&subfiles, "subfile")) {
num_flowcharts = subfiles.getSize() + 1;
}
mFlowcharts.allocBufferAssert(num_flowcharts, heap);
res::LoadRequest request;
request.mRequester = "ResourceFlowchart";
request.mPackHandle = pack_handle;
request._22 = true;
// Load the main flowchart.
sead::FixedSafeString<128> path;
mFlowcharts[0].loaded = false;
path.format("EventFlow/%s.bfevfl", mName.cstr());
mFlowcharts[0].handle.requestLoad(path, &request);
// Load any subfiles.
for (int i = 1; i < num_flowcharts; ++i) {
al::ByamlIter file_entry;
subfiles.tryGetIterByIndex(&file_entry, i - 1);
const char* file_name;
file_entry.tryGetStringByKey(&file_name, "file");
mFlowcharts[i].loaded = false;
path.format("EventFlow/%s", file_name);
mFlowcharts[i].handle.requestLoad(path, &request);
}
}
bool ResourceFlowchart::finishLoad() {
bool ready = true;
for (int i = 0; i < mFlowcharts.size(); ++i) {
auto& flowchart = mFlowcharts[i];
if (flowchart.loaded)
continue;
if (flowchart.handle.isReadyOrNeedsParse())
flowchart.handle.parseResource(nullptr);
if (flowchart.handle.isSuccess()) {
auto* evfl_res = sead::DynamicCast<res::EventFlowchart>(flowchart.handle.getResource());
if (evfl_res) {
flowchart.res_event_flow_file = evfl_res->getRes();
flowchart.res_flowchart = flowchart.res_event_flow_file->flowcharts.Get()->Get();
flowchart.loaded = true;
}
} else if (flowchart.handle.checkLoadStatus()) {
flowchart.loaded = true;
mLoadFailed = true;
}
if (!flowchart.loaded)
ready = false;
}
return ready;
}
long bindActorActions(evfl::FlowchartContext& context, res::EventFlowActionBinder binder) {
u8 any_ok = 0;
u8 any_failed = 0;
for (auto it = context.GetObjs().begin(); it != context.GetObjs().end(); ++it) {
bool ok;
bool failed;
auto binder_ = binder;
ok = false;
failed = false;
auto& evfl_bindings = it->GetActBinder().GetBindings();
for (auto b = evfl_bindings.begin(); b != evfl_bindings.end(); ++b) {
if (!b->IsUsed() || !b->IsInitialized())
continue;
const auto* actor = b->GetActor();
for (auto a = b->GetActions().begin(); a != b->GetActions().end(); ++a) {
binder_.bind(a, a->res_action, actor, static_cast<ActorBinding*>(b->GetUserData()));
(a->handler ? ok : failed) = true;
}
}
if (ok)
any_ok = 1;
if (failed)
any_failed = 1;
}
return (int(any_failed) << 8) | int(any_ok);
}
long bindActorQueries(evfl::FlowchartContext& context, res::EventFlowQueryBinder binder) {
u8 any_ok = 0;
u8 any_failed = 0;
for (auto it = context.GetObjs().begin(); it != context.GetObjs().end(); ++it) {
bool ok;
bool failed;
auto binder_ = binder;
ok = false;
failed = false;
auto& evfl_bindings = it->GetActBinder().GetBindings();
for (auto b = evfl_bindings.begin(); b != evfl_bindings.end(); ++b) {
if (!b->IsUsed() || !b->IsInitialized())
continue;
const auto* actor = b->GetActor();
for (auto q = b->GetQueries().begin(); q != b->GetQueries().end(); ++q) {
binder_.bind(q, q->res_query, actor, static_cast<ActorBinding*>(b->GetUserData()));
(q->handler ? ok : failed) = true;
}
}
if (ok)
any_ok = 1;
if (failed)
any_failed = 1;
}
return (int(any_failed) << 8) | int(any_ok);
}
bool ResourceFlowchart::setUpBindings(ActorBindings* bindings, sead::Heap* heap) {
evfl::FlowchartContext context;
if (!buildFlowchart(&context, heap))
return false;
// Bind actors. We do it twice: once in order to figure out how many actor bindings
// need to be allocated, and a second time to actually bind actors.
const auto bind_actors = [&] {
for (auto it = context.GetObjs().begin(); it != context.GetObjs().end(); ++it) {
bool ok;
bool failed;
res::EventFlowActorBinder binder{bindings, heap};
ok = false;
failed = false;
auto& evfl_bindings = it->GetActBinder().GetBindings();
for (auto binding = evfl_bindings.begin(); binding != evfl_bindings.end(); ++binding) {
if (binding->IsUsed() && !binding->IsInitialized()) {
binder.bind(binding, binding->GetActor());
(binding->IsInitialized() ? ok : failed) = true;
}
}
}
};
bind_actors();
bindings->allocBindings(heap);
bind_actors();
// Bind actions.
res::EventFlowActionBinder action_binder_1{bindings, heap};
bindActorActions(context, action_binder_1);
bindings->allocBindingsActions(heap);
res::EventFlowActionBinder action_binder_2{bindings, heap};
bindActorActions(context, action_binder_2);
// Bind queries.
res::EventFlowQueryBinder query_binder_1{bindings, heap};
bindActorQueries(context, query_binder_1);
bindings->allocBindingsQueries(heap);
res::EventFlowQueryBinder query_binder_2{bindings, heap};
bindActorQueries(context, query_binder_2);
context.UnbindAll();
return bindings->getNumBindings() != 0;
}
bool ResourceFlowchart::buildFlowchart(evfl::FlowchartContext* context, sead::Heap* heap) {
std::array<const evfl::ResFlowchart*, 32> flowcharts;
int num_flowcharts = 0;
const auto add_flowcharts = [&] {
for (int i = 0; i < mFlowcharts.size(); ++i)
flowcharts[num_flowcharts++] = mFlowcharts[i].res_flowchart;
};
add_flowcharts();
evfl::FlowchartContext::Builder builder({flowcharts.data(), num_flowcharts});
if (!builder.SetEntryPoint(mName.cstr(), mEntryPoint.cstr()))
return false;
evfl::FlowchartContext::Builder::BuildResult result;
if (builder.Build(&result, context, makeEvflAllocateArg(heap)))
return true;
// Load any missing flowchart.
// Note: this only works in debug builds; in release builds there is no debug heap
// to allocate the mMissingFlowcharts buffer.
mMissingFlowcharts.freeBuffer();
static constexpr int NumFallbackRes = 8;
for (int i = 0; i < NumFallbackRes; ++i) {
if (result.result !=
evfl::FlowchartContext::Builder::BuildResultType::kResFlowchartNotFound) {
continue;
}
sead::FormatFixedSafeString<128> path("EventFlow/%s.bfevfl",
result.missing_flowchart_name.data());
if (!mMissingFlowcharts.isBufferReady())
mMissingFlowcharts.allocBufferAssert(NumFallbackRes, util::getDebugHeap());
res::ResourceMgrTask::instance()->controlField9c0d88(false);
mMissingFlowcharts[i].handle.load(path, nullptr);
res::ResourceMgrTask::instance()->controlField9c0d88(true);
/// @bug Bug? This causes all flowcharts in mFlowcharts to be added more than once...
add_flowcharts();
for (int flow_idx = 0; flow_idx < i + 1; ++flow_idx) {
auto* flowchart = sead::DynamicCast<res::EventFlowchart>(
mMissingFlowcharts[flow_idx].handle.getResource());
flowcharts[num_flowcharts++] = flowchart->getRes()->flowcharts.Get()->Get();
}
evfl::FlowchartContext::Builder builder2({flowcharts.data(), num_flowcharts});
if (!builder2.SetEntryPoint(mName.cstr(), mEntryPoint.cstr()))
continue;
if (!builder2.Build(&result, context, makeEvflAllocateArg(heap)))
continue;
return true;
}
return false;
}
bool ResourceFlowchart::isResourceLoaded(const sead::SafeString& path_substring) const {
for (int i = 0; i < mFlowcharts.size(); ++i) {
auto* unit = mFlowcharts[i].handle.getUnit();
if (unit && unit->getPath().include(path_substring))
return true;
}
return false;
}
} // namespace ksys::evt
|