Project

General

Profile

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

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

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++) mAlgorithms[i][j] = (1<<jsonAlg.getInt(j));
538
      }
539

    
540
    JSONArray jsonEdges = object.getJSONArray("edges");
541
    int numEdges = jsonEdges.length();
542
    mEdges = new int[numEdges][];
543

    
544
    for(int i=0; i<numEdges; i++)
545
      {
546
      JSONArray jsonEdge = jsonEdges.getJSONArray(i);
547
      int numEntries = jsonEdge.length();
548
      mEdges[i] = new int[numEntries];
549
      for(int j=0; j<numEntries; j++) mEdges[i][j] = jsonEdge.getInt(j);
550
      }
551
    }
552

    
553
///////////////////////////////////////////////////////////////////////////////////////////////////
554

    
555
  private void parseScrambling11orMore(JSONObject object) throws JSONException
556
    {
557
    JSONArray jsonAlgorithms = object.getJSONArray("algorithms");
558
    int numAlgs = jsonAlgorithms.length();
559
    mAlgorithms = new int[numAlgs][];
560

    
561
    for(int i=0; i<numAlgs; i++)
562
      {
563
      JSONArray jsonAlg = jsonAlgorithms.getJSONArray(i);
564
      int numEntries = jsonAlg.length();
565
      mAlgorithms[i] = new int[numEntries];
566
      for(int j=0; j<numEntries; j++) mAlgorithms[i][j] = jsonAlg.getInt(j);
567
      }
568

    
569
    JSONArray jsonEdges = object.getJSONArray("edges");
570
    int numEdges = jsonEdges.length();
571
    mEdges = new int[numEdges][];
572

    
573
    for(int i=0; i<numEdges; i++)
574
      {
575
      JSONArray jsonEdge = jsonEdges.getJSONArray(i);
576
      int numEntries = jsonEdge.length();
577
      mEdges[i] = new int[numEntries];
578
      for(int j=0; j<numEntries; j++) mEdges[i][j] = jsonEdge.getInt(j);
579
      }
580
    }
581

    
582
///////////////////////////////////////////////////////////////////////////////////////////////////
583

    
584
  private void parseScrambling(JSONObject object, int major) throws JSONException
585
    {
586
    mScrambleType = object.getInt("scrambleType");
587

    
588
    if( mScrambleType==0 )
589
      {
590
      if( major==6 )       parseScrambling6(object);
591
      else if( major<=10 ) parseScrambling7to10(object);
592
      else                 parseScrambling11orMore(object);
593
      }
594
    }
595

    
596
///////////////////////////////////////////////////////////////////////////////////////////////////
597

    
598
  private void parseTouchcontrol(JSONObject object) throws JSONException
