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.BufferedWriter;
|
13
|
import java.io.FileOutputStream;
|
14
|
import java.io.IOException;
|
15
|
import java.io.OutputStreamWriter;
|
16
|
import java.nio.charset.StandardCharsets;
|
17
|
|
18
|
import org.distorted.objectlib.helpers.ObjectFaceShape;
|
19
|
import org.distorted.objectlib.helpers.ObjectStickerOverride;
|
20
|
import org.distorted.objectlib.helpers.ObjectVertexEffects;
|
21
|
import org.distorted.objectlib.metadata.Metadata;
|
22
|
import org.json.JSONArray;
|
23
|
import org.json.JSONException;
|
24
|
import org.json.JSONObject;
|
25
|
|
26
|
import org.distorted.library.type.Static3D;
|
27
|
import org.distorted.library.type.Static4D;
|
28
|
|
29
|
import org.distorted.objectlib.helpers.ObjectShape;
|
30
|
import org.distorted.objectlib.helpers.ObjectSticker;
|
31
|
import org.distorted.objectlib.main.TwistyObject;
|
32
|
|
33
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
34
|
|
35
|
public class JsonWriter
|
36
|
{
|
37
|
public static final int VERSION_OBJECT_APP = 15;
|
38
|
public static final int VERSION_EXTRAS_APP = 1;
|
39
|
|
40
|
private static JsonWriter mThis;
|
41
|
private static int mNumCubitFaces;
|
42
|
|
43
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
44
|
|
45
|
private JsonWriter()
|
46
|
{
|
47
|
|
48
|
}
|
49
|
|
50
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
51
|
|
52
|
private JSONArray generateCubits(TwistyObject object) throws JSONException
|
53
|
{
|
54
|
JSONArray array = new JSONArray();
|
55
|
|
56
|
int[] numLayers = object.getNumLayers();
|
57
|
float[][] positions = object.getCubitPositions(numLayers);
|
58
|
int numCubits = positions.length;
|
59
|
|
60
|
for(int c=0; c<numCubits; c++)
|
61
|
{
|
62
|
JSONObject cubit = new JSONObject();
|
63
|
Static4D rotQuat = object.getCubitQuats(c,numLayers);
|
64
|
int variant = object.getCubitVariant(c,numLayers);
|
65
|
int type = object.getCubitType(c);
|
66
|
float[] offset = object.getCubitOffset(c);
|
67
|
|
68
|
JSONArray pos = new JSONArray();
|
69
|
int numPos = positions[c].length;
|
70
|
for(int j=0; j<numPos; j++) pos.put(positions[c][j]);
|
71
|
cubit.put("centers", pos);
|
72
|
cubit.put("qx", rotQuat.get0() );
|
73
|
cubit.put("qy", rotQuat.get1() );
|
74
|
cubit.put("qz", rotQuat.get2() );
|
75
|
cubit.put("qw", rotQuat.get3() );
|
76
|
cubit.put("variant", variant );
|
77
|
cubit.put("type", type);
|
78
|
|
79
|
if( offset!=null )
|
80
|
{
|
81
|
cubit.put("offsetX", offset[0]);
|
82
|
cubit.put("offsetY", offset[1]);
|
83
|
cubit.put("offsetZ", offset[2]);
|
84
|
}
|
85
|
|
86
|
JSONArray colors = new JSONArray();
|
87
|
|
88
|
for(int f=0; f<mNumCubitFaces; f++)
|
89
|
{
|
90
|
int cubColor = object.getCubitFaceMap(c,f);
|
91
|
colors.put(cubColor);
|
92
|
}
|
93
|
cubit.put("colors",colors);
|
94
|
|
95
|
array.put(cubit);
|
96
|
}
|
97
|
|
98
|
return array;
|
99
|
}
|
100
|
|
101
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
102
|
|
103
|
private JSONArray generateBands(float[][] bands) throws JSONException
|
104
|
{
|
105
|
JSONArray array = new JSONArray();
|
106
|
|
107
|
for (float[] b : bands)
|
108
|
{
|
109
|
JSONObject band = new JSONObject();
|
110
|
band.put("height" , b[0]);
|
111
|
band.put("angle" , b[1]);
|
112
|
band.put("distanceToCenter", b[2]);
|
113
|
band.put("distanceToFlat" , b[3]);
|
114
|
band.put("numOfBands" , b[4]);
|
115
|
band.put("extraI" , b[5]);
|
116
|
band.put("extraJ" , b[6]);
|
117
|
array.put(band);
|
118
|
}
|
119
|
|
120
|
return array;
|
121
|
}
|
122
|
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
|
125
|
private JSONArray generateFaces(TwistyObject object, ObjectShape shape, int[] bandIndices, int[] faceOuter, int cubit, int[] numLayers) throws JSONException
|
126
|
{
|
127
|
JSONArray array = new JSONArray();
|
128
|
int numFaces = shape.getNumFaces();
|
129
|
int variant = object.getCubitVariant(cubit,numLayers);
|
130
|
boolean multigon = shape.isMultigon();
|
131
|
int[][] vertIndices1 = shape.getVertIndices();
|
132
|
int[][][] vertIndices2 = shape.getMultigonIndices();
|
133
|
|
134
|
for(int i=0; i<numFaces; i++)
|
135
|
{
|
136
|
JSONObject face = new JSONObject();
|
137
|
face.put("bandIndex", bandIndices[i]);
|
138
|
|
139
|
int stiShape = object.getVariantStickerShape(variant,i);
|
140
|
face.put("sticker", stiShape);
|
141
|
face.put("isOuter", faceOuter[i]);
|
142
|
|
143
|
JSONArray faceArr = new JSONArray();
|
144
|
|
145
|
if( !multigon )
|
146
|
{
|
147
|
int num = vertIndices1[i].length;
|
148
|
for(int j=0; j<num; j++) faceArr.put(vertIndices1[i][j]);
|
149
|
}
|
150
|
else
|
151
|
{
|
152
|
int num = vertIndices2[i].length;
|
153
|
for(int j=0; j<num; j++)
|
154
|
{
|
155
|
JSONArray compArr = new JSONArray();
|
156
|
int com = vertIndices2[i][j].length;
|
157
|
for(int k=0; k<com; k++) compArr.put(vertIndices2[i][j][k]);
|
158
|
faceArr.put(compArr);
|
159
|
}
|
160
|
}
|
161
|
|
162
|
face.put("vertexIndices",faceArr);
|
163
|
array.put(face);
|
164
|
}
|
165
|
|
166
|
return array;
|
167
|
}
|
168
|
|
169
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
170
|
|
171
|
private JSONArray generateVertices(float[][] vertices) throws JSONException
|
172
|
{
|
173
|
JSONArray array = new JSONArray();
|
174
|
|
175
|
for (float[] vertex : vertices)
|
176
|
{
|
177
|
JSONObject vert = new JSONObject();
|
178
|
vert.put("x", vertex[0]);
|
179
|
vert.put("y", vertex[1]);
|
180
|
vert.put("z", vertex[2]);
|
181
|
array.put(vert);
|
182
|
}
|
183
|
|
184
|
return array;
|
185
|
}
|
186
|
|
187
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
188
|
|
189
|
private JSONObject generateConvexity(float[] convexity) throws JSONException
|
190
|
{
|
191
|
JSONObject object = new JSONObject();
|
192
|
object.put("x", convexity[0]);
|
193
|
object.put("y", convexity[1]);
|
194
|
object.put("z", convexity[2]);
|
195
|
return object;
|
196
|
}
|
197
|
|
198
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
199
|
|
200
|
private JSONObject generateEffect(String name, float[] var, float[] center, float[] region, boolean use) throws JSONException
|
201
|
{
|
202
|
JSONObject object = new JSONObject();
|
203
|
|
204
|
object.put("name",name);
|
205
|
|
206
|
object.put("var0", var[0]);
|
207
|
object.put("var1", var[1]);
|
208
|
object.put("var2", var[2]);
|
209
|
object.put("var3", var[3]);
|
210
|
object.put("var4", var[4]);
|
211
|
|
212
|
object.put("center0", center[0]);
|
213
|
object.put("center1", center[1]);
|
214
|
object.put("center2", center[2]);
|
215
|
|
216
|
object.put("region0", region[0]);
|
217
|
object.put("region1", region[1]);
|
218
|
object.put("region2", region[2]);
|
219
|
object.put("region3", region[3]);
|
220
|
|
221
|
object.put("use", use);
|
222
|
|
223
|
return object;
|
224
|
}
|
225
|
|
226
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
227
|
|
228
|
private JSONArray generateEffects(String[] names, float[][] vars, float[][] centers, float[][] regions, boolean[] uses) throws JSONException
|
229
|
{
|
230
|
int numEffects = names==null ? 0 : names.length;
|
231
|
|
232
|
if( numEffects>0 )
|
233
|
{
|
234
|
JSONArray array = new JSONArray();
|
235
|
|
236
|
for(int i=0; i<numEffects; i++)
|
237
|
{
|
238
|
if( names[i]!=null )
|
239
|
{
|
240
|
JSONObject object = generateEffect(names[i],vars[i],centers[i],regions[i],uses[i]);
|
241
|
array.put(object);
|
242
|
}
|
243
|
}
|
244
|
|
245
|
return array;
|
246
|
}
|
247
|
|
248
|
return null;
|
249
|
}
|
250
|
|
251
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
252
|
|
253
|
private int findCubitWithVariant(TwistyObject object, int variant, int numCubits, int[] numLayers)
|
254
|
{
|
255
|
for(int i=0; i<numCubits; i++)
|
256
|
{
|
257
|
if( object.getCubitVariant(i,numLayers)==variant ) return i;
|
258
|
}
|
259
|
|
260
|
return -1;
|
261
|
}
|
262
|
|
263
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
264
|
|
265
|
private JSONArray generateShapes(TwistyObject object) throws JSONException
|
266
|
{
|
267
|
JSONArray shapes = new JSONArray();
|
268
|
|
269
|
int[] numLayers = object.getNumLayers();
|
270
|
int numVariants = object.getNumCubitVariants(numLayers);
|
271
|
float[][] positions = object.getCubitPositions(numLayers);
|
272
|
int[][] faceOuter = object.getVariantFaceIsOuter();
|
273
|
int numCubits = positions.length;
|
274
|
mNumCubitFaces = 0;
|
275
|
|
276
|
for(int i=0; i<numVariants; i++)
|
277
|
{
|
278
|
JSONObject shapeObj = new JSONObject();
|
279
|
|
280
|
ObjectShape shape = object.getObjectShape(i);
|
281
|
ObjectFaceShape face = object.getObjectFaceShape(i);
|
282
|
ObjectVertexEffects effects = object.getVertexEffects(i);
|
283
|
|
284
|
float[] convexity = face.getConvexityCenter();
|
285
|
float[][] vertices = shape.getVertices();
|
286
|
float[][] bands = face.getBands();
|
287
|
int[] bandIndices = face.getBandIndices();
|
288
|
|
289
|
int num = shape.getNumFaces();
|
290
|
if( num>mNumCubitFaces ) mNumCubitFaces=num;
|
291
|
|
292
|
int cubit = findCubitWithVariant(object,i,numCubits,numLayers);
|
293
|
|
294
|
if( convexity!=null )
|
295
|
{
|
296
|
JSONObject convObj = generateConvexity(convexity);
|
297
|
shapeObj.put("convexity", convObj);
|
298
|
}
|
299
|
|
300
|
if( shape.isMultigon() ) shapeObj.put("facesMultigon", true);
|
301
|
|
302
|
JSONArray verticesArr = generateVertices(vertices);
|
303
|
shapeObj.put("vertices", verticesArr);
|
304
|
JSONArray facesArr = generateFaces(object,shape,bandIndices,faceOuter[i],cubit,numLayers);
|
305
|
shapeObj.put("faces", facesArr);
|
306
|
JSONArray bandsArr = generateBands(bands);
|
307
|
shapeObj.put("bands", bandsArr);
|
308
|
|
309
|
if( effects!=null )
|
310
|
{
|
311
|
String[] effNames = effects.getNames();
|
312
|
float[][] effVars = effects.getVariables();
|
313
|
float[][] effCent = effects.getCenters();
|
314
|
float[][] effRegi = effects.getRegions();
|
315
|
boolean[] effUses = effects.getUses();
|
316
|
|
317
|
JSONArray effectsArr = generateEffects(effNames,effVars,effCent,effRegi,effUses);
|
318
|
if( effectsArr!=null ) shapeObj.put("effects", effectsArr);
|
319
|
}
|
320
|
|
321
|
shapes.put(shapeObj);
|
322
|
}
|
323
|
|
324
|
return shapes;
|
325
|
}
|
326
|
|
327
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
328
|
|
329
|
private JSONArray generateStickers(TwistyObject object) throws JSONException
|
330
|
{
|
331
|
JSONArray stickers = new JSONArray();
|
332
|
|
333
|
int numStickers = object.getNumStickerTypes();
|
334
|
|
335
|
for(int s=0; s<numStickers; s++)
|
336
|
{
|
337
|
JSONObject stickerObj= new JSONObject();
|
338
|
JSONArray loops = new JSONArray();
|
339
|
ObjectSticker sticker= object.retSticker(s);
|
340
|
float[][][] coords = sticker.getCoords();
|
341
|
float[][] curvatures = sticker.getCurvature();
|
342
|
float[][] radii = sticker.getRadii();
|
343
|
float[][] stroke = sticker.getStrokes();
|
344
|
|
345
|
int numLoops = coords.length;
|
346
|
|
347
|
for(int l=0; l<numLoops; l++)
|
348
|
{
|
349
|
JSONArray loop = new JSONArray();
|
350
|
|
351
|
int numVertices=coords[l].length;
|
352
|
|
353
|
for(int v=0; v<numVertices; v++)
|
354
|
{
|
355
|
JSONObject vertex=new JSONObject();
|
356
|
vertex.put("x", coords[l][v][0]);
|
357
|
vertex.put("y", coords[l][v][1]);
|
358
|
vertex.put("angle", (curvatures==null || curvatures[l]==null) ? 0 : curvatures[l][v]);
|
359
|
vertex.put("radius", radii[l][v]);
|
360
|
vertex.put("stroke", stroke[l][v]);
|
361
|
loop.put(vertex);
|
362
|
}
|
363
|
|
364
|
loops.put(loop);
|
365
|
}
|
366
|
|
367
|
stickerObj.put("loops",loops);
|
368
|
stickers.put(stickerObj);
|
369
|
}
|
370
|
|
371
|
return stickers;
|
372
|
}
|
373
|
|
374
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
375
|
|
376
|
private JSONArray generateOverrides(TwistyObject object) throws JSONException
|
377
|
{
|
378
|
ObjectStickerOverride[] overrides = object.getStickerOverrides();
|
379
|
|
380
|
if( overrides!=null )
|
381
|
{
|
382
|
JSONArray overrideArray = new JSONArray();
|
383
|
|
384
|
for (ObjectStickerOverride objectStickerOverride : overrides)
|
385
|
{
|
386
|
JSONObject override = new JSONObject();
|
387
|
int[] cubfac = objectStickerOverride.getCubitFaces();
|
388
|
int color = objectStickerOverride.getColor();
|
389
|
JSONArray cubfacArray = new JSONArray();
|
390
|
for (int cf : cubfac) cubfacArray.put(cf);
|
391
|
|
392
|
override.put("cubitfaces", cubfacArray);
|
393
|
override.put("color", color);
|
394
|
|
395
|
overrideArray.put(override);
|
396
|
}
|
397
|
|
398
|
return overrideArray;
|
399
|
}
|
400
|
|
401
|
return null;
|
402
|
}
|
403
|
|
404
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
405
|
|
406
|
private JSONObject generateMesh(TwistyObject object) throws JSONException
|
407
|
{
|
408
|
JSONObject mesh = new JSONObject();
|
409
|
|
410
|
JSONArray shapes = generateShapes(object); // do this before cubits so we calculate numCubitFaces
|
411
|
JSONArray cubits = generateCubits(object);
|
412
|
JSONArray stickers = generateStickers(object);
|
413
|
|
414
|
mesh.put("shapes" , shapes);
|
415
|
mesh.put("cubits" , cubits);
|
416
|
mesh.put("stickers", stickers);
|
417
|
mesh.put("pillow" , object.getPillowCoeff() );
|
418
|
|
419
|
JSONArray overrides = generateOverrides(object);
|
420
|
if( overrides!=null ) mesh.put("overrides", overrides);
|
421
|
|
422
|
return mesh;
|
423
|
}
|
424
|
|
425
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
426
|
|
427
|
private JSONObject generateMetadata(TwistyObject object, Metadata meta) throws JSONException
|
428
|
{
|
429
|
JSONObject metadata = new JSONObject();
|
430
|
|
431
|
metadata.put("longname" , object.getObjectName() );
|
432
|
metadata.put("inventor" , object.getInventor());
|
433
|
metadata.put("year" , object.getYearOfInvention());
|
434
|
metadata.put("complexity" , object.getComplexity());
|
435
|
metadata.put("size" , object.getSize() );
|
436
|
metadata.put("scrambles" , meta.numScrambles() );
|
437
|
metadata.put("shortname" , object.getShortName() );
|
438
|
metadata.put("resetmaps" , object.shouldResetTextureMaps() );
|
439
|
metadata.put("num_faces" , object.getNumPuzzleFaces() );
|
440
|
metadata.put("price" , meta.price() );
|
441
|
metadata.put("category" , meta.getCategory() );
|
442
|
|
443
|
JSONArray sigArray = new JSONArray();
|
444
|
long[] sig = object.getSignature().getArray();
|
445
|
for (long l : sig) sigArray.put(l);
|
446
|
metadata.put("signature" , sigArray );
|
447
|
|
448
|
return metadata;
|
449
|
}
|
450
|
|
451
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
452
|
|
453
|
private JSONArray generateQuats(TwistyObject object) throws JSONException
|
454
|
{
|
455
|
JSONArray quatsArray = new JSONArray();
|
456
|
Static4D[] quats = object.getQuats();
|
457
|
|
458
|
for(Static4D quat : quats)
|
459
|
{
|
460
|
JSONObject q = new JSONObject();
|
461
|
q.put("x",quat.get0());
|
462
|
q.put("y",quat.get1());
|
463
|
q.put("z",quat.get2());
|
464
|
q.put("w",quat.get3());
|
465
|
quatsArray.put(q);
|
466
|
}
|
467
|
|
468
|
return quatsArray;
|
469
|
}
|
470
|
|
471
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
472
|
|
473
|
private JSONArray generateAxis(TwistyObject object) throws JSONException
|
474
|
{
|
475
|
JSONArray axis = new JSONArray();
|
476
|
|
477
|
Static3D[] rotAxis = object.getRotationAxis();
|
478
|
int numAxis = rotAxis.length;
|
479
|
int[][] basicAngle = object.getBasicAngles();
|
480
|
int[] numLayers = object.getNumLayers();
|
481
|
float[][] cuts = object.getCuts(numLayers);
|
482
|
boolean[][] rotatable = object.getLayerRotatable(numLayers);
|
483
|
float[][] rotFactor = object.returnRotationFactor();
|
484
|
int[][] minimalCubits = object.getMinimalCubiesInRow();
|
485
|
|
486
|
for( int i=0; i<numAxis; i++ )
|
487
|
{
|
488
|
JSONObject axObject = new JSONObject();
|
489
|
Static3D ax = rotAxis[i];
|
490
|
|
491
|
axObject.put("x", ax.get0() );
|
492
|
axObject.put("y", ax.get1() );
|
493
|
axObject.put("z", ax.get2() );
|
494
|
|
495
|
JSONArray angleArray = new JSONArray();
|
496
|
for(float angle : basicAngle[i]) angleArray.put(angle);
|
497
|
axObject.put("basicAngles", angleArray);
|
498
|
JSONArray cutsArray = new JSONArray();
|
499
|
if( cuts[i]!=null ) for(float cut : cuts[i]) cutsArray.put(cut);
|
500
|
axObject.put("cuts", cutsArray);
|
501
|
JSONArray rotaArray = new JSONArray();
|
502
|
for(boolean rot : rotatable[i]) rotaArray.put(rot);
|
503
|
axObject.put("rotatable", rotaArray );
|
504
|
JSONArray factorArray = new JSONArray();
|
505
|
for(float rotf : rotFactor[i]) factorArray.put(rotf);
|
506
|
axObject.put("factor", factorArray );
|
507
|
|
508
|
if( minimalCubits!=null )
|
509
|
{
|
510
|
JSONArray minimalArray = new JSONArray();
|
511
|
for(int min : minimalCubits[i]) minimalArray.put(min);
|
512
|
axObject.put("minimal", minimalArray );
|
513
|
}
|
514
|
|
515
|
axis.put(axObject);
|
516
|
}
|
517
|
|
518
|
return axis;
|
519
|
}
|
520
|
|
521
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
522
|
|
523
|
private JSONObject generateScrambling(TwistyObject object) throws JSONException
|
524
|
{
|
525
|
JSONObject scrambling = new JSONObject();
|
526
|
|
527
|
int scrambleType = object.getScrambleType();
|
528
|
scrambling.put("scrambleType",scrambleType );
|
529
|
int[][] algorithms = object.getScrambleAlgorithms();
|
530
|
|
531
|
if( algorithms!=null )
|
532
|
{
|
533
|
JSONArray scrambleAlgorithms = new JSONArray();
|
534
|
|
535
|
for(int[] algorithm : algorithms)
|
536
|
{
|
537
|
JSONArray algArray = new JSONArray();
|
538
|
for (int entry : algorithm) algArray.put(entry);
|
539
|
scrambleAlgorithms.put(algArray);
|
540
|
}
|
541
|
|
542
|
scrambling.put("algorithms", scrambleAlgorithms);
|
543
|
}
|
544
|
|
545
|
int[][] edges = object.getScrambleEdges();
|
546
|
|
547
|
if( edges!=null )
|
548
|
{
|
549
|
JSONArray scrambleEdges = new JSONArray();
|
550
|
|
551
|
for(int[] edge : edges)
|
552
|
{
|
553
|
JSONArray edgeArray = new JSONArray();
|
554
|
for (int entry : edge) edgeArray.put(entry);
|
555
|
scrambleEdges.put(edgeArray);
|
556
|
}
|
557
|
|
558
|
scrambling.put("edges", scrambleEdges);
|
559
|
}
|
560
|
|
561
|
return scrambling;
|
562
|
}
|
563
|
|
564
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
565
|
|
566
|
private JSONObject generateTouchControl(TwistyObject object) throws JSONException
|
567
|
{
|
568
|
JSONObject touchControl = new JSONObject();
|
569
|
|
570
|
touchControl.put("movementType" , object.getTouchControlType() );
|
571
|
touchControl.put("movementSplit", object.getTouchControlSplit() );
|
572
|
|
573
|
int[][][] enabled = object.getEnabled();
|
574
|
|
575
|
if( enabled!=null )
|
576
|
{
|
577
|
JSONArray enabledArray = new JSONArray();
|
578
|
|
579
|
for(int[][] faceEnabled : enabled)
|
580
|
{
|
581
|
JSONArray faceArray = new JSONArray();
|
582
|
|
583
|
for(int[] sectionEnabled : faceEnabled)
|
584
|
{
|
585
|
JSONArray sectionArray = new JSONArray();
|
586
|
for(int ax : sectionEnabled) sectionArray.put(ax);
|
587
|
faceArray.put(sectionArray);
|
588
|
}
|
589
|
enabledArray.put(faceArray);
|
590
|
}
|
591
|
|
592
|
touchControl.put("enabledAxis", enabledArray);
|
593
|
}
|
594
|
|
595
|
int[] numLayers = object.getNumLayers();
|
596
|
float[] dist3D = object.getDist3D(numLayers);
|
597
|
|
598
|
if( dist3D!=null )
|
599
|
{
|
600
|
JSONArray distArray = new JSONArray();
|
601
|
for( float dist: dist3D ) distArray.put(dist);
|
602
|
touchControl.put("dist3D", distArray);
|
603
|
}
|
604
|
|
605
|
return touchControl;
|
606
|
}
|
607
|
|
608
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
609
|
|
610
|
private JSONArray generateColors(TwistyObject object)
|
611
|
{
|
612
|
JSONArray jsonColors = new JSONArray();
|
613
|
int[] colorTable = object.getColorTable();
|
614
|
int numFaceColors = colorTable.length;
|
615
|
for(int i=0; i<numFaceColors; i++) jsonColors.put(colorTable[i]);
|
616
|
|
617
|
jsonColors.put(object.getInternalColor());
|
618
|
|
619
|
return jsonColors;
|
620
|
}
|
621
|
|
622
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
623
|
|
624
|
private JSONObject generateSolved(TwistyObject object) throws JSONException
|
625
|
{
|
626
|
JSONObject solved = new JSONObject();
|
627
|
int[][] solvedGroups = object.getSolvedQuats();
|
628
|
|
629
|
solved.put("functionIndex", object.getSolvedFunctionIndex() );
|
630
|
|
631
|
if( solvedGroups!=null )
|
632
|
{
|
633
|
JSONArray groupArray = new JSONArray();
|
634
|
|
635
|
for( int[] group : solvedGroups )
|
636
|
{
|
637
|
JSONArray groupElements = new JSONArray();
|
638
|
for( int element : group ) groupElements.put(element);
|
639
|
groupArray.put(groupElements);
|
640
|
}
|
641
|
|
642
|
solved.put("groups", groupArray );
|
643
|
}
|
644
|
|
645
|
return solved;
|
646
|
}
|
647
|
|
648
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
649
|
|
650
|
public String createObjectString(TwistyObject object) throws JSONException
|
651
|
{
|
652
|
JSONObject json = new JSONObject();
|
653
|
Metadata meta = object.getMetadata();
|
654
|
|
655
|
JSONObject metadata = generateMetadata(object,meta);
|
656
|
JSONObject mesh = generateMesh(object);
|
657
|
JSONArray axis = generateAxis(object);
|
658
|
JSONArray quats = generateQuats(object);
|
659
|
JSONObject scrambling = generateScrambling(object);
|
660
|
JSONObject touchControl= generateTouchControl(object);
|
661
|
JSONArray colors = generateColors(object);
|
662
|
JSONObject solved = generateSolved(object);
|
663
|
|
664
|
json.put("major" , VERSION_OBJECT_APP);
|
665
|
json.put("minor" , meta.objectVersion() );
|
666
|
json.put("metadata" , metadata);
|
667
|
json.put("mesh" , mesh);
|
668
|
json.put("axis" , axis);
|
669
|
json.put("quats" , quats);
|
670
|
json.put("scrambling" , scrambling);
|
671
|
json.put("touchcontrol", touchControl);
|
672
|
json.put("colors" , colors);
|
673
|
json.put("solved" , solved);
|
674
|
|
675
|
return json.toString();
|
676
|
}
|
677
|
|
678
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
679
|
|
680
|
public String createExtrasString(TwistyObject object) throws JSONException
|
681
|
{
|
682
|
String[][] tuts = object.getTutorials();
|
683
|
|
684
|
if( tuts!=null )
|
685
|
{
|
686
|
JSONObject json = new JSONObject();
|
687
|
JSONArray tutorials = new JSONArray();
|
688
|
|
689
|
for(String[] tut : tuts)
|
690
|
{
|
691
|
String language = tut[0];
|
692
|
String link = tut[1];
|
693
|
String title = tut[2];
|
694
|
String author = tut[3];
|
695
|
|
696
|
JSONObject oneTut = new JSONObject();
|
697
|
|
698
|
oneTut.put("language", language);
|
699
|
oneTut.put("link" , link );
|
700
|
oneTut.put("title" , title );
|
701
|
oneTut.put("author" , author );
|
702
|
|
703
|
tutorials.put(oneTut);
|
704
|
}
|
705
|
|
706
|
String name = object.getShortName();
|
707
|
Metadata meta = object.getMetadata();
|
708
|
|
709
|
json.put("major" , VERSION_EXTRAS_APP);
|
710
|
json.put("minor" , meta.extrasVersion() );
|
711
|
json.put("object" , name );
|
712
|
json.put("tutorials" , tutorials);
|
713
|
|
714
|
return json.toString();
|
715
|
}
|
716
|
|
717
|
return null;
|
718
|
}
|
719
|
|
720
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
721
|
|
722
|
public void write(String filename, String contents) throws IOException
|
723
|
{
|
724
|
FileOutputStream fos = new FileOutputStream(filename);
|
725
|
OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
|
726
|
BufferedWriter bw = new BufferedWriter(osw);
|
727
|
bw.write(contents);
|
728
|
bw.flush();
|
729
|
}
|
730
|
|
731
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
732
|
|
733
|
public static JsonWriter getInstance()
|
734
|
{
|
735
|
if( mThis==null ) mThis = new JsonWriter();
|
736
|
return mThis;
|
737
|
}
|
738
|
}
|