summaryrefslogtreecommitdiff
path: root/src/Companion.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Companion.cpp')
-rw-r--r--src/Companion.cpp131
1 files changed, 72 insertions, 59 deletions
diff --git a/src/Companion.cpp b/src/Companion.cpp
index df793be..d234ee7 100644
--- a/src/Companion.cpp
+++ b/src/Companion.cpp
@@ -5,92 +5,105 @@
#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 "n64/Cartridge.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
+ N64::Cartridge cartridge = 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);
-
- this->ProcessAssets();
-}
+ if(!config[cartridge.GetHash()]){
+ std::cout << "No config found for " << cartridge.GetHash() << '\n';
+ return;
+ }
-void Companion::ProcessAssets() {
- json assets = json::parse( std::ifstream( "assets.json" ) );
+ auto cfg = config[cartridge.GetHash()];
+ auto path = cfg["path"].as<std::string>();
+ auto otr = cfg["output"].as<std::string>();
- SWrapper wrapper = SWrapper("smcube.otr");
+ auto wrapper = SWrapper(otr);
- 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>();
+ std::cout << "Writing " << output << '\n';
+ wrapper.CreateFile(output, buffer);
+
+ if(type != "TEXTURE") {
+ std::string dpath = "debug/" + path;
+ 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 " << path << std::endl;
- write.Close();
+ write.Close();
+ }
}
MIO0Decoder::ClearCache();
wrapper.Close();
-} \ No newline at end of file
+}
+
+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];
+}