Files
Torch/src/Companion.cpp
T

90 lines
2.8 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 "factories/RawFactory.h"
2023-08-19 23:55:59 -06:00
#include "factories/BlobFactory.h"
2023-08-30 21:26:38 -06:00
#include "factories/AudioFactory.h"
2023-08-17 00:40:00 -06:00
#include "factories/TextureFactory.h"
#include "factories/AnimFactory.h"
2023-08-17 00:40:00 -06:00
#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 = {
2023-08-19 23:55:59 -06:00
{ ".png", new TextureFactory() },
{ ".anim", new AnimFactory() },
2023-08-30 21:26:38 -06:00
{ ".aiff", new AudioFactory() },
2023-08-19 23:55:59 -06:00
{ ".bin", new BlobFactory(LUS::ResourceType::Blob) },
2023-08-30 21:26:38 -06:00
{ ".m64", new BlobFactory(LUS::ResourceType::Blob) },
2023-08-19 23:55:59 -06:00
{ ".sbox", new BlobFactory(LUS::ResourceType::Blob, true) },
2023-08-17 00:40:00 -06:00
};
void Companion::Start() {
std::cout << "Super Mario 64 Rom Path: " << this->gRomPath << '\n';
std::ifstream input( this->gRomPath, std::ios::binary );
this->gRomData = std::vector<uint8_t>( std::istreambuf_iterator<char>( input ), {} );
input.close();
// TODO: Validate hash
std::cout << "Detected Rom Size: " << this->gRomData.size() << '\n';
this->ProcessAssets();
}
void Companion::ProcessAssets() {
json assets = json::parse( std::ifstream( "assets.json" ) );
SWrapper wrapper = SWrapper("smcube.otr");
for( auto& [asset, data] : assets.items() ) {
std::string extension = fs::path(asset).extension().string();
if( gFactories.find(extension) == gFactories.end() ) {
2023-08-30 21:26:38 -06:00
// std::cout << "No factory found for " << asset << '\n';
2023-08-17 00:40:00 -06:00
continue;
}
LUS::BinaryWriter write = LUS::BinaryWriter();
std::string path = asset.substr(0, asset.find_last_of('.'));
json entry = {
{ "path", path },
{ "offsets", data }
};
2023-08-19 23:55:59 -06:00
RawFactory* factory = gFactories.at(extension);
if(!factory->process(&write, entry, this->gRomData)){
2023-08-30 21:26:38 -06:00
// std::cout << "Failed to process " << asset << '\n';
2023-08-17 02:47:21 -06:00
continue;
}
2023-08-17 00:40:00 -06:00
2023-08-19 23:55:59 -06:00
bool isTexture = typeid(*factory) == typeid(TextureFactory);
2023-08-17 00:40:00 -06:00
auto buffer = write.ToVector();
2023-08-17 02:47:21 -06:00
// TODO: Change this that we all know its going to crash with other assets
2023-08-19 23:55:59 -06:00
wrapper.CreateFile(isTexture ? path.substr(0, path.find_last_of('.')) : path, buffer);
if(!isTexture) {
// Write to file too
std::string dpath = "debug/" + path;
2023-08-30 21:26:38 -06:00
if(!fs::exists(fs::path(dpath).parent_path())){
fs::create_directories(fs::path(dpath).parent_path());
}
2023-08-19 23:55:59 -06:00
std::ofstream output(dpath, std::ios::binary);
output.write((char*)buffer.data(), buffer.size());
output.close();
}
2023-08-30 21:26:38 -06:00
// std::cout << "Processed " << path << std::endl;
2023-08-17 00:40:00 -06:00
write.Close();
}
MIO0Decoder::ClearCache();
wrapper.Close();
}