Project

General

Profile

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

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

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 ae9d9227 leszek
import org.distorted.objectlib.metadata.Metadata;
22 82eb152a Leszek Koltunski
import org.json.JSONArray;
23
import org.json.JSONException;
24
import org.json.JSONObject;
25
26 e26eb4e7 Leszek Koltunski
import org.distorted.library.type.Static3D;
27
import org.distorted.library.type.Static4D;
28 82eb152a Leszek Koltunski
29 e26eb4e7 Leszek Koltunski
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 e61a158a leszek
  public static final int VERSION_OBJECT_APP = 15;
38 b5744b1f leszek
  public static final int VERSION_EXTRAS_APP = 1;
39 e26eb4e7 Leszek Koltunski
40
  private static JsonWriter mThis;
41 1f264f3e Leszek Koltunski
  private static int mNumCubitFaces;
42 e26eb4e7 Leszek Koltunski
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 19595510 Leszek Koltunski
    int numCubits       = positions.length;
59 e26eb4e7 Leszek Koltunski
60 d4105efe Leszek Koltunski
    for(int c=0; c<numCubits; c++)
61 e26eb4e7 Leszek Koltunski
      {
62
      JSONObject cubit = new JSONObject();
63 7aae846c Leszek Koltunski
      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 e26eb4e7 Leszek Koltunski
68
      JSONArray pos = new JSONArray();
69 d4105efe Leszek Koltunski
      int numPos = positions[c].length;
70
      for(int j=0; j<numPos; j++) pos.put(positions[c][j]);
71 e26eb4e7 Leszek Koltunski
      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 7aae846c Leszek Koltunski
      cubit.put("type", type);
78 52cc8639 Leszek Koltunski
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 e26eb4e7 Leszek Koltunski
86
      JSONArray colors = new JSONArray();
87
88 d4105efe Leszek Koltunski
      for(int f=0; f<mNumCubitFaces; f++)
89 e26eb4e7 Leszek Koltunski
        {
90 ed0988c0 Leszek Koltunski
        int cubColor = object.getCubitFaceMap(c,f);
91 e26eb4e7 Leszek Koltunski
        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 5377b942 Leszek Koltunski
  private JSONArray generateFaces(TwistyObject object, ObjectShape shape, int[] bandIndices, int[] faceOuter, int cubit, int[] numLayers) throws JSONException
126 e26eb4e7 Leszek Koltunski
    {
127
    JSONArray array = new JSONArray();
128 5377b942 Leszek Koltunski
    int numFaces = shape.getNumFaces();
129 e26eb4e7 Leszek Koltunski
    int variant = object.getCubitVariant(cubit,numLayers);
130 5377b942 Leszek Koltunski
    boolean multigon = shape.isMultigon();
131
    int[][] vertIndices1   = shape.getVertIndices();
132
    int[][][] vertIndices2 = shape.getMultigonIndices();
133 e26eb4e7 Leszek Koltunski
134
    for(int i=0; i<numFaces; i++)
135
      {
136
      JSONObject face = new JSONObject();
137
      face.put("bandIndex", bandIndices[i]);
138
139 51262d81 Leszek Koltunski
      int stiShape = object.getVariantStickerShape(variant,i);
140
      face.put("sticker", stiShape);
141 40e77224 Leszek Koltunski
      face.put("isOuter", faceOuter[i]);
142 e26eb4e7 Leszek Koltunski
143 5377b942 Leszek Koltunski
      JSONArray faceArr = new JSONArray();
144 e26eb4e7 Leszek Koltunski
145 5377b942 Leszek Koltunski
      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 e26eb4e7 Leszek Koltunski
      array.put(face);
164
      }
165
166
    return array;
167
    }
168
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
171 84a17011 Leszek Koltunski
  private JSONArray generateVertices(float[][] vertices) throws JSONException
172 e26eb4e7 Leszek Koltunski
    {
173
    JSONArray array = new JSONArray();
174
175 84a17011 Leszek Koltunski
    for (float[] vertex : vertices)
176 e26eb4e7 Leszek Koltunski
      {
177
      JSONObject vert = new JSONObject();
178 84a17011 Leszek Koltunski
      vert.put("x", vertex[0]);
179
      vert.put("y", vertex[1]);
180
      vert.put("z", vertex[2]);
181 e26eb4e7 Leszek Koltunski
      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 84a17011 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 8bb3e677 Leszek Koltunski
        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 84a17011 Leszek Koltunski
        }
244
245
      return array;
246
      }
247
248
    return null;
249
    }
250
251 e26eb4e7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 40e77224 Leszek Koltunski
    int[][] faceOuter   = object.getVariantFaceIsOuter();
273 e26eb4e7 Leszek Koltunski
    int numCubits = positions.length;
274 1f264f3e Leszek Koltunski
    mNumCubitFaces = 0;
275 e26eb4e7 Leszek Koltunski
276
    for(int i=0; i<numVariants; i++)
277
      {
278
      JSONObject shapeObj = new JSONObject();
279
280
      ObjectShape shape = object.getObjectShape(i);
281 3ee1d662 Leszek Koltunski
      ObjectFaceShape face = object.getObjectFaceShape(i);
282 84a17011 Leszek Koltunski
      ObjectVertexEffects effects = object.getVertexEffects(i);
283 e26eb4e7 Leszek Koltunski
284 3ee1d662 Leszek Koltunski
      float[] convexity  = face.getConvexityCenter();
285 57ef6378 Leszek Koltunski
      float[][] vertices = shape.getVertices();
286 3ee1d662 Leszek Koltunski
      float[][] bands    = face.getBands();
287
      int[] bandIndices  = face.getBandIndices();
288 e26eb4e7 Leszek Koltunski
289 5377b942 Leszek Koltunski
      int num = shape.getNumFaces();
290 1f264f3e Leszek Koltunski
      if( num>mNumCubitFaces ) mNumCubitFaces=num;
291
292 e26eb4e7 Leszek Koltunski
      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 5377b942 Leszek Koltunski
      if( shape.isMultigon() ) shapeObj.put("facesMultigon", true);
301
302 84a17011 Leszek Koltunski
      JSONArray verticesArr = generateVertices(vertices);
303 e26eb4e7 Leszek Koltunski
      shapeObj.put("vertices", verticesArr);
304 5377b942 Leszek Koltunski
      JSONArray facesArr = generateFaces(object,shape,bandIndices,faceOuter[i],cubit,numLayers);
305 e26eb4e7 Leszek Koltunski
      shapeObj.put("faces", facesArr);
306
      JSONArray bandsArr = generateBands(bands);
307
      shapeObj.put("bands", bandsArr);
308 8bb3e677 Leszek Koltunski
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 e26eb4e7 Leszek Koltunski
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 7af68038 Leszek Koltunski
    int numStickers = object.getNumStickerTypes();
334 e26eb4e7 Leszek Koltunski
335 ebe8c08e leszek
    for(int s=0; s<numStickers; s++)
336 e26eb4e7 Leszek Koltunski
      {
337 ebe8c08e leszek
      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 e61a158a leszek
      float[][] stroke     = sticker.getStrokes();
344 e26eb4e7 Leszek Koltunski
345 ebe8c08e leszek
      int numLoops = coords.length;
346 e26eb4e7 Leszek Koltunski
347 ebe8c08e leszek
      for(int l=0; l<numLoops; l++)
348
        {
349
        JSONArray loop = new JSONArray();
350 e26eb4e7 Leszek Koltunski
351 ebe8c08e leszek
        int numVertices=coords[l].length;
352 e26eb4e7 Leszek Koltunski
353 ebe8c08e leszek
        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 e61a158a leszek
          vertex.put("stroke", stroke[l][v]);
361 ebe8c08e leszek
          loop.put(vertex);
362
          }
363
364
        loops.put(loop);
365 e26eb4e7 Leszek Koltunski
        }
366 e61a158a leszek
367 ebe8c08e leszek
      stickerObj.put("loops",loops);
368 e26eb4e7 Leszek Koltunski
      stickers.put(stickerObj);
369
      }
370
371
    return stickers;
372
    }
