Project

General

Profile

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

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

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 float[][] mRotationFactor;
51
  private int mMovementType, mMovementSplit;
52
  private int[][][] mEnabled;
53
  private float[] mDist3D;
54
  private int mNumCubitFaces, mNumFaces, mNumFaceColors;
55
  private float[][] mPositions;
56
  private ObjectShape[] mShapes;
57
  private ObjectFaceShape[] mFaceShapes;
58
  private ObjectVertexEffects[] mVertexEffects;
59
  private Static4D[] mCubitQuats;
60
  private int mNumCubitVariants;
61
  private int[] mCubitVariant;
62
  private int[][] mVariantStickerShape, mVariantFaceIsOuter, mCubitFaceColor;
63
  private ObjectSticker[] mObjectSticker;
64
  private Static3D[] mAxis;
65
  private int[][] mBasicAngle;
66
  private String mLongName, mShortName, mInventor;
67
  private int mYearOfInvention, mComplexity;
68
  private int[] mNumLayers;
69
  private float mSize;
70
  private int mScrambleType, mNumScrambles;
71
  private int mPrice;
72
  private int[] mColor;
73
  private int mInternalColor;
74
  private boolean mResetMaps;
75
  private String mTutorialObject;
76
  private String[][] mTutorials;
77
  private ObjectSignature mSignature;
78
  private int[] mCubitType;
79
  private float[][] mCubitRowOffset;
80
  private ObjectStickerOverride[] mStickerOverrides;
81
  private float mPillowCoeff;
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

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

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

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

    
102
    return new ObjectSignature(signature);
103
    }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

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

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

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

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

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

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

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

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

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

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

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

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

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

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

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

    
198
    mNumCubitFaces = -1;
199

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317

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

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

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

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

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

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

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349

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

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

    
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375

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

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

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

    
391
///////////////////////////////////////////////////////////////////////////////////////////////////
392

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

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

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

    
408
      float x = (float)jsonAx.getDouble("x");
409
      float y = (float)jsonAx.getDouble("y");
410
      float z = (float)jsonAx.getDouble("z");
411

    
412
      mAxis[i] = new Static3D(x,y,z);
413

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

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

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

    
429
      JSONArray jsonFactor = jsonAx.optJSONArray("factor");
430

    
431
      if( jsonFactor!=null )
432
        {
433
        int numFactor = jsonFactor.length();
434
        mRotationFactor[i] = new float[numFactor];
435
        for(int j=0; j<numFactor; j++) mRotationFactor[i][j] = (float)jsonFactor.getDouble(j);
436
        }
437
      else
438
        {
439
        mRotationFactor[i] = new float[numRota];
440
        for(int j=0; j<numRota; j++) mRotationFactor[i][j] = 1.0f;
441
        }
442

    
443
      mNumLayers[i] = numRota;
444
      }
445
    }
446

    
447
///////////////////////////////////////////////////////////////////////////////////////////////////
448

    
449
  private void parseQuats(JSONArray object) throws JSONException
450
    {
451
    int numQuats = object.length();
452
    mQuats = new Static4D[numQuats];
453

    
454
    for(int i=0; i<numQuats; i++)
455
      {
456
      JSONObject jsonQuat = object.getJSONObject(i);
457

    
458
      float x = (float)jsonQuat.getDouble("x");
459
      float y = (float)jsonQuat.getDouble("y");
460
      float z = (float)jsonQuat.getDouble("z");
461
      float w = (float)jsonQuat.getDouble("w");
462

    
463
      mQuats[i] = new Static4D(x,y,z,w);
464
      }
465
    }
466

    
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468

    
469
  private int getAlgorithmIndex(int ax, int layer, int angle)
470
    {
471
    int numAlgs = mAlgorithms.length;
472

    
473
    for(int alg=0; alg<numAlgs; alg++)
474
      {
475
      int[] a = mAlgorithms[alg];
476
      if( a.length>=3 && a[0]==ax && a[1]==layer && a[2]==angle ) return alg;
477
      }
478

    
479
    return -1;
480
    }
481

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

    
484
  private int[] produceEdge(int[][] scramblingData)
