Skip to content

Instantly share code, notes, and snippets.

@spacechase0
Last active June 23, 2017 20:18
Show Gist options
  • Save spacechase0/f3c0638f5670cca9ac41ad01a2ceb968 to your computer and use it in GitHub Desktop.
Save spacechase0/f3c0638f5670cca9ac41ad01a2ceb968 to your computer and use it in GitHub Desktop.
Xnb tool
#include <boost/filesystem.hpp>
#include <SFML/Graphics/Image.hpp>
#include <util/File.hpp>
#include <xnb/File.hpp>
namespace fs = boost::filesystem;
void extract( xnb::File& file, const std::string& output )
{
}
void pack( const std::string& input, const std::string& output )
{
xnb::File file;
if ( input.length() >= 5 && input.substr( input.length() - 5 ) == ".tbin" )
{
std::string data = util::getFileContents( input, true );
if ( data == "" )
{
std::cout << "ERROR: Failed to load file: " << input << std::endl;
return;
}
file.data.reset( new xnb::TbinData() );
auto tbin = static_cast< xnb::TbinData* >( file.data.get() );
tbin->data = data;
}
else if ( input.length() >= 4 && input.substr( input.length() - 4 ) == ".png" )
{
if ( !image.loadFromFile( input ) )
{
std::cout << "ERROR: Failed to load file: " << input << std::endl;
return;
}
file.data.reset( new xnb::Texture2DData() );
auto tex = static_cast< xnb::Texture2DData* >( file.data.get() );
tex->data.push_back( image );
}
else if ( input.length() >= 5 && input.substr( input.length() - 5 ) == ".json" )
{
// TODO
}
else
{
std::cout << "ERROR: Unknown file type for " << input << std::endl;
return;
}
file.saveToFile( output );
}
int main( int argc, char* argv[] )
{
if ( argc != 4 )
{
std::cout << "Usage: ./xnb.exe <extract|pack> <input> <output>" << std::endl;
return 1;
}
bool extract = true;
if ( argc[ 1 ] == std::string( "pack" ) )
extract = false;
else if ( argc[ 1 ] != std::string( "extract" ) )
{
std::cout << "ERROR: Command must be 'extract' or 'pack'." << std::endl;
return 2;
}
if ( !fs::exists( argc[ 2 ] ) )
{
std::cout << "ERROR: Input does not exist." << std::endl;
return 3;
}
if ( fs::is_directory( argc[ 2 ] ) )
{
for ( fs::recursive_directory_iterator it( argc[ 2 ] ); it != decltype( it )(); ++it )
{
std::string filename = it->filename().string();
if ( filename.length() >= 4 && filename.substr( filename.length() - 4 ) == ".xnb" )
{
if ( extract )
{
xnb::File file;
file.loadFromFile( filename );
extract( file, TODO );
}
else
{
pack( file, TODO );
}
}
}
}
else
{
if ( extract )
{
xnb::File file;
file.loadFromFile( argc[ 2 ] );
extract( file, argc[ 3 ] );
}
else
{
pack( argc[ 2 ], argc[ 3 ] );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment