Project

General

Profile

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

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

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 parseScrambling7orMore(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] = 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 parseScrambling(JSONObject object, int major) throws JSONException
556
    {
557
    mScrambleType = object.getInt("scrambleType");
558

    
559
    if( mScrambleType==0 )
560
      {
561
      if( major==6 ) parseScrambling6(object);
562
      else           parseScrambling7orMore(object);
563
      }
564
    }
565

    
566
///////////////////////////////////////////////////////////////////////////////////////////////////
567

    
568
  private void parseTouchcontrol(JSONObject object) throws JSONException
569
    {
570
    mMovementType = object.getInt("movementType");
571
    mMovementSplit= object.getInt("movementSplit");
572

    
573
    try
574
      {
575
      JSONArray jsonEnabled = object.getJSONArray("enabledAxis");
576
      int numFace = jsonEnabled.length();
577

    
578
      mEnabled = new int[numFace][][];
579

    
580
      for(int i=0; i<numFace; i++)
581
        {
582
        JSONArray jsonSection = jsonEnabled.getJSONArray(i);
583
        int numSection = jsonSection.length();
584
        mEnabled[i] = new int[numSection][];
585

    
586
        for(int j=0; j<numSection; j++)
587
          {
588
          JSONArray jsonAx = jsonSection.getJSONArray(j);
589
          int numAxis = jsonAx.length();
590
          mEnabled[i][j] = new int[numAxis];
591
          for(int k=0; k<numAxis; k++) mEnabled[i][j][k] = jsonAx.getInt(k);
592
          }
593
        }
594
      }
595
    catch( JSONException ex )
596
      {
597
      // ignore, the object does not have to have 'enabledAxis' defined at all.
598
      }
599

    
600
    try
601
      {
602
      JSONArray jsonDist = object.getJSONArray("dist3D");
603
      int num = jsonDist.length();
604
      mDist3D = new float[num];
605
      for(int j=0; j<num; j++) mDist3D[j] = (float)jsonDist.getDouble(j);
606
      }
607
    catch( JSONException ex )
608
      {
609
      // ignore, the object does not have a 'dist3D' which is possible.
610
      }
611
    }
612

    
613
///////////////////////////////////////////////////////////////////////////////////////////////////
614

    
615
  private void parseColors(JSONArray object) throws JSONException
616
    {
617
    mNumFaceColors = object.length()-1;
618

    
619
    mColor = new int[mNumFaceColors];
620
    for(int i=0; i<mNumFaceColors; i++) mColor[i] = object.getInt(i);
621

    
622
    mInternalColor = object.getInt(mNumFaceColors);
623
    }
624

    
625
///////////////////////////////////////////////////////////////////////////////////////////////////
626

    
627
  private void parseSolved(JSONObject solved) throws JSONException
628
    {
629
    mSolvedFuncIndex = solved.getInt("functionIndex");
630

    
631
    try
632
      {
633
      JSONArray groupArray= solved.getJSONArray("groups");
634
      int numGroups = groupArray.length();
635
      mSolvedQuats  = new int[numGroups][];
636

    
637
      for(int i=0; i<numGroups; i++)
638
        {
639
        JSONArray groupElements = groupArray.getJSONArray(i);
640
        int groupSize = groupElements.length();
641
        mSolvedQuats[i] = new int[groupSize];
642
        for(int j=0; j<groupSize; j++) mSolvedQuats[i][j] = groupElements.getInt(j);
643
        }
644
      }
645
    catch( JSONException ex )
646
      {
647
      // ignore, the object does not have to have an array of solved groups.
648
      }
649
    }
650

    
651
///////////////////////////////////////////////////////////////////////////////////////////////////
652

    
653
  private void parseFile(JSONObject object, int major) throws JSONException
654
    {
655
    JSONObject metadata    = object.getJSONObject("metadata");
656
    parseMetadata(metadata);
657
    JSONObject mesh        = object.getJSONObject("mesh");
658
    parseMesh(mesh);
659
    JSONArray axis         = object.getJSONArray("axis");
660
    parseAxis(axis);
661
    JSONArray quats        = object.getJSONArray("quats");
662
    parseQuats(quats);
663
    JSONObject scrambling  = object.getJSONObject("scrambling");
664
    parseScrambling(scrambling,major);
665
    JSONObject touchcontrol= object.getJSONObject("touchcontrol");
666
    parseTouchcontrol(touchcontrol);
667
    JSONArray colors       = object.getJSONArray("colors");
668
    parseColors(colors);
669
    JSONObject solved      = object.getJSONObject("solved");
670
    parseSolved(solved);
671
    }
