Project

General

Profile

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

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

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 static org.distorted.objectlib.objects.TwistyBandagedCuboid.OBJECT_NAME_CUBOID;
13
import static org.distorted.objectlib.objects.TwistyBandagedPyraminx.OBJECT_NAME_PYRAMINX;
14
import static org.distorted.objectlib.scrambling.ScrambleStateLocallyBandaged.MAX_SUPPORTED_SIZE;
15

    
16
import java.io.BufferedReader;
17
import java.io.IOException;
18
import java.io.InputStream;
19
import java.io.InputStreamReader;
20
import java.nio.charset.StandardCharsets;
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.TwistyObjectCubit;
27
import org.distorted.objectlib.scrambling.ScrambleEdgeGenerator;
28
import org.json.JSONArray;
29
import org.json.JSONException;
30
import org.json.JSONObject;
31

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

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

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
public class JsonReader
42
{
43
  private int[][] mAlgorithms;
44
  private int[][] mEdges;
45
  private int[][] mSolvedQuats;
46
  private Static4D[] mQuats;
47
  private int mSolvedFuncIndex;
48
  private int mNumStickerTypes;
49
  private float[][] mCuts;
50
  private boolean[][] mLayerRotatable;
51
  private float[][] mRotationFactor;
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
  public static ObjectSignature produceSignature(String shortName, String longName, long[] signature)
87
    {
88
    // 'BAN*_*' ( or future possible 'BA**_*' ) objects
89
    if( shortName.charAt(0)=='B' && shortName.charAt(1)=='A' && shortName.charAt(4)=='_' )
90
      {
91
      char second = shortName.charAt(2);
92
      char third  = shortName.charAt(3);
93

    
94
      if( (second=='N' || (second>='0' && second<='9')) && (third>='0' && third<='9') )
95
        {
96
        int size = shortName.charAt(5) - '0';
97
        if( size>=2 && size<=MAX_SUPPORTED_SIZE ) return new ObjectSignature(size,signature);
98
        }
99
      }
100

    
101
    if( longName.equals(OBJECT_NAME_CUBOID)   ||
102
        longName.equals(OBJECT_NAME_PYRAMINX)  ) return new ObjectSignature(shortName,signature);
103

    
104
    return new ObjectSignature(signature);
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

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

    
121
    try
122
      {
123
      JSONArray sigArray = metadata.getJSONArray("signature");
124
      int size = sigArray.length();
125
      long[] signature = new long[size];
126
      for(int i=0; i<size; i++) signature[i] = sigArray.getLong(i);
127
      mSignature = produceSignature(mShortName,mLongName,signature);
128
      }
129
    catch(JSONException ex)
130
      {
131
      long signature = metadata.getLong("signature");
132
      mSignature = new ObjectSignature(signature);
133
      }
134

    
135
    boolean free = metadata.optBoolean("free", true);
136

    
137
    if( free ) mPrice = 0;
138
    else mPrice = metadata.optInt("price", ObjectType.DEFAULT_PRICE_OF_OLD_OBJECTS );
139
    }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

    
143
  private void parseCubits(JSONArray object) throws JSONException
144
    {
145
    int numCubits = object.length();
146

    
147
    mCubitQuats     = new Static4D[numCubits];
148
    mCubitVariant   = new int[numCubits];
149
    mPositions      = new float[numCubits][];
150
    mCubitFaceColor = new int[numCubits][];
151
    mCubitType      = new int[numCubits];
152
    mCubitRowOffset = new float[numCubits][];
153

    
154
    for(int i=0; i<numCubits; i++)
155
      {
156
      JSONObject jsonCubit = object.getJSONObject(i);
157

    
158
      float qx = (float)jsonCubit.getDouble("qx");
159
      float qy = (float)jsonCubit.getDouble("qy");
160
      float qz = (float)jsonCubit.getDouble("qz");
161
      float qw = (float)jsonCubit.getDouble("qw");
162

    
163
      mCubitQuats[i] = new Static4D(qx,qy,qz,qw);
164
      mCubitVariant[i] = jsonCubit.getInt("variant");
165

    
166
      JSONArray jsonCenter = jsonCubit.getJSONArray("centers");
167
      int numCenter = jsonCenter.length();
168
      mPositions[i] = new float[numCenter];
169
      for(int j=0; j<numCenter; j++) mPositions[i][j] = (float)jsonCenter.getDouble(j);
170

    
171
      JSONArray jsonColor  = jsonCubit.getJSONArray("colors");
172
      int numColor = jsonColor.length();
173
      mCubitFaceColor[i] = new int[numColor];
174
      for(int j=0; j<numColor; j++) mCubitFaceColor[i][j] = jsonColor.getInt(j);
175

    
176
      mCubitType[i] = jsonCubit.optInt("type", TwistyObjectCubit.TYPE_NORMAL);
177
      float xoff = (float)jsonCubit.optDouble("offsetX", 0 );
178
      float yoff = (float)jsonCubit.optDouble("offsetY", 0 );
179
      float zoff = (float)jsonCubit.optDouble("offsetZ", 0 );
180
      mCubitRowOffset[i] = new float[] {xoff,yoff,zoff};
181
      }
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
  private void parseShapes(JSONArray object) throws JSONException
187
    {
188
    mNumCubitVariants     = object.length();
189
    mVariantStickerShape  = new int[mNumCubitVariants][];
190
    mVariantFaceIsOuter   = new int[mNumCubitVariants][];
191
    mShapes               = new ObjectShape[mNumCubitVariants];
192
    mFaceShapes           = new ObjectFaceShape[mNumCubitVariants];
193
    mVertexEffects        = new ObjectVertexEffects[mNumCubitVariants];
194
    mNumCubitFaces = -1;
195

    
196
    for(int i=0; i<mNumCubitVariants; i++)
197
      {
198
      JSONObject jsonShape = object.getJSONObject(i);
199

    
200
      /////////////////////////////////////////////////////////////////////////
201
      ////// ObjectShape //////////////////////////////////////////////////////
202
      JSONArray jsonVertices= jsonShape.getJSONArray("vertices");
203
      int numVertices = jsonVertices.length();
204
      float[][] verts = new float[numVertices][3];
205

    
206
      for(int v=0; v<numVertices; v++)
207
        {
208
        JSONObject vert = jsonVertices.getJSONObject(v);
209
        verts[v][0] = (float)vert.getDouble("x");
210
        verts[v][1] = (float)vert.getDouble("y");
211
        verts[v][2] = (float)vert.getDouble("z");
212
        }
213

    
214
      JSONArray jsonFaces= jsonShape.getJSONArray("faces");
215
      int numFaces = jsonFaces.length();
216
      if( mNumCubitFaces<numFaces ) mNumCubitFaces=numFaces;
217
      boolean multigon = jsonShape.optBoolean("facesMultigon", false);
218

    
219
      if( !multigon )
220
        {
221
        int[][] vertIndices = new int[numFaces][];
222

    
223
        for(int f=0; f<numFaces; f++)
224
          {
225
          JSONObject jsonFace = jsonFaces.getJSONObject(f);
226
          JSONArray vertices = jsonFace.getJSONArray("vertexIndices");
227
          int numV = vertices.length();
228
          vertIndices[f] = new int[numV];
229
          for(int k=0; k<numV; k++) vertIndices[f][k] = vertices.getInt(k);
230
          }
231

    
232
        mShapes[i] = new ObjectShape(verts,vertIndices);
233
        }
234
      else
235
        {
236
        int[][][] vertIndices = new int[numFaces][][];
237

    
238
        for(int f=0; f<numFaces; f++)
239
          {
240
          JSONObject jsonFace = jsonFaces.getJSONObject(f);
241
          JSONArray vertices = jsonFace.getJSONArray("vertexIndices");
242
          int numC = vertices.length();
243
          vertIndices[f] = new int[numC][];
244

    
245
          for(int c=0; c<numC; c++)
246
            {
247
            JSONArray components = vertices.getJSONArray(c);
248
            int numV = components.length();
249
            vertIndices[f][c] = new int[numV];
250
            for(int k=0; k<numV; k++) vertIndices[f][c][k] = components.getInt(k);
251
            }
252
          }
253

    
254
        mShapes[i] = new ObjectShape(verts,vertIndices);
255
        }
256

    
257
      /////////////////////////////////////////////////////////////////////////
258
      ////// StickerShape & Outer /////////////////////////////////////////////
259
      mVariantStickerShape[i]= new int[numFaces];
260
      mVariantFaceIsOuter[i] = new int[numFaces];
261
      int[] bandIndices = new int[numFaces];
262

    
263
      for(int f=0; f<numFaces; f++)
264
        {
265
        JSONObject jsonFace = jsonFaces.getJSONObject(f);
266
        mVariantStickerShape[i][f]= jsonFace.getInt("sticker");
267
        mVariantFaceIsOuter[i][f] = jsonFace.optInt("isOuter",0);
268
        bandIndices[f] = jsonFace.getInt("bandIndex");
269
        }
270

    
271
      /////////////////////////////////////////////////////////////////////////
272
      ////// ObjectFaceShape //////////////////////////////////////////////////
273
      JSONArray jsonBands= jsonShape.getJSONArray("bands");
274
      int numBands = jsonBands.length();
275
      float[][] bands = new float[numBands][7];
276

    
277
      for(int b=0; b<numBands; b++)
278
        {
279
        JSONObject jsonBand = jsonBands.getJSONObject(b);
280
        float[] band = bands[b];
281

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

    
291
      JSONObject jsonConvexity = jsonShape.optJSONObject("convexity");
292
      float[] convexity = null;
293

    
294
      if( jsonConvexity!=null )
295
        {
296
        convexity = new float[3];
297
        convexity[0] = (float)jsonConvexity.getDouble("x");
298
        convexity[1] = (float)jsonConvexity.getDouble("y");
299
        convexity[2] = (float)jsonConvexity.getDouble("z");
300
        }
301

    
302
      mFaceShapes[i] = new ObjectFaceShape(bands,bandIndices,convexity);
303

    
304
      /////////////////////////////////////////////////////////////////////////
305
      ////// ObjectVertexEffects //////////////////////////////////////////////
306
      JSONArray jsonEffects = jsonShape.optJSONArray("effects");
307

    
308
      if( jsonEffects!=null )
309
        {
310
        int numEffects = jsonEffects.length();
311

    
312
        String[] name    = new String[numEffects];
313
        float[][] vars   = new float[numEffects][5];
314
        float[][] center = new float[numEffects][3];
315
        float[][] region = new float[numEffects][4];
316
        boolean[] use    = new boolean[numEffects];
317

    
318
        for(int j=0; j<numEffects; j++)
319
          {
320
          JSONObject jsonEffect = jsonEffects.getJSONObject(j);
321

    
322
          name[j] = jsonEffect.getString("name");
323

    
324
          vars[j][0] = (float)jsonEffect.getDouble("var0");
325
          vars[j][1] = (float)jsonEffect.getDouble("var1");
326
          vars[j][2] = (float)jsonEffect.getDouble("var2");
327
          vars[j][3] = (float)jsonEffect.getDouble("var3");
328
          vars[j][4] = (float)jsonEffect.getDouble("var4");
329

    
330
          center[j][0] = (float)jsonEffect.getDouble("center0");
331
          center[j][1] = (float)jsonEffect.getDouble("center1");
332
          center[j][2] = (float)jsonEffect.getDouble("center2");
333

    
334
          region[j][0] = (float)jsonEffect.getDouble("region0");
335
          region[j][1] = (float)jsonEffect.getDouble("region1");
336
          region[j][2] = (float)jsonEffect.getDouble("region2");
337
          region[j][3] = (float)jsonEffect.getDouble("region3");
338

    
339
          use[j] = jsonEffect.getBoolean("use");
340
          }
341

    
342
        mVertexEffects[i] = new ObjectVertexEffects(name,vars,center,region,use);
343
        }
344
      }
345
    }
346

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348

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

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

    
361
      float[] coords     = new float[2*numVertices];
362
      float[] curvatures = new float[numVertices];
363
      float[] radii      = new float[numVertices];
364

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

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

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

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

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

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

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406

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

    
416
    JSONArray overrides= object.optJSONArray("overrides");
417
    parseOverrides(overrides);
418

    
419
    mPillowCoeff = (float)object.optDouble("pillow",1.0);
420
    }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423

    
424
  private void parseAxis(JSONArray object) throws JSONException
425
    {
426
    int numAxis = object.length();
427

    
428
    mBasicAngle     = new int[numAxis][];
429
    mAxis           = new Static3D[numAxis];
430
    mCuts           = new float[numAxis][];
431
    mLayerRotatable = new boolean[numAxis][];
432
    mRotationFactor = new float[numAxis][];
433
    mNumLayers      = new int[numAxis];
434

    
435
    for(int i=0; i<numAxis; i++)
436
      {
437
      JSONObject jsonAx = object.getJSONObject(i);
438

    
439
      float x = (float)jsonAx.getDouble("x");
440
      float y = (float)jsonAx.getDouble("y");
441
      float z = (float)jsonAx.getDouble("z");
442

    
443
      mAxis[i] = new Static3D(x,y,z);
444

    
445
      JSONArray jsonAngles = jsonAx.getJSONArray("basicAngles");
446
      int numAngles = jsonAngles.length();
447
      mBasicAngle[i] = new int[numAngles];
448
      for(int j=0; j<numAngles; j++) mBasicAngle[i][j] = jsonAngles.getInt(j);
449

    
450
      JSONArray jsonCuts = jsonAx.getJSONArray("cuts");
451
      int numCuts = jsonCuts.length();
452
      mCuts[i] = new float[numCuts];
453
      for(int j=0; j<numCuts; j++) mCuts[i][j] = (float)jsonCuts.getDouble(j);
454

    
455
      JSONArray jsonRota = jsonAx.getJSONArray("rotatable");
456
      int numRota = jsonRota.length();
457
      mLayerRotatable[i] = new boolean[numRota];
458
      for(int j=0; j<numRota; j++) mLayerRotatable[i][j] = jsonRota.getBoolean(j);
459

    
460
      JSONArray jsonFactor = jsonAx.optJSONArray("factor");
461

    
462
      if( jsonFactor!=null )
463
        {
464
        int numFactor = jsonFactor.length();
465
        mRotationFactor[i] = new float[numFactor];
466
        for(int j=0; j<numFactor; j++) mRotationFactor[i][j] = (float)jsonFactor.getDouble(j);
467
        }
468
      else
469
        {
470
        mRotationFactor[i] = new float[numRota];
471
        for(int j=0; j<numRota; j++) mRotationFactor[i][j] = 1.0f;
472
        }
473

    
474
      mNumLayers[i] = numRota;
475
      }
476
    }
477

    
478
///////////////////////////////////////////////////////////////////////////////////////////////////
479

    
480
  private void parseQuats(JSONArray object) throws JSONException
481
    {
482
    int numQuats = object.length();
483
    mQuats = new Static4D[numQuats];
484

    
485
    for(int i=0; i<numQuats; i++)
486
      {
487
      JSONObject jsonQuat = object.getJSONObject(i);
488

    
489
      float x = (float)jsonQuat.getDouble("x");
490
      float y = (float)jsonQuat.getDouble("y");
491
      float z = (float)jsonQuat.getDouble("z");
492
      float w = (float)jsonQuat.getDouble("w");
493

    
494
      mQuats[i] = new Static4D(x,y,z,w);
495
      }
496
    }
497

    
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499

    
500
  private int getAlgorithmIndex(int ax, int layer, int angle)
501
    {
502
    int numAlgs = mAlgorithms.length;
503

    
504
    for(int alg=0; alg<numAlgs; alg++)
505
      {
506
      int[] a = mAlgorithms[alg];
507
      if( a.length>=3 && a[0]==ax && a[1]==layer && a[2]==angle ) return alg;
508
      }
509

    
510
    return -1;
511
    }
512

    
513
///////////////////////////////////////////////////////////////////////////////////////////////////
514

    
515
  private int[] produceEdge(int[][] scramblingData)
516
    {
517
    int index=0, length=0;
518
    int numAxis = scramblingData.length;
519
    for (int[] scramblingDatum : scramblingData) length += scramblingDatum.length;
520

    
521
    int[] ret = new int[2*length/3];
522

    
523
    for(int ax=0; ax<numAxis; ax++)
524
      {
525
      int[] data = scramblingData[ax];
526
      int num = data.length/3;
527

    
528
      for(int j=0; j<num; j++)
529
        {
530
        int layer = data[3*j];
531
        int angle = data[3*j+1];
532
        int state = data[3*j+2];
533

    
534
        ret[2*index  ] = getAlgorithmIndex(ax,layer,angle);
535
        ret[2*index+1] = state;
536
        index++;
537
        }
538
      }
539

    
540
    return ret;
541
    }
542

    
543
///////////////////////////////////////////////////////////////////////////////////////////////////
544

    
545
  private void parseScrambling6(JSONObject object) throws JSONException
546
    {
547
    JSONArray jsonStates = object.getJSONArray("scrambleStates");
548
    int numStates = jsonStates.length();
549
    int[][][] scramblingData = new int[numStates][][];
550

    
551
    for(int i=0; i<numStates; i++)
552
      {
553
      JSONArray jsonState = jsonStates.getJSONArray(i);
554
      int numAxis = jsonState.length();
555
      scramblingData[i] = new int[numAxis][];
556

    
557
      for(int j=0; j<numAxis; j++)
558
        {
559
        JSONArray jsonData = jsonState.getJSONArray(j);
560
        int numData = jsonData.length();
561
        scramblingData[i][j] = new int[numData];
562
        for(int k=0; k<numData; k++) scramblingData[i][j][k] = jsonData.getInt(k);
563
        }
564
      }
565

    
566
    mAlgorithms = ScrambleEdgeGenerator.getScramblingAlgorithms(mBasicAngle);
567
    mEdges = new int[numStates][];
568
    for(int i=0; i<numStates; i++) mEdges[i] = produceEdge(scramblingData[i]);
569
    }
570

    
571
///////////////////////////////////////////////////////////////////////////////////////////////////
572

    
573
  private void parseScrambling7to10(JSONObject object) throws JSONException
574
    {
575
    JSONArray jsonAlgorithms = object.getJSONArray("algorithms");
576
    int numAlgs = jsonAlgorithms.length();
577
    mAlgorithms = new int[numAlgs][];
578

    
579
    for(int i=0; i<numAlgs; i++)
580
      {
581
      JSONArray jsonAlg = jsonAlgorithms.getJSONArray(i);
582
      int numEntries = jsonAlg.length();
583
      mAlgorithms[i] = new int[numEntries];
584
      for(int j=0; j<numEntries; j++)
585
        {
586
        int entry = jsonAlg.getInt(j);
587
        mAlgorithms[i][j] = ( (j%3)==1 ? (1<<entry) : entry );
588
        }
589
      }
590

    
591
    JSONArray jsonEdges = object.getJSONArray("edges");
592
    int numEdges = jsonEdges.length();
593
    mEdges = new int[numEdges][];
594

    
595
    for(int i=0; i<numEdges; i++)
596
      {
597
      JSONArray jsonEdge = jsonEdges.getJSONArray(i);
598
      int numEntries = jsonEdge.length();
599
      mEdges[i] = new int[numEntries];
600
      for(int j=0; j<numEntries; j++) mEdges[i][j] = jsonEdge.getInt(j);
601
      }
602
    }
603

    
604
///////////////////////////////////////////////////////////////////////////////////////////////////
605

    
606
  private void parseScrambling11orMore(JSONObject object) throws JSONException
607
    {
608
    JSONArray jsonAlgorithms = object.getJSONArray("algorithms");
609
    int numAlgs = jsonAlgorithms.length();
610
    mAlgorithms = new int[numAlgs][];
611

    
612
    for(int i=0; i<numAlgs; i++)
613
      {
614
      JSONArray jsonAlg = jsonAlgorithms.getJSONArray(i);
615
      int numEntries = jsonAlg.length();
616
      mAlgorithms[i] = new int[numEntries];
617
      for(int j=0; j<numEntries; j++) mAlgorithms[i][j] = jsonAlg.getInt(j);
618
      }
619

    
620
    JSONArray jsonEdges = object.getJSONArray("edges");
621
    int numEdges = jsonEdges.length();
622
    mEdges = new int[numEdges][];
623

    
624
    for(int i=0; i<numEdges; i++)
625
      {
626
      JSONArray jsonEdge = jsonEdges.getJSONArray(i);
627
      int numEntries = jsonEdge.length();
628
      mEdges[i] = new int[numEntries];
629
      for(int j=0; j<numEntries; j++) mEdges[i][j] = jsonEdge.getInt(j);
630
      }
631
    }
632

    
633
///////////////////////////////////////////////////////////////////////////////////////////////////
634

    
635
  private void parseScrambling(JSONObject object, int major) throws JSONException
636
    {
637
    mScrambleType = object.getInt("scrambleType");
638

    
639
    if( mScrambleType==0 )
640
      {
641
      if( major==6 )       parseScrambling6(object);
642
      else if( major<=10 ) parseScrambling7to10(object);
643
      else                 parseScrambling11orMore(object);
644
      }
645
    }
646

    
647
///////////////////////////////////////////////////////////////////////////////////////////////////
648

    
649
  private void parseTouchcontrol(JSONObject object) throws JSONException
650
    {
651
    mMovementType = object.getInt("movementType");
652
    mMovementSplit= object.getInt("movementSplit");
653

    
654
    try
655
      {
656
      JSONArray jsonEnabled = object.getJSONArray("enabledAxis");
657
      int numFace = jsonEnabled.length();
658

    
659
      mEnabled = new int[numFace][][];
660

    
661
      for(int i=0; i<numFace; i++)
662
        {
663
        JSONArray jsonSection = jsonEnabled.getJSONArray(i);
664
        int numSection = jsonSection.length();
665
        mEnabled[i] = new int[numSection][];
666

    
667
        for(int j=0; j<numSection; j++)
668
          {
669
          JSONArray jsonAx = jsonSection.getJSONArray(j);
670
          int numAxis = jsonAx.length();
671
          mEnabled[i][j] = new int[numAxis];
672
          for(int k=0; k<numAxis; k++) mEnabled[i][j][k] = jsonAx.getInt(k);
673
          }
674
        }
675
      }
676
    catch( JSONException ex )
677
      {
678
      // ignore, the object does not have to have 'enabledAxis' defined at all.
679
      }
680

    
681
    try
682
      {
683
      JSONArray jsonDist = object.getJSONArray("dist3D");
684
      int num = jsonDist.length();
685
      mDist3D = new float[num];
686
      for(int j=0; j<num; j++) mDist3D[j] = (float)jsonDist.getDouble(j);
687
      }
688
    catch( JSONException ex )
689
      {
690
      // ignore, the object does not have a 'dist3D' which is possible.
691
      }
692
    }
693

    
694
///////////////////////////////////////////////////////////////////////////////////////////////////
695

    
696
  private void parseColors(JSONArray object) throws JSONException
697
    {
698
    mNumFaceColors = object.length()-1;
699

    
700
    mColor = new int[mNumFaceColors];
701
    for(int i=0; i<mNumFaceColors; i++) mColor[i] = object.getInt(i);
702

    
703
    mInternalColor = object.getInt(mNumFaceColors);
704
    }
705

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

    
708
  private void parseSolved(JSONObject solved) throws JSONException
709
    {
710
    mSolvedFuncIndex = solved.getInt("functionIndex");
711

    
712
    try
713
      {
714
      JSONArray groupArray= solved.getJSONArray("groups");
715
      int numGroups = groupArray.length();
716
      mSolvedQuats  = new int[numGroups][];
717

    
718
      for(int i=0; i<numGroups; i++)
719
        {
720
        JSONArray groupElements = groupArray.getJSONArray(i);
721
        int groupSize = groupElements.length();
722
        mSolvedQuats[i] = new int[groupSize];
723
        for(int j=0; j<groupSize; j++) mSolvedQuats[i][j] = groupElements.getInt(j);
724
        }
725
      }
726
    catch( JSONException ex )
727
      {
728
      // ignore, the object does not have to have an array of solved groups.
729
      }
730
    }
731

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

    
734
  private void parseFile(JSONObject object, int major) throws JSONException
735
    {
736
    JSONObject metadata    = object.getJSONObject("metadata");
737
    parseMetadata(metadata);
738
    JSONObject mesh        = object.getJSONObject("mesh");
739
    parseMesh(mesh);
740
    JSONArray axis         = object.getJSONArray("axis");
741
    parseAxis(axis);
742
    JSONArray quats        = object.getJSONArray("quats");
743
    parseQuats(quats);
744
    JSONObject scrambling  = object.getJSONObject("scrambling");
745
    parseScrambling(scrambling,major);
746
    JSONObject touchcontrol= object.getJSONObject("touchcontrol");
747
    parseTouchcontrol(touchcontrol);
748
    JSONArray colors       = object.getJSONArray("colors");
749
    parseColors(colors);
750
    JSONObject solved      = object.getJSONObject("solved");
751
    parseSolved(solved);
752
    }
753

    
754
///////////////////////////////////////////////////////////////////////////////////////////////////
755

    
756
  private void parseTutorial(JSONObject object) throws JSONException
757
    {
758
    mTutorialObject = object.getString("object");
759
    JSONArray tuts= object.getJSONArray("tutorials");
760

    
761
    int len = tuts.length();
762
    mTutorials = new String[len][4];
763

    
764
    for(int i=0; i<len; i++)
765
      {
766
      JSONObject tut = tuts.getJSONObject(i);
767
      mTutorials[i][0] = tut.getString("language");
768
      mTutorials[i][1] = tut.getString("link");
769
      mTutorials[i][2] = tut.getString("title");
770
      mTutorials[i][3] = tut.getString("author");
771
      }
772
    }
773

    
774
///////////////////////////////////////////////////////////////////////////////////////////////////
775
// PUBLIC
776
///////////////////////////////////////////////////////////////////////////////////////////////////
777

    
778
  public void parseJsonFile(InputStream jsonStream) throws JSONException, IOException
779
    {
780
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
781
    StringBuilder contents = new StringBuilder();
782
    String line;
783

    
784
    while( (line = br.readLine()) != null) contents.append(line);
785
    br.close();
786
    jsonStream.close();
787
    String contentsString = contents.toString();
788
    JSONObject object = new JSONObject(contentsString);
789
    int major = object.getInt("major");
790

    
791
    if( major>=6 )
792
      {
793
      parseFile(object,major);
794
      }
795
    else
796
      {
797
      android.util.Log.e("readJsonFile", "Unknown version "+major);
798
      }
799
    }
800

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

    
803
  public void parseJsonFileMetadata(InputStream jsonStream) throws JSONException, IOException
804
    {
805
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
806
    StringBuilder contents = new StringBuilder();
807
    String tmp;
808

    
809
    while( (tmp = br.readLine()) != null) contents.append(tmp);
810
    br.close();
811
    jsonStream.close();
812

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

    
816
    if( major>=6 )
817
      {
818
      JSONObject metadata = object.getJSONObject("metadata");
819
      parseMetadata(metadata);
820
      }
821
    else
822
      {
823
      android.util.Log.e("readJsonFileQuick", "Unknown version "+major);
824
      }
825
    }
826

    
827
///////////////////////////////////////////////////////////////////////////////////////////////////
828

    
829
  public void readNumScramblesAndPrice(InputStream stream) throws IOException, JSONException
830
    {
831
    BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
832

    
833
    StringBuilder contents = new StringBuilder();
834
    String tmp;
835
    while( (tmp = br.readLine()) != null) contents.append(tmp);
836
    br.close();
837
    stream.close();
838

    
839
    JSONObject object = new JSONObject(contents.toString());
840
    JSONObject metadata = object.getJSONObject("metadata");
841
    mNumScrambles = metadata.getInt("scrambles");
842

    
843
    boolean free = metadata.optBoolean("free",true);
844
    if( free ) mPrice = 0;
845
    else mPrice = metadata.optInt("price", ObjectType.DEFAULT_PRICE_OF_OLD_OBJECTS );
846
    }
847

    
848
///////////////////////////////////////////////////////////////////////////////////////////////////
849

    
850
  public void parseJsonTutorial(InputStream jsonStream) throws IOException, JSONException
851
    {
852
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
853
    StringBuilder contents = new StringBuilder();
854
    String tmp;
855

    
856
    while( (tmp = br.readLine()) != null) contents.append(tmp);
857
    br.close();
858
    jsonStream.close();
859

    
860
    JSONObject object = new JSONObject(contents.toString());
861
    int major = object.getInt("major");
862

    
863
    if( major==1 )
864
      {
865
      parseTutorial(object);
866
      }
867
    else
868
      {
869
      android.util.Log.e("readJsonFile", "Unknown tutorial version "+major);
870
      }
871
    }
872

    
873
///////////////////////////////////////////////////////////////////////////////////////////////////
874

    
875
  public int getScrambleType()
876
    {
877
    return mScrambleType;
878
    }
879

    
880
///////////////////////////////////////////////////////////////////////////////////////////////////
881

    
882
  public int[][] getScrambleEdges()
883
    {
884
    return mEdges;
885
    }
886

    
887
///////////////////////////////////////////////////////////////////////////////////////////////////
888

    
889
  public int[][] getScrambleAlgorithms()
890
    {
891
    return mAlgorithms;
892
    }
893

    
894
///////////////////////////////////////////////////////////////////////////////////////////////////
895

    
896
  public int[][] getSolvedQuats()
897
    {
898
    return mSolvedQuats;
899
    }
900

    
901
///////////////////////////////////////////////////////////////////////////////////////////////////
902

    
903
  public Static4D[] getQuats()
904
    {
905
    return mQuats;
906
    }
907

    
908
///////////////////////////////////////////////////////////////////////////////////////////////////
909

    
910
  public int getSolvedFunctionIndex()
911
    {
912
    return mSolvedFuncIndex;
913
    }
914

    
915
///////////////////////////////////////////////////////////////////////////////////////////////////
916

    
917
  public int getNumStickerTypes()
918
    {
919
    return mNumStickerTypes;
920
    }
921

    
922
///////////////////////////////////////////////////////////////////////////////////////////////////
923

    
924
  public float[][] getCuts()
925
    {
926
    return mCuts;
927
    }
928

    
929
///////////////////////////////////////////////////////////////////////////////////////////////////
930

    
931
  public boolean[][] getLayerRotatable()
932
    {
933
    return mLayerRotatable;
934
    }
935

    
936
///////////////////////////////////////////////////////////////////////////////////////////////////
937

    
938
  public int getMovementType()
939
    {
940
    return mMovementType;
941
    }
942

    
943
///////////////////////////////////////////////////////////////////////////////////////////////////
944

    
945
  public int getMovementSplit()
946
    {
947
    return mMovementSplit;
948
    }
949

    
950
///////////////////////////////////////////////////////////////////////////////////////////////////
951

    
952
  public int[][][] getEnabled()
953
    {
954
    return mEnabled;
955
    }
956

    
957
///////////////////////////////////////////////////////////////////////////////////////////////////
958

    
959
  public float[] getDist3D()
960
    {
961
    return mDist3D;
962
    }
963

    
964
///////////////////////////////////////////////////////////////////////////////////////////////////
965

    
966
  public int getNumCubitFaces()
967
    {
968
    return mNumCubitFaces;
969
    }
970

    
971
///////////////////////////////////////////////////////////////////////////////////////////////////
972

    
973
  public float[][] getCubitPositions()
974
    {
975
    return mPositions;
976
    }
977

    
978
///////////////////////////////////////////////////////////////////////////////////////////////////
979

    
980
  public ObjectShape getObjectShape(int variant)
981
    {
982
    return mShapes[variant];
983
    }
984

    
985
///////////////////////////////////////////////////////////////////////////////////////////////////
986

    
987
  public ObjectFaceShape getObjectFaceShape(int variant)
988
    {
989
    return mFaceShapes[variant];
990
    }
991

    
992
///////////////////////////////////////////////////////////////////////////////////////////////////
993

    
994
  public ObjectVertexEffects getVertexEffects(int variant)
995
    {
996
    return mVertexEffects[variant];
997
    }
998

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

    
1001
  public Static4D getCubitQuats(int cubit)
1002
    {
1003
    return mCubitQuats[cubit];
1004
    }
1005

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

    
1008
  public int getNumCubitVariants()
1009
    {
1010
    return mNumCubitVariants;
1011
    }
1012

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

    
1015
  public int getCubitVariant(int cubit)
1016
    {
1017
    return mCubitVariant[cubit];
1018
    }
1019

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

    
1022
  public int getVariantStickerShape(int variant, int face)
1023
    {
1024
    int[] shapes = mVariantStickerShape[variant];
1025
    return shapes.length>face ? shapes[face] : -1;
1026
    }
1027

    
1028
///////////////////////////////////////////////////////////////////////////////////////////////////
1029

    
1030
  public float[][] returnRotationFactor()
1031
    {
1032
    return mRotationFactor;
1033
    }
1034

    
1035
///////////////////////////////////////////////////////////////////////////////////////////////////
1036

    
1037
  public int[] getCubitTypes()
1038
    {
1039
    return mCubitType;
1040
    }
1041

    
1042
///////////////////////////////////////////////////////////////////////////////////////////////////
1043

    
1044
  public float[][] getCubitOffsets()
1045
    {
1046
    return mCubitRowOffset;
1047
    }
1048

    
1049
///////////////////////////////////////////////////////////////////////////////////////////////////
1050

    
1051
  public int[][] getVariantFaceIsOuter()
1052
    {
1053
    return mVariantFaceIsOuter;
1054
    }
1055

    
1056
///////////////////////////////////////////////////////////////////////////////////////////////////
1057

    
1058
  public int getCubitFaceFace(int cubit, int face)
1059
    {
1060
    return mCubitFaceColor[cubit][face];
1061
    }
1062

    
1063
///////////////////////////////////////////////////////////////////////////////////////////////////
1064

    
1065
  public int getNumScrambles()
1066
    {
1067
    return mNumScrambles;
1068
    }
1069

    
1070
///////////////////////////////////////////////////////////////////////////////////////////////////
1071

    
1072
  public int getPrice()
1073
    {
1074
    return mPrice;
1075
    }
1076

    
1077
///////////////////////////////////////////////////////////////////////////////////////////////////
1078

    
1079
  public ObjectSticker retSticker(int sticker)
1080
    {
1081
    return mObjectSticker[sticker];
1082
    }
1083

    
1084
///////////////////////////////////////////////////////////////////////////////////////////////////
1085

    
1086
  public ObjectStickerOverride[] getStickerOverrides()
1087
    {
1088
    return mStickerOverrides;
1089
    }
1090

    
1091
///////////////////////////////////////////////////////////////////////////////////////////////////
1092

    
1093
  public float getPillowCoeff()
1094
    {
1095
    return mPillowCoeff;
1096
    }
1097

    
1098
///////////////////////////////////////////////////////////////////////////////////////////////////
1099

    
1100
  public Static3D[] getRotationAxis()
1101
    {
1102
    return mAxis;
1103
    }
1104

    
1105
///////////////////////////////////////////////////////////////////////////////////////////////////
1106

    
1107
  public int[][] getBasicAngle()
1108
    {
1109
    return mBasicAngle;
1110
    }
1111

    
1112
///////////////////////////////////////////////////////////////////////////////////////////////////
1113

    
1114
  public ObjectSignature getSignature()
1115
    {
1116
    return mSignature;
1117
    }
1118

    
1119
///////////////////////////////////////////////////////////////////////////////////////////////////
1120

    
1121
  public String getObjectName()
1122
    {
1123
    return mLongName;
1124
    }
1125

    
1126
///////////////////////////////////////////////////////////////////////////////////////////////////
1127

    
1128
  public String getShortName()
1129
    {
1130
    return mShortName;
1131
    }
1132

    
1133
///////////////////////////////////////////////////////////////////////////////////////////////////
1134

    
1135
  public String getInventor()
1136
    {
1137
    return mInventor;
1138
    }
1139

    
1140
///////////////////////////////////////////////////////////////////////////////////////////////////
1141

    
1142
  public int getYearOfInvention()
1143
    {
1144
    return mYearOfInvention;
1145
    }
1146

    
1147
///////////////////////////////////////////////////////////////////////////////////////////////////
1148

    
1149
  public int getComplexity()
1150
    {
1151
    return mComplexity;
1152
    }
1153

    
1154
///////////////////////////////////////////////////////////////////////////////////////////////////
1155

    
1156
  public int getNumFaces()
1157
    {
1158
    return mNumFaces;
1159
    }
1160

    
1161
///////////////////////////////////////////////////////////////////////////////////////////////////
1162

    
1163
  public int getNumFaceColors()
1164
    {
1165
    return mNumFaceColors;
1166
    }
1167

    
1168
///////////////////////////////////////////////////////////////////////////////////////////////////
1169

    
1170
  public int[] getNumLayers()
1171
    {
1172
    return mNumLayers;
1173
    }
1174

    
1175
///////////////////////////////////////////////////////////////////////////////////////////////////
1176

    
1177
  public float getSize()
1178
    {
1179
    return mSize;
1180
    }
1181

    
1182
///////////////////////////////////////////////////////////////////////////////////////////////////
1183

    
1184
  public int getColor(int face)
1185
    {
1186
    return mColor[face];
1187
    }
1188

    
1189
///////////////////////////////////////////////////////////////////////////////////////////////////
1190

    
1191
  public int getInternalColor()
1192
    {
1193
    return mInternalColor;
1194
    }
1195

    
1196
///////////////////////////////////////////////////////////////////////////////////////////////////
1197

    
1198
  public boolean shouldResetTextureMaps()
1199
    {
1200
    return mResetMaps;
1201
    }
1202

    
1203
///////////////////////////////////////////////////////////////////////////////////////////////////
1204

    
1205
  public String getTutorialObject()
1206
    {
1207
    return mTutorialObject;
1208
    }
1209

    
1210
///////////////////////////////////////////////////////////////////////////////////////////////////
1211

    
1212
  public String[][] getTutorials()
1213
    {
1214
    return mTutorials;
1215
    }
1216
}
(1-1/2)