Project

General

Profile

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

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

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;
13
import static org.distorted.objectlib.scrambling.ScrambleStateBandagedCuboid.MAX_SUPPORTED_SIZE;
14

    
15
import java.io.BufferedReader;
16
import java.io.IOException;
17
import java.io.InputStream;
18
import java.io.InputStreamReader;
19
import java.nio.charset.StandardCharsets;
20

    
21
import org.distorted.objectlib.helpers.ObjectFaceShape;
22
import org.distorted.objectlib.helpers.ObjectSignature;
23
import org.distorted.objectlib.helpers.ObjectStickerOverride;
24
import org.distorted.objectlib.helpers.ObjectVertexEffects;
25
import org.distorted.objectlib.main.TwistyObjectCubit;
26
import org.distorted.objectlib.scrambling.ScrambleEdgeGenerator;
27
import org.json.JSONArray;
28
import org.json.JSONException;
29
import org.json.JSONObject;
30

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

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

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

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

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

    
84
  public static ObjectSignature produceSignature(String shortName, String longName, long[] signature)
85
    {
86
    // 'BAN*_*' ( or future possible 'BA**_*' ) objects
87
    if( shortName.charAt(0)=='B' && shortName.charAt(1)=='A' && shortName.charAt(4)=='_' )
88
      {
89
      char second = shortName.charAt(2);
90
      char third  = shortName.charAt(3);
91

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

    
99
    if( longName.equals(OBJECT_NAME) ) new ObjectSignature(shortName,signature);
100

    
101
    return new ObjectSignature(signature);
102
    }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

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

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

    
132
    boolean free = metadata.optBoolean("free", true);
133

    
134
    if( free ) mPrice = 0;
135
    else mPrice = metadata.optInt("price", ObjectType.DEFAULT_PRICE_OF_OLD_OBJECTS );
136
    }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
  private void parseCubits(JSONArray object) throws JSONException