373
374 3a0a23bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 ff60e713 Leszek Koltunski
        int[] cubfac = objectStickerOverride.getCubitFaces();
388
        int color    = objectStickerOverride.getColor();
389
        JSONArray cubfacArray = new JSONArray();
390
        for (int cf : cubfac) cubfacArray.put(cf);
391 3a0a23bf Leszek Koltunski
392 ff60e713 Leszek Koltunski
        override.put("cubitfaces", cubfacArray);
393 3a0a23bf Leszek Koltunski
        override.put("color", color);
394
395
        overrideArray.put(override);
396
        }
397
398
      return overrideArray;
399
      }
400
401
    return null;
402
    }
403
404 e26eb4e7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
405
406
  private JSONObject generateMesh(TwistyObject object) throws JSONException
407
    {
408
    JSONObject mesh = new JSONObject();
409
410 1f264f3e Leszek Koltunski
    JSONArray shapes   = generateShapes(object);  // do this before cubits so we calculate numCubitFaces
411 e26eb4e7 Leszek Koltunski
    JSONArray cubits   = generateCubits(object);
412
    JSONArray stickers = generateStickers(object);
413
414
    mesh.put("shapes"  , shapes);
415 1f264f3e Leszek Koltunski
    mesh.put("cubits"  , cubits);
416 e26eb4e7 Leszek Koltunski
    mesh.put("stickers", stickers);
417 6db8fe2e Leszek Koltunski
    mesh.put("pillow"  , object.getPillowCoeff() );
418 e26eb4e7 Leszek Koltunski
419 3a0a23bf Leszek Koltunski
    JSONArray overrides = generateOverrides(object);
420
    if( overrides!=null ) mesh.put("overrides", overrides);
421
422 e26eb4e7 Leszek Koltunski
    return mesh;
423
    }
