Project

General

Profile

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

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

1 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6 4c87f159 Leszek Koltunski
// 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 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.objectlib.json;
11
12 6612cbb4 leszek
import static org.distorted.objectlib.objects.TwistyBandagedCuboid.OBJECT_NAME_CUBOID;
13 97a75106 leszek
import static org.distorted.objectlib.objects.TwistyBandagedDiamond.OBJECT_NAME_DIAMOND;
14 3ede7593 leszek
import static org.distorted.objectlib.objects.TwistyBandagedMegaminx.OBJECT_NAME_MEGAMINX;
15 6612cbb4 leszek
import static org.distorted.objectlib.objects.TwistyBandagedPyraminx.OBJECT_NAME_PYRAMINX;
16 30e03516 leszek
import static org.distorted.objectlib.objects.TwistyBandagedSkewb.OBJECT_NAME_SKEWB;
17 64f13076 leszek
import static org.distorted.objectlib.scrambling.ObjectScrambler.SCRAMBLING_ALGORITHMS;
18
import static org.distorted.objectlib.scrambling.ObjectScrambler.SCRAMBLING_SHAPESHIFTER;
19 3d4cd65e leszek
import static org.distorted.objectlib.scrambling.ScrambleStateLocallyBandaged.MAX_SUPPORTED_SIZE;
20 8f44228f Leszek Koltunski
21 82eb152a Leszek Koltunski
import java.io.BufferedReader;
22
import java.io.IOException;
23
import java.io.InputStream;
24
import java.io.InputStreamReader;
25
import java.nio.charset.StandardCharsets;
26
27 3ee1d662 Leszek Koltunski
import org.distorted.objectlib.helpers.ObjectFaceShape;
28 97a75106 leszek
import org.distorted.objectlib.signature.ObjectSignature;
29 3a0a23bf Leszek Koltunski
import org.distorted.objectlib.helpers.ObjectStickerOverride;
30 84a17011 Leszek Koltunski
import org.distorted.objectlib.helpers.ObjectVertexEffects;
31 5618c5a9 Leszek Koltunski
import org.distorted.objectlib.main.TwistyObjectCubit;
32 731280f7 Leszek Koltunski
import org.distorted.objectlib.scrambling.ScrambleEdgeGenerator;
33 97a75106 leszek
import org.distorted.objectlib.signature.ObjectSignatureCuboid;
34
import org.distorted.objectlib.signature.ObjectSignatureDiamond;
35
import org.distorted.objectlib.signature.ObjectSignatureMegaminx;
36
import org.distorted.objectlib.signature.ObjectSignaturePyraminx;
37 30e03516 leszek
import org.distorted.objectlib.signature.ObjectSignatureSkewb;
38 82eb152a Leszek Koltunski
import org.json.JSONArray;
39
import org.json.JSONException;
40
import org.json.JSONObject;
41
42
import org.distorted.library.type.Static3D;
43
import org.distorted.library.type.Static4D;
44
45
import org.distorted.objectlib.helpers.ObjectShape;
46
import org.distorted.objectlib.helpers.ObjectSticker;
47 361fd0de leszek
import org.distorted.objectlib.metadata.ListObjects;
48 82eb152a Leszek Koltunski
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51
public class JsonReader
52
{
53 9ba7f3f6 Leszek Koltunski
  private int[][] mAlgorithms;
54
  private int[][] mEdges;
55 82eb152a Leszek Koltunski
  private int[][] mSolvedQuats;
56
  private Static4D[] mQuats;
57
  private int mSolvedFuncIndex;
58
  private int mNumStickerTypes;
59
  private float[][] mCuts;
60
  private boolean[][] mLayerRotatable;
61 deaf067b Leszek Koltunski
  private float[][] mRotationFactor;
62 82eb152a Leszek Koltunski
  private int mMovementType, mMovementSplit;
63
  private int[][][] mEnabled;
64
  private float[] mDist3D;
65 ade99032 leszek
  private int mNumCubitFaces, mNumFaces;
66 82eb152a Leszek Koltunski
  private float[][] mPositions;
67
  private ObjectShape[] mShapes;
68 3ee1d662 Leszek Koltunski
  private ObjectFaceShape[] mFaceShapes;
69 84a17011 Leszek Koltunski
  private ObjectVertexEffects[] mVertexEffects;
70 82eb152a Leszek Koltunski
  private Static4D[] mCubitQuats;
71
  private int mNumCubitVariants;
72
  private int[] mCubitVariant;
73 51262d81 Leszek Koltunski
  private int[][] mVariantStickerShape, mVariantFaceIsOuter, mCubitFaceColor;
74 82eb152a Leszek Koltunski
  private ObjectSticker[] mObjectSticker;
75
  private Static3D[] mAxis;
76 beee90ab Leszek Koltunski
  private int[][] mBasicAngle;
77 71df2bd4 leszek
  private String mLongName, mShortName, mAuthor;
78 da5551f4 leszek
  private int mYearOfInvention;
79 71df2bd4 leszek
  private float mDifficulty;
80
  private int mCategory;
81 82eb152a Leszek Koltunski
  private int[] mNumLayers;
82
  private float mSize;
83 e1a86bf2 Leszek Koltunski
  private int mScrambleType, mNumScrambles;
84 8b3b1d85 Leszek Koltunski
  private int mPrice;
85 962b8ff6 leszek
  private int[] mColorTable;
86 253e440f Leszek Koltunski
  private int mInternalColor;
87 0f72365b Leszek Koltunski
  private boolean mResetMaps;
88 052e0362 Leszek Koltunski
  private String mTutorialObject;
89
  private String[][] mTutorials;
90 1d581993 Leszek Koltunski
  private ObjectSignature mSignature;
91 7aae846c Leszek Koltunski
  private int[] mCubitType;
92
  private float[][] mCubitRowOffset;
93 3a0a23bf Leszek Koltunski
  private ObjectStickerOverride[] mStickerOverrides;
94 6db8fe2e Leszek Koltunski
  private float mPillowCoeff;
95 63bfcdd1 leszek
  private int[][] mMinimalCubitsInRow;
96 ade99032 leszek
  private boolean mSupportsAdjustableColors;
97 82eb152a Leszek Koltunski
98 8f44228f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
99
100
  public static ObjectSignature produceSignature(String shortName, String longName, long[] signature)
101
    {
102
    // 'BAN*_*' ( or future possible 'BA**_*' ) objects
103
    if( shortName.charAt(0)=='B' && shortName.charAt(1)=='A' && shortName.charAt(4)=='_' )
104
      {
105
      char second = shortName.charAt(2);
106
      char third  = shortName.charAt(3);
107
108
      if( (second=='N' || (second>='0' && second<='9')) && (third>='0' && third<='9') )
109
        {
110 97a75106 leszek
        int s = shortName.charAt(5) - '0';
111
        if( s>=2 && s<=MAX_SUPPORTED_SIZE ) return new ObjectSignatureCuboid(s,s,s,signature);
112 8f44228f Leszek Koltunski
        }
113
      }
114
115 97a75106 leszek
    if( longName.equals(OBJECT_NAME_CUBOID) )
116
      {
117
      int x=shortName.charAt(0)-'0';
118
      int y=shortName.charAt(1)-'0';
119
      int z=shortName.charAt(2)-'0';
120
      return new ObjectSignatureCuboid(x,y,z,signature);
121
      }
122
    if( longName.equals(OBJECT_NAME_PYRAMINX) )
123
      {
124
      int x=shortName.charAt(1)-'0';
125
      return new ObjectSignaturePyraminx(x,signature);
126
      }
127
    if( longName.equals(OBJECT_NAME_MEGAMINX) )
128
      {
129
      int x=shortName.charAt(1)-'0';
130
      return new ObjectSignatureMegaminx(x,signature);
131
      }
132
    if( longName.equals(OBJECT_NAME_DIAMOND) )
133
      {
134
      int x=shortName.charAt(1)-'0';
135
      return new ObjectSignatureDiamond(x,signature);
136
      }
137 30e03516 leszek
    if( longName.equals(OBJECT_NAME_SKEWB) )
138
      {
139
      int x=shortName.charAt(1)-'0';
140
      return new ObjectSignatureSkewb(x,signature);
141
      }
142 8f44228f Leszek Koltunski
143
    return new ObjectSignature(signature);
144
    }
145
146 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
147
148 da5551f4 leszek
  private void parseMetadata(JSONObject metadata, int major) throws JSONException
149 82eb152a Leszek Koltunski
    {
150 84a17011 Leszek Koltunski
    mLongName        = metadata.getString("longname");
151
    mShortName       = metadata.getString("shortname");
152 71df2bd4 leszek
    mAuthor          = metadata.getString("inventor");
153 84a17011 Leszek Koltunski
    mYearOfInvention = metadata.getInt("year");
154
    mSize            = (float)metadata.getDouble("size");
155
    mNumScrambles    = metadata.getInt("scrambles");
156
    mResetMaps       = metadata.getBoolean("resetmaps");
157
    mNumFaces        = metadata.getInt("num_faces");
158 882a8142 Leszek Koltunski
159 da5551f4 leszek
    if( major<=13 )
160
      {
161 71df2bd4 leszek
      mDifficulty = metadata.getInt("complexity");
162
      mCategory   = 0;
163 da5551f4 leszek
      }
164
    else
165
      {
166 71df2bd4 leszek
      mDifficulty = (float)metadata.getDouble("complexity");
167
      mCategory   = metadata.getInt("category");
168 da5551f4 leszek
      }
169
170 882a8142 Leszek Koltunski
    try
171
      {
172 84a17011 Leszek Koltunski
      JSONArray sigArray = metadata.getJSONArray("signature");
173 882a8142 Leszek Koltunski
      int size = sigArray.length();
174
      long[] signature = new long[size];
175
      for(int i=0; i<size; i++) signature[i] = sigArray.getLong(i);
176 8f44228f Leszek Koltunski
      mSignature = produceSignature(mShortName,mLongName,signature);
177 1d581993 Leszek Koltunski
      }
178
    catch(JSONException ex)
179
      {
180 84a17011 Leszek Koltunski
      long signature = metadata.getLong("signature");
181 1d581993 Leszek Koltunski
      mSignature = new ObjectSignature(signature);
182 5f54927b Leszek Koltunski
      }
183 84224c99 Leszek Koltunski
184 84a17011 Leszek Koltunski
    boolean free = metadata.optBoolean("free", true);
185 8b3b1d85 Leszek Koltunski
186
    if( free ) mPrice = 0;
187 361fd0de leszek
    else mPrice = metadata.optInt("price", ListObjects.DEFAULT_PRICE_OF_OLD_OBJECTS );
188 ade99032 leszek
189
    mSupportsAdjustableColors = metadata.optBoolean("adj_colors", true);
190 82eb152a Leszek Koltunski
    }
191
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
194
  private void parseCubits(JSONArray object) throws JSONException
195
    {
196
    int numCubits = object.length();
197
198
    mCubitQuats     = new Static4D[numCubits];
199
    mCubitVariant   = new int[numCubits];
200
    mPositions      = new float[numCubits][];
201
    mCubitFaceColor = new int[numCubits][];
202 7aae846c Leszek Koltunski
    mCubitType      = new int[numCubits];
203
    mCubitRowOffset = new float[numCubits][];
204 82eb152a Leszek Koltunski
205
    for(int i=0; i<numCubits; i++)
206
      {
207
      JSONObject jsonCubit = object.getJSONObject(i);
208
209
      float qx = (float)jsonCubit.getDouble("qx");
210
      float qy = (float)jsonCubit.getDouble("qy");
211
      float qz = (float)jsonCubit.getDouble("qz");
212
      float qw = (float)jsonCubit.getDouble("qw");
213
214
      mCubitQuats[i] = new Static4D(qx,qy,qz,qw);
215
      mCubitVariant[i] = jsonCubit.getInt("variant");
216
217
      JSONArray jsonCenter = jsonCubit.getJSONArray("centers");
218
      int numCenter = jsonCenter.length();
219
      mPositions[i] = new float[numCenter];
220
      for(int j=0; j<numCenter; j++) mPositions[i][j] = (float)jsonCenter.getDouble(j);
221
222
      JSONArray jsonColor  = jsonCubit.getJSONArray("colors");
223
      int numColor = jsonColor.length();
224
      mCubitFaceColor[i] = new int[numColor];
225
      for(int j=0; j<numColor; j++) mCubitFaceColor[i][j] = jsonColor.getInt(j);
226 7aae846c Leszek Koltunski
227 5618c5a9 Leszek Koltunski
      mCubitType[i] = jsonCubit.optInt("type", TwistyObjectCubit.TYPE_NORMAL);
228 7aae846c Leszek Koltunski
      float xoff = (float)jsonCubit.optDouble("offsetX", 0 );
229
      float yoff = (float)jsonCubit.optDouble("offsetY", 0 );
230
      float zoff = (float)jsonCubit.optDouble("offsetZ", 0 );
231
      mCubitRowOffset[i] = new float[] {xoff,yoff,zoff};
232 82eb152a Leszek Koltunski
      }
233
    }
234
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236
237
  private void parseShapes(JSONArray object) throws JSONException
238
    {
239 40e77224 Leszek Koltunski
    mNumCubitVariants     = object.length();
240 51262d81 Leszek Koltunski
    mVariantStickerShape  = new int[mNumCubitVariants][];
241 40e77224 Leszek Koltunski
    mVariantFaceIsOuter   = new int[mNumCubitVariants][];
242
    mShapes               = new ObjectShape[mNumCubitVariants];
243
    mFaceShapes           = new ObjectFaceShape[mNumCubitVariants];
244 84a17011 Leszek Koltunski
    mVertexEffects        = new ObjectVertexEffects[mNumCubitVariants];
245 d66e98d7 Leszek Koltunski
    mNumCubitFaces = -1;
246
247 82eb152a Leszek Koltunski
    for(int i=0; i<mNumCubitVariants; i++)
248
      {
249
      JSONObject jsonShape = object.getJSONObject(i);
250
251 5377b942 Leszek Koltunski
      /////////////////////////////////////////////////////////////////////////
252
      ////// ObjectShape //////////////////////////////////////////////////////
253 82eb152a Leszek Koltunski
      JSONArray jsonVertices= jsonShape.getJSONArray("vertices");
254
      int numVertices = jsonVertices.length();
255 5377b942 Leszek Koltunski
      float[][] verts = new float[numVertices][3];
256 82eb152a Leszek Koltunski
257 5377b942 Leszek Koltunski
      for(int v=0; v<numVertices; v++)
258 82eb152a Leszek Koltunski
        {
259 5377b942 Leszek Koltunski
        JSONObject vert = jsonVertices.getJSONObject(v);
260
        verts[v][0] = (float)vert.getDouble("x");
261
        verts[v][1] = (float)vert.getDouble("y");
262
        verts[v][2] = (float)vert.getDouble("z");
263 82eb152a Leszek Koltunski
        }
264
265
      JSONArray jsonFaces= jsonShape.getJSONArray("faces");
266
      int numFaces = jsonFaces.length();
267 5377b942 Leszek Koltunski
      if( mNumCubitFaces<numFaces ) mNumCubitFaces=numFaces;
268
      boolean multigon = jsonShape.optBoolean("facesMultigon", false);
269
270
      if( !multigon )
271
        {
272
        int[][] vertIndices = new int[numFaces][];
273
274
        for(int f=0; f<numFaces; f++)
275
          {
276
          JSONObject jsonFace = jsonFaces.getJSONObject(f);
277
          JSONArray vertices = jsonFace.getJSONArray("vertexIndices");
278
          int numV = vertices.length();
279
          vertIndices[f] = new int[numV];
280
          for(int k=0; k<numV; k++) vertIndices[f][k] = vertices.getInt(k);
281
          }
282
283
        mShapes[i] = new ObjectShape(verts,vertIndices);
284
        }
285
      else
286
        {
287
        int[][][] vertIndices = new int[numFaces][][];
288
289
        for(int f=0; f<numFaces; f++)
290
          {
291
          JSONObject jsonFace = jsonFaces.getJSONObject(f);
292
          JSONArray vertices = jsonFace.getJSONArray("vertexIndices");
293
          int numC = vertices.length();
294
          vertIndices[f] = new int[numC][];
295
296
          for(int c=0; c<numC; c++)
297
            {
298
            JSONArray components = vertices.getJSONArray(c);
299
            int numV = components.length();
300
            vertIndices[f][c] = new int[numV];
301
            for(int k=0; k<numV; k++) vertIndices[f][c][k] = components.getInt(k);
302
            }
303
          }
304
305
        mShapes[i] = new ObjectShape(verts,vertIndices);
306
        }
307
308
      /////////////////////////////////////////////////////////////////////////
309
      ////// StickerShape & Outer /////////////////////////////////////////////
310 51262d81 Leszek Koltunski
      mVariantStickerShape[i]= new int[numFaces];
311 40e77224 Leszek Koltunski
      mVariantFaceIsOuter[i] = new int[numFaces];
312 5377b942 Leszek Koltunski
      int[] bandIndices = new int[numFaces];
313 d66e98d7 Leszek Koltunski
314 5377b942 Leszek Koltunski
      for(int f=0; f<numFaces; f++)
315 82eb152a Leszek Koltunski
        {
316 5377b942 Leszek Koltunski
        JSONObject jsonFace = jsonFaces.getJSONObject(f);
317
        mVariantStickerShape[i][f]= jsonFace.getInt("sticker");
318
        mVariantFaceIsOuter[i][f] = jsonFace.optInt("isOuter",0);
319
        bandIndices[f] = jsonFace.getInt("bandIndex");
320 82eb152a Leszek Koltunski
        }
321
322 5377b942 Leszek Koltunski
      /////////////////////////////////////////////////////////////////////////
323
      ////// ObjectFaceShape //////////////////////////////////////////////////
324 82eb152a Leszek Koltunski
      JSONArray jsonBands= jsonShape.getJSONArray("bands");
325
      int numBands = jsonBands.length();
326 5377b942 Leszek Koltunski
      float[][] bands = new float[numBands][7];
327 82eb152a Leszek Koltunski
328 5377b942 Leszek Koltunski
      for(int b=0; b<numBands; b++)
329 82eb152a Leszek Koltunski
        {
330 5377b942 Leszek Koltunski
        JSONObject jsonBand = jsonBands.getJSONObject(b);
331
        float[] band = bands[b];
332
333
        band[0] = (float)jsonBand.getDouble("height");
334
        band[1] = (float)jsonBand.getDouble("angle");
335
        band[2] = (float)jsonBand.getDouble("distanceToCenter");
336
        band[3] = (float)jsonBand.getDouble("distanceToFlat");
337
        band[4] = (float)jsonBand.getDouble("numOfBands");
338
        band[5] = (float)jsonBand.getDouble("extraI");
339
        band[6] = (float)jsonBand.getDouble("extraJ");
340 82eb152a Leszek Koltunski
        }
341
342 84a17011 Leszek Koltunski
      JSONObject jsonConvexity = jsonShape.optJSONObject("convexity");
343 5377b942 Leszek Koltunski
      float[] convexity = null;
344 82eb152a Leszek Koltunski
345 84a17011 Leszek Koltunski
      if( jsonConvexity!=null )
346 82eb152a Leszek Koltunski
        {
347 5377b942 Leszek Koltunski
        convexity = new float[3];
348
        convexity[0] = (float)jsonConvexity.getDouble("x");
349
        convexity[1] = (float)jsonConvexity.getDouble("y");
350
        convexity[2] = (float)jsonConvexity.getDouble("z");
351 82eb152a Leszek Koltunski
        }
352
353 5377b942 Leszek Koltunski
      mFaceShapes[i] = new ObjectFaceShape(bands,bandIndices,convexity);
354
355
      /////////////////////////////////////////////////////////////////////////
356
      ////// ObjectVertexEffects //////////////////////////////////////////////
357 84a17011 Leszek Koltunski
      JSONArray jsonEffects = jsonShape.optJSONArray("effects");
358 82eb152a Leszek Koltunski
359 84a17011 Leszek Koltunski
      if( jsonEffects!=null )
360 82eb152a Leszek Koltunski
        {
361 84a17011 Leszek Koltunski
        int numEffects = jsonEffects.length();
362 9e8eb9e4 Leszek Koltunski
363 84a17011 Leszek Koltunski
        String[] name    = new String[numEffects];
364
        float[][] vars   = new float[numEffects][5];
365
        float[][] center = new float[numEffects][3];
366
        float[][] region = new float[numEffects][4];
367
        boolean[] use    = new boolean[numEffects];
368
369
        for(int j=0; j<numEffects; j++)
370 9e8eb9e4 Leszek Koltunski
          {
371 84a17011 Leszek Koltunski
          JSONObject jsonEffect = jsonEffects.getJSONObject(j);
372 82eb152a Leszek Koltunski
373 84a17011 Leszek Koltunski
          name[j] = jsonEffect.getString("name");
374 9e8eb9e4 Leszek Koltunski
375 84a17011 Leszek Koltunski
          vars[j][0] = (float)jsonEffect.getDouble("var0");
376
          vars[j][1] = (float)jsonEffect.getDouble("var1");
377
          vars[j][2] = (float)jsonEffect.getDouble("var2");
378
          vars[j][3] = (float)jsonEffect.getDouble("var3");
379
          vars[j][4] = (float)jsonEffect.getDouble("var4");
380
381
          center[j][0] = (float)jsonEffect.getDouble("center0");
382
          center[j][1] = (float)jsonEffect.getDouble("center1");
383
          center[j][2] = (float)jsonEffect.getDouble("center2");
384
385
          region[j][0] = (float)jsonEffect.getDouble("region0");
386
          region[j][1] = (float)jsonEffect.getDouble("region1");
387
          region[j][2] = (float)jsonEffect.getDouble("region2");
388
          region[j][3] = (float)jsonEffect.getDouble("region3");
389
390
          use[j] = jsonEffect.getBoolean("use");
391
          }
392
393
        mVertexEffects[i] = new ObjectVertexEffects(name,vars,center,region,use);
394 82eb152a Leszek Koltunski
        }
395
      }
396
    }
397
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399
400 ebe8c08e leszek
  private void parseStickers11(JSONArray object) throws JSONException
401 82eb152a Leszek Koltunski
    {
402
    mNumStickerTypes = object.length();
403
    mObjectSticker = new ObjectSticker[mNumStickerTypes];
404
405
    for(int i=0; i<mNumStickerTypes; i++)
406
      {
407
      JSONObject sticker = object.getJSONObject(i);
408
      float stroke = (float)sticker.getDouble("stroke");
409
      JSONArray vertices = sticker.getJSONArray("vertices");
410
      int numVertices = vertices.length();
411
412 ebe8c08e leszek
      float[][][] coords   = new float[1][numVertices][2];
413
      float[][] curvatures = new float[1][numVertices];
414
      float[][] radii      = new float[1][numVertices];
415 e61a158a leszek
      float[][] strokes    = new float[1][numVertices];
416 82eb152a Leszek Koltunski
417
      for(int j=0; j<numVertices; j++)
418
        {
419
        JSONObject vertex = vertices.getJSONObject(j);
420
421 ebe8c08e leszek
        coords[0][j][0]  = (float)vertex.getDouble("x");
422
        coords[0][j][1]  = (float)vertex.getDouble("y");
423
        curvatures[0][j] = (float)vertex.getDouble("angle");
424
        radii[0][j]      = (float)vertex.getDouble("radius");
425 e61a158a leszek
        strokes[0][j]    = stroke;
426 ebe8c08e leszek
        }
427
428 e61a158a leszek
      mObjectSticker[i] = new ObjectSticker(coords,curvatures,radii,strokes);
429 ebe8c08e leszek
      }
430
    }
431
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433
434 e61a158a leszek
  private void parseStickers12to14(JSONArray object) throws JSONException
435 ebe8c08e leszek
    {
436
    mNumStickerTypes = object.length();
437
    mObjectSticker = new ObjectSticker[mNumStickerTypes];
438
439
    for(int i=0; i<mNumStickerTypes; i++)
440
      {
441
      JSONObject sticker = object.getJSONObject(i);
442
      float stroke = (float)sticker.getDouble("stroke");
443
      JSONArray loops = sticker.getJSONArray("loops");
444
      int numLoops = loops.length();
445
446
      float[][][] coords   = new float[numLoops][][];
447
      float[][] curvatures = new float[numLoops][];
448
      float[][] radii      = new float[numLoops][];
449 e61a158a leszek
      float[][] strokes    = new float[numLoops][];
450 ebe8c08e leszek
451
      for(int l=0; l<numLoops; l++)
452
        {
453
        JSONArray loop = loops.getJSONArray(l);
454
        int numVertices= loop.length();
455
456
        coords[l]     = new float[numVertices][2];
457
        curvatures[l] = new float[numVertices];
458
        radii[l]      = new float[numVertices];
459 e61a158a leszek
        strokes[l]    = new float[numVertices];
460 ebe8c08e leszek
461
        for(int v=0; v<numVertices; v++)
462
          {
463
          JSONObject vertex= loop.getJSONObject(v);
464
465
          coords[l][v][0]  = (float)vertex.getDouble("x");
466
          coords[l][v][1]  = (float)vertex.getDouble("y");
467
          curvatures[l][v] = (float)vertex.getDouble("angle");
468
          radii[l][v]      = (float)vertex.getDouble("radius");
469 e61a158a leszek
          strokes[l][v]    = stroke;
470 ebe8c08e leszek
          }
471 82eb152a Leszek Koltunski
        }
472
473 e61a158a leszek
      mObjectSticker[i] = new ObjectSticker(coords,curvatures,radii,strokes);
474
      }
475
    }
476
477
///////////////////////////////////////////////////////////////////////////////////////////////////
478
479
  private void parseStickers15up(JSONArray object) throws JSONException
480
    {
481
    mNumStickerTypes = object.length();
482
    mObjectSticker = new ObjectSticker[mNumStickerTypes];
483
484
    for(int i=0; i<mNumStickerTypes; i++)
485
      {
486
      JSONObject sticker = object.getJSONObject(i);
487
      JSONArray loops = sticker.getJSONArray("loops");
488
      int numLoops = loops.length();
489
490
      float[][][] coords   = new float[numLoops][][];
491
      float[][] curvatures = new float[numLoops][];
492
      float[][] radii      = new float[numLoops][];
493
      float[][] strokes    = new float[numLoops][];
494
495
      for(int l=0; l<numLoops; l++)
496
        {
497
        JSONArray loop = loops.getJSONArray(l);
498
        int numVertices= loop.length();
499
500
        coords[l]     = new float[numVertices][2];
501
        curvatures[l] = new float[numVertices];
502
        radii[l]      = new float[numVertices];
503
        strokes[l]    = new float[numVertices];
504
505
        for(int v=0; v<numVertices; v++)
506
          {
507
          JSONObject vertex= loop.getJSONObject(v);
508
509
          coords[l][v][0]  = (float)vertex.getDouble("x");
510
          coords[l][v][1]  = (float)vertex.getDouble("y");
511
          curvatures[l][v] = (float)vertex.getDouble("angle");
512
          radii[l][v]      = (float)vertex.getDouble("radius");
513
          strokes[l][v]    = (float)vertex.getDouble("stroke");
514
          }
515
        }
516
517
      mObjectSticker[i] = new ObjectSticker(coords,curvatures,radii,strokes);
518 82eb152a Leszek Koltunski
      }
519
    }
520
521 3a0a23bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
522
523
  private void parseOverrides(JSONArray object) throws JSONException
524
    {
525 fd9d4a2c Leszek Koltunski
    if( object!=null )
526
      {
527
      int numOverrides = object.length();
528
      mStickerOverrides= new ObjectStickerOverride[numOverrides];
529 3a0a23bf Leszek Koltunski
530 fd9d4a2c Leszek Koltunski
      for(int i=0; i<numOverrides; i++)
531
        {
532
        JSONObject override  = object.getJSONObject(i);
533
        JSONArray cubitArray = override.getJSONArray("cubitfaces");
534
        int color = override.getInt("color");
535
        int numCubits = cubitArray.length();
536
        int[] cubitface = new int[numCubits];
537
        for(int j=0; j<numCubits; j++) cubitface[j] = cubitArray.getInt(j);
538
        mStickerOverrides[i] = new ObjectStickerOverride(cubitface,color);
539
        }
540
      }
541
    else
542 3a0a23bf Leszek Koltunski
      {
543 fd9d4a2c Leszek Koltunski
      mStickerOverrides = null;
544 3a0a23bf Leszek Koltunski
      }
545
    }
546
547 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
548
549 ebe8c08e leszek
  private void parseMesh(JSONObject object, int major) throws JSONException
550 82eb152a Leszek Koltunski
    {
551
    JSONArray cubits   = object.getJSONArray("cubits");
552
    parseCubits(cubits);
553
    JSONArray shapes   = object.getJSONArray("shapes");
554
    parseShapes(shapes);
555
    JSONArray stickers = object.getJSONArray("stickers");
556 e61a158a leszek
    if( major<=11 )      parseStickers11(stickers);
557
    else if( major<=14 ) parseStickers12to14(stickers);
558
    else                 parseStickers15up(stickers);
559 3a0a23bf Leszek Koltunski
560
    JSONArray overrides= object.optJSONArray("overrides");
561 fd9d4a2c Leszek Koltunski
    parseOverrides(overrides);
562 6db8fe2e Leszek Koltunski
563
    mPillowCoeff = (float)object.optDouble("pillow",1.0);
564 82eb152a Leszek Koltunski
    }
565
566
///////////////////////////////////////////////////////////////////////////////////////////////////
567
568 84a17011 Leszek Koltunski
  private void parseAxis(JSONArray object) throws JSONException
569 beee90ab Leszek Koltunski
    {
570
    int numAxis = object.length();
571
572
    mBasicAngle     = new int[numAxis][];
573
    mAxis           = new Static3D[numAxis];
574
    mCuts           = new float[numAxis][];
575
    mLayerRotatable = new boolean[numAxis][];
576 deaf067b Leszek Koltunski
    mRotationFactor = new float[numAxis][];
577 beee90ab Leszek Koltunski
    mNumLayers      = new int[numAxis];
578
579
    for(int i=0; i<numAxis; i++)
580
      {
581
      JSONObject jsonAx = object.getJSONObject(i);
582 82eb152a Leszek Koltunski
583
      float x = (float)jsonAx.getDouble("x");
584
      float y = (float)jsonAx.getDouble("y");
585
      float z = (float)jsonAx.getDouble("z");
586
587
      mAxis[i] = new Static3D(x,y,z);
588
589 beee90ab Leszek Koltunski
      JSONArray jsonAngles = jsonAx.getJSONArray("basicAngles");
590
      int numAngles = jsonAngles.length();
591
      mBasicAngle[i] = new int[numAngles];
592
      for(int j=0; j<numAngles; j++) mBasicAngle[i][j] = jsonAngles.getInt(j);
593
594 82eb152a Leszek Koltunski
      JSONArray jsonCuts = jsonAx.getJSONArray("cuts");
595
      int numCuts = jsonCuts.length();
596
      mCuts[i] = new float[numCuts];
597
      for(int j=0; j<numCuts; j++) mCuts[i][j] = (float)jsonCuts.getDouble(j);
598
599
      JSONArray jsonRota = jsonAx.getJSONArray("rotatable");
600
      int numRota = jsonRota.length();
601
      mLayerRotatable[i] = new boolean[numRota];
602
      for(int j=0; j<numRota; j++) mLayerRotatable[i][j] = jsonRota.getBoolean(j);
603
604 deaf067b Leszek Koltunski
      JSONArray jsonFactor = jsonAx.optJSONArray("factor");
605
606
      if( jsonFactor!=null )
607
        {
608
        int numFactor = jsonFactor.length();
609
        mRotationFactor[i] = new float[numFactor];
610
        for(int j=0; j<numFactor; j++) mRotationFactor[i][j] = (float)jsonFactor.getDouble(j);
611
        }
612
      else
613
        {
614
        mRotationFactor[i] = new float[numRota];
615
        for(int j=0; j<numRota; j++) mRotationFactor[i][j] = 1.0f;
616
        }
617
618 63bfcdd1 leszek
      JSONArray jsonMinimal = jsonAx.optJSONArray("minimal");
619
620
      if( jsonMinimal!=null )
621
        {
622
        if( mMinimalCubitsInRow==null ) mMinimalCubitsInRow = new int[numAxis][];
623
        int numMinimal = jsonMinimal.length();
624
        mMinimalCubitsInRow[i] = new int[numMinimal];
625
        for(int j=0; j<numMinimal; j++) mMinimalCubitsInRow[i][j] = jsonMinimal.getInt(j);
626
        }
627
628 82eb152a Leszek Koltunski
      mNumLayers[i] = numRota;
629
      }
630
    }
631
632
///////////////////////////////////////////////////////////////////////////////////////////////////
633
634
  private void parseQuats(JSONArray object) throws JSONException
635
    {
636
    int numQuats = object.length();
637
    mQuats = new Static4D[numQuats];
638
639
    for(int i=0; i<numQuats; i++)
640
      {
641
      JSONObject jsonQuat = object.getJSONObject(i);
642
643
      float x = (float)jsonQuat.getDouble("x");
644
      float y = (float)jsonQuat.getDouble("y");
645
      float z = (float)jsonQuat.getDouble("z");
646
      float w = (float)jsonQuat.getDouble("w");
647
648
      mQuats[i] = new Static4D(x,y,z,w);
649
      }
650
    }
651
652
///////////////////////////////////////////////////////////////////////////////////////////////////
653
654 28bfa000 Leszek Koltunski
  private int getAlgorithmIndex(int ax, int layer, int angle)
655 82eb152a Leszek Koltunski
    {
656 28bfa000 Leszek Koltunski
    int numAlgs = mAlgorithms.length;
657 82eb152a Leszek Koltunski
658 28bfa000 Leszek Koltunski
    for(int alg=0; alg<numAlgs; alg++)
659
      {
660 731280f7 Leszek Koltunski
      int[] a = mAlgorithms[alg];
661
      if( a.length>=3 && a[0]==ax && a[1]==layer && a[2]==angle ) return alg;
662 28bfa000 Leszek Koltunski
      }
663
664
    return -1;
665
    }
666
667
///////////////////////////////////////////////////////////////////////////////////////////////////
668
669
  private int[] produceEdge(int[][] scramblingData)
670
    {
671
    int index=0, length=0;
672
    int numAxis = scramblingData.length;
673
    for (int[] scramblingDatum : scramblingData) length += scramblingDatum.length;
674
675
    int[] ret = new int[2*length/3];
676
677
    for(int ax=0; ax<numAxis; ax++)
678 82eb152a Leszek Koltunski
      {
679 28bfa000 Leszek Koltunski
      int[] data = scramblingData[ax];
680
      int num = data.length/3;
681 82eb152a Leszek Koltunski
682 28bfa000 Leszek Koltunski
      for(int j=0; j<num; j++)
683 82eb152a Leszek Koltunski
        {
684 28bfa000 Leszek Koltunski
        int layer = data[3*j];
685
        int angle = data[3*j+1];
686
        int state = data[3*j+2];
687
688
        ret[2*index  ] = getAlgorithmIndex(ax,layer,angle);
689
        ret[2*index+1] = state;
690
        index++;
691 9ba7f3f6 Leszek Koltunski
        }
692 28bfa000 Leszek Koltunski
      }
693 82eb152a Leszek Koltunski
694 28bfa000 Leszek Koltunski
    return ret;
695
    }
696
697
///////////////////////////////////////////////////////////////////////////////////////////////////
698 82eb152a Leszek Koltunski
699 28bfa000 Leszek Koltunski
  private void parseScrambling6(JSONObject object) throws JSONException
700
    {
701
    JSONArray jsonStates = object.getJSONArray("scrambleStates");
702
    int numStates = jsonStates.length();
703
    int[][][] scramblingData = new int[numStates][][];
704
705
    for(int i=0; i<numStates; i++)
706
      {
707
      JSONArray jsonState = jsonStates.getJSONArray(i);
708
      int numAxis = jsonState.length();
709
      scramblingData[i] = new int[numAxis][];
710
711
      for(int j=0; j<numAxis; j++)
712 9ba7f3f6 Leszek Koltunski
        {
713 28bfa000 Leszek Koltunski
        JSONArray jsonData = jsonState.getJSONArray(j);
714
        int numData = jsonData.length();
715
        scramblingData[i][j] = new int[numData];
716
        for(int k=0; k<numData; k++) scramblingData[i][j][k] = jsonData.getInt(k);
717 82eb152a Leszek Koltunski
        }
718
      }
719 28bfa000 Leszek Koltunski
720 731280f7 Leszek Koltunski
    mAlgorithms = ScrambleEdgeGenerator.getScramblingAlgorithms(mBasicAngle);
721 28bfa000 Leszek Koltunski
    mEdges = new int[numStates][];
722
    for(int i=0; i<numStates; i++) mEdges[i] = produceEdge(scramblingData[i]);
723
    }
724
725
///////////////////////////////////////////////////////////////////////////////////////////////////
726
727 464a5f12 Leszek Koltunski
  private void parseScrambling7to10(JSONObject object) throws JSONException
728
    {
729
    JSONArray jsonAlgorithms = object.getJSONArray("algorithms");
730
    int numAlgs = jsonAlgorithms.length();
731
    mAlgorithms = new int[numAlgs][];
732
733
    for(int i=0; i<numAlgs; i++)
734
      {
735
      JSONArray jsonAlg = jsonAlgorithms.getJSONArray(i);
736
      int numEntries = jsonAlg.length();
737
      mAlgorithms[i] = new int[numEntries];
738 e17d01c2 Leszek Koltunski
      for(int j=0; j<numEntries; j++)
739
        {
740
        int entry = jsonAlg.getInt(j);
741
        mAlgorithms[i][j] = ( (j%3)==1 ? (1<<entry) : entry );
742
        }
743 464a5f12 Leszek Koltunski
      }
744
745
    JSONArray jsonEdges = object.getJSONArray("edges");
746
    int numEdges = jsonEdges.length();
747
    mEdges = new int[numEdges][];
748
749
    for(int i=0; i<numEdges; i++)
750
      {
751
      JSONArray jsonEdge = jsonEdges.getJSONArray(i);
752
      int numEntries = jsonEdge.length();
753
      mEdges[i] = new int[numEntries];
754
      for(int j=0; j<numEntries; j++) mEdges[i][j] = jsonEdge.getInt(j);
755
      }
756
    }
757
758
///////////////////////////////////////////////////////////////////////////////////////////////////
759
760
  private void parseScrambling11orMore(JSONObject object) throws JSONException
761 28bfa000 Leszek Koltunski
    {
762
    JSONArray jsonAlgorithms = object.getJSONArray("algorithms");
763
    int numAlgs = jsonAlgorithms.length();
764
    mAlgorithms = new int[numAlgs][];
765
766
    for(int i=0; i<numAlgs; i++)
767
      {
768
      JSONArray jsonAlg = jsonAlgorithms.getJSONArray(i);
769
      int numEntries = jsonAlg.length();
770
      mAlgorithms[i] = new int[numEntries];
771
      for(int j=0; j<numEntries; j++) mAlgorithms[i][j] = jsonAlg.getInt(j);
772
      }
773
774
    JSONArray jsonEdges = object.getJSONArray("edges");
775
    int numEdges = jsonEdges.length();
776
    mEdges = new int[numEdges][];
777
778
    for(int i=0; i<numEdges; i++)
779
      {
780
      JSONArray jsonEdge = jsonEdges.getJSONArray(i);
781
      int numEntries = jsonEdge.length();
782
      mEdges[i] = new int[numEntries];
783
      for(int j=0; j<numEntries; j++) mEdges[i][j] = jsonEdge.getInt(j);
784
      }
785
    }
786
787
///////////////////////////////////////////////////////////////////////////////////////////////////
788
789
  private void parseScrambling(JSONObject object, int major) throws JSONException
790
    {
791
    mScrambleType = object.getInt("scrambleType");
792
793 64f13076 leszek
    if( mScrambleType==SCRAMBLING_ALGORITHMS || mScrambleType==SCRAMBLING_SHAPESHIFTER )
794 28bfa000 Leszek Koltunski
      {
795 464a5f12 Leszek Koltunski
      if( major==6 )       parseScrambling6(object);
796
      else if( major<=10 ) parseScrambling7to10(object);
797
      else                 parseScrambling11orMore(object);
798 28bfa000 Leszek Koltunski
      }
799 82eb152a Leszek Koltunski
    }
800
801
///////////////////////////////////////////////////////////////////////////////////////////////////
802
803
  private void parseTouchcontrol(JSONObject object) throws JSONException
804
    {
805
    mMovementType = object.getInt("movementType");
806
    mMovementSplit= object.getInt("movementSplit");
807
808 82904e62 Leszek Koltunski
    try
809 82eb152a Leszek Koltunski
      {
810 82904e62 Leszek Koltunski
      JSONArray jsonEnabled = object.getJSONArray("enabledAxis");
811
      int numFace = jsonEnabled.length();
812 82eb152a Leszek Koltunski
813 82904e62 Leszek Koltunski
      mEnabled = new int[numFace][][];
814
815
      for(int i=0; i<numFace; i++)
816 82eb152a Leszek Koltunski
        {
817 82904e62 Leszek Koltunski
        JSONArray jsonSection = jsonEnabled.getJSONArray(i);
818
        int numSection = jsonSection.length();
819
        mEnabled[i] = new int[numSection][];
820
821
        for(int j=0; j<numSection; j++)
822
          {
823
          JSONArray jsonAx = jsonSection.getJSONArray(j);
824
          int numAxis = jsonAx.length();
825
          mEnabled[i][j] = new int[numAxis];
826
          for(int k=0; k<numAxis; k++) mEnabled[i][j][k] = jsonAx.getInt(k);
827
          }
828 82eb152a Leszek Koltunski
        }
829
      }
830 82904e62 Leszek Koltunski
    catch( JSONException ex )
831
      {
832
      // ignore, the object does not have to have 'enabledAxis' defined at all.
833
      }
834 82eb152a Leszek Koltunski
835
    try
836
      {
837
      JSONArray jsonDist = object.getJSONArray("dist3D");
838 a72cd106 Leszek Koltunski
      int num = jsonDist.length();
839
      mDist3D = new float[num];
840
      for(int j=0; j<num; j++) mDist3D[j] = (float)jsonDist.getDouble(j);
841 82eb152a Leszek Koltunski
      }
842
    catch( JSONException ex )
843
      {
844 a72cd106 Leszek Koltunski
      // ignore, the object does not have a 'dist3D' which is possible.
845 82eb152a Leszek Koltunski
      }
846
    }
847
848
///////////////////////////////////////////////////////////////////////////////////////////////////
849
850
  private void parseColors(JSONArray object) throws JSONException
851
    {
852 ade99032 leszek
    int num = object.length()-1;
853
    mColorTable = new int[num];
854
    for(int i=0; i<num; i++) mColorTable[i] = object.getInt(i);
855
    mInternalColor = object.getInt(num);
856 82eb152a Leszek Koltunski
    }
857
858 3c48fab9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
859
860
  private void parseSolved(JSONObject solved) throws JSONException
861
    {
862
    mSolvedFuncIndex = solved.getInt("functionIndex");
863
864
    try
865
      {
866
      JSONArray groupArray= solved.getJSONArray("groups");
867
      int numGroups = groupArray.length();
868
      mSolvedQuats  = new int[numGroups][];
869
870
      for(int i=0; i<numGroups; i++)
871
        {
872
        JSONArray groupElements = groupArray.getJSONArray(i);
873
        int groupSize = groupElements.length();
874
        mSolvedQuats[i] = new int[groupSize];
875
        for(int j=0; j<groupSize; j++) mSolvedQuats[i][j] = groupElements.getInt(j);
876
        }
877
      }
878
    catch( JSONException ex )
879
      {
880
      // ignore, the object does not have to have an array of solved groups.
881
      }
882
    }
883
884 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
885
886 28bfa000 Leszek Koltunski
  private void parseFile(JSONObject object, int major) throws JSONException
887 beee90ab Leszek Koltunski
    {
888
    JSONObject metadata    = object.getJSONObject("metadata");
889 da5551f4 leszek
    parseMetadata(metadata,major);
890 beee90ab Leszek Koltunski
    JSONObject mesh        = object.getJSONObject("mesh");
891 ebe8c08e leszek
    parseMesh(mesh,major);
892 beee90ab Leszek Koltunski
    JSONArray axis         = object.getJSONArray("axis");
893 84a17011 Leszek Koltunski
    parseAxis(axis);
894 882a8142 Leszek Koltunski
    JSONArray quats        = object.getJSONArray("quats");
895
    parseQuats(quats);
896
    JSONObject scrambling  = object.getJSONObject("scrambling");
897 28bfa000 Leszek Koltunski
    parseScrambling(scrambling,major);
898 882a8142 Leszek Koltunski
    JSONObject touchcontrol= object.getJSONObject("touchcontrol");
899
    parseTouchcontrol(touchcontrol);
900
    JSONArray colors       = object.getJSONArray("colors");
901
    parseColors(colors);
902
    JSONObject solved      = object.getJSONObject("solved");
903
    parseSolved(solved);
904
    }
905
906
///////////////////////////////////////////////////////////////////////////////////////////////////
907
908 84a17011 Leszek Koltunski
  private void parseTutorial(JSONObject object) throws JSONException
909 052e0362 Leszek Koltunski
    {
910
    mTutorialObject = object.getString("object");
911
    JSONArray tuts= object.getJSONArray("tutorials");
912
913
    int len = tuts.length();
914
    mTutorials = new String[len][4];
915
916
    for(int i=0; i<len; i++)
917
      {
918
      JSONObject tut = tuts.getJSONObject(i);
919
      mTutorials[i][0] = tut.getString("language");
920
      mTutorials[i][1] = tut.getString("link");
921
      mTutorials[i][2] = tut.getString("title");
922
      mTutorials[i][3] = tut.getString("author");
923
      }
924
    }
925
926 b39f8e39 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
927 84a17011 Leszek Koltunski
// PUBLIC
928 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
929
930 3ce95490 Leszek Koltunski
  public void parseJsonFile(InputStream jsonStream) throws JSONException, IOException
931 82eb152a Leszek Koltunski
    {
932
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
933 3ce95490 Leszek Koltunski
    StringBuilder contents = new StringBuilder();
934 f8a992a9 Leszek Koltunski
    String line;
935 82eb152a Leszek Koltunski
936 f8a992a9 Leszek Koltunski
    while( (line = br.readLine()) != null) contents.append(line);
937 3ce95490 Leszek Koltunski
    br.close();
938
    jsonStream.close();
939 f8a992a9 Leszek Koltunski
    String contentsString = contents.toString();
940
    JSONObject object = new JSONObject(contentsString);
941 3ce95490 Leszek Koltunski
    int major = object.getInt("major");
942 82eb152a Leszek Koltunski
943 27a44b5e Leszek Koltunski
    if( major>=6 )
944 882a8142 Leszek Koltunski
      {
945 28bfa000 Leszek Koltunski
      parseFile(object,major);
946 882a8142 Leszek Koltunski
      }
947 3ce95490 Leszek Koltunski
    else
948 82eb152a Leszek Koltunski
      {
949 3ce95490 Leszek Koltunski
      android.util.Log.e("readJsonFile", "Unknown version "+major);
950 82eb152a Leszek Koltunski
      }
951
    }
952
953 b39f8e39 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
954
955 3ce95490 Leszek Koltunski
  public void parseJsonFileMetadata(InputStream jsonStream) throws JSONException, IOException
956 b39f8e39 Leszek Koltunski
    {
957
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
958 3ce95490 Leszek Koltunski
    StringBuilder contents = new StringBuilder();
959
    String tmp;
960 b39f8e39 Leszek Koltunski
961 3ce95490 Leszek Koltunski
    while( (tmp = br.readLine()) != null) contents.append(tmp);
962
    br.close();
963
    jsonStream.close();
964 b39f8e39 Leszek Koltunski
965 3ce95490 Leszek Koltunski
    JSONObject object = new JSONObject(contents.toString());
966
    int major = object.getInt("major");
967 b39f8e39 Leszek Koltunski
968 27a44b5e Leszek Koltunski
    if( major>=6 )
969 882a8142 Leszek Koltunski
      {
970 80fd07aa Leszek Koltunski
      JSONObject metadata = object.getJSONObject("metadata");
971 da5551f4 leszek
      parseMetadata(metadata,major);
972 882a8142 Leszek Koltunski
      }
973 3ce95490 Leszek Koltunski
    else
974 b39f8e39 Leszek Koltunski
      {
975 3ce95490 Leszek Koltunski
      android.util.Log.e("readJsonFileQuick", "Unknown version "+major);
976 b39f8e39 Leszek Koltunski
      }
977
    }
978
979 052e0362 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
980
981 3ce95490 Leszek Koltunski
  public void parseJsonTutorial(InputStream jsonStream) throws IOException, JSONException
982 052e0362 Leszek Koltunski
    {
983
    BufferedReader br = new BufferedReader(new InputStreamReader(jsonStream, StandardCharsets.UTF_8));
984 3ce95490 Leszek Koltunski
    StringBuilder contents = new StringBuilder();
985
    String tmp;
986 052e0362 Leszek Koltunski
987 3ce95490 Leszek Koltunski
    while( (tmp = br.readLine()) != null) contents.append(tmp);
988
    br.close();
989
    jsonStream.close();
990 052e0362 Leszek Koltunski
991 3ce95490 Leszek Koltunski
    JSONObject object = new JSONObject(contents.toString());
992
    int major = object.getInt("major");
993 052e0362 Leszek Koltunski
994 27a44b5e Leszek Koltunski
    if( major==1 )
995 052e0362 Leszek Koltunski
      {
996 84a17011 Leszek Koltunski
      parseTutorial(object);
997 052e0362 Leszek Koltunski
      }
998 3ce95490 Leszek Koltunski
    else
999 052e0362 Leszek Koltunski
      {
1000 3ce95490 Leszek Koltunski
      android.util.Log.e("readJsonFile", "Unknown tutorial version "+major);
1001 052e0362 Leszek Koltunski
      }
1002
    }
1003
1004 63bfcdd1 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
1005
1006
  public int[][] getMinimalCubiesInRow()
1007
    {
1008
    return mMinimalCubitsInRow;
1009
    }
1010
1011 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1012
1013 9ba7f3f6 Leszek Koltunski
  public int getScrambleType()
1014
    {
1015
    return mScrambleType;
1016
    }
1017
1018
///////////////////////////////////////////////////////////////////////////////////////////////////
1019
1020
  public int[][] getScrambleEdges()
1021 82eb152a Leszek Koltunski
    {
1022 9ba7f3f6 Leszek Koltunski
    return mEdges;
1023
    }
1024
1025
///////////////////////////////////////////////////////////////////////////////////////////////////
1026
1027
  public int[][] getScrambleAlgorithms()
1028
    {
1029
    return mAlgorithms;
1030 82eb152a Leszek Koltunski
    }
1031
1032
///////////////////////////////////////////////////////////////////////////////////////////////////
1033
1034 19595510 Leszek Koltunski
  public int[][] getSolvedQuats()
1035 82eb152a Leszek Koltunski
    {
1036 19595510 Leszek Koltunski
    return mSolvedQuats;
1037 82eb152a Leszek Koltunski
    }
1038
1039
///////////////////////////////////////////////////////////////////////////////////////////////////
1040
1041
  public Static4D[] getQuats()
1042
    {
1043
    return mQuats;
1044
    }
1045
1046
///////////////////////////////////////////////////////////////////////////////////////////////////
1047
1048
  public int getSolvedFunctionIndex()
1049
    {
1050
    return mSolvedFuncIndex;
1051
    }
1052
1053
///////////////////////////////////////////////////////////////////////////////////////////////////
1054
1055
  public int getNumStickerTypes()
1056
    {
1057
    return mNumStickerTypes;
1058
    }
1059
1060
///////////////////////////////////////////////////////////////////////////////////////////////////
1061
1062
  public float[][] getCuts()
1063
    {
1064
    return mCuts;
1065
    }
1066
1067
///////////////////////////////////////////////////////////////////////////////////////////////////
1068
1069
  public boolean[][] getLayerRotatable()
1070
    {
1071
    return mLayerRotatable;
1072
    }
1073
1074
///////////////////////////////////////////////////////////////////////////////////////////////////
1075
1076
  public int getMovementType()
1077
    {
1078
    return mMovementType;
1079
    }
1080
1081
///////////////////////////////////////////////////////////////////////////////////////////////////
1082
1083
  public int getMovementSplit()
1084
    {
1085
    return mMovementSplit;
1086
    }
1087
1088
///////////////////////////////////////////////////////////////////////////////////////////////////
1089
1090
  public int[][][] getEnabled()
1091
    {
1092
    return mEnabled;
1093
    }
1094
1095
///////////////////////////////////////////////////////////////////////////////////////////////////
1096
1097
  public float[] getDist3D()
1098
    {
1099
    return mDist3D;
1100
    }
1101
1102 d66e98d7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1103
1104
  public int getNumCubitFaces()
1105
    {
1106
    return mNumCubitFaces;
1107
    }
1108
1109 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1110
1111
  public float[][] getCubitPositions()
1112
    {
1113
    return mPositions;
1114
    }
1115
1116
///////////////////////////////////////////////////////////////////////////////////////////////////
1117
1118
  public ObjectShape getObjectShape(int variant)
1119
    {
1120
    return mShapes[variant];
1121
    }
1122
1123 3ee1d662 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1124
1125
  public ObjectFaceShape getObjectFaceShape(int variant)
1126
    {
1127
    return mFaceShapes[variant];
1128
    }
1129
1130 84a17011 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1131
1132
  public ObjectVertexEffects getVertexEffects(int variant)
1133
    {
1134
    return mVertexEffects[variant];
1135
    }
1136
1137 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1138
1139 d0e6cf7f Leszek Koltunski
  public Static4D getCubitQuats(int cubit)
1140 82eb152a Leszek Koltunski
    {
1141
    return mCubitQuats[cubit];
1142
    }
1143
1144
///////////////////////////////////////////////////////////////////////////////////////////////////
1145
1146
  public int getNumCubitVariants()
1147
    {
1148
    return mNumCubitVariants;
1149
    }
1150
1151
///////////////////////////////////////////////////////////////////////////////////////////////////
1152
1153
  public int getCubitVariant(int cubit)
1154
    {
1155
    return mCubitVariant[cubit];
1156
    }
1157
1158
///////////////////////////////////////////////////////////////////////////////////////////////////
1159
1160 51262d81 Leszek Koltunski
  public int getVariantStickerShape(int variant, int face)
1161 82eb152a Leszek Koltunski
    {
1162 51262d81 Leszek Koltunski
    int[] shapes = mVariantStickerShape[variant];
1163
    return shapes.length>face ? shapes[face] : -1;
1164 82eb152a Leszek Koltunski
    }
1165
1166 deaf067b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1167
1168
  public float[][] returnRotationFactor()
1169
    {
1170
    return mRotationFactor;
1171
    }
1172
1173 7aae846c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1174
1175
  public int[] getCubitTypes()
1176
    {
1177
    return mCubitType;
1178
    }
1179
1180
///////////////////////////////////////////////////////////////////////////////////////////////////
1181
1182
  public float[][] getCubitOffsets()
1183
    {
1184
    return mCubitRowOffset;
1185
    }
1186
1187 40e77224 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1188
1189
  public int[][] getVariantFaceIsOuter()
1190
    {
1191
    return mVariantFaceIsOuter;
1192
    }
1193
1194 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1195
1196 ed0988c0 Leszek Koltunski
  public int getCubitFaceFace(int cubit, int face)
1197 82eb152a Leszek Koltunski
    {
1198
    return mCubitFaceColor[cubit][face];
1199
    }
1200
1201 e1a86bf2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1202
1203
  public int getNumScrambles()
1204
    {
1205
    return mNumScrambles;
1206
    }
1207
1208 84224c99 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1209
1210 8b3b1d85 Leszek Koltunski
  public int getPrice()
1211 84224c99 Leszek Koltunski
    {
1212 8b3b1d85 Leszek Koltunski
    return mPrice;
1213 84224c99 Leszek Koltunski
    }
1214
1215 ade99032 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
1216
1217
  public boolean getAdjustableColors()
1218
    {
1219
    return mSupportsAdjustableColors;
1220
    }
1221
1222 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1223
1224
  public ObjectSticker retSticker(int sticker)
1225
    {
1226
    return mObjectSticker[sticker];
1227
    }
1228 3a0a23bf Leszek Koltunski
1229
///////////////////////////////////////////////////////////////////////////////////////////////////
1230
1231
  public ObjectStickerOverride[] getStickerOverrides()
1232
    {
1233
    return mStickerOverrides;
1234
    }
1235 82eb152a Leszek Koltunski
1236 6db8fe2e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1237
1238
  public float getPillowCoeff()
1239
    {
1240
    return mPillowCoeff;
1241
    }
1242
1243 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1244
1245
  public Static3D[] getRotationAxis()
1246
    {
1247
    return mAxis;
1248
    }
1249
1250
///////////////////////////////////////////////////////////////////////////////////////////////////
1251
1252 beee90ab Leszek Koltunski
  public int[][] getBasicAngle()
1253 82eb152a Leszek Koltunski
    {
1254
    return mBasicAngle;
1255
    }
1256
1257
///////////////////////////////////////////////////////////////////////////////////////////////////
1258
1259 1d581993 Leszek Koltunski
  public ObjectSignature getSignature()
1260 82eb152a Leszek Koltunski
    {
1261 5f54927b Leszek Koltunski
    return mSignature;
1262 82eb152a Leszek Koltunski
    }
1263
1264
///////////////////////////////////////////////////////////////////////////////////////////////////
1265
1266
  public String getObjectName()
1267
    {
1268
    return mLongName;
1269
    }
1270
1271 e1a86bf2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1272
1273
  public String getShortName()
1274
    {
1275
    return mShortName;
1276
    }
1277
1278 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1279
1280 71df2bd4 leszek
  public String getAuthor()
1281 82eb152a Leszek Koltunski
    {
1282 71df2bd4 leszek
    return mAuthor;
1283 82eb152a Leszek Koltunski
    }
1284
1285
///////////////////////////////////////////////////////////////////////////////////////////////////
1286
1287
  public int getYearOfInvention()
1288
    {
1289
    return mYearOfInvention;
1290
    }
1291
1292
///////////////////////////////////////////////////////////////////////////////////////////////////
1293
1294 71df2bd4 leszek
  public float getDifficulty()
1295
    {
1296
    return mDifficulty;
1297
    }
1298
1299
///////////////////////////////////////////////////////////////////////////////////////////////////
1300
1301
  public int getCategory()
1302 82eb152a Leszek Koltunski
    {
1303 71df2bd4 leszek
    return mCategory;
1304 82eb152a Leszek Koltunski
    }
1305
1306
///////////////////////////////////////////////////////////////////////////////////////////////////
1307
1308
  public int getNumFaces()
1309
    {
1310
    return mNumFaces;
1311
    }
1312
1313
///////////////////////////////////////////////////////////////////////////////////////////////////
1314
1315
  public int[] getNumLayers()
1316
    {
1317
    return mNumLayers;
1318
    }
1319
1320
///////////////////////////////////////////////////////////////////////////////////////////////////
1321
1322
  public float getSize()
1323
    {
1324
    return mSize;
1325
    }
1326
1327
///////////////////////////////////////////////////////////////////////////////////////////////////
1328
1329 962b8ff6 leszek
  public int[] getColorTable()
1330 82eb152a Leszek Koltunski
    {
1331 962b8ff6 leszek
    return mColorTable;
1332 82eb152a Leszek Koltunski
    }
1333 0f72365b Leszek Koltunski
1334 253e440f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1335
1336
  public int getInternalColor()
1337
    {
1338
    return mInternalColor;
1339
    }
1340
1341 0f72365b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1342
1343
  public boolean shouldResetTextureMaps()
1344
    {
1345
    return mResetMaps;
1346
    }
1347 052e0362 Leszek Koltunski
1348
///////////////////////////////////////////////////////////////////////////////////////////////////
1349
1350
  public String getTutorialObject()
1351
    {
1352
    return mTutorialObject;
1353
    }
1354
1355
///////////////////////////////////////////////////////////////////////////////////////////////////
1356
1357
  public String[][] getTutorials()
1358
    {
1359
    return mTutorials;
1360
    }
1361 82eb152a Leszek Koltunski
}