Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / json / JsonWriter.java @ deaf067b

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