Rework flow impl on top of FlowService

This commit is contained in:
Luke Street
2026-08-21 01:43:22 -06:00
parent 5d1dda7e94
commit 932c3aaba7
11 changed files with 1108 additions and 925 deletions
-6
View File
@@ -1,6 +0,0 @@
#pragma once
#include "dolphin/types.h"
inline constexpr u16 BASE_CUSTOM_MSG_AND_FLOW_ID = 21000;
inline constexpr u16 CUSTOM_BMG_GROUP = 9;
+301 -344
View File
@@ -1,6 +1,5 @@
#include "messages.hpp"
#include "custom_flow_ids.hpp"
#include "randomizer_context.hpp"
#include "stages.h"
#include "tools.h"
@@ -12,21 +11,17 @@
#include "d/d_save.h"
#include "d/d_stage.h"
#include <mods/bits.hpp>
#include <mods/svc/flow.hpp>
#include <mods/svc/log.hpp>
#include <fmt/format.h>
#include <algorithm>
#include <array>
#include <cstdint>
#include <cstring>
#include <limits>
#include <span>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
@@ -34,17 +29,11 @@ namespace randomizer::messages {
namespace {
constexpr uint16_t kMessageGroupCount = 9;
constexpr uint16_t kLegacyQueryEventFlag = 53;
constexpr uint16_t kLegacyQueryChangeTime = 54;
constexpr uint16_t kLegacyQueryReturnToSpawn = 55;
constexpr uint8_t kLegacyEventNoOp = 43;
constexpr uint8_t kLegacyEventChangeTime = 44;
constexpr uint8_t kLegacyEventReturnToSpawn = 45;
constexpr uint8_t kLegacyEventSetTrackerFlag = 46;
constexpr uint8_t kLegacyEventRemoveTradeItem = 47;
constexpr uint32_t kPoeSoulGetMessage = 325;
constexpr uint32_t kSkyCharacterGetMessage = 335;
using LegacyIdMap = std::unordered_map<uint16_t, uint16_t>;
using MessageIdMaps = std::array<LegacyIdMap, kMessageGroupCount>;
using MessageIdMaps = std::array<std::unordered_map<std::string, MessageId>, kMessageGroupCount>;
using NodeIdMaps = std::array<std::unordered_map<std::string, uint16_t>, kMessageGroupCount>;
mods::flow::Query s_eventFlagQuery;
mods::flow::Query s_changeTimeQuery;
@@ -61,6 +50,15 @@ uint32_t read_parameter(const uint8_t parameters[4]) {
static_cast<uint32_t>(parameters[2]) << 8 | parameters[3];
}
std::array<uint8_t, 4> parameter_bytes(uint32_t parameter) {
return {
static_cast<uint8_t>(parameter >> 24),
static_cast<uint8_t>(parameter >> 16),
static_cast<uint8_t>(parameter >> 8),
static_cast<uint8_t>(parameter),
};
}
uint16_t query_event_flag(ModContext*, const FlowQueryContext* query, void*) {
if (query == nullptr || query->parameter >= std::size(dSv_event_flag_c::saveBitLabels)) {
return 0;
@@ -120,65 +118,46 @@ std::vector<uint8_t> encoded_text(const std::string& text) {
return result;
}
MessageEntryData default_message_entry() {
MessageEntryData entry{};
entry.bytes[8] = 0x24;
entry.bytes[12] = 0xff;
entry.bytes[16] = 0x02;
entry.bytes[17] = 0x03;
entry.bytes[18] = 0x04;
return entry;
}
MessageEntryData message_entry(const RandomizerContext& context, uint32_t legacyKey) {
MessageEntryData entry = default_message_entry();
const auto found = context.mAttributeOverrides.find(legacyKey);
if (found != context.mAttributeOverrides.end()) {
std::memcpy(entry.bytes, found->second.data(), sizeof(entry.bytes));
std::fill_n(entry.bytes, 6, uint8_t{});
}
return entry;
}
std::vector<uint32_t> text_keys(const RandomizerContext& context, uint16_t group) {
std::unordered_set<uint32_t> unique;
for (const auto& [language, overrides] : context.mTextOverrides) {
for (const auto& [key, text] : overrides) {
if (key >> 16 == group) {
unique.insert(key);
}
}
}
std::vector<uint32_t> result{unique.begin(), unique.end()};
std::ranges::sort(result);
return result;
mods::flow::MessageStyle message_style(const RandomizerContext::MessageStyleData& source) {
return mods::flow::MessageStyle{}
.event_label_id(source.eventLabelId)
.speaker(source.speaker)
.box_kind(static_cast<MessageBoxKind>(source.boxKind))
.draw_type(static_cast<MessageDrawType>(source.drawType))
.box_position(static_cast<MessageBoxPosition>(source.boxPosition))
.line_alignment(source.lineAlignment)
.speaker_mood(source.speakerMood)
.camera_attr(source.cameraAttr)
.talk_anim(source.talkAnim)
.face_anim(source.faceAnim)
.trailing_data(source.trailingData);
}
ModResult register_custom_messages(const RandomizerContext& context, MessageIdMaps& messageIds) {
const auto keys = text_keys(context, CUSTOM_BMG_GROUP);
for (uint16_t group = 0; group < kMessageGroupCount; ++group) {
for (const uint32_t key : keys) {
std::vector<mods::flow::MessageVariant> variants;
for (const auto& [language, overrides] : context.mTextOverrides) {
const auto found = overrides.find(key);
if (found == overrides.end() || language < MESSAGE_LANGUAGE_ENGLISH ||
language > MESSAGE_LANGUAGE_ITALIAN)
{
continue;
}
variants.emplace_back(static_cast<MessageLanguage>(language),
message_entry(context, key), encoded_text(found->second));
}
if (variants.empty()) {
return MOD_INVALID_ARGUMENT;
}
auto message = mods::flow::register_message(group, variants);
if (!message) {
return message.result();
}
messageIds[group].emplace(static_cast<uint16_t>(key), message.id());
s_messages.push_back(std::move(message));
for (const auto& definition : context.mCustomMessages) {
if (definition.group >= kMessageGroupCount || definition.name.empty() ||
messageIds[definition.group].contains(definition.name))
{
return MOD_INVALID_ARGUMENT;
}
const auto style = message_style(definition.style);
std::vector<mods::flow::MessageVariant> variants;
for (const auto& [language, text] : definition.text) {
if (language < MESSAGE_LANGUAGE_ENGLISH || language > MESSAGE_LANGUAGE_ITALIAN) {
continue;
}
variants.emplace_back(static_cast<MessageLanguage>(language),
style.data(), encoded_text(text));
}
if (variants.empty()) {
return MOD_INVALID_ARGUMENT;
}
auto message = mods::flow::register_message(definition.group, variants);
if (!message) {
return message.result();
}
messageIds[definition.group].emplace(definition.name, message.id());
s_messages.push_back(std::move(message));
}
return MOD_OK;
}
@@ -200,14 +179,12 @@ bool formatted_override(
}
uint32_t value = 0;
// For item counts, execItemGet hasn't run yet, so add one to the count
// Message resolution runs before the acquisition updates these counters.
switch (key) {
case 325: // Group 0, id 325
// Poe Soul get item text
case kPoeSoulGetMessage:
value = dComIfGs_getPohSpiritNum() + 1;
break;
case 335: // Group 0, id 335
// Sky book characters get item text
case kSkyCharacterGetMessage:
value = getAncientDocumentNum() + 1;
break;
default:
@@ -222,338 +199,318 @@ bool formatted_override(
}
ModResult register_native_overrides(const RandomizerContext& context) {
for (uint16_t group = 0; group < kMessageGroupCount; ++group) {
for (const uint32_t key : text_keys(context, group)) {
for (const auto& [language, overrides] : context.mTextOverrides) {
const auto found = overrides.find(key);
if (found == overrides.end() || language < MESSAGE_LANGUAGE_ENGLISH ||
language > MESSAGE_LANGUAGE_ITALIAN)
{
continue;
}
mods::flow::MessageOverride message;
if (key == 325 || key == 335) {
message = mods::flow::override_message_fn(group, static_cast<uint16_t>(key),
static_cast<MessageLanguage>(language), formatted_override);
} else {
const auto text = encoded_text(found->second);
message = mods::flow::override_message(group, static_cast<uint16_t>(key),
static_cast<MessageLanguage>(language), std::span{text});
}
if (!message) {
return message.result();
}
s_overrides.push_back(std::move(message));
for (const auto& [language, overrides] : context.mTextOverrides) {
if (language < MESSAGE_LANGUAGE_ENGLISH || language > MESSAGE_LANGUAGE_ITALIAN) {
continue;
}
for (const auto& [key, value] : overrides) {
const uint16_t group = key >> 16;
const uint16_t messageId = static_cast<uint16_t>(key);
mods::flow::MessageOverride message;
if (key == kPoeSoulGetMessage || key == kSkyCharacterGetMessage) {
message = mods::flow::override_message_fn(group, messageId,
static_cast<MessageLanguage>(language), formatted_override);
} else {
const auto text = encoded_text(value);
message = mods::flow::override_message(
group, messageId, static_cast<MessageLanguage>(language), std::span{text});
}
if (!message) {
return message.result();
}
s_overrides.push_back(std::move(message));
}
}
return MOD_OK;
}
std::vector<uint16_t> custom_node_ids(const RandomizerContext& context) {
std::vector<uint16_t> result;
for (const auto& [key, node] : context.mFlowPatches) {
if (key >> 16 == CUSTOM_BMG_GROUP) {
result.push_back(static_cast<uint16_t>(key));
}
bool resolve_query(const std::string& name, FlowQueryId& out) {
static const std::unordered_map<std::string, FlowQueryId> builtins{
{"event flag", FLOW_QUERY_EVENT_FLAG},
{"rupees", FLOW_QUERY_RUPEES},
{"item owned", FLOW_QUERY_ITEM_OWNED},
{"empty bottles", FLOW_QUERY_EMPTY_BOTTLES},
{"select 2 cancel", FLOW_QUERY_SELECT_2_CANCEL},
{"select 3 cancel", FLOW_QUERY_SELECT_3_CANCEL},
};
if (const auto found = builtins.find(name); found != builtins.end()) {
out = found->second;
return true;
}
std::ranges::sort(result);
return result;
if (name == "randomizer event flag") {
out = s_eventFlagQuery.id();
} else if (name == "randomizer change time") {
out = s_changeTimeQuery.id();
} else if (name == "randomizer return to spawn") {
out = s_returnToSpawnQuery.id();
} else {
return false;
}
return true;
}
ModResult allocate_shared_node_ids(const std::array<FlowGraphHandle, kMessageGroupCount>& handles,
size_t count, std::array<std::vector<uint16_t>, kMessageGroupCount>& allocated,
LegacyIdMap& nodeIds, const std::vector<uint16_t>& legacyIds) {
if (count == 0) {
return MOD_OK;
bool resolve_event(const std::string& name, FlowEventId& out) {
static const std::unordered_map<std::string, FlowEventId> builtins{
{"remove rupees", FLOW_EVENT_REMOVE_RUPEES},
{"start event", FLOW_EVENT_START_EVENT},
{"select vertical", FLOW_EVENT_SELECT_VERTICAL},
{"set switch", FLOW_EVENT_SET_SWITCH},
{"shop sold out", FLOW_EVENT_SHOP_SOLD_OUT},
{"add donation", FLOW_EVENT_ADD_DONATION},
{"no-op", FLOW_EVENT_UNUSED_42},
};
if (const auto found = builtins.find(name); found != builtins.end()) {
out = found->second;
return true;
}
std::array<std::unordered_set<uint16_t>, kMessageGroupCount> allocatedSets;
std::vector<uint16_t> common;
while (common.size() < count) {
for (size_t group = 0; group < handles.size(); ++group) {
uint16_t id = 0;
const ModResult result = svc_flow->allocate_node(mod_ctx, handles[group], &id);
if (result != MOD_OK) {
return result;
}
allocated[group].push_back(id);
allocatedSets[group].insert(id);
}
common.clear();
for (const uint16_t candidate : allocatedSets.front()) {
const bool present = std::ranges::all_of(
allocatedSets, [candidate](const auto& ids) { return ids.contains(candidate); });
if (present) {
common.push_back(candidate);
}
}
if (name == "randomizer change time") {
out = s_changeTimeEvent.id();
} else if (name == "randomizer return to spawn") {
out = s_returnToSpawnEvent.id();
} else if (name == "randomizer remove trade item") {
out = s_removeTradeItemEvent.id();
} else {
return false;
}
std::ranges::sort(common);
for (size_t i = 0; i < legacyIds.size(); ++i) {
nodeIds.emplace(legacyIds[i], common[i]);
}
return MOD_OK;
return true;
}
ModResult remap_target(uint16_t legacy, const LegacyIdMap& nodeIds, uint16_t& out) {
if (legacy == mods::flow::kEnd || legacy < BASE_CUSTOM_MSG_AND_FLOW_ID) {
out = legacy;
return MOD_OK;
bool resolve_reference(const RandomizerContext::FlowReference& reference, uint16_t group,
const NodeIdMaps& nodeIds, uint16_t& out) {
if (reference.nativeId.has_value()) {
out = reference.nativeId.value();
return true;
}
const auto found = nodeIds.find(legacy);
if (found == nodeIds.end()) {
return MOD_INVALID_ARGUMENT;
const auto found = nodeIds[group].find(reference.name);
if (found == nodeIds[group].end()) {
return false;
}
out = found->second;
return MOD_OK;
return true;
}
ModResult remap_query(FlowNodeData& node) {
const uint16_t legacy = mods::read_bits<uint16_t>(node.bytes + 2);
FlowQueryId query = legacy;
switch (legacy) {
case kLegacyQueryEventFlag:
query = s_eventFlagQuery.id();
break;
case kLegacyQueryChangeTime:
query = s_changeTimeQuery.id();
break;
case kLegacyQueryReturnToSpawn:
query = s_returnToSpawnQuery.id();
break;
default:
if (legacy >= FLOW_QUERY_BUILTIN_COUNT) {
bool resolve_message(const RandomizerContext::FlowReference& reference, uint16_t group,
const MessageIdMaps& messageIds, uint16_t& out) {
if (reference.nativeId.has_value()) {
out = reference.nativeId.value();
return true;
}
const auto found = messageIds[group].find(reference.name);
if (found == messageIds[group].end()) {
return false;
}
out = found->second;
return true;
}
ModResult add_custom_node(const RandomizerContext::FlowNode& flow,
const MessageIdMaps& messageIds, mods::flow::GraphBuilder& builder,
mods::flow::NodeRef& out) {
if (flow.type == RandomizerContext::FlowNodeType::MESSAGE) {
uint16_t message = 0;
if (!resolve_message(flow.message, flow.group, messageIds, message)) {
return MOD_INVALID_ARGUMENT;
}
break;
}
mods::write_bits(node.bytes + 2, query);
return MOD_OK;
}
ModResult remap_event(FlowNodeData& node) {
switch (node.bytes[1]) {
case kLegacyEventNoOp:
node.bytes[1] = FLOW_EVENT_UNUSED_42;
break;
case kLegacyEventChangeTime:
node.bytes[1] = s_changeTimeEvent.id();
break;
case kLegacyEventReturnToSpawn:
node.bytes[1] = s_returnToSpawnEvent.id();
break;
case kLegacyEventSetTrackerFlag:
// Older seed files may contain the retired pre-grant tracker event.
node.bytes[1] = FLOW_EVENT_UNUSED_42;
break;
case kLegacyEventRemoveTradeItem:
node.bytes[1] = s_removeTradeItemEvent.id();
break;
default:
if (node.bytes[1] >= FLOW_EVENT_BUILTIN_COUNT) {
return MOD_INVALID_ARGUMENT;
}
break;
}
return MOD_OK;
}
FlowNodeData raw_node(uint64_t value) {
FlowNodeData node{};
std::memcpy(node.bytes, &value, sizeof(node.bytes));
return node;
}
ModResult remap_node(const RandomizerContext& context, uint32_t legacyKey, uint16_t group,
FlowGraphHandle handle, const LegacyIdMap& nodeIds, const MessageIdMaps& messageIds,
FlowNodeData& node) {
switch (node.bytes[0]) {
case 1: {
const uint16_t legacyMessage = mods::read_bits<uint16_t>(node.bytes + 2);
if (legacyMessage >= BASE_CUSTOM_MSG_AND_FLOW_ID) {
const auto found = messageIds[group].find(legacyMessage);
if (found == messageIds[group].end()) {
return MOD_INVALID_ARGUMENT;
}
mods::write_bits(node.bytes + 2, found->second);
}
uint16_t target = 0;
const ModResult result =
remap_target(mods::read_bits<uint16_t>(node.bytes + 4), nodeIds, target);
if (result != MOD_OK) {
return result;
}
mods::write_bits(node.bytes + 4, target);
out = builder.add_message(message);
return MOD_OK;
}
case 2: {
ModResult result = remap_query(node);
if (result != MOD_OK) {
return result;
}
const auto overrides = context.mFlowPatchesBranchOverrides.find(legacyKey);
if (overrides == context.mFlowPatchesBranchOverrides.end()) {
return MOD_OK;
}
if (overrides->second.empty() || overrides->second.size() != node.bytes[1] ||
overrides->second.size() > std::numeric_limits<uint16_t>::max())
{
if (flow.type == RandomizerContext::FlowNodeType::BRANCH) {
FlowQueryId query = 0;
if (flow.parameters > 0xffff || !resolve_query(flow.operation, query)) {
return MOD_INVALID_ARGUMENT;
}
out = builder.add_branch(query, static_cast<uint16_t>(flow.parameters));
return MOD_OK;
}
FlowEventId event = 0;
if (!resolve_event(flow.operation, event)) {
return MOD_INVALID_ARGUMENT;
}
out = builder.add_event(event, parameter_bytes(flow.parameters));
return MOD_OK;
}
ModResult wire_custom_node(const RandomizerContext::FlowNode& flow, uint16_t group,
const NodeIdMaps& nodeIds, mods::flow::NodeRef node) {
if (flow.type == RandomizerContext::FlowNodeType::BRANCH) {
std::vector<uint16_t> targets;
targets.reserve(overrides->second.size());
for (const uint16_t legacyTarget : overrides->second) {
targets.reserve(flow.results.size());
for (const auto& result : flow.results) {
uint16_t target = 0;
result = remap_target(legacyTarget, nodeIds, target);
if (result != MOD_OK) {
return result;
if (!resolve_reference(result, group, nodeIds, target)) {
return MOD_INVALID_ARGUMENT;
}
targets.push_back(target);
}
uint16_t firstEdge = 0;
result = svc_flow->add_edges(
mod_ctx, handle, targets.data(), static_cast<uint16_t>(targets.size()), &firstEdge);
if (result == MOD_OK) {
mods::write_bits(node.bytes + 6, firstEdge);
}
return result;
node.results(targets);
return MOD_OK;
}
case 3: {
ModResult result = remap_event(node);
if (result != MOD_OK || node.bytes[1] == FLOW_EVENT_JUMP_FLOW) {
return result;
}
const uint16_t legacyEdge = mods::read_bits<uint16_t>(node.bytes + 2);
if (legacyEdge < BASE_CUSTOM_MSG_AND_FLOW_ID) {
return MOD_OK;
}
uint16_t target = 0;
result = remap_target(legacyEdge, nodeIds, target);
if (result != MOD_OK) {
return result;
}
uint16_t edge = 0;
result = svc_flow->add_edges(mod_ctx, handle, &target, 1, &edge);
if (result == MOD_OK) {
mods::write_bits(node.bytes + 2, edge);
}
return result;
}
default:
uint16_t target = 0;
if (!resolve_reference(flow.next, group, nodeIds, target)) {
return MOD_INVALID_ARGUMENT;
}
node.next(target);
return MOD_OK;
}
void remap_actor_record(std::vector<uint8_t>& bytes, const LegacyIdMap& nodeIds) {
if (bytes.size() < sizeof(stage_actor_data_class)) {
return;
ModResult apply_flow_patch(const RandomizerContext::FlowNode& flow, uint16_t group,
const MessageIdMaps& messageIds, const NodeIdMaps& nodeIds,
mods::flow::GraphBuilder& builder) {
if (!flow.patchIndex.has_value()) {
return MOD_INVALID_ARGUMENT;
}
stage_actor_data_class actor{};
std::memcpy(&actor, bytes.data(), sizeof(actor));
if (std::memcmp(actor.name, "Obj_kn2", 7) != 0) {
return;
if (flow.type == RandomizerContext::FlowNodeType::MESSAGE) {
uint16_t message = 0;
uint16_t target = 0;
if (!resolve_message(flow.message, group, messageIds, message) ||
!resolve_reference(flow.next, group, nodeIds, target))
{
return MOD_INVALID_ARGUMENT;
}
builder.patch_node(flow.patchIndex.value(), mods::flow::message(0, message, target));
return MOD_OK;
}
const uint16_t legacy = static_cast<uint16_t>(actor.base.angle.x);
const auto found = nodeIds.find(legacy);
if (found == nodeIds.end()) {
return;
if (flow.type == RandomizerContext::FlowNodeType::BRANCH) {
FlowQueryId query = 0;
if (flow.parameters > 0xffff || !resolve_query(flow.operation, query)) {
return MOD_INVALID_ARGUMENT;
}
std::vector<uint16_t> targets;
targets.reserve(flow.results.size());
for (const auto& result : flow.results) {
uint16_t target = 0;
if (!resolve_reference(result, group, nodeIds, target)) {
return MOD_INVALID_ARGUMENT;
}
targets.push_back(target);
}
builder.patch_branch(flow.patchIndex.value(), query,
static_cast<uint16_t>(flow.parameters), targets);
return MOD_OK;
}
actor.base.angle.x = static_cast<int16_t>(found->second);
std::memcpy(bytes.data(), &actor, sizeof(actor));
FlowEventId event = 0;
uint16_t target = 0;
if (!resolve_event(flow.operation, event) ||
!resolve_reference(flow.next, group, nodeIds, target))
{
return MOD_INVALID_ARGUMENT;
}
builder.patch_event(
flow.patchIndex.value(), event, parameter_bytes(flow.parameters), target);
return MOD_OK;
}
void remap_actor_flows(RandomizerContext& context, const LegacyIdMap& nodeIds) {
for (auto& [stage, patches] : context.mObjectPatches) {
for (auto& [crc, bytes] : patches) {
remap_actor_record(bytes, nodeIds);
ModResult resolve_actor_flow(RandomizerContext::ActorData& actor, uint16_t group,
const NodeIdMaps& nodeIds) {
if (actor.flow.empty()) {
return MOD_OK;
}
const auto found = nodeIds[group].find(actor.flow);
if (found == nodeIds[group].end() || actor.bytes.size() < sizeof(stage_actor_data_class)) {
return MOD_INVALID_ARGUMENT;
}
stage_actor_data_class data{};
std::memcpy(&data, actor.bytes.data(), sizeof(data));
data.base.angle.x = static_cast<int16_t>(found->second);
std::memcpy(actor.bytes.data(), &data, sizeof(data));
return MOD_OK;
}
ModResult resolve_actor_flows(RandomizerContext& context, const NodeIdMaps& nodeIds) {
auto group_for_key = [](uint32_t key, uint16_t& group) {
const uint32_t stage = key >> 16;
if (stage >= std::size(allStageMessageGroups)) {
return false;
}
group = allStageMessageGroups[stage];
return group < kMessageGroupCount;
};
for (auto& [key, patches] : context.mObjectPatches) {
uint16_t group = 0;
if (!group_for_key(key, group)) {
return MOD_INVALID_ARGUMENT;
}
for (auto& [crc, actor] : patches) {
const ModResult result = resolve_actor_flow(actor, group, nodeIds);
if (result != MOD_OK) {
return result;
}
}
}
for (auto& [stage, additions] : context.mObjectAdditions) {
for (auto& bytes : additions) {
remap_actor_record(bytes, nodeIds);
for (auto& [key, additions] : context.mObjectAdditions) {
uint16_t group = 0;
if (!group_for_key(key, group)) {
return MOD_INVALID_ARGUMENT;
}
for (auto& actor : additions) {
const ModResult result = resolve_actor_flow(actor, group, nodeIds);
if (result != MOD_OK) {
return result;
}
}
}
return MOD_OK;
}
ModResult register_graphs(RandomizerContext& context, const MessageIdMaps& messageIds) {
const auto legacyIds = custom_node_ids(context);
std::array<FlowGraphHandle, kMessageGroupCount> handles{};
std::array<mods::flow::Graph, kMessageGroupCount> guards;
for (uint16_t group = 0; group < kMessageGroupCount; ++group) {
ModResult result = svc_flow->begin_graph(mod_ctx, group, &handles[group]);
if (result != MOD_OK) {
return result;
NodeIdMaps nodeIds;
for (const auto& flow : context.mFlowNodes) {
if (flow.group >= kMessageGroupCount) {
return MOD_INVALID_ARGUMENT;
}
guards[group] = mods::flow::Graph{handles[group], MOD_OK};
}
std::array<std::vector<uint16_t>, kMessageGroupCount> allocated;
LegacyIdMap nodeIds;
ModResult result =
allocate_shared_node_ids(handles, legacyIds.size(), allocated, nodeIds, legacyIds);
if (result != MOD_OK) {
return result;
}
std::unordered_map<uint16_t, uint16_t> dynamicToLegacy;
for (const auto& [legacy, dynamic] : nodeIds) {
dynamicToLegacy.emplace(dynamic, legacy);
}
const FlowNodeData unused = mods::flow::event(FLOW_EVENT_UNUSED_42, 0, {});
for (uint16_t group = 0; group < kMessageGroupCount; ++group) {
for (const uint16_t dynamicId : allocated[group]) {
FlowNodeData node = unused;
const auto legacy = dynamicToLegacy.find(dynamicId);
if (legacy != dynamicToLegacy.end()) {
const uint32_t key = static_cast<uint32_t>(CUSTOM_BMG_GROUP) << 16 | legacy->second;
const auto raw = context.mFlowPatches.find(key);
if (raw == context.mFlowPatches.end()) {
return MOD_INVALID_ARGUMENT;
}
node = raw_node(raw->second);
result = remap_node(context, key, group, handles[group], nodeIds, messageIds, node);
if (result != MOD_OK) {
return result;
}
}
result = svc_flow->fill_node(mod_ctx, handles[group], dynamicId, &node);
if (result != MOD_OK) {
return result;
}
bool hasNodes = false;
for (const auto& flow : context.mFlowNodes) {
hasNodes |= flow.group == group;
}
if (!hasNodes) {
continue;
}
for (const auto& [key, value] : context.mFlowPatches) {
if (key >> 16 != group) {
mods::flow::GraphBuilder builder{group};
std::unordered_map<std::string, mods::flow::NodeRef> refs;
for (const auto& flow : context.mFlowNodes) {
if (flow.group != group || flow.patchIndex.has_value()) {
continue;
}
FlowNodeData node = raw_node(value);
result = remap_node(context, key, group, handles[group], nodeIds, messageIds, node);
if (flow.name.empty() || refs.contains(flow.name)) {
return MOD_INVALID_ARGUMENT;
}
mods::flow::NodeRef ref;
const ModResult result = add_custom_node(flow, messageIds, builder, ref);
if (result != MOD_OK) {
return result;
}
result =
svc_flow->patch_node(mod_ctx, handles[group], static_cast<uint16_t>(key), &node);
refs.emplace(flow.name, ref);
nodeIds[group].emplace(flow.name, ref.id());
}
for (const auto& flow : context.mFlowNodes) {
if (flow.group != group || flow.patchIndex.has_value()) {
continue;
}
const ModResult result = wire_custom_node(flow, group, nodeIds, refs.at(flow.name));
if (result != MOD_OK) {
return result;
}
}
}
for (const FlowGraphHandle handle : handles) {
result = svc_flow->commit_graph(mod_ctx, handle);
if (result != MOD_OK) {
return result;
for (const auto& flow : context.mFlowNodes) {
if (flow.group != group || !flow.patchIndex.has_value()) {
continue;
}
const ModResult result =
apply_flow_patch(flow, group, messageIds, nodeIds, builder);
if (result != MOD_OK) {
return result;
}
}
auto graph = builder.commit();
if (!graph) {
return graph.result();
}
}
for (auto& graph : guards) {
s_graphs.push_back(std::move(graph));
}
remap_actor_flows(context, nodeIds);
return MOD_OK;
return resolve_actor_flows(context, nodeIds);
}
} // namespace
+396 -296
View File
@@ -16,18 +16,98 @@
#include "../generator/logic/entrance_shuffle.hpp"
#include <fstream>
#include <type_traits>
#include <unordered_set>
#include <mods/svc/log.hpp>
#include "custom_flow_ids.hpp"
#include "d/actor/d_a_alink.h"
#include "d/d_com_inf_game.h"
#include "d/d_meter2.h"
#include "d/d_meter2_draw.h"
#include "d/d_meter2_info.h"
#include "d/d_msg_class.h"
#include "d/d_msg_flow.h"
#include "m_Do/m_Do_audio.h"
namespace {
const char* flow_node_type_name(RandomizerContext::FlowNodeType type) {
switch (type) {
case RandomizerContext::FlowNodeType::MESSAGE:
return "message";
case RandomizerContext::FlowNodeType::BRANCH:
return "branch";
case RandomizerContext::FlowNodeType::EVENT:
return "event";
}
return "";
}
RandomizerContext::FlowNodeType parse_flow_node_type(const YAML::Node& node) {
const auto type = node.as<std::string>();
if (type == "message") {
return RandomizerContext::FlowNodeType::MESSAGE;
}
if (type == "branch") {
return RandomizerContext::FlowNodeType::BRANCH;
}
if (type == "event") {
return RandomizerContext::FlowNodeType::EVENT;
}
throw std::runtime_error("Unknown flow node type: " + type);
}
void write_flow_reference(
YAML::Node node, const RandomizerContext::FlowReference& reference) {
if (reference.nativeId.has_value()) {
node = reference.nativeId.value();
} else {
node = reference.name;
}
}
RandomizerContext::FlowReference parse_flow_reference(const YAML::Node& node) {
RandomizerContext::FlowReference reference{};
try {
reference.nativeId = node.as<u16>();
} catch (const YAML::BadConversion&) {
reference.name = node.as<std::string>();
}
return reference;
}
void write_message_style(
YAML::Node node, const RandomizerContext::MessageStyleData& style) {
node["eventLabelId"] = style.eventLabelId;
node["speaker"] = style.speaker;
node["boxKind"] = style.boxKind;
node["drawType"] = style.drawType;
node["boxPosition"] = style.boxPosition;
node["lineAlignment"] = style.lineAlignment;
node["speakerMood"] = style.speakerMood;
node["cameraAttr"] = style.cameraAttr;
node["talkAnim"] = style.talkAnim;
node["faceAnim"] = style.faceAnim;
node["trailingData"] = style.trailingData;
}
RandomizerContext::MessageStyleData parse_message_style(const YAML::Node& node) {
RandomizerContext::MessageStyleData style{};
style.eventLabelId = node["eventLabelId"].as<u16>();
style.speaker = node["speaker"].as<u8>();
style.boxKind = node["boxKind"].as<u8>();
style.drawType = node["drawType"].as<u8>();
style.boxPosition = node["boxPosition"].as<u8>();
style.lineAlignment = node["lineAlignment"].as<u8>();
style.speakerMood = node["speakerMood"].as<u8>();
style.cameraAttr = node["cameraAttr"].as<u8>();
style.talkAnim = node["talkAnim"].as<u8>();
style.faceAnim = node["faceAnim"].as<u8>();
style.trailingData = node["trailingData"].as<u16>();
return style;
}
} // namespace
std::optional<std::string> RandomizerContext::WriteToFile() {
std::ofstream seedData(this->GetSeedDataPath());
@@ -36,6 +116,7 @@ std::optional<std::string> RandomizerContext::WriteToFile() {
}
YAML::Node out{};
out["formatVersion"] = FORMAT_VERSION;
for (const auto& [setting, option] : this->mSettings) {
out["mSettings"][setting] = option;
@@ -88,23 +169,64 @@ std::optional<std::string> RandomizerContext::WriteToFile() {
for (const auto& [stageRoomLayer, actorPatches] : this->mObjectPatches) {
for (const auto& [actorCRC, actorPatch] : actorPatches) {
out["mObjectPatches"][stageRoomLayer][actorCRC] = ContainerToHexString(actorPatch);
auto node = out["mObjectPatches"][stageRoomLayer][actorCRC];
node["data"] = ContainerToHexString(actorPatch.bytes);
if (!actorPatch.flow.empty()) {
node["flow"] = actorPatch.flow;
}
}
}
for (const auto& [stageRoomLayer, newActors] : this->mObjectAdditions) {
for (const auto& actor : newActors) {
out["mObjectAdditions"][stageRoomLayer].push_back(ContainerToHexString(actor));
YAML::Node node{};
node["data"] = ContainerToHexString(actor.bytes);
if (!actor.flow.empty()) {
node["flow"] = actor.flow;
}
out["mObjectAdditions"][stageRoomLayer].push_back(node);
}
}
out["mFlowPatches"] = this->mFlowPatches;
for (const auto& [key, branchOverrides]: this->mFlowPatchesBranchOverrides) {
for (auto override : branchOverrides) {
out["mFlowPatchesBranchOverrides"][key].push_back(override);
for (const auto& flow : mFlowNodes) {
YAML::Node node{};
node["type"] = flow_node_type_name(flow.type);
node["group"] = flow.group;
if (flow.patchIndex.has_value()) {
node["patchIndex"] = flow.patchIndex.value();
} else {
node["name"] = flow.name;
}
node["parameters"] = flow.parameters;
if (!flow.operation.empty()) {
node["operation"] = flow.operation;
}
if (flow.type == FlowNodeType::MESSAGE) {
write_flow_reference(node["message"], flow.message);
}
if (flow.type != FlowNodeType::BRANCH) {
write_flow_reference(node["next"], flow.next);
}
for (const auto& result : flow.results) {
YAML::Node resultNode{};
write_flow_reference(resultNode, result);
node["results"].push_back(resultNode);
}
out["mFlowNodes"].push_back(node);
}
for (const auto& message : mCustomMessages) {
YAML::Node node{};
node["group"] = message.group;
node["name"] = message.name;
write_message_style(node["style"], message.style);
for (const auto& [language, text] : message.text) {
const auto languageName = randomizer::languageToString(
static_cast<randomizer::Text::Language>(language));
node["text"][languageName] = YAML::Binary(
reinterpret_cast<const unsigned char*>(text.data()), text.size());
}
out["mCustomMessages"].push_back(node);
}
// Dump text overrides as binary to avoid losing intentional null characters
@@ -125,10 +247,6 @@ std::optional<std::string> RandomizerContext::WriteToFile() {
textData << YAML::EndMap;
textData << YAML::EndMap;
for (const auto& [key, override] : mAttributeOverrides) {
out["mAttributeOverrides"][key] = ContainerToHexString(override);
}
for (const auto& [key, override] : mEntranceOverrides) {
out["mEntranceOverrides"][std::bit_cast<uint64_t>(key)] = std::bit_cast<uint64_t>(override);
}
@@ -154,6 +272,11 @@ std::optional<std::string> RandomizerContext::LoadFromHash(const std::string& ha
}
auto in = LoadYAML(this->GetSeedDataPath());
if (!in["formatVersion"] || in["formatVersion"].as<u32>() != FORMAT_VERSION) {
mods::log::error("Seed {} uses an obsolete data format and must be regenerated", hash);
mHash.clear();
return "Seed data format is obsolete; regenerate this seed";
}
// Necessary settings
for (const auto& settingNode : in["mSettings"] ) {
@@ -257,7 +380,11 @@ std::optional<std::string> RandomizerContext::LoadFromHash(const std::string& ha
u32 stageRoomLayer = stageRoomLayerNode.first.as<u32>();
for (const auto& actorPatchNode : stageRoomLayerNode.second) {
u32 actorCRC = actorPatchNode.first.as<u32>();
this->mObjectPatches[stageRoomLayer][actorCRC] = HexToBytes(actorPatchNode.second.as<std::string>());
auto& actor = this->mObjectPatches[stageRoomLayer][actorCRC];
actor.bytes = HexToBytes(actorPatchNode.second["data"].as<std::string>());
if (actorPatchNode.second["flow"]) {
actor.flow = actorPatchNode.second["flow"].as<std::string>();
}
}
}
@@ -265,24 +392,52 @@ std::optional<std::string> RandomizerContext::LoadFromHash(const std::string& ha
for (const auto& stageNode: in["mObjectAdditions"]) {
u32 stageRoomLayer = stageNode.first.as<u32>();
for (const auto& objectData : stageNode.second) {
this->mObjectAdditions[stageRoomLayer].emplace_back(HexToBytes(objectData.as<std::string>()));
ActorData actor{};
actor.bytes = HexToBytes(objectData["data"].as<std::string>());
if (objectData["flow"]) {
actor.flow = objectData["flow"].as<std::string>();
}
this->mObjectAdditions[stageRoomLayer].push_back(std::move(actor));
}
}
// Flow Patches
for (const auto& flowNode: in["mFlowPatches"]) {
auto key = flowNode.first.as<u32>();
auto value = flowNode.second.as<u64>();
this->mFlowPatches[key] = value;
for (const auto& flowNode : in["mFlowNodes"]) {
FlowNode flow{};
flow.type = parse_flow_node_type(flowNode["type"]);
flow.group = flowNode["group"].as<u8>();
if (flowNode["patchIndex"]) {
flow.patchIndex = flowNode["patchIndex"].as<u16>();
} else {
flow.name = flowNode["name"].as<std::string>();
}
flow.parameters = flowNode["parameters"].as<u32>();
if (flowNode["operation"]) {
flow.operation = flowNode["operation"].as<std::string>();
}
if (flowNode["message"]) {
flow.message = parse_flow_reference(flowNode["message"]);
}
if (flowNode["next"]) {
flow.next = parse_flow_reference(flowNode["next"]);
}
for (const auto& result : flowNode["results"]) {
flow.results.push_back(parse_flow_reference(result));
}
mFlowNodes.push_back(std::move(flow));
}
// Flow Patch Branch Overrides
for (const auto& flowNode : in["mFlowPatchesBranchOverrides"]) {
auto key = flowNode.first.as<u32>();
for (const auto& branchNode : flowNode.second) {
auto override = branchNode.as<u16>();
this->mFlowPatchesBranchOverrides[key].push_back(override);
for (const auto& messageNode : in["mCustomMessages"]) {
CustomMessage message{};
message.group = messageNode["group"].as<u8>();
message.name = messageNode["name"].as<std::string>();
message.style = parse_message_style(messageNode["style"]);
for (const auto& textNode : messageNode["text"]) {
const auto language = randomizer::stringToLanguage(textNode.first.as<std::string>());
const auto binary = textNode.second.as<YAML::Binary>();
message.text[language] =
std::string(reinterpret_cast<const char*>(binary.data()), binary.size());
}
mCustomMessages.push_back(std::move(message));
}
// Text Overrides
@@ -297,15 +452,6 @@ std::optional<std::string> RandomizerContext::LoadFromHash(const std::string& ha
}
}
// Attribute Overrides
for (const auto& attributeNode : in["mAttributeOverrides"]) {
auto key = attributeNode.first.as<u32>();
std::vector<u8> overrideVec = HexToBytes(attributeNode.second.as<std::string>());
std::array<u8, 20> override{};
std::copy(overrideVec.begin(), overrideVec.end(), override.begin());
this->mAttributeOverrides[key] = override;
}
// Entrance Overrides
for (const auto& entranceNode : in["mEntranceOverrides"]) {
const auto key = std::bit_cast<EntranceOverride>(entranceNode.first.as<uint64_t>());;
@@ -997,68 +1143,9 @@ void parseObjPatchData(stage_tgsc_data_class& object, const YAML::Node& patchNod
}
}
static std::array<u8, 20> CreateAttributeData(const YAML::Node& node, const std::string& name) {
auto attributesStr = node.as<std::string>();
auto attributesVec = HexToBytes(attributesStr);
if (attributesVec.size() != 16) {
throw std::runtime_error(fmt::format("Attributes for Text Override {} "
"are the wrong length. (Expected: 16, Actual: {}", name, attributesVec.size()));
}
std::array<u8, 20> attributes{};
for (size_t i = 0; i < attributesVec.size(); ++i) {
attributes[i + 4] = attributesVec[i];
}
return attributes;
}
RandomizerContext WriteSeedData(randomizer::logic::world::World* world) {
RandomizerContext randoData{};
// Give custom flows and messages new indices as we read them in/create them
std::unordered_map<std::string, u16> customMessageIDs{};
std::unordered_map<std::string, u16> customFlowIDs{};
std::unordered_set<u16> usedMessageIDs{};
std::unordered_set<u16> usedFlowIDs{};
u16 curCustomMessageID = BASE_CUSTOM_MSG_AND_FLOW_ID;
u16 curCustomFlowID = BASE_CUSTOM_MSG_AND_FLOW_ID;
// Helper functions for assigning new custom flow IDs/message IDs
auto handleCustomID = [](const std::string& name, auto& customIds, auto& usedIds, u16& curCustomID) {
u16 resultIndex{};
// Check to see if we're setting a custom index
auto resultInt = randomizer::utility::str::toInt(name);
// If we have a regular index, then use that directly
if (resultInt.has_value()) {
resultIndex = resultInt.value();
} else {
// If we don't, assume we're setting the index as custom
if (customIds.contains(name)) {
resultIndex = customIds[name];
} else {
while (usedIds.contains(curCustomID)) {
++curCustomID;
}
auto newIndex = curCustomID++;
resultIndex = newIndex;
customIds[name] = newIndex;
}
}
usedIds.insert(resultIndex);
return resultIndex;
};
auto handleCustomFlowID = [&](const std::string& name) {
return handleCustomID(name, customFlowIDs, usedFlowIDs, curCustomFlowID);
};
auto handleCustomMessageID = [&](const std::string& name) {
return handleCustomID(name, customMessageIDs, usedMessageIDs, curCustomMessageID);
};
// Settings we need to check ingame
for (const auto& [setting, info] : *randomizer::seedgen::settings::GetAllSettingsInfo()) {
if (info->NeedInGame()) {
@@ -1268,6 +1355,7 @@ RandomizerContext WriteSeedData(randomizer::logic::world::World* world) {
randoData.mStartHour = 24;
// Actor Patches
std::unordered_map<std::string, u8> objectFlowGroups{};
auto actorPatches = LOAD_EMBED_YAML(RANDO_DATA_PATH "object_patches.yaml");
for (const auto& stageNode : actorPatches) {
const auto& stageName = stageNode.first.as<std::string>();
@@ -1295,20 +1383,36 @@ RandomizerContext WriteSeedData(randomizer::logic::world::World* world) {
u32 objectCRC32 = getStageObjCRC32(reinterpret_cast<u8*>(&object), objDataSize);
// Depending on the action, store data on this actor
std::vector<u8> actorData(0);
RandomizerContext::ActorData actorData{};
if (objectNode["flow"]) {
actorData.flow = objectNode["flow"].as<std::string>();
const int stageId = getStageID(stageName.c_str());
if (stageId < 0 ||
static_cast<size_t>(stageId) >= std::size(allStageMessageGroups))
{
throw std::runtime_error("Unknown stage for flow-bearing actor: " + stageName);
}
const u8 group = allStageMessageGroups[stageId];
const auto [found, inserted] = objectFlowGroups.emplace(actorData.flow, group);
if (!inserted && found->second != group) {
throw std::runtime_error(
"Actor flow is referenced from multiple message groups: " + actorData.flow);
}
object.base.angle.x = 0;
}
// If we're patching this object, Then override the object with whatever parts are being patched
// and add that patch data to our actorData
if (action == "patch") {
parseObjPatchData(object, objectNode["patch"]);
actorData.resize(objDataSize);
std::memcpy(actorData.data(), &object, objDataSize);
actorData.bytes.resize(objDataSize);
std::memcpy(actorData.bytes.data(), &object, objDataSize);
} else if (action == "add") {
// If we're adding the object, add it's regular data to the actorData
actorData.resize(objDataSize);
std::memcpy(actorData.data(), &object, objDataSize);
actorData.bytes.resize(objDataSize);
std::memcpy(actorData.bytes.data(), &object, objDataSize);
} else if (action == "delete") {
// If we're deleting this actor, give it a specific size to indicate we're deleting it
actorData.resize(RandomizerContext::OBJ_DELETE_SIZE);
actorData.bytes.resize(RandomizerContext::OBJ_DELETE_SIZE);
} else {
// Unknown action. Don't continue
throw std::runtime_error("object patch action \"" + action + "\" not recognized");
@@ -1333,228 +1437,224 @@ RandomizerContext WriteSeedData(randomizer::logic::world::World* world) {
}
}
// Flow Patches
auto source_reference = [](const YAML::Node& node) {
RandomizerContext::FlowReference reference{};
const auto value = node.as<std::string>();
if (const auto numeric = randomizer::utility::str::toInt(value); numeric.has_value()) {
if (numeric.value() < 0 || numeric.value() > 0xffff) {
throw std::runtime_error("Flow reference is outside the 16-bit range: " + value);
}
reference.nativeId = static_cast<u16>(numeric.value());
} else {
reference.name = value;
}
return reference;
};
std::unordered_map<std::string, std::unordered_set<u8>> customMessageGroups{};
std::unordered_map<std::string, std::string> splitMessageStyles{};
auto flowPatches = LOAD_EMBED_YAML(RANDO_DATA_PATH "flow_patches.yaml");
for (const auto& groupNode : flowPatches) {
u8 groupNo = groupNode.first.as<u8>();
const auto groupName = groupNode.first.as<std::string>();
const bool customSection = groupName == "custom";
const u8 defaultGroup = customSection ? 0 : groupNode.first.as<u8>();
for (const auto& flowNode : groupNode.second) {
// Check to see if this patch is contingent on a specific setting
if (flowNode["only if"]) {
const auto& condition = flowNode["only if"].as<std::string>();
// If the required condition isn't set, then skip this one
if (!world->EvaluateSettingCondition(condition)) {
continue;
}
if (flowNode["only if"] &&
!world->EvaluateSettingCondition(flowNode["only if"].as<std::string>()))
{
continue;
}
std::string name{};
std::list<u16> indices{};
if (flowNode["index"]) {
// If we're specifying a sequence of indices
if (flowNode["index"].IsSequence()) {
for (const auto& indexNode : flowNode["index"]) {
auto index = indexNode.as<u16>();
indices.push_back(index);
usedFlowIDs.insert(index);
}
name = std::to_string(indices.front());
}
// If we have just a single index
else if (flowNode["index"].IsScalar()) {
auto index = flowNode["index"].as<u16>();
indices.push_back(index);
name = std::to_string(index);
usedFlowIDs.insert(index);
}
// If we're specifying an index as well as a name, add the index to the custom
// ids
if (flowNode["name"]) {
name = flowNode["name"].as<std::string>();
customFlowIDs[name] = indices.front();
RandomizerContext::FlowNode flow{};
flow.type = parse_flow_node_type(flowNode["type"]);
flow.parameters = flowNode["parameters"].as<u32>();
std::vector<u16> patchIndices{};
if (customSection) {
flow.name = flowNode["name"].as<std::string>();
if (flowNode["group"]) {
flow.group = flowNode["group"].as<u8>();
} else if (const auto found = objectFlowGroups.find(flow.name);
found != objectFlowGroups.end())
{
flow.group = found->second;
} else {
throw std::runtime_error("Custom flow has no message group: " + flow.name);
}
} else {
name = flowNode["name"].as<std::string>();
indices.push_back(handleCustomFlowID(flowNode["name"].as<std::string>()));
}
const auto& type = flowNode["type"].as<std::string>();
u64 value{};
if (type == "branch") {
auto branch = reinterpret_cast<mesg_flow_node_branch*>(&value);
branch->type = 2;
branch->result_count = flowNode["num results"].as<u8>();
branch->query_idx = flowNode["query"].as<u16>();
branch->param = flowNode["parameters"].as<u16>();
branch->next_node_idx = flowNode["next node index"].as<u16>();
// If we're using custom result indices
if (flowNode["results"]) {
auto& results = flowNode["results"];
if (results.size() != branch->result_count) {
throw std::runtime_error(fmt::format("Flow results size for {} "
"do not match num results. (expected: {}. size: {})", name, branch->result_count, results.size()));
}
for (const auto& resultNode : results) {
auto resultIndex = handleCustomFlowID(resultNode.as<std::string>());
for (auto index : indices) {
u32 key = (groupNo << 16) | index;
randoData.mFlowPatchesBranchOverrides[key].push_back(resultIndex);
}
flow.group = defaultGroup;
if (flowNode["index"].IsSequence()) {
for (const auto& index : flowNode["index"]) {
patchIndices.push_back(index.as<u16>());
}
} else {
patchIndices.push_back(flowNode["index"].as<u16>());
}
}
else if (type == "event") {
auto event = reinterpret_cast<mesg_flow_node_event*>(&value);
event->type = 3;
event->event_idx = flowNode["event"].as<u8>();
event->next_node_idx = handleCustomFlowID(flowNode["next node index"].as<std::string>());
u32 params = flowNode["parameters"].as<u32>();
event->params[0] = (params >> 24) & 0xFF;
event->params[1] = (params >> 16) & 0xFF;
event->params[2] = (params >> 8) & 0xFF;
event->params[3] = params & 0xFF;
} else if (type == "message") {
auto message = reinterpret_cast<mesg_flow_node*>(&value);
message->type = 1;
message->msg_index = handleCustomMessageID(flowNode["inf index"].as<std::string>());
message->next_node_idx = handleCustomFlowID(flowNode["next flow index"].as<std::string>());
// If a custom message is too long, split it up among additional flow/message nodes
if (message->msg_index >= BASE_CUSTOM_MSG_AND_FLOW_ID) {
auto textName = flowNode["inf index"].as<std::string>();
if (world->GetTextDatabase().contains(textName)) {
auto& text = world->GetTextObject(textName);
if (text.IsTooLong()) {
// Get the attributes for the text at this ID. Pretty inefficient since we
// have to loop through every element unfortunately
std::optional<std::array<u8, 20>> customAttributes{};
auto textOverrides = LOAD_EMBED_YAML(RANDO_DATA_PATH "text/text_overrides.yaml");
for (const auto& overrideNode : textOverrides) {
const auto& overrideName = overrideNode["Name"].as<std::string>();
if (overrideName == textName && overrideNode["Attributes"]) {
customAttributes = CreateAttributeData(overrideNode["Attributes"], textName);
break;
}
if (flow.type == RandomizerContext::FlowNodeType::BRANCH) {
flow.operation = flowNode["query"].as<std::string>();
for (const auto& result : flowNode["results"]) {
flow.results.push_back(source_reference(result));
}
if (flow.parameters > 0xffff || flow.results.empty() ||
flow.results.size() > 0xff)
{
throw std::runtime_error("Flow branch has invalid parameters or results");
}
} else if (flow.type == RandomizerContext::FlowNodeType::EVENT) {
flow.operation = flowNode["event"].as<std::string>();
flow.next = source_reference(flowNode["next"]);
} else {
flow.message = source_reference(flowNode["message"]);
flow.next = source_reference(flowNode["next"]);
if (!flow.message.nativeId.has_value()) {
customMessageGroups[flow.message.name].insert(flow.group);
if (world->GetTextDatabase().contains(flow.message.name)) {
auto& text = world->GetTextObject(flow.message.name);
// The game still owns textbox pagination, so preserve the existing
// per-message limit while representing overflow as ordinary flow nodes.
const auto extraText = text.SplitToFitTextLimits();
if (!extraText.empty()) {
const auto originalNext = flow.next;
std::vector<std::string> extraNames{};
for (size_t i = 0; i < extraText.size(); ++i) {
auto extraName = flow.message.name + std::to_string(i + 1);
world->AddNewText(extraName) = extraText[i];
splitMessageStyles[extraName] = flow.message.name;
customMessageGroups[extraName].insert(flow.group);
extraNames.push_back(std::move(extraName));
}
// Add each split text as a new custom message entry. The original entry
// still exists but has been sliced down to fit properly.
auto extraText = text.SplitToFitTextLimits();
std::vector<mesg_flow_node> newMsgFlows{};
for (size_t i = 0; i < extraText.size(); i++) {
// Add this custom text to the world
auto extraTextName = textName + std::to_string(i + 1);
world->AddNewText(extraTextName) = extraText[i];
// Create new Flow and Message Ids for the split text object
auto newCustomFlowIndex = handleCustomFlowID(extraTextName);
auto newCustomMessageIndex = handleCustomMessageID(extraTextName);
// Create the new flow node. We're storing its own flow index with
// itself for now, but we'll shift it back to the previous node later
mesg_flow_node newMsgFlow{};
newMsgFlow.type = 1;
newMsgFlow.msg_index = newCustomMessageIndex;
newMsgFlow.next_node_idx = newCustomFlowIndex;
newMsgFlows.push_back(newMsgFlow);
//Add the custom text to the rando data
u32 key = (CUSTOM_BMG_GROUP << 16) | newCustomMessageIndex;
for (auto language : randomizer::supportedLanguages) {
std::string newText = extraText[i].mText[language];
randomizer::applyMessageCodes(newText);
randoData.mTextOverrides[language][key] = newText;
}
// Add custom attribute data as well if it exists
if (customAttributes.has_value()) {
auto attributes = customAttributes.value();
// Set the message id in the attribute data
attributes[4] = newCustomMessageIndex >> 8;
attributes[5] = newCustomMessageIndex & 0xFF;
randoData.mAttributeOverrides[key] = attributes;
}
}
// Shift all the next_node_idx fields back a node and set the original
// next node idx as the next node idx for the final of the new flows
auto finalNodeIdx = message->next_node_idx;
message->next_node_idx = newMsgFlows[0].next_node_idx;
for (size_t i = 0; i < newMsgFlows.size(); i++) {
auto& curFlow = newMsgFlows[i];
auto curFlowIdx = curFlow.next_node_idx;
if (i == newMsgFlows.size() - 1) {
curFlow.next_node_idx = finalNodeIdx;
} else {
curFlow.next_node_idx = newMsgFlows[i + 1].next_node_idx;
}
// Also Add the new custom flows to our rando data
u32 key = (CUSTOM_BMG_GROUP << 16) | curFlowIdx;
randoData.mFlowPatches[key] = std::bit_cast<u64>(curFlow);
flow.next = {.name = extraNames.front()};
for (size_t i = 0; i < extraNames.size(); ++i) {
RandomizerContext::FlowNode extraFlow{
.type = RandomizerContext::FlowNodeType::MESSAGE,
.group = flow.group,
.name = extraNames[i],
.message = {.name = extraNames[i]},
.next = i + 1 < extraNames.size() ?
RandomizerContext::FlowReference{
.name = extraNames[i + 1]} :
originalNext,
};
randoData.mFlowNodes.push_back(std::move(extraFlow));
}
}
}
}
}
for (auto index : indices) {
u32 key = (groupNo << 16) | index;
randoData.mFlowPatches[key] = value;
if (customSection) {
randoData.mFlowNodes.push_back(std::move(flow));
} else {
for (const u16 patchIndex : patchIndices) {
auto patch = flow;
patch.patchIndex = patchIndex;
randoData.mFlowNodes.push_back(std::move(patch));
}
}
}
}
// Text Overrides
std::array<std::unordered_set<std::string>, 9> flowNames{};
for (const auto& flow : randoData.mFlowNodes) {
if (flow.group >= flowNames.size()) {
throw std::runtime_error("Flow node has an invalid message group");
}
if (!flow.patchIndex.has_value() &&
(flow.name.empty() || !flowNames[flow.group].insert(flow.name).second))
{
throw std::runtime_error("Custom flow name is empty or duplicated: " + flow.name);
}
}
auto validate_flow_reference = [&](u8 group, const RandomizerContext::FlowReference& reference) {
if (!reference.nativeId.has_value() &&
(reference.name.empty() || !flowNames[group].contains(reference.name)))
{
throw std::runtime_error(fmt::format(
"Unresolved flow reference in group {}: {}", group, reference.name));
}
};
for (const auto& [name, group] : objectFlowGroups) {
validate_flow_reference(group, {.name = name});
}
for (const auto& flow : randoData.mFlowNodes) {
if (flow.type == RandomizerContext::FlowNodeType::BRANCH) {
for (const auto& result : flow.results) {
validate_flow_reference(flow.group, result);
}
} else {
validate_flow_reference(flow.group, flow.next);
}
}
auto parse_style = [](const YAML::Node& node) {
RandomizerContext::MessageStyleData style{};
if (!node) {
return style;
}
auto set = [&](const char* key, auto& field) {
if (node[key]) {
field = node[key].as<std::remove_reference_t<decltype(field)>>();
}
};
set("Event Label", style.eventLabelId);
set("Speaker", style.speaker);
set("Box Kind", style.boxKind);
set("Draw Type", style.drawType);
set("Box Position", style.boxPosition);
set("Line Alignment", style.lineAlignment);
set("Speaker Mood", style.speakerMood);
set("Camera", style.cameraAttr);
set("Talk Animation", style.talkAnim);
set("Face Animation", style.faceAnim);
set("Trailing Data", style.trailingData);
return style;
};
auto textOverrides = LOAD_EMBED_YAML(RANDO_DATA_PATH "text/text_overrides.yaml");
std::unordered_map<std::string, YAML::Node> textDefinitions{};
for (const auto& overrideNode : textOverrides) {
// Check to see if this override is contingent on a specific setting
if (overrideNode["Only If"]) {
const auto& condition = overrideNode["Only If"].as<std::string>();
// If the required condition isn't set, then skip this one
if (!world->EvaluateSettingCondition(condition)) {
continue;
}
const auto name = overrideNode["Name"].as<std::string>();
textDefinitions.emplace(name, overrideNode);
if (!overrideNode["Message Id"] ||
(overrideNode["Only If"] &&
!world->EvaluateSettingCondition(overrideNode["Only If"].as<std::string>())))
{
continue;
}
const auto& name = overrideNode["Name"].as<std::string>();
u8 group;
u16 messageId;
if (overrideNode["Group"]) {
group = overrideNode["Group"].as<u8>();
} else {
// If no group specified, assume custom bmg group
group = CUSTOM_BMG_GROUP;
}
if (overrideNode["Message Id"]) {
messageId = overrideNode["Message Id"].as<u16>();
} else {
// If no message id specified, assume a custom one
messageId = handleCustomMessageID(overrideNode["Name"].as<std::string>());
}
u32 key = (group << 16) | messageId;
for (auto language : randomizer::supportedLanguages) {
std::string text;
if (world->GetTextDatabase().contains(name)) {
text = world->GetText(name, randomizer::Text::STANDARD, language);
} else {
text = randomizer::getTextStr(name, randomizer::Text::STANDARD, language);
}
const u8 group = overrideNode["Group"].as<u8>();
const u16 messageId = overrideNode["Message Id"].as<u16>();
const u32 key = static_cast<u32>(group) << 16 | messageId;
for (const auto language : randomizer::supportedLanguages) {
std::string text = world->GetTextDatabase().contains(name) ?
world->GetText(name, randomizer::Text::STANDARD, language) :
randomizer::getTextStr(name, randomizer::Text::STANDARD, language);
randomizer::applyMessageCodes(text);
randoData.mTextOverrides[language][key] = text;
randoData.mTextOverrides[language][key] = std::move(text);
}
}
// If we have custom attributes
if (overrideNode["Attributes"]) {
auto attributes = CreateAttributeData(overrideNode["Attributes"], name);
// Set the message id in the attribute data
attributes[4] = messageId >> 8;
attributes[5] = messageId & 0xFF;
randoData.mAttributeOverrides[key] = attributes;
for (const auto& [name, groups] : customMessageGroups) {
const auto styleName = splitMessageStyles.contains(name) ? splitMessageStyles[name] : name;
const auto definition = textDefinitions.find(styleName);
if (definition == textDefinitions.end()) {
throw std::runtime_error("Custom flow message has no text definition: " + styleName);
}
for (const u8 group : groups) {
RandomizerContext::CustomMessage message{
.group = group,
.name = name,
.style = parse_style(definition->second["Style"]),
};
for (const auto language : randomizer::supportedLanguages) {
std::string text = world->GetTextDatabase().contains(name) ?
world->GetText(name, randomizer::Text::STANDARD, language) :
randomizer::getTextStr(name, randomizer::Text::STANDARD, language);
randomizer::applyMessageCodes(text);
message.text[language] = std::move(text);
}
randoData.mCustomMessages.push_back(std::move(message));
}
}
+56 -10
View File
@@ -19,6 +19,7 @@
*/
class RandomizerContext {
public:
static constexpr u32 FORMAT_VERSION = 2;
static constexpr size_t ACTR_CRC_SIZE = 32;
static constexpr size_t TGSC_CRC_SIZE = 35; // 3 extra bytes for scale x, y, z
static constexpr size_t OBJ_DELETE_SIZE = 1;
@@ -56,17 +57,62 @@ public:
u8 mStartHour{0};
u8 mMapBits{};
std::unordered_map<u32, std::unordered_map<u32, std::vector<u8>>> mObjectPatches{};
std::unordered_map<u32, std::list<std::vector<u8>>> mObjectAdditions{};
// std::unordered_map<u32, std::unordered_set<u32>> mTgscDeletions{};
std::unordered_map<u32, u64> mFlowPatches{};
std::unordered_map<u32, std::vector<u16>> mFlowPatchesBranchOverrides{};
std::unordered_map<u32, std::array<u8, 20>> mAttributeOverrides{};
struct ActorData {
std::vector<u8> bytes{};
std::string flow{};
};
std::unordered_map<u32, std::unordered_map<u32, ActorData>> mObjectPatches{};
std::unordered_map<u32, std::list<ActorData>> mObjectAdditions{};
// std::unordered_map<u32, std::unordered_set<u32>> mTgscDeletions{};
struct FlowReference {
std::optional<u16> nativeId{};
std::string name{};
};
enum class FlowNodeType : u8 {
MESSAGE,
BRANCH,
EVENT,
};
struct FlowNode {
FlowNodeType type{};
u8 group{};
std::optional<u16> patchIndex{};
std::string name{};
std::string operation{};
FlowReference message{};
FlowReference next{};
std::vector<FlowReference> results{};
u32 parameters{};
};
struct MessageStyleData {
u16 eventLabelId{};
u8 speaker{0x24};
u8 boxKind{};
u8 drawType{};
u8 boxPosition{};
u8 lineAlignment{};
u8 speakerMood{};
u8 cameraAttr{};
u8 talkAnim{0x02};
u8 faceAnim{0x03};
u16 trailingData{0x0400};
};
struct CustomMessage {
u8 group{};
std::string name{};
MessageStyleData style{};
std::unordered_map<int, std::string> text{};
};
std::vector<FlowNode> mFlowNodes{};
std::vector<CustomMessage> mCustomMessages{};
// struct TextOverride {
// std::array<u8, 16> mAttributes{};
// std::string mText{};
// };
// Map of language -> map of key -> string
std::unordered_map<int, std::unordered_map<u32, std::string>> mTextOverrides{};
+7 -6
View File
@@ -292,15 +292,15 @@ void registerStageEdits() {
const u8 room = (key >> 8) & 0xFF;
const s8 layer = static_cast<s8>(key & 0xFF);
for (const auto& [crc, bytes] : patches) {
for (const auto& [crc, actor] : patches) {
StageActorHandle handle{};
ModResult res;
if (bytes.size() == RandomizerContext::OBJ_DELETE_SIZE) {
if (actor.bytes.size() == RandomizerContext::OBJ_DELETE_SIZE) {
res = svc_mng.stage->delete_actor(mod_ctx, stage, room, layer, crc, &handle);
} else {
res = svc_mng.stage->patch_actor(
mod_ctx, stage, room, layer, crc, bytes.data(), bytes.size(), &handle);
res = svc_mng.stage->patch_actor(mod_ctx, stage, room, layer, crc,
actor.bytes.data(), actor.bytes.size(), &handle);
}
if (res == MOD_OK) {
@@ -317,10 +317,11 @@ void registerStageEdits() {
const u8 room = (key >> 8) & 0xFF;
const s8 layer = static_cast<s8>(key & 0xFF);
for (const auto& bytes : additions) {
for (const auto& actor : additions) {
StageActorHandle handle{};
ModResult rt;
rt = svc_mng.stage->add_actor(mod_ctx, stage, room, layer, bytes.data(), bytes.size(), &handle);
rt = svc_mng.stage->add_actor(mod_ctx, stage, room, layer, actor.bytes.data(),
actor.bytes.size(), &handle);
if (rt == MOD_OK) {
s_stage_edits.push_back(handle);
}
+9 -1
View File
@@ -79,4 +79,12 @@ const char allStages[78][8] = {
"R_SP209", // 75
"R_SP300", // 76
"R_SP301" // 77
};
};
// Message groups are the STAG metadata for the corresponding allStages entry.
const unsigned char allStageMessageGroups[78] = {
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 1, 8, 1, 1, 6, 2, 3, 2, 8, 7, 6, 8, 4, 6, 6, 8, 8, 8, 6,
6, 8, 7, 6, 6, 1, 4, 6, 2, 3, 4, 7, 6, 4, 4, 2, 5, 4,
};
+1
View File
@@ -83,3 +83,4 @@ enum StageIDs
};
extern const char allStages[78][8];
extern const unsigned char allStageMessageGroups[78];