141
    {
142
    int numCubits = object.length();
143

    
144
    mCubitQuats     = new Static4D[numCubits];
145
    mCubitVariant   = new int[numCubits];
146
    mPositions      = new float[numCubits][];
147
    mCubitFaceColor = new int[numCubits][];
148
    mCubitType      = new int[numCubits];
149
    mCubitRowOffset = new float[numCubits][];
150

    
151
    for(int i=0; i<numCubits; i++)
152
      {
153
      JSONObject jsonCubit = object.getJSONObject(i);
154

    
155
      float qx = (float)jsonCubit.getDouble("qx");
156
      float qy = (float)jsonCubit.getDouble("qy");
157
      float qz = (float)jsonCubit.getDouble("qz");
158
      float qw = (float)jsonCubit.getDouble("qw");
159

    
160
      mCubitQuats[i] = new Static4D(qx,qy,qz,qw);
161
      mCubitVariant[i] = jsonCubit.getInt("variant");
162

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

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

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

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  private void parseShapes(JSONArray object) throws JSONException
184
    {
185
    mNumCubitVariants     = object.length();
186
    mVariantStickerShape  = new int[mNumCubitVariants][];
187
    mVariantFaceIsOuter   = new int[mNumCubitVariants][];
188
    mShapes               = new ObjectShape[mNumCubitVariants];
189
    mFaceShapes           = new ObjectFaceShape[mNumCubitVariants];
190
    mVertexEffects        = new ObjectVertexEffects[mNumCubitVariants];
191
    float[][][] verts     = new float[mNumCubitVariants][][];
192
    float[][][] bands     = new float[mNumCubitVariants][][];
193
    float[][] convexity   = new float[mNumCubitVariants][];
194
    int[][] bandIndices   = new int[mNumCubitVariants][];
195
    int[][][] vertIndices = new int[mNumCubitVariants][][];
196

    
197
    mNumCubitFaces = -1;
198

    
199
    for(int i=0; i<mNumCubitVariants; i++)
200
      {
201
      JSONObject jsonShape = object.getJSONObject(i);
202

    
203
      ////// vertices /////////////////////////////////////////////////
204
      JSONArray jsonVertices= jsonShape.getJSONArray("vertices");
205
      int numVertices = jsonVertices.length();
206
      verts[i] = new float[numVertices][3];
207

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

    
216
      ////// faces ////////////////////////////////////////////////////
217
      JSONArray jsonFaces= jsonShape.getJSONArray("faces");
218
      int numFaces = jsonFaces.length();
219
      mVariantStickerShape[i]= new int[numFaces];
220
      mVariantFaceIsOuter[i] = new int[numFaces];
221
      bandIndices[i] = new int[numFaces];
222
      vertIndices[i] = new int[numFaces][];
223

    
224
      if( mNumCubitFaces<numFaces ) mNumCubitFaces=numFaces;
225

    
226
      for(int j=0; j<numFaces; j++)
227
        {
228
        JSONObject jsonFace = jsonFaces.getJSONObject(j);
229
        mVariantStickerShape[i][j]= jsonFace.getInt("sticker");
230
        mVariantFaceIsOuter[i][j] = jsonFace.optInt("isOuter",0);
231
        bandIndices[i][j] = jsonFace.getInt("bandIndex");
232
        JSONArray vertices = jsonFace.getJSONArray("vertexIndices");
233
        int numV = vertices.length();
234
        vertIndices[i][j] = new int[numV];
235
        for(int k=0; k<numV; k++) vertIndices[i][j][k] = vertices.getInt(k);
236
        }
237

    
238
      ////// bands ////////////////////////////////////////////////////
239
      JSONArray jsonBands= jsonShape.getJSONArray("bands");
240
      int numBands = jsonBands.length();
241
      bands[i] = new float[numBands][7];
242

    
243
      for(int j=0; j<numBands; j++)
244
        {
245
        JSONObject jsonBand = jsonBands.getJSONObject(j);
246

    
247
        bands[i][j][0] = (float)jsonBand.getDouble("height");
248
        bands[i][j][1] = (float)jsonBand.getDouble("angle");
249
        bands[i][j][2] = (float)jsonBand.getDouble("distanceToCenter");
250
        bands[i][j][3] = (float)jsonBand.getDouble("distanceToFlat");
251
        bands[i][j][4] = (float)jsonBand.getDouble("numOfBands");
252
        bands[i][j][5] = (float)jsonBand.getDouble("extraI");
253
        bands[i][j][6] = (float)jsonBand.getDouble("extraJ");
254
        }
255

    
256
      ////// convexity ///////////////////////////////////////////////
257
      JSONObject jsonConvexity = jsonShape.optJSONObject("convexity");
258

    
259
      if( jsonConvexity!=null )
260
        {
261
        convexity[i] = new float[3];
262
        convexity[i][0] = (float)jsonConvexity.getDouble("x");
263
        convexity[i][1] = (float)jsonConvexity.getDouble("y");
264
        convexity[i][2] = (float)jsonConvexity.getDouble("z");
265
        }
266

    
267
      ////// effects /////////////////////////////////////////////////
268
      JSONArray jsonEffects = jsonShape.optJSONArray("effects");
269

    
270
      if( jsonEffects!=null )
271
        {
272
        int numEffects = jsonEffects.length();
273

    
274
        String[] name    = new String[numEffects];
275
        float[][] vars   = new float[numEffects][5];
276
        float[][] center = new float[numEffects][3];
277
        float[][] region = new float[numEffects][4];
278
        boolean[] use    = new boolean[numEffects];
279

    
280
        for(int j=0; j<numEffects; j++)
281
          {
282
          JSONObject jsonEffect = jsonEffects.getJSONObject(j);
283

    
284
          name[j] = jsonEffect.getString("name");
285

    
286
          vars[j][0] = (float)jsonEffect.getDouble("var0");
287
          vars[j][1] = (float)jsonEffect.getDouble("var1");
288
          vars[j][2] = (float)jsonEffect.getDouble("var2");
289
          vars[j][3] = (float)jsonEffect.getDouble("var3");
290
          vars[j][4] = (float)jsonEffect.getDouble("var4");
291

    
292
          center[j][0] = (float)jsonEffect.getDouble("center0");
293
          center[j][1] = (float)jsonEffect.getDouble("center1");
294
          center[j][2] = (float)jsonEffect.getDouble("center2");
295

    
296
          region[j][0] = (float)jsonEffect.getDouble("region0");
297
          region[j][1] = (float)jsonEffect.getDouble("region1");
298
          region[j][2] = (float)jsonEffect.getDouble("region2");
299
          region[j][3] = (float)jsonEffect.getDouble("region3");
300

    
301
          use[j] = jsonEffect.getBoolean("use");
302
          }
303

    
304
        mVertexEffects[i] = new ObjectVertexEffects(name,vars,center,region,use);
305
        }
306
      }
307

    
308
    for(int i=0; i<mNumCubitVariants; i++)
309
      {
310
      mShapes[i] = new ObjectShape(verts[i],vertIndices[i]);
311
      mFaceShapes[i] = new ObjectFaceShape(bands[i],bandIndices[i],convexity[i]);
312
      }
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
  private void parseStickers(JSONArray object) throws JSONException
318
    {
319
    mNumStickerTypes = object.length();
320
    mObjectSticker = new ObjectSticker[mNumStickerTypes];
321

    
322
    for(int i=0; i<mNumStickerTypes; i++)
323
      {
324
      JSONObject sticker = object.getJSONObject(i);
325
      float stroke = (float)sticker.getDouble("stroke");
326
      JSONArray vertices = sticker.getJSONArray("vertices");
327
      int numVertices = vertices.length();
328

    
329
      float[] coords     = new float[2*numVertices];
330
      float[] curvatures = new float[numVertices];
331
      float[] radii      = new float[numVertices];
332

    
333
      for(int j=0; j<numVertices; j++)
334
        {
335
        JSONObject vertex = vertices.getJSONObject(j);
336

    
337
        coords[2*j  ] = (float)vertex.getDouble("x");
338
        coords[2*j+1] = (float)vertex.getDouble("y");
339
        curvatures[j] = (float)vertex.getDouble("angle");
340
        radii[j]      = (float)vertex.getDouble("radius");
341
        }
342

    
343
      mObjectSticker[i] = new ObjectSticker(coords,curvatures,radii,stroke);
344
      }
345
    }
346

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

    
349
  private void parseOverrides(JSONArray object) throws JSONException
350
    {
351
    if( object!=null )
352
      {
353
      int numOverrides = object.length();
354
      mStickerOverrides= new ObjectStickerOverride[numOverrides];
355

    
356
      for(int i=0; i<numOverrides; i++)
357
        {
358
        JSONObject override  = object.getJSONObject(i);
359
        JSONArray cubitArray = override.getJSONArray("cubitfaces");
360
        int color = override.getInt("color");
361
        int numCubits = cubitArray.length();
362
        int[] cubitface = new int[numCubits];
363
        for(int j=0; j<numCubits; j++) cubitface[j] = cubitArray.getInt(j);
364
        mStickerOverrides[i] = new ObjectStickerOverride(cubitface,color);
365
        }
366
      }
367
    else
368
      {
369
      mStickerOverrides = null;
370
      }
371
    }
372

    
373
///////////////////////////////////////////////////////////////////////////////////////////////////
374

    
375
  private void parseMesh(JSONObject object) throws JSONException
376
    {
377
    JSONArray cubits   = object.getJSONArray("cubits");
378
    parseCubits(cubits);
379
    JSONArray shapes   = object.getJSONArray("shapes");
380
    parseShapes(shapes);
381
    JSONArray stickers = object.getJSONArray("stickers");
382
    parseStickers(stickers);
383

    
384
    JSONArray overrides= object.optJSONArray("overrides");
385
    parseOverrides(overrides);
386

    
387
    mPillowCoeff = (float)object.optDouble("pillow",1.0);
388
    }
389

    
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391

    
392
  private void parseAxis(JSONArray object) throws JSONException
393
    {
394
    int numAxis = object.length();
395

    
396
    mBasicAngle     = new int[numAxis][];
397
    mAxis           = new Static3D[numAxis];
398
    mCuts           = new float[numAxis][];
399
    mLayerRotatable = new boolean[numAxis][];
400
    mNumLayers      = new int[numAxis];
401

    
402
    for(int i=0; i<numAxis; i++)
403
      {
404
      JSONObject jsonAx = object.getJSONObject(i);
405

    
406
      float x = (float)jsonAx.getDouble("x");
407
      float y = (float)jsonAx.getDouble("y");
408
      float z = (float)jsonAx.getDouble("z");
409

    
410
      mAxis[i] = new Static3D(x,y,z);
411

    
412
      JSONArray jsonAngles = jsonAx.getJSONArray("basicAngles");
413
      int numAngles = jsonAngles.length();
414
      mBasicAngle[i] = new int[numAngles];
415
      for(int j=0; j<numAngles; j++) mBasicAngle[i][j] = jsonAngles.getInt(j);
416

    
417
      JSONArray jsonCuts = jsonAx.getJSONArray("cuts");
418
      int numCuts = jsonCuts.length();
419
      mCuts[i] = new float[numCuts];
420
      for(int j=0; j<numCuts; j++) mCuts[i][j] = (float)jsonCuts.getDouble(j);
421

    
422
      JSONArray jsonRota = jsonAx.getJSONArray("rotatable");
423
      int numRota = jsonRota.length();
424
      mLayerRotatable[i] = new boolean[numRota];
425
      for(int j=0; j<numRota; j++) mLayerRotatable[i][j] = jsonRota.getBoolean(j);
426

    
427
      mNumLayers[i] = numRota;
428
      }
429
    }
430

    
431
///////////////////////////////////////////////////////////////////////////////////////////////////
432

    
433
  private void parseQuats(JSONArray object) throws JSONException
434
    {
435
    int numQuats = object.length();
436
    mQuats = new Static4D[numQuats];
437

    
438
    for(int i=0; i<numQuats; i++)
439
      {
440
      JSONObject jsonQuat = object.getJSONObject(i);
441

    
442
      float x = (float)jsonQuat.getDouble("x");
443
      float y = (float)jsonQuat.getDouble("y");
444
      float z = (float)jsonQuat.getDouble("z");
445
      float w = (float)jsonQuat.getDouble("w");
446

    
447
      mQuats[i] = new Static4D(x,y,z,w);
448
      }
449
    }
450

    
451
///////////////////////////////////////////////////////////////////////////////////////////////////
452

    
453
  private int getAlgorithmIndex(int ax, int layer, int angle)
454
    {
455
    int numAlgs = mAlgorithms.length;
456

    
457
    for(int alg=0; alg<numAlgs; alg++)
458
      {
459
      int[] a = mAlgorithms[alg];
460
      if( a.length>=3 && a[0]==ax && a[1]==layer && a[2]==angle ) return alg;
461
      }
462

    
463
    return -1;
464
    }
465

    
466
///////////////////////////////////////////////////////////////////////////////////////////////////
467

    
468
  private int[] produceEdge(int[][] scramblingData)
469
    {
470
    int index=0, length=0;
471
    int numAxis = scramblingData.length;
472
    for (int[] scramblingDatum : scramblingData) length += scramblingDatum.length;
473

    
474
    int[] ret = new int[2*length/3];
475

    
476
    for(int ax=0; ax<numAxis; ax++)
477
      {
478
      int[] data = scramblingData[ax];
479
      int num = data.length/3;
480

    
481
      for(int j=0; j<num; j++)
482
        {
483
        int layer = data[3*j];
484
        int angle = data[3*j+1];
485
        int state = data[3*j+2];
486

    
487
        ret[2*index  ] = getAlgorithmIndex(ax,layer,angle);
488
        ret[2*index+1] = state;
489
        index++;
490
        }
491
      }
492

    
493
    return ret;
494
    }
495

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497

    
498
  private void parseScrambling6(JSONObject object) throws JSONException
499
    {
500
    JSONArray jsonStates = object.getJSONArray("scrambleStates");
501
    int numStates = jsonStates.length();
502
    int[][][] scramblingData = new int[numStates][][];
503

    
504
    for(int i=0; i<numStates; i++)
505
      {
506
      JSONArray jsonState = jsonStates.getJSONArray(i);
507
      int numAxis = jsonState.length();
508
      scramblingData[i] = new int[numAxis][];
509

    
510
      for(int j=0; j<numAxis; j++)
511
        {
512
        JSONArray jsonData = jsonState.getJSONArray(j);
513
        int numData = jsonData.length();
514
        scramblingData[i][j] = new int[numData];
515
        for(int k=0; k<numData; k++) scramblingData[i][j][k] = jsonData.getInt(k);
516
        }
517
      }
518

    
519
    mAlgorithms = ScrambleEdgeGenerator.getScramblingAlgorithms(mBasicAngle);
520
    mEdges = new int[numStates][];
521
    for(int i=0; i<numStates; i++) mEdges[i] = produceEdge(scramblingData[i]);
522
    }
523

    
524
///////////////////////////////////////////////////////////////////////////////////////////////////
525

    
526
  private void parseScrambling7to10(JSONObject object) throws JSONException
527
    {
528
    JSONArray jsonAlgorithms = object.getJSONArray("algorithms");
529
    int numAlgs = jsonAlgorithms.length();
530
    mAlgorithms = new int[numAlgs][];
531

    
532
    for(int i=0; i<numAlgs; i++)
533
      {
534
      JSONArray jsonAlg = jsonAlgorithms.getJSONArray(i);
535
      int numEntries = jsonAlg.length();
536
      mAlgorithms[i] = new int[numEntries];
537
      for(int j=0; j<numEntries; j++)
538
        {
539
        int entry = jsonAlg.getInt(j);
540
        mAlgorithms[i][j] = ( (j%3)==1 ? (1<<entry) : entry );
541
        }
542
      }
543

    
544
    JSONArray jsonEdges = object.getJSONArray("edges");
545
    int numEdges = jsonEdges.length();
546
    mEdges = new int[numEdges][];
547

    
548
    for(int i=0; i<numEdges; i++)
549
      {
550
      JSONArray jsonEdge = jsonEdges.getJSONArray(i);
551
      int numEntries = jsonEdge.length();
552
      mEdges[i] = new int[numEntries];
553
      for(int j=0; j<numEntries; j++) mEdges[i][j] = jsonEdge.getInt(j);
554
      }
555
    }
556

    
557
///////////////////////////////////////////////////////////////////////////////////////////////////
558

    
559
  private void parseScrambling11orMore(JSONObject object) throws JSONException
560
    {
561
    JSONArray jsonAlgorithms = object.getJSONArray("algorithms");
562
    int numAlgs = jsonAlgorithms.length();
563
    mAlgorithms = new int[numAlgs][];
564

    
565
    for(int i=0; i<numAlgs; i++)
566
      {
567
      JSONArray jsonAlg = jsonAlgorithms.getJSONArray(i);
568
      int numEntries = jsonAlg.length();
569
      mAlgorithms[i] = new int[numEntries];
570
      for(int j=0; j<numEntries; j++) mAlgorithms[i][j] = jsonAlg.getInt(j);
571
      }
572

    
573
    JSONArray jsonEdges = object.getJSONArray("edges");
574
    int numEdges = jsonEdges.length();
575
    mEdges = new int[numEdges][];
576

    
577
    for(int i=0; i<numEdges; i++)
578
      {
579
      JSONArray jsonEdge = jsonEdges.getJSONArray(i);
580
      int numEntries = jsonEdge.length();
581
      mEdges[i] = new int[numEntries];
582
      for(int j=0; j<numEntries; j++) mEdges[i][j] = jsonEdge.getInt(j);
583
      }
584
    }
585

    
586
///////////////////////////////////////////////////////////////////////////////////////////////////
587

    
588
  private void parseScrambling(JSONObject object, int major) throws JSONException
589
    {
590
    mScrambleType = object.getInt("scrambleType");
591

    
592
    if( mScrambleType==0 )
593
      {
594
      if( major==6 )       parseScrambling6(object);
595
      else if( major<=10 ) parseScrambling7to10(object);
596
      else                 parseScrambling11orMore(object);
597
      }
598
    }
599

    
600
///////////////////////////////////////////////////////////////////////////////////////////////////
601

    
602
  private void parseTouchcontrol(JSONObject object) throws JSONException
603
    {
604
    mMovementType = object.getInt("movementType");
605
    mMovementSplit= object.getInt("movementSplit");
606

    
607
    try
608
      {
609
      JSONArray jsonEnabled = object.getJSONArray("enabledAxis");
610
      int numFace = jsonEnabled.length();
611

    
612
      mEnabled = new int[numFace][][];
613

    
614
      for(int i=0; i<numFace; i++)
615
        {
616
        JSONArray jsonSection = jsonEnabled.getJSONArray(i);
617
        int numSection = jsonSection.length();
618
        mEnabled[i] = new int[numSection][];
619

    
620
        for(int j=0; j<numSection; j++)
621
          {
622
          JSONArray jsonAx = jsonSection.getJSONArray(j);
623
          int numAxis = jsonAx.length();
624
          mEnabled[i][j] = new int[numAxis];
625
          for(int k=0; k<numAxis; k++) mEnabled[i][j][k] = jsonAx.getInt(k);
626
          }
627
        }
628
      }
629
    catch( JSONException ex )
630
      {
631
      // ignore, the object does not have to have 'enabledAxis' defined at all.
632
      }
633

    
634
    try
635
      {
636
      JSONArray jsonDist = object.getJSONArray("dist3D");
637
      int num = jsonDist.length();
638
      mDist3D = new float[num];
639
      for(int j=0; j<num; j++) mDist3D[j] = (float)jsonDist.getDouble(j);
640
      }
641
    catch( JSONException ex )
642
      {
643
      // ignore, the object does not have a 'dist3D' which is possible.
644
      }
645
    }
646

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

    
649
  private void parseColors(JSONArray object) throws JSONException
650
    {
651
    mNumFaceColors = object.length()-1;
652

    
653
    mColor = new int[mNumFaceColors];
654
    for(int i=0; i<mNumFaceColors; i++) mColor[i] = object.getInt(i);
655

    
656
    mInternalColor = object.getInt(mNumFaceColors);
657
    }
658

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

    
661
  private void parseSolved(JSONObject solved) throws JSONException
662
    {
663
    mSolvedFuncIndex = solved.getInt("functionIndex");
664

    
665
    try
666
      {
667
      JSONArray groupArray= solved.getJSONArray("groups");
668
      int numGroups = groupArray.length();
669
      mSolvedQuats  = new int[numGroups][];
670

    
671
      for(int i=0; i<numGroups; i++)
672
        {
673
        JSONArray groupElements = groupArray.getJSONArray(i);
674
        int groupSize = groupElements.length();
675
        mSolvedQuats[i] = new int[groupSize];
676
        for(int j=0; j<groupSize; j++) mSolvedQuats[i][j] = groupElements.getInt(j);
677
        }
678
      }
679
    catch( JSONException ex )
680
      {
681
      // ignore, the object does not have to have an array of solved groups.
682
      }
683
    }
684

    
685
///////////////////////////////////////////////////////////////////////////////////////////////////
686

    
687
  private void parseFile(JSONObject object, int major) throws JSONException
688
    {
689
    JSONObject metadata    = object.getJSONObject("metadata");
690
    parseMetadata(metadata);
691
    JSONObject mesh        = object.getJSONObject("mesh");
692
    parseMesh(mesh);
693
    JSONArray axis         = object.getJSONArray("axis");
694
    parseAxis(axis);
695
    JSONArray quats        = object.getJSONArray("quats");
696
    parseQuats(quats);
697
    JSONObject scrambling  = object.getJSONObject("scrambling");
698
    parseScrambling(scrambling,major);
699
    JSONObject touchcontrol= object.getJSONObject("touchcontrol");
700
    parseTouchcontrol(touchcontrol);
701
    JSONArray colors       = object.getJSONArray("colors");
702
    parseColors(colors);
703
    JSONObject solved      = object.getJSONObject("solved");
704
    parseSolved(solved);
705
    }
706

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

    
709
  private void parseTutorial(JSONObject object) throws JSONException
710
    {
711
    mTutorialObject = object.getString("object");
712
    JSONArray tuts= object.getJSONArray("tutorials");
713

    
714
    int len = tuts.length();
715
    mTutorials = new String[len][4];
716

    
717
    for(int i=0; i<len; i++)
718
      {
719
      JSONObject tut = tuts.getJSONObject(i);
720
      mTutorials[i][0] = tut.getString("language");
721
      mTutorials[i][1] = tut.getString("link");
722
      mTutorials[i][2] = tut.getString("title");
723
      mTutorials[i][3] = tut.getString("author");
724
      }
725
    }
726

    
727
///////////////////////////////////////////////////////////////////////////////////////////////////
728
// PUBLIC
729
///////////////////////////////////////////////////////////////////////////////////////////////////
730

    
731
  public void parseJsonFile(InputStream jsonStream) throws JSONException, IOException
732
    {
733
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
734
    StringBuilder contents = new StringBuilder();
735
    String line;
736

    
737
    while( (line = br.readLine()) != null) contents.append(line);
738
    br.close();
739
    jsonStream.close();
740
    String contentsString = contents.toString();
741
    JSONObject object = new JSONObject(contentsString);
742
    int major = object.getInt("major");
743

    
744
    if( major>=6 )
745
      {
746
      parseFile(object,major);
747
      }
748
    else
749
      {
750
      android.util.Log.e("readJsonFile", "Unknown version "+major);
751
      }
752
    }
753

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

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

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

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

    
769
    if( major>=6 )
770
      {
771
      JSONObject metadata = object.getJSONObject("metadata");
772
      parseMetadata(metadata);
773
      }
774
    else
775
      {
776
      android.util.Log.e("readJsonFileQuick", "Unknown version "+major);
777
      }
778
    }
779

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

    
782
  public void readNumScramblesAndPrice(InputStream stream) throws IOException, JSONException
783
    {
784
    BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
785

    
786
    StringBuilder contents = new StringBuilder();
787
    String tmp;
788
    while( (tmp = br.readLine()) != null) contents.append(tmp);
789
    br.close();
790
    stream.close();
791

    
792
    JSONObject object = new JSONObject(contents.toString());
793
    JSONObject metadata = object.getJSONObject("metadata");
794
    mNumScrambles = metadata.getInt("scrambles");
795

    
796
    boolean free = metadata.optBoolean("free",true);
797
    if( free ) mPrice = 0;
798
    else mPrice = metadata.optInt("price", ObjectType.DEFAULT_PRICE_OF_OLD_OBJECTS );
799
    }
800

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

    
803
  public void parseJsonTutorial(InputStream jsonStream) throws IOException, JSONException
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==1 )
817
      {
818
      parseTutorial(object);
819
      }
820
    else
821
      {
822
      android.util.Log.e("readJsonFile", "Unknown tutorial version "+major);
823
      }
824
    }
