Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.objectlib.json;
11

    
12
import java.io.BufferedReader;
13
import java.io.File;
14
import java.io.FileInputStream;
15
import java.io.IOException;
16
import java.io.InputStream;
17
import java.io.InputStreamReader;
18
import java.nio.charset.StandardCharsets;
19

    
20
import android.content.Context;
21

    
22
import org.distorted.objectlib.helpers.ObjectFaceShape;
23
import org.distorted.objectlib.helpers.ObjectSignature;
24
import org.distorted.objectlib.helpers.ObjectStickerOverride;
25
import org.distorted.objectlib.helpers.ObjectVertexEffects;
26
import org.distorted.objectlib.main.Cubit;
27
import org.distorted.objectlib.objects.TwistyBandagedCuboid;
28
import org.distorted.objectlib.scrambling.ScrambleEdgeGenerator;
29
import org.json.JSONArray;
30
import org.json.JSONException;
31
import org.json.JSONObject;
32

    
33
import org.distorted.library.type.Static3D;
34
import org.distorted.library.type.Static4D;
35

    
36
import org.distorted.objectlib.helpers.ObjectShape;
37
import org.distorted.objectlib.helpers.ObjectSticker;
38
import org.distorted.objectlib.main.ObjectType;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
public class JsonReader
43
{
44
  private int[][] mAlgorithms;
45
  private int[][] mEdges;
46
  private int[][] mSolvedQuats;
47
  private Static4D[] mQuats;
48
  private int mSolvedFuncIndex;
49
  private int mNumStickerTypes;
50
  private float[][] mCuts;
51
  private boolean[][] mLayerRotatable;
52
  private int mMovementType, mMovementSplit;
53
  private int[][][] mEnabled;
54
  private float[] mDist3D;
55
  private int mNumCubitFaces, mNumFaces, mNumFaceColors;
56
  private float[][] mPositions;
57
  private ObjectShape[] mShapes;
58
  private ObjectFaceShape[] mFaceShapes;
59
  private ObjectVertexEffects[] mVertexEffects;
60
  private Static4D[] mCubitQuats;
61
  private int mNumCubitVariants;
62
  private int[] mCubitVariant;
63
  private int[][] mVariantStickerShape, mVariantFaceIsOuter, mCubitFaceColor;
64
  private ObjectSticker[] mObjectSticker;
65
  private Static3D[] mAxis;
66
  private int[][] mBasicAngle;
67
  private String mLongName, mShortName, mInventor;
68
  private int mYearOfInvention, mComplexity;
69
  private int[] mNumLayers;
70
  private float mSize;
71
  private int mScrambleType, mNumScrambles;
72
  private int mPrice;
73
  private int[] mColor;
74
  private int mInternalColor;
75
  private boolean mResetMaps;
76
  private String mTutorialObject;
77
  private String[][] mTutorials;
78
  private ObjectSignature mSignature;
79
  private int[] mCubitType;
80
  private float[][] mCubitRowOffset;
81
  private ObjectStickerOverride[] mStickerOverrides;
82
  private float mPillowCoeff;
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
  private void parseMetadata(JSONObject metadata) throws JSONException
87
    {
88
    mLongName        = metadata.getString("longname");
89
    mShortName       = metadata.getString("shortname");
90
    mInventor        = metadata.getString("inventor");
91
    mYearOfInvention = metadata.getInt("year");
92
    mComplexity      = metadata.getInt("complexity");
93
    mSize            = (float)metadata.getDouble("size");
94
    mNumScrambles    = metadata.getInt("scrambles");
95
    mResetMaps       = metadata.getBoolean("resetmaps");
96
    mNumFaces        = metadata.getInt("num_faces");
97

    
98
    try
99
      {
100
      JSONArray sigArray = metadata.getJSONArray("signature");
101
      int size = sigArray.length();
102
      long[] signature = new long[size];
103
      for(int i=0; i<size; i++) signature[i] = sigArray.getLong(i);
104

    
105
      switch( TwistyBandagedCuboid.getType(mShortName,mLongName) )
106
        {
107
        case 0: mSignature = new ObjectSignature(signature); break;
108
        case 1: mSignature = new ObjectSignature(mShortName,signature); break;
109
        case 2: mSignature = new ObjectSignature("333",signature); break;
110
        }
111
      }
112
    catch(JSONException ex)
113
      {
114
      long signature = metadata.getLong("signature");
115
      mSignature = new ObjectSignature(signature);
116
      }
117

    
118
    boolean free = metadata.optBoolean("free", true);
119

    
120
    if( free ) mPrice = 0;
121
    else mPrice = metadata.optInt("price", ObjectType.DEFAULT_PRICE_OF_OLD_OBJECTS );
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
  private void parseCubits(JSONArray object) throws JSONException
127
    {
128
    int numCubits = object.length();
129

    
130
    mCubitQuats     = new Static4D[numCubits];
131
    mCubitVariant   = new int[numCubits];
132
    mPositions      = new float[numCubits][];
133
    mCubitFaceColor = new int[numCubits][];
134
    mCubitType      = new int[numCubits];
135
    mCubitRowOffset = new float[numCubits][];
136

    
137
    for(int i=0; i<numCubits; i++)
138
      {
139
      JSONObject jsonCubit = object.getJSONObject(i);
140

    
141
      float qx = (float)jsonCubit.getDouble("qx");
142
      float qy = (float)jsonCubit.getDouble("qy");
143
      float qz = (float)jsonCubit.getDouble("qz");
144
      float qw = (float)jsonCubit.getDouble("qw");
145

    
146
      mCubitQuats[i] = new Static4D(qx,qy,qz,qw);
147
      mCubitVariant[i] = jsonCubit.getInt("variant");
148

    
149
      JSONArray jsonCenter = jsonCubit.getJSONArray("centers");
150
      int numCenter = jsonCenter.length();
151
      mPositions[i] = new float[numCenter];
152
      for(int j=0; j<numCenter; j++) mPositions[i][j] = (float)jsonCenter.getDouble(j);
153

    
154
      JSONArray jsonColor  = jsonCubit.getJSONArray("colors");
155
      int numColor = jsonColor.length();
156
      mCubitFaceColor[i] = new int[numColor];
157
      for(int j=0; j<numColor; j++) mCubitFaceColor[i][j] = jsonColor.getInt(j);
158

    
159
      mCubitType[i] = jsonCubit.optInt("type", Cubit.TYPE_NORMAL);
160
      float xoff = (float)jsonCubit.optDouble("offsetX", 0 );
161
      float yoff = (float)jsonCubit.optDouble("offsetY", 0 );
162
      float zoff = (float)jsonCubit.optDouble("offsetZ", 0 );
163
      mCubitRowOffset[i] = new float[] {xoff,yoff,zoff};
164
      }
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
  private void parseShapes(JSONArray object) throws JSONException
170
    {
171
    mNumCubitVariants     = object.length();
172
    mVariantStickerShape  = new int[mNumCubitVariants][];
173
    mVariantFaceIsOuter   = new int[mNumCubitVariants][];
174
    mShapes               = new ObjectShape[mNumCubitVariants];
175
    mFaceShapes           = new ObjectFaceShape[mNumCubitVariants];
176
    mVertexEffects        = new ObjectVertexEffects[mNumCubitVariants];
177
    float[][][] verts     = new float[mNumCubitVariants][][];
178
    float[][][] bands     = new float[mNumCubitVariants][][];
179
    float[][] convexity   = new float[mNumCubitVariants][];
180
    int[][] bandIndices   = new int[mNumCubitVariants][];
181
    int[][][] vertIndices = new int[mNumCubitVariants][][];
182

    
183
    mNumCubitFaces = -1;
184

    
185
    for(int i=0; i<mNumCubitVariants; i++)
186
      {
187
      JSONObject jsonShape = object.getJSONObject(i);
188

    
189
      ////// vertices /////////////////////////////////////////////////
190
      JSONArray jsonVertices= jsonShape.getJSONArray("vertices");
191
      int numVertices = jsonVertices.length();
192
      verts[i] = new float[numVertices][3];
193

    
194
      for(int j=0; j<numVertices; j++)
195
        {
196
        JSONObject vert = jsonVertices.getJSONObject(j);
197
        verts[i][j][0] = (float)vert.getDouble("x");
198
        verts[i][j][1] = (float)vert.getDouble("y");
199
        verts[i][j][2] = (float)vert.getDouble("z");
200
        }
201

    
202
      ////// faces ////////////////////////////////////////////////////
203
      JSONArray jsonFaces= jsonShape.getJSONArray("faces");
204
      int numFaces = jsonFaces.length();
205
      mVariantStickerShape[i]= new int[numFaces];
206
      mVariantFaceIsOuter[i] = new int[numFaces];
207
      bandIndices[i] = new int[numFaces];
208
      vertIndices[i] = new int[numFaces][];
209

    
210
      if( mNumCubitFaces<numFaces ) mNumCubitFaces=numFaces;
211

    
212
      for(int j=0; j<numFaces; j++)
213
        {
214
        JSONObject jsonFace = jsonFaces.getJSONObject(j);
215
        mVariantStickerShape[i][j]= jsonFace.getInt("sticker");
216
        mVariantFaceIsOuter[i][j] = jsonFace.optInt("isOuter",0);
217
        bandIndices[i][j] = jsonFace.getInt("bandIndex");
218
        JSONArray vertices = jsonFace.getJSONArray("vertexIndices");
219
        int numV = vertices.length();
220
        vertIndices[i][j] = new int[numV];
221
        for(int k=0; k<numV; k++) vertIndices[i][j][k] = vertices.getInt(k);
222
        }
223

    
224
      ////// bands ////////////////////////////////////////////////////
225
      JSONArray jsonBands= jsonShape.getJSONArray("bands");
226
      int numBands = jsonBands.length();
227
      bands[i] = new float[numBands][7];
228

    
229
      for(int j=0; j<numBands; j++)
230
        {
231
        JSONObject jsonBand = jsonBands.getJSONObject(j);
232

    
233
        bands[i][j][0] = (float)jsonBand.getDouble("height");
234
        bands[i][j][1] = (float)jsonBand.getDouble("angle");
235
        bands[i][j][2] = (float)jsonBand.getDouble("distanceToCenter");
236
        bands[i][j][3] = (float)jsonBand.getDouble("distanceToFlat");
237
        bands[i][j][4] = (float)jsonBand.getDouble("numOfBands");
238
        bands[i][j][5] = (float)jsonBand.getDouble("extraI");
239
        bands[i][j][6] = (float)jsonBand.getDouble("extraJ");
240
        }
241

    
242
      ////// convexity ///////////////////////////////////////////////
243
      JSONObject jsonConvexity = jsonShape.optJSONObject("convexity");
244

    
245
      if( jsonConvexity!=null )
246
        {
247
        convexity[i] = new float[3];
248
        convexity[i][0] = (float)jsonConvexity.getDouble("x");
249
        convexity[i][1] = (float)jsonConvexity.getDouble("y");
250
        convexity[i][2] = (float)jsonConvexity.getDouble("z");
251
        }
252

    
253
      ////// effects /////////////////////////////////////////////////
254
      JSONArray jsonEffects = jsonShape.optJSONArray("effects");
255

    
256
      if( jsonEffects!=null )
257
        {
258
        int numEffects = jsonEffects.length();
259

    
260
        String[] name    = new String[numEffects];
261
        float[][] vars   = new float[numEffects][5];
262
        float[][] center = new float[numEffects][3];
263
        float[][] region = new float[numEffects][4];
264
        boolean[] use    = new boolean[numEffects];
265

    
266
        for(int j=0; j<numEffects; j++)
267
          {
268
          JSONObject jsonEffect = jsonEffects.getJSONObject(j);
269

    
270
          name[j] = jsonEffect.getString("name");
271

    
272
          vars[j][0] = (float)jsonEffect.getDouble("var0");
273
          vars[j][1] = (float)jsonEffect.getDouble("var1");
274
          vars[j][2] = (float)jsonEffect.getDouble("var2");
275
          vars[j][3] = (float)jsonEffect.getDouble("var3");
276
          vars[j][4] = (float)jsonEffect.getDouble("var4");
277

    
278
          center[j][0] = (float)jsonEffect.getDouble("center0");
279
          center[j][1] = (float)jsonEffect.getDouble("center1");
280
          center[j][2] = (float)jsonEffect.getDouble("center2");
281

    
282
          region[j][0] = (float)jsonEffect.getDouble("region0");
283
          region[j][1] = (float)jsonEffect.getDouble("region1");
284
          region[j][2] = (float)jsonEffect.getDouble("region2");
285
          region[j][3] = (float)jsonEffect.getDouble("region3");
286

    
287
          use[j] = jsonEffect.getBoolean("use");
288
          }
289

    
290
        mVertexEffects[i] = new ObjectVertexEffects(name,vars,center,region,use);
291
        }
292
      }
293

    
294
    for(int i=0; i<mNumCubitVariants; i++)
295
      {
296
      mShapes[i] = new ObjectShape(verts[i],vertIndices[i]);
297
      mFaceShapes[i] = new ObjectFaceShape(bands[i],bandIndices[i],convexity[i]);
298
      }
299
    }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

    
303
  private void parseStickers(JSONArray object) throws JSONException
304
    {
305
    mNumStickerTypes = object.length();
306
    mObjectSticker = new ObjectSticker[mNumStickerTypes];
307

    
308
    for(int i=0; i<mNumStickerTypes; i++)
309
      {
310
      JSONObject sticker = object.getJSONObject(i);
311
      float stroke = (float)sticker.getDouble("stroke");
312
      JSONArray vertices = sticker.getJSONArray("vertices");
313
      int numVertices = vertices.length();
314

    
315
      float[] coords     = new float[2*numVertices];
316
      float[] curvatures = new float[numVertices];
317
      float[] radii      = new float[numVertices];
318

    
319
      for(int j=0; j<numVertices; j++)
320
        {
321
        JSONObject vertex = vertices.getJSONObject(j);
322

    
323
        coords[2*j  ] = (float)vertex.getDouble("x");
324
        coords[2*j+1] = (float)vertex.getDouble("y");
325
        curvatures[j] = (float)vertex.getDouble("angle");
326
        radii[j]      = (float)vertex.getDouble("radius");
327
        }
328

    
329
      mObjectSticker[i] = new ObjectSticker(coords,curvatures,radii,stroke);
330
      }
331
    }
332

    
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334

    
335
  private void parseOverrides(JSONArray object) throws JSONException
336
    {
337
    if( object!=null )
338
      {
339
      int numOverrides = object.length();
340
      mStickerOverrides= new ObjectStickerOverride[numOverrides];
341

    
342
      for(int i=0; i<numOverrides; i++)
343
        {
344
        JSONObject override  = object.getJSONObject(i);
345
        JSONArray cubitArray = override.getJSONArray("cubitfaces");
346
        int color = override.getInt("color");
347
        int numCubits = cubitArray.length();
348
        int[] cubitface = new int[numCubits];
349
        for(int j=0; j<numCubits; j++) cubitface[j] = cubitArray.getInt(j);
350
        mStickerOverrides[i] = new ObjectStickerOverride(cubitface,color);
351
        }
352
      }
353
    else
354
      {
355
      mStickerOverrides = null;
356
      }
357
    }
358

    
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360

    
361
  private void parseMesh(JSONObject object) throws JSONException
362
    {
363
    JSONArray cubits   = object.getJSONArray("cubits");
364
    parseCubits(cubits);
365
    JSONArray shapes   = object.getJSONArray("shapes");
366
    parseShapes(shapes);
367
    JSONArray stickers = object.getJSONArray("stickers");
368
    parseStickers(stickers);
369

    
370
    JSONArray overrides= object.optJSONArray("overrides");
371
    parseOverrides(overrides);
372

    
373
    mPillowCoeff = (float)object.optDouble("pillow",1.0);
374
    }
375

    
376
///////////////////////////////////////////////////////////////////////////////////////////////////
377

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

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

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

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

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

    
398
      JSONArray jsonAngles = jsonAx.getJSONArray("basicAngles");
399
      int numAngles = jsonAngles.length();
400
      mBasicAngle[i] = new int[numAngles];
401
      for(int j=0; j<numAngles; j++) mBasicAngle[i][j] = jsonAngles.getInt(j);
402

    
403
      JSONArray jsonCuts = jsonAx.getJSONArray("cuts");
404
      int numCuts = jsonCuts.length();
405
      mCuts[i] = new float[numCuts];
406
      for(int j=0; j<numCuts; j++) mCuts[i][j] = (float)jsonCuts.getDouble(j);
407

    
408
      JSONArray jsonRota = jsonAx.getJSONArray("rotatable");
409
      int numRota = jsonRota.length();
410
      mLayerRotatable[i] = new boolean[numRota];
411
      for(int j=0; j<numRota; j++) mLayerRotatable[i][j] = jsonRota.getBoolean(j);
412

    
413
      mNumLayers[i] = numRota;
414
      }
415
    }
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418

    
419
  private void parseQuats(JSONArray object) throws JSONException
420
    {
421
    int numQuats = object.length();
422
    mQuats = new Static4D[numQuats];
423

    
424
    for(int i=0; i<numQuats; i++)
425
      {
426
      JSONObject jsonQuat = object.getJSONObject(i);
427

    
428
      float x = (float)jsonQuat.getDouble("x");
429
      float y = (float)jsonQuat.getDouble("y");
430
      float z = (float)jsonQuat.getDouble("z");
431
      float w = (float)jsonQuat.getDouble("w");
432

    
433
      mQuats[i] = new Static4D(x,y,z,w);
434
      }
435
    }
436

    
437
///////////////////////////////////////////////////////////////////////////////////////////////////
438

    
439
  private int getAlgorithmIndex(int ax, int layer, int angle)
440
    {
441
    int numAlgs = mAlgorithms.length;
442

    
443
    for(int alg=0; alg<numAlgs; alg++)
444
      {
445
      int[] a = mAlgorithms[alg];
446
      if( a.length>=3 && a[0]==ax && a[1]==layer && a[2]==angle ) return alg;
447
      }
448

    
449
    return -1;
450
    }
451

    
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453

    
454
  private int[] produceEdge(int[][] scramblingData)
455
    {
456
    int index=0, length=0;
457
    int numAxis = scramblingData.length;
458
    for (int[] scramblingDatum : scramblingData) length += scramblingDatum.length;
459

    
460
    int[] ret = new int[2*length/3];
461

    
462
    for(int ax=0; ax<numAxis; ax++)
463
      {
464
      int[] data = scramblingData[ax];
465
      int num = data.length/3;
466

    
467
      for(int j=0; j<num; j++)
468
        {
469
        int layer = data[3*j];
470
        int angle = data[3*j+1];
471
        int state = data[3*j+2];
472

    
473
        ret[2*index  ] = getAlgorithmIndex(ax,layer,angle);
474
        ret[2*index+1] = state;
475
        index++;
476
        }
477
      }
478

    
479
    return ret;
480
    }
481

    
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483

    
484
  private void parseScrambling6(JSONObject object) throws JSONException
485
    {
486
    JSONArray jsonStates = object.getJSONArray("scrambleStates");
487
    int numStates = jsonStates.length();
488
    int[][][] scramblingData = new int[numStates][][];
489

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

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

    
505
    mAlgorithms = ScrambleEdgeGenerator.getScramblingAlgorithms(mBasicAngle);
506
    mEdges = new int[numStates][];
507
    for(int i=0; i<numStates; i++) mEdges[i] = produceEdge(scramblingData[i]);
508
    }
509

    
510
///////////////////////////////////////////////////////////////////////////////////////////////////
511

    
512
  private void parseScrambling7orMore(JSONObject object) throws JSONException
513
    {
514
    JSONArray jsonAlgorithms = object.getJSONArray("algorithms");
515
    int numAlgs = jsonAlgorithms.length();
516
    mAlgorithms = new int[numAlgs][];
517

    
518
    for(int i=0; i<numAlgs; i++)
519
      {
520
      JSONArray jsonAlg = jsonAlgorithms.getJSONArray(i);
521
      int numEntries = jsonAlg.length();
522
      mAlgorithms[i] = new int[numEntries];
523
      for(int j=0; j<numEntries; j++) mAlgorithms[i][j] = jsonAlg.getInt(j);
524
      }
525

    
526
    JSONArray jsonEdges = object.getJSONArray("edges");
527
    int numEdges = jsonEdges.length();
528
    mEdges = new int[numEdges][];
529

    
530
    for(int i=0; i<numEdges; i++)
531
      {
532
      JSONArray jsonEdge = jsonEdges.getJSONArray(i);
533
      int numEntries = jsonEdge.length();
534
      mEdges[i] = new int[numEntries];
535
      for(int j=0; j<numEntries; j++) mEdges[i][j] = jsonEdge.getInt(j);
536
      }
537
    }
538

    
539
///////////////////////////////////////////////////////////////////////////////////////////////////
540

    
541
  private void parseScrambling(JSONObject object, int major) throws JSONException
542
    {
543
    mScrambleType = object.getInt("scrambleType");
544

    
545
    if( mScrambleType==0 )
546
      {
547
      if( major==6 ) parseScrambling6(object);
548
      else           parseScrambling7orMore(object);
549
      }
550
    }
551

    
552
///////////////////////////////////////////////////////////////////////////////////////////////////
553

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

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

    
564
      mEnabled = new int[numFace][][];
565

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

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

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

    
599
///////////////////////////////////////////////////////////////////////////////////////////////////
600

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

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

    
608
    mInternalColor = object.getInt(mNumFaceColors);
609
    }
