-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bin2D.d
362 lines (310 loc) · 9.29 KB
/
Bin2D.d
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env rdmd
module Bin2D;
import std.file : exists, isFile, isDir, dirEntries, SpanMode;
import std.stdio : writeln, File, SEEK_CUR, write, stdout;
import std.string : indexOf, lastIndexOf, tr;
import std.path : baseName, dirName;
import std.math : ceil;
import std.conv : to;
import std.getopt : getopt, defaultGetoptPrinter, config;
import std.format : formattedWrite;
import std.array : Appender;
static ubyte[4096] buffer;
int main(string[] args) {
string[] originalArgs = args;
string outputFile;
string modulename;
if (args.length > 2) {
outputFile = args[1];
if (outputFile.indexOf("=") > 0) {
modulename = outputFile[outputFile.indexOf("=") + 1 .. $];
outputFile = outputFile[0 .. outputFile.indexOf("=")];
}
args = args[0] ~ args[2 .. $];
} else
args = [args[0]];
bool usePackage;
bool useUnittest;
bool useEnum;
auto procArgs = getopt(
args,
"onlyPackage", "Limits the embedded files using package modifier", &usePackage,
"onlyUnittest", "Limits the embedded files using version(unittest)", &useUnittest,
"useEnum", `Use enum instead of const(ubyte[]) to store the data.
Allows for usage at compile time but memory increase for runtime.
Will require usage of the enum __ctfeValues instead of values for CTFE access.`, &useEnum
);
args = args[1 .. $];
if (args.length > 0 && !procArgs.helpWanted) {
File output = File(outputFile, "w");
scope(exit) output.close;
output.write("/**\n * Generated_By $(WEB, github.com/rikkimax/bin2d, Bin2D)\n",
" * Copyright: Richard (Rikki) Andrew Cattermole 2014 - 2015\n * \n",
" * Using_Command: ");
foreach(arg; originalArgs) {
output.write(arg ~ " ");
}
output.seek(-1, SEEK_CUR);
output.write("\n * \n * Do $(I not) modify this file directly.\n");
output.write(" */\nmodule ", modulename, ";\n");
if (usePackage && useUnittest)
output.write("version(unittest) package:\n");
else if (usePackage)
output.write("package:\n");
else if (useUnittest)
output.write("version(unittest):\n");
// file name processing
string[] files;
string[] filesWithoutScanDir;
foreach (file; args) {
if (file[$-1] == '/' || file[$-1] == '\\') //Clean off paths with slash on end eg. folder\
file.length--;
file = file.tr("\\", "/"); //use forward slashes
if (exists(file)) {
if (isFile(file)) {
files ~= file;
filesWithoutScanDir ~= baseName(file);
} else if (isDir(file)) {
foreach (entry; dirEntries(file, SpanMode.breadth)) {
if (isFile(entry)) {
files ~= entry.tr("\\", "/");
filesWithoutScanDir ~= entry[file.length + 1 .. $].tr("\\", "/");
}
}
}
}
}
ushort longestFileName;
string[] filenames;
foreach(file; files){
// not ideal in terms of allocation
// but atleast it is only the file names,
// if we were duplicating or actually storing the file data
// then we might start having problems
char[] t = cast(char[])file.dup;
// has to be duplicated because of modification would override the values
// of course if this was string, it would do something similar
// with implicit allocations + dup.
if (lastIndexOf(t, "/") > 0)
t = t[lastIndexOf(t, "/") + 1 .. $];
// sanitises file names
foreach(i, c; t) {
switch(c) {
case 'a': .. case 'z':
case 'A': .. case 'Z':
case '0': .. case '9':
case '_':
break;
default:
t[i] = '_';
}
}
filenames ~= cast(string)t;
if (file.length > longestFileName)
longestFileName = cast(ushort)file.length;
}
output.write(
/*BEGIN FILE HEADER P1*/
`
import std.file : write, isDir, exists, mkdirRecurse, rmdirRecurse, tempDir, mkdir;
import std.path : buildPath, dirName;
import std.process : thisProcessID;
import std.conv : text;
deprecated("Use outputFilesToFileSystem instead")
alias outputBin2D2FS = outputFilesToFileSystem;
deprecated("Use names instead")
alias assetNames = names;
deprecated("Use values instead")
alias assetValues = values;
deprecated("Use originalNames instead")
alias assetOriginalNames = originalNames;
string rootDirectory;
void cleanup(){
rmdirRecurse(rootDirectory);
}
string[string] outputFilesToFileSystem() {
return outputFilesToFileSystem(buildPath(tempDir(), text(thisProcessID())));
}
string[string] outputFilesToFileSystem(string dir)
in {
rootDirectory = dir;
if (exists(dir)) {
if (isDir(dir)) {
rmdirRecurse(dir);
mkdirRecurse(dir);
} else {
mkdirRecurse(dir);
}
} else {
mkdirRecurse(dir);
}
} body {
`
/*END FILE HEADER P1*/);
if (useEnum) {
output.write(/*BEGIN FILE HEADER P2*/`
string[string] files;`);
foreach(i, file; files) {
output.write("
if (!buildPath(dir, \"", file, "\").dirName().exists())
mkdir(cast(string)buildPath(dir, \"", file, "\").dirName());
files[\"", file, "\"] ~= cast(string)buildPath(dir, \"", file, "\");
write(buildPath(dir, \"", file, "\"), ", filenames[i], ");");
}
output.write(`
return files;
}
`
/*END FILE HEADER p2*/);
} else {
output.write(/*BEGIN FILE HEADER P2*/`
string[string] files;
foreach(i, name; names) {
string realname = originalNames[i];
if (!buildPath(dir, realname).dirName().exists())
mkdir(cast(string)buildPath(dir, realname).dirName());
files[cast(string)realname] ~= cast(string)buildPath(dir, realname);
write(buildPath(dir, realname), *values[i]);
}
return files;
}
`
/*END FILE HEADER p2*/);
}
// write report heading
ushort lenNames = cast(ushort)(longestFileName + 2);
write("|");
foreach(i; 0 .. (lenNames * 2) + 1)
write("-");
writeln("|");
write("|");
foreach(i; 0 .. lenNames-3)
write(" ");
write("REPORT");
foreach(i; 0 .. lenNames-2)
write(" ");
writeln("|");
write("|");
foreach(i; 0 .. lenNames)
write("-");
write("|");
foreach(i; 0 .. lenNames)
write("-");
writeln("|");
stdout.flush;
Appender!(char[]) dfout;
// giant chunk of memory that should be able to hold exactly one chunk read
// is reused, so memory use of program shouldn't be all that high
dfout.reserve(buffer.length * 3);
foreach(i, file; files) {
// output file contents
if (useEnum)
output.write("enum ubyte[] ", filenames[i], " = cast(ubyte[])x\"");
else
output.write("const(ubyte[]) ", filenames[i], " = cast(const(ubyte[]))x\"");
File readFrom = File(file, "rb");
bool readSome;
foreach(bytes; readFrom.byChunk(buffer)) {
foreach(b; bytes) {
readSome = true;
formattedWrite(dfout, "%02x ", b);
}
output.write(dfout.data());
dfout.clear();
}
if (readSome)
output.seek(-1, SEEK_CUR);
output.write("\";\n");
readFrom.close;
// output report for file
string replac = filenames[i];
write('|');
if (file.length > lenNames-2)
write(' ', file[0 .. lenNames-2], ' ');
else {
foreach(j; 0 .. lenNames/2 - cast(uint)ceil(file.length / 2f) + 1)
write(' ');
write(file);
foreach(j; 0 .. lenNames/2 - (file.length / 2))
write(' ');
}
write('|');
if (replac.length > lenNames - 2)
write(' ', replac[0 .. lenNames-2], ' ');
else {
foreach(j; 0 .. lenNames/2 - cast(uint)ceil(replac.length / 2f) + 1)
write(' ');
write(replac);
foreach(j; 0 .. lenNames/2 - (replac.length / 2))
write(' ');
}
writeln('|');
stdout.flush;
}
// close report table
write("|");
foreach(i; 0 .. lenNames)
write("-");
write("|");
foreach(i; 0 .. lenNames)
write("-");
writeln("|");
stdout.flush;
// write names/originalNames/values out
output.write("\n\n");
if (useEnum)
output.write("enum string[] names = [");
else
output.write("const(string[]) names = [");
foreach(i, name; filenames) {
output.write("\"", name , "\", ");
}
output.seek(-2, SEEK_CUR);
output.write("];\n");
if (useEnum)
output.write("enum string[] originalNames = [");
else
output.write("const(string[]) originalNames = [");
foreach(name; files) {
output.write("`", name.tr("/","\\"), "`, ");
}
output.seek(-2, SEEK_CUR);
output.write("];\n");
if (useEnum) {
output.write("enum ubyte[][] __ctfeValues = [");
foreach(i, name; filenames) {
output.write(name, ", ");
}
output.seek(-2, SEEK_CUR);
output.write("];\n");
}
if (useEnum) {
output.write(`
const(ubyte[]*[]) values;
shared static this() {
ubyte[]*[] ret;
ret.length = __ctfeValues.length;
foreach(i, v; __ctfeValues)
ret[i] = &v;
values = cast(const)ret;
}`);
} else {
output.write("const(ubyte[]*[]) values = [");
foreach(i, name; filenames) {
if (!useEnum)
output.write("&", name, ", ");
}
output.seek(-2, SEEK_CUR);
output.write("];");
}
return 0;
} else {
defaultGetoptPrinter("
Usage: Bin2D <output file>[=<module name>] [switches] <files or directories...>
Bin2D is a resource compiler for the D programming language.
It compiles resources down to D source code.
For inclusion into a build. Later accessible given an optional module name.
Switches:"[1 .. $], procArgs.options);
return -1;
}
}