424
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426
427 71df2bd4 leszek
  private JSONObject generateMetadata(TwistyObject object, Metadata meta) throws JSONException
428 e26eb4e7 Leszek Koltunski
    {
429
    JSONObject metadata = new JSONObject();
430
431 de4a7e02 Leszek Koltunski
    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 71df2bd4 leszek
    metadata.put("scrambles"  , meta.numScrambles() );
437 5f54927b Leszek Koltunski
    metadata.put("shortname"  , object.getShortName() );
438 0f72365b Leszek Koltunski
    metadata.put("resetmaps"  , object.shouldResetTextureMaps() );
439 1eafa9c6 leszek
    metadata.put("num_faces"  , object.getNumPuzzleFaces() );
440 71df2bd4 leszek
    metadata.put("price"      , meta.price() );
441
    metadata.put("category"   , meta.getCategory() );
442 1d581993 Leszek Koltunski
443 882a8142 Leszek Koltunski
    JSONArray sigArray = new JSONArray();
444
    long[] sig = object.getSignature().getArray();
445
    for (long l : sig) sigArray.put(l);
446
    metadata.put("signature" , sigArray );
447 e26eb4e7 Leszek Koltunski
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 beee90ab Leszek Koltunski
    int[][] basicAngle = object.getBasicAngles();
480 e26eb4e7 Leszek Koltunski
    int[] numLayers = object.getNumLayers();
481
    float[][] cuts = object.getCuts(numLayers);
482
    boolean[][] rotatable = object.getLayerRotatable(numLayers);
483 deaf067b Leszek Koltunski
    float[][] rotFactor = object.returnRotationFactor();
484 63bfcdd1 leszek
    int[][] minimalCubits = object.getMinimalCubiesInRow();
485 e26eb4e7 Leszek Koltunski
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 beee90ab Leszek Koltunski
      JSONArray angleArray = new JSONArray();
496
      for(float angle : basicAngle[i]) angleArray.put(angle);
497
      axObject.put("basicAngles", angleArray);
498 e26eb4e7 Leszek Koltunski
      JSONArray cutsArray = new JSONArray();
499 95123ad0 Leszek Koltunski
      if( cuts[i]!=null ) for(float cut : cuts[i]) cutsArray.put(cut);
500 e26eb4e7 Leszek Koltunski
      axObject.put("cuts", cutsArray);
501
      JSONArray rotaArray = new JSONArray();
502
      for(boolean rot : rotatable[i]) rotaArray.put(rot);
503
      axObject.put("rotatable", rotaArray );
504 deaf067b Leszek Koltunski
      JSONArray factorArray = new JSONArray();
505
      for(float rotf : rotFactor[i]) factorArray.put(rotf);
506
      axObject.put("factor", factorArray );
507 e26eb4e7 Leszek Koltunski
508 63bfcdd1 leszek
      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 e26eb4e7 Leszek Koltunski
      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 9ba7f3f6 Leszek Koltunski
    int[][] algorithms = object.getScrambleAlgorithms();
530 e26eb4e7 Leszek Koltunski
531 9ba7f3f6 Leszek Koltunski
    if( algorithms!=null )
532 e26eb4e7 Leszek Koltunski
      {
533 9ba7f3f6 Leszek Koltunski
      JSONArray scrambleAlgorithms = new JSONArray();
534 e26eb4e7 Leszek Koltunski
535 9ba7f3f6 Leszek Koltunski
      for(int[] algorithm : algorithms)
536 e26eb4e7 Leszek Koltunski
        {
537 9ba7f3f6 Leszek Koltunski
        JSONArray algArray = new JSONArray();
538
        for (int entry : algorithm) algArray.put(entry);
539
        scrambleAlgorithms.put(algArray);
540
        }
541 e26eb4e7 Leszek Koltunski
542 9ba7f3f6 Leszek Koltunski
      scrambling.put("algorithms", scrambleAlgorithms);
543
      }
544 e26eb4e7 Leszek Koltunski
545 9ba7f3f6 Leszek Koltunski
    int[][] edges = object.getScrambleEdges();
546 e26eb4e7 Leszek Koltunski
547 9ba7f3f6 Leszek Koltunski
    if( edges!=null )
548
      {
549
      JSONArray scrambleEdges = new JSONArray();
550 e26eb4e7 Leszek Koltunski
551 9ba7f3f6 Leszek Koltunski
      for(int[] edge : edges)
552
        {
553
        JSONArray edgeArray = new JSONArray();
554
        for (int entry : edge) edgeArray.put(entry);
555
        scrambleEdges.put(edgeArray);
556 e26eb4e7 Leszek Koltunski
        }
557
558 9ba7f3f6 Leszek Koltunski
      scrambling.put("edges", scrambleEdges);
559 e26eb4e7 Leszek Koltunski
      }
560
561
    return scrambling;
562
    }