672

    
673
///////////////////////////////////////////////////////////////////////////////////////////////////
674

    
675
  private void parseTutorial(JSONObject object) throws JSONException
676
    {
677
    mTutorialObject = object.getString("object");
678
    JSONArray tuts= object.getJSONArray("tutorials");
679

    
680
    int len = tuts.length();
681
    mTutorials = new String[len][4];
682

    
683
    for(int i=0; i<len; i++)
684
      {
685
      JSONObject tut = tuts.getJSONObject(i);
686
      mTutorials[i][0] = tut.getString("language");
687
      mTutorials[i][1] = tut.getString("link");
688
      mTutorials[i][2] = tut.getString("title");
689
      mTutorials[i][3] = tut.getString("author");
690
      }
691
    }
692

    
693
///////////////////////////////////////////////////////////////////////////////////////////////////
694
// PUBLIC
695
///////////////////////////////////////////////////////////////////////////////////////////////////
696

    
697
  public void parseJsonFile(InputStream jsonStream) throws JSONException, IOException
698
    {
699
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
700
    StringBuilder contents = new StringBuilder();
701
    String line;
702

    
703
    while( (line = br.readLine()) != null) contents.append(line);
704
    br.close();
705
    jsonStream.close();
706
    String contentsString = contents.toString();
707
    JSONObject object = new JSONObject(contentsString);
708
    int major = object.getInt("major");
709

    
710
    if( major>=6 )
711
      {
712
      parseFile(object,major);
713
      }
714
    else
715
      {
716
      android.util.Log.e("readJsonFile", "Unknown version "+major);
717
      }
718
    }
719

    
720
///////////////////////////////////////////////////////////////////////////////////////////////////
721

    
722
  public void parseJsonFileMetadata(InputStream jsonStream) throws JSONException, IOException
723
    {
724
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
725
    StringBuilder contents = new StringBuilder();
726
    String tmp;
727

    
728
    while( (tmp = br.readLine()) != null) contents.append(tmp);
729
    br.close();
730
    jsonStream.close();
731

    
732
    JSONObject object = new JSONObject(contents.toString());
733
    int major = object.getInt("major");
734

    
735
    if( major>=6 )
736
      {
737
      JSONObject metadata = object.getJSONObject("metadata");
738
      parseMetadata(metadata);
739
      }
740
    else
741
      {
742
      android.util.Log.e("readJsonFileQuick", "Unknown version "+major);
743
      }
744
    }
745

    
746
///////////////////////////////////////////////////////////////////////////////////////////////////
747

    
748
  public void readNumScramblesAndPrice(InputStream stream) throws IOException, JSONException
749
    {
750
    BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
751

    
752
    StringBuilder contents = new StringBuilder();
753
    String tmp;
754
    while( (tmp = br.readLine()) != null) contents.append(tmp);
755
    br.close();
756
    stream.close();
757

    
758
    JSONObject object = new JSONObject(contents.toString());
759
    JSONObject metadata = object.getJSONObject("metadata");
760
    mNumScrambles = metadata.getInt("scrambles");
761

    
762
    boolean free = metadata.optBoolean("free",true);
763
    if( free ) mPrice = 0;
764
    else mPrice = metadata.optInt("price", ObjectType.DEFAULT_PRICE_OF_OLD_OBJECTS );
765
    }
766

    
767
///////////////////////////////////////////////////////////////////////////////////////////////////
768

    
769
  public void parseJsonTutorial(InputStream jsonStream) throws IOException, JSONException
770
    {
771
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
772
    StringBuilder contents = new StringBuilder();
773
    String tmp;
774

    
775
    while( (tmp = br.readLine()) != null) contents.append(tmp);
776
    br.close();
777
    jsonStream.close();
778

    
779
    JSONObject object = new JSONObject(contents.toString());
780
    int major = object.getInt("major");
781

    
782
    if( major==1 )
783
      {
784
      parseTutorial(object);
785
      }
786
    else
787
      {
788
      android.util.Log.e("readJsonFile", "Unknown tutorial version "+major);
789
      }
790
    }
791

    
792
///////////////////////////////////////////////////////////////////////////////////////////////////
793

    
794
  public int getScrambleType()