485
    {
486
    int index=0, length=0;
487
    int numAxis = scramblingData.length;
488
    for (int[] scramblingDatum : scramblingData) length += scramblingDatum.length;
489

    
490
    int[] ret = new int[2*length/3];
491

    
492
    for(int ax=0; ax<numAxis; ax++)
493
      {
494
      int[] data = scramblingData[ax];
495
      int num = data.length/3;
496

    
497
      for(int j=0; j<num; j++)
498
        {
499
        int layer = data[3*j];
500
        int angle = data[3*j+1];
501
        int state = data[3*j+2];
502

    
503
        ret[2*index  ] = getAlgorithmIndex(ax,layer,angle);
504
        ret[2*index+1] = state;
505
        index++;
506
        }
507
      }
508

    
509
    return ret;
510
    }
511

    
512
///////////////////////////////////////////////////////////////////////////////////////////////////
513

    
514
  private void parseScrambling6(JSONObject object) throws JSONException
515
    {
516
    JSONArray jsonStates = object.getJSONArray("scrambleStates");
517
    int numStates = jsonStates.length();
518
    int[][][] scramblingData = new int[numStates][][];
519

    
520
    for(int i=0; i<numStates; i++)
521
      {
522
      JSONArray jsonState = jsonStates.getJSONArray(i);
523
      int numAxis = jsonState.length();
524
      scramblingData[i] = new int[numAxis][];
525

    
526
      for(int j=0; j<numAxis; j++)
527
        {
528
        JSONArray jsonData = jsonState.getJSONArray(j);
529
        int numData = jsonData.length();
530
        scramblingData[i][j] = new int[numData];
531
        for(int k=0; k<numData; k++) scramblingData[i][j][k] = jsonData.getInt(k);
532
        }
533
      }
534

    
535
    mAlgorithms = ScrambleEdgeGenerator.getScramblingAlgorithms(mBasicAngle);
536
    mEdges = new int[numStates][];
537
    for(int i=0; i<numStates; i++) mEdges[i] = produceEdge(scramblingData[i]);
538
    }
539

    
540
///////////////////////////////////////////////////////////////////////////////////////////////////
541

    
542
  private void parseScrambling7to10(JSONObject object) throws JSONException
543
    {
544
    JSONArray jsonAlgorithms = object.getJSONArray("algorithms");
545
    int numAlgs = jsonAlgorithms.length();
546
    mAlgorithms = new int[numAlgs][];
547

    
548
    for(int i=0; i<numAlgs; i++)
549
      {
550
      JSONArray jsonAlg = jsonAlgorithms.getJSONArray(i);
551
      int numEntries = jsonAlg.length();
552
      mAlgorithms[i] = new int[numEntries];
553
      for(int j=0; j<numEntries; j++)
554
        {
555
        int entry = jsonAlg.getInt(j);
556
        mAlgorithms[i][j] = ( (j%3)==1 ? (1<<entry) : entry );
557
        }
558
      }
559

    
560
    JSONArray jsonEdges = object.getJSONArray("edges");
561
    int numEdges = jsonEdges.length();
562
    mEdges = new int[numEdges][];
563

    
564
    for(int i=0; i<numEdges; i++)
565
      {
566
      JSONArray jsonEdge = jsonEdges.getJSONArray(i);
567
      int numEntries = jsonEdge.length();
568
      mEdges[i] = new int[numEntries];
569
      for(int j=0; j<numEntries; j++) mEdges[i][j] = jsonEdge.getInt(j);
570
      }
571
    }
572

    
573
///////////////////////////////////////////////////////////////////////////////////////////////////
574

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

    
581
    for(int i=0; i<numAlgs; i++)
582
      {
583
      JSONArray jsonAlg = jsonAlgorithms.getJSONArray(i);
584
      int numEntries = jsonAlg.length();
585
      mAlgorithms[i] = new int[numEntries];
586
      for(int j=0; j<numEntries; j++) mAlgorithms[i][j] = jsonAlg.getInt(j);
587
      }
588

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

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

    
602
///////////////////////////////////////////////////////////////////////////////////////////////////
603

    
604
  private void parseScrambling(JSONObject object, int major) throws JSONException
605
    {
606
    mScrambleType = object.getInt("scrambleType");
607

    
608
    if( mScrambleType==0 )
609
      {
610
      if( major==6 )       parseScrambling6(object);
611
      else if( major<=10 ) parseScrambling7to10(object);
612
      else                 parseScrambling11orMore(object);
613
      }
614
    }