563
564
///////////////////////////////////////////////////////////////////////////////////////////////////
565
566
  private JSONObject generateTouchControl(TwistyObject object) throws JSONException
567
    {
568
    JSONObject touchControl = new JSONObject();
569
570 11fa413d Leszek Koltunski
    touchControl.put("movementType" , object.getTouchControlType() );
571
    touchControl.put("movementSplit", object.getTouchControlSplit() );
572 e26eb4e7 Leszek Koltunski
573
    int[][][] enabled = object.getEnabled();
574
575 82904e62 Leszek Koltunski
    if( enabled!=null )
576 e26eb4e7 Leszek Koltunski
      {
577 82904e62 Leszek Koltunski
      JSONArray enabledArray = new JSONArray();
578 e26eb4e7 Leszek Koltunski
579 82904e62 Leszek Koltunski
      for(int[][] faceEnabled : enabled)
580 e26eb4e7 Leszek Koltunski
        {
581 82904e62 Leszek Koltunski
        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 e26eb4e7 Leszek Koltunski
        }
591
592 82904e62 Leszek Koltunski
      touchControl.put("enabledAxis", enabledArray);
593
      }
594 e26eb4e7 Leszek Koltunski
595 82eb152a Leszek Koltunski
    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 e26eb4e7 Leszek Koltunski
    return touchControl;
606
    }
