1 /* Project 16 Source Code~
2 * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover
4 * This file is part of Project 16.
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.
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.
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.
23 #include "src/lib/mapread.h"
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) {
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) {
41 fprintf(stdout, "%s\n", js);
42 fprintf(stdout, "\n");
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);
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"))
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
64 FUCK well i am stuck.... wwww
66 map->data[q] = (byte)atoi(js+t->start);
68 fprintf(stdout, "%d[%d]", q, map->data[q]);
72 if(strstr(js_sv, "height"))
74 map->height = atoi(js+t->start);
76 fprintf(stdout, "indent= [%d] ", indent);
77 fprintf(stdout, "h:[%d]\n", map->height);
79 }else if(strstr(js_sv, "width"))
81 map->width = atoi(js+t->start);
83 fprintf(stdout, "indent= [%d] ", indent);
84 fprintf(stdout, "w:[%d]\n", map->width);
88 /* We may use strndup() to fetch string value */
89 } else if (t->type == JSMN_STRING) {
90 if(jsoneq(js, t, "data") == 0)
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));
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);
113 if (jsoneq(js, t, "height") == 0 && indent<=1)
115 strcpy(js_sv, "height");//strdup(js+t->start);//, t->end - t->start);
117 if(jsoneq(js, t, "width") == 0 && indent<=1)
119 strcpy(js_sv, "width");//strdup(js+t->start);//, t->end - t->start);
120 }else strcpy(js_sv, "\0");
122 } else if (t->type == JSMN_OBJECT) {
123 //fprintf(stdout, "\n");
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");
133 } else if (t->type == JSMN_ARRAY) {
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");
147 int loadmap(char *mn, map_t *map)
151 int eof_expected = 0;
155 static char js_ss[16];
161 FILE *fh = fopen(mn, "r");
166 /* Allocate some tokens as a start */
167 //0000fprintf(stderr, "tok malloc\n");
168 tok = malloc(sizeof(*tok) * tokcount);
170 fprintf(stderr, "malloc(): errno=%d\n", errno);
175 /* Read another chunk */
176 //0000fprintf(stderr, "read\n");
177 r = fread(buf, 1, sizeof(buf), fh);
179 fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
183 if (eof_expected != 0) {
186 fprintf(stderr, "fread(): unexpected EOF\n");
190 //0000fprintf(stdout, "r= [%d] BUFSIZ=%d\n", r, BUFSIZ);
191 //0000fprintf(stderr, "js alloc~\n");
192 js = realloc(js, jslen + r + 1);
194 fprintf(stderr, "*js=%Fp\n", *js);
195 fprintf(stderr, "realloc(): errno = %d\n", errno);
198 strncpy(js + jslen, buf, r);
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);
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
207 r = jsmn_parse(&p, js, jslen, tok, tokcount);
208 //0000fprintf(stdout, "r= [%d]\n", r);
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);
215 fprintf(stderr, "realloc(): errno=%d\n", errno);
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");
235 dump(js, tok, p.toknext, incr, &js_ss, map, 0);