#include "QuizQuestionFactory.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 QUIZ_QUESTION_HEADER_1 0x01 #define QUIZ_QUESTION_HEADER_2 0x01 #define QUIZ_QUESTION_HEADER_3 0x02 #define QUIZ_QUESTION_HEADER_4 0x05 #define QUIZ_QUESTION_HEADER_5 0x00 namespace BK64 { namespace { constexpr int8_t kUsHeader[3] = { QUIZ_QUESTION_HEADER_1, QUIZ_QUESTION_HEADER_2, QUIZ_QUESTION_HEADER_3 }; constexpr int8_t kPalHeader[3] = { 0x03, 0x01, 0x02 }; // One language's quiz data. Same layout for every language. The last three entries are // the answer options; their first two bytes are a fixed control pair the game expects, // so they stay part of the string. QuizQuestionLang ParseQuizBlock(LUS::BinaryReader& reader, const std::string& symbol) { const int textSize = reader.ReadUByte(); auto text = ReadDialogStrings(reader, textSize - kQuestionOptionCount, symbol, "QuizQuestion"); auto options = ReadDialogStrings(reader, textSize >= kQuestionOptionCount ? kQuestionOptionCount : 0, symbol, "QuizQuestion option"); return { std::move(text), std::move(options) }; } void WriteQuizLangBlock(LUS::BinaryWriter& writer, const std::vector& text, const std::vector& options) { WriteDialogStrings(writer, text); WriteDialogStrings(writer, options); } void EmitQuizLang(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; // Options expose their leading control pair as separate fields so a pack can // edit the text without touching them. PAL's options have no pair. out << YAML::BeginSeq; for (const auto& [cmd, str] : options) { out << YAML::Flow; out << YAML::BeginSeq; out << YAML_HEX((uint32_t)cmd); if (str.size() >= 2 && static_cast(str[0]) == 0xFD) { out << YAML_HEX((uint32_t)static_cast(str[0])); out << YAML_HEX((uint32_t)static_cast(str[1])); EmitText(out, str.substr(2)); } else { EmitText(out, str); } out << YAML::EndSeq; } out << YAML::EndSeq; } QuizQuestionLang ReadQuizLang(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() >= 3) { SPDLOG_WARN("BK64 QuizQuestion: Only 3 Options Allowed; extra options ignored"); break; } if ((*it).size() != 4 && (*it).size() != 2) { throw std::runtime_error( "BK64 QuizQuestion: option must be [cmd, 0xfd, 0x6c, \"text\"] or [cmd, \"text\"]"); } DialogString optionString; optionString.cmd = (*it)[0].as(); if ((*it).size() == 4) { optionString.str.push_back(static_cast((*it)[1].as())); optionString.str.push_back(static_cast((*it)[2].as())); } optionString.str += DecodeText((*it)[(*it).size() - 1].as()); optionString.str += '\0'; options.push_back(optionString); } if (options.size() != 3) { throw std::runtime_error("BK64 QuizQuestion: Requires Exactly 3 Options"); } return { std::move(text), std::move(options) }; } } // namespace ExportResult QuizQuestionCodeExporter::Export(std::ostream& write, std::shared_ptr raw, std::string& entryName, YAML::Node& node, std::string* replacement) { auto offset = GetSafeNode(node, "offset"); auto quizQuestion = std::static_pointer_cast(raw); const auto symbol = GetSafeNode(node, "symbol", entryName); write << "u8 " << symbol << "[] = {\n"; write << fourSpaceTab << "QUIZ_QUESTION_HEADER_1" << ", " << "QUIZ_QUESTION_HEADER_2" << ", " << "QUIZ_QUESTION_HEADER_3" << ", " << "QUIZ_QUESTION_HEADER_4" << ", " << "QUIZ_QUESTION_HEADER_5" << ",\n"; write << fourSpaceTab << "/* QuizQuestion */\n"; WriteDialogStringArray(write, quizQuestion->mText); write << fourSpaceTab << "/* Options */\n"; WriteDialogStringArray(write, quizQuestion->mOptions); write << "};\n\n"; return offset; } ExportResult BK64::QuizQuestionBinaryExporter::Export(std::ostream& write, std::shared_ptr raw, std::string& entryName, YAML::Node& node, std::string* replacement) { auto writer = LUS::BinaryWriter(); const auto quizQuestion = std::static_pointer_cast(raw); WriteHeader(writer, Torch::ResourceType::BKQuizQuestion, 0); // 1 for US/JP, 3 for PAL (EN + FR + DE) const uint32_t langCount = 1 + static_cast(quizQuestion->mExtraLangs.size()); writer.Write(langCount); // English always goes first WriteQuizLangBlock(writer, quizQuestion->mText, quizQuestion->mOptions); // PAL only: French then German for (const auto& lang : quizQuestion->mExtraLangs) { WriteQuizLangBlock(writer, lang.text, lang.options); } writer.Finish(write); return std::nullopt; } ExportResult BK64::QuizQuestionModdingExporter::Export(std::ostream& write, std::shared_ptr raw, std::string& entryName, YAML::Node& node, std::string* replacement) { const auto quizQuestion = 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; EmitQuizLang(out, quizQuestion->mText, quizQuestion->mOptions); for (size_t i = 0; i < quizQuestion->mExtraLangs.size() && i < std::size(kPalExtraLangs); i++) { out << YAML::Key << kPalExtraLangs[i]; out << YAML::Value; out << YAML::BeginMap; EmitQuizLang(out, quizQuestion->mExtraLangs[i].text, quizQuestion->mExtraLangs[i].options); out << YAML::EndMap; } out << YAML::EndMap; out << YAML::EndMap; write.write(out.c_str(), out.size()); return std::nullopt; } std::optional> QuizQuestionFactory::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, "QuizQuestion", kUsHeader, kPalHeader); if (!extraOffsets.has_value()) { return std::nullopt; } auto english = ParseQuizBlock(reader, symbol); std::vector extraLangs; for (const uint16_t offset : *extraOffsets) { reader.Seek(offset, LUS::SeekOffsetType::Start); extraLangs.push_back(ParseQuizBlock(reader, symbol)); } return std::make_shared(std::move(english.text), std::move(english.options), std::move(extraLangs)); } std::optional> QuizQuestionFactory::parse_modding(std::vector& buffer, YAML::Node& node) { const auto symbol = GetSafeNode(node, "symbol"); const YAML::Node info = YamlEntry(buffer, symbol); auto english = ReadQuizLang(info); std::vector extraLangs; for (const auto& lang : ReadPalExtraLangs(info, symbol, "QuizQuestion")) { extraLangs.push_back(ReadQuizLang(lang)); } return std::make_shared(std::move(english.text), std::move(english.options), std::move(extraLangs)); } } // namespace BK64