610

    
611
///////////////////////////////////////////////////////////////////////////////////////////////////
612

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

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

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

    
637
///////////////////////////////////////////////////////////////////////////////////////////////////
638

    
639
  private void parseFile(JSONObject object, int major) throws JSONException
640
    {
641
    JSONObject metadata    = object.getJSONObject("metadata");
642
    parseMetadata(metadata);
643
    JSONObject mesh        = object.getJSONObject("mesh");
644
    parseMesh(mesh);
645
    JSONArray axis         = object.getJSONArray("axis");
646
    parseAxis(axis);
647
    JSONArray quats        = object.getJSONArray("quats");
648
    parseQuats(quats);
649
    JSONObject scrambling  = object.getJSONObject("scrambling");
650
    parseScrambling(scrambling,major);
651
    JSONObject touchcontrol= object.getJSONObject("touchcontrol");
652
    parseTouchcontrol(touchcontrol);
653
    JSONArray colors       = object.getJSONArray("colors");
654
    parseColors(colors);
655
    JSONObject solved      = object.getJSONObject("solved");
656
    parseSolved(solved);
657
    }
658

    
659
///////////////////////////////////////////////////////////////////////////////////////////////////
660

    
661
  private void parseTutorial(JSONObject object) throws JSONException
662
    {
663
    mTutorialObject = object.getString("object");
664
    JSONArray tuts= object.getJSONArray("tutorials");
665

    
666
    int len = tuts.length();
667
    mTutorials = new String[len][4];
668

    
669
    for(int i=0; i<len; i++)
670
      {
671
      JSONObject tut = tuts.getJSONObject(i);
672
      mTutorials[i][0] = tut.getString("language");
673
      mTutorials[i][1] = tut.getString("link");
674
      mTutorials[i][2] = tut.getString("title");
675
      mTutorials[i][3] = tut.getString("author");
676
      }
677
    }