825

    
826
///////////////////////////////////////////////////////////////////////////////////////////////////
827

    
828
  public int getScrambleType()
829
    {
830
    return mScrambleType;
831
    }
832

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

    
835
  public int[][] getScrambleEdges()
836
    {
837
    return mEdges;
838
    }
839

    
840
///////////////////////////////////////////////////////////////////////////////////////////////////
841

    
842
  public int[][] getScrambleAlgorithms()
843
    {
844
    return mAlgorithms;
845
    }
846

    
847
///////////////////////////////////////////////////////////////////////////////////////////////////
848

    
849
  public int[][] getSolvedQuats()
850
    {
851
    return mSolvedQuats;
852
    }
853

    
854
///////////////////////////////////////////////////////////////////////////////////////////////////
855

    
856
  public Static4D[] getQuats()
857
    {
858
    return mQuats;
859
    }
860

    
861
///////////////////////////////////////////////////////////////////////////////////////////////////
862

    
863
  public int getSolvedFunctionIndex()
864
    {
865
    return mSolvedFuncIndex;
866
    }
867

    
868
///////////////////////////////////////////////////////////////////////////////////////////////////
869

    
870
  public int getNumStickerTypes()
871
    {
872
    return mNumStickerTypes;
873
    }
874

    
875
///////////////////////////////////////////////////////////////////////////////////////////////////
876

    
877
  public float[][] getCuts()