795
    {
796
    return mScrambleType;
797
    }
798

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

    
801
  public int[][] getScrambleEdges()
802
    {
803
    return mEdges;
804
    }
805

    
806
///////////////////////////////////////////////////////////////////////////////////////////////////
807

    
808
  public int[][] getScrambleAlgorithms()
809
    {
810
    return mAlgorithms;
811
    }
812

    
813
///////////////////////////////////////////////////////////////////////////////////////////////////
814

    
815
  public int[][] getSolvedQuats()
816
    {
817
    return mSolvedQuats;
818
    }
819

    
820
///////////////////////////////////////////////////////////////////////////////////////////////////
821

    
822
  public Static4D[] getQuats()
823
    {
824
    return mQuats;
825
    }
826

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

    
829
  public int getSolvedFunctionIndex()
830
    {
831
    return mSolvedFuncIndex;
832
    }
833

    
834
///////////////////////////////////////////////////////////////////////////////////////////////////
835

    
836
  public int getNumStickerTypes()
837
    {
838
    return mNumStickerTypes;
839
    }
840

    
841
///////////////////////////////////////////////////////////////////////////////////////////////////
842

    
843
  public float[][] getCuts()
844
    {
845
    return mCuts;
846
    }
847

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

    
850
  public boolean[][] getLayerRotatable()
851
    {
852
    return mLayerRotatable;
853
    }
854

    
855
///////////////////////////////////////////////////////////////////////////////////////////////////
856

    
857
  public int getMovementType()
858
    {
859
    return mMovementType;
860
    }
861

    
862
///////////////////////////////////////////////////////////////////////////////////////////////////
863

    
864
  public int getMovementSplit()
865
    {
866
    return mMovementSplit;
867
    }
868

    
869
///////////////////////////////////////////////////////////////////////////////////////////////////
870

    
871
  public int[][][] getEnabled()
872
    {
873
    return mEnabled;
874
    }
875

    
876
///////////////////////////////////////////////////////////////////////////////////////////////////
877

    
878
  public float[] getDist3D()
879
    {
880
    return mDist3D;
881
    }
882

    
883
///////////////////////////////////////////////////////////////////////////////////////////////////
884

    
885
  public int getNumCubitFaces()
886
    {
887
    return mNumCubitFaces;
888
    }
889

    
890
///////////////////////////////////////////////////////////////////////////////////////////////////
891

    
892
  public float[][] getCubitPositions()
893
    {
894
    return mPositions;
895
    }
896

    
897
///////////////////////////////////////////////////////////////////////////////////////////////////
898

    
899
  public ObjectShape getObjectShape(int variant)
900
    {
901
    return mShapes[variant];
902
    }
903

    
904
///////////////////////////////////////////////////////////////////////////////////////////////////
905

    
906
  public ObjectFaceShape getObjectFaceShape(int variant)
907
    {
908
    return mFaceShapes[variant];
909
    }
910

    
911
///////////////////////////////////////////////////////////////////////////////////////////////////
912

    
913
  public ObjectVertexEffects getVertexEffects(int variant)
914
    {
915
    return mVertexEffects[variant];
916
    }
917

    
918
///////////////////////////////////////////////////////////////////////////////////////////////////
919

    
920
  public Static4D getCubitQuats(int cubit)
921
    {
922
    return mCubitQuats[cubit];
923
    }
924

    
925
///////////////////////////////////////////////////////////////////////////////////////////////////
926

    
927
  public int getNumCubitVariants()
928
    {
929
    return mNumCubitVariants;
930
    }
931

    
932
///////////////////////////////////////////////////////////////////////////////////////////////////
933

    
934
  public int getCubitVariant(int cubit)
935
    {
936
    return mCubitVariant[cubit];
937
    }
938

    
939
///////////////////////////////////////////////////////////////////////////////////////////////////
940

    
941
  public int getVariantStickerShape(int variant, int face)
942
    {
943
    int[] shapes = mVariantStickerShape[variant];
944
    return shapes.length>face ? shapes[face] : -1;
945
    }
946

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

    
949
  public int[] getCubitTypes()
950
    {
951
    return mCubitType;
952
    }
953

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

    
956
  public float[][] getCubitOffsets()
957
    {
958
    return mCubitRowOffset;
959
    }
960

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

    
963
  public int[][] getVariantFaceIsOuter()