599
    {
600
    mMovementType = object.getInt("movementType");
601
    mMovementSplit= object.getInt("movementSplit");
602

    
603
    try
604
      {
605
      JSONArray jsonEnabled = object.getJSONArray("enabledAxis");
606
      int numFace = jsonEnabled.length();
607

    
608
      mEnabled = new int[numFace][][];
609

    
610
      for(int i=0; i<numFace; i++)
611
        {
612
        JSONArray jsonSection = jsonEnabled.getJSONArray(i);
613
        int numSection = jsonSection.length();
614
        mEnabled[i] = new int[numSection][];
615

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

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

    
643
///////////////////////////////////////////////////////////////////////////////////////////////////
644

    
645
  private void parseColors(JSONArray object) throws JSONException
646
    {
647
    mNumFaceColors = object.length()-1;
648

    
649
    mColor = new int[mNumFaceColors];
650
    for(int i=0; i<mNumFaceColors; i++) mColor[i] = object.getInt(i);
651

    
652
    mInternalColor = object.getInt(mNumFaceColors);
653
    }
654

    
655
///////////////////////////////////////////////////////////////////////////////////////////////////
656

    
657
  private void parseSolved(JSONObject solved) throws JSONException
658
    {
659
    mSolvedFuncIndex = solved.getInt("functionIndex");
660

    
661
    try
662
      {
663
      JSONArray groupArray= solved.getJSONArray("groups");
664
      int numGroups = groupArray.length();
665
      mSolvedQuats  = new int[numGroups][];
666

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

    
681
///////////////////////////////////////////////////////////////////////////////////////////////////
682

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

    
703
///////////////////////////////////////////////////////////////////////////////////////////////////
704

    
705
  private void parseTutorial(JSONObject object) throws JSONException
706
    {
707
    mTutorialObject = object.getString("object");
708
    JSONArray tuts= object.getJSONArray("tutorials");
709

    
710
    int len = tuts.length();
711
    mTutorials = new String[len][4];
712

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

    
723
///////////////////////////////////////////////////////////////////////////////////////////////////
724
// PUBLIC
725
///////////////////////////////////////////////////////////////////////////////////////////////////
726

    
727
  public void parseJsonFile(InputStream jsonStream) throws JSONException, IOException
728
    {
729
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
730
    StringBuilder contents = new StringBuilder();
731
    String line;
732

    
733
    while( (line = br.readLine()) != null) contents.append(line);
734
    br.close();
735
    jsonStream.close();
736
    String contentsString = contents.toString();
737
    JSONObject object = new JSONObject(contentsString);
738
    int major = object.getInt("major");
739

    
740
    if( major>=6 )
741
      {
742
      parseFile(object,major);
743
      }
744
    else
745
      {
746
      android.util.Log.e("readJsonFile", "Unknown version "+major);
747
      }
748
    }
749

    
750
///////////////////////////////////////////////////////////////////////////////////////////////////
751

    
752
  public void parseJsonFileMetadata(InputStream jsonStream) throws JSONException, IOException
753
    {
754
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
755
    StringBuilder contents = new StringBuilder();
756
    String tmp;
757

    
758
    while( (tmp = br.readLine()) != null) contents.append(tmp);
759
    br.close();
760
    jsonStream.close();
761

    
762
    JSONObject object = new JSONObject(contents.toString());
763
    int major = object.getInt("major");
764

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

    
776
///////////////////////////////////////////////////////////////////////////////////////////////////
777

    
778
  public void readNumScramblesAndPrice(InputStream stream) throws IOException, JSONException
779
    {
780
    BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
781

    
782
    StringBuilder contents = new StringBuilder();
783
    String tmp;
784
    while( (tmp = br.readLine()) != null) contents.append(tmp);
785
    br.close();
786
    stream.close();
787

    
788
    JSONObject object = new JSONObject(contents.toString());
789
    JSONObject metadata = object.getJSONObject("metadata");
790
    mNumScrambles = metadata.getInt("scrambles");
791

    
792
    boolean free = metadata.optBoolean("free",true);
793
    if( free ) mPrice = 0;
794
    else mPrice = metadata.optInt("price", ObjectType.DEFAULT_PRICE_OF_OLD_OBJECTS );
795
    }
796

    
797
///////////////////////////////////////////////////////////////////////////////////////////////////
798

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

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

    
809
    JSONObject object = new JSONObject(contents.toString());
810
    int major = object.getInt("major");
811

    
812
    if( major==1 )
813
      {
814
      parseTutorial(object);
815
      }
816
    else
817
      {
818
      android.util.Log.e("readJsonFile", "Unknown tutorial version "+major);
819
      }
820
    }
821

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

    
824
  public int getScrambleType()
825
    {
826
    return mScrambleType;
827
    }
828

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

    
831
  public int[][] getScrambleEdges()
832
    {
833
    return mEdges;
834
    }
835

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

    
838
  public int[][] getScrambleAlgorithms()
839
    {
840
    return mAlgorithms;
841
    }
842

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

    
845
  public int[][] getSolvedQuats()
846
    {
847
    return mSolvedQuats;
848
    }
849

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

    
852
  public Static4D[] getQuats()
853
    {
854
    return mQuats;
855
    }
856

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

    
859
  public int getSolvedFunctionIndex()
860
    {
861
    return mSolvedFuncIndex;
862
    }
863

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

    
866
  public int getNumStickerTypes()
867
    {
868
    return mNumStickerTypes;
869
    }
870

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

    
873
  public float[][] getCuts()
874
    {
875
    return mCuts;
876
    }
877

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

    
880
  public boolean[][] getLayerRotatable()
881
    {
882
    return mLayerRotatable;
883
    }
884

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

    
887
  public int getMovementType()
888
    {
889
    return mMovementType;
890
    }
891

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

    
894
  public int getMovementSplit()
895
    {
896
    return mMovementSplit;
897
    }
898

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

    
901
  public int[][][] getEnabled()
902
    {
903
    return mEnabled;
904
    }
905

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

    
908
  public float[] getDist3D()
909
    {
910
    return mDist3D;
911
    }
912

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

    
915
  public int getNumCubitFaces()
916
    {
917
    return mNumCubitFaces;
918
    }
919

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

    
922
  public float[][] getCubitPositions()
923
    {
924
    return mPositions;
925
    }
926

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

    
929
  public ObjectShape getObjectShape(int variant)
930
    {
931
    return mShapes[variant];
932
    }
933

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

    
936
  public ObjectFaceShape getObjectFaceShape(int variant)
937
    {
938
    return mFaceShapes[variant];
939
    }
940

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

    
943
  public ObjectVertexEffects getVertexEffects(int variant)
944
    {
945
    return mVertexEffects[variant];
946
    }
947

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

    
950
  public Static4D getCubitQuats(int cubit)
951
    {
952
    return mCubitQuats[cubit];
953
    }
954

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

    
957
  public int getNumCubitVariants()
958
    {
959
    return mNumCubitVariants;
960
    }
961

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

    
964
  public int getCubitVariant(int cubit)
965
    {
966
    return mCubitVariant[cubit];
967
    }
968

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

    
971
  public int getVariantStickerShape(int variant, int face)
972
    {
973
    int[] shapes = mVariantStickerShape[variant];
974
    return shapes.length>face ? shapes[face] : -1;
975
    }
976

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

    
979
  public int[] getCubitTypes()
980
    {
981
    return mCubitType;
982
    }
983

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

    
986
  public float[][] getCubitOffsets()
987
    {
988
    return mCubitRowOffset;
989
    }
990

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

    
993
  public int[][] getVariantFaceIsOuter()
994
    {
995
    return mVariantFaceIsOuter;
996
    }
997

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

    
1000
  public int getCubitFaceFace(int cubit, int face)
1001
    {
1002
    return mCubitFaceColor[cubit][face];
1003
    }
1004

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

    
1007
  public int getNumScrambles()
1008
    {
1009
    return mNumScrambles;
1010
    }
1011

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

    
1014
  public int getPrice()
1015
    {
1016
    return mPrice;
1017
    }
1018

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

    
1021
  public ObjectSticker retSticker(int sticker)
1022
    {
1023
    return mObjectSticker[sticker];
1024
    }
1025

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

    
1028
  public ObjectStickerOverride[] getStickerOverrides()
1029
    {
1030
    return mStickerOverrides;
1031
    }
1032

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

    
1035
  public float getPillowCoeff()
1036
    {
1037
    return mPillowCoeff;
1038
    }
1039

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

    
1042
  public Static3D[] getRotationAxis()
1043
    {
1044
    return mAxis;
1045
    }
1046

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

    
1049
  public int[][] getBasicAngle()
1050
    {
1051
    return mBasicAngle;
1052
    }
1053

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

    
1056
  public ObjectSignature getSignature()
1057
    {
1058
    return mSignature;
1059
    }
1060

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

    
1063
  public String getObjectName()
1064
    {
1065
    return mLongName;
1066
    }
1067

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

    
1070
  public String getShortName()
1071
    {
1072
    return mShortName;
1073
    }
1074

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

    
1077
  public String getInventor()
1078
    {
1079
    return mInventor;
1080
    }
1081

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

    
1084
  public int getYearOfInvention()
1085
    {
1086
    return mYearOfInvention;
1087
    }
1088

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

    
1091
  public int getComplexity()
1092
    {
1093
    return mComplexity;
1094
    }
1095

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

    
1098
  public int getNumFaces()
1099
    {
1100
    return mNumFaces;
1101
    }
1102

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

    
1105
  public int getNumFaceColors()
1106
    {
1107
    return mNumFaceColors;
1108
    }
1109

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

    
1112
  public int[] getNumLayers()
1113
    {
1114
    return mNumLayers;
1115
    }
1116

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

    
1119
  public float getSize()
1120
    {
1121
    return mSize;
1122
    }
1123

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

    
1126
  public int getColor(int face)
1127
    {
1128
    return mColor[face];
1129
    }
1130

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

    
1133
  public int getInternalColor()
1134
    {
1135
    return mInternalColor;
1136
    }
1137

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

    
1140
  public boolean shouldResetTextureMaps()
1141
    {
1142
    return mResetMaps;
1143
    }
1144

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

    
1147
  public String getTutorialObject()
1148
    {
1149
    return mTutorialObject;
1150
    }
1151

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

    
1154
  public String[][] getTutorials()
1155
    {
1156
    return mTutorials;
1157
    }
1158
}
(1-1/2)