Files
Torch/src/Companion.cpp
T

110 lines
3.4 KiB
C++
Raw Normal View History

2023-08-17 00:40:00 -06:00
#include "Companion.h"
#include "storm/SWrapper.h"
#include "utils/MIODecoder.h"
#include "audio/AudioManager.h"
2023-08-17 00:40:00 -06:00
#include "factories/RawFactory.h"
2023-08-19 23:55:59 -06:00
#include "factories/BlobFactory.h"
2023-09-17 23:55:59 -06:00
#include "factories/AudioHeaderFactory.h"
#include "factories/SequenceFactory.h"
#include "factories/SampleFactory.h"
#include "factories/BankFactory.h"
2023-08-17 00:40:00 -06:00
#include "factories/TextureFactory.h"
#include "factories/AnimFactory.h"
2023-09-17 23:55:59 -06:00
#include "n64/Cartridge.h"
2023-08-17 00:40:00 -06:00
#include <fstream>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
2023-09-17 23:55:59 -06:00
void Companion::Init() {
2023-08-17 00:40:00 -06:00
2023-09-17 23:55:59 -06:00
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() {
2023-08-17 00:40:00 -06:00
std::ifstream input( this->gRomPath, std::ios::binary );
this->gRomData = std::vector<uint8_t>( std::istreambuf_iterator<char>( input ), {} );
input.close();
2023-09-17 23:55:59 -06:00
N64::Cartridge cartridge = N64::Cartridge(this->gRomData);
cartridge.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
2023-09-17 23:55:59 -06:00
if(!config[cartridge.GetHash()]){
std::cout << "No config found for " << cartridge.GetHash() << '\n';
return;
}
2023-09-17 23:55:59 -06:00
auto cfg = config[cartridge.GetHash()];
auto path = cfg["path"].as<std::string>();
auto otr = cfg["output"].as<std::string>();
2023-08-17 00:40:00 -06:00
2023-09-17 23:55:59 -06:00
auto wrapper = SWrapper(otr);
2023-08-17 00:40:00 -06:00
2023-09-17 23:55:59 -06:00
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>();
2023-08-17 00:40:00 -06:00
2023-09-17 23:55:59 -06:00
LUS::BinaryWriter write = LUS::BinaryWriter();
RawFactory* factory = this->GetFactory(type);
2023-08-19 23:55:59 -06:00
2023-09-17 23:55:59 -06:00
if(!factory->process(&write, asset->second, this->gRomData)){
std::cout << "Failed to process " << asset->first.as<std::string>() << '\n';
continue;
2023-08-30 21:26:38 -06:00
}
2023-09-17 23:55:59 -06:00
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::cout << "Processed " << path << std::endl;
write.Close();
2023-08-19 23:55:59 -06:00
}
2023-08-17 00:40:00 -06:00
}
MIO0Decoder::ClearCache();
wrapper.Close();
2023-09-17 23:55:59 -06:00
}
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];
}