Project

General

Profile

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

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

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
    mNumCubitFaces = -1;
193

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

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

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

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

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

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

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

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

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

    
252
        mShapes[i] = new ObjectShape(verts,vertIndices);
253
        }
254

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

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

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

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

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

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

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

    
300
      mFaceShapes[i] = new ObjectFaceShape(bands,bandIndices,convexity);
301

    
302
      /////////////////////////////////////////////////////////////////////////
303
      ////// ObjectVertexEffects //////////////////////////////////////////////
304
      JSONArray jsonEffects = jsonShape.optJSONArray("effects");
305

    
306
      if( jsonEffects!=null )
307
        {
308
        int numEffects = jsonEffects.length();
309

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

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

    
320
          name[j] = jsonEffect.getString("name");
321

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

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

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

    
337
          use[j] = jsonEffect.getBoolean("use");
338
          }
339

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

    
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346

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

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

    
359
      float[] coords     = new float[2*numVertices];
360
      float[] curvatures = new float[numVertices];
361
      float[] radii      = new float[numVertices];
362

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

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

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

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

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

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

    
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404

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

    
414
    JSONArray overrides= object.optJSONArray("overrides");
415
    parseOverrides(overrides);
416

    
417
    mPillowCoeff = (float)object.optDouble("pillow",1.0);
418
    }
419

    
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421

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

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

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

    
437
      float x = (float)jsonAx.getDouble("x");
438
      float y = (float)jsonAx.getDouble("y");
439
      float z = (float)jsonAx.getDouble("z");
440

    
441
      mAxis[i] = new Static3D(x,y,z);
442

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

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

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

    
458
      JSONArray jsonFactor = jsonAx.optJSONArray("factor");
459

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

    
472
      mNumLayers[i] = numRota;
473
      }
474
    }
475

    
476
///////////////////////////////////////////////////////////////////////////////////////////////////
477

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

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

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

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

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

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

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

    
508
    return -1;
509
    }
510

    
511
///////////////////////////////////////////////////////////////////////////////////////////////////
512

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

    
519
    int[] ret = new int[2*length/3];
520

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

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

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

    
538
    return ret;
539
    }
540

    
541
///////////////////////////////////////////////////////////////////////////////////////////////////
542

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

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

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

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

    
569
///////////////////////////////////////////////////////////////////////////////////////////////////
570

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

    
577
    for(int i=0; i<numAlgs; i++)
