Project

General

Profile

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

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

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_CUBOID;
13
import static org.distorted.objectlib.objects.TwistyBandagedMegaminx.OBJECT_NAME_MEGAMINX;
14
import static org.distorted.objectlib.objects.TwistyBandagedPyraminx.OBJECT_NAME_PYRAMINX;
15
import static org.distorted.objectlib.scrambling.ScrambleStateLocallyBandaged.MAX_SUPPORTED_SIZE;
16

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

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

    
33
import org.distorted.library.type.Static3D;
34
import org.distorted.library.type.Static4D;
35

    
36
import org.distorted.objectlib.helpers.ObjectShape;
37
import org.distorted.objectlib.helpers.ObjectSticker;
38
import org.distorted.objectlib.main.ObjectType;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

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

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

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

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

    
102
    if( longName.equals(OBJECT_NAME_CUBOID)   ||
103
        longName.equals(OBJECT_NAME_PYRAMINX) ||
104
        longName.equals(OBJECT_NAME_MEGAMINX)  ) return new ObjectSignature(shortName,signature);
105

    
106
    return new ObjectSignature(signature);
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

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

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

    
137
    boolean free = metadata.optBoolean("free", true);
138

    
139
    if( free ) mPrice = 0;
140
    else mPrice = metadata.optInt("price", ObjectType.DEFAULT_PRICE_OF_OLD_OBJECTS );
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  private void parseCubits(JSONArray object) throws JSONException
146
    {
147
    int numCubits = object.length();
148

    
149
    mCubitQuats     = new Static4D[numCubits];
150
    mCubitVariant   = new int[numCubits];
151
    mPositions      = new float[numCubits][];
152
    mCubitFaceColor = new int[numCubits][];
153
    mCubitType      = new int[numCubits];
154
    mCubitRowOffset = new float[numCubits][];
155

    
156
    for(int i=0; i<numCubits; i++)
157
      {
158
      JSONObject jsonCubit = object.getJSONObject(i);
159

    
160
      float qx = (float)jsonCubit.getDouble("qx");
161
      float qy = (float)jsonCubit.getDouble("qy");
162
      float qz = (float)jsonCubit.getDouble("qz");
163
      float qw = (float)jsonCubit.getDouble("qw");
164

    
165
      mCubitQuats[i] = new Static4D(qx,qy,qz,qw);
166
      mCubitVariant[i] = jsonCubit.getInt("variant");
167

    
168
      JSONArray jsonCenter = jsonCubit.getJSONArray("centers");
169
      int numCenter = jsonCenter.length();
170
      mPositions[i] = new float[numCenter];
171
      for(int j=0; j<numCenter; j++) mPositions[i][j] = (float)jsonCenter.getDouble(j);
172

    
173
      JSONArray jsonColor  = jsonCubit.getJSONArray("colors");
174
      int numColor = jsonColor.length();
175
      mCubitFaceColor[i] = new int[numColor];
176
      for(int j=0; j<numColor; j++) mCubitFaceColor[i][j] = jsonColor.getInt(j);
177

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

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  private void parseShapes(JSONArray object) throws JSONException
189
    {
190
    mNumCubitVariants     = object.length();
191
    mVariantStickerShape  = new int[mNumCubitVariants][];
192
    mVariantFaceIsOuter   = new int[mNumCubitVariants][];
193
    mShapes               = new ObjectShape[mNumCubitVariants];
194
    mFaceShapes           = new ObjectFaceShape[mNumCubitVariants];
195
    mVertexEffects        = new ObjectVertexEffects[mNumCubitVariants];
196
    mNumCubitFaces = -1;
197

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

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

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

    
216
      JSONArray jsonFaces= jsonShape.getJSONArray("faces");
217
      int numFaces = jsonFaces.length();
218
      if( mNumCubitFaces<numFaces ) mNumCubitFaces=numFaces;
219
      boolean multigon = jsonShape.optBoolean("facesMultigon", false);
220

    
221
      if( !multigon )
222
        {
223
        int[][] vertIndices = new int[numFaces][];
224

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

    
234
        mShapes[i] = new ObjectShape(verts,vertIndices);
235
        }
236
      else
237
        {
238
        int[][][] vertIndices = new int[numFaces][][];
239

    
240
        for(int f=0; f<numFaces; f++)
241
          {
242
          JSONObject jsonFace = jsonFaces.getJSONObject(f);
243
          JSONArray vertices = jsonFace.getJSONArray("vertexIndices");
244
          int numC = vertices.length();
245
          vertIndices[f] = new int[numC][];
246

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

    
256
        mShapes[i] = new ObjectShape(verts,vertIndices);
257
        }
258

    
259
      /////////////////////////////////////////////////////////////////////////
260
      ////// StickerShape & Outer /////////////////////////////////////////////
261
      mVariantStickerShape[i]= new int[numFaces];
262
      mVariantFaceIsOuter[i] = new int[numFaces];
263
      int[] bandIndices = new int[numFaces];
264

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

    
273
      /////////////////////////////////////////////////////////////////////////
274
      ////// ObjectFaceShape //////////////////////////////////////////////////
275
      JSONArray jsonBands= jsonShape.getJSONArray("bands");
276
      int numBands = jsonBands.length();
277
      float[][] bands = new float[numBands][7];
278

    
279
      for(int b=0; b<numBands; b++)
280
        {
281
        JSONObject jsonBand = jsonBands.getJSONObject(b);
282
        float[] band = bands[b];
283

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

    
293
      JSONObject jsonConvexity = jsonShape.optJSONObject("convexity");
294
      float[] convexity = null;
295

    
296
      if( jsonConvexity!=null )
297
        {
298
        convexity = new float[3];
299
        convexity[0] = (float)jsonConvexity.getDouble("x");
300
        convexity[1] = (float)jsonConvexity.getDouble("y");
301
        convexity[2] = (float)jsonConvexity.getDouble("z");
302
        }
303

    
304
      mFaceShapes[i] = new ObjectFaceShape(bands,bandIndices,convexity);
305

    
306
      /////////////////////////////////////////////////////////////////////////
307
      ////// ObjectVertexEffects //////////////////////////////////////////////
308
      JSONArray jsonEffects = jsonShape.optJSONArray("effects");
309

    
310
      if( jsonEffects!=null )
311
        {
312
        int numEffects = jsonEffects.length();
313

    
314
        String[] name    = new String[numEffects];
315
        float[][] vars   = new float[numEffects][5];
316
        float[][] center = new float[numEffects][3];
317
        float[][] region = new float[numEffects][4];
318
        boolean[] use    = new boolean[numEffects];
319

    
320
        for(int j=0; j<numEffects; j++)
321
          {
322
          JSONObject jsonEffect = jsonEffects.getJSONObject(j);
323

    
324
          name[j] = jsonEffect.getString("name");
325

    
326
          vars[j][0] = (float)jsonEffect.getDouble("var0");
327
          vars[j][1] = (float)jsonEffect.getDouble("var1");
328
          vars[j][2] = (float)jsonEffect.getDouble("var2");
329
          vars[j][3] = (float)jsonEffect.getDouble("var3");
330
          vars[j][4] = (float)jsonEffect.getDouble("var4");
331

    
332
          center[j][0] = (float)jsonEffect.getDouble("center0");
333
          center[j][1] = (float)jsonEffect.getDouble("center1");
334
          center[j][2] = (float)jsonEffect.getDouble("center2");
335

    
336
          region[j][0] = (float)jsonEffect.getDouble("region0");
337
          region[j][1] = (float)jsonEffect.getDouble("region1");
338
          region[j][2] = (float)jsonEffect.getDouble("region2");
339
          region[j][3] = (float)jsonEffect.getDouble("region3");
340

    
341
          use[j] = jsonEffect.getBoolean("use");
342
          }
343

    
344
        mVertexEffects[i] = new ObjectVertexEffects(name,vars,center,region,use);
345
        }
346
      }
347
    }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

    
351
  private void parseStickers11(JSONArray object) throws JSONException
352
    {
353
    mNumStickerTypes = object.length();
354
    mObjectSticker = new ObjectSticker[mNumStickerTypes];
355

    
356
    for(int i=0; i<mNumStickerTypes; i++)
357
      {
358
      JSONObject sticker = object.getJSONObject(i);
359
      float stroke = (float)sticker.getDouble("stroke");
360
      JSONArray vertices = sticker.getJSONArray("vertices");
361
      int numVertices = vertices.length();
362

    
363
      float[][][] coords   = new float[1][numVertices][2];
364
      float[][] curvatures = new float[1][numVertices];
365
      float[][] radii      = new float[1][numVertices];
366

    
367
      for(int j=0; j<numVertices; j++)
368
        {
369
        JSONObject vertex = vertices.getJSONObject(j);
370

    
371
        coords[0][j][0]  = (float)vertex.getDouble("x");
372
        coords[0][j][1]  = (float)vertex.getDouble("y");
373
        curvatures[0][j] = (float)vertex.getDouble("angle");
374
        radii[0][j]      = (float)vertex.getDouble("radius");
375
        }
376

    
377
      mObjectSticker[i] = new ObjectSticker(coords,curvatures,radii,stroke);
378
      }
379
    }
380

    
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382

    
383
  private void parseStickers12OrUp(JSONArray object) throws JSONException
384
    {
385
    mNumStickerTypes = object.length();
386
    mObjectSticker = new ObjectSticker[mNumStickerTypes];
387

    
388
    for(int i=0; i<mNumStickerTypes; i++)
389
      {
390
      JSONObject sticker = object.getJSONObject(i);
391
      float stroke = (float)sticker.getDouble("stroke");
392
      JSONArray loops = sticker.getJSONArray("loops");
393
      int numLoops = loops.length();
394

    
395
      float[][][] coords   = new float[numLoops][][];
396
      float[][] curvatures = new float[numLoops][];
397
      float[][] radii      = new float[numLoops][];
398

    
399
      for(int l=0; l<numLoops; l++)
400
        {
401
        JSONArray loop = loops.getJSONArray(l);
402
        int numVertices= loop.length();
403

    
404
        coords[l]     = new float[numVertices][2];
405
        curvatures[l] = new float[numVertices];
406
        radii[l]      = new float[numVertices];
407

    
408
        for(int v=0; v<numVertices; v++)
409
          {
410
          JSONObject vertex= loop.getJSONObject(v);
411

    
412
          coords[l][v][0]  = (float)vertex.getDouble("x");
413
          coords[l][v][1]  = (float)vertex.getDouble("y");
414
          curvatures[l][v] = (float)vertex.getDouble("angle");
415
          radii[l][v]      = (float)vertex.getDouble("radius");
416
          }
417
        }
418

    
419
      mObjectSticker[i] = new ObjectSticker(coords,curvatures,radii,stroke);
420
      }
421
    }
422

    
423
///////////////////////////////////////////////////////////////////////////////////////////////////
424

    
425
  private void parseOverrides(JSONArray object) throws JSONException
426
    {
427
    if( object!=null )
428
      {
429
      int numOverrides = object.length();
430
      mStickerOverrides= new ObjectStickerOverride[numOverrides];
431

    
432
      for(int i=0; i<numOverrides; i++)
433
        {
434
        JSONObject override  = object.getJSONObject(i);
435
        JSONArray cubitArray = override.getJSONArray("cubitfaces");
436
        int color = override.getInt("color");
437
        int numCubits = cubitArray.length();
438
        int[] cubitface = new int[numCubits];
439
        for(int j=0; j<numCubits; j++) cubitface[j] = cubitArray.getInt(j);
440
        mStickerOverrides[i] = new ObjectStickerOverride(cubitface,color);
441
        }
442
      }
443
    else
444
      {
445
      mStickerOverrides = null;
446
      }
447
    }
448

    
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450

    
451
  private void parseMesh(JSONObject object, int major) throws JSONException
452
    {
453
    JSONArray cubits   = object.getJSONArray("cubits");
454
    parseCubits(cubits);
455
    JSONArray shapes   = object.getJSONArray("shapes");
456
    parseShapes(shapes);
457
    JSONArray stickers = object.getJSONArray("stickers");
458
    if( major<=11 ) parseStickers11(stickers);
459
    else            parseStickers12OrUp(stickers);
460

    
461
    JSONArray overrides= object.optJSONArray("overrides");
462
    parseOverrides(overrides);
463

    
464
    mPillowCoeff = (float)object.optDouble("pillow",1.0);
465
    }
466

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

    
469
  private void parseAxis(JSONArray object) throws JSONException
470
    {
471
    int numAxis = object.length();
472

    
473
    mBasicAngle     = new int[numAxis][];
474
    mAxis           = new Static3D[numAxis];
475
    mCuts           = new float[numAxis][];
476
    mLayerRotatable = new boolean[numAxis][];
477
    mRotationFactor = new float[numAxis][];
478
    mNumLayers      = new int[numAxis];
479

    
480
    for(int i=0; i<numAxis; i++)
481
      {
482
      JSONObject jsonAx = object.getJSONObject(i);
483

    
484
      float x = (float)jsonAx.getDouble("x");
485
      float y = (float)jsonAx.getDouble("y");
486
      float z = (float)jsonAx.getDouble("z");
487

    
488
      mAxis[i] = new Static3D(x,y,z);
489

    
490
      JSONArray jsonAngles = jsonAx.getJSONArray("basicAngles");
491
      int numAngles = jsonAngles.length();
492
      mBasicAngle[i] = new int[numAngles];
493
      for(int j=0; j<numAngles; j++) mBasicAngle[i][j] = jsonAngles.getInt(j);
494

    
495
      JSONArray jsonCuts = jsonAx.getJSONArray("cuts");
496
      int numCuts = jsonCuts.length();
497
      mCuts[i] = new float[numCuts];
498
      for(int j=0; j<numCuts; j++) mCuts[i][j] = (float)jsonCuts.getDouble(j);
499

    
500
      JSONArray jsonRota = jsonAx.getJSONArray("rotatable");
501
      int numRota = jsonRota.length();
502
      mLayerRotatable[i] = new boolean[numRota];
503
      for(int j=0; j<numRota; j++) mLayerRotatable[i][j] = jsonRota.getBoolean(j);
504

    
505
      JSONArray jsonFactor = jsonAx.optJSONArray("factor");
506

    
507
      if( jsonFactor!=null )
508
        {
509
        int numFactor = jsonFactor.length();
510
        mRotationFactor[i] = new float[numFactor];
511
        for(int j=0; j<numFactor; j++) mRotationFactor[i][j] = (float)jsonFactor.getDouble(j);
512
        }
513
      else
514
        {
515
        mRotationFactor[i] = new float[numRota];
516
        for(int j=0; j<numRota; j++) mRotationFactor[i][j] = 1.0f;
517
        }
518

    
519
      mNumLayers[i] = numRota;
520
      }
521
    }
522

    
523
///////////////////////////////////////////////////////////////////////////////////////////////////
524

    
525
  private void parseQuats(JSONArray object) throws JSONException
526
    {
527
    int numQuats = object.length();
528
    mQuats = new Static4D[numQuats];
529

    
530
    for(int i=0; i<numQuats; i++)
531
      {
532
      JSONObject jsonQuat = object.getJSONObject(i);
533

    
534
      float x = (float)jsonQuat.getDouble("x");
535
      float y = (float)jsonQuat.getDouble("y");
536
      float z = (float)jsonQuat.getDouble("z");
537
      float w = (float)jsonQuat.getDouble("w");
538

    
539
      mQuats[i] = new Static4D(x,y,z,w);
540
      }
541
    }
542

    
543
///////////////////////////////////////////////////////////////////////////////////////////////////
544

    
545
  private int getAlgorithmIndex(int ax, int layer, int angle)
546
    {
547
    int numAlgs = mAlgorithms.length;
548

    
549
    for(int alg=0; alg<numAlgs; alg++)
550
      {
551
      int[] a = mAlgorithms[alg];
552
      if( a.length>=3 && a[0]==ax && a[1]==layer && a[2]==angle ) return alg;
553
      }
554

    
555
    return -1;
556
    }
557

    
558
///////////////////////////////////////////////////////////////////////////////////////////////////
559

    
560
  private int[] produceEdge(int[][] scramblingData)
561
    {
562
    int index=0, length=0;
563
    int numAxis = scramblingData.length;
564
    for (int[] scramblingDatum : scramblingData) length += scramblingDatum.length;
565

    
566
    int[] ret = new int[2*length/3];
567

    
568
    for(int ax=0; ax<numAxis; ax++)
569
      {
570
      int[] data = scramblingData[ax];
571
      int num = data.length/3;
572

    
573
      for(int j=0; j<num; j++)
574
        {
575
        int layer = data[3*j];
576
        int angle = data[3*j+1];
577
        int state = data[3*j+2];
578

    
579
        ret[2*index  ] = getAlgorithmIndex(ax,layer,angle);
580
        ret[2*index+1] = state;
581
        index++;
582
        }
583
      }
584

    
585
    return ret;
586
    }
587

    
588
///////////////////////////////////////////////////////////////////////////////////////////////////
589

    
590
  private void parseScrambling6(JSONObject object) throws JSONException
591
    {
592
    JSONArray jsonStates = object.getJSONArray("scrambleStates");
593
    int numStates = jsonStates.length();
594
    int[][][] scramblingData = new int[numStates][][];
595

    
596
    for(int i=0; i<numStates; i++)
597
      {
598
      JSONArray jsonState = jsonStates.getJSONArray(i);
599
      int numAxis = jsonState.length();
600
      scramblingData[i] = new int[numAxis][];
601

    
602
      for(int j=0; j<numAxis; j++)
603
        {
604
        JSONArray jsonData = jsonState.getJSONArray(j);
605
        int numData = jsonData.length();
606
        scramblingData[i][j] = new int[numData];
607
        for(int k=0; k<numData; k++) scramblingData[i][j][k] = jsonData.getInt(k);
608
        }
609
      }
610

    
611
    mAlgorithms = ScrambleEdgeGenerator.getScramblingAlgorithms(mBasicAngle);
612
    mEdges = new int[numStates][];
613
    for(int i=0; i<numStates; i++) mEdges[i] = produceEdge(scramblingData[i]);
614
    }
615

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

    
618
  private void parseScrambling7to10(JSONObject object) throws JSONException
619
    {
620
    JSONArray jsonAlgorithms = object.getJSONArray("algorithms");
621
    int numAlgs = jsonAlgorithms.length();
622
    mAlgorithms = new int[numAlgs][];
623

    
624
    for(int i=0; i<numAlgs; i++)
625
      {
626
      JSONArray jsonAlg = jsonAlgorithms.getJSONArray(i);
627
      int numEntries = jsonAlg.length();
628
      mAlgorithms[i] = new int[numEntries];
629
      for(int j=0; j<numEntries; j++)
630
        {
631
        int entry = jsonAlg.getInt(j);
632
        mAlgorithms[i][j] = ( (j%3)==1 ? (1<<entry) : entry );
633
        }
634
      }
635

    
636
    JSONArray jsonEdges = object.getJSONArray("edges");
637
    int numEdges = jsonEdges.length();
638
    mEdges = new int[numEdges][];
639

    
640
    for(int i=0; i<numEdges; i++)
641
      {
642
      JSONArray jsonEdge = jsonEdges.getJSONArray(i);
643
      int numEntries = jsonEdge.length();
644
      mEdges[i] = new int[numEntries];
645
      for(int j=0; j<numEntries; j++) mEdges[i][j] = jsonEdge.getInt(j);
646
      }
647
    }
648

    
649
///////////////////////////////////////////////////////////////////////////////////////////////////
650

    
651
  private void parseScrambling11orMore(JSONObject object) throws JSONException
652
    {
653
    JSONArray jsonAlgorithms = object.getJSONArray("algorithms");
654
    int numAlgs = jsonAlgorithms.length();
655
    mAlgorithms = new int[numAlgs][];
656

    
657
    for(int i=0; i<numAlgs; i++)
658
      {
659
      JSONArray jsonAlg = jsonAlgorithms.getJSONArray(i);
660
      int numEntries = jsonAlg.length();
661
      mAlgorithms[i] = new int[numEntries];
662
      for(int j=0; j<numEntries; j++) mAlgorithms[i][j] = jsonAlg.getInt(j);
663
      }
664

    
665
    JSONArray jsonEdges = object.getJSONArray("edges");
666
    int numEdges = jsonEdges.length();
667
    mEdges = new int[numEdges][];
668

    
669
    for(int i=0; i<numEdges; i++)
670
      {
671
      JSONArray jsonEdge = jsonEdges.getJSONArray(i);
672
      int numEntries = jsonEdge.length();
673
      mEdges[i] = new int[numEntries];
674
      for(int j=0; j<numEntries; j++) mEdges[i][j] = jsonEdge.getInt(j);
675
      }
676
    }
677

    
678
///////////////////////////////////////////////////////////////////////////////////////////////////
679

    
680
  private void parseScrambling(JSONObject object, int major) throws JSONException
681
    {
682
    mScrambleType = object.getInt("scrambleType");
683

    
684
    if( mScrambleType==0 )
685
      {
686
      if( major==6 )       parseScrambling6(object);
687
      else if( major<=10 ) parseScrambling7to10(object);
688
      else                 parseScrambling11orMore(object);
689
      }
690
    }
691

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

    
694
  private void parseTouchcontrol(JSONObject object) throws JSONException
695
    {
696
    mMovementType = object.getInt("movementType");
697
    mMovementSplit= object.getInt("movementSplit");
698

    
699
    try
700
      {
701
      JSONArray jsonEnabled = object.getJSONArray("enabledAxis");
702
      int numFace = jsonEnabled.length();
703

    
704
      mEnabled = new int[numFace][][];
705

    
706
      for(int i=0; i<numFace; i++)
707
        {
708
        JSONArray jsonSection = jsonEnabled.getJSONArray(i);
709
        int numSection = jsonSection.length();
710
        mEnabled[i] = new int[numSection][];
711

    
712
        for(int j=0; j<numSection; j++)
713
          {
714
          JSONArray jsonAx = jsonSection.getJSONArray(j);
715
          int numAxis = jsonAx.length();
716
          mEnabled[i][j] = new int[numAxis];
717
          for(int k=0; k<numAxis; k++) mEnabled[i][j][k] = jsonAx.getInt(k);
718
          }
719
        }
720
      }
721
    catch( JSONException ex )
722
      {
723
      // ignore, the object does not have to have 'enabledAxis' defined at all.
724
      }
725

    
726
    try
727
      {
728
      JSONArray jsonDist = object.getJSONArray("dist3D");
729
      int num = jsonDist.length();
730
      mDist3D = new float[num];
731
      for(int j=0; j<num; j++) mDist3D[j] = (float)jsonDist.getDouble(j);
732
      }
733
    catch( JSONException ex )
734
      {
735
      // ignore, the object does not have a 'dist3D' which is possible.
736
      }
737
    }
738

    
739
///////////////////////////////////////////////////////////////////////////////////////////////////
740

    
741
  private void parseColors(JSONArray object) throws JSONException
742
    {
743
    mNumFaceColors = object.length()-1;
744

    
745
    mColor = new int[mNumFaceColors];
746
    for(int i=0; i<mNumFaceColors; i++) mColor[i] = object.getInt(i);
747

    
748
    mInternalColor = object.getInt(mNumFaceColors);
749
    }
750

    
751
///////////////////////////////////////////////////////////////////////////////////////////////////
752

    
753
  private void parseSolved(JSONObject solved) throws JSONException
754
    {
755
    mSolvedFuncIndex = solved.getInt("functionIndex");
756

    
757
    try
758
      {
759
      JSONArray groupArray= solved.getJSONArray("groups");
760
      int numGroups = groupArray.length();
761
      mSolvedQuats  = new int[numGroups][];
762

    
763
      for(int i=0; i<numGroups; i++)
764
        {
765
        JSONArray groupElements = groupArray.getJSONArray(i);
766
        int groupSize = groupElements.length();
767
        mSolvedQuats[i] = new int[groupSize];
768
        for(int j=0; j<groupSize; j++) mSolvedQuats[i][j] = groupElements.getInt(j);
769
        }
770
      }
771
    catch( JSONException ex )
772
      {
773
      // ignore, the object does not have to have an array of solved groups.
774
      }
775
    }
776

    
777
///////////////////////////////////////////////////////////////////////////////////////////////////
778

    
779
  private void parseFile(JSONObject object, int major) throws JSONException
780
    {
781
    JSONObject metadata    = object.getJSONObject("metadata");
782
    parseMetadata(metadata);
783
    JSONObject mesh        = object.getJSONObject("mesh");
784
    parseMesh(mesh,major);
785
    JSONArray axis         = object.getJSONArray("axis");
786
    parseAxis(axis);
787
    JSONArray quats        = object.getJSONArray("quats");
788
    parseQuats(quats);
789
    JSONObject scrambling  = object.getJSONObject("scrambling");
790
    parseScrambling(scrambling,major);
791
    JSONObject touchcontrol= object.getJSONObject("touchcontrol");
792
    parseTouchcontrol(touchcontrol);
793
    JSONArray colors       = object.getJSONArray("colors");
794
    parseColors(colors);
795
    JSONObject solved      = object.getJSONObject("solved");
796
    parseSolved(solved);
797
    }
798

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

    
801
  private void parseTutorial(JSONObject object) throws JSONException
802
    {
803
    mTutorialObject = object.getString("object");
804
    JSONArray tuts= object.getJSONArray("tutorials");
805

    
806
    int len = tuts.length();
807
    mTutorials = new String[len][4];
808

    
809
    for(int i=0; i<len; i++)
810
      {
811
      JSONObject tut = tuts.getJSONObject(i);
812
      mTutorials[i][0] = tut.getString("language");
813
      mTutorials[i][1] = tut.getString("link");
814
      mTutorials[i][2] = tut.getString("title");
815
      mTutorials[i][3] = tut.getString("author");
816
      }
817
    }
818

    
819
///////////////////////////////////////////////////////////////////////////////////////////////////
820
// PUBLIC
821
///////////////////////////////////////////////////////////////////////////////////////////////////
822

    
823
  public void parseJsonFile(InputStream jsonStream) throws JSONException, IOException
824
    {
825
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
826
    StringBuilder contents = new StringBuilder();
827
    String line;
828

    
829
    while( (line = br.readLine()) != null) contents.append(line);
830
    br.close();
831
    jsonStream.close();
832
    String contentsString = contents.toString();
833
    JSONObject object = new JSONObject(contentsString);
834
    int major = object.getInt("major");
835

    
836
    if( major>=6 )
837
      {
838
      parseFile(object,major);
839
      }
840
    else
841
      {
842
      android.util.Log.e("readJsonFile", "Unknown version "+major);
843
      }
844
    }
845

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

    
848
  public void parseJsonFileMetadata(InputStream jsonStream) throws JSONException, IOException
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>=6 )
862
      {
863
      JSONObject metadata = object.getJSONObject("metadata");
864
      parseMetadata(metadata);
865
      }
866
    else
867
      {
868
      android.util.Log.e("readJsonFileQuick", "Unknown version "+major);
869
      }
870
    }
871

    
872
///////////////////////////////////////////////////////////////////////////////////////////////////
873

    
874
  public void readNumScramblesAndPrice(InputStream stream) throws IOException, JSONException
875
    {
876
    BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
877

    
878
    StringBuilder contents = new StringBuilder();
879
    String tmp;
880
    while( (tmp = br.readLine()) != null) contents.append(tmp);
881
    br.close();
882
    stream.close();
883

    
884
    JSONObject object = new JSONObject(contents.toString());
885
    JSONObject metadata = object.getJSONObject("metadata");
886
    mNumScrambles = metadata.getInt("scrambles");
887

    
888
    boolean free = metadata.optBoolean("free",true);
889
    if( free ) mPrice = 0;
890
    else mPrice = metadata.optInt("price", ObjectType.DEFAULT_PRICE_OF_OLD_OBJECTS );
891
    }
892

    
893
///////////////////////////////////////////////////////////////////////////////////////////////////
894

    
895
  public void parseJsonTutorial(InputStream jsonStream) throws IOException, JSONException
896
    {
897
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
898
    StringBuilder contents = new StringBuilder();
899
    String tmp;
900

    
901
    while( (tmp = br.readLine()) != null) contents.append(tmp);
902
    br.close();
903
    jsonStream.close();
904

    
905
    JSONObject object = new JSONObject(contents.toString());
906
    int major = object.getInt("major");
907

    
908
    if( major==1 )
909
      {
910
      parseTutorial(object);
911
      }
912
    else
913
      {
914
      android.util.Log.e("readJsonFile", "Unknown tutorial version "+major);
915
      }
916
    }
917

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

    
920
  public int getScrambleType()
921
    {
922
    return mScrambleType;
923
    }
924

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

    
927
  public int[][] getScrambleEdges()
928
    {
929
    return mEdges;
930
    }
931

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

    
934
  public int[][] getScrambleAlgorithms()
935
    {
936
    return mAlgorithms;
937
    }
938

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

    
941
  public int[][] getSolvedQuats()
942
    {
943
    return mSolvedQuats;
944
    }
945

    
946
///////////////////////////////////////////////////////////////////////////////////////////////////
947

    
948
  public Static4D[] getQuats()
949
    {
950
    return mQuats;
951
    }
952

    
953
///////////////////////////////////////////////////////////////////////////////////////////////////
954

    
955
  public int getSolvedFunctionIndex()
956
    {
957
    return mSolvedFuncIndex;
958
    }
959

    
960
///////////////////////////////////////////////////////////////////////////////////////////////////
961

    
962
  public int getNumStickerTypes()
963
    {
964
    return mNumStickerTypes;
965
    }
966

    
967
///////////////////////////////////////////////////////////////////////////////////////////////////
968

    
969
  public float[][] getCuts()
970
    {
971
    return mCuts;
972
    }
973

    
974
///////////////////////////////////////////////////////////////////////////////////////////////////
975

    
976
  public boolean[][] getLayerRotatable()
977
    {
978
    return mLayerRotatable;
979
    }
980

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

    
983
  public int getMovementType()
984
    {
985
    return mMovementType;
986
    }
987

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

    
990
  public int getMovementSplit()
991
    {
992
    return mMovementSplit;
993
    }
994

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

    
997
  public int[][][] getEnabled()
998
    {
999
    return mEnabled;
1000
    }
1001

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

    
1004
  public float[] getDist3D()
1005
    {
1006
    return mDist3D;
1007
    }
1008

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

    
1011
  public int getNumCubitFaces()
1012
    {
1013
    return mNumCubitFaces;
1014
    }
1015

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

    
1018
  public float[][] getCubitPositions()
1019
    {
1020
    return mPositions;
1021
    }
1022

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

    
1025
  public ObjectShape getObjectShape(int variant)
1026
    {
1027
    return mShapes[variant];
1028
    }
1029

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

    
1032
  public ObjectFaceShape getObjectFaceShape(int variant)
1033
    {
1034
    return mFaceShapes[variant];
1035
    }
1036

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

    
1039
  public ObjectVertexEffects getVertexEffects(int variant)
1040
    {
1041
    return mVertexEffects[variant];
1042
    }
1043

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

    
1046
  public Static4D getCubitQuats(int cubit)
1047
    {
1048
    return mCubitQuats[cubit];
1049
    }
1050

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

    
1053
  public int getNumCubitVariants()
1054
    {
1055
    return mNumCubitVariants;
1056
    }
1057

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

    
1060
  public int getCubitVariant(int cubit)
1061
    {
1062
    return mCubitVariant[cubit];
1063
    }
1064

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

    
1067
  public int getVariantStickerShape(int variant, int face)
1068
    {
1069
    int[] shapes = mVariantStickerShape[variant];
1070
    return shapes.length>face ? shapes[face] : -1;
1071
    }
1072

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

    
1075
  public float[][] returnRotationFactor()
1076
    {
1077
    return mRotationFactor;
1078
    }
1079

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

    
1082
  public int[] getCubitTypes()
1083
    {
1084
    return mCubitType;
1085
    }
1086

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

    
1089
  public float[][] getCubitOffsets()
1090
    {
1091
    return mCubitRowOffset;
1092
    }
1093

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

    
1096
  public int[][] getVariantFaceIsOuter()
1097
    {
1098
    return mVariantFaceIsOuter;
1099
    }
1100

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

    
1103
  public int getCubitFaceFace(int cubit, int face)
1104
    {
1105
    return mCubitFaceColor[cubit][face];
1106
    }
1107

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

    
1110
  public int getNumScrambles()
1111
    {
1112
    return mNumScrambles;
1113
    }
1114

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

    
1117
  public int getPrice()
1118
    {
1119
    return mPrice;
1120
    }
1121

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

    
1124
  public ObjectSticker retSticker(int sticker)
1125
    {
1126
    return mObjectSticker[sticker];
1127
    }
1128

    
1129
///////////////////////////////////////////////////////////////////////////////////////////////////
1130

    
1131
  public ObjectStickerOverride[] getStickerOverrides()
1132
    {
1133
    return mStickerOverrides;
1134
    }
1135

    
1136
///////////////////////////////////////////////////////////////////////////////////////////////////
1137

    
1138
  public float getPillowCoeff()
1139
    {
1140
    return mPillowCoeff;
1141
    }
1142

    
1143
///////////////////////////////////////////////////////////////////////////////////////////////////
1144

    
1145
  public Static3D[] getRotationAxis()
1146
    {
1147
    return mAxis;
1148
    }
1149

    
1150
///////////////////////////////////////////////////////////////////////////////////////////////////
1151

    
1152
  public int[][] getBasicAngle()
1153
    {
1154
    return mBasicAngle;
1155
    }
1156

    
1157
///////////////////////////////////////////////////////////////////////////////////////////////////
1158

    
1159
  public ObjectSignature getSignature()
1160
    {
1161
    return mSignature;
1162
    }
1163

    
1164
///////////////////////////////////////////////////////////////////////////////////////////////////
1165

    
1166
  public String getObjectName()
1167
    {
1168
    return mLongName;
1169
    }
1170

    
1171
///////////////////////////////////////////////////////////////////////////////////////////////////
1172

    
1173
  public String getShortName()
1174
    {
1175
    return mShortName;
1176
    }
1177

    
1178
///////////////////////////////////////////////////////////////////////////////////////////////////
1179

    
1180
  public String getInventor()
1181
    {
1182
    return mInventor;
1183
    }
1184

    
1185
///////////////////////////////////////////////////////////////////////////////////////////////////
1186

    
1187
  public int getYearOfInvention()
1188
    {
1189
    return mYearOfInvention;
1190
    }
1191

    
1192
///////////////////////////////////////////////////////////////////////////////////////////////////
1193

    
1194
  public int getComplexity()
1195
    {
1196
    return mComplexity;
1197
    }
1198

    
1199
///////////////////////////////////////////////////////////////////////////////////////////////////
1200

    
1201
  public int getNumFaces()
1202
    {
1203
    return mNumFaces;
1204
    }
1205

    
1206
///////////////////////////////////////////////////////////////////////////////////////////////////
1207

    
1208
  public int getNumFaceColors()
1209
    {
1210
    return mNumFaceColors;
1211
    }
1212

    
1213
///////////////////////////////////////////////////////////////////////////////////////////////////
1214

    
1215
  public int[] getNumLayers()
1216
    {
1217
    return mNumLayers;
1218
    }
1219

    
1220
///////////////////////////////////////////////////////////////////////////////////////////////////
1221

    
1222
  public float getSize()
1223
    {
1224
    return mSize;
1225
    }
1226

    
1227
///////////////////////////////////////////////////////////////////////////////////////////////////
1228

    
1229
  public int getColor(int face)
1230
    {
1231
    return mColor[face];
1232
    }
1233

    
1234
///////////////////////////////////////////////////////////////////////////////////////////////////
1235

    
1236
  public int getInternalColor()
1237
    {
1238
    return mInternalColor;
1239
    }
1240

    
1241
///////////////////////////////////////////////////////////////////////////////////////////////////
1242

    
1243
  public boolean shouldResetTextureMaps()
1244
    {
1245
    return mResetMaps;
1246
    }
1247

    
1248
///////////////////////////////////////////////////////////////////////////////////////////////////
1249

    
1250
  public String getTutorialObject()
1251
    {
1252
    return mTutorialObject;
1253
    }
1254

    
1255
///////////////////////////////////////////////////////////////////////////////////////////////////
1256

    
1257
  public String[][] getTutorials()
1258
    {
1259
    return mTutorials;
1260
    }
1261
}
(1-1/2)