summaryrefslogtreecommitdiff
path: root/tools/nitrogfx/json.c
blob: 53f83ceb0f8ef5d1ff44e2100a50f77fc7db5320 (plain)
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
// Copyright (c) 2021 red031000

#include "global.h"
#include "cJSON.h"
#include "json.h"
#include "util.h"
#include <string.h>
#include <stdlib.h>

static inline bool GetBool(cJSON * in)
{
    if (!cJSON_IsBool(in))
        return false;

    return cJSON_IsTrue(in);
}

static inline int GetInt(cJSON * in)
{
    if (!cJSON_IsNumber(in))
        return 0;

    return in->valueint;
}

static inline char *GetString(cJSON * in)
{
    if (!cJSON_IsString(in))
        return NULL;

    return in->valuestring;
}

struct JsonToCellOptions *ParseNCERJson(char *path)
{
    int fileLength;
    unsigned char *jsonString = ReadWholeFile(path, &fileLength);

    cJSON *json = cJSON_Parse((const char *)jsonString);

    struct JsonToCellOptions *options = malloc(sizeof(struct JsonToCellOptions));

    if (json == NULL)
    {
        const char *errorPtr = cJSON_GetErrorPtr();
        FATAL_ERROR("Error in line \"%s\"\n", errorPtr);
    }

    cJSON *labelBool = cJSON_GetObjectItemCaseSensitive(json, "label");
    cJSON *extended = cJSON_GetObjectItemCaseSensitive(json, "extended");
    cJSON *imageHeight = cJSON_GetObjectItemCaseSensitive(json, "imageHeight");
    cJSON *imageWidth = cJSON_GetObjectItemCaseSensitive(json, "imageWidth");
    cJSON *cellCount = cJSON_GetObjectItemCaseSensitive(json, "cellCount");

    options->label = GetBool(labelBool);
    options->extended = GetBool(extended);
    options->imageHeight = GetInt(imageHeight);
    options->imageWidth = GetInt(imageWidth);
    options->cellCount = GetInt(cellCount);

    options->cells = malloc(sizeof(struct Cell *) * options->cellCount);

    for (int i = 0; i < options->cellCount; i++)
    {
        options->cells[i] = malloc(sizeof(struct Cell));
    }

    int i = 0;
    cJSON *cells = cJSON_GetObjectItemCaseSensitive(json, "cells");
    cJSON *cell = NULL;

    cJSON_ArrayForEach(cell, cells)
    {
        if (i > options->cellCount - 1)
            FATAL_ERROR("Cell count is incorrect.\n");

        cJSON *readOnly = cJSON_GetObjectItemCaseSensitive(cell, "readOnly");

        options->cells[i]->readOnly = (short)GetInt(readOnly);
        if (options->extended)
        {
            cJSON *maxX = cJSON_GetObjectItemCaseSensitive(cell, "maxX");
            cJSON *maxY = cJSON_GetObjectItemCaseSensitive(cell, "maxY");
            cJSON *minX = cJSON_GetObjectItemCaseSensitive(cell, "minX");
            cJSON *minY = cJSON_GetObjectItemCaseSensitive(cell, "minY");

            options->cells[i]->maxX = (short)GetInt(maxX);
            options->cells[i]->maxY = (short)GetInt(maxY);
            options->cells[i]->minX = (short)GetInt(minX);
            options->cells[i]->minY = (short)GetInt(minY);
        }

        if (options->label)
        {
            cJSON *label = cJSON_GetObjectItemCaseSensitive(cell, "label");
            char *labelString = GetString(label);
            options->cells[i]->label = malloc(strlen(labelString) + 1);
            strcpy(options->cells[i]->label, labelString);
        }
        //OAM data
        cJSON *OAM = cJSON_GetObjectItemCaseSensitive(cell, "OAM");

        //Attr0
        cJSON *Attr0 = cJSON_GetObjectItemCaseSensitive(OAM, "Attr0");

        cJSON *YCoordinate = cJSON_GetObjectItemCaseSensitive(Attr0, "YCoordinate");
        cJSON *Rotation = cJSON_GetObjectItemCaseSensitive(Attr0, "Rotation");
        cJSON *SizeDisable = cJSON_GetObjectItemCaseSensitive(Attr0, "SizeDisable");
        cJSON *Mode = cJSON_GetObjectItemCaseSensitive(Attr0, "Mode");
        cJSON *Mosaic = cJSON_GetObjectItemCaseSensitive(Attr0, "Mosaic");
        cJSON *Colours = cJSON_GetObjectItemCaseSensitive(Attr0, "Colours");
        cJSON *Shape = cJSON_GetObjectItemCaseSensitive(Attr0, "Shape");

        options->cells[i]->oam.attr0.YCoordinate = GetInt(YCoordinate);
        options->cells[i]->oam.attr0.Rotation = GetBool(Rotation);
        options->cells[i]->oam.attr0.SizeDisable = GetBool(SizeDisable);
        options->cells[i]->oam.attr0.Mode = GetInt(Mode);
        options->cells[i]->oam.attr0.Mosaic = GetBool(Mosaic);
        options->cells[i]->oam.attr0.Colours = GetInt(Colours);
        options->cells[i]->oam.attr0.Shape = GetInt(Shape);

        //Attr1
        cJSON *Attr1 = cJSON_GetObjectItemCaseSensitive(OAM, "Attr1");

        cJSON *XCoordinate = cJSON_GetObjectItemCaseSensitive(Attr1, "XCoordinate");
        cJSON *RotationScaling = cJSON_GetObjectItemCaseSensitive(Attr1, "RotationScaling");
        cJSON *Size = cJSON_GetObjectItemCaseSensitive(Attr1, "Size");

        options->cells[i]->oam.attr1.XCoordinate = GetInt(XCoordinate);
        options->cells[i]->oam.attr1.RotationScaling = GetInt(RotationScaling);
        options->cells[i]->oam.attr1.Size = GetInt(Size);

        //Attr2
        cJSON *Attr2 = cJSON_GetObjectItemCaseSensitive(OAM, "Attr2");

        cJSON *CharName = cJSON_GetObjectItemCaseSensitive(Attr2, "CharName");
        cJSON *Priority = cJSON_GetObjectItemCaseSensitive(Attr2, "Priority");
        cJSON *Palette = cJSON_GetObjectItemCaseSensitive(Attr2, "Palette");

        options->cells[i]->oam.attr2.CharName = GetInt(CharName);
        options->cells[i]->oam.attr2.Priority = GetInt(Priority);
        options->cells[i]->oam.attr2.Palette = GetInt(Palette);

        i++;
    }

