Refactored to use a better file format

This commit is contained in:
KiritoDv
2023-09-17 23:55:59 -06:00
parent 87ba0d2587
commit 396f732bc5
49 changed files with 43210 additions and 25449 deletions
+74 -61
View File
@@ -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);
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");
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>();
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);
LUS::BinaryWriter write = LUS::BinaryWriter();
RawFactory* factory = this->GetFactory(type);
if(!factory->process(&write, entry, this->gRomData)){
std::cout << "Failed to process " << asset << '\n';
continue;
}
bool isTexture = typeid(*factory) == typeid(TextureFactory);
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(!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());
if(!factory->process(&write, asset->second, this->gRomData)){
std::cout << "Failed to process " << asset->first.as<std::string>() << '\n';
continue;
}
std::ofstream output(dpath, std::ios::binary);
output.write((char*)buffer.data(), buffer.size());
output.close();
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();
}
std::cout << "Processed " << path << std::endl;
write.Close();
}
MIO0Decoder::ClearCache();
wrapper.Close();
}
}
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];
}