615

    
616
///////////////////////////////////////////////////////////////////////////////////////////////////
617

    
618
  private void parseTouchcontrol(JSONObject object) throws JSONException
619
    {
620
    mMovementType = object.getInt("movementType");
621
    mMovementSplit= object.getInt("movementSplit");
622

    
623
    try
624
      {
625
      JSONArray jsonEnabled = object.getJSONArray("enabledAxis");
626
      int numFace = jsonEnabled.length();
627

    
628
      mEnabled = new int[numFace][][];
629

    
630
      for(int i=0; i<numFace; i++)
631
        {
632
        JSONArray jsonSection = jsonEnabled.getJSONArray(i);
633
        int numSection = jsonSection.length();
634
        mEnabled[i] = new int[numSection][];
635

    
636
        for(int j=0; j<numSection; j++)
637
          {
638
          JSONArray jsonAx = jsonSection.getJSONArray(j);
639
          int numAxis = jsonAx.length();
640
          mEnabled[i][j] = new int[numAxis];
641
          for(int k=0; k<numAxis; k++) mEnabled[i][j][k] = jsonAx.getInt(k);
642
          }
643
        }
644
      }
645
    catch( JSONException ex )
646
      {
647
      // ignore, the object does not have to have 'enabledAxis' defined at all.
648
      }
649

    
650
    try
651
      {
652
      JSONArray jsonDist = object.getJSONArray("dist3D");
653
      int num = jsonDist.length();
654
      mDist3D = new float[num];
655
      for(int j=0; j<num; j++) mDist3D[j] = (float)jsonDist.getDouble(j);
656
      }
657
    catch( JSONException ex )
658
      {
659
      // ignore, the object does not have a 'dist3D' which is possible.
660
      }
661
    }
662

    
663
///////////////////////////////////////////////////////////////////////////////////////////////////
664

    
665
  private void parseColors(JSONArray object) throws JSONException
666
    {
667
    mNumFaceColors = object.length()-1;
668

    
669
    mColor = new int[mNumFaceColors];
670
    for(int i=0; i<mNumFaceColors; i++) mColor[i] = object.getInt(i);
671

    
672
    mInternalColor = object.getInt(mNumFaceColors);
673
    }
674

    
675
///////////////////////////////////////////////////////////////////////////////////////////////////
676

    
677
  private void parseSolved(JSONObject solved) throws JSONException
678
    {
679
    mSolvedFuncIndex = solved.getInt("functionIndex");
680

    
681
    try
682
      {
683
      JSONArray groupArray= solved.getJSONArray("groups");
684
      int numGroups = groupArray.length();
685
      mSolvedQuats  = new int[numGroups][];
686

    
687
      for(int i=0; i<numGroups; i++)
688
        {
689
        JSONArray groupElements = groupArray.getJSONArray(i);
690
        int groupSize = groupElements.length();
691
        mSolvedQuats[i] = new int[groupSize];
692
        for(int j=0; j<groupSize; j++) mSolvedQuats[i][j] = groupElements.getInt(j);
693
        }
694
      }
695
    catch( JSONException ex )
696
      {
697
      // ignore, the object does not have to have an array of solved groups.
698
      }
699
    }
700

    
701
///////////////////////////////////////////////////////////////////////////////////////////////////
702

    
703
  private void parseFile(JSONObject object, int major) throws JSONException
704
    {
705
    JSONObject metadata    = object.getJSONObject("metadata");
706
    parseMetadata(metadata);
707
    JSONObject mesh        = object.getJSONObject("mesh");
708
    parseMesh(mesh);
709
    JSONArray axis         = object.getJSONArray("axis");
710
    parseAxis(axis);
711
    JSONArray quats        = object.getJSONArray("quats");
712
    parseQuats(quats);
713
    JSONObject scrambling  = object.getJSONObject("scrambling");
714
    parseScrambling(scrambling,major);
715
    JSONObject touchcontrol= object.getJSONObject("touchcontrol");
716
    parseTouchcontrol(touchcontrol);
717
    JSONArray colors       = object.getJSONArray("colors");
718
    parseColors(colors);
719
    JSONObject solved      = object.getJSONObject("solved");
720
    parseSolved(solved);
721
    }
