2025-05-07 18:51:30 +02:00
/*
Crafter . Build
Copyright ( C ) 2025 Catcrafts ®
Catcrafts . net
This library is free software ; you can redistribute it and / or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation ; either
version 3.0 of the License , or ( at your option ) any later version .
This library is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public
License along with this library ; if not , write to the Free Software
Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
*/
2025-05-06 12:37:14 +02:00
# include <iostream>
# include <string>
# include <filesystem>
# include <unordered_map>
# include <vector>
# include <functional>
import Crafter . Asset ;
using namespace Crafter ;
namespace fs = std : : filesystem ;
int main ( int argc , char * argv [ ] ) {
if ( argc = = 1 ) {
std : : cout < < " No arguments provided, use help for help " < < std : : endl ;
return - 1 ;
}
std : : string command = std : : string ( argv [ 1 ] ) ;
if ( command = = " help " ) {
std : : cout < < " help: Displays this help message. \n add {file} {entry file} {entry type} {serializer} {entry name}: Adds an entry to an existing assset file or creates a new file if the file in {file} does not exist, {entry file} is the path to the file you want to add to the asset, {entry type} is the type of the resulting entry, {serializer} is the serializer to use to add the entry file to the asset, {entry name} is the name of the resulting entry \n remove {file} {entry} \n extract {file} {entry name} {extractor} {extracted path}: Extracts a entry with the name {entry name} from the asset file in {file}, using the extractor in {extractor} writes the result to the file {extracted path} \n list serialize: Shows a list of the support serializers \n list {file}: Lists all entries in the asset file \n list extract: Shows a list of supported extractors " < < std : : endl ;
} else if ( command = = " list " ) {
if ( argc = = 2 ) {
std : : cout < < " Unkown command, use help for help " < < std : : endl ;
return - 1 ;
}
std : : string command2 = std : : string ( argv [ 2 ] ) ;
if ( command2 = = " serialize " ) {
for ( const auto & pair : serializers ) {
std : : cout < < pair . first < < std : : endl ;
}
} else if ( command2 = = " extract " ) {
for ( const auto & pair : extractors ) {
std : : cout < < pair . first < < std : : endl ;
}
} else {
if ( ! fs : : exists ( argv [ 2 ] ) ) {
std : : cout < < " Asset file does not exist " < < std : : endl ;
return - 1 ;
}
Asset asset ;
asset . LoadFull ( argv [ 2 ] ) ;
std : : cout < < " <name> \t <type> \t <size> " < < std : : endl ;
for ( const AssetEntry & entry : asset . entries ) {
std : : cout < < std : : format ( " {} \t {} \t {} " , entry . name , entry . type , entry . data . size ( ) ) < < std : : endl ;
}
}
} else if ( command = = " remove " ) {
if ( argc = = 2 ) {
std : : cout < < " Missing argument {file}. Run help for help " < < std : : endl ;
return - 1 ;
} else if ( argc = = 3 ) {
std : : cout < < " Missing argument {enty name}. Run help for help " < < std : : endl ;
return - 1 ;
}
if ( ! fs : : exists ( argv [ 2 ] ) ) {
std : : cout < < " Asset file does not exist " < < std : : endl ;
return - 1 ;
}
Asset asset ;
asset . LoadFull ( argv [ 2 ] ) ;
if ( asset . entries . size ( ) = = 0 ) {
std : : cout < < " Nothing to remove, asset file empty " < < std : : endl ;
}
for ( auto it = asset . entries . begin ( ) ; it ! = asset . entries . end ( ) ; std : : advance ( it , 1 ) ) {
if ( it - > name = = argv [ 3 ] ) {
asset . entries . erase ( it ) ;
fs : : remove ( argv [ 2 ] ) ;
asset . Save ( argv [ 2 ] ) ;
return 0 ;
}
std : : cout < < std : : format ( " Entry {} not found " , argv [ 3 ] ) < < std : : endl ;
return - 1 ;
}
} else if ( command = = " add " ) {
if ( argc = = 2 ) {
std : : cout < < " Missing argument {file}. Run help for help " < < std : : endl ;
return - 1 ;
} else if ( argc = = 3 ) {
std : : cout < < " Missing argument {entry file}. Run help for help " < < std : : endl ;
return - 1 ;
} else if ( argc = = 4 ) {
std : : cout < < " Missing argument {entry type}. Run help for help " < < std : : endl ;
return - 1 ;
} else if ( argc = = 5 ) {
std : : cout < < " Missing argument {serializer}. Run help for help " < < std : : endl ;
return - 1 ;
} else if ( argc = = 6 ) {
std : : cout < < " Missing argument {entry name}. Run help for help " < < std : : endl ;
return - 1 ;
}
Asset asset ;
if ( fs : : exists ( argv [ 2 ] ) ) {
asset . LoadFull ( argv [ 2 ] ) ;
fs : : remove ( argv [ 2 ] ) ;
}
AssetEntry entry ;
entry . name = std : : string ( argv [ 6 ] ) ;
entry . type = std : : string ( argv [ 4 ] ) ;
std : : unordered_map < std : : string , std : : function < std : : vector < char > ( fs : : path ) > > : : const_iterator pos = serializers . find ( argv [ 5 ] ) ;
if ( pos = = serializers . end ( ) ) {
std : : cout < < std : : format ( " Unkown serializer {} " , argv [ 5 ] ) < < std : : endl ;
return - 1 ;
} else {
entry . data = pos - > second ( argv [ 3 ] ) ;
}
asset . entries . push_back ( entry ) ;
asset . Save ( argv [ 2 ] ) ;
} else if ( command = = " extract " ) {
if ( argc = = 2 ) {
std : : cout < < " Missing argument {file}. Run help for help " < < std : : endl ;
return - 1 ;
} else if ( argc = = 3 ) {
std : : cout < < " Missing argument {entry name}. Run help for help " < < std : : endl ;
return - 1 ;
} else if ( argc = = 4 ) {
std : : cout < < " Missing argument {extractor}. Run help for help " < < std : : endl ;
return - 1 ;
} else if ( argc = = 5 ) {
std : : cout < < " Missing argument {extracted path}. Run help for help " < < std : : endl ;
return - 1 ;
}
if ( ! fs : : exists ( argv [ 2 ] ) ) {
std : : cout < < " Asset file does not exist " < < std : : endl ;
return - 1 ;
}
Asset asset ;
asset . LoadFull ( argv [ 2 ] ) ;
auto pos = extractors . find ( argv [ 4 ] ) ;
if ( pos = = extractors . end ( ) ) {
std : : cout < < std : : format ( " Unkown extractor {} " , argv [ 4 ] ) < < std : : endl ;
return - 1 ;
} else {
for ( const AssetEntry & entry : asset . entries ) {
if ( entry . name = = std : : string ( argv [ 3 ] ) ) {
pos - > second ( argv [ 5 ] , entry . data . data ( ) ) ;
return 0 ;
}
}
std : : cout < < std : : format ( " Entry {} not found " , argv [ 3 ] ) < < std : : endl ;
return - 1 ;
}
} else {
std : : cout < < " Unkown command, use help for help " < < std : : endl ;
}
// Asset test;
// AssetEntry entry;
// entry.name = "Logo";
// entry.type = "Crafter.Graphics.TextureR8B8G8A8";
// //entry.data = serializers.find("PNG:Crafter.Graphics.TextureR8B8G8A8")->second("/home/jorijn/Pictures/3dfortslogo.png");
// test.entries.push_back(entry);
// test.Save("test.cras");
// Asset test2;
// test2.LoadFull("test.cras");
//extractors.find(std::format("{}:PNG", test2.entries[0].type))->second("/home/jorijn/repos/Crafter/Crafter.Asset/test.png", test2.entries[0].data.data());
}