Project

General

Profile

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

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

1 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.objectlib.json;
21
22
import java.io.BufferedReader;
23 bf52deb2 Leszek Koltunski
import java.io.File;
24
import java.io.FileInputStream;
25
import java.io.FileNotFoundException;
26 82eb152a Leszek Koltunski
import java.io.IOException;
27
import java.io.InputStream;
28
import java.io.InputStreamReader;
29
import java.nio.charset.StandardCharsets;
30
31 bf52deb2 Leszek Koltunski
import android.content.Context;
32
33 3ee1d662 Leszek Koltunski
import org.distorted.objectlib.helpers.ObjectFaceShape;
34 1d581993 Leszek Koltunski
import org.distorted.objectlib.helpers.ObjectSignature;
35 efeca8ef Leszek Koltunski
import org.distorted.objectlib.objects.TwistyBandagedGeneric;
36 82eb152a Leszek Koltunski
import org.json.JSONArray;
37
import org.json.JSONException;
38
import org.json.JSONObject;
39
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42
43
import org.distorted.objectlib.helpers.ObjectShape;
44
import org.distorted.objectlib.helpers.ObjectSticker;
45 10b7e306 Leszek Koltunski
import org.distorted.objectlib.scrambling.ScrambleState;
46 82eb152a Leszek Koltunski
import org.distorted.objectlib.main.ObjectType;
47
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50
public class JsonReader
51
{
52
  private ScrambleState[] mStates;
53
  private int[][] mSolvedQuats;
54
  private Static4D[] mQuats;
55
  private int mSolvedFuncIndex;
56
  private int mNumStickerTypes;
57
  private float[][] mCuts;
58
  private boolean[][] mLayerRotatable;
59
  private int mMovementType, mMovementSplit;
60
  private int[][][] mEnabled;
61
  private float[] mDist3D;
62 d66e98d7 Leszek Koltunski
  private int mNumCubitFaces, mNumFaces, mNumFaceColors;
63 82eb152a Leszek Koltunski
  private float[][] mPositions;
64
  private ObjectShape[] mShapes;
65 3ee1d662 Leszek Koltunski
  private ObjectFaceShape[] mFaceShapes;
66 82eb152a Leszek Koltunski
  private Static4D[] mCubitQuats;
67
  private int mNumCubitVariants;
68
  private int[] mCubitVariant;
69
  private int[][] mVariantFaceColor, mCubitFaceColor;
70
  private ObjectSticker[] mObjectSticker;
71
  private Static3D[] mAxis;
72 beee90ab Leszek Koltunski
  private int[][] mBasicAngle;
73 e1a86bf2 Leszek Koltunski
  private String mLongName, mShortName, mInventor;
74 82eb152a Leszek Koltunski
  private int mYearOfInvention, mComplexity;
75
  private int[] mNumLayers;
76
  private float mSize;
77 e1a86bf2 Leszek Koltunski
  private int mScrambleType, mNumScrambles;
78 82eb152a Leszek Koltunski
  private int[] mColor;
79 253e440f Leszek Koltunski
  private int mInternalColor;
80 0f72365b Leszek Koltunski
  private boolean mResetMaps;
81 052e0362 Leszek Koltunski
  private String mTutorialObject;
82
  private String[][] mTutorials;
83 1d581993 Leszek Koltunski
  private ObjectSignature mSignature;
84 82eb152a Leszek Koltunski
85 a2d6c41a Leszek Koltunski
  private static JsonReader mThis;
86
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
89
  private JsonReader()
90
    {
91
    }
92
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
95
  public static JsonReader getInstance()
96
    {
97
    if( mThis==null ) mThis = new JsonReader();
98
    return mThis;
99
    }
100
101 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
102
103 1d581993 Leszek Koltunski
  private void parseMetadata1(JSONObject object) throws JSONException
104 82eb152a Leszek Koltunski
    {
105
    mLongName        = object.getString("longname");
106 e1a86bf2 Leszek Koltunski
    mShortName       = object.getString("shortname");
107 82eb152a Leszek Koltunski
    mInventor        = object.getString("inventor");
108
    mYearOfInvention = object.getInt("year");
109
    mComplexity      = object.getInt("complexity");
110
    mSize            = (float)object.getDouble("size");
111 e1a86bf2 Leszek Koltunski
    mNumScrambles    = object.getInt("scrambles");
112 0f72365b Leszek Koltunski
    mResetMaps       = object.getBoolean("resetmaps");
113 a72cd106 Leszek Koltunski
    mNumFaces        = object.getInt("num_faces");
114 e1a86bf2 Leszek Koltunski
115 5f54927b Leszek Koltunski
    try
116
      {
117 1d581993 Leszek Koltunski
      long signature = object.getLong("signature");
118
      mSignature = new ObjectSignature(signature);
119 5f54927b Leszek Koltunski
      }
120
    catch(JSONException ex)
121
      {
122
      // objects older than Feb 2022 do not have the 'signature' field. They all use the ObjectType.ordinal
123
      // as their signature.
124 1d581993 Leszek Koltunski
      long signature = ObjectType.getOrdinal(mShortName);
125
      mSignature = new ObjectSignature(signature);
126
      }
127
    }
128
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
131
  private void parseMetadata2(JSONObject object) throws JSONException
132
    {
133
    mLongName        = object.getString("longname");
134
    mShortName       = object.getString("shortname");
135
    mInventor        = object.getString("inventor");
136
    mYearOfInvention = object.getInt("year");
137
    mComplexity      = object.getInt("complexity");
138
    mSize            = (float)object.getDouble("size");
139
    mNumScrambles    = object.getInt("scrambles");
140
    mResetMaps       = object.getBoolean("resetmaps");
141
    mNumFaces        = object.getInt("num_faces");
142
143
    try
144
      {
145
      long signature1 = object.getLong("signature1");
146
      long signature2 = object.getLong("signature2");
147
      long signature3 = object.getLong("signature3");
148 efeca8ef Leszek Koltunski
149
      if( mLongName.equals(TwistyBandagedGeneric.OBJECT_NAME) )
150
        {
151
        mSignature = new ObjectSignature(mShortName,signature1,signature2,signature3);
152
        }
153
      else
154
        {
155
        mSignature = new ObjectSignature(signature1,signature2,signature3);
156
        }
157 1d581993 Leszek Koltunski
      }
158
    catch(JSONException ex)
159
      {
160
      long signature = object.getLong("signature");
161
      mSignature = new ObjectSignature(signature);
162 5f54927b Leszek Koltunski
      }
163 82eb152a Leszek Koltunski
    }
164
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
167
  private void parseCubits(JSONArray object) throws JSONException
168
    {
169
    int numCubits = object.length();
170
171
    mCubitQuats     = new Static4D[numCubits];
172
    mCubitVariant   = new int[numCubits];
173
    mPositions      = new float[numCubits][];
174
    mCubitFaceColor = new int[numCubits][];
175
176
    for(int i=0; i<numCubits; i++)
177
      {
178
      JSONObject jsonCubit = object.getJSONObject(i);
179
180
      float qx = (float)jsonCubit.getDouble("qx");
181
      float qy = (float)jsonCubit.getDouble("qy");
182
      float qz = (float)jsonCubit.getDouble("qz");
183
      float qw = (float)jsonCubit.getDouble("qw");
184
185
      mCubitQuats[i] = new Static4D(qx,qy,qz,qw);
186
      mCubitVariant[i] = jsonCubit.getInt("variant");
187
188
      JSONArray jsonCenter = jsonCubit.getJSONArray("centers");
189
      int numCenter = jsonCenter.length();
190
      mPositions[i] = new float[numCenter];
191
      for(int j=0; j<numCenter; j++) mPositions[i][j] = (float)jsonCenter.getDouble(j);
192
193
      JSONArray jsonColor  = jsonCubit.getJSONArray("colors");
194
      int numColor = jsonColor.length();
195
      mCubitFaceColor[i] = new int[numColor];
196
      for(int j=0; j<numColor; j++) mCubitFaceColor[i][j] = jsonColor.getInt(j);
197
      }
198
    }
199
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201
202
  private void parseShapes(JSONArray object) throws JSONException
203
    {
204
    mNumCubitVariants = object.length();
205
    mVariantFaceColor = new int[mNumCubitVariants][];
206 3ee1d662 Leszek Koltunski
    mShapes     = new ObjectShape[mNumCubitVariants];
207
    mFaceShapes = new ObjectFaceShape[mNumCubitVariants];
208 57ef6378 Leszek Koltunski
    float[][][] verts     = new float[mNumCubitVariants][][];
209
    float[][][] bands     = new float[mNumCubitVariants][][];
210
    float[][][] corners   = new float[mNumCubitVariants][][];
211
    float[][][] centers   = new float[mNumCubitVariants][][];
212
    float[][] convexity   = new float[mNumCubitVariants][];
213 82eb152a Leszek Koltunski
    int[][] cornerIndices = new int[mNumCubitVariants][];
214
    int[][] centerIndices = new int[mNumCubitVariants][];
215 57ef6378 Leszek Koltunski
    int[][] bandIndices   = new int[mNumCubitVariants][];
216 82eb152a Leszek Koltunski
    int[][][] vertIndices = new int[mNumCubitVariants][][];
217
218 d66e98d7 Leszek Koltunski
    mNumCubitFaces = -1;
219
220 82eb152a Leszek Koltunski
    for(int i=0; i<mNumCubitVariants; i++)
221
      {
222
      JSONObject jsonShape = object.getJSONObject(i);
223
224
      ////// vertices /////////////////////////////////////////////////
225
      JSONArray jsonVertices= jsonShape.getJSONArray("vertices");
226
      int numVertices = jsonVertices.length();
227 57ef6378 Leszek Koltunski
      verts[i] = new float[numVertices][3];
228 82eb152a Leszek Koltunski
      cornerIndices[i] = new int[numVertices];
229
      centerIndices[i] = new int[numVertices];
230
231
      for(int j=0; j<numVertices; j++)
232
        {
233
        JSONObject vert = jsonVertices.getJSONObject(j);
234 57ef6378 Leszek Koltunski
        verts[i][j][0] = (float)vert.getDouble("x");
235
        verts[i][j][1] = (float)vert.getDouble("y");
236
        verts[i][j][2] = (float)vert.getDouble("z");
237 82eb152a Leszek Koltunski
        cornerIndices[i][j] = vert.getInt("cornerIndex");
238
        centerIndices[i][j] = vert.getInt("centerIndex");
239
        }
240
241
      ////// faces ////////////////////////////////////////////////////
242
      JSONArray jsonFaces= jsonShape.getJSONArray("faces");
243
      int numFaces = jsonFaces.length();
244
      mVariantFaceColor[i] = new int[numFaces];
245
      bandIndices[i] = new int[numFaces];
246
      vertIndices[i] = new int[numFaces][];
247
248 d66e98d7 Leszek Koltunski
      if( mNumCubitFaces<numFaces ) mNumCubitFaces=numFaces;
249
250 82eb152a Leszek Koltunski
      for(int j=0; j<numFaces; j++)
251
        {
252
        JSONObject jsonFace = jsonFaces.getJSONObject(j);
253
        mVariantFaceColor[i][j] = jsonFace.getInt("sticker");
254
        bandIndices[i][j] = jsonFace.getInt("bandIndex");
255
        JSONArray vertices = jsonFace.getJSONArray("vertexIndices");
256
        int numV = vertices.length();
257
        vertIndices[i][j] = new int[numV];
258
        for(int k=0; k<numV; k++) vertIndices[i][j][k] = vertices.getInt(k);
259
        }
260
261
      ////// bands ////////////////////////////////////////////////////
262
      JSONArray jsonBands= jsonShape.getJSONArray("bands");
263
      int numBands = jsonBands.length();
264
      bands[i] = new float[numBands][7];
265
266
      for(int j=0; j<numBands; j++)
267
        {
268
        JSONObject jsonBand = jsonBands.getJSONObject(j);
269
270
        bands[i][j][0] = (float)jsonBand.getDouble("height");
271
        bands[i][j][1] = (float)jsonBand.getDouble("angle");
272
        bands[i][j][2] = (float)jsonBand.getDouble("distanceToCenter");
273
        bands[i][j][3] = (float)jsonBand.getDouble("distanceToFlat");
274
        bands[i][j][4] = (float)jsonBand.getDouble("numOfBands");
275
        bands[i][j][5] = (float)jsonBand.getDouble("extraI");
276
        bands[i][j][6] = (float)jsonBand.getDouble("extraJ");
277
        }
278
279
      ////// cornerPush ///////////////////////////////////////////////
280 9e8eb9e4 Leszek Koltunski
      JSONArray jsonCornerPush= jsonShape.optJSONArray("cornerPush");
281 82eb152a Leszek Koltunski
282 9e8eb9e4 Leszek Koltunski
      if( jsonCornerPush!=null )
283 82eb152a Leszek Koltunski
        {
284 9e8eb9e4 Leszek Koltunski
        int numCornerP = jsonCornerPush.length();
285
        corners[i] = new float[numCornerP][2];
286
287
        for(int j=0; j<numCornerP; j++)
288
          {
289
          JSONObject jsonC = jsonCornerPush.getJSONObject(j);
290
          corners[i][j][0] = (float)jsonC.getDouble("strength");
291
          corners[i][j][1] = (float)jsonC.getDouble("radius");
292
          }
293 82eb152a Leszek Koltunski
        }
294
295
      ////// centerPush ///////////////////////////////////////////////
296 9e8eb9e4 Leszek Koltunski
      JSONArray jsonCenterPush= jsonShape.optJSONArray("centerPush");
297 82eb152a Leszek Koltunski
298 9e8eb9e4 Leszek Koltunski
      if( jsonCenterPush!=null )
299 82eb152a Leszek Koltunski
        {
300 9e8eb9e4 Leszek Koltunski
        int numCenterP = jsonCenterPush.length();
301
        centers[i] = new float[numCenterP][3];
302
303
        for(int j=0; j<numCenterP; j++)
304
          {
305
          JSONObject jsonC = jsonCenterPush.getJSONObject(j);
306
          centers[i][j][0] = (float)jsonC.getDouble("x");
307
          centers[i][j][1] = (float)jsonC.getDouble("y");
308
          centers[i][j][2] = (float)jsonC.getDouble("z");
309
          }
310 82eb152a Leszek Koltunski
        }
311
312
      ////// convexity ///////////////////////////////////////////////
313 9e8eb9e4 Leszek Koltunski
      JSONObject jsonConvexity = jsonShape.optJSONObject("convexity");
314
315
      if( jsonConvexity!=null )
316 82eb152a Leszek Koltunski
        {
317
        convexity[i] = new float[3];
318
        convexity[i][0] = (float)jsonConvexity.getDouble("x");
319
        convexity[i][1] = (float)jsonConvexity.getDouble("y");
320
        convexity[i][2] = (float)jsonConvexity.getDouble("z");
321
        }
322
      }
323
324
    for(int i=0; i<mNumCubitVariants; i++)
325
      {
326 59a971c1 Leszek Koltunski
      mShapes[i] = new ObjectShape(verts[i],vertIndices[i]);
327 3ee1d662 Leszek Koltunski
      mFaceShapes[i] = new ObjectFaceShape(bands[i],bandIndices[i],corners[i],cornerIndices[i],centers[i],centerIndices[i],convexity[i] );
328 82eb152a Leszek Koltunski
      }
329
    }
330
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332
333
  private void parseStickers(JSONArray object) throws JSONException
334
    {
335
    mNumStickerTypes = object.length();
336
    mObjectSticker = new ObjectSticker[mNumStickerTypes];
337
338
    for(int i=0; i<mNumStickerTypes; i++)
339
      {
340
      JSONObject sticker = object.getJSONObject(i);
341
      float stroke = (float)sticker.getDouble("stroke");
342
      JSONArray vertices = sticker.getJSONArray("vertices");
343
      int numVertices = vertices.length();
344
345
      float[] coords     = new float[2*numVertices];
346
      float[] curvatures = new float[numVertices];
347
      float[] radii      = new float[numVertices];
348
349
      for(int j=0; j<numVertices; j++)
350
        {
351
        JSONObject vertex = vertices.getJSONObject(j);
352
353
        coords[2*j  ] = (float)vertex.getDouble("x");
354
        coords[2*j+1] = (float)vertex.getDouble("y");
355
        curvatures[j] = (float)vertex.getDouble("angle");
356
        radii[j]      = (float)vertex.getDouble("radius");
357
        }
358
359
      mObjectSticker[i] = new ObjectSticker(coords,curvatures,radii,stroke);
360
      }
361
    }
362
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364
365
  private void parseMesh(JSONObject object) throws JSONException
366
    {
367
    JSONArray cubits   = object.getJSONArray("cubits");
368
    parseCubits(cubits);
369
    JSONArray shapes   = object.getJSONArray("shapes");
370
    parseShapes(shapes);
371
    JSONArray stickers = object.getJSONArray("stickers");
372
    parseStickers(stickers);
373
    }
374
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376
377 beee90ab Leszek Koltunski
  private void parseAxis1(JSONArray object) throws JSONException
378 82eb152a Leszek Koltunski
    {
379
    int numAxis = object.length();
380
381 beee90ab Leszek Koltunski
    mBasicAngle     = new int[numAxis][];
382 82eb152a Leszek Koltunski
    mAxis           = new Static3D[numAxis];
383
    mCuts           = new float[numAxis][];
384
    mLayerRotatable = new boolean[numAxis][];
385
    mNumLayers      = new int[numAxis];
386
387
    for(int i=0; i<numAxis; i++)
388
      {
389
      JSONObject jsonAx = object.getJSONObject(i);
390
391 beee90ab Leszek Koltunski
      float x = (float)jsonAx.getDouble("x");
392
      float y = (float)jsonAx.getDouble("y");
393
      float z = (float)jsonAx.getDouble("z");
394
395
      mAxis[i] = new Static3D(x,y,z);
396
397
      JSONArray jsonCuts = jsonAx.getJSONArray("cuts");
398
      int numCuts = jsonCuts.length();
399
      mCuts[i] = new float[numCuts];
400
      for(int j=0; j<numCuts; j++) mCuts[i][j] = (float)jsonCuts.getDouble(j);
401
402
      JSONArray jsonRota = jsonAx.getJSONArray("rotatable");
403
      int numRota = jsonRota.length();
404
      mLayerRotatable[i] = new boolean[numRota];
405
      for(int j=0; j<numRota; j++) mLayerRotatable[i][j] = jsonRota.getBoolean(j);
406
407
      mBasicAngle[i] = new int[numRota];
408
      int basicAngle = jsonAx.getInt("basicAngle");
409
      for(int j=0; j<numRota; j++) mBasicAngle[i][j] = basicAngle;
410
411
      mNumLayers[i] = numRota;
412
      }
413
    }
414
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416
417
  private void parseAxis2(JSONArray object) throws JSONException
418
    {
419
    int numAxis = object.length();
420
421
    mBasicAngle     = new int[numAxis][];
422
    mAxis           = new Static3D[numAxis];
423
    mCuts           = new float[numAxis][];
424
    mLayerRotatable = new boolean[numAxis][];
425
    mNumLayers      = new int[numAxis];
426
427
    for(int i=0; i<numAxis; i++)
428
      {
429
      JSONObject jsonAx = object.getJSONObject(i);
430 82eb152a Leszek Koltunski
431
      float x = (float)jsonAx.getDouble("x");
432
      float y = (float)jsonAx.getDouble("y");
433
      float z = (float)jsonAx.getDouble("z");
434
435
      mAxis[i] = new Static3D(x,y,z);
436
437 beee90ab Leszek Koltunski
      JSONArray jsonAngles = jsonAx.getJSONArray("basicAngles");
438
      int numAngles = jsonAngles.length();
439
      mBasicAngle[i] = new int[numAngles];
440
      for(int j=0; j<numAngles; j++) mBasicAngle[i][j] = jsonAngles.getInt(j);
441
442 82eb152a Leszek Koltunski
      JSONArray jsonCuts = jsonAx.getJSONArray("cuts");
443
      int numCuts = jsonCuts.length();
444
      mCuts[i] = new float[numCuts];
445
      for(int j=0; j<numCuts; j++) mCuts[i][j] = (float)jsonCuts.getDouble(j);
446
447
      JSONArray jsonRota = jsonAx.getJSONArray("rotatable");
448
      int numRota = jsonRota.length();
449
      mLayerRotatable[i] = new boolean[numRota];
450
      for(int j=0; j<numRota; j++) mLayerRotatable[i][j] = jsonRota.getBoolean(j);
451
452
      mNumLayers[i] = numRota;
453
      }
454
    }
455
456
///////////////////////////////////////////////////////////////////////////////////////////////////
457
458
  private void parseQuats(JSONArray object) throws JSONException
459
    {
460
    int numQuats = object.length();
461
    mQuats = new Static4D[numQuats];
462
463
    for(int i=0; i<numQuats; i++)
464
      {
465
      JSONObject jsonQuat = object.getJSONObject(i);
466
467
      float x = (float)jsonQuat.getDouble("x");
468
      float y = (float)jsonQuat.getDouble("y");
469
      float z = (float)jsonQuat.getDouble("z");
470
      float w = (float)jsonQuat.getDouble("w");
471
472
      mQuats[i] = new Static4D(x,y,z,w);
473
      }
474
    }
475
476
///////////////////////////////////////////////////////////////////////////////////////////////////
477
478
  private void parseScrambling(JSONObject object) throws JSONException
479
    {
480
    mScrambleType = object.getInt("scrambleType");
481
482
    if( mScrambleType==0 )
483
      {
484
      JSONArray jsonStates = object.getJSONArray("scrambleStates");
485
      int numStates = jsonStates.length();
486
      mStates = new ScrambleState[numStates];
487
488
      for(int i=0; i<numStates; i++)
489
        {
490
        JSONArray jsonState = jsonStates.getJSONArray(i);
491
        int numAxis = jsonState.length();
492
        int[][] scramblingData = new int[numAxis][];
493
494
        for(int j=0; j<numAxis; j++)
495
          {
496
          JSONArray jsonData = jsonState.getJSONArray(j);
497
          int numData = jsonData.length();
498
          scramblingData[j] = new int[numData];
499
          for(int k=0; k<numData; k++) scramblingData[j][k] = jsonData.getInt(k);
500
          }
501
502
        mStates[i] = new ScrambleState(scramblingData);
503
        }
504
      }
505
    }
506
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508
509
  private void parseTouchcontrol(JSONObject object) throws JSONException
510
    {
511
    mMovementType = object.getInt("movementType");
512
    mMovementSplit= object.getInt("movementSplit");
513
514 82904e62 Leszek Koltunski
    try
515 82eb152a Leszek Koltunski
      {
516 82904e62 Leszek Koltunski
      JSONArray jsonEnabled = object.getJSONArray("enabledAxis");
517
      int numFace = jsonEnabled.length();
518 82eb152a Leszek Koltunski
519 82904e62 Leszek Koltunski
      mEnabled = new int[numFace][][];
520
521
      for(int i=0; i<numFace; i++)
522 82eb152a Leszek Koltunski
        {
523 82904e62 Leszek Koltunski
        JSONArray jsonSection = jsonEnabled.getJSONArray(i);
524
        int numSection = jsonSection.length();
525
        mEnabled[i] = new int[numSection][];
526
527
        for(int j=0; j<numSection; j++)
528
          {
529
          JSONArray jsonAx = jsonSection.getJSONArray(j);
530
          int numAxis = jsonAx.length();
531
          mEnabled[i][j] = new int[numAxis];
532
          for(int k=0; k<numAxis; k++) mEnabled[i][j][k] = jsonAx.getInt(k);
533
          }
534 82eb152a Leszek Koltunski
        }
535
      }
536 82904e62 Leszek Koltunski
    catch( JSONException ex )
537
      {
538
      // ignore, the object does not have to have 'enabledAxis' defined at all.
539
      }
540 82eb152a Leszek Koltunski
541
    try
542
      {
543
      JSONArray jsonDist = object.getJSONArray("dist3D");
544 a72cd106 Leszek Koltunski
      int num = jsonDist.length();
545
      mDist3D = new float[num];
546
      for(int j=0; j<num; j++) mDist3D[j] = (float)jsonDist.getDouble(j);
547 82eb152a Leszek Koltunski
      }
548
    catch( JSONException ex )
549
      {
550 a72cd106 Leszek Koltunski
      // ignore, the object does not have a 'dist3D' which is possible.
551 82eb152a Leszek Koltunski
      }
552
    }
553
554
///////////////////////////////////////////////////////////////////////////////////////////////////
555
556
  private void parseColors(JSONArray object) throws JSONException
557
    {
558 253e440f Leszek Koltunski
    mNumFaceColors = object.length()-1;
559
560 82eb152a Leszek Koltunski
    mColor = new int[mNumFaceColors];
561
    for(int i=0; i<mNumFaceColors; i++) mColor[i] = object.getInt(i);
562 253e440f Leszek Koltunski
563
    mInternalColor = object.getInt(mNumFaceColors);
564 82eb152a Leszek Koltunski
    }
565
566 3c48fab9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
567
568
  private void parseSolved(JSONObject solved) throws JSONException
569
    {
570
    mSolvedFuncIndex = solved.getInt("functionIndex");
571
572
    try
573
      {
574
      JSONArray groupArray= solved.getJSONArray("groups");
575
      int numGroups = groupArray.length();
576
      mSolvedQuats  = new int[numGroups][];
577
578
      for(int i=0; i<numGroups; i++)
579
        {
580
        JSONArray groupElements = groupArray.getJSONArray(i);
581
        int groupSize = groupElements.length();
582
        mSolvedQuats[i] = new int[groupSize];
583
        for(int j=0; j<groupSize; j++) mSolvedQuats[i][j] = groupElements.getInt(j);
584
        }
585
      }
586
    catch( JSONException ex )
587
      {
588
      // ignore, the object does not have to have an array of solved groups.
589
      }
590
    }
591
592 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
593
594
  private void parseVersion1(JSONObject object) throws JSONException
595
    {
596
    JSONObject metadata    = object.getJSONObject("metadata");
597 1d581993 Leszek Koltunski
    parseMetadata1(metadata);
598 82eb152a Leszek Koltunski
    JSONObject mesh        = object.getJSONObject("mesh");
599
    parseMesh(mesh);
600
    JSONArray axis         = object.getJSONArray("axis");
601 beee90ab Leszek Koltunski
    parseAxis1(axis);
602
    JSONArray quats        = object.getJSONArray("quats");
603
    parseQuats(quats);
604
    JSONObject scrambling  = object.getJSONObject("scrambling");
605
    parseScrambling(scrambling);
606
    JSONObject touchcontrol= object.getJSONObject("touchcontrol");
607
    parseTouchcontrol(touchcontrol);
608
    JSONArray colors       = object.getJSONArray("colors");
609
    parseColors(colors);
610
    JSONObject solved      = object.getJSONObject("solved");
611
    parseSolved(solved);
612
    }
613
614
///////////////////////////////////////////////////////////////////////////////////////////////////
615
616
  private void parseVersion2(JSONObject object) throws JSONException
617
    {
618
    JSONObject metadata    = object.getJSONObject("metadata");
619 1d581993 Leszek Koltunski
    parseMetadata2(metadata);
620 beee90ab Leszek Koltunski
    JSONObject mesh        = object.getJSONObject("mesh");
621
    parseMesh(mesh);
622
    JSONArray axis         = object.getJSONArray("axis");
623
    parseAxis2(axis);
624 82eb152a Leszek Koltunski
    JSONArray quats        = object.getJSONArray("quats");
625
    parseQuats(quats);
626
    JSONObject scrambling  = object.getJSONObject("scrambling");
627
    parseScrambling(scrambling);
628
    JSONObject touchcontrol= object.getJSONObject("touchcontrol");
629
    parseTouchcontrol(touchcontrol);
630
    JSONArray colors       = object.getJSONArray("colors");
631
    parseColors(colors);
632 3c48fab9 Leszek Koltunski
    JSONObject solved      = object.getJSONObject("solved");
633
    parseSolved(solved);
634 82eb152a Leszek Koltunski
    }
635
636 052e0362 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
637
638
  private void parseVersion1Tutorial(JSONObject object) throws JSONException
639
    {
640
    mTutorialObject = object.getString("object");
641
    JSONArray tuts= object.getJSONArray("tutorials");
642
643
    int len = tuts.length();
644
    mTutorials = new String[len][4];
645
646
    for(int i=0; i<len; i++)
647
      {
648
      JSONObject tut = tuts.getJSONObject(i);
649
      mTutorials[i][0] = tut.getString("language");
650
      mTutorials[i][1] = tut.getString("link");
651
      mTutorials[i][2] = tut.getString("title");
652
      mTutorials[i][3] = tut.getString("author");
653
      }
654
    }
655
656 b39f8e39 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
657
658 1d581993 Leszek Koltunski
  private void parseVersion1Metadata(JSONObject object) throws JSONException
659
    {
660
    JSONObject metadata = object.getJSONObject("metadata");
661
    parseMetadata1(metadata);
662
    }
663
664
///////////////////////////////////////////////////////////////////////////////////////////////////
665
666
  private void parseVersion2Metadata(JSONObject object) throws JSONException
667 b39f8e39 Leszek Koltunski
    {
668
    JSONObject metadata = object.getJSONObject("metadata");
669 1d581993 Leszek Koltunski
    parseMetadata2(metadata);
670 b39f8e39 Leszek Koltunski
    }
671
672 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
673
674
  public void parseJsonFile(InputStream jsonStream)
675
    {
676
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
677
678
    try
679
      {
680
      StringBuilder contents = new StringBuilder();
681
      String tmp;
682
      while( (tmp = br.readLine()) != null) contents.append(tmp);
683
      br.close();
684
      jsonStream.close();
685
686
      JSONObject object = new JSONObject(contents.toString());
687
      int major = object.getInt("major");
688
689 e7d3259a Leszek Koltunski
      if( major==1 )
690
        {
691
        parseVersion1(object);
692
        }
693
      else if( major==2 )
694
        {
695
        parseVersion2(object);
696
        }
697 82eb152a Leszek Koltunski
      else
698
        {
699
        android.util.Log.e("readJsonFile", "Unknown version "+major);
700
        }
701
      }
702
    catch(IOException e)
703
      {
704
      android.util.Log.e("readJsonFile", "Error reading JSON file: "+e.toString());
705
      }
706
    catch( JSONException e )
707
      {
708
      android.util.Log.e("parseJsonFile", "Error parsing JSON file: "+e.toString());
709
      }
710
    }
711
712 b39f8e39 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
713
714
  public void parseJsonFileMetadata(InputStream jsonStream)
715
    {
716
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
717
718
    try
719
      {
720
      StringBuilder contents = new StringBuilder();
721
      String tmp;
722
      while( (tmp = br.readLine()) != null) contents.append(tmp);
723
      br.close();
724
      jsonStream.close();
725
726
      JSONObject object = new JSONObject(contents.toString());
727
      int major = object.getInt("major");
728
729 1d581993 Leszek Koltunski
      if( major==1 )
730
        {
731
        parseVersion1Metadata(object);
732
        }
733
      else if( major==2 )
734 b39f8e39 Leszek Koltunski
        {
735 1d581993 Leszek Koltunski
        parseVersion2Metadata(object);
736 b39f8e39 Leszek Koltunski
        }
737
      else
738
        {
739
        android.util.Log.e("readJsonFileQuick", "Unknown version "+major);
740
        }
741
      }
742
    catch(IOException e)
743
      {
744
      android.util.Log.e("readJsonFileQuick", "Error reading JSON file: "+e.toString());
745
      }
746
    catch( JSONException e )
747
      {
748
      android.util.Log.e("parseJsonFileQuick", "Error parsing JSON file: "+e.toString());
749
      }
750
    }
751
752 bf52deb2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
753
754 ef021035 Leszek Koltunski
  public int readNumScrambles(Context context, String fileName)
755 bf52deb2 Leszek Koltunski
    {
756
    File file = new File(context.getFilesDir(), fileName);
757
    InputStream stream;
758
759
    try
760
      {
761
      stream = new FileInputStream(file);
762 ef021035 Leszek Koltunski
      BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
763
764
      StringBuilder contents = new StringBuilder();
765
      String tmp;
766
      while( (tmp = br.readLine()) != null) contents.append(tmp);
767
      br.close();
768
      stream.close();
769
770
      JSONObject object = new JSONObject(contents.toString());
771
      int major = object.getInt("major");
772
773 938fc9af Leszek Koltunski
      if( major==1 || major==2 )
774 ef021035 Leszek Koltunski
        {
775
        JSONObject metadata = object.getJSONObject("metadata");
776
        return metadata.getInt("scrambles");
777
        }
778
      else
779
        {
780
        android.util.Log.e("readNumScrambles", "Unknown version "+major);
781
        }
782 bf52deb2 Leszek Koltunski
      }
783
    catch(FileNotFoundException ex)
784
      {
785 ef021035 Leszek Koltunski
      android.util.Log.e("readNumScrambles", "file "+fileName+" not found: "+ex.getMessage());
786 bf52deb2 Leszek Koltunski
      }
787 ef021035 Leszek Koltunski
    catch(IOException e)
788
      {
789
      android.util.Log.e("readNumScrambles", "Error reading JSON file: "+e.toString());
790
      }
791
    catch( JSONException e )
792
      {
793
      android.util.Log.e("readNumScrambles", "Error parsing JSON file: "+e.toString());
794
      }
795
796
    return 0;
797 bf52deb2 Leszek Koltunski
    }
798
799 052e0362 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
800
801
  public void parseJsonTutorial(InputStream jsonStream)
802
    {
803
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
804
805
    try
806
      {
807
      StringBuilder contents = new StringBuilder();
808
      String tmp;
809
      while( (tmp = br.readLine()) != null) contents.append(tmp);
810
      br.close();
811
      jsonStream.close();
812
813
      JSONObject object = new JSONObject(contents.toString());
814
      int major = object.getInt("major");
815
816
      if( major==1 )
817
        {
818
        parseVersion1Tutorial(object);
819
        }
820
      else
821
        {
822
        android.util.Log.e("readJsonFile", "Unknown tutorial version "+major);
823
        }
824
      }
825
    catch(IOException e)
826
      {
827
      android.util.Log.e("readJsonFile", "Error reading JSON file: "+e.toString());
828
      }
829
    catch( JSONException e )
830
      {
831
      android.util.Log.e("parseJsonFile", "Error parsing JSON file: "+e.toString());
832
      }
833
    }
834
835 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
836
837
  public ScrambleState[] getScrambleStates()
838
    {
839
    return mStates;
840
    }
841
842
///////////////////////////////////////////////////////////////////////////////////////////////////
843
844 19595510 Leszek Koltunski
  public int[][] getSolvedQuats()
845 82eb152a Leszek Koltunski
    {
846 19595510 Leszek Koltunski
    return mSolvedQuats;
847 82eb152a Leszek Koltunski
    }
848
849
///////////////////////////////////////////////////////////////////////////////////////////////////
850
851
  public Static4D[] getQuats()
852
    {
853
    return mQuats;
854
    }
855
856
///////////////////////////////////////////////////////////////////////////////////////////////////
857
858
  public int getSolvedFunctionIndex()
859
    {
860
    return mSolvedFuncIndex;
861
    }
862
863
///////////////////////////////////////////////////////////////////////////////////////////////////
864
865
  public int getNumStickerTypes()
866
    {
867
    return mNumStickerTypes;
868
    }
869
870
///////////////////////////////////////////////////////////////////////////////////////////////////
871
872
  public float[][] getCuts()
873
    {
874
    return mCuts;
875
    }
876
877
///////////////////////////////////////////////////////////////////////////////////////////////////
878
879
  public boolean[][] getLayerRotatable()
880
    {
881
    return mLayerRotatable;
882
    }
883
884
///////////////////////////////////////////////////////////////////////////////////////////////////
885
886
  public int getMovementType()
887
    {
888
    return mMovementType;
889
    }
890
891
///////////////////////////////////////////////////////////////////////////////////////////////////
892
893
  public int getMovementSplit()
894
    {
895
    return mMovementSplit;
896
    }
897
898
///////////////////////////////////////////////////////////////////////////////////////////////////
899
900
  public int[][][] getEnabled()
901
    {
902
    return mEnabled;
903
    }
904
905
///////////////////////////////////////////////////////////////////////////////////////////////////
906
907
  public float[] getDist3D()
908
    {
909
    return mDist3D;
910
    }
911
912 d66e98d7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
913
914
  public int getNumCubitFaces()
915
    {
916
    return mNumCubitFaces;
917
    }
918
919 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
920
921
  public float[][] getCubitPositions()
922
    {
923
    return mPositions;
924
    }
925
926
///////////////////////////////////////////////////////////////////////////////////////////////////
927
928
  public ObjectShape getObjectShape(int variant)
929
    {
930
    return mShapes[variant];
931
    }
932
933 3ee1d662 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
934
935
  public ObjectFaceShape getObjectFaceShape(int variant)
936
    {
937
    return mFaceShapes[variant];
938
    }
939
940 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
941
942 d0e6cf7f Leszek Koltunski
  public Static4D getCubitQuats(int cubit)
943 82eb152a Leszek Koltunski
    {
944
    return mCubitQuats[cubit];
945
    }
946
947
///////////////////////////////////////////////////////////////////////////////////////////////////
948
949
  public int getNumCubitVariants()
950
    {
951
    return mNumCubitVariants;
952
    }
953
954
///////////////////////////////////////////////////////////////////////////////////////////////////
955
956
  public int getCubitVariant(int cubit)
957
    {
958
    return mCubitVariant[cubit];
959
    }
960
961
///////////////////////////////////////////////////////////////////////////////////////////////////
962
963
  public int getVariantFaceColor(int variant, int face)
964
    {
965
    int[] colors = mVariantFaceColor[variant];
966
    return colors.length>face ? colors[face] : -1;
967
    }
968
969
///////////////////////////////////////////////////////////////////////////////////////////////////
970
971 ed0988c0 Leszek Koltunski
  public int getCubitFaceFace(int cubit, int face)
972 82eb152a Leszek Koltunski
    {
973
    return mCubitFaceColor[cubit][face];
974
    }
975
976 e1a86bf2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
977
978
  public int getNumScrambles()
979
    {
980
    return mNumScrambles;
981
    }
982
983 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
984
985
  public ObjectSticker retSticker(int sticker)
986
    {
987
    return mObjectSticker[sticker];
988
    }
989
990
///////////////////////////////////////////////////////////////////////////////////////////////////
991
992
  public Static3D[] getRotationAxis()
993
    {
994
    return mAxis;
995
    }
996
997
///////////////////////////////////////////////////////////////////////////////////////////////////
998
999 beee90ab Leszek Koltunski
  public int[][] getBasicAngle()
1000 82eb152a Leszek Koltunski
    {
1001
    return mBasicAngle;
1002
    }
1003
1004
///////////////////////////////////////////////////////////////////////////////////////////////////
1005
1006 1d581993 Leszek Koltunski
  public ObjectSignature getSignature()
1007 82eb152a Leszek Koltunski
    {
1008 5f54927b Leszek Koltunski
    return mSignature;
1009 82eb152a Leszek Koltunski
    }
1010
1011
///////////////////////////////////////////////////////////////////////////////////////////////////
1012
1013
  public String getObjectName()
1014
    {
1015
    return mLongName;
1016
    }
1017
1018 e1a86bf2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1019
1020
  public String getShortName()
1021
    {
1022
    return mShortName;
1023
    }
1024
1025 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1026
1027
  public String getInventor()
1028
    {
1029
    return mInventor;
1030
    }
1031
1032
///////////////////////////////////////////////////////////////////////////////////////////////////
1033
1034
  public int getYearOfInvention()
1035
    {
1036
    return mYearOfInvention;
1037
    }
1038
1039
///////////////////////////////////////////////////////////////////////////////////////////////////
1040
1041
  public int getComplexity()
1042
    {
1043
    return mComplexity;
1044
    }
1045
1046
///////////////////////////////////////////////////////////////////////////////////////////////////
1047
1048
  public int getNumFaces()
1049
    {
1050
    return mNumFaces;
1051
    }
1052
1053
///////////////////////////////////////////////////////////////////////////////////////////////////
1054
1055
  public int getNumFaceColors()
1056
    {
1057
    return mNumFaceColors;
1058
    }
1059
1060
///////////////////////////////////////////////////////////////////////////////////////////////////
1061
1062
  public int[] getNumLayers()
1063
    {
1064
    return mNumLayers;
1065
    }
1066
1067
///////////////////////////////////////////////////////////////////////////////////////////////////
1068
1069
  public float getSize()
1070
    {
1071
    return mSize;
1072
    }
1073
1074
///////////////////////////////////////////////////////////////////////////////////////////////////
1075
1076
  public int getScrambleType()
1077
    {
1078
    return mScrambleType;
1079
    }
1080
1081
///////////////////////////////////////////////////////////////////////////////////////////////////
1082
1083
  public int getColor(int face)
1084
    {
1085
    return mColor[face];
1086
    }
1087 0f72365b Leszek Koltunski
1088 253e440f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1089
1090
  public int getInternalColor()
1091
    {
1092
    return mInternalColor;
1093
    }
1094
1095 0f72365b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1096
1097
  public boolean shouldResetTextureMaps()
1098
    {
1099
    return mResetMaps;
1100
    }
1101 052e0362 Leszek Koltunski
1102
///////////////////////////////////////////////////////////////////////////////////////////////////
1103
1104
  public String getTutorialObject()
1105
    {
1106
    return mTutorialObject;
1107
    }
1108
1109
///////////////////////////////////////////////////////////////////////////////////////////////////
1110
1111
  public String[][] getTutorials()
1112
    {
1113
    return mTutorials;
1114
    }
1115
1116 82eb152a Leszek Koltunski
}