Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
import java.io.File;
24
import java.io.FileInputStream;
25
import java.io.FileNotFoundException;
26
import java.io.IOException;
27
import java.io.InputStream;
28
import java.io.InputStreamReader;
29
import java.nio.charset.StandardCharsets;
30

    
31
import android.content.Context;
32

    
33
import org.distorted.objectlib.helpers.ObjectFaceShape;
34
import org.distorted.objectlib.helpers.ObjectSignature;
35
import org.distorted.objectlib.objects.TwistyBandagedGeneric;
36
import org.json.JSONArray;
37
import org.json.JSONException;
38
import org.json.JSONObject;
39

    
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42

    
43
import org.distorted.objectlib.helpers.ObjectShape;
44
import org.distorted.objectlib.helpers.ObjectSticker;
45
import org.distorted.objectlib.scrambling.ScrambleState;
46
import org.distorted.objectlib.main.ObjectType;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
public class JsonReader
51
{
52
  private ScrambleState[] mStates;
53
  private int[][] mSolvedQuats;
54
  private Static4D[] mQuats;
55
  private int mSolvedFuncIndex;
56
  private int mNumStickerTypes;
57
  private float[][] mCuts;
58
  private boolean[][] mLayerRotatable;
59
  private int mMovementType, mMovementSplit;
60
  private int[][][] mEnabled;
61
  private float[] mDist3D;
62
  private int mNumCubitFaces, mNumFaces, mNumFaceColors;
63
  private float[][] mPositions;
64
  private ObjectShape[] mShapes;
65
  private ObjectFaceShape[] mFaceShapes;
66
  private Static4D[] mCubitQuats;
67
  private int mNumCubitVariants;
68
  private int[] mCubitVariant;
69
  private int[][] mVariantFaceColor, mCubitFaceColor;
70
  private ObjectSticker[] mObjectSticker;
71
  private Static3D[] mAxis;
72
  private int[][] mBasicAngle;
73
  private String mLongName, mShortName, mInventor;
74
  private int mYearOfInvention, mComplexity;
75
  private int[] mNumLayers;
76
  private float mSize;
77
  private int mScrambleType, mNumScrambles;
78
  private int[] mColor;
79
  private int mInternalColor;
80
  private boolean mResetMaps;
81
  private String mTutorialObject;
82
  private String[][] mTutorials;
83
  private ObjectSignature mSignature;
84

    
85
  private static JsonReader mThis;
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
  private JsonReader()
90
    {
91
    }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
  public static JsonReader getInstance()
96
    {
97
    if( mThis==null ) mThis = new JsonReader();
98
    return mThis;
99
    }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
  private void parseMetadata1(JSONObject object) throws JSONException
104
    {
105
    mLongName        = object.getString("longname");
106
    mShortName       = object.getString("shortname");
107
    mInventor        = object.getString("inventor");
108
    mYearOfInvention = object.getInt("year");
109
    mComplexity      = object.getInt("complexity");
110
    mSize            = (float)object.getDouble("size");
111
    mNumScrambles    = object.getInt("scrambles");
112
    mResetMaps       = object.getBoolean("resetmaps");
113
    mNumFaces        = object.getInt("num_faces");
114

    
115
    try
116
      {
117
      long signature = object.getLong("signature");
118
      mSignature = new ObjectSignature(signature);
119
      }
120
    catch(JSONException ex)
121
      {
122
      // objects older than Feb 2022 do not have the 'signature' field. They all use the ObjectType.ordinal
123
      // as their signature.
124
      long signature = ObjectType.getOrdinal(mShortName);
125
      mSignature = new ObjectSignature(signature);
126
      }
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
  private void parseMetadata2(JSONObject object) throws JSONException
132
    {
133
    mLongName        = object.getString("longname");
134
    mShortName       = object.getString("shortname");
135
    mInventor        = object.getString("inventor");
136
    mYearOfInvention = object.getInt("year");
137
    mComplexity      = object.getInt("complexity");
138
    mSize            = (float)object.getDouble("size");
139
    mNumScrambles    = object.getInt("scrambles");
140
    mResetMaps       = object.getBoolean("resetmaps");
141
    mNumFaces        = object.getInt("num_faces");
142

    
143
    try
144
      {
145
      long signature1 = object.getLong("signature1");
146
      long signature2 = object.getLong("signature2");
147
      long signature3 = object.getLong("signature3");
148

    
149
      if( mLongName.equals(TwistyBandagedGeneric.OBJECT_NAME) )
150
        {
151
        mSignature = new ObjectSignature(mShortName,signature1,signature2,signature3);
152
        }
153
      else
154
        {
155
        mSignature = new ObjectSignature(signature1,signature2,signature3);
156
        }
157
      }
158
    catch(JSONException ex)
159
      {
160
      long signature = object.getLong("signature");
161
      mSignature = new ObjectSignature(signature);
162
      }
163
    }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
  private void parseCubits(JSONArray object) throws JSONException
168
    {
169
    int numCubits = object.length();
170

    
171
    mCubitQuats     = new Static4D[numCubits];
172
    mCubitVariant   = new int[numCubits];
173
    mPositions      = new float[numCubits][];
174
    mCubitFaceColor = new int[numCubits][];
175

    
176
    for(int i=0; i<numCubits; i++)
177
      {
178
      JSONObject jsonCubit = object.getJSONObject(i);
179

    
180
      float qx = (float)jsonCubit.getDouble("qx");
181
      float qy = (float)jsonCubit.getDouble("qy");
182
      float qz = (float)jsonCubit.getDouble("qz");
183
      float qw = (float)jsonCubit.getDouble("qw");
184

    
185
      mCubitQuats[i] = new Static4D(qx,qy,qz,qw);
186
      mCubitVariant[i] = jsonCubit.getInt("variant");
187

    
188
      JSONArray jsonCenter = jsonCubit.getJSONArray("centers");
189
      int numCenter = jsonCenter.length();
190
      mPositions[i] = new float[numCenter];
191
      for(int j=0; j<numCenter; j++) mPositions[i][j] = (float)jsonCenter.getDouble(j);
192

    
193
      JSONArray jsonColor  = jsonCubit.getJSONArray("colors");
194
      int numColor = jsonColor.length();
195
      mCubitFaceColor[i] = new int[numColor];
196
      for(int j=0; j<numColor; j++) mCubitFaceColor[i][j] = jsonColor.getInt(j);
197
      }
198
    }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  private void parseShapes(JSONArray object) throws JSONException
203
    {
204
    mNumCubitVariants = object.length();
205
    mVariantFaceColor = new int[mNumCubitVariants][];
206
    mShapes     = new ObjectShape[mNumCubitVariants];
207
    mFaceShapes = new ObjectFaceShape[mNumCubitVariants];
208
    float[][][] verts     = new float[mNumCubitVariants][][];
209
    float[][][] bands     = new float[mNumCubitVariants][][];
210
    float[][][] corners   = new float[mNumCubitVariants][][];
211
    float[][][] centers   = new float[mNumCubitVariants][][];
212
    float[][] convexity   = new float[mNumCubitVariants][];
213
    int[][] cornerIndices = new int[mNumCubitVariants][];
214
    int[][] centerIndices = new int[mNumCubitVariants][];
215
    int[][] bandIndices   = new int[mNumCubitVariants][];
216
    int[][][] vertIndices = new int[mNumCubitVariants][][];
217

    
218
    mNumCubitFaces = -1;
219

    
220
    for(int i=0; i<mNumCubitVariants; i++)
221
      {
222
      JSONObject jsonShape = object.getJSONObject(i);
223

    
224
      ////// vertices /////////////////////////////////////////////////
225
      JSONArray jsonVertices= jsonShape.getJSONArray("vertices");
226
      int numVertices = jsonVertices.length();
227
      verts[i] = new float[numVertices][3];
228
      cornerIndices[i] = new int[numVertices];
229
      centerIndices[i] = new int[numVertices];
230

    
231
      for(int j=0; j<numVertices; j++)
232
        {
233
        JSONObject vert = jsonVertices.getJSONObject(j);
234
        verts[i][j][0] = (float)vert.getDouble("x");
235
        verts[i][j][1] = (float)vert.getDouble("y");
236
        verts[i][j][2] = (float)vert.getDouble("z");
237
        cornerIndices[i][j] = vert.getInt("cornerIndex");
238
        centerIndices[i][j] = vert.getInt("centerIndex");
239
        }
240

    
241
      ////// faces ////////////////////////////////////////////////////
242
      JSONArray jsonFaces= jsonShape.getJSONArray("faces");
243
      int numFaces = jsonFaces.length();
244
      mVariantFaceColor[i] = new int[numFaces];
245
      bandIndices[i] = new int[numFaces];
246
      vertIndices[i] = new int[numFaces][];
247

    
248
      if( mNumCubitFaces<numFaces ) mNumCubitFaces=numFaces;
249

    
250
      for(int j=0; j<numFaces; j++)
251
        {
252
        JSONObject jsonFace = jsonFaces.getJSONObject(j);
253
        mVariantFaceColor[i][j] = jsonFace.getInt("sticker");
254
        bandIndices[i][j] = jsonFace.getInt("bandIndex");
255
        JSONArray vertices = jsonFace.getJSONArray("vertexIndices");
256
        int numV = vertices.length();
257
        vertIndices[i][j] = new int[numV];
258
        for(int k=0; k<numV; k++) vertIndices[i][j][k] = vertices.getInt(k);
259
        }
260

    
261
      ////// bands ////////////////////////////////////////////////////
262
      JSONArray jsonBands= jsonShape.getJSONArray("bands");
263
      int numBands = jsonBands.length();
264
      bands[i] = new float[numBands][7];
265

    
266
      for(int j=0; j<numBands; j++)
267
        {
268
        JSONObject jsonBand = jsonBands.getJSONObject(j);
269

    
270
        bands[i][j][0] = (float)jsonBand.getDouble("height");
271
        bands[i][j][1] = (float)jsonBand.getDouble("angle");
272
        bands[i][j][2] = (float)jsonBand.getDouble("distanceToCenter");
273
        bands[i][j][3] = (float)jsonBand.getDouble("distanceToFlat");
274
        bands[i][j][4] = (float)jsonBand.getDouble("numOfBands");
275
        bands[i][j][5] = (float)jsonBand.getDouble("extraI");
276
        bands[i][j][6] = (float)jsonBand.getDouble("extraJ");
277
        }
278

    
279
      ////// cornerPush ///////////////////////////////////////////////
280
      JSONArray jsonCornerPush= jsonShape.optJSONArray("cornerPush");
281

    
282
      if( jsonCornerPush!=null )
283
        {
284
        int numCornerP = jsonCornerPush.length();
285
        corners[i] = new float[numCornerP][2];
286

    
287
        for(int j=0; j<numCornerP; j++)
288
          {
289
          JSONObject jsonC = jsonCornerPush.getJSONObject(j);
290
          corners[i][j][0] = (float)jsonC.getDouble("strength");
291
          corners[i][j][1] = (float)jsonC.getDouble("radius");
292
          }
293
        }
294

    
295
      ////// centerPush ///////////////////////////////////////////////
296
      JSONArray jsonCenterPush= jsonShape.optJSONArray("centerPush");
297

    
298
      if( jsonCenterPush!=null )
299
        {
300
        int numCenterP = jsonCenterPush.length();
301
        centers[i] = new float[numCenterP][3];
302

    
303
        for(int j=0; j<numCenterP; j++)
304
          {
305
          JSONObject jsonC = jsonCenterPush.getJSONObject(j);
306
          centers[i][j][0] = (float)jsonC.getDouble("x");
307
          centers[i][j][1] = (float)jsonC.getDouble("y");
308
          centers[i][j][2] = (float)jsonC.getDouble("z");
309
          }
310
        }
311

    
312
      ////// convexity ///////////////////////////////////////////////
313
      JSONObject jsonConvexity = jsonShape.optJSONObject("convexity");
314

    
315
      if( jsonConvexity!=null )
316
        {
317
        convexity[i] = new float[3];
318
        convexity[i][0] = (float)jsonConvexity.getDouble("x");
319
        convexity[i][1] = (float)jsonConvexity.getDouble("y");
320
        convexity[i][2] = (float)jsonConvexity.getDouble("z");
321
        }
322
      }
323

    
324
    for(int i=0; i<mNumCubitVariants; i++)
325
      {
326
      mShapes[i] = new ObjectShape(verts[i],vertIndices[i]);
327
      mFaceShapes[i] = new ObjectFaceShape(bands[i],bandIndices[i],corners[i],cornerIndices[i],centers[i],centerIndices[i],convexity[i] );
328
      }
329
    }
330

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

    
333
  private void parseStickers(JSONArray object) throws JSONException
334
    {
335
    mNumStickerTypes = object.length();
336
    mObjectSticker = new ObjectSticker[mNumStickerTypes];
337

    
338
    for(int i=0; i<mNumStickerTypes; i++)
339
      {
340
      JSONObject sticker = object.getJSONObject(i);
341
      float stroke = (float)sticker.getDouble("stroke");
342
      JSONArray vertices = sticker.getJSONArray("vertices");
343
      int numVertices = vertices.length();
344

    
345
      float[] coords     = new float[2*numVertices];
346
      float[] curvatures = new float[numVertices];
347
      float[] radii      = new float[numVertices];
348

    
349
      for(int j=0; j<numVertices; j++)
350
        {
351
        JSONObject vertex = vertices.getJSONObject(j);
352

    
353
        coords[2*j  ] = (float)vertex.getDouble("x");
354
        coords[2*j+1] = (float)vertex.getDouble("y");
355
        curvatures[j] = (float)vertex.getDouble("angle");
356
        radii[j]      = (float)vertex.getDouble("radius");
357
        }
358

    
359
      mObjectSticker[i] = new ObjectSticker(coords,curvatures,radii,stroke);
360
      }
361
    }
362

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364

    
365
  private void parseMesh(JSONObject object) throws JSONException
366
    {
367
    JSONArray cubits   = object.getJSONArray("cubits");
368
    parseCubits(cubits);
369
    JSONArray shapes   = object.getJSONArray("shapes");
370
    parseShapes(shapes);
371
    JSONArray stickers = object.getJSONArray("stickers");
372
    parseStickers(stickers);
373
    }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

    
377
  private void parseAxis1(JSONArray object) throws JSONException
378
    {
379
    int numAxis = object.length();
380

    
381
    mBasicAngle     = new int[numAxis][];
382
    mAxis           = new Static3D[numAxis];
383
    mCuts           = new float[numAxis][];
384
    mLayerRotatable = new boolean[numAxis][];
385
    mNumLayers      = new int[numAxis];
386

    
387
    for(int i=0; i<numAxis; i++)
388
      {
389
      JSONObject jsonAx = object.getJSONObject(i);
390

    
391
      float x = (float)jsonAx.getDouble("x");
392
      float y = (float)jsonAx.getDouble("y");
393
      float z = (float)jsonAx.getDouble("z");
394

    
395
      mAxis[i] = new Static3D(x,y,z);
396

    
397
      JSONArray jsonCuts = jsonAx.getJSONArray("cuts");
398
      int numCuts = jsonCuts.length();
399
      mCuts[i] = new float[numCuts];
400
      for(int j=0; j<numCuts; j++) mCuts[i][j] = (float)jsonCuts.getDouble(j);
401

    
402
      JSONArray jsonRota = jsonAx.getJSONArray("rotatable");
403
      int numRota = jsonRota.length();
404
      mLayerRotatable[i] = new boolean[numRota];
405
      for(int j=0; j<numRota; j++) mLayerRotatable[i][j] = jsonRota.getBoolean(j);
406

    
407
      mBasicAngle[i] = new int[numRota];
408
      int basicAngle = jsonAx.getInt("basicAngle");
409
      for(int j=0; j<numRota; j++) mBasicAngle[i][j] = basicAngle;
410

    
411
      mNumLayers[i] = numRota;
412
      }
413
    }
414

    
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416

    
417
  private void parseAxis2(JSONArray object) throws JSONException
418
    {
419
    int numAxis = object.length();
420

    
421
    mBasicAngle     = new int[numAxis][];
422
    mAxis           = new Static3D[numAxis];
423
    mCuts           = new float[numAxis][];
424
    mLayerRotatable = new boolean[numAxis][];
425
    mNumLayers      = new int[numAxis];
426

    
427
    for(int i=0; i<numAxis; i++)
428
      {
429
      JSONObject jsonAx = object.getJSONObject(i);
430

    
431
      float x = (float)jsonAx.getDouble("x");
432
      float y = (float)jsonAx.getDouble("y");
433
      float z = (float)jsonAx.getDouble("z");
434

    
435
      mAxis[i] = new Static3D(x,y,z);
436

    
437
      JSONArray jsonAngles = jsonAx.getJSONArray("basicAngles");
438
      int numAngles = jsonAngles.length();
439
      mBasicAngle[i] = new int[numAngles];
440
      for(int j=0; j<numAngles; j++) mBasicAngle[i][j] = jsonAngles.getInt(j);
441

    
442
      JSONArray jsonCuts = jsonAx.getJSONArray("cuts");
443
      int numCuts = jsonCuts.length();
444
      mCuts[i] = new float[numCuts];
445
      for(int j=0; j<numCuts; j++) mCuts[i][j] = (float)jsonCuts.getDouble(j);
446

    
447
      JSONArray jsonRota = jsonAx.getJSONArray("rotatable");
448
      int numRota = jsonRota.length();
449
      mLayerRotatable[i] = new boolean[numRota];
450
      for(int j=0; j<numRota; j++) mLayerRotatable[i][j] = jsonRota.getBoolean(j);
451

    
452
      mNumLayers[i] = numRota;
453
      }
454
    }
455

    
456
///////////////////////////////////////////////////////////////////////////////////////////////////
457

    
458
  private void parseQuats(JSONArray object) throws JSONException
459
    {
460
    int numQuats = object.length();
461
    mQuats = new Static4D[numQuats];
462

    
463
    for(int i=0; i<numQuats; i++)
464
      {
465
      JSONObject jsonQuat = object.getJSONObject(i);
466

    
467
      float x = (float)jsonQuat.getDouble("x");
468
      float y = (float)jsonQuat.getDouble("y");
469
      float z = (float)jsonQuat.getDouble("z");
470
      float w = (float)jsonQuat.getDouble("w");
471

    
472
      mQuats[i] = new Static4D(x,y,z,w);
473
      }
474
    }
475

    
476
///////////////////////////////////////////////////////////////////////////////////////////////////
477

    
478
  private void parseScrambling(JSONObject object) throws JSONException
479
    {
480
    mScrambleType = object.getInt("scrambleType");
481

    
482
    if( mScrambleType==0 )
483
      {
484
      JSONArray jsonStates = object.getJSONArray("scrambleStates");
485
      int numStates = jsonStates.length();
486
      mStates = new ScrambleState[numStates];
487

    
488
      for(int i=0; i<numStates; i++)
489
        {
490
        JSONArray jsonState = jsonStates.getJSONArray(i);
491
        int numAxis = jsonState.length();
492
        int[][] scramblingData = new int[numAxis][];
493

    
494
        for(int j=0; j<numAxis; j++)
495
          {
496
          JSONArray jsonData = jsonState.getJSONArray(j);
497
          int numData = jsonData.length();
498
          scramblingData[j] = new int[numData];
499
          for(int k=0; k<numData; k++) scramblingData[j][k] = jsonData.getInt(k);
500
          }
501

    
502
        mStates[i] = new ScrambleState(scramblingData);
503
        }
504
      }
505
    }
506

    
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508

    
509
  private void parseTouchcontrol(JSONObject object) throws JSONException
510
    {
511
    mMovementType = object.getInt("movementType");
512
    mMovementSplit= object.getInt("movementSplit");
513

    
514
    try
515
      {
516
      JSONArray jsonEnabled = object.getJSONArray("enabledAxis");
517
      int numFace = jsonEnabled.length();
518

    
519
      mEnabled = new int[numFace][][];
520

    
521
      for(int i=0; i<numFace; i++)
522
        {
523
        JSONArray jsonSection = jsonEnabled.getJSONArray(i);
524
        int numSection = jsonSection.length();
525
        mEnabled[i] = new int[numSection][];
526

    
527
        for(int j=0; j<numSection; j++)
528
          {
529
          JSONArray jsonAx = jsonSection.getJSONArray(j);
530
          int numAxis = jsonAx.length();
531
          mEnabled[i][j] = new int[numAxis];
532
          for(int k=0; k<numAxis; k++) mEnabled[i][j][k] = jsonAx.getInt(k);
533
          }
534
        }
535
      }
536
    catch( JSONException ex )
537
      {
538
      // ignore, the object does not have to have 'enabledAxis' defined at all.
539
      }
540

    
541
    try
542
      {
543
      JSONArray jsonDist = object.getJSONArray("dist3D");
544
      int num = jsonDist.length();
545
      mDist3D = new float[num];
546
      for(int j=0; j<num; j++) mDist3D[j] = (float)jsonDist.getDouble(j);
547
      }
548
    catch( JSONException ex )
549
      {
550
      // ignore, the object does not have a 'dist3D' which is possible.
551
      }
552
    }
553

    
554
///////////////////////////////////////////////////////////////////////////////////////////////////
555

    
556
  private void parseColors(JSONArray object) throws JSONException
557
    {
558
    mNumFaceColors = object.length()-1;
559

    
560
    mColor = new int[mNumFaceColors];
561
    for(int i=0; i<mNumFaceColors; i++) mColor[i] = object.getInt(i);
562

    
563
    mInternalColor = object.getInt(mNumFaceColors);
564
    }
565

    
566
///////////////////////////////////////////////////////////////////////////////////////////////////
567

    
568
  private void parseSolved(JSONObject solved) throws JSONException
569
    {
570
    mSolvedFuncIndex = solved.getInt("functionIndex");
571

    
572
    try
573
      {
574
      JSONArray groupArray= solved.getJSONArray("groups");
575
      int numGroups = groupArray.length();
576
      mSolvedQuats  = new int[numGroups][];
577

    
578
      for(int i=0; i<numGroups; i++)
579
        {
580
        JSONArray groupElements = groupArray.getJSONArray(i);
581
        int groupSize = groupElements.length();
582
        mSolvedQuats[i] = new int[groupSize];
583
        for(int j=0; j<groupSize; j++) mSolvedQuats[i][j] = groupElements.getInt(j);
584
        }
585
      }
586
    catch( JSONException ex )
587
      {
588
      // ignore, the object does not have to have an array of solved groups.
589
      }
590
    }
591

    
592
///////////////////////////////////////////////////////////////////////////////////////////////////
593

    
594
  private void parseVersion1(JSONObject object) throws JSONException
595
    {
596
    JSONObject metadata    = object.getJSONObject("metadata");
597
    parseMetadata1(metadata);
598
    JSONObject mesh        = object.getJSONObject("mesh");
599
    parseMesh(mesh);
600
    JSONArray axis         = object.getJSONArray("axis");
601
    parseAxis1(axis);
602
    JSONArray quats        = object.getJSONArray("quats");
603
    parseQuats(quats);
604
    JSONObject scrambling  = object.getJSONObject("scrambling");
605
    parseScrambling(scrambling);
606
    JSONObject touchcontrol= object.getJSONObject("touchcontrol");
607
    parseTouchcontrol(touchcontrol);
608
    JSONArray colors       = object.getJSONArray("colors");
609
    parseColors(colors);
610
    JSONObject solved      = object.getJSONObject("solved");
611
    parseSolved(solved);
612
    }
613

    
614
///////////////////////////////////////////////////////////////////////////////////////////////////
615

    
616
  private void parseVersion2(JSONObject object) throws JSONException
617
    {
618
    JSONObject metadata    = object.getJSONObject("metadata");
619
    parseMetadata2(metadata);
620
    JSONObject mesh        = object.getJSONObject("mesh");
621
    parseMesh(mesh);
622
    JSONArray axis         = object.getJSONArray("axis");
623
    parseAxis2(axis);
624
    JSONArray quats        = object.getJSONArray("quats");
625
    parseQuats(quats);
626
    JSONObject scrambling  = object.getJSONObject("scrambling");
627
    parseScrambling(scrambling);
628
    JSONObject touchcontrol= object.getJSONObject("touchcontrol");
629
    parseTouchcontrol(touchcontrol);
630
    JSONArray colors       = object.getJSONArray("colors");
631
    parseColors(colors);
632
    JSONObject solved      = object.getJSONObject("solved");
633
    parseSolved(solved);
634
    }
635

    
636
///////////////////////////////////////////////////////////////////////////////////////////////////
637

    
638
  private void parseVersion1Tutorial(JSONObject object) throws JSONException
639
    {
640
    mTutorialObject = object.getString("object");
641
    JSONArray tuts= object.getJSONArray("tutorials");
642

    
643
    int len = tuts.length();
644
    mTutorials = new String[len][4];
645

    
646
    for(int i=0; i<len; i++)
647
      {
648
      JSONObject tut = tuts.getJSONObject(i);
649
      mTutorials[i][0] = tut.getString("language");
650
      mTutorials[i][1] = tut.getString("link");
651
      mTutorials[i][2] = tut.getString("title");
652
      mTutorials[i][3] = tut.getString("author");
653
      }
654
    }
655

    
656
///////////////////////////////////////////////////////////////////////////////////////////////////
657

    
658
  private void parseVersion1Metadata(JSONObject object) throws JSONException
659
    {
660
    JSONObject metadata = object.getJSONObject("metadata");
661
    parseMetadata1(metadata);
662
    }
663

    
664
///////////////////////////////////////////////////////////////////////////////////////////////////
665

    
666
  private void parseVersion2Metadata(JSONObject object) throws JSONException
667
    {
668
    JSONObject metadata = object.getJSONObject("metadata");
669
    parseMetadata2(metadata);
670
    }
671

    
672
///////////////////////////////////////////////////////////////////////////////////////////////////
673

    
674
  public void parseJsonFile(InputStream jsonStream)
675
    {
676
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
677

    
678
    try
679
      {
680
      StringBuilder contents = new StringBuilder();
681
      String tmp;
682
      while( (tmp = br.readLine()) != null) contents.append(tmp);
683
      br.close();
684
      jsonStream.close();
685

    
686
      JSONObject object = new JSONObject(contents.toString());
687
      int major = object.getInt("major");
688

    
689
      if( major==1 )
690
        {
691
        parseVersion1(object);
692
        }
693
      else if( major==2 )
694
        {
695
        parseVersion2(object);
696
        }
697
      else
698
        {
699
        android.util.Log.e("readJsonFile", "Unknown version "+major);
700
        }
701
      }
702
    catch(IOException e)
703
      {
704
      android.util.Log.e("readJsonFile", "Error reading JSON file: "+e.toString());
705
      }
706
    catch( JSONException e )
707
      {
708
      android.util.Log.e("parseJsonFile", "Error parsing JSON file: "+e.toString());
709
      }
710
    }
711

    
712
///////////////////////////////////////////////////////////////////////////////////////////////////
713

    
714
  public void parseJsonFileMetadata(InputStream jsonStream)
715
    {
716
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
717

    
718
    try
719
      {
720
      StringBuilder contents = new StringBuilder();
721
      String tmp;
722
      while( (tmp = br.readLine()) != null) contents.append(tmp);
723
      br.close();
724
      jsonStream.close();
725

    
726
      JSONObject object = new JSONObject(contents.toString());
727
      int major = object.getInt("major");
728

    
729
      if( major==1 )
730
        {
731
        parseVersion1Metadata(object);
732
        }
733
      else if( major==2 )
734
        {
735
        parseVersion2Metadata(object);
736
        }
737
      else
738
        {
739
        android.util.Log.e("readJsonFileQuick", "Unknown version "+major);
740
        }
741
      }
742
    catch(IOException e)
743
      {
744
      android.util.Log.e("readJsonFileQuick", "Error reading JSON file: "+e.toString());
745
      }
746
    catch( JSONException e )
747
      {
748
      android.util.Log.e("parseJsonFileQuick", "Error parsing JSON file: "+e.toString());
749
      }
750
    }
751

    
752
///////////////////////////////////////////////////////////////////////////////////////////////////
753

    
754
  public int readNumScrambles(Context context, String fileName)
755
    {
756
    File file = new File(context.getFilesDir(), fileName);
757
    InputStream stream;
758

    
759
    try
760
      {
761
      stream = new FileInputStream(file);
762
      BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
763

    
764
      StringBuilder contents = new StringBuilder();
765
      String tmp;
766
      while( (tmp = br.readLine()) != null) contents.append(tmp);
767
      br.close();
768
      stream.close();
769

    
770
      JSONObject object = new JSONObject(contents.toString());
771
      int major = object.getInt("major");
772

    
773
      if( major==1 || major==2 )
774
        {
775
        JSONObject metadata = object.getJSONObject("metadata");
776
        return metadata.getInt("scrambles");
777
        }
778
      else
779
        {
780
        android.util.Log.e("readNumScrambles", "Unknown version "+major);
781
        }
782
      }
783
    catch(FileNotFoundException ex)
784
      {
785
      android.util.Log.e("readNumScrambles", "file "+fileName+" not found: "+ex.getMessage());
786
      }
787
    catch(IOException e)
788
      {
789
      android.util.Log.e("readNumScrambles", "Error reading JSON file: "+e.toString());
790
      }
791
    catch( JSONException e )
792
      {
793
      android.util.Log.e("readNumScrambles", "Error parsing JSON file: "+e.toString());
794
      }
795

    
796
    return 0;
797
    }
798

    
799
///////////////////////////////////////////////////////////////////////////////////////////////////
800

    
801
  public void parseJsonTutorial(InputStream jsonStream)
802
    {
803
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
804

    
805
    try
806
      {
807
      StringBuilder contents = new StringBuilder();
808
      String tmp;
809
      while( (tmp = br.readLine()) != null) contents.append(tmp);
810
      br.close();
811
      jsonStream.close();
812

    
813
      JSONObject object = new JSONObject(contents.toString());
814
      int major = object.getInt("major");
815

    
816
      if( major==1 )
817
        {
818
        parseVersion1Tutorial(object);
819
        }
820
      else
821
        {
822
        android.util.Log.e("readJsonFile", "Unknown tutorial version "+major);
823
        }
824
      }
825
    catch(IOException e)
826
      {
827
      android.util.Log.e("readJsonFile", "Error reading JSON file: "+e.toString());
828
      }
829
    catch( JSONException e )
830
      {
831
      android.util.Log.e("parseJsonFile", "Error parsing JSON file: "+e.toString());
832
      }
833
    }
834

    
835
///////////////////////////////////////////////////////////////////////////////////////////////////
836

    
837
  public ScrambleState[] getScrambleStates()
838
    {
839
    return mStates;
840
    }
841

    
842
///////////////////////////////////////////////////////////////////////////////////////////////////
843

    
844
  public int[][] getSolvedQuats()
845
    {
846
    return mSolvedQuats;
847
    }
848

    
849
///////////////////////////////////////////////////////////////////////////////////////////////////
850

    
851
  public Static4D[] getQuats()
852
    {
853
    return mQuats;
854
    }
855

    
856
///////////////////////////////////////////////////////////////////////////////////////////////////
857

    
858
  public int getSolvedFunctionIndex()
859
    {
860
    return mSolvedFuncIndex;
861
    }
862

    
863
///////////////////////////////////////////////////////////////////////////////////////////////////
864

    
865
  public int getNumStickerTypes()
866
    {
867
    return mNumStickerTypes;
868
    }
869

    
870
///////////////////////////////////////////////////////////////////////////////////////////////////
871

    
872
  public float[][] getCuts()
873
    {
874
    return mCuts;
875
    }
876

    
877
///////////////////////////////////////////////////////////////////////////////////////////////////
878

    
879
  public boolean[][] getLayerRotatable()
880
    {
881
    return mLayerRotatable;
882
    }
883

    
884
///////////////////////////////////////////////////////////////////////////////////////////////////
885

    
886
  public int getMovementType()
887
    {
888
    return mMovementType;
889
    }
890

    
891
///////////////////////////////////////////////////////////////////////////////////////////////////
892

    
893
  public int getMovementSplit()
894
    {
895
    return mMovementSplit;
896
    }
897

    
898
///////////////////////////////////////////////////////////////////////////////////////////////////
899

    
900
  public int[][][] getEnabled()
901
    {
902
    return mEnabled;
903
    }
904

    
905
///////////////////////////////////////////////////////////////////////////////////////////////////
906

    
907
  public float[] getDist3D()
908
    {
909
    return mDist3D;
910
    }
911

    
912
///////////////////////////////////////////////////////////////////////////////////////////////////
913

    
914
  public int getNumCubitFaces()
915
    {
916
    return mNumCubitFaces;
917
    }
918

    
919
///////////////////////////////////////////////////////////////////////////////////////////////////
920

    
921
  public float[][] getCubitPositions()
922
    {
923
    return mPositions;
924
    }
925

    
926
///////////////////////////////////////////////////////////////////////////////////////////////////
927

    
928
  public ObjectShape getObjectShape(int variant)
929
    {
930
    return mShapes[variant];
931
    }
932

    
933
///////////////////////////////////////////////////////////////////////////////////////////////////
934

    
935
  public ObjectFaceShape getObjectFaceShape(int variant)
936
    {
937
    return mFaceShapes[variant];
938
    }
939

    
940
///////////////////////////////////////////////////////////////////////////////////////////////////
941

    
942
  public Static4D getCubitQuats(int cubit)
943
    {
944
    return mCubitQuats[cubit];
945
    }
946

    
947
///////////////////////////////////////////////////////////////////////////////////////////////////
948

    
949
  public int getNumCubitVariants()
950
    {
951
    return mNumCubitVariants;
952
    }
953

    
954
///////////////////////////////////////////////////////////////////////////////////////////////////
955

    
956
  public int getCubitVariant(int cubit)
957
    {
958
    return mCubitVariant[cubit];
959
    }
960

    
961
///////////////////////////////////////////////////////////////////////////////////////////////////
962

    
963
  public int getVariantFaceColor(int variant, int face)
964
    {
965
    int[] colors = mVariantFaceColor[variant];
966
    return colors.length>face ? colors[face] : -1;
967
    }
968

    
969
///////////////////////////////////////////////////////////////////////////////////////////////////
970

    
971
  public int getCubitFaceFace(int cubit, int face)
972
    {
973
    return mCubitFaceColor[cubit][face];
974
    }
975

    
976
///////////////////////////////////////////////////////////////////////////////////////////////////
977

    
978
  public int getNumScrambles()
979
    {
980
    return mNumScrambles;
981
    }
982

    
983
///////////////////////////////////////////////////////////////////////////////////////////////////
984

    
985
  public ObjectSticker retSticker(int sticker)
986
    {
987
    return mObjectSticker[sticker];
988
    }
989

    
990
///////////////////////////////////////////////////////////////////////////////////////////////////
991

    
992
  public Static3D[] getRotationAxis()
993
    {
994
    return mAxis;
995
    }
996

    
997
///////////////////////////////////////////////////////////////////////////////////////////////////
998

    
999
  public int[][] getBasicAngle()
1000
    {
1001
    return mBasicAngle;
1002
    }
1003

    
1004
///////////////////////////////////////////////////////////////////////////////////////////////////
1005

    
1006
  public ObjectSignature getSignature()
1007
    {
1008
    return mSignature;
1009
    }
1010

    
1011
///////////////////////////////////////////////////////////////////////////////////////////////////
1012

    
1013
  public String getObjectName()
1014
    {
1015
    return mLongName;
1016
    }
1017

    
1018
///////////////////////////////////////////////////////////////////////////////////////////////////
1019

    
1020
  public String getShortName()
1021
    {
1022
    return mShortName;
1023
    }
1024

    
1025
///////////////////////////////////////////////////////////////////////////////////////////////////
1026

    
1027
  public String getInventor()
1028
    {
1029
    return mInventor;
1030
    }
1031

    
1032
///////////////////////////////////////////////////////////////////////////////////////////////////
1033

    
1034
  public int getYearOfInvention()
1035
    {
1036
    return mYearOfInvention;
1037
    }
1038

    
1039
///////////////////////////////////////////////////////////////////////////////////////////////////
1040

    
1041
  public int getComplexity()
1042
    {
1043
    return mComplexity;
1044
    }
1045

    
1046
///////////////////////////////////////////////////////////////////////////////////////////////////
1047

    
1048
  public int getNumFaces()
1049
    {
1050
    return mNumFaces;
1051
    }
1052

    
1053
///////////////////////////////////////////////////////////////////////////////////////////////////
1054

    
1055
  public int getNumFaceColors()
1056
    {
1057
    return mNumFaceColors;
1058
    }
1059

    
1060
///////////////////////////////////////////////////////////////////////////////////////////////////
1061

    
1062
  public int[] getNumLayers()
1063
    {
1064
    return mNumLayers;
1065
    }
1066

    
1067
///////////////////////////////////////////////////////////////////////////////////////////////////
1068

    
1069
  public float getSize()
1070
    {
1071
    return mSize;
1072
    }
1073

    
1074
///////////////////////////////////////////////////////////////////////////////////////////////////
1075

    
1076
  public int getScrambleType()
1077
    {
1078
    return mScrambleType;
1079
    }
1080

    
1081
///////////////////////////////////////////////////////////////////////////////////////////////////
1082

    
1083
  public int getColor(int face)
1084
    {
1085
    return mColor[face];
1086
    }
1087

    
1088
///////////////////////////////////////////////////////////////////////////////////////////////////
1089

    
1090
  public int getInternalColor()
1091
    {
1092
    return mInternalColor;
1093
    }
1094

    
1095
///////////////////////////////////////////////////////////////////////////////////////////////////
1096

    
1097
  public boolean shouldResetTextureMaps()
1098
    {
1099
    return mResetMaps;
1100
    }
1101

    
1102
///////////////////////////////////////////////////////////////////////////////////////////////////
1103

    
1104
  public String getTutorialObject()
1105
    {
1106
    return mTutorialObject;
1107
    }
1108

    
1109
///////////////////////////////////////////////////////////////////////////////////////////////////
1110

    
1111
  public String[][] getTutorials()
1112
    {
1113
    return mTutorials;
1114
    }
1115

    
1116
}
(1-1/2)