678

    
679
///////////////////////////////////////////////////////////////////////////////////////////////////
680
// PUBLIC
681
///////////////////////////////////////////////////////////////////////////////////////////////////
682

    
683
  public void parseJsonFile(InputStream jsonStream) throws JSONException, IOException
684
    {
685
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
686
    StringBuilder contents = new StringBuilder();
687
    String line;
688

    
689
    while( (line = br.readLine()) != null) contents.append(line);
690
    br.close();
691
    jsonStream.close();
692
    String contentsString = contents.toString();
693
    JSONObject object = new JSONObject(contentsString);
694
    int major = object.getInt("major");
695

    
696
    if( major>=6 )
697
      {
698
      parseFile(object,major);
699
      }
700
    else
701
      {
702
      android.util.Log.e("readJsonFile", "Unknown version "+major);
703
      }
704
    }
705

    
706
///////////////////////////////////////////////////////////////////////////////////////////////////
707

    
708
  public void parseJsonFileMetadata(InputStream jsonStream) throws JSONException, IOException
709
    {
710
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
711
    StringBuilder contents = new StringBuilder();
712
    String tmp;
713

    
714
    while( (tmp = br.readLine()) != null) contents.append(tmp);
715
    br.close();
716
    jsonStream.close();
717

    
718
    JSONObject object = new JSONObject(contents.toString());
719
    int major = object.getInt("major");
720

    
721
    if( major>=6 )
722
      {
723
      JSONObject metadata = object.getJSONObject("metadata");
724
      parseMetadata(metadata);
725
      }
726
    else
727
      {
728
      android.util.Log.e("readJsonFileQuick", "Unknown version "+major);
729
      }
730
    }