607
608 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
609
610
  private JSONArray generateColors(TwistyObject object)
611
    {
612
    JSONArray jsonColors = new JSONArray();
613 962b8ff6 leszek
    int[] colorTable = object.getColorTable();
614
    int numFaceColors = colorTable.length;
615
    for(int i=0; i<numFaceColors; i++) jsonColors.put(colorTable[i]);
616 82eb152a Leszek Koltunski
617 253e440f Leszek Koltunski
    jsonColors.put(object.getInternalColor());
618
619 82eb152a Leszek Koltunski
    return jsonColors;
620
    }
621
622 3c48fab9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 e26eb4e7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
649
650 71df2bd4 leszek
  public String createObjectString(TwistyObject object) throws JSONException
651 e26eb4e7 Leszek Koltunski
    {
652
    JSONObject json = new JSONObject();
653 71df2bd4 leszek
    Metadata meta = object.getMetadata();
654 e26eb4e7 Leszek Koltunski
655 71df2bd4 leszek
    JSONObject metadata    = generateMetadata(object,meta);
656 e26eb4e7 Leszek Koltunski
    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 82eb152a Leszek Koltunski
    JSONArray  colors      = generateColors(object);
662 3c48fab9 Leszek Koltunski
    JSONObject solved      = generateSolved(object);
663 e26eb4e7 Leszek Koltunski
664 b5744b1f leszek
    json.put("major"       , VERSION_OBJECT_APP);
665 ae9d9227 leszek
    json.put("minor"       , meta.objectVersion() );
666 e26eb4e7 Leszek Koltunski
    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 82eb152a Leszek Koltunski
    json.put("colors"      , colors);
673 3c48fab9 Leszek Koltunski
    json.put("solved"      , solved);
674 82eb152a Leszek Koltunski
675
    return json.toString();
676
    }
677 e26eb4e7 Leszek Koltunski
678 052e0362 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
679
680 4bf52f4f Leszek Koltunski
  public String createExtrasString(TwistyObject object) throws JSONException
681 052e0362 Leszek Koltunski
    {
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 b5744b1f leszek
      String name = object.getShortName();
707 ae9d9227 leszek
      Metadata meta = object.getMetadata();
708 b5744b1f leszek
709
      json.put("major"     , VERSION_EXTRAS_APP);
710 ae9d9227 leszek
      json.put("minor"     , meta.extrasVersion() );
711 b5744b1f leszek
      json.put("object"    , name );
712 052e0362 Leszek Koltunski
      json.put("tutorials" , tutorials);
713
714
      return json.toString();
715
      }
716
717
    return null;
718
    }
719
720 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
721 e26eb4e7 Leszek Koltunski
722 82eb152a Leszek Koltunski
  public void write(String filename, String contents) throws IOException
723
    {
724 8edbe5b9 leszek
    FileOutputStream fos = new FileOutputStream(filename);
725
    OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
726 82eb152a Leszek Koltunski
    BufferedWriter bw = new BufferedWriter(osw);
727
    bw.write(contents);
728
    bw.flush();
729 e26eb4e7 Leszek Koltunski
    }
730
731
///////////////////////////////////////////////////////////////////////////////////////////////////
732
733
  public static JsonWriter getInstance()
734
    {
735
    if( mThis==null ) mThis = new JsonWriter();
736
    return mThis;
737
    }
738
}