#include "error.h" #include #include #define buffer_size 8 const char* enum_header = "#ifndef pack_h\n" "#define pack_h\n" "typedef enum {\n"; const char* enum_footer = "\tasset_id_count\n" "} Asset_ID;\n"; const char* footer = "#endif\n"; typedef struct { int size; int offset; char name[56]; } Entry; int main(int argc, char** argv) { int i, j, file_count, read; char buffer[buffer_size]; char name[56]; const char* workingdir, * dsep = ""; Entry entry; FILE* outfile, * infile; const char* filename = argv[1]; outfile = fopen(filename, "wb"); if (!outfile) { fprintf( stderr, "Failed to open '%s' for writing.\n", filename ); return 1; } file_count = argc - 3; workingdir = argv[2]; if (workingdir[strlen(workingdir) - 1] != '/') dsep = "/"; fprintf(outfile, "%s", enum_header); for (i = 0; i < file_count; i++) { char* c; strcpy(name, "asset_id_"); strcat(name, argv[i + 3]); for (c = name; *c; c++) { if (c[0] == '.') c[0] = '_'; } fprintf(outfile, "\t%s,\n", name); } fprintf(outfile, "%s", enum_footer); fprintf(outfile, "#ifndef enum_only\n"); fprintf(outfile, "const int asset_offset_table[] = {\n"); entry.offset = 0; for (i = 0; i < file_count; i++) { strcpy(entry.name, argv[i + 3]); strcpy(name, workingdir); strcat(name, dsep); strcat(name, entry.name); infile = fopen(name, "rb"); if (!infile) { fprintf( stderr, "Failed to open '%s' for reading.\n", entry.name ); return 1; } fseek(infile, 0, SEEK_END); entry.size = ftell(infile); fclose(infile); fprintf(outfile, "\t0x%08x,\n", entry.offset); entry.offset += entry.size; } fprintf(outfile, "};\nconst unsigned char asset_data[] = {\n"); for (i = 0; i < file_count; i++) { strcpy(entry.name, argv[i + 3]); strcpy(name, workingdir); strcat(name, dsep); strcat(name, entry.name); infile = fopen(name, "rb"); if (!infile) { fprintf( stderr, "Failed to open '%s' for reading.\n", entry.name ); return 1; } fseek(infile, 0, SEEK_END); entry.size = ftell(infile); rewind(infile); for (j = 0; j < entry.size; j += buffer_size) { int k; read = fread(buffer, 1, buffer_size, infile); fprintf(outfile, "\t"); for (k = 0; k < read; k++) { fprintf(outfile, "0x%02x, ", (unsigned char)buffer[k]); } fseek(outfile, ftell(outfile) - 1, SEEK_SET); fprintf(outfile, "\n"); } fclose(infile); } fprintf(outfile, "};\n#endif\n#endif\n"); fclose(outfile); return 0; }