2016-07-14 18:35:07 +02:00

113 lines
3.8 KiB
C++

#include <cstdio>
#include <string>
#include <vector>
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\
typedef std::unordered_map<std::string, size_t> ResourceSizeMap;\n"
unsigned int n = 0;
bool addFile(const string &inFile, FILE *out)
{
unsigned int size = 0;
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);
size += nbRead;
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, "\t0x00}; // additionnal byte used to null-terminate ascii data\n");
fprintf(out, "size_t data%d_size = %d;\n", n, size);
++n;
fclose(in);
return true;
}
int main(int argc, char *argv[])
{
if(argc < 4)
{
printf("usage : %s RESOURCE_PACK_FILE_TO_CREATE RESOURCE_PACK_ROOT [LIST_OF_FILES_TO_BAKE]\n\n", argv[0]);
printf("RESOURCE_PACK_FILE_TO_CREATE is the c++ source file that will be created,\n");
printf("the resource pack will be named from the file name\n");
printf("example, if the filename provided is : /directory/sources/myResourcePack.cpp , the resource pack will be named myResourcePack\n\n");
printf("RESOURCE_PACK_ROOT is the root directory or the resources, they will be accessible from their relative path to this directory\n");
return EXIT_SUCCESS;
}
string outFilename(argv[1]);
string packName = outFilename.substr(outFilename.find_last_of('/')+1);
packName = packName.substr(0, packName.find_first_of('.'));
for(char c : packName)
{
if(!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '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);
vector<int> offsets;
for(int i=3; i<argc; ++i)
{
int offset = 0;
while(argv[2][offset] == argv[i][offset])
++offset;
if(argv[i][offset] == '/')
++offset;
if(!addFile(argv[i], out))
{
fclose(out);
return EXIT_FAILURE;
}
else
{
printf("successfully added \"%s\" as \"%s\" to resource pack \"%s\".\n", argv[i], argv[i]+offset, packName.c_str());
offsets.push_back(offset);
}
}
// create the function initializing the data pointers map
fprintf(out, "\nvoid getResourcePack_%s(ResourceMap &resourceFilesData)\n{\n", packName.c_str());
for(int i=0; i<int(n); ++i)
fprintf(out, "\tresourceFilesData[\"%s\"] = data%d;\n", argv[3+i]+offsets[i], i);
fprintf(out, "}\n");
// create the function initializing the data size map
fprintf(out, "\nvoid getResourceSizes_%s(ResourceSizeMap &resourceFilesSizes)\n{\n", packName.c_str());
for(int i=0; i<int(n); ++i)
fprintf(out, "\tresourceFilesSizes[\"%s\"] = data%d_size;\n", argv[3+i]+offsets[i], i);
fprintf(out, "}\n");
fprintf(out, "}\n");
fclose(out);
printf("successfully created resource file \"%s\".\n", outFilename.c_str());
return EXIT_SUCCESS;
}