878
    {
879
    return mCuts;
880
    }
881

    
882
///////////////////////////////////////////////////////////////////////////////////////////////////
883

    
884
  public boolean[][] getLayerRotatable()
885
    {
886
    return mLayerRotatable;
887
    }
888

    
889
///////////////////////////////////////////////////////////////////////////////////////////////////
890

    
891
  public int getMovementType()
892
    {
893
    return mMovementType;
894
    }
895

    
896
///////////////////////////////////////////////////////////////////////////////////////////////////
897

    
898
  public int getMovementSplit()
899
    {
900
    return mMovementSplit;
901
    }
902

    
903
///////////////////////////////////////////////////////////////////////////////////////////////////
904

    
905
  public int[][][] getEnabled()
906
    {
907
    return mEnabled;
908
    }
909

    
910
///////////////////////////////////////////////////////////////////////////////////////////////////
911

    
912
  public float[] getDist3D()
913
    {
914
    return mDist3D;
915
    }
916

    
917
///////////////////////////////////////////////////////////////////////////////////////////////////
918

    
919
  public int getNumCubitFaces()
920
    {
921
    return mNumCubitFaces;
922
    }
923

    
924
///////////////////////////////////////////////////////////////////////////////////////////////////
925

    
926
  public float[][] getCubitPositions()