722

    
723
///////////////////////////////////////////////////////////////////////////////////////////////////
724

    
725
  private void parseTutorial(JSONObject object) throws JSONException
726
    {
727
    mTutorialObject = object.getString("object");
728
    JSONArray tuts= object.getJSONArray("tutorials");
729

    
730
    int len = tuts.length();
731
    mTutorials = new String[len][4];
732

    
733
    for(int i=0; i<len; i++)
734
      {
735
      JSONObject tut = tuts.getJSONObject(i);
736
      mTutorials[i][0] = tut.getString("language");
737
      mTutorials[i][1] = tut.getString("link");
738
      mTutorials[i][2] = tut.getString("title");
739
      mTutorials[i][3] = tut.getString("author");
740
      }
741
    }
742

    
743
///////////////////////////////////////////////////////////////////////////////////////////////////
744
// PUBLIC
745
///////////////////////////////////////////////////////////////////////////////////////////////////
746

    
747
  public void parseJsonFile(InputStream jsonStream) throws JSONException, IOException
748
    {
749
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
750
    StringBuilder contents = new StringBuilder();
751
    String line;
752

    
753
    while( (line = br.readLine()) != null) contents.append(line);
754
    br.close();
755
    jsonStream.close();
756
    String contentsString = contents.toString();
757
    JSONObject object = new JSONObject(contentsString);
758
    int major = object.getInt("major");
759

    
760
    if( major>=6 )
761
      {
762
      parseFile(object,major);
763
      }
764
    else
765
      {
766
      android.util.Log.e("readJsonFile", "Unknown version "+major);
767
      }
768
    }
769

    
770
///////////////////////////////////////////////////////////////////////////////////////////////////
771

    
772
  public void parseJsonFileMetadata(InputStream jsonStream) throws JSONException, IOException
773
    {
774
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
775
    StringBuilder contents = new StringBuilder();
776
    String tmp;
777

    
778
    while( (tmp = br.readLine()) != null) contents.append(tmp);
779
    br.close();
780
    jsonStream.close();
781

    
782
    JSONObject object = new JSONObject(contents.toString());
783
    int major = object.getInt("major");
784

    
785
    if( major>=6 )
786
      {
787
      JSONObject metadata = object.getJSONObject("metadata");
788
      parseMetadata(metadata);
789
      }
790
    else
791
      {
792
      android.util.Log.e("readJsonFileQuick", "Unknown version "+major);
793
      }
794
    }
795

    
796
///////////////////////////////////////////////////////////////////////////////////////////////////
797

    
798
  public void readNumScramblesAndPrice(InputStream stream) throws IOException, JSONException
799
    {
800
    BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
801

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

    
808
    JSONObject object = new JSONObject(contents.toString());
809
    JSONObject metadata = object.getJSONObject("metadata");
810
    mNumScrambles = metadata.getInt("scrambles");
811

    
812
    boolean free = metadata.optBoolean("free",true);
813
    if( free ) mPrice = 0;
814
    else mPrice = metadata.optInt("price", ObjectType.DEFAULT_PRICE_OF_OLD_OBJECTS );
815
    }
816

    
817
///////////////////////////////////////////////////////////////////////////////////////////////////
818

    
819
  public void parseJsonTutorial(InputStream jsonStream) throws IOException, JSONException
820
    {
821
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
822
    StringBuilder contents = new StringBuilder();
823
    String tmp;
824

    
825
    while( (tmp = br.readLine()) != null) contents.append(tmp);
826
    br.close();
827
    jsonStream.close();
828

    
829
    JSONObject object = new JSONObject(contents.toString());
830
    int major = object.getInt("major");
831

    
832
    if( major==1 )
833
      {
834
      parseTutorial(object);
835
      }
836
    else
837
      {
838
      android.util.Log.e("readJsonFile", "Unknown tutorial version "+major);
839
      }
840
    }
841

    
842
///////////////////////////////////////////////////////////////////////////////////////////////////
843

    
844
  public int getScrambleType()
845
    {
846
    return mScrambleType;
847
    }
848

    
849
///////////////////////////////////////////////////////////////////////////////////////////////////
850

    
851
  public int[][] getScrambleEdges()
852
    {
853
    return mEdges;
854
    }
855

    
856
///////////////////////////////////////////////////////////////////////////////////////////////////
857

    
858
  public int[][] getScrambleAlgorithms()
