diff options
author | quou <quou@disroot.org> | 2024-09-23 21:11:29 +1000 |
---|---|---|
committer | quou <quou@disroot.org> | 2024-09-23 21:11:29 +1000 |
commit | 259237fae792e8f19d9b7920b2354f75bc1789e2 (patch) | |
tree | af079430f30f7df1b76efc4f96f520b0b6eee0af /packassets.c |
initial commit, basic platform and rendering code.
Diffstat (limited to 'packassets.c')
-rw-r--r-- | packassets.c | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/packassets.c b/packassets.c new file mode 100644 index 0000000..85e0ddb --- /dev/null +++ b/packassets.c @@ -0,0 +1,126 @@ +#include "error.h" + +#include <stdio.h> +#include <string.h> + +#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; +} |