927
    {
928
    return mPositions;
929
    }
930

    
931
///////////////////////////////////////////////////////////////////////////////////////////////////
932

    
933
  public ObjectShape getObjectShape(int variant)
934
    {
935
    return mShapes[variant];
936
    }
937

    
938
///////////////////////////////////////////////////////////////////////////////////////////////////
939

    
940
  public ObjectFaceShape getObjectFaceShape(int variant)
941
    {
942
    return mFaceShapes[variant];
943
    }
944

    
945
///////////////////////////////////////////////////////////////////////////////////////////////////
946

    
947
  public ObjectVertexEffects getVertexEffects(int variant)
948
    {
949
    return mVertexEffects[variant];
950
    }
951

    
952
///////////////////////////////////////////////////////////////////////////////////////////////////
953

    
954
  public Static4D getCubitQuats(int cubit)
955
    {
956
    return mCubitQuats[cubit];
957
    }
958

    
959
///////////////////////////////////////////////////////////////////////////////////////////////////
960

    
961
  public int getNumCubitVariants()
962
    {
963
    return mNumCubitVariants;
964
    }
965

    
966
///////////////////////////////////////////////////////////////////////////////////////////////////
967

    
968
  public int getCubitVariant(int cubit)
969
    {
970
    return mCubitVariant[cubit];
971
    }
