1 /* Project 16 Source Code~
\r
2 * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover
\r
4 * This file is part of Project 16.
\r
6 * Project 16 is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Project 16 is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with this program. If not, see <http://www.gnu.org/licenses/>, or
\r
18 * write to the Free Software Foundation, Inc., 51 Franklin Street,
\r
19 * Fifth Floor, Boston, MA 02110-1301 USA.
\r
23 #include "src/lib/mapread.h"
\r
25 int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
\r
26 if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
\r
27 strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
\r
33 //this function is quite messy ^^; sorry! it is a quick and dirty fix~
\r
34 word dump(const char *js, jsmntok_t *t, size_t count, word indent, char *js_sv, map_t *map, dword q) {
\r
41 fprintf(stdout, "%s\n", js);
\r
42 fprintf(stdout, "\n");
\r
45 #ifdef DEBUG_DUMPVARS
\r
46 fprintf(stdout, "t->size=[%d] ", t->size);
\r
47 fprintf(stdout, "q=[%d] ", q);
\r
48 fprintf(stdout, "indent= [%d] ", indent);
\r
49 fprintf(stdout, "js_sv= [%s]\n", js_sv);
\r
54 /* We may want to do strtol() here to get numeric value */
\r
55 //0000fprintf(stderr, "t->type=%d\n", t->type);
\r
56 if (t->type == JSMN_PRIMITIVE) {
\r
57 if(strstr(js_sv, "data"))
\r
60 here we should recursivly call dump again here to skip over the array until we get the facking width of the map.
\r
61 so we can initiate the map which allocates the facking map->tiles->data->data properly and THEN we can return
\r
62 here to read the data.... That is my design for this... wwww
\r
64 FUCK well i am stuck.... wwww
\r
66 map->data[q] = (byte)atoi(js+t->start);
\r
67 #ifdef DEBUG_MAPDATA
\r
68 fprintf(stdout, "%d[%d]", q, map->data[q]);
\r
72 if(strstr(js_sv, "height"))
\r
74 map->height = atoi(js+t->start);
\r
76 fprintf(stdout, "indent= [%d] ", indent);
\r
77 fprintf(stdout, "h:[%d]\n", map->height);
\r
79 }else if(strstr(js_sv, "width"))
\r
81 map->width = atoi(js+t->start);
\r
83 fprintf(stdout, "indent= [%d] ", indent);
\r
84 fprintf(stdout, "w:[%d]\n", map->width);
\r
88 /* We may use strndup() to fetch string value */
\r
89 } else if (t->type == JSMN_STRING) {
\r
90 if(jsoneq(js, t, "data") == 0)
\r
92 // fprintf(stdout, "[[[[%d|%d]]]]\n", &(t+1)->size, (t+1)->size);
\r
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);
\r
94 map->data = malloc(sizeof(byte) * (t+1)->size);
\r
95 map->tiles = malloc(sizeof(tiles_t));
\r
96 map->tiles->btdata = malloc(sizeof(bitmap_t));
\r
97 //fix this to be far~
\r
98 //0000 bp = bitmapLoadPcx("data/ed.pcx");
\r
99 // bp = bitmapLoadPcx("data/koishi^^.pcx");
\r
100 map->tiles->btdata = &bp;
\r
101 //---- map->tiles->data = planar_buf_from_bitmap(&bp);
\r
102 //map->tiles->data->data = malloc((16/**2*/)*16);
\r
103 //map->tiles->data->width = (16/**2*/);
\r
104 //map->tiles->data->height= 16;
\r
105 map->tiles->tileHeight = 16;
\r
106 map->tiles->tileWidth = 16;
\r
107 map->tiles->rows = 1;
\r
108 map->tiles->cols = 1;
\r
109 map->tiles->debug_text=false;
\r
110 strcpy(js_sv, "data");//strdup(js+t->start);//, t->end - t->start);
\r
113 if (jsoneq(js, t, "height") == 0 && indent<=1)
\r
115 strcpy(js_sv, "height");//strdup(js+t->start);//, t->end - t->start);
\r
117 if(jsoneq(js, t, "width") == 0 && indent<=1)
\r
119 strcpy(js_sv, "width");//strdup(js+t->start);//, t->end - t->start);
\r
120 }else strcpy(js_sv, "\0");
\r
122 } else if (t->type == JSMN_OBJECT) {
\r
123 //fprintf(stdout, "\n");
\r
125 for (i = 0; i < t->size; i++) {
\r
126 //for (k = 0; k < indent; k++) fprintf(stdout, "\t");
\r
127 j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
\r
128 //fprintf(stdout, ": ");
\r
129 j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
\r
130 //fprintf(stdout, "\n");
\r
133 } else if (t->type == JSMN_ARRAY) {
\r
135 //fprintf(stdout, "==\n");
\r
136 for (i = 0; i < t->size; i++) {
\r
137 //for (k = 0; k < indent-1; k++) fprintf(stdout, "\t");
\r
138 //fprintf(stdout, "\t-");
\r
139 j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
\r
140 //fprintf(stdout, "==\n");
\r
147 int loadmap(char *mn, map_t *map)
\r
150 static word incr=0;
\r
151 int eof_expected = 0;
\r
155 static char js_ss[16];
\r
159 size_t tokcount = 2;
\r
161 FILE *fh = fopen(mn, "r");
\r
163 /* Prepare parser */
\r
166 /* Allocate some tokens as a start */
\r
167 //0000fprintf(stderr, "tok malloc\n");
\r
168 tok = malloc(sizeof(*tok) * tokcount);
\r
170 fprintf(stderr, "malloc(): errno=%d\n", errno);
\r
175 /* Read another chunk */
\r
176 //0000fprintf(stderr, "read\n");
\r
177 r = fread(buf, 1, sizeof(buf), fh);
\r
179 fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
\r
183 if (eof_expected != 0) {
\r
186 fprintf(stderr, "fread(): unexpected EOF\n");
\r
190 //0000fprintf(stdout, "r= [%d] BUFSIZ=%d\n", r, BUFSIZ);
\r
191 //0000fprintf(stderr, "js alloc~\n");
\r
192 js = realloc(js, jslen + r + 1);
\r
194 fprintf(stderr, "*js=%Fp\n", *js);
\r
195 fprintf(stderr, "realloc(): errno = %d\n", errno);
\r
198 strncpy(js + jslen, buf, r);
\r
202 //0000fprintf(stdout, " parse~ tok=%zu jslen=%zu r=%d _memavl()=%u BUFSIZ=%d~\n", tokcount, jslen, r, _memavl(), BUFSIZ);
\r
203 //0000fprintf(stdout, "p=[%u] [%u] [%d]\n", p.pos, p.toknext, p.toksuper);
\r
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
\r
207 r = jsmn_parse(&p, js, jslen, tok, tokcount);
\r
208 //0000fprintf(stdout, "r= [%d]\n", r);
\r
210 if (r == JSMN_ERROR_NOMEM) {
\r
211 tokcount = tokcount * 2;
\r
212 //0000fprintf(stderr, "tok realloc~ %zu\n", tokcount);
\r
213 tok = realloc(tok, sizeof(*tok) * tokcount);
\r
215 fprintf(stderr, "realloc(): errno=%d\n", errno);
\r
221 //printf("js=%Fp\n", (js));
\r
222 //printf("*js=%Fp\n", (*(js)));
\r
223 //printf("&*js=%s\n", &(*(js)));
\r
224 //printf("&buf=[%Fp]\n", &buf);
\r
225 //printf("&buf_seg=[%x]\n", FP_SEG(&buf));
\r
226 //printf("&buf_off=[%x]\n", FP_OFF(&buf));
\r
227 //printf("&buf_fp=[%Fp]\n", MK_FP(FP_SEG(&buf), FP_OFF(&buf)));
\r
228 //printf("buf=[\n%s\n]\n", buf);
\r
229 //printf("buff=[%Fp]\n", buff);
\r
230 //printf("(*buff)=[%Fp]\n", (*buff));
\r
231 //printf("&(*buff)=[\n%s\n]\n", &(*buff));
\r
232 #ifdef DEBUG_DUMPVARS
\r
233 fprintf(stdout, "running dump~\n");
\r
235 dump(js, tok, p.toknext, incr, &js_ss, map, 0);
\r