]> 4ch.mooo.com Git - 16.git/blob - src/lib/mapread.c
8a8d5d87396c62a3eff362fd3036459522949446
[16.git] / src / lib / mapread.c
1 /* Project 16 Source Code~
2  * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover
3  *
4  * This file is part of Project 16.
5  *
6  * Project 16 is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Project 16 is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>, or
18  * write to the Free Software Foundation, Inc., 51 Franklin Street,
19  * Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  */
22
23 #include "src/lib/mapread.h"
24
25 int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
26         if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
27                         strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
28                 return 0;
29         }
30         return -1;
31 }
32
33 //this function is quite messy ^^; sorry! it is a quick and dirty fix~
34 word dump(const char *js, jsmntok_t *t, size_t count, word indent, char *js_sv, map_t *map, dword q) {
35         dword i;
36         word j;//, k;
37         bitmap_t bp;
38         #ifdef DEBUG_JS
39         if(indent==0)
40         {
41                 fprintf(stdout, "%s\n", js);
42                 fprintf(stdout, "\n");
43         }
44         #endif
45         #ifdef DEBUG_DUMPVARS
46         fprintf(stdout, "t->size=[%d]   ", t->size);
47         fprintf(stdout, "q=[%d] ", q);
48         fprintf(stdout, "indent= [%d]   ", indent);
49         fprintf(stdout, "js_sv= [%s]\n", js_sv);
50         #endif
51         if (count == 0) {
52                 return 0;
53         }
54         /* We may want to do strtol() here to get numeric value */
55 //0000fprintf(stderr, "t->type=%d\n", t->type);
56         if (t->type == JSMN_PRIMITIVE) {
57                 if(strstr(js_sv, "data"))
58                 {
59                         /*
60                                 here we should recursivly call dump again here to skip over the array until we get the facking width of the map.
61                                 so we can initiate the map which allocates the facking map->tiles->data->data properly and THEN we can return
62                                 here to read the data.... That is my design for this... wwww
63
64                                 FUCK well i am stuck.... wwww
65                         */
66                         map->data[q] = (byte)atoi(js+t->start);
67                         #ifdef DEBUG_MAPDATA
68                                 fprintf(stdout, "%d[%d]", q, map->data[q]);
69                         #endif
70                 }
71                 else
72                 if(strstr(js_sv, "height"))
73                 {
74                         map->height = atoi(js+t->start);
75                         #ifdef DEBUG_MAPVAR
76                         fprintf(stdout, "indent= [%d]   ", indent);
77                         fprintf(stdout, "h:[%d]\n", map->height);
78                         #endif
79                 }else if(strstr(js_sv, "width"))
80                 {
81                         map->width = atoi(js+t->start);
82                         #ifdef DEBUG_MAPVAR
83                         fprintf(stdout, "indent= [%d]   ", indent);
84                         fprintf(stdout, "w:[%d]\n", map->width);
85                         #endif
86                 }
87                 return 1;
88                 /* We may use strndup() to fetch string value */
89         } else if (t->type == JSMN_STRING) {
90                 if(jsoneq(js, t, "data") == 0)
91                 {
92 //                      fprintf(stdout, "[[[[%d|%d]]]]\n", &(t+1)->size, (t+1)->size);
93 //                      fprintf(stdout, "\n%.*s[xx[%d|%d]xx]\n", (t+1)->end - (t+1)->start, js+(t+1)->start, &(t+1)->size, (t+1)->size);
94                         map->data = malloc(sizeof(byte) * (t+1)->size);
95                         map->tiles = malloc(sizeof(tiles_t));
96                         map->tiles->btdata = malloc(sizeof(bitmap_t));
97                         //fix this to be far~
98                         bp = bitmapLoadPcx("data/ed.pcx");
99 //                      bp = bitmapLoadPcx("data/koishi^^.pcx");
100                         map->tiles->btdata = &bp;
101 //----                  map->tiles->data = planar_buf_from_bitmap(&bp);
102                         //map->tiles->data->data = malloc((16/**2*/)*16);
103                         //map->tiles->data->width = (16/**2*/);
104                         //map->tiles->data->height= 16;
105                         map->tiles->tileHeight = 16;
106                         map->tiles->tileWidth = 16;
107                         map->tiles->rows = 1;
108                         map->tiles->cols = 1;
109                         map->tiles->debug_text=false;
110                         strcpy(js_sv, "data");//strdup(js+t->start);//, t->end - t->start);
111                 }
112                 else
113                 if (jsoneq(js, t, "height") == 0 && indent<=1)
114                 {
115                         strcpy(js_sv, "height");//strdup(js+t->start);//, t->end - t->start);
116                 }else
117                 if(jsoneq(js, t, "width") == 0 && indent<=1)
118                 {
119                         strcpy(js_sv, "width");//strdup(js+t->start);//, t->end - t->start);
120                 }else strcpy(js_sv, "\0");
121                 return 1;
122         } else if (t->type == JSMN_OBJECT) {
123                 //fprintf(stdout, "\n");
124                 j = 0;
125                 for (i = 0; i < t->size; i++) {
126                         //for (k = 0; k < indent; k++) fprintf(stdout, "\t");
127                         j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
128                         //fprintf(stdout, ": ");
129                         j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
130                         //fprintf(stdout, "\n");
131                 }
132                 return j+1;
133         } else if (t->type == JSMN_ARRAY) {
134                 j = 0;
135                 //fprintf(stdout, "==\n");
136                 for (i = 0; i < t->size; i++) {
137                         //for (k = 0; k < indent-1; k++) fprintf(stdout, "\t");
138                         //fprintf(stdout, "\t-");
139                         j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
140                         //fprintf(stdout, "==\n");
141                 }
142                 return j+1;
143         }
144         return 0;
145 }
146
147 int loadmap(char *mn, map_t *map)
148 {
149         int r;
150         static word incr=0;
151         int eof_expected = 0;
152         char *js = NULL;
153         size_t jslen = 0;
154         char buf[BUFSIZ];
155         static char js_ss[16];
156
157         jsmn_parser p;
158         jsmntok_t *tok;
159         size_t tokcount = 2;
160
161         FILE *fh = fopen(mn, "r");
162
163         /* Prepare parser */
164         jsmn_init(&p);
165
166         /* Allocate some tokens as a start */
167 //0000fprintf(stderr, "tok malloc\n");
168         tok = malloc(sizeof(*tok) * tokcount);
169         if (tok == NULL) {
170                 fprintf(stderr, "malloc(): errno=%d\n", errno);
171                 return 3;
172         }
173
174         for (;;) {
175                 /* Read another chunk */
176 //0000fprintf(stderr, "read\n");
177                 r = fread(buf, 1, sizeof(buf), fh);
178                 if (r < 0) {
179                         fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
180                         return 1;
181                 }
182                 if (r == 0) {
183                         if (eof_expected != 0) {
184                                 return 0;
185                         } else {
186                                 fprintf(stderr, "fread(): unexpected EOF\n");
187                                 return 2;
188                         }
189                 }
190 //0000fprintf(stdout, "r=       [%d]    BUFSIZ=%d\n", r, BUFSIZ);
191 //0000fprintf(stderr, "js alloc~\n");
192                 js = realloc(js, jslen + r + 1);
193                 if (js == NULL) {
194                         fprintf(stderr, "*js=%Fp\n", *js);
195                         fprintf(stderr, "realloc(): errno = %d\n", errno);
196                         return 3;
197                 }
198                 strncpy(js + jslen, buf, r);
199                 jslen = jslen + r;
200
201 again:
202 //0000fprintf(stdout, " parse~ tok=%zu  jslen=%zu       r=%d    _memavl()=%u    BUFSIZ=%d~\n", tokcount, jslen, r, _memavl(), BUFSIZ);
203 //0000fprintf(stdout, "p=[%u]   [%u]    [%d]\n", p.pos, p.toknext, p.toksuper);
204 /*
205                 I think it crashes on the line below when it tries to parse the data of huge maps... wwww this is a jsmn problem wwww
206 */
207                 r = jsmn_parse(&p, js, jslen, tok, tokcount);
208 //0000fprintf(stdout, "r=       [%d]\n", r);
209                 if (r < 0) {
210                         if (r == JSMN_ERROR_NOMEM) {
211                                 tokcount = tokcount * 2;
212 //0000fprintf(stderr, "tok realloc~ %zu\n", tokcount);
213                                 tok = realloc(tok, sizeof(*tok) * tokcount);
214                                 if (tok == NULL) {
215                                         fprintf(stderr, "realloc(): errno=%d\n", errno);
216                                         return 3;
217                                 }
218                                 goto again;
219                         }
220                 } else {
221                         //printf("js=%Fp\n", (js));
222                         //printf("*js=%Fp\n", (*(js)));
223                         //printf("&*js=%s\n", &(*(js)));
224                         //printf("&buf=[%Fp]\n", &buf);
225                         //printf("&buf_seg=[%x]\n", FP_SEG(&buf));
226                         //printf("&buf_off=[%x]\n", FP_OFF(&buf));
227                         //printf("&buf_fp=[%Fp]\n", MK_FP(FP_SEG(&buf), FP_OFF(&buf)));
228                         //printf("buf=[\n%s\n]\n", buf);
229                         //printf("buff=[%Fp]\n", buff);
230                         //printf("(*buff)=[%Fp]\n", (*buff));
231                         //printf("&(*buff)=[\n%s\n]\n", &(*buff));
232                         #ifdef DEBUG_DUMPVARS
233                         fprintf(stdout, "running dump~\n");
234                         #endif
235                         dump(js, tok, p.toknext, incr, &js_ss, map, 0);
236                         eof_expected = 1;
237                 }
238         }
239
240         free(js);
241         free(tok);
242         fclose(fh);
243
244         return 0;
245 }