972

    
973
///////////////////////////////////////////////////////////////////////////////////////////////////
974

    
975
  public int getVariantStickerShape(int variant, int face)
976
    {
977
    int[] shapes = mVariantStickerShape[variant];
978
    return shapes.length>face ? shapes[face] : -1;
979
    }
980

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

    
983
  public int[] getCubitTypes()
984
    {
985
    return mCubitType;
986
    }
987

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

    
990
  public float[][] getCubitOffsets()
991
    {
992
    return mCubitRowOffset;
993
    }
994

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

    
997
  public int[][] getVariantFaceIsOuter()
998
    {
999
    return mVariantFaceIsOuter;
1000
    }
1001

    
1002
///////////////////////////////////////////////////////////////////////////////////////////////////
1003

    
1004
  public int getCubitFaceFace(int cubit, int face)
1005
    {
1006
    return mCubitFaceColor[cubit][face];
1007
    }
1008

    
1009
///////////////////////////////////////////////////////////////////////////////////////////////////
1010

    
1011
  public int getNumScrambles()
1012
    {
1013
    return mNumScrambles;
1014
    }
1015

    
1016
///////////////////////////////////////////////////////////////////////////////////////////////////
1017

    
1018
  public int getPrice()
1019
    {
1020
    return mPrice;
1021
    }