964
    {
965
    return mVariantFaceIsOuter;
966
    }
967

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

    
970
  public int getCubitFaceFace(int cubit, int face)
971
    {
972
    return mCubitFaceColor[cubit][face];
973
    }
974

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

    
977
  public int getNumScrambles()
978
    {
979
    return mNumScrambles;
980
    }
981

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

    
984
  public int getPrice()
985
    {
986
    return mPrice;
987
    }
988

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

    
991
  public ObjectSticker retSticker(int sticker)
992
    {
993
    return mObjectSticker[sticker];
994
    }
995

    
996
///////////////////////////////////////////////////////////////////////////////////////////////////
997

    
998
  public ObjectStickerOverride[] getStickerOverrides()
999
    {
1000
    return mStickerOverrides;
1001
    }
1002

    
1003
///////////////////////////////////////////////////////////////////////////////////////////////////
1004

    
1005
  public float getPillowCoeff()
1006
    {
1007
    return mPillowCoeff;
1008
    }
1009

    
1010
///////////////////////////////////////////////////////////////////////////////////////////////////
1011

    
1012
  public Static3D[] getRotationAxis()
1013
    {
1014
    return mAxis;
1015
    }
1016

    
1017
///////////////////////////////////////////////////////////////////////////////////////////////////
1018

    
1019
  public int[][] getBasicAngle()
1020
    {
1021
    return mBasicAngle;
1022
    }
1023

    
1024
///////////////////////////////////////////////////////////////////////////////////////////////////
1025

    
1026
  public ObjectSignature getSignature()
1027
    {
1028
    return mSignature;
1029
    }
1030

    
1031
///////////////////////////////////////////////////////////////////////////////////////////////////
1032

    
1033
  public String getObjectName()
1034
    {
1035
    return mLongName;
1036
    }
1037

    
1038
///////////////////////////////////////////////////////////////////////////////////////////////////
1039

    
1040
  public String getShortName()
1041
    {
1042
    return mShortName;
1043
    }
1044

    
1045
///////////////////////////////////////////////////////////////////////////////////////////////////
1046

    
1047
  public String getInventor()
1048
    {
1049
    return mInventor;
1050
    }
1051

    
1052
///////////////////////////////////////////////////////////////////////////////////////////////////
1053

    
1054
  public int getYearOfInvention()
1055
    {
1056
    return mYearOfInvention;
1057
    }
1058

    
1059
///////////////////////////////////////////////////////////////////////////////////////////////////
1060

    
1061
  public int getComplexity()
1062
    {
1063
    return mComplexity;
1064
    }
1065

    
1066
///////////////////////////////////////////////////////////////////////////////////////////////////
1067

    
1068
  public int getNumFaces()
1069
    {
1070
    return mNumFaces;
1071
    }
1072

    
1073
///////////////////////////////////////////////////////////////////////////////////////////////////
1074

    
1075
  public int getNumFaceColors()
1076
    {
1077
    return mNumFaceColors;
1078
    }
1079

    
1080
///////////////////////////////////////////////////////////////////////////////////////////////////
1081

    
1082
  public int[] getNumLayers()
1083
    {
1084
    return mNumLayers;
1085
    }
1086

    
1087
///////////////////////////////////////////////////////////////////////////////////////////////////
1088

    
1089
  public float getSize()
1090
    {
1091
    return mSize;
1092
    }
1093

    
1094
///////////////////////////////////////////////////////////////////////////////////////////////////
1095

    
1096
  public int getColor(int face)
1097
    {
1098
    return mColor[face];
1099
    }
1100

    
1101
///////////////////////////////////////////////////////////////////////////////////////////////////
1102

    
1103
  public int getInternalColor()
1104
    {
1105
    return mInternalColor;
1106
    }
1107

    
1108
///////////////////////////////////////////////////////////////////////////////////////////////////
1109

    
1110
  public boolean shouldResetTextureMaps()
1111
    {
1112
    return mResetMaps;
1113
    }
1114

    
1115
///////////////////////////////////////////////////////////////////////////////////////////////////
1116

    
1117
  public String getTutorialObject()
1118
    {
1119
    return mTutorialObject;
1120
    }
1121

    
1122
///////////////////////////////////////////////////////////////////////////////////////////////////
1123

    
1124
  public String[][] getTutorials()
1125
    {
1126
    return mTutorials;
1127
    }
1128
}
(1-1/2)