578
      {
579
      JSONArray jsonAlg = jsonAlgorithms.getJSONArray(i);
580
      int numEntries = jsonAlg.length();
581
      mAlgorithms[i] = new int[numEntries];
582
      for(int j=0; j<numEntries; j++)
583
        {
584
        int entry = jsonAlg.getInt(j);
585
        mAlgorithms[i][j] = ( (j%3)==1 ? (1<<entry) : entry );
586
        }
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 parseScrambling11orMore(JSONObject object) throws JSONException
605
    {
606
    JSONArray jsonAlgorithms = object.getJSONArray("algorithms");
607
    int numAlgs = jsonAlgorithms.length();
608
    mAlgorithms = new int[numAlgs][];
609

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

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

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

    
631
///////////////////////////////////////////////////////////////////////////////////////////////////
632

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

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

    
645
///////////////////////////////////////////////////////////////////////////////////////////////////
646

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

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

    
657
      mEnabled = new int[numFace][][];
658

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

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

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

    
692
///////////////////////////////////////////////////////////////////////////////////////////////////
693

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

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

    
701
    mInternalColor = object.getInt(mNumFaceColors);
702
    }
703

    
704
///////////////////////////////////////////////////////////////////////////////////////////////////
705

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

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

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

    
730
///////////////////////////////////////////////////////////////////////////////////////////////////
731

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

    
752
///////////////////////////////////////////////////////////////////////////////////////////////////
753

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

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

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

    
772
///////////////////////////////////////////////////////////////////////////////////////////////////
773
// PUBLIC
774
///////////////////////////////////////////////////////////////////////////////////////////////////
775

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

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

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

    
799
///////////////////////////////////////////////////////////////////////////////////////////////////
800

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

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

    
811
    JSONObject object = new JSONObject(contents.toString());
812
    int major = object.getInt("major");
813

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

    
825
///////////////////////////////////////////////////////////////////////////////////////////////////
826

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

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

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

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

    
846
///////////////////////////////////////////////////////////////////////////////////////////////////
847

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

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

    
858
    JSONObject object = new JSONObject(contents.toString());
859
    int major = object.getInt("major");
860

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

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

    
873
  public int getScrambleType()
874
    {
875
    return mScrambleType;
876
    }
877

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

    
880
  public int[][] getScrambleEdges()
881
    {
882
    return mEdges;
883
    }
884

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

    
887
  public int[][] getScrambleAlgorithms()
888
    {
889
    return mAlgorithms;
890
    }
891

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

    
894
  public int[][] getSolvedQuats()
895
    {
896
    return mSolvedQuats;
897
    }
898

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

    
901
  public Static4D[] getQuats()
902
    {
903
    return mQuats;
904
    }
905

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

    
908
  public int getSolvedFunctionIndex()
909
    {
910
    return mSolvedFuncIndex;
911
    }
912

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

    
915
  public int getNumStickerTypes()
916
    {
917
    return mNumStickerTypes;
918
    }
919

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

    
922
  public float[][] getCuts()
923
    {
924
    return mCuts;
925
    }
926

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

    
929
  public boolean[][] getLayerRotatable()
930
    {
931
    return mLayerRotatable;
932
    }
933

    
934
///////////////////////////////////////////////////////////////////////////////////////////////////
935

    
936
  public int getMovementType()
937
    {
938
    return mMovementType;
939
    }
940

    
941
///////////////////////////////////////////////////////////////////////////////////////////////////
942

    
943
  public int getMovementSplit()
944
    {
945
    return mMovementSplit;
946
    }
947

    
948
///////////////////////////////////////////////////////////////////////////////////////////////////
949

    
950
  public int[][][] getEnabled()
951
    {
952
    return mEnabled;
953
    }
954

    
955
///////////////////////////////////////////////////////////////////////////////////////////////////
956

    
957
  public float[] getDist3D()
958
    {
959
    return mDist3D;
960
    }
961

    
962
///////////////////////////////////////////////////////////////////////////////////////////////////
963

    
964
  public int getNumCubitFaces()
965
    {
966
    return mNumCubitFaces;
967
    }
968

    
969
///////////////////////////////////////////////////////////////////////////////////////////////////
970

    
971
  public float[][] getCubitPositions()
972
    {
973
    return mPositions;
974
    }
975

    
976
///////////////////////////////////////////////////////////////////////////////////////////////////
977

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

    
983
///////////////////////////////////////////////////////////////////////////////////////////////////
984

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

    
990
///////////////////////////////////////////////////////////////////////////////////////////////////
991

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

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

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

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

    
1006
  public int getNumCubitVariants()
1007
    {
1008
    return mNumCubitVariants;
1009
    }
1010

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

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

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

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

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

    
1028
  public float[][] returnRotationFactor()
1029
    {
1030
    return mRotationFactor;
1031
    }
1032

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

    
1035
  public int[] getCubitTypes()
1036
    {
1037
    return mCubitType;
1038
    }
1039

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

    
1042
  public float[][] getCubitOffsets()
1043
    {
1044
    return mCubitRowOffset;
1045
    }
1046

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

    
1049
  public int[][] getVariantFaceIsOuter()
1050
    {
1051
    return mVariantFaceIsOuter;
1052
    }
1053

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

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

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

    
1063
  public int getNumScrambles()
1064
    {
1065
    return mNumScrambles;
1066
    }
1067

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

    
1070
  public int getPrice()
1071
    {
1072
    return mPrice;
1073
    }
1074

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

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

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

    
1084
  public ObjectStickerOverride[] getStickerOverrides()
1085
    {
1086
    return mStickerOverrides;
1087
    }
1088

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

    
1091
  public float getPillowCoeff()
1092
    {
1093
    return mPillowCoeff;
1094
    }
1095

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

    
1098
  public Static3D[] getRotationAxis()
1099
    {
1100
    return mAxis;
1101
    }
1102

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

    
1105
  public int[][] getBasicAngle()
1106
    {
1107
    return mBasicAngle;
1108
    }
1109

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

    
1112
  public ObjectSignature getSignature()
1113
    {
1114
    return mSignature;
1115
    }
1116

    
1117
///////////////////////////////////////////////////////////////////////////////////////////////////
1118

    
1119
  public String getObjectName()
1120
    {
1121
    return mLongName;
1122
    }
1123

    
1124
///////////////////////////////////////////////////////////////////////////////////////////////////
1125

    
1126
  public String getShortName()
1127
    {
1128
    return mShortName;
1129
    }
1130

    
1131
///////////////////////////////////////////////////////////////////////////////////////////////////
1132

    
1133
  public String getInventor()
1134
    {
1135
    return mInventor;
1136
    }
1137

    
1138
///////////////////////////////////////////////////////////////////////////////////////////////////
1139

    
1140
  public int getYearOfInvention()
1141
    {
1142
    return mYearOfInvention;
1143
    }
1144

    
1145
///////////////////////////////////////////////////////////////////////////////////////////////////
1146

    
1147
  public int getComplexity()
1148
    {
1149
    return mComplexity;
1150
    }
1151

    
1152
///////////////////////////////////////////////////////////////////////////////////////////////////
1153

    
1154
  public int getNumFaces()
1155
    {
1156
    return mNumFaces;
1157
    }
1158

    
1159
///////////////////////////////////////////////////////////////////////////////////////////////////
1160

    
1161
  public int getNumFaceColors()
1162
    {
1163
    return mNumFaceColors;
1164
    }
1165

    
1166
///////////////////////////////////////////////////////////////////////////////////////////////////
1167

    
1168
  public int[] getNumLayers()
1169
    {
1170
    return mNumLayers;
1171
    }
1172

    
1173
///////////////////////////////////////////////////////////////////////////////////////////////////
1174

    
1175
  public float getSize()
1176
    {
1177
    return mSize;
1178
    }
1179

    
1180
///////////////////////////////////////////////////////////////////////////////////////////////////
1181

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

    
1187
///////////////////////////////////////////////////////////////////////////////////////////////////
1188

    
1189
  public int getInternalColor()
1190
    {
1191
    return mInternalColor;
1192
    }
1193

    
1194
///////////////////////////////////////////////////////////////////////////////////////////////////
1195

    
1196
  public boolean shouldResetTextureMaps()
1197
    {
1198
    return mResetMaps;
1199
    }
1200

    
1201
///////////////////////////////////////////////////////////////////////////////////////////////////
1202

    
1203
  public String getTutorialObject()
1204
    {
1205
    return mTutorialObject;
1206
    }
1207

    
1208
///////////////////////////////////////////////////////////////////////////////////////////////////
1209

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