diff options
| author | Lywx <kiritodev01@gmail.com> | 2023-09-18 18:48:50 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-18 18:48:50 -0600 |
| commit | 290d99632885b34bf38dd657309d148b7193d9b7 (patch) | |
| tree | 012bebf1327b732879ba6a91a5ed0c29803486b4 /src/Companion.cpp | |
| parent | 9421c024c891a883b547babdee471d8ca300ec0a (diff) | |
| parent | 00bb8fa3477511a655669cb26277594b4bf9d370 (diff) | |
Added support for extracting audio and multiversion support
Added support for extracting audio and multiversion support
Diffstat (limited to 'src/Companion.cpp')
| -rw-r--r-- | src/Companion.cpp | 138 |
1 files changed, 79 insertions, 59 deletions
diff --git a/src/Companion.cpp b/src/Companion.cpp index df793be..c8ae0d5 100644 --- a/src/Companion.cpp +++ b/src/Companion.cpp @@ -2,95 +2,115 @@ #include "storm/SWrapper.h" #include "utils/MIODecoder.h" -#include "audio/AudioManager.h" #include "factories/RawFactory.h" #include "factories/BlobFactory.h" -#include "factories/AudioFactory.h" +#include "factories/AudioHeaderFactory.h" +#include "factories/SequenceFactory.h" +#include "factories/SampleFactory.h" +#include "factories/BankFactory.h" #include "factories/TextureFactory.h" #include "factories/AnimFactory.h" #include <fstream> #include <iostream> #include <filesystem> -#include <nlohmann/json.hpp> -using json = nlohmann::json; namespace fs = std::filesystem; -static const std::unordered_map<std::string, RawFactory*> gFactories = { - { ".png", new TextureFactory() }, - { ".anim", new AnimFactory() }, - { ".aiff", new AudioFactory() }, - { ".bnk", new AudioFactory() }, - { ".bset", new AudioFactory() }, - { ".m64", new AudioFactory() }, - { ".bin", new BlobFactory(LUS::ResourceType::Blob) }, - { ".sbox", new BlobFactory(LUS::ResourceType::Blob, true) }, -}; +void Companion::Init() { -void Companion::Start() { - std::cout << "Super Mario 64 Rom Path: " << this->gRomPath << '\n'; + this->RegisterFactory("AUDIO:HEADER", new AudioHeaderFactory()); + this->RegisterFactory("SEQUENCE", new SequenceFactory()); + this->RegisterFactory("SAMPLE", new SampleFactory()); + this->RegisterFactory("BANK", new BankFactory()); + + this->RegisterFactory("TEXTURE", new TextureFactory()); + this->RegisterFactory("BLOB", new BlobFactory()); + + // SM64 Specific + + this->RegisterFactory("SM64:ANIM", new AnimFactory()); + this->Process(); +} + +void Companion::Process() { std::ifstream input( this->gRomPath, std::ios::binary ); this->gRomData = std::vector<uint8_t>( std::istreambuf_iterator<char>( input ), {} ); input.close(); - // TODO: Validate hash + this->cartridge = new N64::Cartridge(this->gRomData); + cartridge->Initialize(); - std::cout << "Detected Rom Size: " << this->gRomData.size() << '\n'; + YAML::Node config = YAML::LoadFile("config.yml"); - AudioManager::Instance = new AudioManager(); - AudioManager::Instance->initialize(this->gRomData); + if(!config[cartridge->GetHash()]){ + std::cout << "No config found for " << cartridge->GetHash() << '\n'; + return; + } - this->ProcessAssets(); -} + auto cfg = config[cartridge->GetHash()]; + auto path = cfg["path"].as<std::string>(); + auto otr = cfg["output"].as<std::string>(); -void Companion::ProcessAssets() { - json assets = json::parse( std::ifstream( "assets.json" ) ); + auto wrapper = SWrapper(otr); - SWrapper wrapper = SWrapper("smcube.otr"); + auto vWriter = LUS::BinaryWriter(); + vWriter.SetEndianness(LUS::Endianness::Big); + vWriter.Write((uint8_t) LUS::Endianness::Big); + vWriter.Write(cartridge->GetCRC()); - for( auto& [asset, data] : assets.items() ) { - std::string extension = fs::path(asset).extension().string(); - if( gFactories.find(extension) == gFactories.end() ) { - std::cout << "No factory found for " << asset << '\n'; - continue; - } - LUS::BinaryWriter write = LUS::BinaryWriter(); - std::string path = asset.substr(0, asset.find_last_of('.')); - json entry = { - { "path", path }, - { "offsets", data } - }; - RawFactory* factory = gFactories.at(extension); - - if(!factory->process(&write, entry, this->gRomData)){ - std::cout << "Failed to process " << asset << '\n'; - continue; - } + for (const auto & entry : fs::directory_iterator(path)){ + std::cout << "Processing " << entry.path() << '\n'; + YAML::Node root = YAML::LoadFile(entry.path()); + for(auto asset = root.begin(); asset != root.end(); ++asset){ + auto type = asset->second["type"].as<std::string>(); - bool isTexture = typeid(*factory) == typeid(TextureFactory); + LUS::BinaryWriter write = LUS::BinaryWriter(); + RawFactory* factory = this->GetFactory(type); - auto buffer = write.ToVector(); - // TODO: Change this that we all know its going to crash with other assets - wrapper.CreateFile(isTexture ? path.substr(0, path.find_last_of('.')) : path, buffer); + if(!factory->process(&write, asset->second, this->gRomData)){ + std::cout << "Failed to process " << asset->first.as<std::string>() << '\n'; + continue; + } - if(!isTexture) { - // Write to file too - std::string dpath = "debug/" + path; - if(!fs::exists(fs::path(dpath).parent_path())){ - fs::create_directories(fs::path(dpath).parent_path()); + auto buffer = write.ToVector(); + auto output = entry.path().stem() / asset->first.as<std::string>(); + wrapper.CreateFile(output, buffer); + + if(type != "TEXTURE") { + std::string dpath = "debug/" + output.string(); + if(!fs::exists(fs::path(dpath).parent_path())){ + fs::create_directories(fs::path(dpath).parent_path()); + } + std::ofstream stream(dpath, std::ios::binary); + stream.write((char*)buffer.data(), buffer.size()); + stream.close(); } - std::ofstream output(dpath, std::ios::binary); - output.write((char*)buffer.data(), buffer.size()); - output.close(); - } - std::cout << "Processed " << path << std::endl; + std::cout << "Processed " << output << std::endl; - write.Close(); + write.Close(); + } } + wrapper.CreateFile("version", vWriter.ToVector()); + vWriter.Close(); + MIO0Decoder::ClearCache(); wrapper.Close(); -}
\ No newline at end of file + this->cartridge = nullptr; + Companion::Instance = nullptr; +} + +void Companion::RegisterFactory(const std::string &extension, RawFactory *factory) { + this->gFactories[extension] = factory; +} + +RawFactory *Companion::GetFactory(const std::string &extension) { + if(!this->gFactories.contains(extension)){ + throw std::runtime_error("No factory found for " + extension); + } + + return this->gFactories[extension]; +} |
