1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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;
}
|