731

    
732
///////////////////////////////////////////////////////////////////////////////////////////////////
733

    
734
  public void readNumScramblesAndPrice(Context context, String fileName) throws IOException, JSONException
735
    {
736
    File file = new File(context.getFilesDir(), fileName);
737
    InputStream stream = new FileInputStream(file);
738
    BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
739

    
740
    StringBuilder contents = new StringBuilder();
741
    String tmp;
742
    while( (tmp = br.readLine()) != null) contents.append(tmp);
743
    br.close();
744
    stream.close();
745

    
746
    JSONObject object = new JSONObject(contents.toString());
747
    JSONObject metadata = object.getJSONObject("metadata");
748
    mNumScrambles = metadata.getInt("scrambles");
749

    
750
    boolean free = metadata.optBoolean("free",true);
751
    if( free ) mPrice = 0;
752
    else mPrice = metadata.optInt("price", ObjectType.DEFAULT_PRICE_OF_OLD_OBJECTS );
753
    }
754

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

    
757
  public void parseJsonTutorial(InputStream jsonStream) throws IOException, JSONException
758
    {
759
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
760
    StringBuilder contents = new StringBuilder();
761
    String tmp;
762

    
763
    while( (tmp = br.readLine()) != null) contents.append(tmp);
764
    br.close();
765
    jsonStream.close();
766

    
767
    JSONObject object = new JSONObject(contents.toString());
768
    int major = object.getInt("major");
769

    
770
    if( major==1 )
771
      {
772
      parseTutorial(object);
773
      }
774
    else
775
      {
776
      android.util.Log.e("readJsonFile", "Unknown tutorial version "+major);
777
      }
778
    }
