-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrespack.cpp
More file actions
325 lines (303 loc) · 9.49 KB
/
respack.cpp
File metadata and controls
325 lines (303 loc) · 9.49 KB
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
/*\file respack.cpp
* \brief Implementation for resource packer
*/
#include "respack.h"
#include <stdarg.h>
/*!\brief Constructor
* \param Unitname Name of output files
* \param Classname Name of resource class
*/
ResPack::ResPack(const std::string &Unitname,const std::string &Classname){
unitname=Unitname;
classname=Classname;
}
/*!\brief Deconstructor
*
* Free's allocated data
*/
ResPack::~ResPack(){
for(unsigned i=0;i<data.size();i++){
delete [] data[i].data;
}
}
/*!\brief Generates code to encapsulate resource
* \param Resource Resource to encapsulate
* \return String with generated code
*/
std::string ResPack::CreateImplementation(const resource_t &Resource){
std::string r;
r+=classname+"::blob "+classname+"::"+Resource.name+"(){\n";
r+=" const static uint8_t "+Resource.name+"_buffer[]={";
for(unsigned i=0;i<Resource.size;i++){
if(i%20==0){
r+="\n ";
}
r+=Format("0x%02x,",Resource.data[i]);
}
r=r.substr(0,r.size()-1)+"};\n";
r+=" blob object;\n";
r+=" object.data="+Resource.name+"_buffer;\n";
r+=" object.size="+Format("%d",Resource.size)+";\n";
r+=" return object;\n";
r+="}\n";
return r;
}
/*!\brief Generates code to encapsulate loaded resources
* \return String with generated implementation code
*/
std::string ResPack::CreateImplementation(){
// Includes
std::string r;
r+="#include \""+unitname+".h\"\n\n";
// Resource constructor
r+=classname+"::resource::resource(const std::string &Name,const size_t &Size){\n";
r+=" name=Name;\n";
r+=" size=Size;\n";
r+="}\n\n";
// Resource enumerator
r+="std::vector<"+classname+"::resource> "+classname+"::Enumerate(){\n";
r+=" std::vector<"+classname+"::resource> resources;\n";
for(unsigned i=0;i<data.size();i++){
r+=" resources.push_back(resource(\""+data[i].name+"\","+Format("%d",data[i].size)+"));\n";
}
r+=" return resources;\n";
r+="}\n\n";
// Resource implementations
for(unsigned i=0;i<data.size();i++){
r+=CreateImplementation(data[i])+"\n";
}
return r;
}
/*!\brief Generates code to encapsulate resource
* \param Resource Resource to encapsulate
* \return String with generated header code
*/
std::string ResPack::CreateHeader(const resource_t &Resource){
return "blob "+Resource.name+"();";
}
/*!\brief Generates code to encapsulate loaded resource
* \return String with generated code
*/
std::string ResPack::CreateHeader(){
// Create headerguard and includes
std::string r;
std::string hg=unitname;
for(unsigned i=0;i<hg.size();i++) hg[i]=toupper(hg[i]);
r+="#ifndef _"+hg+"_H_\n";
r+="#define _"+hg+"_H_\n\n";
r+="#include <stdint.h>\n";
r+="#include <string>\n";
r+="#include <vector>\n";
r+="\n";
// Create resource class
r+="class "+classname+"{\n";
r+=" public:\n";
r+=" // Structure for enumerating resources\n";
r+=" struct resource{\n";
r+=" resource(const std::string &Name,const size_t &Size);\n";
r+=" std::string name;\n";
r+=" size_t size;\n";
r+=" };\n\n";
r+=" // Structure for encapsulating resources\n";
r+=" struct blob{\n";
r+=" const uint8_t *data=0;\n";
r+=" size_t size=0;\n";
r+=" };\n";
r+="\n";
r+=" // Resources\n";
for(unsigned i=0;i<data.size();i++){
r+=" static "+CreateHeader(data[i])+"\n";
}
r+="\n";
r+=" // Resource enumerator\n";
r+=" static std::vector<resource> Enumerate();\n";
r+="};\n";
r+="\n";
// Terminate headerguard
r+="#endif\n";
return r;
}
/*!\brief Formats a string
* \param Input String with formatting
* \return Formatted string
*/
std::string ResPack::Format(const std::string &Input,...){
const int size=1024;
char buffer[size];
va_list arg;
va_start(arg,Input);
int len=vsnprintf(buffer,size,Input.c_str(),arg);
va_end(arg);
if(len>=0){
return std::string(buffer,len);
}
else{
return "";
}
}
/*!\brief Parses file basename (No folder or extension) from path
* \param Path Pathname to parse
* \return File basename
*/
std::string ResPack::ParseName(const std::string &Path){
size_t i=Path.rfind('/',Path.length());
if(i!=std::string::npos){
std::string filename=Path.substr(i+1,Path.length()-i);
size_t lastindex=filename.find_last_of(".");
return filename.substr(0,lastindex).c_str();
}
size_t lastindex=Path.find_last_of(".");
return Path.substr(0,lastindex).c_str();
}
/*!\brief Parses file extension from path
* \param Path Pathname to parse
* \return File extension as a string
*/
std::string ResPack::ParseExt(const std::string &Path){
size_t limit=Path.find_last_of("/");
if(limit==std::string::npos) limit=0;
size_t p=Path.rfind('.');
if(p!=std::string::npos && p>=limit){
return Path.substr(p+1,Path.length()-p).c_str();
}
return "";
}
/*!\brief Add resource to encapsulate
* \param Path Pathname to resource
* \return True upon success
*/
bool ResPack::AddResource(const std::string &Path){
bool retval=false;
FILE *fd=fopen(Path.c_str(),"rb");
if(fd){
// Read data from file
fseek(fd,0,SEEK_END);
size_t size=ftell(fd);
fseek(fd,0,SEEK_SET);
resource_t res;
res.data=new uint8_t[size+1];
res.size=size;
if(res.data){
if(fread(res.data,1,size,fd)==size){
// Create an asset name
std::string n=ParseName(Path);
std::string e=ParseExt(Path);
// TODO: Internal replace function
//n.replace(".","_");
//e.replace(".","_");
res.name=e.size()?n+"_"+e:n;
// Stash asset
data.push_back(res);
retval=true;
}
else{
delete [] res.data;
}
}
fclose(fd);
}
if(retval){
//TODO: Optionally compress data
}
return retval;
}
#ifndef NOMAIN
int main(int argc,char *argv[]){
// Parse parameters
std::string uname="resources";
std::string cname="Resources";
bool print=false;
std::vector<std::string> resources;
for(int i=1;i<argc;i++){
if(!strcmp(argv[i],"--uname") || !strcmp(argv[i],"-u")){
uname=argv[++i];
}
else if(!strcmp(argv[i],"--cname") || !strcmp(argv[i],"-c")){
cname=argv[++i];
}
else if(!strcmp(argv[i],"--print") || !strcmp(argv[i],"-p")){
print=true;
}
else if(!strcmp(argv[i],"--help") || !strcmp(argv[i],"-h")){
printf("Resource packer\n");
printf("Vegard Fiksdal\n");
printf("\n");
printf("Usage: %s [OPTIONS] [RESOURCES]\n",argv[0]);
printf("\n");
printf("Options:\n");
printf("\t-u|--uname NAME\tSet unit name (Default=\"resources\")\n");
printf("\t-c|--cname NAME\tSet class name (Default=\"Resources\")\n");
printf("\t-p|--print\tPrint output instead of writing to file\n");
printf("\t-h|--help\tThis helpfull screen\n");
printf("\n");
return 0;
}
else if(argv[i][0]=='-'){
printf("Invalid parameter: %s\n",argv[i]);
printf("Use %s -h for usage instructions\n",argv[0]);
return -1;
}
else{
resources.push_back(argv[i]);
}
}
// Check for resources to pack
if(resources.size()==0){
printf("No resources to process!\n");
printf("Use %s -h for usage instructions\n",argv[0]);
return -1;
}
// Check for duplicates
for(unsigned i=0;i<resources.size();i++){
for(unsigned j=0;j<resources.size();j++){
if(i!=j && resources[i]==resources[j]){
printf("Dupicate resources: %s\n",resources[i].c_str());
return -1;
}
}
}
// Pack resources
ResPack respack(uname,cname);
for(unsigned i=0;i<resources.size();i++){
if(!respack.AddResource(resources[i])){
printf("Failed to load resource %s\n",resources[i].c_str());
return -1;
}
}
// Output results
std::string header=respack.CreateHeader();
std::string implementation=respack.CreateImplementation();
if(print){
printf("%s\n",header.c_str());
printf("%s\n",implementation.c_str());
}
else{
FILE *hfd=fopen((uname+".h").c_str(),"wb");
if(!hfd){
printf("Failed to open %s.h for writing!\n",uname.c_str());
return -1;
}
FILE *ifd=fopen((uname+".cpp").c_str(),"wb");
if(!ifd){
printf("Failed to open %s.cpp for writing!\n",uname.c_str());
fclose(hfd);
return -1;
}
if(fwrite(header.c_str(),1,header.size(),hfd)!=header.size()){
printf("Failed write to header file %s.h\n",uname.c_str());
fclose(hfd);
fclose(ifd);
return -1;
}
if(fwrite(implementation.c_str(),1,implementation.size(),ifd)!=implementation.size()){
printf("Failed write to implementation file %s.cpp\n",uname.c_str());
fclose(hfd);
fclose(ifd);
return -1;
}
fclose(hfd);
fclose(ifd);
}
return 0;
}
#endif