-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.c
400 lines (328 loc) · 9.03 KB
/
6.c
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <curl/curl.h>
#include <jansson.h>
// Code does: load a texture needed by shader from shadertoy
// see: https://www.shadertoy.com/api
// If json exists, don't download, parse from file.
// If texture exists, download is skipped also.
// example:
// ./6 Ms2SWW # with text
// ./6 Xds3Rr # with sound -> should tell not supported
/*
Access the assets
-----------------
When you retrieve a shader you will see a key called "inputs",
this can be a texture/video/keyboard/sound used by the shader.
The JSON returned when accessing a shader will look like this:
[..]
{"inputs":[{"id":17,"src":"/media/a/(hash.extension)","ctype":"texture","channel":0}
[..]
[array] | {"object"}
What kind of hash is it? It's not sha256sum!
Don't understand the logic/purpose behind the ID also right now either;
prob. DB connection in mindbut those should be auto.
To access this specific asset you can just cut and paste this path
https://www.shadertoy.com/media/a/(hash.extension)
*/
#define CONFFILE "api_key"
#define BUFFER_SIZE (256 * 1024) /* 256 KB */
// concat base and api
#define URL_BASE "https://www.shadertoy.com"
#define URL_API ""URL_BASE"/api/v1/shaders/%s?key=%s"
#define URL_SIZE 512
struct write_result
{
char *data;
int pos;
};
// read file into array
char *read_file(char *);
// read file into array without newline
char *read_conf(char *);
// load json
char *request(const char *);
size_t write_response(void *, size_t , size_t , void *);
// load and write image if file doesn't exist
int save_image( char *, char *);
int main(int argc, char *argv[])
{
// check arg
if(argc != 2){
fprintf(stderr, "usage: %s shader-ID\n", argv[0]);
return 1;
}
// check file
char *json_file;
char buffer[64];
char *text;
snprintf(buffer, sizeof(buffer), "%s.json",argv[1]);
if( access( buffer, F_OK ) != -1 ){
printf("exists, nothing to download!\n");
text=read_file(buffer);
json_file = text;
} else {
// read API-Key
char *key;
key=read_conf(""CONFFILE);
if(!key)
return 2;
// set URL
char url[URL_SIZE];
snprintf(url, URL_SIZE, URL_API, argv[1], key);
// download json
text = request(url);
if(!text)
return 3;
json_file = text;
// save shader json
FILE *fp;
fp = fopen( buffer , "w" );
fputs(text, fp);
fclose(fp);
printf(buffer);
// set file non executable, one probably won't need this
char cmd[128];
snprintf(cmd, sizeof(cmd), "chmod -x %s", buffer);
system(cmd);
free(key);
}
if(!json_file)
return 2;
// decode json
json_error_t error;
json_t *root;
root = json_loads(json_file, 0, &error);
if(!root){
fprintf(stderr, "error: on line %d: %s\n", error.line, error.text);
return 1;
}
// extract name
const char *requested_info;
json_t *Shader, *info, *name;
Shader = json_object_get(root , "Shader");
info = json_object_get(Shader, "info");
name = json_object_get(info , "name");
requested_info = json_string_value(name);
printf("Shader-Name: %s\n",requested_info);
json_t *renderpass, *inputs; // *id;
renderpass = json_object_get(Shader, "renderpass");
for(size_t i = 0; i < json_array_size(renderpass); i++){
json_t *data = json_array_get(renderpass, i);
inputs = json_object_get(data, "inputs");
if(json_is_array(inputs)){
for(size_t j = 0; j < json_array_size(inputs); j++){
json_t *data = json_array_get(inputs , j);
// getting ID - no use for now
// id = json_object_get(data, "id");
// int number = json_integer_value(id);
// getting type
json_t *type;
type = json_object_get(data, "ctype");
// only texture support for now
requested_info = json_string_value(type);
if (!strcmp("texture",requested_info)){
// attrib_position = i;
printf("type: %s\n",requested_info);
// "channel":0
json_t *channel;
channel= json_object_get(data, "channel");
int chan = json_integer_value(channel);
// printf("channel: %d\n",chan);
(void) chan;
// "sampler":{"filter":"mipmap","wrap":"repeat","vflip":"true","srgb":"false","internal":"byte"}
// getting partial URL
json_t *src;
src = json_object_get(data, "src");
requested_info = json_string_value(src);
// printf("src: %s\n",requested_info);
char *str = strdup(requested_info);
// printf("%s\n", requested_info);
char *token = strtok(str, "/");
char *last;
while (token != NULL){
// printf("%s\n", token);
last=token;
token = strtok(NULL, "/");
}
// printf(str);
// printf("Filename: %s\n", last);
// add filename and check if file is there
// if type is texture, check and set vars
// merge into shade it with drop events
// first text drop
// then file drop
// no sopport
// .ST.glsl ST.json
// then FFT
// save image
char url[URL_SIZE];
snprintf(url, URL_SIZE, URL_BASE"%s", requested_info);
// printf("%s\n",url);
// fflush(stdout);
save_image(url,last);
} else
printf("Not a supported type!\n");
}
}
}
free(text);
json_decref(root);
return EXIT_SUCCESS;
}
char * read_conf(char *filename)
{
long length = 0;
char *result = NULL;
FILE *file = fopen(filename, "r");
if(file) {
int status = fseek(file, 0, SEEK_END);
if(status != 0) {
fclose(file);
return NULL;
}
length = ftell(file);
status = fseek(file, 0, SEEK_SET);
if(status != 0){
fclose(file);
return NULL;
}
result = malloc((length) * sizeof(char));
//substitute newline against string termination
if(result) {
size_t actual_length = fread(result, sizeof(char), length , file);
result[actual_length-1] = '\0';
}
fclose(file);
return result;
}
fprintf(stderr,"Couldn't read %s\n", filename);
return NULL;
}
size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream)
{
struct write_result *result = (struct write_result *)stream;
if(result->pos + size * nmemb >= BUFFER_SIZE - 1){
fprintf(stderr, "error: too small buffer\n");
return 0;
}
memcpy(result->data + result->pos, ptr, size * nmemb);
result->pos += size * nmemb;
return size * nmemb;
}
char *request(const char *url)
{
CURL *curl = NULL;
CURLcode status;
struct curl_slist *headers = NULL;
char *data = NULL;
long code;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(!curl)
goto error;
data = malloc(BUFFER_SIZE);
if(!data)
goto error;
struct write_result write_result = {
.data = data,
.pos = 0
};
curl_easy_setopt(curl, CURLOPT_URL, url);
headers = curl_slist_append(headers, "User-Agent: Acry Shadertoy API crawler");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_response);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_result);
status = curl_easy_perform(curl);
if(status != 0){
fprintf(stderr, "error: unable to request data from %s:\n", url);
fprintf(stderr, "%s\n", curl_easy_strerror(status));
goto error;
}
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
if(code != 200){
fprintf(stderr, "error: server responded with code %ld\n", code);
goto error;
}
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
curl_global_cleanup();
/* zero-terminate the result */
data[write_result.pos] = '\0';
return data;
error:
if(data)
free(data);
if(curl)
curl_easy_cleanup(curl);
if(headers)
curl_slist_free_all(headers);
curl_global_cleanup();
return NULL;
}
int save_image( char *url, char *filename)
{
// check if file exists:
if( access( filename, F_OK ) != -1 ) {
printf("exists, nothing to do!\n");
return 0;
} else {
printf("does not exist, trying to download...\n");
CURL *image;
image = curl_easy_init();
if( image ){
// Open file
FILE *fp;
fp = fopen(filename, "wb");
// set file non executable, one probably won't need this
char buffer[128];
snprintf(buffer, sizeof(buffer), "chmod -x %s", filename);
system(buffer);
if( fp == NULL )
printf("no image for you");
CURLcode imgresult;
curl_easy_setopt(image, CURLOPT_URL, url);
curl_easy_setopt(image, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(image, CURLOPT_WRITEDATA, fp);
// Grab image
imgresult = curl_easy_perform(image);
if( imgresult ){
printf("still no image for you");
}
curl_easy_cleanup(image);
fclose(fp);
return 0;
}
}
return 1;
}
char * read_file(char *filename)
{
long length = 0;
char *result = NULL;
FILE *file = fopen(filename, "r");
if(file) {
int status = fseek(file, 0, SEEK_END);
if(status != 0) {
fclose(file);
return NULL;
}
length = ftell(file);
status = fseek(file, 0, SEEK_SET);
if(status != 0){
fclose(file);
return NULL;
}
result = malloc((length+1) * sizeof(char));
if(result) {
size_t actual_length = fread(result, sizeof(char), length , file);
result[actual_length++] = '\0';
}
fclose(file);
return result;
}
fprintf(stderr,"Couldn't read %s\n", filename);
return NULL;
}