779

    
780
///////////////////////////////////////////////////////////////////////////////////////////////////
781

    
782
  public int getScrambleType()
783
    {
784
    return mScrambleType;
785
    }
786

    
787
///////////////////////////////////////////////////////////////////////////////////////////////////
788

    
789
  public int[][] getScrambleEdges()
790
    {
791
    return mEdges;
792
    }
793

    
794
///////////////////////////////////////////////////////////////////////////////////////////////////
795

    
796
  public int[][] getScrambleAlgorithms()
797
    {
798
    return mAlgorithms;
799
    }
800

    
801
///////////////////////////////////////////////////////////////////////////////////////////////////
802

    
803
  public int[][] getSolvedQuats()
804
    {
805
    return mSolvedQuats;
806
    }
807

    
808
///////////////////////////////////////////////////////////////////////////////////////////////////
809

    
810
  public Static4D[] getQuats()
811
    {
812
    return mQuats;
813
    }
814

    
815
///////////////////////////////////////////////////////////////////////////////////////////////////
816

    
817
  public int getSolvedFunctionIndex()
818
    {
819
    return mSolvedFuncIndex;
820
    }
821

    
822
///////////////////////////////////////////////////////////////////////////////////////////////////
823

    
824
  public int getNumStickerTypes()
825
    {
826
    return mNumStickerTypes;
827
    }
