#include "GruntyQuestionFactory.h" #include "BKDialogShared.h" #include "BKEmitText.h" #include "Companion.h" #include "spdlog/spdlog.h" #include "types/RawBuffer.h" #include "utils/Decompressor.h" #include "utils/TorchUtils.h" #define GRUNTY_QUESTION_HEADER_1 0x01 #define GRUNTY_QUESTION_HEADER_2 0x03 #define GRUNTY_QUESTION_HEADER_3 0x00 #define GRUNTY_QUESTION_HEADER_4 0x05 #define GRUNTY_QUESTION_HEADER_5 0x00 namespace BK64 { namespace { constexpr int8_t kUsHeader[3] = { GRUNTY_QUESTION_HEADER_1, GRUNTY_QUESTION_HEADER_2, GRUNTY_QUESTION_HEADER_3 }; constexpr int8_t kPalHeader[3] = { 0x03, 0x03, 0x00 }; // One language's question data. Same shape for every language. Unlike quiz options, // these split their leading control pair out of the string. GruntyQuestionLang ParseGruntyBlock(LUS::BinaryReader& reader, const std::string& symbol) { const int textSize = reader.ReadUByte(); auto text = ReadDialogStrings(reader, textSize - kQuestionOptionCount, symbol, "GruntyQuestion"); std::vector options; if (textSize >= kQuestionOptionCount) { for (int i = 0; i < kQuestionOptionCount; i++) { // cmd + length + the two control bytes, then the string past them if (!HasBytes(reader, 4)) { SPDLOG_WARN("[BK64] GruntyQuestion {}: option {} of {} runs past the end of the blob; dropping " "the rest.", symbol, i + 1, kQuestionOptionCount); break; } OptionString optionString; optionString.cmd = reader.ReadUByte(); const auto strLen = reader.ReadUByte(); optionString.unk0 = reader.ReadUByte(); optionString.unk1 = reader.ReadUByte(); if (strLen < 2 || !HasBytes(reader, strLen - 2)) { SPDLOG_WARN("[BK64] GruntyQuestion {}: option {} of {} claims {} bytes the blob doesn't hold; " "dropping the rest.", symbol, i + 1, kQuestionOptionCount, strLen); break; } optionString.str = reader.ReadString(strLen - 2); options.push_back(optionString); } } return { std::move(text), std::move(options) }; } void WriteGruntyLangBlock(LUS::BinaryWriter& writer, const std::vector& text, const std::vector& options) { WriteDialogStrings(writer, text); writer.Write((uint32_t)options.size()); for (const auto& optionString : options) { writer.Write(optionString.cmd); writer.Write(optionString.unk0); writer.Write(optionString.unk1); writer.Write((uint32_t)optionString.str.length()); // [port] Write(string) would prefix the length twice writer.Write((char*)optionString.str.data(), optionString.str.size()); } } void EmitGruntyLang(YAML::Emitter& out, const std::vector& text, const std::vector& options) { out << YAML::Key << "Text"; out << YAML::Value; EmitDialogStringSeq(out, text); out << YAML::Key << "Options"; out << YAML::Value; out << YAML::BeginSeq; for (const auto& [cmd, unk0, unk1, str] : options) { out << YAML::Flow; out << YAML::BeginSeq; out << YAML_HEX((uint32_t)cmd); out << YAML_HEX((uint32_t)unk0); out << YAML_HEX((uint32_t)unk1); EmitText(out, str); out << YAML::EndSeq; } out << YAML::EndSeq; } GruntyQuestionLang ReadGruntyLang(const YAML::Node& lang) { auto text = ReadModdingDialogSeq(lang["Text"]); std::vector options; auto optionsNode = lang["Options"]; for (YAML::iterator it = optionsNode.begin(); it != optionsNode.end(); ++it) { if (options.size() >= kQuestionOptionCount) { SPDLOG_WARN("BK64 GruntyQuestion: Only 3 Options Allowed; extra options ignored"); break; } OptionString optionString; optionString.cmd = (*it)[0].as(); optionString.unk0 = (*it)[1].as(); optionString.unk1 = (*it)[2].as(); optionString.str = DecodeText((*it)[3].as()); optionString.str += '\0'; options.push_back(optionString); } if (options.size() != kQuestionOptionCount) { throw std::runtime_error("BK64 GruntyQuestion: Requires Exactly 3 Options"); } return { std::move(text), std::move(options) }; } } // namespace ExportResult GruntyQuestionCodeExporter::Export(std::ostream& write, std::shared_ptr raw, std::string& entryName, YAML::Node& node, std::string* replacement) { auto offset = GetSafeNode(node, "offset"); auto gruntyQuestion = std::static_pointer_cast(raw); const auto symbol = GetSafeNode(node, "symbol", entryName); write << "u8 " << symbol << "[] = {\n"; write << fourSpaceTab << "GRUNTY_QUESTION_HEADER_1" << ", " << "GRUNTY_QUESTION_HEADER_2" << ", " << "GRUNTY_QUESTION_HEADER_3" << ", " << "GRUNTY_QUESTION_HEADER_4" << ", " << "GRUNTY_QUESTION_HEADER_5" << ",\n"; write << fourSpaceTab << "/* GruntyQuestion */\n"; WriteDialogStringArray(write, gruntyQuestion->mText); write << fourSpaceTab << "/* Options */\n"; write << fourSpaceTab << gruntyQuestion->mOptions.size() << ",\n"; for (const auto& [cmd, unk0, unk1, str] : gruntyQuestion->mOptions) { write << fourSpaceTab << "0x" << FORMAT_HEX((uint32_t)cmd, 2) << ", " << str.length(); write << ", 0x" << FORMAT_HEX((uint32_t)unk0, 2) << ", 0x" << FORMAT_HEX((uint32_t)unk1, 2); WriteEscapedChars(write, str); write << ",\n"; } write << "};\n\n"; return offset; } ExportResult BK64::GruntyQuestionBinaryExporter::Export(std::ostream& write, std::shared_ptr raw, std::string& entryName, YAML::Node& node, std::string* replacement) { auto writer = LUS::BinaryWriter(); const auto gruntyQuestion = std::static_pointer_cast(raw); WriteHeader(writer, Torch::ResourceType::BKGruntyQuestion, 0); // 1 for US/JP, 3 for PAL (EN + FR + DE) const uint32_t langCount = 1 + static_cast(gruntyQuestion->mExtraLangs.size()); writer.Write(langCount); // English always goes first WriteGruntyLangBlock(writer, gruntyQuestion->mText, gruntyQuestion->mOptions); // PAL only: French then German for (const auto& lang : gruntyQuestion->mExtraLangs) { WriteGruntyLangBlock(writer, lang.text, lang.options); } writer.Finish(write); return std::nullopt; } ExportResult BK64::GruntyQuestionModdingExporter::Export(std::ostream& write, std::shared_ptr raw, std::string& entryName, YAML::Node& node, std::string* replacement) { const auto gruntyQuestion = std::static_pointer_cast(raw); const auto symbol = GetSafeNode(node, "symbol", entryName); *replacement += ".yaml"; YAML::Emitter out; out << YAML::BeginMap; out << YAML::Key << symbol; out << YAML::Value; out.SetIndent(2); out << YAML::BeginMap; EmitGruntyLang(out, gruntyQuestion->mText, gruntyQuestion->mOptions); for (size_t i = 0; i < gruntyQuestion->mExtraLangs.size() && i < std::size(kPalExtraLangs); i++) { out << YAML::Key << kPalExtraLangs[i]; out << YAML::Value; out << YAML::BeginMap; EmitGruntyLang(out, gruntyQuestion->mExtraLangs[i].text, gruntyQuestion->mExtraLangs[i].options); out << YAML::EndMap; } out << YAML::EndMap; out << YAML::EndMap; write.write(out.c_str(), out.size()); return std::nullopt; } std::optional> GruntyQuestionFactory::parse(std::vector& buffer, YAML::Node& node) { auto [_, segment] = Decompressor::AutoDecode(node, buffer); LUS::BinaryReader reader(segment.data, segment.size); reader.SetEndianness(Torch::Endianness::Big); const auto symbol = GetSafeNode(node, "symbol"); const auto extraOffsets = SeekQuestionBlock(reader, symbol, "GruntyQuestion", kUsHeader, kPalHeader); if (!extraOffsets.has_value()) { return std::nullopt; } auto english = ParseGruntyBlock(reader, symbol); std::vector extraLangs; for (const uint16_t offset : *extraOffsets) { reader.Seek(offset, LUS::SeekOffsetType::Start); extraLangs.push_back(ParseGruntyBlock(reader, symbol)); } return std::make_shared(std::move(english.text), std::move(english.options), std::move(extraLangs)); } std::optional> GruntyQuestionFactory::parse_modding(std::vector& buffer, YAML::Node& node) { const auto symbol = GetSafeNode(node, "symbol"); const YAML::Node info = YamlEntry(buffer, symbol); auto english = ReadGruntyLang(info); std::vector extraLangs; for (const auto& lang : ReadPalExtraLangs(info, symbol, "GruntyQuestion")) { extraLangs.push_back(ReadGruntyLang(lang)); } return std::make_shared(std::move(english.text), std::move(english.options), std::move(extraLangs)); } } // namespace BK64