Project

General

Profile

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

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

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.helpers.ObjectStickerOverride;
36
import org.distorted.objectlib.main.Cubit;
37
import org.distorted.objectlib.objects.TwistyBandagedCuboid;
38
import org.json.JSONArray;
39
import org.json.JSONException;
40
import org.json.JSONObject;
41

    
42
import org.distorted.library.type.Static3D;
43
import org.distorted.library.type.Static4D;
44

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

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

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

    
90
  private static JsonReader mThis;
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

    
94
  private JsonReader()
95
    {
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  public static JsonReader getInstance()
101
    {
102
    if( mThis==null ) mThis = new JsonReader();
103
    return mThis;
104
    }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

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

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

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  private void parseMetadata2or3or4(JSONObject object) throws JSONException
137
    {
138
    mLongName        = object.getString("longname");
139
    mShortName       = object.getString("shortname");
140
    mInventor        = object.getString("inventor");
141
    mYearOfInvention = object.getInt("year");
142
    mComplexity      = object.getInt("complexity");
143
    mSize            = (float)object.getDouble("size");
144
    mNumScrambles    = object.getInt("scrambles");
145
    mResetMaps       = object.getBoolean("resetmaps");
146
    mNumFaces        = object.getInt("num_faces");
147

    
148
    try
149
      {
150
      long signature1 = object.getLong("signature1");
151
      long signature2 = object.getLong("signature2");
152
      long signature3 = object.getLong("signature3");
153

    
154
      switch( TwistyBandagedCuboid.getType(mShortName,mLongName) )
155
        {
156
        case 0: mSignature = new ObjectSignature(signature1,signature2,signature3); break;
157
        case 1: mSignature = new ObjectSignature(mShortName,signature1,signature2,signature3); break;
158
        case 2: mSignature = new ObjectSignature("333",signature1,signature2,signature3); break;
159
        }
160
      }
161
    catch(JSONException ex)
162
      {
163
      long signature = object.getLong("signature");
164
      mSignature = new ObjectSignature(signature);
165
      }
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
  private void parseCubits(JSONArray object) throws JSONException
171
    {
172
    int numCubits = object.length();
173

    
174
    mCubitQuats     = new Static4D[numCubits];
175
    mCubitVariant   = new int[numCubits];
176
    mPositions      = new float[numCubits][];
177
    mCubitFaceColor = new int[numCubits][];
178
    mCubitType      = new int[numCubits];
179
    mCubitRowOffset = new float[numCubits][];
180

    
181
    for(int i=0; i<numCubits; i++)
182
      {
183
      JSONObject jsonCubit = object.getJSONObject(i);
184

    
185
      float qx = (float)jsonCubit.getDouble("qx");
186
      float qy = (float)jsonCubit.getDouble("qy");
187
      float qz = (float)jsonCubit.getDouble("qz");
188
      float qw = (float)jsonCubit.getDouble("qw");
189

    
190
      mCubitQuats[i] = new Static4D(qx,qy,qz,qw);
191
      mCubitVariant[i] = jsonCubit.getInt("variant");
192

    
193
      JSONArray jsonCenter = jsonCubit.getJSONArray("centers");
194
      int numCenter = jsonCenter.length();
195
      mPositions[i] = new float[numCenter];
196
      for(int j=0; j<numCenter; j++) mPositions[i][j] = (float)jsonCenter.getDouble(j);
197

    
198
      JSONArray jsonColor  = jsonCubit.getJSONArray("colors");
199
      int numColor = jsonColor.length();
200
      mCubitFaceColor[i] = new int[numColor];
201
      for(int j=0; j<numColor; j++) mCubitFaceColor[i][j] = jsonColor.getInt(j);
202

    
203
      mCubitType[i] = jsonCubit.optInt("type", Cubit.TYPE_NORMAL);
204
      float xoff = (float)jsonCubit.optDouble("offsetX", 0 );
205
      float yoff = (float)jsonCubit.optDouble("offsetY", 0 );
206
      float zoff = (float)jsonCubit.optDouble("offsetZ", 0 );
207
      mCubitRowOffset[i] = new float[] {xoff,yoff,zoff};
208
      }
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  private void parseShapes(JSONArray object) throws JSONException
214
    {
215
    mNumCubitVariants     = object.length();
216
    mVariantFaceColor     = new int[mNumCubitVariants][];
217
    mVariantFaceIsOuter   = new int[mNumCubitVariants][];
218
    mShapes               = new ObjectShape[mNumCubitVariants];
219
    mFaceShapes           = new ObjectFaceShape[mNumCubitVariants];
220
    float[][][] verts     = new float[mNumCubitVariants][][];
221
    float[][][] bands     = new float[mNumCubitVariants][][];
222
    float[][][] corners   = new float[mNumCubitVariants][][];
223
    float[][][] centers   = new float[mNumCubitVariants][][];
224
    float[][] convexity   = new float[mNumCubitVariants][];
225
    int[][] cornerIndices = new int[mNumCubitVariants][];
226
    int[][] centerIndices = new int[mNumCubitVariants][];
227
    int[][] bandIndices   = new int[mNumCubitVariants][];
228
    int[][][] vertIndices = new int[mNumCubitVariants][][];
229

    
230
    mNumCubitFaces = -1;
231

    
232
    for(int i=0; i<mNumCubitVariants; i++)
233
      {
234
      JSONObject jsonShape = object.getJSONObject(i);
235

    
236
      ////// vertices /////////////////////////////////////////////////
237
      JSONArray jsonVertices= jsonShape.getJSONArray("vertices");
238
      int numVertices = jsonVertices.length();
239
      verts[i] = new float[numVertices][3];
240
      cornerIndices[i] = new int[numVertices];
241
      centerIndices[i] = new int[numVertices];
242

    
243
      for(int j=0; j<numVertices; j++)
244
        {
245
        JSONObject vert = jsonVertices.getJSONObject(j);
246
        verts[i][j][0] = (float)vert.getDouble("x");
247
        verts[i][j][1] = (float)vert.getDouble("y");
248
        verts[i][j][2] = (float)vert.getDouble("z");
249
        cornerIndices[i][j] = vert.getInt("cornerIndex");
250
        centerIndices[i][j] = vert.getInt("centerIndex");
251
        }
252

    
253
      ////// faces ////////////////////////////////////////////////////
254
      JSONArray jsonFaces= jsonShape.getJSONArray("faces");
255
      int numFaces = jsonFaces.length();
256
      mVariantFaceColor[i]   = new int[numFaces];
257
      mVariantFaceIsOuter[i] = new int[numFaces];
258
      bandIndices[i] = new int[numFaces];
259
      vertIndices[i] = new int[numFaces][];
260

    
261
      if( mNumCubitFaces<numFaces ) mNumCubitFaces=numFaces;
262

    
263
      for(int j=0; j<numFaces; j++)
264
        {
265
        JSONObject jsonFace = jsonFaces.getJSONObject(j);
266
        mVariantFaceColor[i][j]   = jsonFace.getInt("sticker");
267
        mVariantFaceIsOuter[i][j] = jsonFace.optInt("isOuter",0);
268
        bandIndices[i][j] = jsonFace.getInt("bandIndex");
269
        JSONArray vertices = jsonFace.getJSONArray("vertexIndices");
270
        int numV = vertices.length();
271
        vertIndices[i][j] = new int[numV];
272
        for(int k=0; k<numV; k++) vertIndices[i][j][k] = vertices.getInt(k);
273
        }
274

    
275
      ////// bands ////////////////////////////////////////////////////
276
      JSONArray jsonBands= jsonShape.getJSONArray("bands");
277
      int numBands = jsonBands.length();
278
      bands[i] = new float[numBands][7];
279

    
280
      for(int j=0; j<numBands; j++)
281
        {
282
        JSONObject jsonBand = jsonBands.getJSONObject(j);
283

    
284
        bands[i][j][0] = (float)jsonBand.getDouble("height");
285
        bands[i][j][1] = (float)jsonBand.getDouble("angle");
286
        bands[i][j][2] = (float)jsonBand.getDouble("distanceToCenter");
287
        bands[i][j][3] = (float)jsonBand.getDouble("distanceToFlat");
288
        bands[i][j][4] = (float)jsonBand.getDouble("numOfBands");
289
        bands[i][j][5] = (float)jsonBand.getDouble("extraI");
290
        bands[i][j][6] = (float)jsonBand.getDouble("extraJ");
291
        }
292

    
293
      ////// cornerPush ///////////////////////////////////////////////
294
      JSONArray jsonCornerPush= jsonShape.optJSONArray("cornerPush");
295

    
296
      if( jsonCornerPush!=null )
297
        {
298
        int numCornerP = jsonCornerPush.length();
299
        corners[i] = new float[numCornerP][2];
300

    
301
        for(int j=0; j<numCornerP; j++)
302
          {
303
          JSONObject jsonC = jsonCornerPush.getJSONObject(j);
304
          corners[i][j][0] = (float)jsonC.getDouble("strength");
305
          corners[i][j][1] = (float)jsonC.getDouble("radius");
306
          }
307
        }
308

    
309
      ////// centerPush ///////////////////////////////////////////////
310
      JSONArray jsonCenterPush= jsonShape.optJSONArray("centerPush");
311

    
312
      if( jsonCenterPush!=null )
313
        {
314
        int numCenterP = jsonCenterPush.length();
315
        centers[i] = new float[numCenterP][3];
316

    
317
        for(int j=0; j<numCenterP; j++)
318
          {
319
          JSONObject jsonC = jsonCenterPush.getJSONObject(j);
320
          centers[i][j][0] = (float)jsonC.getDouble("x");
321
          centers[i][j][1] = (float)jsonC.getDouble("y");
322
          centers[i][j][2] = (float)jsonC.getDouble("z");
323
          }
324
        }
325

    
326
      ////// convexity ///////////////////////////////////////////////
327
      JSONObject jsonConvexity = jsonShape.optJSONObject("convexity");
328

    
329
      if( jsonConvexity!=null )
330
        {
331
        convexity[i] = new float[3];
332
        convexity[i][0] = (float)jsonConvexity.getDouble("x");
333
        convexity[i][1] = (float)jsonConvexity.getDouble("y");
334
        convexity[i][2] = (float)jsonConvexity.getDouble("z");
335
        }
336
      }
337

    
338
    for(int i=0; i<mNumCubitVariants; i++)
339
      {
340
      mShapes[i] = new ObjectShape(verts[i],vertIndices[i]);
341
      mFaceShapes[i] = new ObjectFaceShape(bands[i],bandIndices[i],corners[i],cornerIndices[i],centers[i],centerIndices[i],convexity[i] );
342
      }
343
    }
344

    
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346

    
347
  private void parseStickers(JSONArray object) throws JSONException
348
    {
349
    mNumStickerTypes = object.length();
350
    mObjectSticker = new ObjectSticker[mNumStickerTypes];
351

    
352
    for(int i=0; i<mNumStickerTypes; i++)
353
      {
354
      JSONObject sticker = object.getJSONObject(i);
355
      float stroke = (float)sticker.getDouble("stroke");
356
      JSONArray vertices = sticker.getJSONArray("vertices");
357
      int numVertices = vertices.length();
358

    
359
      float[] coords     = new float[2*numVertices];
360
      float[] curvatures = new float[numVertices];
361
      float[] radii      = new float[numVertices];
362

    
363
      for(int j=0; j<numVertices; j++)
364
        {
365
        JSONObject vertex = vertices.getJSONObject(j);
366

    
367
        coords[2*j  ] = (float)vertex.getDouble("x");
368
        coords[2*j+1] = (float)vertex.getDouble("y");
369
        curvatures[j] = (float)vertex.getDouble("angle");
370
        radii[j]      = (float)vertex.getDouble("radius");
371
        }
372

    
373
      mObjectSticker[i] = new ObjectSticker(coords,curvatures,radii,stroke);
374
      }
375
    }
376

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

    
379
  private void parseOverrides(JSONArray object) throws JSONException
380
    {
381
    if( object!=null )
382
      {
383
      int numOverrides = object.length();
384
      mStickerOverrides= new ObjectStickerOverride[numOverrides];
385

    
386
      for(int i=0; i<numOverrides; i++)
387
        {
388
        JSONObject override  = object.getJSONObject(i);
389
        JSONArray cubitArray = override.getJSONArray("cubitfaces");
390
        int color = override.getInt("color");
391
        int numCubits = cubitArray.length();
392
        int[] cubitface = new int[numCubits];
393
        for(int j=0; j<numCubits; j++) cubitface[j] = cubitArray.getInt(j);
394
        mStickerOverrides[i] = new ObjectStickerOverride(cubitface,color);
395
        }
396
      }
397
    else
398
      {
399
      mStickerOverrides = null;
400
      }
401
    }
402

    
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404

    
405
  private void parseMesh(JSONObject object) throws JSONException
406
    {
407
    JSONArray cubits   = object.getJSONArray("cubits");
408
    parseCubits(cubits);
409
    JSONArray shapes   = object.getJSONArray("shapes");
410
    parseShapes(shapes);
411
    JSONArray stickers = object.getJSONArray("stickers");
412
    parseStickers(stickers);
413

    
414
    JSONArray overrides= object.optJSONArray("overrides");
415
    parseOverrides(overrides);
416
    }
417

    
418
///////////////////////////////////////////////////////////////////////////////////////////////////
419

    
420
  private void parseAxis1(JSONArray object) throws JSONException
421
    {
422
    int numAxis = object.length();
423

    
424
    mBasicAngle     = new int[numAxis][];
425
    mAxis           = new Static3D[numAxis];
426
    mCuts           = new float[numAxis][];
427
    mLayerRotatable = new boolean[numAxis][];
428
    mNumLayers      = new int[numAxis];
429

    
430
    for(int i=0; i<numAxis; i++)
431
      {
432
      JSONObject jsonAx = object.getJSONObject(i);
433

    
434
      float x = (float)jsonAx.getDouble("x");
435
      float y = (float)jsonAx.getDouble("y");
436
      float z = (float)jsonAx.getDouble("z");
437

    
438
      mAxis[i] = new Static3D(x,y,z);
439

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

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

    
450
      mBasicAngle[i] = new int[numRota];
451
      int basicAngle = jsonAx.getInt("basicAngle");
452
      for(int j=0; j<numRota; j++) mBasicAngle[i][j] = basicAngle;
453

    
454
      mNumLayers[i] = numRota;
455
      }
456
    }
457

    
458
///////////////////////////////////////////////////////////////////////////////////////////////////
459

    
460
  private void parseAxis2or3or4(JSONArray object) throws JSONException
461
    {
462
    int numAxis = object.length();
463

    
464
    mBasicAngle     = new int[numAxis][];
465
    mAxis           = new Static3D[numAxis];
466
    mCuts           = new float[numAxis][];
467
    mLayerRotatable = new boolean[numAxis][];
468
    mNumLayers      = new int[numAxis];
469

    
470
    for(int i=0; i<numAxis; i++)
471
      {
472
      JSONObject jsonAx = object.getJSONObject(i);
473

    
474
      float x = (float)jsonAx.getDouble("x");
475
      float y = (float)jsonAx.getDouble("y");
476
      float z = (float)jsonAx.getDouble("z");
477

    
478
      mAxis[i] = new Static3D(x,y,z);
479

    
480
      JSONArray jsonAngles = jsonAx.getJSONArray("basicAngles");
481
      int numAngles = jsonAngles.length();
482
      mBasicAngle[i] = new int[numAngles];
483
      for(int j=0; j<numAngles; j++) mBasicAngle[i][j] = jsonAngles.getInt(j);
484

    
485
      JSONArray jsonCuts = jsonAx.getJSONArray("cuts");
486
      int numCuts = jsonCuts.length();
487
      mCuts[i] = new float[numCuts];
488
      for(int j=0; j<numCuts; j++) mCuts[i][j] = (float)jsonCuts.getDouble(j);
489

    
490
      JSONArray jsonRota = jsonAx.getJSONArray("rotatable");
491
      int numRota = jsonRota.length();
492
      mLayerRotatable[i] = new boolean[numRota];
493
      for(int j=0; j<numRota; j++) mLayerRotatable[i][j] = jsonRota.getBoolean(j);
494

    
495
      mNumLayers[i] = numRota;
496
      }
497
    }
498

    
499
///////////////////////////////////////////////////////////////////////////////////////////////////
500

    
501
  private void parseQuats(JSONArray object) throws JSONException
502
    {
503
    int numQuats = object.length();
504
    mQuats = new Static4D[numQuats];
505

    
506
    for(int i=0; i<numQuats; i++)
507
      {
508
      JSONObject jsonQuat = object.getJSONObject(i);
509

    
510
      float x = (float)jsonQuat.getDouble("x");
511
      float y = (float)jsonQuat.getDouble("y");
512
      float z = (float)jsonQuat.getDouble("z");
513
      float w = (float)jsonQuat.getDouble("w");
514

    
515
      mQuats[i] = new Static4D(x,y,z,w);
516
      }
517
    }
518

    
519
///////////////////////////////////////////////////////////////////////////////////////////////////
520

    
521
  private void parseScrambling(JSONObject object) throws JSONException
522
    {
523
    mScrambleType = object.getInt("scrambleType");
524

    
525
    if( mScrambleType==0 )
526
      {
527
      JSONArray jsonStates = object.getJSONArray("scrambleStates");
528
      int numStates = jsonStates.length();
529
      mStates = new ScrambleState[numStates];
530

    
531
      for(int i=0; i<numStates; i++)
532
        {
533
        JSONArray jsonState = jsonStates.getJSONArray(i);
534
        int numAxis = jsonState.length();
535
        int[][] scramblingData = new int[numAxis][];
536

    
537
        for(int j=0; j<numAxis; j++)
538
          {
539
          JSONArray jsonData = jsonState.getJSONArray(j);
540
          int numData = jsonData.length();
541
          scramblingData[j] = new int[numData];
542
          for(int k=0; k<numData; k++) scramblingData[j][k] = jsonData.getInt(k);
543
          }
544

    
545
        mStates[i] = new ScrambleState(scramblingData);
546
        }
547
      }
548
    }
549

    
550
///////////////////////////////////////////////////////////////////////////////////////////////////
551

    
552
  private void parseTouchcontrol(JSONObject object) throws JSONException
553
    {
554
    mMovementType = object.getInt("movementType");
555
    mMovementSplit= object.getInt("movementSplit");
556

    
557
    try
558
      {
559
      JSONArray jsonEnabled = object.getJSONArray("enabledAxis");
560
      int numFace = jsonEnabled.length();
561

    
562
      mEnabled = new int[numFace][][];
563

    
564
      for(int i=0; i<numFace; i++)
565
        {
566
        JSONArray jsonSection = jsonEnabled.getJSONArray(i);
567
        int numSection = jsonSection.length();
568
        mEnabled[i] = new int[numSection][];
569

    
570
        for(int j=0; j<numSection; j++)
571
          {
572
          JSONArray jsonAx = jsonSection.getJSONArray(j);
573
          int numAxis = jsonAx.length();
574
          mEnabled[i][j] = new int[numAxis];
575
          for(int k=0; k<numAxis; k++) mEnabled[i][j][k] = jsonAx.getInt(k);
576
          }
577
        }
578
      }
579
    catch( JSONException ex )
580
      {
581
      // ignore, the object does not have to have 'enabledAxis' defined at all.
582
      }
583

    
584
    try
585
      {
586
      JSONArray jsonDist = object.getJSONArray("dist3D");
587
      int num = jsonDist.length();
588
      mDist3D = new float[num];
589
      for(int j=0; j<num; j++) mDist3D[j] = (float)jsonDist.getDouble(j);
590
      }
591
    catch( JSONException ex )
592
      {
593
      // ignore, the object does not have a 'dist3D' which is possible.
594
      }
595
    }
596

    
597
///////////////////////////////////////////////////////////////////////////////////////////////////
598

    
599
  private void parseColors(JSONArray object) throws JSONException
600
    {
601
    mNumFaceColors = object.length()-1;
602

    
603
    mColor = new int[mNumFaceColors];
604
    for(int i=0; i<mNumFaceColors; i++) mColor[i] = object.getInt(i);
605

    
606
    mInternalColor = object.getInt(mNumFaceColors);
607
    }
608

    
609
///////////////////////////////////////////////////////////////////////////////////////////////////
610

    
611
  private void parseSolved(JSONObject solved) throws JSONException
612
    {
613
    mSolvedFuncIndex = solved.getInt("functionIndex");
614

    
615
    try
616
      {
617
      JSONArray groupArray= solved.getJSONArray("groups");
618
      int numGroups = groupArray.length();
619
      mSolvedQuats  = new int[numGroups][];
620

    
621
      for(int i=0; i<numGroups; i++)
622
        {
623
        JSONArray groupElements = groupArray.getJSONArray(i);
624
        int groupSize = groupElements.length();
625
        mSolvedQuats[i] = new int[groupSize];
626
        for(int j=0; j<groupSize; j++) mSolvedQuats[i][j] = groupElements.getInt(j);
627
        }
628
      }
629
    catch( JSONException ex )
630
      {
631
      // ignore, the object does not have to have an array of solved groups.
632
      }
633
    }
634

    
635
///////////////////////////////////////////////////////////////////////////////////////////////////
636

    
637
  private void parseVersion1(JSONObject object) throws JSONException
638
    {
639
    JSONObject metadata    = object.getJSONObject("metadata");
640
    parseMetadata1(metadata);
641
    JSONObject mesh        = object.getJSONObject("mesh");
642
    parseMesh(mesh);
643
    JSONArray axis         = object.getJSONArray("axis");
644
    parseAxis1(axis);
645
    JSONArray quats        = object.getJSONArray("quats");
646
    parseQuats(quats);
647
    JSONObject scrambling  = object.getJSONObject("scrambling");
648
    parseScrambling(scrambling);
649
    JSONObject touchcontrol= object.getJSONObject("touchcontrol");
650
    parseTouchcontrol(touchcontrol);
651
    JSONArray colors       = object.getJSONArray("colors");
652
    parseColors(colors);
653
    JSONObject solved      = object.getJSONObject("solved");
654
    parseSolved(solved);
655
    }
656

    
657
///////////////////////////////////////////////////////////////////////////////////////////////////
658

    
659
  private void parseVersion2or3or4(JSONObject object) throws JSONException
660
    {
661
    JSONObject metadata    = object.getJSONObject("metadata");
662
    parseMetadata2or3or4(metadata);
663
    JSONObject mesh        = object.getJSONObject("mesh");
664
    parseMesh(mesh);
665
    JSONArray axis         = object.getJSONArray("axis");
666
    parseAxis2or3or4(axis);
667
    JSONArray quats        = object.getJSONArray("quats");
668
    parseQuats(quats);
669
    JSONObject scrambling  = object.getJSONObject("scrambling");
670
    parseScrambling(scrambling);
671
    JSONObject touchcontrol= object.getJSONObject("touchcontrol");
672
    parseTouchcontrol(touchcontrol);
673
    JSONArray colors       = object.getJSONArray("colors");
674
    parseColors(colors);
675
    JSONObject solved      = object.getJSONObject("solved");
676
    parseSolved(solved);
677
    }
678

    
679
///////////////////////////////////////////////////////////////////////////////////////////////////
680

    
681
  private void parseVersion1Tutorial(JSONObject object) throws JSONException
682
    {
683
    mTutorialObject = object.getString("object");
684
    JSONArray tuts= object.getJSONArray("tutorials");
685

    
686
    int len = tuts.length();
687
    mTutorials = new String[len][4];
688

    
689
    for(int i=0; i<len; i++)
690
      {
691
      JSONObject tut = tuts.getJSONObject(i);
692
      mTutorials[i][0] = tut.getString("language");
693
      mTutorials[i][1] = tut.getString("link");
694
      mTutorials[i][2] = tut.getString("title");
695
      mTutorials[i][3] = tut.getString("author");
696
      }
697
    }
698

    
699
///////////////////////////////////////////////////////////////////////////////////////////////////
700

    
701
  private void parseVersion1Metadata(JSONObject object) throws JSONException
702
    {
703
    JSONObject metadata = object.getJSONObject("metadata");
704
    parseMetadata1(metadata);
705
    }
706

    
707
///////////////////////////////////////////////////////////////////////////////////////////////////
708

    
709
  private void parseVersion2or3or4Metadata(JSONObject object) throws JSONException
710
    {
711
    JSONObject metadata = object.getJSONObject("metadata");
712
    parseMetadata2or3or4(metadata);
713
    }
714

    
715
///////////////////////////////////////////////////////////////////////////////////////////////////
716

    
717
  public void parseJsonFile(InputStream jsonStream)
718
    {
719
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
720

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

    
729
      JSONObject object = new JSONObject(contents.toString());
730
      int major = object.getInt("major");
731

    
732
      if( major==1 )
733
        {
734
        parseVersion1(object);
735
        }
736
      else if( major==2 || major==3 || major==4 )
737
        {
738
        parseVersion2or3or4(object);
739
        }
740
      else
741
        {
742
        android.util.Log.e("readJsonFile", "Unknown version "+major);
743
        }
744
      }
745
    catch(Exception e)
746
      {
747
      android.util.Log.e("readJsonFile", "Error reading JSON file: "+e.toString());
748
      }
749
    }
750

    
751
///////////////////////////////////////////////////////////////////////////////////////////////////
752

    
753
  public void parseJsonFileMetadata(InputStream jsonStream)
754
    {
755
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
756

    
757
    try
758
      {
759
      StringBuilder contents = new StringBuilder();
760
      String tmp;
761
      while( (tmp = br.readLine()) != null) contents.append(tmp);
762
      br.close();
763
      jsonStream.close();
764

    
765
      JSONObject object = new JSONObject(contents.toString());
766
      int major = object.getInt("major");
767

    
768
      if( major==1 )
769
        {
770
        parseVersion1Metadata(object);
771
        }
772
      else if( major==2 || major==3 || major==4 )
773
        {
774
        parseVersion2or3or4Metadata(object);
775
        }
776
      else
777
        {
778
        android.util.Log.e("readJsonFileQuick", "Unknown version "+major);
779
        }
780
      }
781
    catch(IOException e)
782
      {
783
      android.util.Log.e("readJsonFileQuick", "Error reading JSON file: "+e.toString());
784
      }
785
    catch( JSONException e )
786
      {
787
      android.util.Log.e("parseJsonFileQuick", "Error parsing JSON file: "+e.toString());
788
      }
789
    }
790

    
791
///////////////////////////////////////////////////////////////////////////////////////////////////
792

    
793
  public int readNumScrambles(Context context, String fileName)
794
    {
795
    File file = new File(context.getFilesDir(), fileName);
796
    InputStream stream;
797

    
798
    try
799
      {
800
      stream = new FileInputStream(file);
801
      BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
802

    
803
      StringBuilder contents = new StringBuilder();
804
      String tmp;
805
      while( (tmp = br.readLine()) != null) contents.append(tmp);
806
      br.close();
807
      stream.close();
808

    
809
      JSONObject object = new JSONObject(contents.toString());
810
      JSONObject metadata = object.getJSONObject("metadata");
811
      return metadata.getInt("scrambles");
812
      }
813
    catch(FileNotFoundException ex)
814
      {
815
      android.util.Log.e("readNumScrambles", "file "+fileName+" not found: "+ex.getMessage());
816
      }
817
    catch(IOException e)
818
      {
819
      android.util.Log.e("readNumScrambles", "Error reading JSON file: "+e.toString());
820
      }
821
    catch( JSONException e )
822
      {
823
      android.util.Log.e("readNumScrambles", "Error parsing JSON file: "+e.toString());
824
      }
825

    
826
    return 0;
827
    }
828

    
829
///////////////////////////////////////////////////////////////////////////////////////////////////
830

    
831
  public void parseJsonTutorial(InputStream jsonStream)
832
    {
833
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
834

    
835
    try
836
      {
837
      StringBuilder contents = new StringBuilder();
838
      String tmp;
839
      while( (tmp = br.readLine()) != null) contents.append(tmp);
840
      br.close();
841
      jsonStream.close();
842

    
843
      JSONObject object = new JSONObject(contents.toString());
844
      int major = object.getInt("major");
845

    
846
      if( major==1 )
847
        {
848
        parseVersion1Tutorial(object);
849
        }
850
      else
851
        {
852
        android.util.Log.e("readJsonFile", "Unknown tutorial version "+major);
853
        }
854
      }
855
    catch(IOException e)
856
      {
857
      android.util.Log.e("readJsonFile", "Error reading JSON file: "+e.toString());
858
      }
859
    catch( JSONException e )
860
      {
861
      android.util.Log.e("parseJsonFile", "Error parsing JSON file: "+e.toString());
862
      }
863
    }
864

    
865
///////////////////////////////////////////////////////////////////////////////////////////////////
866

    
867
  public ScrambleState[] getScrambleStates()
868
    {
869
    return mStates;
870
    }
871

    
872
///////////////////////////////////////////////////////////////////////////////////////////////////
873

    
874
  public int[][] getSolvedQuats()
875
    {
876
    return mSolvedQuats;
877
    }
878

    
879
///////////////////////////////////////////////////////////////////////////////////////////////////
880

    
881
  public Static4D[] getQuats()
882
    {
883
    return mQuats;
884
    }
885

    
886
///////////////////////////////////////////////////////////////////////////////////////////////////
887

    
888
  public int getSolvedFunctionIndex()
889
    {
890
    return mSolvedFuncIndex;
891
    }
892

    
893
///////////////////////////////////////////////////////////////////////////////////////////////////
894

    
895
  public int getNumStickerTypes()
896
    {
897
    return mNumStickerTypes;
898
    }
899

    
900
///////////////////////////////////////////////////////////////////////////////////////////////////
901

    
902
  public float[][] getCuts()
903
    {
904
    return mCuts;
905
    }
906

    
907
///////////////////////////////////////////////////////////////////////////////////////////////////
908

    
909
  public boolean[][] getLayerRotatable()
910
    {
911
    return mLayerRotatable;
912
    }
913

    
914
///////////////////////////////////////////////////////////////////////////////////////////////////
915

    
916
  public int getMovementType()
917
    {
918
    return mMovementType;
919
    }
920

    
921
///////////////////////////////////////////////////////////////////////////////////////////////////
922

    
923
  public int getMovementSplit()
924
    {
925
    return mMovementSplit;
926
    }
927

    
928
///////////////////////////////////////////////////////////////////////////////////////////////////
929

    
930
  public int[][][] getEnabled()
931
    {
932
    return mEnabled;
933
    }
934

    
935
///////////////////////////////////////////////////////////////////////////////////////////////////
936

    
937
  public float[] getDist3D()
938
    {
939
    return mDist3D;
940
    }
941

    
942
///////////////////////////////////////////////////////////////////////////////////////////////////
943

    
944
  public int getNumCubitFaces()
945
    {
946
    return mNumCubitFaces;
947
    }
948

    
949
///////////////////////////////////////////////////////////////////////////////////////////////////
950

    
951
  public float[][] getCubitPositions()
952
    {
953
    return mPositions;
954
    }
955

    
956
///////////////////////////////////////////////////////////////////////////////////////////////////
957

    
958
  public ObjectShape getObjectShape(int variant)
959
    {
960
    return mShapes[variant];
961
    }
962

    
963
///////////////////////////////////////////////////////////////////////////////////////////////////
964

    
965
  public ObjectFaceShape getObjectFaceShape(int variant)
966
    {
967
    return mFaceShapes[variant];
968
    }
969

    
970
///////////////////////////////////////////////////////////////////////////////////////////////////
971

    
972
  public Static4D getCubitQuats(int cubit)
973
    {
974
    return mCubitQuats[cubit];
975
    }
976

    
977
///////////////////////////////////////////////////////////////////////////////////////////////////
978

    
979
  public int getNumCubitVariants()
980
    {
981
    return mNumCubitVariants;
982
    }
983

    
984
///////////////////////////////////////////////////////////////////////////////////////////////////
985

    
986
  public int getCubitVariant(int cubit)
987
    {
988
    return mCubitVariant[cubit];
989
    }
990

    
991
///////////////////////////////////////////////////////////////////////////////////////////////////
992

    
993
  public int getVariantFaceColor(int variant, int face)
994
    {
995
    int[] colors = mVariantFaceColor[variant];
996
    return colors.length>face ? colors[face] : -1;
997
    }
998

    
999
///////////////////////////////////////////////////////////////////////////////////////////////////
1000

    
1001
  public int[] getCubitTypes()
1002
    {
1003
    return mCubitType;
1004
    }
1005

    
1006
///////////////////////////////////////////////////////////////////////////////////////////////////
1007

    
1008
  public float[][] getCubitOffsets()
1009
    {
1010
    return mCubitRowOffset;
1011
    }
1012

    
1013
///////////////////////////////////////////////////////////////////////////////////////////////////
1014

    
1015
  public int[][] getVariantFaceIsOuter()
1016
    {
1017
    return mVariantFaceIsOuter;
1018
    }
1019

    
1020
///////////////////////////////////////////////////////////////////////////////////////////////////
1021

    
1022
  public int getCubitFaceFace(int cubit, int face)
1023
    {
1024
    return mCubitFaceColor[cubit][face];
1025
    }
1026

    
1027
///////////////////////////////////////////////////////////////////////////////////////////////////
1028

    
1029
  public int getNumScrambles()
1030
    {
1031
    return mNumScrambles;
1032
    }
1033

    
1034
///////////////////////////////////////////////////////////////////////////////////////////////////
1035

    
1036
  public ObjectSticker retSticker(int sticker)
1037
    {
1038
    return mObjectSticker[sticker];
1039
    }
1040

    
1041
///////////////////////////////////////////////////////////////////////////////////////////////////
1042

    
1043
  public ObjectStickerOverride[] getStickerOverrides()
1044
    {
1045
    return mStickerOverrides;
1046
    }
1047

    
1048
///////////////////////////////////////////////////////////////////////////////////////////////////
1049

    
1050
  public Static3D[] getRotationAxis()
1051
    {
1052
    return mAxis;
1053
    }
1054

    
1055
///////////////////////////////////////////////////////////////////////////////////////////////////
1056

    
1057
  public int[][] getBasicAngle()
1058
    {
1059
    return mBasicAngle;
1060
    }
1061

    
1062
///////////////////////////////////////////////////////////////////////////////////////////////////
1063

    
1064
  public ObjectSignature getSignature()
1065
    {
1066
    return mSignature;
1067
    }
1068

    
1069
///////////////////////////////////////////////////////////////////////////////////////////////////
1070

    
1071
  public String getObjectName()
1072
    {
1073
    return mLongName;
1074
    }
1075

    
1076
///////////////////////////////////////////////////////////////////////////////////////////////////
1077

    
1078
  public String getShortName()
1079
    {
1080
    return mShortName;
1081
    }
1082

    
1083
///////////////////////////////////////////////////////////////////////////////////////////////////
1084

    
1085
  public String getInventor()
1086
    {
1087
    return mInventor;
1088
    }
1089

    
1090
///////////////////////////////////////////////////////////////////////////////////////////////////
1091

    
1092
  public int getYearOfInvention()
1093
    {
1094
    return mYearOfInvention;
1095
    }
1096

    
1097
///////////////////////////////////////////////////////////////////////////////////////////////////
1098

    
1099
  public int getComplexity()
1100
    {
1101
    return mComplexity;
1102
    }
1103

    
1104
///////////////////////////////////////////////////////////////////////////////////////////////////
1105

    
1106
  public int getNumFaces()
1107
    {
1108
    return mNumFaces;
1109
    }
1110

    
1111
///////////////////////////////////////////////////////////////////////////////////////////////////
1112

    
1113
  public int getNumFaceColors()
1114
    {
1115
    return mNumFaceColors;
1116
    }
1117

    
1118
///////////////////////////////////////////////////////////////////////////////////////////////////
1119

    
1120
  public int[] getNumLayers()
1121
    {
1122
    return mNumLayers;
1123
    }
1124

    
1125
///////////////////////////////////////////////////////////////////////////////////////////////////
1126

    
1127
  public float getSize()
1128
    {
1129
    return mSize;
1130
    }
1131

    
1132
///////////////////////////////////////////////////////////////////////////////////////////////////
1133

    
1134
  public int getScrambleType()
1135
    {
1136
    return mScrambleType;
1137
    }
1138

    
1139
///////////////////////////////////////////////////////////////////////////////////////////////////
1140

    
1141
  public int getColor(int face)
1142
    {
1143
    return mColor[face];
1144
    }
1145

    
1146
///////////////////////////////////////////////////////////////////////////////////////////////////
1147

    
1148
  public int getInternalColor()
1149
    {
1150
    return mInternalColor;
1151
    }
1152

    
1153
///////////////////////////////////////////////////////////////////////////////////////////////////
1154

    
1155
  public boolean shouldResetTextureMaps()
1156
    {
1157
    return mResetMaps;
1158
    }
1159

    
1160
///////////////////////////////////////////////////////////////////////////////////////////////////
1161

    
1162
  public String getTutorialObject()
1163
    {
1164
    return mTutorialObject;
1165
    }
1166

    
1167
///////////////////////////////////////////////////////////////////////////////////////////////////
1168

    
1169
  public String[][] getTutorials()
1170
    {
1171
    return mTutorials;
1172
    }
1173

    
1174
}
(1-1/2)