828

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

    
831
  public float[][] getCuts()
832
    {
833
    return mCuts;
834
    }
835

    
836
///////////////////////////////////////////////////////////////////////////////////////////////////
837

    
838
  public boolean[][] getLayerRotatable()
839
    {
840
    return mLayerRotatable;
841
    }
842

    
843
///////////////////////////////////////////////////////////////////////////////////////////////////
844

    
845
  public int getMovementType()
846
    {
847
    return mMovementType;
848
    }
849

    
850
///////////////////////////////////////////////////////////////////////////////////////////////////
851

    
852
  public int getMovementSplit()
853
    {
854
    return mMovementSplit;
855
    }
856

    
857
///////////////////////////////////////////////////////////////////////////////////////////////////
858

    
859
  public int[][][] getEnabled()
860
    {
861
    return mEnabled;
862
    }
863

    
864
///////////////////////////////////////////////////////////////////////////////////////////////////
865

    
866
  public float[] getDist3D()
867
    {
868
    return mDist3D;
869
    }
870

    
871
///////////////////////////////////////////////////////////////////////////////////////////////////
872

    
873
  public int getNumCubitFaces()
874
    {
875
    return mNumCubitFaces;
876
    }
877

    
878
///////////////////////////////////////////////////////////////////////////////////////////////////
879

    
880
  public float[][] getCubitPositions()
