~melchizedek6809/WolkenWelten

ref: e910bd82e598d6f560b8899dd1d88fb3ed554cd3 WolkenWelten/tools/assets.c -rw-r--r-- 2.3 KiB
e910bd82Ben (X13/Arch) Updated contact info 8 months ago
                                                                                
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
/*
 * Wolkenwelten - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>

char *fileSlug(char *path){
	static char ret[1024];
	char c,*r,*start;
	r = ret;
	start = NULL;
	while((c = *path++)){
		if((c == '/') || (c == '.') || (c < 32)){
			if(start == NULL){start = r+1;}
			*r++ = '_';
			continue;
		}
		*r++ = c;
	}
	*r = 0;
	return start;
}

int main(int argc,char *argv[]){
	char fn[512];
	snprintf(fn,sizeof(fn)-1,"%s.c",argv[1]);
	fn[sizeof(fn)-1] = 0;
	FILE *cfp = fopen(fn,"w");
	snprintf(fn,sizeof(fn)-1,"%s.h",argv[1]);
	fn[sizeof(fn)-1] = 0;
	FILE *hfp = fopen(fn,"w");
	static unsigned char buffer[1024*1024*32];
	size_t filelen=0,readlen=0,ii;
	int lc=0,i;
	if((cfp == NULL) || (hfp == NULL)){
		fprintf(stderr,"Error opening src/tmp/assets*");
		return 1;
	}
	fprintf(hfp,"#pragma once\n#include <stddef.h>\n\n");
	fprintf(cfp,"#include <stddef.h>\n\n");
	for(i=2;i<argc;i++){
		FILE *afp = fopen(argv[i],"rb");
		if(afp == NULL){
			fprintf(stderr,"Error opening %s\n",argv[i]);
			return 2;
		}
		readlen=0;
		fseek(afp,0,SEEK_END);
		filelen = ftell(afp);
		fseek(afp,0,SEEK_SET);

		while(readlen < filelen){
			readlen += fread(buffer+readlen,1,filelen-readlen,afp);
		}
		fclose(afp);

		fprintf(hfp,"extern unsigned  int %s_len;\n",fileSlug(argv[i]));
		fprintf(hfp,"extern unsigned char %s_data[];\n\n",fileSlug(argv[i]));

		fprintf(cfp,"unsigned  int %s_len    = %u;\n",fileSlug(argv[i]),(int)filelen);
		fprintf(cfp,"unsigned char %s_data[] = {\n ",fileSlug(argv[i]));
		lc=0;
		for(ii=0;ii<filelen;ii++){
			fprintf(cfp,"0x%02X,",buffer[ii]);
			if(++lc > 19){
				fprintf(cfp,"\n ");
				lc=0;
			}
		}
		fprintf(cfp,"0\n};\n\n");


	}

	fclose(cfp);
	fclose(hfp);
	return 0;
}