1022

    
1023
///////////////////////////////////////////////////////////////////////////////////////////////////
1024

    
1025
  public ObjectSticker retSticker(int sticker)
1026
    {
1027
    return mObjectSticker[sticker];
1028
    }
1029

    
1030
///////////////////////////////////////////////////////////////////////////////////////////////////
1031

    
1032
  public ObjectStickerOverride[] getStickerOverrides()
1033
    {
1034
    return mStickerOverrides;
1035
    }
1036

    
1037
///////////////////////////////////////////////////////////////////////////////////////////////////
1038

    
1039
  public float getPillowCoeff()
1040
    {
1041
    return mPillowCoeff;
1042
    }
1043

    
1044
///////////////////////////////////////////////////////////////////////////////////////////////////
1045

    
1046
  public Static3D[] getRotationAxis()
1047
    {
1048
    return mAxis;
1049
    }
1050

    
1051
///////////////////////////////////////////////////////////////////////////////////////////////////
1052

    
1053
  public int[][] getBasicAngle()
1054
    {
1055
    return mBasicAngle;
1056
    }
1057

    
1058
///////////////////////////////////////////////////////////////////////////////////////////////////
1059

    
1060
  public ObjectSignature getSignature()
1061
    {
1062
    return mSignature;
1063
    }
