Project

General

Profile

Download (35.4 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / json / JsonReader.java @ 1d581993

1 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted 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 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted 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 Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.objectlib.json;
21
22
import java.io.BufferedReader;
23 bf52deb2 Leszek Koltunski
import java.io.File;
24
import java.io.FileInputStream;
25
import java.io.FileNotFoundException;
26 82eb152a Leszek Koltunski
import java.io.IOException;
27
import java.io.InputStream;
28
import java.io.InputStreamReader;
29
import java.nio.charset.StandardCharsets;
30
31 bf52deb2 Leszek Koltunski
import android.content.Context;
32
33 3ee1d662 Leszek Koltunski
import org.distorted.objectlib.helpers.ObjectFaceShape;
34 1d581993 Leszek Koltunski
import org.distorted.objectlib.helpers.ObjectSignature;
35 82eb152a Leszek Koltunski
import org.json.JSONArray;
36
import org.json.JSONException;
37
import org.json.JSONObject;
38
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41
42
import org.distorted.objectlib.helpers.ObjectShape;
43
import org.distorted.objectlib.helpers.ObjectSticker;
44 10b7e306 Leszek Koltunski
import org.distorted.objectlib.scrambling.ScrambleState;
45 82eb152a Leszek Koltunski
import org.distorted.objectlib.main.ObjectType;
46
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49
public class JsonReader
50
{
51
  private ScrambleState[] mStates;
52
  private int[][] mSolvedQuats;
53
  private Static4D[] mQuats;
54
  private int mSolvedFuncIndex;
55
  private int mNumStickerTypes;
56
  private float[][] mCuts;
57
  private boolean[][] mLayerRotatable;
58
  private int mMovementType, mMovementSplit;
59
  private int[][][] mEnabled;
60
  private float[] mDist3D;
61 d66e98d7 Leszek Koltunski
  private int mNumCubitFaces, mNumFaces, mNumFaceColors;
62 82eb152a Leszek Koltunski
  private float[][] mPositions;
63
  private ObjectShape[] mShapes;
64 3ee1d662 Leszek Koltunski
  private ObjectFaceShape[] mFaceShapes;
65 82eb152a Leszek Koltunski
  private Static4D[] mCubitQuats;
66
  private int mNumCubitVariants;
67
  private int[] mCubitVariant;
68
  private int[][] mVariantFaceColor, mCubitFaceColor;
69
  private ObjectSticker[] mObjectSticker;
70
  private Static3D[] mAxis;
71 beee90ab Leszek Koltunski
  private int[][] mBasicAngle;
72 e1a86bf2 Leszek Koltunski
  private String mLongName, mShortName, mInventor;
73 82eb152a Leszek Koltunski
  private int mYearOfInvention, mComplexity;
74
  private int[] mNumLayers;
75
  private float mSize;
76 e1a86bf2 Leszek Koltunski
  private int mScrambleType, mNumScrambles;
77 82eb152a Leszek Koltunski
  private int[] mColor;
78 253e440f Leszek Koltunski
  private int mInternalColor;
79 0f72365b Leszek Koltunski
  private boolean mResetMaps;
80 052e0362 Leszek Koltunski
  private String mTutorialObject;
81
  private String[][] mTutorials;
82 1d581993 Leszek Koltunski
  private ObjectSignature mSignature;
83 82eb152a Leszek Koltunski
84 a2d6c41a Leszek Koltunski
  private static JsonReader mThis;
85
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
88
  private JsonReader()
89
    {
90
    }
91
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
94
  public static JsonReader getInstance()
95
    {
96
    if( mThis==null ) mThis = new JsonReader();
97
    return mThis;
98
    }
99
100 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
101
102 1d581993 Leszek Koltunski
  private void parseMetadata1(JSONObject object) throws JSONException
103 82eb152a Leszek Koltunski
    {
104
    mLongName        = object.getString("longname");
105 e1a86bf2 Leszek Koltunski
    mShortName       = object.getString("shortname");
106 82eb152a Leszek Koltunski
    mInventor        = object.getString("inventor");
107
    mYearOfInvention = object.getInt("year");
108
    mComplexity      = object.getInt("complexity");
109
    mSize            = (float)object.getDouble("size");
110 e1a86bf2 Leszek Koltunski
    mNumScrambles    = object.getInt("scrambles");
111 0f72365b Leszek Koltunski
    mResetMaps       = object.getBoolean("resetmaps");
112 a72cd106 Leszek Koltunski
    mNumFaces        = object.getInt("num_faces");
113 e1a86bf2 Leszek Koltunski
114 5f54927b Leszek Koltunski
    try
115
      {
116 1d581993 Leszek Koltunski
      long signature = object.getLong("signature");
117
      mSignature = new ObjectSignature(signature);
118 5f54927b Leszek Koltunski
      }
119
    catch(JSONException ex)
120
      {
121
      // objects older than Feb 2022 do not have the 'signature' field. They all use the ObjectType.ordinal
122
      // as their signature.
123 1d581993 Leszek Koltunski
      long signature = ObjectType.getOrdinal(mShortName);
124
      mSignature = new ObjectSignature(signature);
125
      }
126
    }
127
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
130
  private void parseMetadata2(JSONObject object) throws JSONException
131
    {
132
    mLongName        = object.getString("longname");
133
    mShortName       = object.getString("shortname");
134
    mInventor        = object.getString("inventor");
135
    mYearOfInvention = object.getInt("year");
136
    mComplexity      = object.getInt("complexity");
137
    mSize            = (float)object.getDouble("size");
138
    mNumScrambles    = object.getInt("scrambles");
139
    mResetMaps       = object.getBoolean("resetmaps");
140
    mNumFaces        = object.getInt("num_faces");
141
142
    try
143
      {
144
      long signature1 = object.getLong("signature1");
145
      long signature2 = object.getLong("signature2");
146
      long signature3 = object.getLong("signature3");
147
      mSignature = new ObjectSignature(signature1,signature2,signature3);
148
      }
149
    catch(JSONException ex)
150
      {
151
      long signature = object.getLong("signature");
152
      mSignature = new ObjectSignature(signature);
153 5f54927b Leszek Koltunski
      }
154 82eb152a Leszek Koltunski
    }
155
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
158
  private void parseCubits(JSONArray object) throws JSONException
159
    {
160
    int numCubits = object.length();
161
162
    mCubitQuats     = new Static4D[numCubits];
163
    mCubitVariant   = new int[numCubits];
164
    mPositions      = new float[numCubits][];
165
    mCubitFaceColor = new int[numCubits][];
166
167
    for(int i=0; i<numCubits; i++)
168
      {
169
      JSONObject jsonCubit = object.getJSONObject(i);
170
171
      float qx = (float)jsonCubit.getDouble("qx");
172
      float qy = (float)jsonCubit.getDouble("qy");
173
      float qz = (float)jsonCubit.getDouble("qz");
174
      float qw = (float)jsonCubit.getDouble("qw");
175
176
      mCubitQuats[i] = new Static4D(qx,qy,qz,qw);
177
      mCubitVariant[i] = jsonCubit.getInt("variant");
178
179
      JSONArray jsonCenter = jsonCubit.getJSONArray("centers");
180
      int numCenter = jsonCenter.length();
181
      mPositions[i] = new float[numCenter];
182
      for(int j=0; j<numCenter; j++) mPositions[i][j] = (float)jsonCenter.getDouble(j);
183
184
      JSONArray jsonColor  = jsonCubit.getJSONArray("colors");
185
      int numColor = jsonColor.length();
186
      mCubitFaceColor[i] = new int[numColor];
187
      for(int j=0; j<numColor; j++) mCubitFaceColor[i][j] = jsonColor.getInt(j);
188
      }
189
    }
190
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
193
  private void parseShapes(JSONArray object) throws JSONException
194
    {
195
    mNumCubitVariants = object.length();
196
    mVariantFaceColor = new int[mNumCubitVariants][];
197 3ee1d662 Leszek Koltunski
    mShapes     = new ObjectShape[mNumCubitVariants];
198
    mFaceShapes = new ObjectFaceShape[mNumCubitVariants];
199 57ef6378 Leszek Koltunski
    float[][][] verts     = new float[mNumCubitVariants][][];
200
    float[][][] bands     = new float[mNumCubitVariants][][];
201
    float[][][] corners   = new float[mNumCubitVariants][][];
202
    float[][][] centers   = new float[mNumCubitVariants][][];
203
    float[][] convexity   = new float[mNumCubitVariants][];
204 82eb152a Leszek Koltunski
    int[][] cornerIndices = new int[mNumCubitVariants][];
205
    int[][] centerIndices = new int[mNumCubitVariants][];
206 57ef6378 Leszek Koltunski
    int[][] bandIndices   = new int[mNumCubitVariants][];
207 82eb152a Leszek Koltunski
    int[][][] vertIndices = new int[mNumCubitVariants][][];
208
209 d66e98d7 Leszek Koltunski
    mNumCubitFaces = -1;
210
211 82eb152a Leszek Koltunski
    for(int i=0; i<mNumCubitVariants; i++)
212
      {
213
      JSONObject jsonShape = object.getJSONObject(i);
214
215
      ////// vertices /////////////////////////////////////////////////
216
      JSONArray jsonVertices= jsonShape.getJSONArray("vertices");
217
      int numVertices = jsonVertices.length();
218 57ef6378 Leszek Koltunski
      verts[i] = new float[numVertices][3];
219 82eb152a Leszek Koltunski
      cornerIndices[i] = new int[numVertices];
220
      centerIndices[i] = new int[numVertices];
221
222
      for(int j=0; j<numVertices; j++)
223
        {
224
        JSONObject vert = jsonVertices.getJSONObject(j);
225 57ef6378 Leszek Koltunski
        verts[i][j][0] = (float)vert.getDouble("x");
226
        verts[i][j][1] = (float)vert.getDouble("y");
227
        verts[i][j][2] = (float)vert.getDouble("z");
228 82eb152a Leszek Koltunski
        cornerIndices[i][j] = vert.getInt("cornerIndex");
229
        centerIndices[i][j] = vert.getInt("centerIndex");
230
        }
231
232
      ////// faces ////////////////////////////////////////////////////
233
      JSONArray jsonFaces= jsonShape.getJSONArray("faces");
234
      int numFaces = jsonFaces.length();
235
      mVariantFaceColor[i] = new int[numFaces];
236
      bandIndices[i] = new int[numFaces];
237
      vertIndices[i] = new int[numFaces][];
238
239 d66e98d7 Leszek Koltunski
      if( mNumCubitFaces<numFaces ) mNumCubitFaces=numFaces;
240
241 82eb152a Leszek Koltunski
      for(int j=0; j<numFaces; j++)
242
        {
243
        JSONObject jsonFace = jsonFaces.getJSONObject(j);
244
        mVariantFaceColor[i][j] = jsonFace.getInt("sticker");
245
        bandIndices[i][j] = jsonFace.getInt("bandIndex");
246
        JSONArray vertices = jsonFace.getJSONArray("vertexIndices");
247
        int numV = vertices.length();
248
        vertIndices[i][j] = new int[numV];
249
        for(int k=0; k<numV; k++) vertIndices[i][j][k] = vertices.getInt(k);
250
        }
251
252
      ////// bands ////////////////////////////////////////////////////
253
      JSONArray jsonBands= jsonShape.getJSONArray("bands");
254
      int numBands = jsonBands.length();
255
      bands[i] = new float[numBands][7];
256
257
      for(int j=0; j<numBands; j++)
258
        {
259
        JSONObject jsonBand = jsonBands.getJSONObject(j);
260
261
        bands[i][j][0] = (float)jsonBand.getDouble("height");
262
        bands[i][j][1] = (float)jsonBand.getDouble("angle");
263
        bands[i][j][2] = (float)jsonBand.getDouble("distanceToCenter");
264
        bands[i][j][3] = (float)jsonBand.getDouble("distanceToFlat");
265
        bands[i][j][4] = (float)jsonBand.getDouble("numOfBands");
266
        bands[i][j][5] = (float)jsonBand.getDouble("extraI");
267
        bands[i][j][6] = (float)jsonBand.getDouble("extraJ");
268
        }
269
270
      ////// cornerPush ///////////////////////////////////////////////
271 9e8eb9e4 Leszek Koltunski
      JSONArray jsonCornerPush= jsonShape.optJSONArray("cornerPush");
272 82eb152a Leszek Koltunski
273 9e8eb9e4 Leszek Koltunski
      if( jsonCornerPush!=null )
274 82eb152a Leszek Koltunski
        {
275 9e8eb9e4 Leszek Koltunski
        int numCornerP = jsonCornerPush.length();
276
        corners[i] = new float[numCornerP][2];
277
278
        for(int j=0; j<numCornerP; j++)
279
          {
280
          JSONObject jsonC = jsonCornerPush.getJSONObject(j);
281
          corners[i][j][0] = (float)jsonC.getDouble("strength");
282
          corners[i][j][1] = (float)jsonC.getDouble("radius");
283
          }
284 82eb152a Leszek Koltunski
        }
285
286
      ////// centerPush ///////////////////////////////////////////////
287 9e8eb9e4 Leszek Koltunski
      JSONArray jsonCenterPush= jsonShape.optJSONArray("centerPush");
288 82eb152a Leszek Koltunski
289 9e8eb9e4 Leszek Koltunski
      if( jsonCenterPush!=null )
290 82eb152a Leszek Koltunski
        {
291 9e8eb9e4 Leszek Koltunski
        int numCenterP = jsonCenterPush.length();
292
        centers[i] = new float[numCenterP][3];
293
294
        for(int j=0; j<numCenterP; j++)
295
          {
296
          JSONObject jsonC = jsonCenterPush.getJSONObject(j);
297
          centers[i][j][0] = (float)jsonC.getDouble("x");
298
          centers[i][j][1] = (float)jsonC.getDouble("y");
299
          centers[i][j][2] = (float)jsonC.getDouble("z");
300
          }
301 82eb152a Leszek Koltunski
        }
302
303
      ////// convexity ///////////////////////////////////////////////
304 9e8eb9e4 Leszek Koltunski
      JSONObject jsonConvexity = jsonShape.optJSONObject("convexity");
305
306
      if( jsonConvexity!=null )
307 82eb152a Leszek Koltunski
        {
308
        convexity[i] = new float[3];
309
        convexity[i][0] = (float)jsonConvexity.getDouble("x");
310
        convexity[i][1] = (float)jsonConvexity.getDouble("y");
311
        convexity[i][2] = (float)jsonConvexity.getDouble("z");
312
        }
313
      }
314
315
    for(int i=0; i<mNumCubitVariants; i++)
316
      {
317 59a971c1 Leszek Koltunski
      mShapes[i] = new ObjectShape(verts[i],vertIndices[i]);
318 3ee1d662 Leszek Koltunski
      mFaceShapes[i] = new ObjectFaceShape(bands[i],bandIndices[i],corners[i],cornerIndices[i],centers[i],centerIndices[i],convexity[i] );
319 82eb152a Leszek Koltunski
      }
320
    }
321
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323
324
  private void parseStickers(JSONArray object) throws JSONException
325
    {
326
    mNumStickerTypes = object.length();
327
    mObjectSticker = new ObjectSticker[mNumStickerTypes];
328
329
    for(int i=0; i<mNumStickerTypes; i++)
330
      {
331
      JSONObject sticker = object.getJSONObject(i);
332
      float stroke = (float)sticker.getDouble("stroke");
333
      JSONArray vertices = sticker.getJSONArray("vertices");
334
      int numVertices = vertices.length();
335
336
      float[] coords     = new float[2*numVertices];
337
      float[] curvatures = new float[numVertices];
338
      float[] radii      = new float[numVertices];
339
340
      for(int j=0; j<numVertices; j++)
341
        {
342
        JSONObject vertex = vertices.getJSONObject(j);
343
344
        coords[2*j  ] = (float)vertex.getDouble("x");
345
        coords[2*j+1] = (float)vertex.getDouble("y");
346
        curvatures[j] = (float)vertex.getDouble("angle");
347
        radii[j]      = (float)vertex.getDouble("radius");
348
        }
349
350
      mObjectSticker[i] = new ObjectSticker(coords,curvatures,radii,stroke);
351
      }
352
    }
353
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355
356
  private void parseMesh(JSONObject object) throws JSONException
357
    {
358
    JSONArray cubits   = object.getJSONArray("cubits");
359
    parseCubits(cubits);
360
    JSONArray shapes   = object.getJSONArray("shapes");
361
    parseShapes(shapes);
362
    JSONArray stickers = object.getJSONArray("stickers");
363
    parseStickers(stickers);
364
    }
365
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367
368 beee90ab Leszek Koltunski
  private void parseAxis1(JSONArray object) throws JSONException
369 82eb152a Leszek Koltunski
    {
370
    int numAxis = object.length();
371
372 beee90ab Leszek Koltunski
    mBasicAngle     = new int[numAxis][];
373 82eb152a Leszek Koltunski
    mAxis           = new Static3D[numAxis];
374
    mCuts           = new float[numAxis][];
375
    mLayerRotatable = new boolean[numAxis][];
376
    mNumLayers      = new int[numAxis];
377
378
    for(int i=0; i<numAxis; i++)
379
      {
380
      JSONObject jsonAx = object.getJSONObject(i);
381
382 beee90ab Leszek Koltunski
      float x = (float)jsonAx.getDouble("x");
383
      float y = (float)jsonAx.getDouble("y");
384
      float z = (float)jsonAx.getDouble("z");
385
386
      mAxis[i] = new Static3D(x,y,z);
387
388
      JSONArray jsonCuts = jsonAx.getJSONArray("cuts");
389
      int numCuts = jsonCuts.length();
390
      mCuts[i] = new float[numCuts];
391
      for(int j=0; j<numCuts; j++) mCuts[i][j] = (float)jsonCuts.getDouble(j);
392
393
      JSONArray jsonRota = jsonAx.getJSONArray("rotatable");
394
      int numRota = jsonRota.length();
395
      mLayerRotatable[i] = new boolean[numRota];
396
      for(int j=0; j<numRota; j++) mLayerRotatable[i][j] = jsonRota.getBoolean(j);
397
398
      mBasicAngle[i] = new int[numRota];
399
      int basicAngle = jsonAx.getInt("basicAngle");
400
      for(int j=0; j<numRota; j++) mBasicAngle[i][j] = basicAngle;
401
402
      mNumLayers[i] = numRota;
403
      }
404
    }
405
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407
408
  private void parseAxis2(JSONArray object) throws JSONException
409
    {
410
    int numAxis = object.length();
411
412
    mBasicAngle     = new int[numAxis][];
413
    mAxis           = new Static3D[numAxis];
414
    mCuts           = new float[numAxis][];
415
    mLayerRotatable = new boolean[numAxis][];
416
    mNumLayers      = new int[numAxis];
417
418
    for(int i=0; i<numAxis; i++)
419
      {
420
      JSONObject jsonAx = object.getJSONObject(i);
421 82eb152a Leszek Koltunski
422
      float x = (float)jsonAx.getDouble("x");
423
      float y = (float)jsonAx.getDouble("y");
424
      float z = (float)jsonAx.getDouble("z");
425
426
      mAxis[i] = new Static3D(x,y,z);
427
428 beee90ab Leszek Koltunski
      JSONArray jsonAngles = jsonAx.getJSONArray("basicAngles");
429
      int numAngles = jsonAngles.length();
430
      mBasicAngle[i] = new int[numAngles];
431
      for(int j=0; j<numAngles; j++) mBasicAngle[i][j] = jsonAngles.getInt(j);
432
433 82eb152a Leszek Koltunski
      JSONArray jsonCuts = jsonAx.getJSONArray("cuts");
434
      int numCuts = jsonCuts.length();
435
      mCuts[i] = new float[numCuts];
436
      for(int j=0; j<numCuts; j++) mCuts[i][j] = (float)jsonCuts.getDouble(j);
437
438
      JSONArray jsonRota = jsonAx.getJSONArray("rotatable");
439
      int numRota = jsonRota.length();
440
      mLayerRotatable[i] = new boolean[numRota];
441
      for(int j=0; j<numRota; j++) mLayerRotatable[i][j] = jsonRota.getBoolean(j);
442
443
      mNumLayers[i] = numRota;
444
      }
445
    }
446
447
///////////////////////////////////////////////////////////////////////////////////////////////////
448
449
  private void parseQuats(JSONArray object) throws JSONException
450
    {
451
    int numQuats = object.length();
452
    mQuats = new Static4D[numQuats];
453
454
    for(int i=0; i<numQuats; i++)
455
      {
456
      JSONObject jsonQuat = object.getJSONObject(i);
457
458
      float x = (float)jsonQuat.getDouble("x");
459
      float y = (float)jsonQuat.getDouble("y");
460
      float z = (float)jsonQuat.getDouble("z");
461
      float w = (float)jsonQuat.getDouble("w");
462
463
      mQuats[i] = new Static4D(x,y,z,w);
464
      }
465
    }
466
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468
469
  private void parseScrambling(JSONObject object) throws JSONException
470
    {
471
    mScrambleType = object.getInt("scrambleType");
472
473
    if( mScrambleType==0 )
474
      {
475
      JSONArray jsonStates = object.getJSONArray("scrambleStates");
476
      int numStates = jsonStates.length();
477
      mStates = new ScrambleState[numStates];
478
479
      for(int i=0; i<numStates; i++)
480
        {
481
        JSONArray jsonState = jsonStates.getJSONArray(i);
482
        int numAxis = jsonState.length();
483
        int[][] scramblingData = new int[numAxis][];
484
485
        for(int j=0; j<numAxis; j++)
486
          {
487
          JSONArray jsonData = jsonState.getJSONArray(j);
488
          int numData = jsonData.length();
489
          scramblingData[j] = new int[numData];
490
          for(int k=0; k<numData; k++) scramblingData[j][k] = jsonData.getInt(k);
491
          }
492
493
        mStates[i] = new ScrambleState(scramblingData);
494
        }
495
      }
496
    }
497
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499
500
  private void parseTouchcontrol(JSONObject object) throws JSONException
501
    {
502
    mMovementType = object.getInt("movementType");
503
    mMovementSplit= object.getInt("movementSplit");
504
505 82904e62 Leszek Koltunski
    try
506 82eb152a Leszek Koltunski
      {
507 82904e62 Leszek Koltunski
      JSONArray jsonEnabled = object.getJSONArray("enabledAxis");
508
      int numFace = jsonEnabled.length();
509 82eb152a Leszek Koltunski
510 82904e62 Leszek Koltunski
      mEnabled = new int[numFace][][];
511
512
      for(int i=0; i<numFace; i++)
513 82eb152a Leszek Koltunski
        {
514 82904e62 Leszek Koltunski
        JSONArray jsonSection = jsonEnabled.getJSONArray(i);
515
        int numSection = jsonSection.length();
516
        mEnabled[i] = new int[numSection][];
517
518
        for(int j=0; j<numSection; j++)
519
          {
520
          JSONArray jsonAx = jsonSection.getJSONArray(j);
521
          int numAxis = jsonAx.length();
522
          mEnabled[i][j] = new int[numAxis];
523
          for(int k=0; k<numAxis; k++) mEnabled[i][j][k] = jsonAx.getInt(k);
524
          }
525 82eb152a Leszek Koltunski
        }
526
      }
527 82904e62 Leszek Koltunski
    catch( JSONException ex )
528
      {
529
      // ignore, the object does not have to have 'enabledAxis' defined at all.
530
      }
531 82eb152a Leszek Koltunski
532
    try
533
      {
534
      JSONArray jsonDist = object.getJSONArray("dist3D");
535 a72cd106 Leszek Koltunski
      int num = jsonDist.length();
536
      mDist3D = new float[num];
537
      for(int j=0; j<num; j++) mDist3D[j] = (float)jsonDist.getDouble(j);
538 82eb152a Leszek Koltunski
      }
539
    catch( JSONException ex )
540
      {
541 a72cd106 Leszek Koltunski
      // ignore, the object does not have a 'dist3D' which is possible.
542 82eb152a Leszek Koltunski
      }
543
    }
544
545
///////////////////////////////////////////////////////////////////////////////////////////////////
546
547
  private void parseColors(JSONArray object) throws JSONException
548
    {
549 253e440f Leszek Koltunski
    mNumFaceColors = object.length()-1;
550
551 82eb152a Leszek Koltunski
    mColor = new int[mNumFaceColors];
552
    for(int i=0; i<mNumFaceColors; i++) mColor[i] = object.getInt(i);
553 253e440f Leszek Koltunski
554
    mInternalColor = object.getInt(mNumFaceColors);
555 82eb152a Leszek Koltunski
    }
556
557 3c48fab9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
558
559
  private void parseSolved(JSONObject solved) throws JSONException
560
    {
561
    mSolvedFuncIndex = solved.getInt("functionIndex");
562
563
    try
564
      {
565
      JSONArray groupArray= solved.getJSONArray("groups");
566
      int numGroups = groupArray.length();
567
      mSolvedQuats  = new int[numGroups][];
568
569
      for(int i=0; i<numGroups; i++)
570
        {
571
        JSONArray groupElements = groupArray.getJSONArray(i);
572
        int groupSize = groupElements.length();
573
        mSolvedQuats[i] = new int[groupSize];
574
        for(int j=0; j<groupSize; j++) mSolvedQuats[i][j] = groupElements.getInt(j);
575
        }
576
      }
577
    catch( JSONException ex )
578
      {
579
      // ignore, the object does not have to have an array of solved groups.
580
      }
581
    }
582
583 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
584
585
  private void parseVersion1(JSONObject object) throws JSONException
586
    {
587
    JSONObject metadata    = object.getJSONObject("metadata");
588 1d581993 Leszek Koltunski
    parseMetadata1(metadata);
589 82eb152a Leszek Koltunski
    JSONObject mesh        = object.getJSONObject("mesh");
590
    parseMesh(mesh);
591
    JSONArray axis         = object.getJSONArray("axis");
592 beee90ab Leszek Koltunski
    parseAxis1(axis);
593
    JSONArray quats        = object.getJSONArray("quats");
594
    parseQuats(quats);
595
    JSONObject scrambling  = object.getJSONObject("scrambling");
596
    parseScrambling(scrambling);
597
    JSONObject touchcontrol= object.getJSONObject("touchcontrol");
598
    parseTouchcontrol(touchcontrol);
599
    JSONArray colors       = object.getJSONArray("colors");
600
    parseColors(colors);
601
    JSONObject solved      = object.getJSONObject("solved");
602
    parseSolved(solved);
603
    }
604
605
///////////////////////////////////////////////////////////////////////////////////////////////////
606
607
  private void parseVersion2(JSONObject object) throws JSONException
608
    {
609
    JSONObject metadata    = object.getJSONObject("metadata");
610 1d581993 Leszek Koltunski
    parseMetadata2(metadata);
611 beee90ab Leszek Koltunski
    JSONObject mesh        = object.getJSONObject("mesh");
612
    parseMesh(mesh);
613
    JSONArray axis         = object.getJSONArray("axis");
614
    parseAxis2(axis);
615 82eb152a Leszek Koltunski
    JSONArray quats        = object.getJSONArray("quats");
616
    parseQuats(quats);
617
    JSONObject scrambling  = object.getJSONObject("scrambling");
618
    parseScrambling(scrambling);
619
    JSONObject touchcontrol= object.getJSONObject("touchcontrol");
620
    parseTouchcontrol(touchcontrol);
621
    JSONArray colors       = object.getJSONArray("colors");
622
    parseColors(colors);
623 3c48fab9 Leszek Koltunski
    JSONObject solved      = object.getJSONObject("solved");
624
    parseSolved(solved);
625 82eb152a Leszek Koltunski
    }
626
627 052e0362 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
628
629
  private void parseVersion1Tutorial(JSONObject object) throws JSONException
630
    {
631
    mTutorialObject = object.getString("object");
632
    JSONArray tuts= object.getJSONArray("tutorials");
633
634
    int len = tuts.length();
635
    mTutorials = new String[len][4];
636
637
    for(int i=0; i<len; i++)
638
      {
639
      JSONObject tut = tuts.getJSONObject(i);
640
      mTutorials[i][0] = tut.getString("language");
641
      mTutorials[i][1] = tut.getString("link");
642
      mTutorials[i][2] = tut.getString("title");
643
      mTutorials[i][3] = tut.getString("author");
644
      }
645
    }
646
647 b39f8e39 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
648
649 1d581993 Leszek Koltunski
  private void parseVersion1Metadata(JSONObject object) throws JSONException
650
    {
651
    JSONObject metadata = object.getJSONObject("metadata");
652
    parseMetadata1(metadata);
653
    }
654
655
///////////////////////////////////////////////////////////////////////////////////////////////////
656
657
  private void parseVersion2Metadata(JSONObject object) throws JSONException
658 b39f8e39 Leszek Koltunski
    {
659
    JSONObject metadata = object.getJSONObject("metadata");
660 1d581993 Leszek Koltunski
    parseMetadata2(metadata);
661 b39f8e39 Leszek Koltunski
    }
662
663 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
664
665
  public void parseJsonFile(InputStream jsonStream)
666
    {
667
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
668
669
    try
670
      {
671
      StringBuilder contents = new StringBuilder();
672
      String tmp;
673
      while( (tmp = br.readLine()) != null) contents.append(tmp);
674
      br.close();
675
      jsonStream.close();
676
677
      JSONObject object = new JSONObject(contents.toString());
678
      int major = object.getInt("major");
679
680 e7d3259a Leszek Koltunski
      if( major==1 )
681
        {
682
        parseVersion1(object);
683
        }
684
      else if( major==2 )
685
        {
686
        parseVersion2(object);
687
        }
688 82eb152a Leszek Koltunski
      else
689
        {
690
        android.util.Log.e("readJsonFile", "Unknown version "+major);
691
        }
692
      }
693
    catch(IOException e)
694
      {
695
      android.util.Log.e("readJsonFile", "Error reading JSON file: "+e.toString());
696
      }
697
    catch( JSONException e )
698
      {
699
      android.util.Log.e("parseJsonFile", "Error parsing JSON file: "+e.toString());
700
      }
701
    }
702
703 b39f8e39 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
704
705
  public void parseJsonFileMetadata(InputStream jsonStream)
706
    {
707
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
708
709
    try
710
      {
711
      StringBuilder contents = new StringBuilder();
712
      String tmp;
713
      while( (tmp = br.readLine()) != null) contents.append(tmp);
714
      br.close();
715
      jsonStream.close();
716
717
      JSONObject object = new JSONObject(contents.toString());
718
      int major = object.getInt("major");
719
720 1d581993 Leszek Koltunski
      if( major==1 )
721
        {
722
        parseVersion1Metadata(object);
723
        }
724
      else if( major==2 )
725 b39f8e39 Leszek Koltunski
        {
726 1d581993 Leszek Koltunski
        parseVersion2Metadata(object);
727 b39f8e39 Leszek Koltunski
        }
728
      else
729
        {
730
        android.util.Log.e("readJsonFileQuick", "Unknown version "+major);
731
        }
732
      }
733
    catch(IOException e)
734
      {
735
      android.util.Log.e("readJsonFileQuick", "Error reading JSON file: "+e.toString());
736
      }
737
    catch( JSONException e )
738
      {
739
      android.util.Log.e("parseJsonFileQuick", "Error parsing JSON file: "+e.toString());
740
      }
741
    }
742
743 bf52deb2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
744
745 ef021035 Leszek Koltunski
  public int readNumScrambles(Context context, String fileName)
746 bf52deb2 Leszek Koltunski
    {
747
    File file = new File(context.getFilesDir(), fileName);
748
    InputStream stream;
749
750
    try
751
      {
752
      stream = new FileInputStream(file);
753 ef021035 Leszek Koltunski
      BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
754
755
      StringBuilder contents = new StringBuilder();
756
      String tmp;
757
      while( (tmp = br.readLine()) != null) contents.append(tmp);
758
      br.close();
759
      stream.close();
760
761
      JSONObject object = new JSONObject(contents.toString());
762
      int major = object.getInt("major");
763
764 938fc9af Leszek Koltunski
      if( major==1 || major==2 )
765 ef021035 Leszek Koltunski
        {
766
        JSONObject metadata = object.getJSONObject("metadata");
767
        return metadata.getInt("scrambles");
768
        }
769
      else
770
        {
771
        android.util.Log.e("readNumScrambles", "Unknown version "+major);
772
        }
773 bf52deb2 Leszek Koltunski
      }
774
    catch(FileNotFoundException ex)
775
      {
776 ef021035 Leszek Koltunski
      android.util.Log.e("readNumScrambles", "file "+fileName+" not found: "+ex.getMessage());
777 bf52deb2 Leszek Koltunski
      }
778 ef021035 Leszek Koltunski
    catch(IOException e)
779
      {
780
      android.util.Log.e("readNumScrambles", "Error reading JSON file: "+e.toString());
781
      }
782
    catch( JSONException e )
783
      {
784
      android.util.Log.e("readNumScrambles", "Error parsing JSON file: "+e.toString());
785
      }
786
787
    return 0;
788 bf52deb2 Leszek Koltunski
    }
789
790 052e0362 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
791
792
  public void parseJsonTutorial(InputStream jsonStream)
793
    {
794
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
795
796
    try
797
      {
798
      StringBuilder contents = new StringBuilder();
799
      String tmp;
800
      while( (tmp = br.readLine()) != null) contents.append(tmp);
801
      br.close();
802
      jsonStream.close();
803
804
      JSONObject object = new JSONObject(contents.toString());
805
      int major = object.getInt("major");
806
807
      if( major==1 )
808
        {
809
        parseVersion1Tutorial(object);
810
        }
811
      else
812
        {
813
        android.util.Log.e("readJsonFile", "Unknown tutorial version "+major);
814
        }
815
      }
816
    catch(IOException e)
817
      {
818
      android.util.Log.e("readJsonFile", "Error reading JSON file: "+e.toString());
819
      }
820
    catch( JSONException e )
821
      {
822
      android.util.Log.e("parseJsonFile", "Error parsing JSON file: "+e.toString());
823
      }
824
    }
825
826 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
827
828
  public ScrambleState[] getScrambleStates()
829
    {
830
    return mStates;
831
    }
832
833
///////////////////////////////////////////////////////////////////////////////////////////////////
834
835 19595510 Leszek Koltunski
  public int[][] getSolvedQuats()
836 82eb152a Leszek Koltunski
    {
837 19595510 Leszek Koltunski
    return mSolvedQuats;
838 82eb152a Leszek Koltunski
    }
839
840
///////////////////////////////////////////////////////////////////////////////////////////////////
841
842
  public Static4D[] getQuats()
843
    {
844
    return mQuats;
845
    }
846
847
///////////////////////////////////////////////////////////////////////////////////////////////////
848
849
  public int getSolvedFunctionIndex()
850
    {
851
    return mSolvedFuncIndex;
852
    }
853
854
///////////////////////////////////////////////////////////////////////////////////////////////////
855
856
  public int getNumStickerTypes()
857
    {
858
    return mNumStickerTypes;
859
    }
860
861
///////////////////////////////////////////////////////////////////////////////////////////////////
862
863
  public float[][] getCuts()
864
    {
865
    return mCuts;
866
    }
867
868
///////////////////////////////////////////////////////////////////////////////////////////////////
869
870
  public boolean[][] getLayerRotatable()
871
    {
872
    return mLayerRotatable;
873
    }
874
875
///////////////////////////////////////////////////////////////////////////////////////////////////
876
877
  public int getMovementType()
878
    {
879
    return mMovementType;
880
    }
881
882
///////////////////////////////////////////////////////////////////////////////////////////////////
883
884
  public int getMovementSplit()
885
    {
886
    return mMovementSplit;
887
    }
888
889
///////////////////////////////////////////////////////////////////////////////////////////////////
890
891
  public int[][][] getEnabled()
892
    {
893
    return mEnabled;
894
    }
895
896
///////////////////////////////////////////////////////////////////////////////////////////////////
897
898
  public float[] getDist3D()
899
    {
900
    return mDist3D;
901
    }
902
903 d66e98d7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
904
905
  public int getNumCubitFaces()
906
    {
907
    return mNumCubitFaces;
908
    }
909
910 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
911
912
  public float[][] getCubitPositions()
913
    {
914
    return mPositions;
915
    }
916
917
///////////////////////////////////////////////////////////////////////////////////////////////////
918
919
  public ObjectShape getObjectShape(int variant)
920
    {
921
    return mShapes[variant];
922
    }
923
924 3ee1d662 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
925
926
  public ObjectFaceShape getObjectFaceShape(int variant)
927
    {
928
    return mFaceShapes[variant];
929
    }
930
931 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
932
933 d0e6cf7f Leszek Koltunski
  public Static4D getCubitQuats(int cubit)
934 82eb152a Leszek Koltunski
    {
935
    return mCubitQuats[cubit];
936
    }
937
938
///////////////////////////////////////////////////////////////////////////////////////////////////
939
940
  public int getNumCubitVariants()
941
    {
942
    return mNumCubitVariants;
943
    }
944
945
///////////////////////////////////////////////////////////////////////////////////////////////////
946
947
  public int getCubitVariant(int cubit)
948
    {
949
    return mCubitVariant[cubit];
950
    }
951
952
///////////////////////////////////////////////////////////////////////////////////////////////////
953
954
  public int getVariantFaceColor(int variant, int face)
955
    {
956
    int[] colors = mVariantFaceColor[variant];
957
    return colors.length>face ? colors[face] : -1;
958
    }
959
960
///////////////////////////////////////////////////////////////////////////////////////////////////
961
962 ed0988c0 Leszek Koltunski
  public int getCubitFaceFace(int cubit, int face)
963 82eb152a Leszek Koltunski
    {
964
    return mCubitFaceColor[cubit][face];
965
    }
966
967 e1a86bf2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
968
969
  public int getNumScrambles()
970
    {
971
    return mNumScrambles;
972
    }
973
974 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
975
976
  public ObjectSticker retSticker(int sticker)
977
    {
978
    return mObjectSticker[sticker];
979
    }
980
981
///////////////////////////////////////////////////////////////////////////////////////////////////
982
983
  public Static3D[] getRotationAxis()
984
    {
985
    return mAxis;
986
    }
987
988
///////////////////////////////////////////////////////////////////////////////////////////////////
989
990 beee90ab Leszek Koltunski
  public int[][] getBasicAngle()
991 82eb152a Leszek Koltunski
    {
992
    return mBasicAngle;
993
    }
994
995
///////////////////////////////////////////////////////////////////////////////////////////////////
996
997 1d581993 Leszek Koltunski
  public ObjectSignature getSignature()
998 82eb152a Leszek Koltunski
    {
999 5f54927b Leszek Koltunski
    return mSignature;
1000 82eb152a Leszek Koltunski
    }
1001
1002
///////////////////////////////////////////////////////////////////////////////////////////////////
1003
1004
  public String getObjectName()
1005
    {
1006
    return mLongName;
1007
    }
1008
1009 e1a86bf2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1010
1011
  public String getShortName()
1012
    {
1013
    return mShortName;
1014
    }
1015
1016 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1017
1018
  public String getInventor()
1019
    {
1020
    return mInventor;
1021
    }
1022
1023
///////////////////////////////////////////////////////////////////////////////////////////////////
1024
1025
  public int getYearOfInvention()
1026
    {
1027
    return mYearOfInvention;
1028
    }
1029
1030
///////////////////////////////////////////////////////////////////////////////////////////////////
1031
1032
  public int getComplexity()
1033
    {
1034
    return mComplexity;
1035
    }
1036
1037
///////////////////////////////////////////////////////////////////////////////////////////////////
1038
1039
  public int getNumFaces()
1040
    {
1041
    return mNumFaces;
1042
    }
1043
1044
///////////////////////////////////////////////////////////////////////////////////////////////////
1045
1046
  public int getNumFaceColors()
1047
    {
1048
    return mNumFaceColors;
1049
    }
1050
1051
///////////////////////////////////////////////////////////////////////////////////////////////////
1052
1053
  public int[] getNumLayers()
1054
    {
1055
    return mNumLayers;
1056
    }
1057
1058
///////////////////////////////////////////////////////////////////////////////////////////////////
1059
1060
  public float getSize()
1061
    {
1062
    return mSize;
1063
    }
1064
1065
///////////////////////////////////////////////////////////////////////////////////////////////////
1066
1067
  public int getScrambleType()
1068
    {
1069
    return mScrambleType;
1070
    }
1071
1072
///////////////////////////////////////////////////////////////////////////////////////////////////
1073
1074
  public int getColor(int face)
1075
    {
1076
    return mColor[face];
1077
    }
1078 0f72365b Leszek Koltunski
1079 253e440f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1080
1081
  public int getInternalColor()
1082
    {
1083
    return mInternalColor;
1084
    }
1085
1086 0f72365b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1087
1088
  public boolean shouldResetTextureMaps()
1089
    {
1090
    return mResetMaps;
1091
    }
1092 052e0362 Leszek Koltunski
1093
///////////////////////////////////////////////////////////////////////////////////////////////////
1094
1095
  public String getTutorialObject()
1096
    {
1097
    return mTutorialObject;
1098
    }
1099
1100
///////////////////////////////////////////////////////////////////////////////////////////////////
1101
1102
  public String[][] getTutorials()
1103
    {
1104
    return mTutorials;
1105
    }
1106
1107 82eb152a Leszek Koltunski
}