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