Files
Torch/src/Companion.cpp
T

967 lines
35 KiB
C++
Raw Normal View History

2023-08-17 00:40:00 -06:00
#include "Companion.h"
#include "storm/SWrapper.h"
2024-01-30 23:03:23 -06:00
#include "utils/Decompressor.h"
2024-03-12 11:25:51 -05:00
2023-11-15 15:11:15 -06:00
#include "factories/sm64/AnimationFactory.h"
#include "factories/sm64/DialogFactory.h"
#include "factories/sm64/DictionaryFactory.h"
#include "factories/sm64/TextFactory.h"
#include "factories/sm64/GeoLayoutFactory.h"
2023-09-17 23:55:59 -06:00
#include "factories/BankFactory.h"
2023-11-15 15:11:15 -06:00
#include "factories/AudioHeaderFactory.h"
#include "factories/SampleFactory.h"
#include "factories/SequenceFactory.h"
#include "factories/VtxFactory.h"
2023-08-17 00:40:00 -06:00
#include "factories/TextureFactory.h"
2023-11-15 15:11:15 -06:00
#include "factories/DisplayListFactory.h"
2024-03-09 23:12:39 -06:00
#include "factories/DisplayListOverrides.h"
2023-11-15 15:11:15 -06:00
#include "factories/BlobFactory.h"
#include "factories/LightsFactory.h"
2024-03-06 11:46:19 -07:00
#include "factories/mk64/CourseVtx.h"
#include "factories/mk64/Waypoints.h"
#include "factories/mk64/TrackSections.h"
#include "factories/mk64/SpawnData.h"
2023-09-24 13:39:02 -06:00
#include "spdlog/spdlog.h"
2024-02-21 12:51:47 -06:00
#include "hj/sha1.h"
2023-08-17 00:40:00 -06:00
#include <fstream>
#include <iostream>
#include <filesystem>
#include "factories/sf64/ColPolyFactory.h"
2024-03-10 20:15:27 -06:00
#include "factories/sf64/MessageFactory.h"
2024-03-10 21:22:26 -06:00
#include "factories/sf64/MessageLookupFactory.h"
2024-03-12 11:25:51 -05:00
#include "factories/sf64/SkeletonFactory.h"
#include "factories/sf64/AnimFactory.h"
#include "factories/sf64/ScriptFactory.h"
2024-03-12 17:16:29 -05:00
#include "factories/sf64/HitboxFactory.h"
2024-03-13 10:09:54 -05:00
#include "factories/sf64/EnvSettingsFactory.h"
#include "factories/sf64/ObjInitFactory.h"
#include <regex>
2023-08-17 00:40:00 -06:00
2023-09-24 14:06:18 -06:00
using namespace std::chrono;
2023-11-15 15:11:15 -06:00
namespace fs = std::filesystem;
2023-08-17 00:40:00 -06:00
2023-11-15 15:11:15 -06:00
static const std::string regular = "[%Y-%m-%d %H:%M:%S.%e] [%l] %v";
static const std::string line = "[%Y-%m-%d %H:%M:%S.%e] [%l] > %v";
void Companion::Init(const ExportType type) {
2023-08-17 00:40:00 -06:00
2023-09-24 13:39:02 -06:00
spdlog::set_level(spdlog::level::debug);
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%l] %v");
this->gConfig.exporterType = type;
2023-11-15 15:11:15 -06:00
this->RegisterFactory("BLOB", std::make_shared<BlobFactory>());
this->RegisterFactory("TEXTURE", std::make_shared<TextureFactory>());
this->RegisterFactory("VTX", std::make_shared<VtxFactory>());
this->RegisterFactory("LIGHTS", std::make_shared<LightsFactory>());
this->RegisterFactory("GFX", std::make_shared<DListFactory>());
this->RegisterFactory("AUDIO:HEADER", std::make_shared<AudioHeaderFactory>());
this->RegisterFactory("SEQUENCE", std::make_shared<SequenceFactory>());
this->RegisterFactory("SAMPLE", std::make_shared<SampleFactory>());
this->RegisterFactory("BANK", std::make_shared<BankFactory>());
2023-09-17 23:55:59 -06:00
2023-11-15 15:11:15 -06:00
// SM64 specific
this->RegisterFactory("SM64:DIALOG", std::make_shared<SM64::DialogFactory>());
this->RegisterFactory("SM64:TEXT", std::make_shared<SM64::TextFactory>());
this->RegisterFactory("SM64:DICTIONARY", std::make_shared<SM64::DictionaryFactory>());
this->RegisterFactory("SM64:ANIM", std::make_shared<SM64::AnimationFactory>());
this->RegisterFactory("SM64:GEO_LAYOUT", std::make_shared<SM64::GeoLayoutFactory>());
2023-09-17 23:55:59 -06:00
2023-11-15 15:11:15 -06:00
// MK64 specific
2024-03-13 10:21:41 -06:00
this->RegisterFactory("MK64:COURSE_VTX", std::make_shared<MK64::CourseVtxFactory>());
this->RegisterFactory("MK64:TRACK_WAYPOINTS", std::make_shared<MK64::WaypointsFactory>());
this->RegisterFactory("MK64:TRACK_SECTIONS", std::make_shared<MK64::TrackSectionsFactory>());
this->RegisterFactory("MK64:SPAWN_DATA", std::make_shared<MK64::SpawnDataFactory>());
2024-03-10 19:26:21 -05:00
// SF64 specific
2024-03-10 20:43:44 -05:00
this->RegisterFactory("SF64:ANIM", std::make_shared<SF64::AnimFactory>());
2024-03-11 10:58:08 -05:00
this->RegisterFactory("SF64:SKELETON", std::make_shared<SF64::SkeletonFactory>());
2024-03-10 20:15:27 -06:00
this->RegisterFactory("SF64:MESSAGE", std::make_shared<SF64::MessageFactory>());
2024-03-10 21:22:26 -06:00
this->RegisterFactory("SF64:MSG_TABLE", std::make_shared<SF64::MessageLookupFactory>());
2024-03-12 11:25:51 -05:00
this->RegisterFactory("SF64:SCRIPT", std::make_shared<SF64::ScriptFactory>());
2024-03-12 17:16:29 -05:00
this->RegisterFactory("SF64:HITBOX", std::make_shared<SF64::HitboxFactory>());
2024-03-13 10:11:54 -06:00
this->RegisterFactory("SF64:ENV_SETTINGS", std::make_shared<SF64::EnvSettingsFactory>());
this->RegisterFactory("SF64:OBJECT_INIT", std::make_shared<SF64::ObjInitFactory>());
this->RegisterFactory("SF64:COLPOLY", std::make_shared<SF64::ColPolyFactory>());
2024-03-12 11:25:51 -05:00
2023-09-17 23:55:59 -06:00
this->Process();
}
void Companion::ParseEnums(std::string& header) {
std::ifstream file(header);
if (!file.is_open()) {
throw std::runtime_error("Failed to open file");
}
std::regex enumRegex(R"(enum\s+(\w+)\s*(?:\s*:\s*(\w+))?[\s\n\r]*\{)");
std::string line;
std::smatch match;
std::string enumName;
bool inEnum = false;
int enumIndex;
while (std::getline(file, line)) {
if (!inEnum && std::regex_search(line, match, enumRegex) && match.size() > 1) {
enumName = match.str(1);
inEnum = true;
enumIndex = -1;
continue;
}
if(!inEnum) {
continue;
}
if(line.find("}") != std::string::npos) {
inEnum = false;
continue;
}
// Remove any comments and non-alphanumeric characters
line = std::regex_replace(line, std::regex(R"((/\*.*?\*/)|(//.*$)|([^a-zA-Z0-9=_\-\.]))"), "");
if(line.find("=") != std::string::npos) {
auto value = line.substr(line.find("=") + 1);
auto name = line.substr(0, line.find("="));
enumIndex = std::stoi(value);
this->gEnums[enumName][enumIndex] = name;
} else {
enumIndex++;
this->gEnums[enumName][enumIndex] = line;
}
}
}
2023-11-15 15:11:15 -06:00
void Companion::ExtractNode(YAML::Node& node, std::string& name, SWrapper* binary) {
std::ostringstream stream;
2024-01-30 23:03:23 -06:00
auto type = GetSafeNode<std::string>(node, "type");
2023-11-15 15:11:15 -06:00
std::transform(type.begin(), type.end(), type.begin(), ::toupper);
2024-01-31 11:17:29 -06:00
spdlog::set_pattern(regular);
if(node["offset"]) {
auto offset = node["offset"].as<uint32_t>();
2024-01-31 11:17:29 -06:00
SPDLOG_INFO("- [{}] Processing {} at 0x{:X}", type, name, offset);
} else {
2024-01-31 11:17:29 -06:00
SPDLOG_INFO("- [{}] Processing {}", type, name);
}
2024-01-31 11:17:29 -06:00
spdlog::set_pattern(line);
2023-11-15 15:11:15 -06:00
auto factory = this->GetFactory(type);
if(!factory.has_value()){
SPDLOG_ERROR("No factory found for {}", name);
return;
}
2024-02-21 12:51:47 -06:00
auto impl = factory->get();
std::optional<std::shared_ptr<IParsedData>> result;
if(this->gConfig.modding) {
if(impl->SupportModdedAssets() && this->gModdedAssetPaths.contains(name)) {
auto path = fs::path(this->gConfig.moddingPath) / this->gModdedAssetPaths[name];
if(!fs::exists(path)) {
SPDLOG_ERROR("Modded asset {} not found", this->gModdedAssetPaths[name]);
return;
}
std::ifstream input(path, std::ios::binary);
std::vector<uint8_t> data = std::vector<uint8_t>( std::istreambuf_iterator( input ), {});
input.close();
result = factory->get()->parse_modding(data, node);
} else {
result = factory->get()->parse(this->gRomData, node);
}
} else {
result = factory->get()->parse(this->gRomData, node);
}
2023-11-15 15:11:15 -06:00
if(!result.has_value()){
SPDLOG_ERROR("Failed to process {}", name);
return;
}
auto exporter = factory->get()->GetExporter(this->gConfig.exporterType);
2023-11-15 15:11:15 -06:00
if(!exporter.has_value()){
2024-03-12 22:42:00 -06:00
SPDLOG_WARN("No exporter found for {}", name);
2023-11-15 15:11:15 -06:00
return;
}
for (auto [fst, snd] : this->gAssetDependencies[this->gCurrentFile]) {
if(snd.second) {
continue;
}
2023-11-15 15:11:15 -06:00
std::string doutput = (this->gCurrentDirectory / fst).string();
std::replace(doutput.begin(), doutput.end(), '\\', '/');
this->gAssetDependencies[this->gCurrentFile][fst].second = true;
this->ExtractNode(snd.first, doutput, binary);
2024-01-31 11:17:29 -06:00
spdlog::set_pattern(regular);
SPDLOG_INFO("------------------------------------------------");
spdlog::set_pattern(line);
2023-11-15 15:11:15 -06:00
}
switch (this->gConfig.exporterType) {
2023-11-15 15:11:15 -06:00
case ExportType::Binary: {
if(binary == nullptr) {
break;
}
2023-11-15 15:11:15 -06:00
stream.str("");
stream.clear();
exporter->get()->Export(stream, result.value(), name, node, &name);
auto data = stream.str();
binary->CreateFile(name, std::vector(data.begin(), data.end()));
break;
}
2024-02-21 12:51:47 -06:00
case ExportType::Modding: {
stream.str("");
stream.clear();
std::string ogname = name;
exporter->get()->Export(stream, result.value(), name, node, &name);
auto data = stream.str();
if(data.empty()) {
break;
}
std::string dpath = Instance->GetOutputPath() + "/" + name;
if(!exists(fs::path(dpath).parent_path())){
create_directories(fs::path(dpath).parent_path());
}
this->gModdedAssetPaths[ogname] = name;
std::ofstream file(dpath, std::ios::binary);
file.write(data.c_str(), data.size());
file.close();
break;
}
2023-11-15 15:11:15 -06:00
default: {
exporter->get()->Export(stream, result.value(), name, node, &name);
2024-02-20 17:28:22 -06:00
if(this->gConfig.exporterType == ExportType::Code) {
if(node["pad"]){
2024-03-05 00:09:30 -06:00
auto filename = this->gCurrentDirectory.filename().string();
2024-02-20 17:28:22 -06:00
auto pad = GetSafeNode<uint32_t>(node, "pad");
2024-03-05 00:57:56 -06:00
stream << "char pad_" << filename << "_" << std::to_string(gCurrentPad++) << "[] = {\n" << tab;
2024-02-20 17:28:22 -06:00
for(int i = 0; i < pad; i++){
stream << "0x00, ";
}
2024-02-20 17:30:51 -06:00
stream << "\n};\n\n";
2024-02-20 17:28:22 -06:00
}
}
2023-11-15 15:11:15 -06:00
break;
}
}
SPDLOG_INFO("Processed {}", name);
if(node["offset"]) {
this->gWriteMap[this->gCurrentFile][type].emplace_back(node["offset"].as<uint32_t>(), stream.str());
}
2023-11-15 15:11:15 -06:00
}
2024-02-21 12:51:47 -06:00
void Companion::ParseModdingConfig() {
auto path = fs::path(this->gConfig.moddingPath) / "modding.yml";
if(!fs::exists(path)) {
throw std::runtime_error("No modding config found, please run in export mode first");
}
2024-02-21 21:05:28 -06:00
auto modding = YAML::LoadFile(path.string());
2024-02-21 12:51:47 -06:00
for(auto assets = modding["assets"].begin(); assets != modding["assets"].end(); ++assets) {
auto name = assets->first.as<std::string>();
auto asset = assets->second.as<std::string>();
this->gModdedAssetPaths[name] = asset;
}
}
void Companion::ParseCurrentFileConfig(YAML::Node node) {
if(node["segments"]) {
auto segments = node["segments"];
// Set global variables for segmented data
if (segments.IsSequence() && segments.size()) {
if (segments[0].IsSequence() && segments[0].size() == 2) {
gCurrentSegmentNumber = segments[0][0].as<uint32_t>();
gCurrentFileOffset = segments[0][1].as<uint32_t>();
gCurrentCompressionType = GetCompressionType(this->gRomData, gCurrentFileOffset);
} else {
2024-03-09 22:42:25 -07:00
throw std::runtime_error("Incorrect yaml syntax for segments.\n\nThe yaml expects:\n:config:\n segments:\n - [<segment>, <file_offset>]\n\nLike so:\nsegments:\n - [0x06, 0x821D10]");
}
}
// Set file offset for later use.
for(size_t i = 0; i < segments.size(); i++) {
auto segment = segments[i];
if (segment.IsSequence() && segment.size() == 2) {
const auto id = segment[0].as<uint32_t>();
const auto replacement = segment[1].as<uint32_t>();
this->gConfig.segment.local[id] = replacement;
SPDLOG_DEBUG("Segment {} replaced with 0x{:X}", id, replacement);
} else {
2024-03-09 22:42:25 -07:00
throw std::runtime_error("Incorrect yaml syntax for segments.\n\nThe yaml expects:\n:config:\n segments:\n - [<segment>, <file_offset>]\n\nLike so:\nsegments:\n - [0x06, 0x821D10]");
}
}
}
if(node["header"]) {
2024-02-18 16:54:21 -06:00
auto header = node["header"];
switch (this->gConfig.exporterType) {
case ExportType::Header: {
if(header["header"].IsSequence()) {
for(auto line = header["header"].begin(); line != header["header"].end(); ++line) {
this->gFileHeader += line->as<std::string>() + "\n";
}
}
break;
}
case ExportType::Code: {
if(header["code"].IsSequence()) {
for(auto line = header["code"].begin(); line != header["code"].end(); ++line) {
this->gFileHeader += line->as<std::string>() + "\n";
}
}
break;
}
default: break;
}
}
if(node["tables"]){
for(auto table = node["tables"].begin(); table != node["tables"].end(); ++table){
auto name = table->first.as<std::string>();
auto range = table->second["range"].as<std::vector<uint32_t>>();
auto start = gCurrentSegmentNumber ? gCurrentSegmentNumber << 24 | range[0] : range[0];
auto end = gCurrentSegmentNumber ? gCurrentSegmentNumber << 24 | range[1] : range[1];
auto mode = GetSafeNode<std::string>(table->second, "mode", "APPEND");
TableMode tMode = mode == "REFERENCE" ? TableMode::Reference : TableMode::Append;
this->gTables.push_back({name, start, end, tMode});
}
}
2024-03-12 19:09:40 -06:00
if(node["vram"]){
auto vram = node["vram"];
const auto addr = GetSafeNode<uint32_t>(vram, "addr");
const auto offset = GetSafeNode<uint32_t>(vram, "offset");
this->gCurrentVram = { addr, offset };
}
}
2023-09-17 23:55:59 -06:00
void Companion::Process() {
2023-08-17 00:40:00 -06:00
2023-11-15 15:11:15 -06:00
if(!fs::exists("config.yml")) {
SPDLOG_ERROR("No config file found");
return;
}
2023-09-24 14:26:01 -06:00
2023-09-24 14:06:18 -06:00
auto start = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
2023-08-17 00:40:00 -06:00
std::ifstream input( this->gRomPath, std::ios::binary );
2023-11-15 15:11:15 -06:00
this->gRomData = std::vector<uint8_t>( std::istreambuf_iterator( input ), {} );
2023-08-17 00:40:00 -06:00
input.close();
this->gCartridge = std::make_shared<N64::Cartridge>(this->gRomData);
this->gCartridge->Initialize();
2023-08-17 00:40:00 -06:00
2023-09-17 23:55:59 -06:00
YAML::Node config = YAML::LoadFile("config.yml");
2023-08-17 00:40:00 -06:00
if(!config[this->gCartridge->GetHash()]){
SPDLOG_ERROR("No config found for {}", this->gCartridge->GetHash());
2023-09-17 23:55:59 -06:00
return;
}
auto rom = config[this->gCartridge->GetHash()];
2023-11-15 15:11:15 -06:00
auto cfg = rom["config"];
if(!cfg) {
SPDLOG_ERROR("No config found for {}", this->gCartridge->GetHash());
2023-11-15 15:11:15 -06:00
return;
}
if(rom["segments"]) {
auto segments = rom["segments"].as<std::vector<uint32_t>>();
for (int i = 0; i < segments.size(); i++) {
this->gConfig.segment.global[i + 1] = segments[i];
}
2023-11-15 15:11:15 -06:00
}
auto path = rom["path"].as<std::string>();
auto opath = cfg["output"];
auto gbi = cfg["gbi"];
2024-02-21 12:51:47 -06:00
auto modding_path = opath && opath["modding"] ? opath["modding"].as<std::string>() : "modding";
2023-11-15 15:11:15 -06:00
2024-02-21 12:51:47 -06:00
this->gConfig.moddingPath = modding_path;
switch (this->gConfig.exporterType) {
2023-11-15 15:11:15 -06:00
case ExportType::Binary: {
this->gConfig.outputPath = opath && opath["binary"] ? opath["binary"].as<std::string>() : "generic.otr";
2023-11-15 15:11:15 -06:00
break;
}
case ExportType::Header: {
this->gConfig.outputPath = opath && opath["headers"] ? opath["headers"].as<std::string>() : "headers";
2023-11-15 15:11:15 -06:00
break;
}
case ExportType::Code: {
this->gConfig.outputPath = opath && opath["code"] ? opath["code"].as<std::string>() : "code";
2023-11-15 15:11:15 -06:00
break;
}
2024-02-21 12:51:47 -06:00
case ExportType::Modding: {
this->gConfig.outputPath = modding_path;
break;
}
2023-11-15 15:11:15 -06:00
}
if(gbi) {
auto key = gbi.as<std::string>();
if(key == "F3D") {
this->gConfig.gbi.version = GBIVersion::f3d;
2023-11-15 15:11:15 -06:00
} else if(key == "F3DEX") {
this->gConfig.gbi.version = GBIVersion::f3dex;
2023-11-15 15:11:15 -06:00
} else if(key == "F3DB") {
this->gConfig.gbi.version = GBIVersion::f3db;
2023-11-15 15:11:15 -06:00
} else if(key == "F3DEX2") {
this->gConfig.gbi.version = GBIVersion::f3dex2;
2023-11-15 15:11:15 -06:00
} else if(key == "F3DEXB") {
this->gConfig.gbi.version = GBIVersion::f3dexb;
2023-11-15 15:11:15 -06:00
} else if (key == "F3DEX_MK64") {
this->gConfig.gbi.version = GBIVersion::f3dex;
this->gConfig.gbi.subversion = GBIMinorVersion::Mk64;
2023-11-15 15:11:15 -06:00
} else {
SPDLOG_ERROR("Invalid GBI version");
return;
}
}
if(auto sort = cfg["sort"]) {
if(sort.IsSequence()) {
this->gWriteOrder = sort.as<std::vector<std::string>>();
} else {
this->gWriteOrder = sort.as<std::string>();
}
} else {
this->gWriteOrder = std::vector<std::string> {
"LIGHTS", "TEXTURE", "VTX", "GFX"
};
}
2024-02-21 12:51:47 -06:00
if(this->gConfig.exporterType == ExportType::Code && this->gConfig.modding) {
this->ParseModdingConfig();
}
2023-11-15 15:11:15 -06:00
if(std::holds_alternative<std::vector<std::string>>(this->gWriteOrder)) {
for (auto& [key, _] : this->gFactories) {
auto entries = std::get<std::vector<std::string>>(this->gWriteOrder);
if(std::find(entries.begin(), entries.end(), key) != entries.end()) {
continue;
}
entries.push_back(key);
}
}
2023-08-17 00:40:00 -06:00
if(cfg["enums"]) {
auto enums = GetSafeNode<std::vector<std::string>>(cfg, "enums");
for (auto& file : enums) {
this->ParseEnums(file);
}
}
2024-03-12 22:42:00 -06:00
if(cfg["logging"]){
auto level = cfg["logging"].as<std::string>();
if(level == "TRACE") {
spdlog::set_level(spdlog::level::trace);
} else if(level == "DEBUG") {
spdlog::set_level(spdlog::level::debug);
} else if(level == "INFO") {
spdlog::set_level(spdlog::level::info);
} else if(level == "WARN") {
spdlog::set_level(spdlog::level::warn);
} else if(level == "ERROR") {
spdlog::set_level(spdlog::level::err);
} else if(level == "CRITICAL") {
spdlog::set_level(spdlog::level::critical);
} else if(level == "OFF") {
spdlog::set_level(spdlog::level::off);
} else {
throw std::runtime_error("Invalid logging level, please use TRACE, DEBUG, INFO, WARN, ERROR, CRITICAL or OFF");
}
}
2023-09-24 14:06:18 -06:00
SPDLOG_INFO("------------------------------------------------");
2023-09-24 14:26:01 -06:00
spdlog::set_pattern(line);
2023-09-24 13:39:02 -06:00
2023-11-15 15:11:15 -06:00
SPDLOG_INFO("Starting Torch...");
SPDLOG_INFO("Game: {}", this->gCartridge->GetGameTitle());
SPDLOG_INFO("CRC: {}", this->gCartridge->GetCRC());
SPDLOG_INFO("Version: {}", this->gCartridge->GetVersion());
SPDLOG_INFO("Country: [{}]", this->gCartridge->GetCountryCode());
SPDLOG_INFO("Hash: {}", this->gCartridge->GetHash());
2023-09-24 13:39:02 -06:00
SPDLOG_INFO("Assets: {}", path);
2024-02-01 17:37:13 -06:00
AudioManager::Instance = new AudioManager();
auto wrapper = this->gConfig.exporterType == ExportType::Binary ? new SWrapper(this->gConfig.outputPath) : nullptr;
2023-08-17 00:40:00 -06:00
2023-09-18 03:39:03 -06:00
auto vWriter = LUS::BinaryWriter();
vWriter.SetEndianness(LUS::Endianness::Big);
2023-11-15 15:11:15 -06:00
vWriter.Write(static_cast<uint8_t>(LUS::Endianness::Big));
vWriter.Write(this->gCartridge->GetCRC());
2023-09-18 03:39:03 -06:00
2023-11-15 15:11:15 -06:00
for (const auto & entry : fs::recursive_directory_iterator(path)){
if(entry.is_directory()) {
continue;
}
const auto yamlPath = entry.path().string();
if(yamlPath.find(".yaml") == std::string::npos && yamlPath.find(".yml") == std::string::npos) {
continue;
}
YAML::Node root = YAML::LoadFile(yamlPath);
2023-11-15 15:11:15 -06:00
this->gCurrentDirectory = relative(entry.path(), path).replace_extension("");
this->gCurrentFile = yamlPath;
2023-08-17 00:40:00 -06:00
// Set compressed file offsets and compression type
if (auto segments = root[":config"]["segments"]) {
if (segments.IsSequence() && segments.size() > 0) {
if (segments[0].IsSequence() && segments[0].size() == 2) {
gCurrentSegmentNumber = segments[0][0].as<uint32_t>();
gCurrentFileOffset = segments[0][1].as<uint32_t>();
gCurrentCompressionType = GetCompressionType(this->gRomData, gCurrentFileOffset);
} else {
2024-03-09 22:42:25 -07:00
throw std::runtime_error("Incorrect yaml syntax for segments.\n\nThe yaml expects:\n:config:\n segments:\n - [<segment>, <file_offset>]\n\nLike so:\nsegments:\n - [0x06, 0x821D10]");
}
}
}
2023-11-15 15:11:15 -06:00
for(auto asset = root.begin(); asset != root.end(); ++asset){
auto node = asset->second;
2024-02-01 17:37:13 -06:00
auto entryName = asset->first.as<std::string>();
2024-02-01 17:37:13 -06:00
// Parse horizontal assets
if(node["files"]){
auto segment = node["segment"] ? node["segment"].as<uint8_t>() : -1;
const auto files = node["files"];
for (const auto& file : files) {
auto assetNode = file.as<YAML::Node>();
auto childName = assetNode["name"].as<std::string>();
auto output = (this->gCurrentDirectory / entryName / childName).string();
std::replace(output.begin(), output.end(), '\\', '/');
if(assetNode["type"]){
const auto type = GetSafeNode<std::string>(assetNode, "type");
if(type == "SAMPLE"){
AudioManager::Instance->bind_sample(assetNode, output);
}
}
if(!assetNode["offset"]) {
continue;
}
if(segment != -1 || gCurrentSegmentNumber) {
2024-02-01 17:37:13 -06:00
assetNode["offset"] = (segment << 24) | assetNode["offset"].as<uint32_t>();
}
this->gAddrMap[this->gCurrentFile][assetNode["offset"].as<uint32_t>()] = std::make_tuple(output, assetNode);
}
} else {
2024-02-01 17:37:13 -06:00
auto output = (this->gCurrentDirectory / entryName).string();
std::replace(output.begin(), output.end(), '\\', '/');
2024-02-01 17:37:13 -06:00
if(node["type"]){
const auto type = GetSafeNode<std::string>(node, "type");
if(type == "SAMPLE"){
AudioManager::Instance->bind_sample(node, output);
}
}
if(!node["offset"]) {
continue;
}
if(gCurrentSegmentNumber) {
if (IS_SEGMENTED(node["offset"].as<uint32_t>()) == false) {
node["offset"] = (gCurrentSegmentNumber << 24) | node["offset"].as<uint32_t>();
}
}
2024-02-01 17:37:13 -06:00
this->gAddrMap[this->gCurrentFile][node["offset"].as<uint32_t>()] = std::make_tuple(output, node);
}
2023-11-15 15:11:15 -06:00
}
// Stupid hack because the iteration broke the assets
root = YAML::LoadFile(yamlPath);
this->gConfig.segment.local.clear();
this->gFileHeader.clear();
2024-03-05 00:09:30 -06:00
this->gCurrentPad = 0;
2024-03-12 19:09:40 -06:00
this->gCurrentVram = std::nullopt;
this->gCurrentSegmentNumber = 0;
this->gTables.clear();
2024-03-09 23:12:39 -06:00
GFXDOverride::ClearVtx();
if(root[":config"]) {
this->ParseCurrentFileConfig(root[":config"]);
}
spdlog::set_pattern(regular);
SPDLOG_INFO("------------------------------------------------");
spdlog::set_pattern(line);
2023-11-15 15:11:15 -06:00
for(auto asset = root.begin(); asset != root.end(); ++asset){
2023-08-19 23:55:59 -06:00
2023-11-15 15:11:15 -06:00
auto entryName = asset->first.as<std::string>();
2024-01-31 10:26:10 -06:00
auto assetNode = asset->second;
2023-11-15 15:11:15 -06:00
2024-01-31 10:26:10 -06:00
if(entryName.find(":config") != std::string::npos) {
continue;
}
2024-01-31 10:26:10 -06:00
// Parse horizontal assets
if(assetNode["files"]){
auto segment = assetNode["segment"] ? assetNode["segment"].as<uint8_t>() : -1;
auto files = assetNode["files"];
for (const auto& file : files) {
auto node = file.as<YAML::Node>();
auto childName = node["name"].as<std::string>();
2024-01-31 10:26:10 -06:00
if(!node["offset"]) {
continue;
}
if(segment != -1 || gCurrentFileOffset) {
2024-01-31 10:26:10 -06:00
node["offset"] = (segment << 24) | node["offset"].as<uint32_t>();
}
auto output = (this->gCurrentDirectory / entryName / childName).string();
std::replace(output.begin(), output.end(), '\\', '/');
this->gConfig.segment.temporal.clear();
this->ExtractNode(node, output, wrapper);
}
} else {
2024-03-12 19:09:40 -06:00
const auto offset = assetNode["offset"].as<uint32_t>();
if(gCurrentFileOffset) {
2024-03-12 19:09:40 -06:00
if (IS_SEGMENTED(offset) == false) {
assetNode["offset"] = (gCurrentSegmentNumber << 24) | offset;
}
}
2024-01-31 10:26:10 -06:00
std::string output = (this->gCurrentDirectory / entryName).string();
std::replace(output.begin(), output.end(), '\\', '/');
this->gConfig.segment.temporal.clear();
this->ExtractNode(assetNode, output, wrapper);
}
spdlog::set_pattern(regular);
SPDLOG_INFO("------------------------------------------------");
spdlog::set_pattern(line);
2023-11-15 15:11:15 -06:00
}
2024-02-21 12:51:47 -06:00
auto fsout = fs::path(this->gConfig.outputPath);
if(this->gConfig.exporterType == ExportType::Modding) {
fsout /= "modding.yml";
YAML::Node modding;
for (const auto& [key, value] : this->gModdedAssetPaths) {
modding["assets"][key] = value;
}
std::ofstream file(fsout.string(), std::ios::binary);
file << modding;
file.close();
} else if(this->gConfig.exporterType != ExportType::Binary){
2024-02-18 16:54:21 -06:00
std::string filename = this->gCurrentDirectory.filename().string();
2023-11-15 15:11:15 -06:00
switch (this->gConfig.exporterType) {
2023-11-15 15:11:15 -06:00
case ExportType::Header: {
2024-02-18 16:54:21 -06:00
fsout /= filename + ".h";
2023-11-15 15:11:15 -06:00
break;
}
case ExportType::Code: {
fsout /= this->gCurrentDirectory / (filename + ".c");
2023-11-15 15:11:15 -06:00
break;
}
default: break;
}
std::ostringstream stream;
if(std::holds_alternative<std::string>(this->gWriteOrder)) {
auto sort = std::get<std::string>(this->gWriteOrder);
std::vector<std::pair<uint32_t, std::string>> outbuf;
for (const auto& [type, buffer] : this->gWriteMap[this->gCurrentFile]) {
outbuf.insert(outbuf.end(), buffer.begin(), buffer.end());
}
this->gWriteMap.clear();
if(sort == "OFFSET") {
std::sort(outbuf.begin(), outbuf.end(), [](const auto& a, const auto& b) {
return std::get<uint32_t>(a) < std::get<uint32_t>(b);
});
} else if(sort == "ROFFSET") {
std::sort(outbuf.begin(), outbuf.end(), [](const auto& a, const auto& b) {
return std::get<uint32_t>(a) > std::get<uint32_t>(b);
});
} else if(sort != "LINEAR") {
throw std::runtime_error("Invalid write order");
}
for (auto& [symbol, buffer] : outbuf) {
stream << buffer;
}
outbuf.clear();
} else {
for (const auto& type : std::get<std::vector<std::string>>(this->gWriteOrder)) {
std::vector<std::pair<uint32_t, std::string>> outbuf = this->gWriteMap[this->gCurrentFile][type];
std::sort(outbuf.begin(), outbuf.end(), [](const auto& a, const auto& b) {
return std::get<uint32_t>(a) > std::get<uint32_t>(b);
});
for (auto& [symbol, buffer] : outbuf) {
stream << buffer;
}
}
}
std::string buffer = stream.str();
if(buffer.empty()) {
2023-09-17 23:55:59 -06:00
continue;
2023-08-30 21:26:38 -06:00
}
2023-09-17 23:55:59 -06:00
2024-02-18 16:54:21 -06:00
std::string output = fsout.string();
2023-09-24 03:28:51 -06:00
std::replace(output.begin(), output.end(), '\\', '/');
2023-11-15 15:11:15 -06:00
if(!exists(fs::path(output).parent_path())){
create_directories(fs::path(output).parent_path());
2023-09-17 23:55:59 -06:00
}
2023-11-15 15:11:15 -06:00
std::ofstream file(output, std::ios::binary);
2023-09-17 23:55:59 -06:00
if(this->gConfig.exporterType == ExportType::Header) {
2023-11-17 09:39:13 -05:00
std::string symbol = entry.path().stem().string();
2023-11-15 15:11:15 -06:00
std::transform(symbol.begin(), symbol.end(), symbol.begin(), toupper);
file << "#ifndef " << symbol << "_H" << std::endl;
file << "#define " << symbol << "_H" << std::endl << std::endl;
2024-02-18 16:54:21 -06:00
if(!this->gFileHeader.empty()) {
file << this->gFileHeader << std::endl;
}
2023-11-15 15:11:15 -06:00
file << buffer;
file << std::endl << "#endif" << std::endl;
} else {
if(!this->gFileHeader.empty()) {
file << this->gFileHeader << std::endl;
}
2023-11-15 15:11:15 -06:00
file << buffer;
}
file.close();
2023-08-19 23:55:59 -06:00
}
2023-08-17 00:40:00 -06:00
}
2023-11-15 15:11:15 -06:00
if(wrapper != nullptr) {
SPDLOG_INFO("Writing version file");
wrapper->CreateFile("version", vWriter.ToVector());
vWriter.Close();
wrapper->Close();
}
2023-09-24 14:06:18 -06:00
auto end = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
2024-03-12 22:42:00 -06:00
auto level = spdlog::get_level();
spdlog::set_level(spdlog::level::info);
2023-09-24 14:06:18 -06:00
SPDLOG_INFO("Done! Took {}ms", end.count() - start.count());
2024-03-12 22:42:00 -06:00
spdlog::set_level(level);
2023-09-24 14:26:01 -06:00
spdlog::set_pattern(regular);
2023-09-24 13:39:02 -06:00
SPDLOG_INFO("------------------------------------------------");
2023-09-18 03:39:03 -06:00
2024-01-30 23:03:23 -06:00
Decompressor::ClearCache();
2023-11-15 15:11:15 -06:00
this->gCartridge = nullptr;
Instance = nullptr;
2023-09-17 23:55:59 -06:00
}
void Companion::Pack(const std::string& folder, const std::string& output) {
2023-11-15 15:11:15 -06:00
spdlog::set_level(spdlog::level::debug);
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%l] %v");
2023-11-15 15:11:15 -06:00
SPDLOG_INFO("------------------------------------------------");
2023-11-15 15:11:15 -06:00
SPDLOG_INFO("Starting Torch...");
SPDLOG_INFO("Scanning {}", folder);
2023-11-15 15:11:15 -06:00
auto start = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
std::unordered_map<std::string, std::vector<char>> files;
2023-11-15 15:11:15 -06:00
for (const auto & entry : fs::recursive_directory_iterator(folder)){
if(entry.is_directory()) {
continue;
}
2023-11-15 15:11:15 -06:00
std::ifstream input( entry.path(), std::ios::binary );
auto data = std::vector( std::istreambuf_iterator( input ), {} );
input.close();
files[entry.path().string()] = data;
}
2023-11-15 15:11:15 -06:00
auto wrapper = SWrapper(output);
2023-11-15 15:11:15 -06:00
for(auto& [path, data] : files){
std::string normalized = path;
std::replace(normalized.begin(), normalized.end(), '\\', '/');
// Remove parent folder
normalized = normalized.substr(folder.length() + 1);
wrapper.CreateFile(normalized, data);
SPDLOG_INFO("> Added {}", normalized);
}
2023-11-15 15:11:15 -06:00
auto end = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
SPDLOG_INFO("Done! Took {}ms", end.count() - start.count());
SPDLOG_INFO("Exported to {}", output);
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%l] %v");
SPDLOG_INFO("------------------------------------------------");
2023-11-15 15:11:15 -06:00
wrapper.Close();
}
std::optional<std::tuple<std::string, YAML::Node>> Companion::RegisterAsset(const std::string& name, YAML::Node& node) {
2024-03-12 19:09:40 -06:00
if(!node["offset"]) {
return std::nullopt;
}
2023-11-15 15:11:15 -06:00
this->gAssetDependencies[this->gCurrentFile][name] = std::make_pair(node, false);
auto output = (this->gCurrentDirectory / name).string();
std::replace(output.begin(), output.end(), '\\', '/');
auto entry = std::make_tuple(output, node);
this->gAddrMap[this->gCurrentFile][node["offset"].as<uint32_t>()] = entry;
return entry;
2023-11-15 15:11:15 -06:00
}
void Companion::RegisterFactory(const std::string& type, const std::shared_ptr<BaseFactory>& factory) {
this->gFactories[type] = factory;
SPDLOG_DEBUG("Registered factory for {}", type);
}
std::optional<std::shared_ptr<BaseFactory>> Companion::GetFactory(const std::string &type) {
if(!this->gFactories.contains(type)){
return std::nullopt;
}
return this->gFactories[type];
}
/**
* @param offset Rom offset of compressed mio0 file.
* @returns CompressionType
*/
CompressionType Companion::GetCompressionType(std::vector<uint8_t>& buffer, const uint32_t offset) {
if (offset) {
LUS::BinaryReader reader((char*) buffer.data() + offset, sizeof(uint32_t));
reader.SetEndianness(LUS::Endianness::Big);
const std::string header = reader.ReadCString();
// Check if a compressed header exists
if (header == "MIO0") {
return CompressionType::MIO0;
} else if (header == "YAY0") {
return CompressionType::YAY0;
} else if (header == "YAZ0") {
return CompressionType::YAZ0;
}
}
return CompressionType::None;
}
std::optional<Table> Companion::SearchTable(uint32_t addr){
for(auto& table : this->gTables){
if(addr >= table.start || addr <= table.end){
return table;
}
}
return std::nullopt;
}
std::optional<std::string> Companion::GetEnumFromValue(const std::string& key, int32_t id) {
if(!this->gEnums.contains(key)){
return std::nullopt;
}
if(!this->gEnums[key].contains(id)){
return std::nullopt;
}
return this->gEnums[key][id];
}
std::optional<std::uint32_t> Companion::GetFileOffsetFromSegmentedAddr(const uint8_t segment) const {
auto segments = this->gConfig.segment;
if(segments.temporal.contains(segment)) {
return segments.temporal[segment];
}
if(segments.local.contains(segment)) {
return segments.local[segment];
2023-11-15 15:11:15 -06:00
}
if(segments.global.contains(segment)) {
return segments.global[segment];
}
return std::nullopt;
2023-11-15 15:11:15 -06:00
}
std::optional<std::tuple<std::string, YAML::Node>> Companion::GetNodeByAddr(const uint32_t addr){
if(!this->gAddrMap.contains(this->gCurrentFile)){
return std::nullopt;
}
if(!this->gAddrMap[this->gCurrentFile].contains(addr)){
return std::nullopt;
}
return this->gAddrMap[this->gCurrentFile][addr];
}
2024-03-09 23:12:39 -06:00
std::optional<std::vector<std::tuple<std::string, YAML::Node>>> Companion::GetNodesByType(const std::string& type){
std::vector<std::tuple<std::string, YAML::Node>> nodes;
if(!this->gAddrMap.contains(this->gCurrentFile)){
return nodes;
}
for(auto& [addr, tpl] : this->gAddrMap[this->gCurrentFile]){
auto [name, node] = tpl;
const auto n_type = GetSafeNode<std::string>(node, "type");
2024-03-10 15:51:51 -06:00
if(node["autogen"]){
SPDLOG_DEBUG("Skipping autogenerated asset {}", name);
continue;
}
2024-03-09 23:12:39 -06:00
if(n_type == type){
nodes.push_back(tpl);
}
}
return nodes;
}
2023-11-15 15:11:15 -06:00
std::string Companion::NormalizeAsset(const std::string& name) const {
auto path = fs::path(this->gCurrentFile).stem().string() + "_" + name;
return path;
2024-02-21 12:51:47 -06:00
}
std::string Companion::CalculateHash(const std::vector<uint8_t>& data) {
return Chocobo1::SHA1().addData(data).finalize().toString();
}