Project

General

Profile

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

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

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(IOException e)
746
      {
747
      android.util.Log.e("readJsonFile", "Error reading JSON file: "+e.toString());
748
      }
749
    catch( JSONException e )
750
      {
751
      android.util.Log.e("parseJsonFile", "Error parsing JSON file: "+e.toString());
752
      }
753
    }
754

    
755
///////////////////////////////////////////////////////////////////////////////////////////////////
756

    
757
  public void parseJsonFileMetadata(InputStream jsonStream)
758
    {
759
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
760

    
761
    try
762
      {
763
      StringBuilder contents = new StringBuilder();
764
      String tmp;
765
      while( (tmp = br.readLine()) != null) contents.append(tmp);
766
      br.close();
767
      jsonStream.close();
768

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

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

    
795
///////////////////////////////////////////////////////////////////////////////////////////////////
796

    
797
  public int readNumScrambles(Context context, String fileName)
798
    {
799
    File file = new File(context.getFilesDir(), fileName);
800
    InputStream stream;
801

    
802
    try
803
      {
804
      stream = new FileInputStream(file);
805
      BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
806

    
807
      StringBuilder contents = new StringBuilder();
808
      String tmp;
809
      while( (tmp = br.readLine()) != null) contents.append(tmp);
810
      br.close();
811
      stream.close();
812

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

    
830
    return 0;
831
    }
832

    
833
///////////////////////////////////////////////////////////////////////////////////////////////////
834

    
835
  public void parseJsonTutorial(InputStream jsonStream)
836
    {
837
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
838

    
839
    try
840
      {
841
      StringBuilder contents = new StringBuilder();
842
      String tmp;
843
      while( (tmp = br.readLine()) != null) contents.append(tmp);
844
      br.close();
845
      jsonStream.close();
846

    
847
      JSONObject object = new JSONObject(contents.toString());
848
      int major = object.getInt("major");
849

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

    
869
///////////////////////////////////////////////////////////////////////////////////////////////////
870

    
871
  public ScrambleState[] getScrambleStates()
872
    {
873
    return mStates;
874
    }
875

    
876
///////////////////////////////////////////////////////////////////////////////////////////////////
877

    
878
  public int[][] getSolvedQuats()
879
    {
880
    return mSolvedQuats;
881
    }
882

    
883
///////////////////////////////////////////////////////////////////////////////////////////////////
884

    
885
  public Static4D[] getQuats()
886
    {
887
    return mQuats;
888
    }
889

    
890
///////////////////////////////////////////////////////////////////////////////////////////////////
891

    
892
  public int getSolvedFunctionIndex()
893
    {
894
    return mSolvedFuncIndex;
895
    }
896

    
897
///////////////////////////////////////////////////////////////////////////////////////////////////
898

    
899
  public int getNumStickerTypes()
900
    {
901
    return mNumStickerTypes;
902
    }
903

    
904
///////////////////////////////////////////////////////////////////////////////////////////////////
905

    
906
  public float[][] getCuts()
907
    {
908
    return mCuts;
909
    }
910

    
911
///////////////////////////////////////////////////////////////////////////////////////////////////
912

    
913
  public boolean[][] getLayerRotatable()
914
    {
915
    return mLayerRotatable;
916
    }
917

    
918
///////////////////////////////////////////////////////////////////////////////////////////////////
919

    
920
  public int getMovementType()
921
    {
922
    return mMovementType;
923
    }
924

    
925
///////////////////////////////////////////////////////////////////////////////////////////////////
926

    
927
  public int getMovementSplit()
928
    {
929
    return mMovementSplit;
930
    }
931

    
932
///////////////////////////////////////////////////////////////////////////////////////////////////
933

    
934
  public int[][][] getEnabled()
935
    {
936
    return mEnabled;
937
    }
938

    
939
///////////////////////////////////////////////////////////////////////////////////////////////////
940

    
941
  public float[] getDist3D()
942
    {
943
    return mDist3D;
944
    }
945

    
946
///////////////////////////////////////////////////////////////////////////////////////////////////
947

    
948
  public int getNumCubitFaces()
949
    {
950
    return mNumCubitFaces;
951
    }
952

    
953
///////////////////////////////////////////////////////////////////////////////////////////////////
954

    
955
  public float[][] getCubitPositions()
956
    {
957
    return mPositions;
958
    }
959

    
960
///////////////////////////////////////////////////////////////////////////////////////////////////
961

    
962
  public ObjectShape getObjectShape(int variant)
963
    {
964
    return mShapes[variant];
965
    }
966

    
967
///////////////////////////////////////////////////////////////////////////////////////////////////
968

    
969
  public ObjectFaceShape getObjectFaceShape(int variant)
970
    {
971
    return mFaceShapes[variant];
972
    }
973

    
974
///////////////////////////////////////////////////////////////////////////////////////////////////
975

    
976
  public Static4D getCubitQuats(int cubit)
977
    {
978
    return mCubitQuats[cubit];
979
    }
980

    
981
///////////////////////////////////////////////////////////////////////////////////////////////////
982

    
983
  public int getNumCubitVariants()
984
    {
985
    return mNumCubitVariants;
986
    }
987

    
988
///////////////////////////////////////////////////////////////////////////////////////////////////
989

    
990
  public int getCubitVariant(int cubit)
991
    {
992
    return mCubitVariant[cubit];
993
    }
994

    
995
///////////////////////////////////////////////////////////////////////////////////////////////////
996

    
997
  public int getVariantFaceColor(int variant, int face)
998
    {
999
    int[] colors = mVariantFaceColor[variant];
1000
    return colors.length>face ? colors[face] : -1;
1001
    }
1002

    
1003
///////////////////////////////////////////////////////////////////////////////////////////////////
1004

    
1005
  public int[] getCubitTypes()
1006
    {
1007
    return mCubitType;
1008
    }
1009

    
1010
///////////////////////////////////////////////////////////////////////////////////////////////////
1011

    
1012
  public float[][] getCubitOffsets()
1013
    {
1014
    return mCubitRowOffset;
1015
    }
1016

    
1017
///////////////////////////////////////////////////////////////////////////////////////////////////
1018

    
1019
  public int[][] getVariantFaceIsOuter()
1020
    {
1021
    return mVariantFaceIsOuter;
1022
    }
1023

    
1024
///////////////////////////////////////////////////////////////////////////////////////////////////
1025

    
1026
  public int getCubitFaceFace(int cubit, int face)
1027
    {
1028
    return mCubitFaceColor[cubit][face];
1029
    }
1030

    
1031
///////////////////////////////////////////////////////////////////////////////////////////////////
1032

    
1033
  public int getNumScrambles()
1034
    {
1035
    return mNumScrambles;
1036
    }
1037

    
1038
///////////////////////////////////////////////////////////////////////////////////////////////////
1039

    
1040
  public ObjectSticker retSticker(int sticker)
1041
    {
1042
    return mObjectSticker[sticker];
1043
    }
1044

    
1045
///////////////////////////////////////////////////////////////////////////////////////////////////
1046

    
1047
  public ObjectStickerOverride[] getStickerOverrides()
1048
    {
1049
    return mStickerOverrides;
1050
    }
1051

    
1052
///////////////////////////////////////////////////////////////////////////////////////////////////
1053

    
1054
  public Static3D[] getRotationAxis()
1055
    {
1056
    return mAxis;
1057
    }
1058

    
1059
///////////////////////////////////////////////////////////////////////////////////////////////////
1060

    
1061
  public int[][] getBasicAngle()
1062
    {
1063
    return mBasicAngle;
1064
    }
1065

    
1066
///////////////////////////////////////////////////////////////////////////////////////////////////
1067

    
1068
  public ObjectSignature getSignature()
1069
    {
1070
    return mSignature;
1071
    }
1072

    
1073
///////////////////////////////////////////////////////////////////////////////////////////////////
1074

    
1075
  public String getObjectName()
1076
    {
1077
    return mLongName;
1078
    }
1079

    
1080
///////////////////////////////////////////////////////////////////////////////////////////////////
1081

    
1082
  public String getShortName()
1083
    {
1084
    return mShortName;
1085
    }
1086

    
1087
///////////////////////////////////////////////////////////////////////////////////////////////////
1088

    
1089
  public String getInventor()
1090
    {
1091
    return mInventor;
1092
    }
1093

    
1094
///////////////////////////////////////////////////////////////////////////////////////////////////
1095

    
1096
  public int getYearOfInvention()
1097
    {
1098
    return mYearOfInvention;
1099
    }
1100

    
1101
///////////////////////////////////////////////////////////////////////////////////////////////////
1102

    
1103
  public int getComplexity()
1104
    {
1105
    return mComplexity;
1106
    }
1107

    
1108
///////////////////////////////////////////////////////////////////////////////////////////////////
1109

    
1110
  public int getNumFaces()
1111
    {
1112
    return mNumFaces;
1113
    }
1114

    
1115
///////////////////////////////////////////////////////////////////////////////////////////////////
1116

    
1117
  public int getNumFaceColors()
1118
    {
1119
    return mNumFaceColors;
1120
    }
1121

    
1122
///////////////////////////////////////////////////////////////////////////////////////////////////
1123

    
1124
  public int[] getNumLayers()
1125
    {
1126
    return mNumLayers;
1127
    }
1128

    
1129
///////////////////////////////////////////////////////////////////////////////////////////////////
1130

    
1131
  public float getSize()
1132
    {
1133
    return mSize;
1134
    }
1135

    
1136
///////////////////////////////////////////////////////////////////////////////////////////////////
1137

    
1138
  public int getScrambleType()
1139
    {
1140
    return mScrambleType;
1141
    }
1142

    
1143
///////////////////////////////////////////////////////////////////////////////////////////////////
1144

    
1145
  public int getColor(int face)
1146
    {
1147
    return mColor[face];
1148
    }
1149

    
1150
///////////////////////////////////////////////////////////////////////////////////////////////////
1151

    
1152
  public int getInternalColor()
1153
    {
1154
    return mInternalColor;
1155
    }
1156

    
1157
///////////////////////////////////////////////////////////////////////////////////////////////////
1158

    
1159
  public boolean shouldResetTextureMaps()
1160
    {
1161
    return mResetMaps;
1162
    }
1163

    
1164
///////////////////////////////////////////////////////////////////////////////////////////////////
1165

    
1166
  public String getTutorialObject()
1167
    {
1168
    return mTutorialObject;
1169
    }
1170

    
1171
///////////////////////////////////////////////////////////////////////////////////////////////////
1172

    
1173
  public String[][] getTutorials()
1174
    {
1175
    return mTutorials;
1176
    }
1177

    
1178
}
(1-1/2)