881
    {
882
    return mPositions;
883
    }
884

    
885
///////////////////////////////////////////////////////////////////////////////////////////////////
886

    
887
  public ObjectShape getObjectShape(int variant)
888
    {
889
    return mShapes[variant];
890
    }
891

    
892
///////////////////////////////////////////////////////////////////////////////////////////////////
893

    
894
  public ObjectFaceShape getObjectFaceShape(int variant)
895
    {
896
    return mFaceShapes[variant];
897
    }
898

    
899
///////////////////////////////////////////////////////////////////////////////////////////////////
900

    
901
  public ObjectVertexEffects getVertexEffects(int variant)
902
    {
903
    return mVertexEffects[variant];
904
    }
905

    
906
///////////////////////////////////////////////////////////////////////////////////////////////////
907

    
908
  public Static4D getCubitQuats(int cubit)
909
    {
910
    return mCubitQuats[cubit];
911
    }
912

    
913
///////////////////////////////////////////////////////////////////////////////////////////////////
914

    
915
  public int getNumCubitVariants()
916
    {
917
    return mNumCubitVariants;
918
    }
919

    
920
///////////////////////////////////////////////////////////////////////////////////////////////////
921

    
922
  public int getCubitVariant(int cubit)
923
    {
924
    return mCubitVariant[cubit];
925
    }
