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