    cJSON_Delete(json);
    free(jsonString);
    return options;
}

struct JsonToScreenOptions *ParseNSCRJson(char *path)
{
    int fileLength;
    unsigned char *jsonString = ReadWholeFile(path, &fileLength);

    cJSON *json = cJSON_Parse((const char *)jsonString);

    struct JsonToScreenOptions *options = malloc(sizeof(struct JsonToScreenOptions));

    if (json == NULL)
    {
        const char *errorPtr = cJSON_GetErrorPtr();
        FATAL_ERROR("Error in line \"%s\"\n", errorPtr);
    }

    cJSON *Height = cJSON_GetObjectItemCaseSensitive(json, "height");
    cJSON *Width = cJSON_GetObjectItemCaseSensitive(json, "width");

    options->height = GetInt(Height);
    options->width = GetInt(Width);

    options->data = malloc(sizeof(unsigned short) * options->height * options->width);

    cJSON *layer = NULL;
    cJSON *layers = cJSON_GetObjectItemCaseSensitive(json, "layers");
    int palette = 0;
    cJSON *tilesets = cJSON_GetObjectItemCaseSensitive(json, "tilesets");
    int tilesetSize = 0;
    if (cJSON_GetArraySize(tilesets) != 1)
    {
        cJSON *tileset = cJSON_GetArrayItem(tilesets, 1);
        cJSON *firstGid = cJSON_GetObjectItemCaseSensitive(tileset, "firstgid");
        tilesetSize = GetInt(firstGid) - 1;
        if (tilesetSize <= 1)
            FATAL_ERROR("Wrong tileset index (tileset 0 should be added first)\n");
    }

    cJSON_ArrayForEach(layer, layers)
    {
        cJSON *tile = NULL;
        cJSON *data = cJSON_GetObjectItemCaseSensitive(layer, "data");
        int i = 0;
        cJSON_ArrayForEach(tile, data)
        {
            int tileInt = GetInt(tile) - 1;
            if (tileInt != -1)
            {
                if (tilesetSize != 0)
                {
                    palette = tileInt / tilesetSize;
                    tileInt %= tilesetSize;
                }
                bool vFlip = tileInt >> 30;
                bool hFlip = tileInt >> 31;
                tileInt |= vFlip << 11;
                tileInt |= hFlip << 10;
                tileInt |= palette << 12;
                options->data[i] = (short) (tileInt & 0xFFFF);
            }
            i++;
        }
    }

    cJSON_Delete(json);
    free(jsonString);
    return options;
}

void FreeNCERCell(struct JsonToCellOptions *options)
{
    for (int i = 0; i < options->cellCount; i++)
    {
        if (options->label)
        {
            free(options->cells[i]->label);
        }
        free(options->cells[i]);
    }
    free(options);
}

void FreeNSCRScreen(struct JsonToScreenOptions *options)
{
    free(options->data);
    free(options);
}