1064

    
1065
///////////////////////////////////////////////////////////////////////////////////////////////////
1066

    
1067
  public String getObjectName()
1068
    {
1069
    return mLongName;
1070
    }
1071

    
1072
///////////////////////////////////////////////////////////////////////////////////////////////////
1073

    
1074
  public String getShortName()
1075
    {
1076
    return mShortName;
1077
    }
1078

    
1079
///////////////////////////////////////////////////////////////////////////////////////////////////
1080

    
1081
  public String getInventor()
1082
    {
1083
    return mInventor;
1084
    }
1085

    
1086
///////////////////////////////////////////////////////////////////////////////////////////////////
1087

    
1088
  public int getYearOfInvention()
1089
    {
1090
    return mYearOfInvention;
1091
    }
1092

    
1093
///////////////////////////////////////////////////////////////////////////////////////////////////
1094

    
1095
  public int getComplexity()
1096
    {
1097
    return mComplexity;
1098
    }
1099

    
1100
///////////////////////////////////////////////////////////////////////////////////////////////////
1101

    
1102
  public int getNumFaces()
1103
    {
1104
    return mNumFaces;
1105
    }
1106

    
1107
///////////////////////////////////////////////////////////////////////////////////////////////////
1108

    
1109
  public int getNumFaceColors()
1110
    {
1111
    return mNumFaceColors;
1112
    }
1113

    
1114
///////////////////////////////////////////////////////////////////////////////////////////////////
1115

    
1116
  public int[] getNumLayers()
1117
    {
1118
    return mNumLayers;
1119
    }
1120

    
1121
///////////////////////////////////////////////////////////////////////////////////////////////////
1122

    
1123
  public float getSize()
1124
    {
1125
    return mSize;
1126
    }
1127

    
1128
///////////////////////////////////////////////////////////////////////////////////////////////////
1129

    
1130
  public int getColor(int face)
1131
    {
1132
    return mColor[face];
1133
    }
1134

    
1135
///////////////////////////////////////////////////////////////////////////////////////////////////
1136

    
1137
  public int getInternalColor()
1138
    {
1139
    return mInternalColor;
1140
    }
1141

    
1142
///////////////////////////////////////////////////////////////////////////////////////////////////
1143

    
1144
  public boolean shouldResetTextureMaps()
1145
    {
1146
    return mResetMaps;
1147
    }
1148

    
1149
///////////////////////////////////////////////////////////////////////////////////////////////////
1150

    
1151
  public String getTutorialObject()
1152
    {
1153
    return mTutorialObject;
1154
    }
1155

    
1156
///////////////////////////////////////////////////////////////////////////////////////////////////
1157

    
1158
  public String[][] getTutorials()
1159
    {
1160
    return mTutorials;
1161
    }
1162
}
(1-1/2)