859
    {
860
    return mAlgorithms;
861
    }
862

    
863
///////////////////////////////////////////////////////////////////////////////////////////////////
864

    
865
  public int[][] getSolvedQuats()
866
    {
867
    return mSolvedQuats;
868
    }
869

    
870
///////////////////////////////////////////////////////////////////////////////////////////////////
871

    
872
  public Static4D[] getQuats()
873
    {
874
    return mQuats;
875
    }
876

    
877
///////////////////////////////////////////////////////////////////////////////////////////////////
878

    
879
  public int getSolvedFunctionIndex()
880
    {
881
    return mSolvedFuncIndex;
882
    }
883

    
884
///////////////////////////////////////////////////////////////////////////////////////////////////
885

    
886
  public int getNumStickerTypes()
887
    {
888
    return mNumStickerTypes;
889
    }
890

    
891
///////////////////////////////////////////////////////////////////////////////////////////////////
892

    
893
  public float[][] getCuts()
894
    {
895
    return mCuts;
896
    }
897

    
898
///////////////////////////////////////////////////////////////////////////////////////////////////
899

    
900
  public boolean[][] getLayerRotatable()
901
    {
902
    return mLayerRotatable;
903
    }
904

    
905
///////////////////////////////////////////////////////////////////////////////////////////////////
906

    
907
  public int getMovementType()
908
    {
909
    return mMovementType;
910
    }
911

    
912
///////////////////////////////////////////////////////////////////////////////////////////////////
913

    
914
  public int getMovementSplit()
915
    {
916
    return mMovementSplit;
917
    }
918

    
919
///////////////////////////////////////////////////////////////////////////////////////////////////
920

    
921
  public int[][][] getEnabled()
922
    {
923
    return mEnabled;
924
    }
925

    
926
///////////////////////////////////////////////////////////////////////////////////////////////////
927

    
928
  public float[] getDist3D()
929
    {
930
    return mDist3D;
931
    }
932

    
933
///////////////////////////////////////////////////////////////////////////////////////////////////
934

    
935
  public int getNumCubitFaces()
936
    {
937
    return mNumCubitFaces;
938
    }
939

    
940
///////////////////////////////////////////////////////////////////////////////////////////////////
941

    
942
  public float[][] getCubitPositions()
943
    {
944
    return mPositions;
945
    }
946

    
947
///////////////////////////////////////////////////////////////////////////////////////////////////
948

    
949
  public ObjectShape getObjectShape(int variant)
950
    {
951
    return mShapes[variant];
952
    }
953

    
954
///////////////////////////////////////////////////////////////////////////////////////////////////
955

    
956
  public ObjectFaceShape getObjectFaceShape(int variant)
957
    {
958
    return mFaceShapes[variant];
959
    }
960

    
961
///////////////////////////////////////////////////////////////////////////////////////////////////
962

    
963
  public ObjectVertexEffects getVertexEffects(int variant)
964
    {
965
    return mVertexEffects[variant];
966
    }
967

    
968
///////////////////////////////////////////////////////////////////////////////////////////////////
969

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

    
975
///////////////////////////////////////////////////////////////////////////////////////////////////
976

    
977
  public int getNumCubitVariants()
978
    {
979
    return mNumCubitVariants;
980
    }
981

    
982
///////////////////////////////////////////////////////////////////////////////////////////////////
983

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

    
989
///////////////////////////////////////////////////////////////////////////////////////////////////
990

    
991
  public int getVariantStickerShape(int variant, int face)
992
    {
993
    int[] shapes = mVariantStickerShape[variant];
994
    return shapes.length>face ? shapes[face] : -1;
995
    }
996

    
997
///////////////////////////////////////////////////////////////////////////////////////////////////
998

    
999
  public float[][] returnRotationFactor()
1000
    {
1001
    return mRotationFactor;
1002
    }
1003

    
1004
///////////////////////////////////////////////////////////////////////////////////////////////////
1005

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

    
1011
///////////////////////////////////////////////////////////////////////////////////////////////////
1012

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

    
1018
///////////////////////////////////////////////////////////////////////////////////////////////////
1019

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

    
1025
///////////////////////////////////////////////////////////////////////////////////////////////////
1026

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

    
1032
///////////////////////////////////////////////////////////////////////////////////////////////////
1033

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

    
1039
///////////////////////////////////////////////////////////////////////////////////////////////////
1040

    
1041
  public int getPrice()
