93 lines
2.3 KiB
C++
93 lines
2.3 KiB
C++
#include <cstdio>
|
|
#include <string>
|
|
#include <regex>
|
|
|
|
using namespace std;
|
|
|
|
#define NB_VAL_PER_LINE 256
|
|
|
|
|
|
#define FILE_BEGIN "#include <string>\n\
|
|
#include <unordered_map>\n\
|
|
\n\
|
|
namespace Resource {\n\
|
|
typedef std::unordered_map<std::string, const char*> ResourceMap;\n"
|
|
|
|
|
|
#define FILE_MID ""
|
|
|
|
|
|
#define FILE_END "\n\
|
|
}\n\
|
|
}\n"
|
|
|
|
unsigned int n = 0;
|
|
|
|
bool addFile(const string &inFile, FILE *out)
|
|
{
|
|
FILE *in = fopen(inFile.c_str(), "r");
|
|
if(in == NULL)
|
|
{
|
|
fprintf(stderr, "can't open \"%s\" for reading\n", inFile.c_str());
|
|
return false;
|
|
}
|
|
fprintf(out, "\nconst char data%d[] = {\n", n);
|
|
char ptr[NB_VAL_PER_LINE];
|
|
size_t nbRead;
|
|
do
|
|
{
|
|
nbRead = fread(ptr, sizeof(char), NB_VAL_PER_LINE, in);
|
|
for(unsigned int i=0; i<nbRead; ++i)
|
|
fprintf(out, i ? ",0x%x" : "\t0x%x", ptr[i]);
|
|
fprintf(out, "\n");
|
|
}
|
|
while(nbRead == NB_VAL_PER_LINE);
|
|
fprintf(out, "};\n");
|
|
++n;
|
|
fclose(in);
|
|
return true;
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
if(argc < 3)
|
|
{
|
|
printf("usage : %s RESOURCE_PACK_NAME [LIST_OF_FILES_TO_BAKE]\n", argv[0]);
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
string outFilename(argv[1]);
|
|
string packName = outFilename.substr(outFilename.find_last_of('/')+1);
|
|
packName = packName.substr(0, packName.find_first_of('.'));
|
|
if(!regex_match(packName, regex("[a-zA-Z0-9]+")))
|
|
{
|
|
printf("error : \"%s\" is an invalid resource pack name, it must match this regex : [a-zA-Z0-9]+\n", packName.c_str());
|
|
return EXIT_SUCCESS;
|
|
}
|
|
FILE *out = fopen(outFilename.c_str(), "w");
|
|
if(out == NULL)
|
|
{
|
|
fprintf(stderr, "can't open \"%s\" for writing\n", argv[1]);
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
fprintf(out, FILE_BEGIN);
|
|
for(int i=2; i<argc; ++i)
|
|
{
|
|
if(!addFile(argv[i], out))
|
|
{
|
|
fclose(out);
|
|
return EXIT_FAILURE;
|
|
}
|
|
else
|
|
printf("successfully added \"%s\" to \"%s\".\n", argv[i], packName.c_str());
|
|
}
|
|
fprintf(out, "\nvoid getResourcePack_%s(ResourceMap &resourceFilesData)\n{\n", packName.c_str());
|
|
for(int i=0; i<n; ++i)
|
|
fprintf(out, "\tresourceFilesData[\"%s\"] = data%d;\n", argv[2+i], i);
|
|
fprintf(out, FILE_END);
|
|
fclose(out);
|
|
printf("successfully created resource file \"%s\".\n", outFilename.c_str());
|
|
return EXIT_SUCCESS;
|
|
}
|