926

    
927
///////////////////////////////////////////////////////////////////////////////////////////////////
928

    
929
  public int getVariantStickerShape(int variant, int face)
930
    {
931
    int[] shapes = mVariantStickerShape[variant];
932
    return shapes.length>face ? shapes[face] : -1;
933
    }
934

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

    
937
  public int[] getCubitTypes()
938
    {
939
    return mCubitType;
940
    }
941

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

    
944
  public float[][] getCubitOffsets()
945
    {
946
    return mCubitRowOffset;
947
    }
948

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

    
951
  public int[][] getVariantFaceIsOuter()
952
    {
953
    return mVariantFaceIsOuter;
954
    }
955

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

    
958
  public int getCubitFaceFace(int cubit, int face)
959
    {
960
    return mCubitFaceColor[cubit][face];
961
    }
962

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

    
965
  public int getNumScrambles()
966
    {
967
    return mNumScrambles;
968
    }
969

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

    
972
  public int getPrice()
973
    {
974
    return mPrice;
975
    }
976

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

    
979
  public ObjectSticker retSticker(int sticker)
980
    {
981
    return mObjectSticker[sticker];
982
    }
983

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

    
986
  public ObjectStickerOverride[] getStickerOverrides()
987
    {
988
    return mStickerOverrides;
989
    }
990

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

    
993
  public float getPillowCoeff()
994
    {
995
    return mPillowCoeff;
996
    }
997

    
998
///////////////////////////////////////////////////////////////////////////////////////////////////
999

    
1000
  public Static3D[] getRotationAxis()
1001
    {
1002
    return mAxis;
1003
    }
1004

    
1005
///////////////////////////////////////////////////////////////////////////////////////////////////
1006

    
1007
  public int[][] getBasicAngle()
1008
    {
1009
    return mBasicAngle;
1010
    }
1011

    
1012
///////////////////////////////////////////////////////////////////////////////////////////////////
1013

    
1014
  public ObjectSignature getSignature()
1015
    {
1016
    return mSignature;
1017
    }
1018

    
1019
///////////////////////////////////////////////////////////////////////////////////////////////////
1020

    
1021
  public String getObjectName()
1022
    {
1023
    return mLongName;
1024
    }
1025

    
1026
///////////////////////////////////////////////////////////////////////////////////////////////////
1027

    
1028
  public String getShortName()
1029
    {
1030
    return mShortName;
1031
    }
1032

    
1033
///////////////////////////////////////////////////////////////////////////////////////////////////
1034

    
1035
  public String getInventor()
1036
    {
1037
    return mInventor;
1038
    }
1039

    
1040
///////////////////////////////////////////////////////////////////////////////////////////////////
1041

    
1042
  public int getYearOfInvention()
1043
    {
1044
    return mYearOfInvention;
1045
    }
1046

    
1047
///////////////////////////////////////////////////////////////////////////////////////////////////
1048

    
1049
  public int getComplexity()
1050
    {
1051
    return mComplexity;
1052
    }
1053

    
1054
///////////////////////////////////////////////////////////////////////////////////////////////////
1055

    
1056
  public int getNumFaces()
1057
    {
1058
    return mNumFaces;
1059
    }
1060

    
1061
///////////////////////////////////////////////////////////////////////////////////////////////////
1062

    
1063
  public int getNumFaceColors()
1064
    {
1065
    return mNumFaceColors;
1066
    }
1067

    
1068
///////////////////////////////////////////////////////////////////////////////////////////////////
1069

    
1070
  public int[] getNumLayers()
1071
    {
1072
    return mNumLayers;
1073
    }
1074

    
1075
///////////////////////////////////////////////////////////////////////////////////////////////////
1076

    
1077
  public float getSize()
1078
    {
1079
    return mSize;
1080
    }
1081

    
1082
///////////////////////////////////////////////////////////////////////////////////////////////////
1083

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

    
1089
///////////////////////////////////////////////////////////////////////////////////////////////////
1090

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

    
1096
///////////////////////////////////////////////////////////////////////////////////////////////////
1097

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

    
1103
///////////////////////////////////////////////////////////////////////////////////////////////////
1104

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

    
1110
///////////////////////////////////////////////////////////////////////////////////////////////////
1111

    
1112
  public String[][] getTutorials()
1113
    {
1114
    return mTutorials;
1115
    }
1116
}
(1-1/2)