1042
    {
1043
    return mPrice;
1044
    }
1045

    
1046
///////////////////////////////////////////////////////////////////////////////////////////////////
1047

    
1048
  public ObjectSticker retSticker(int sticker)
1049
    {
1050
    return mObjectSticker[sticker];
1051
    }
1052

    
1053
///////////////////////////////////////////////////////////////////////////////////////////////////
1054

    
1055
  public ObjectStickerOverride[] getStickerOverrides()
1056
    {
1057
    return mStickerOverrides;
1058
    }
1059

    
1060
///////////////////////////////////////////////////////////////////////////////////////////////////
1061

    
1062
  public float getPillowCoeff()
1063
    {
1064
    return mPillowCoeff;
1065
    }
1066

    
1067
///////////////////////////////////////////////////////////////////////////////////////////////////
1068

    
1069
  public Static3D[] getRotationAxis()
1070
    {
1071
    return mAxis;
1072
    }
1073

    
1074
///////////////////////////////////////////////////////////////////////////////////////////////////
1075

    
1076
  public int[][] getBasicAngle()
1077
    {
1078
    return mBasicAngle;
1079
    }
1080

    
1081
///////////////////////////////////////////////////////////////////////////////////////////////////
1082

    
1083
  public ObjectSignature getSignature()
1084
    {
1085
    return mSignature;
1086
    }
1087

    
1088
///////////////////////////////////////////////////////////////////////////////////////////////////
1089

    
1090
  public String getObjectName()
1091
    {
1092
    return mLongName;
1093
    }
1094

    
1095
///////////////////////////////////////////////////////////////////////////////////////////////////
1096

    
1097
  public String getShortName()
1098
    {
1099
    return mShortName;
1100
    }
1101

    
1102
///////////////////////////////////////////////////////////////////////////////////////////////////
1103

    
1104
  public String getInventor()
1105
    {
1106
    return mInventor;
1107
    }
1108

    
1109
///////////////////////////////////////////////////////////////////////////////////////////////////
1110

    
1111
  public int getYearOfInvention()
1112
    {
1113
    return mYearOfInvention;
1114
    }
1115

    
1116
///////////////////////////////////////////////////////////////////////////////////////////////////
1117

    
1118
  public int getComplexity()
1119
    {
1120
    return mComplexity;
1121
    }
1122

    
1123
///////////////////////////////////////////////////////////////////////////////////////////////////
1124

    
1125
  public int getNumFaces()
1126
    {
1127
    return mNumFaces;
1128
    }
1129

    
1130
///////////////////////////////////////////////////////////////////////////////////////////////////
1131

    
1132
  public int getNumFaceColors()
1133
    {
1134
    return mNumFaceColors;
1135
    }
1136

    
1137
///////////////////////////////////////////////////////////////////////////////////////////////////
1138

    
1139
  public int[] getNumLayers()
1140
    {
1141
    return mNumLayers;
1142
    }
1143

    
1144
///////////////////////////////////////////////////////////////////////////////////////////////////
1145

    
1146
  public float getSize()
1147
    {
1148
    return mSize;
1149
    }
1150

    
1151
///////////////////////////////////////////////////////////////////////////////////////////////////
1152

    
1153
  public int getColor(int face)
1154
    {
1155
    return mColor[face];
1156
    }
1157

    
1158
///////////////////////////////////////////////////////////////////////////////////////////////////
1159

    
1160
  public int getInternalColor()
1161
    {
1162
    return mInternalColor;
1163
    }
1164

    
1165
///////////////////////////////////////////////////////////////////////////////////////////////////
1166

    
1167
  public boolean shouldResetTextureMaps()
1168
    {
1169
    return mResetMaps;
1170
    }
1171

    
1172
///////////////////////////////////////////////////////////////////////////////////////////////////
1173

    
1174
  public String getTutorialObject()
1175
    {
1176
    return mTutorialObject;
1177
    }
1178

    
1179
///////////////////////////////////////////////////////////////////////////////////////////////////
1180

    
1181
  public String[][] getTutorials()
1182
    {
1183
    return mTutorials;
1184
    }
1185
}
(1-1/2)