Project

General

Profile

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

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

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 10b7e306 Leszek Koltunski
import org.distorted.objectlib.scrambling.ScrambleState;
31 e26eb4e7 Leszek Koltunski
import org.distorted.objectlib.main.TwistyObject;
32
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
35
public class JsonWriter
36
{
37 b63235a0 Leszek Koltunski
  public static final int VERSION_OBJECT_MAJOR = 6;
38 4bf52f4f Leszek Koltunski
  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 e26eb4e7 Leszek Koltunski
42
  private static JsonWriter mThis;
43 1f264f3e Leszek Koltunski
  private static int mNumCubitFaces;
44 e26eb4e7 Leszek Koltunski
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 19595510 Leszek Koltunski
    int numCubits       = positions.length;
61 e26eb4e7 Leszek Koltunski
62 d4105efe Leszek Koltunski
    for(int c=0; c<numCubits; c++)
63 e26eb4e7 Leszek Koltunski
      {
64
      JSONObject cubit = new JSONObject();
65 7aae846c Leszek Koltunski
      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 e26eb4e7 Leszek Koltunski
70
      JSONArray pos = new JSONArray();
71 d4105efe Leszek Koltunski
      int numPos = positions[c].length;
72
      for(int j=0; j<numPos; j++) pos.put(positions[c][j]);
73 e26eb4e7 Leszek Koltunski
      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 7aae846c Leszek Koltunski
      cubit.put("type", type);
80 52cc8639 Leszek Koltunski
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 e26eb4e7 Leszek Koltunski
88
      JSONArray colors = new JSONArray();
89
90 d4105efe Leszek Koltunski
      for(int f=0; f<mNumCubitFaces; f++)
91 e26eb4e7 Leszek Koltunski
        {
92 ed0988c0 Leszek Koltunski
        int cubColor = object.getCubitFaceMap(c,f);
93 e26eb4e7 Leszek Koltunski
        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 40e77224 Leszek Koltunski
  private JSONArray generateFaces(TwistyObject object, int[][] vertIndices, int[] bandIndices, int[] faceOuter, int cubit, int[] numLayers) throws JSONException
128 e26eb4e7 Leszek Koltunski
    {
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 51262d81 Leszek Koltunski
      int stiShape = object.getVariantStickerShape(variant,i);
139
      face.put("sticker", stiShape);
140 40e77224 Leszek Koltunski
      face.put("isOuter", faceOuter[i]);
141 e26eb4e7 Leszek Koltunski
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 84a17011 Leszek Koltunski
  private JSONArray generateVertices(float[][] vertices) throws JSONException
156 e26eb4e7 Leszek Koltunski
    {
157
    JSONArray array = new JSONArray();
158
159 84a17011 Leszek Koltunski
    for (float[] vertex : vertices)
160 e26eb4e7 Leszek Koltunski
      {
161
      JSONObject vert = new JSONObject();
162 84a17011 Leszek Koltunski
      vert.put("x", vertex[0]);
163
      vert.put("y", vertex[1]);
164
      vert.put("z", vertex[2]);
165 e26eb4e7 Leszek Koltunski
      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 84a17011 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 8bb3e677 Leszek Koltunski
        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 84a17011 Leszek Koltunski
        }
228
229
      return array;
230
      }
231
232
    return null;
233
    }
234
235 e26eb4e7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 40e77224 Leszek Koltunski
    int[][] faceOuter   = object.getVariantFaceIsOuter();
257 e26eb4e7 Leszek Koltunski
    int numCubits = positions.length;
258 1f264f3e Leszek Koltunski
    mNumCubitFaces = 0;
259 e26eb4e7 Leszek Koltunski
260
    for(int i=0; i<numVariants; i++)
261
      {
262
      JSONObject shapeObj = new JSONObject();
263
264
      ObjectShape shape = object.getObjectShape(i);
265 3ee1d662 Leszek Koltunski
      ObjectFaceShape face = object.getObjectFaceShape(i);
266 84a17011 Leszek Koltunski
      ObjectVertexEffects effects = object.getVertexEffects(i);
267 e26eb4e7 Leszek Koltunski
268 3ee1d662 Leszek Koltunski
      float[] convexity  = face.getConvexityCenter();
269 57ef6378 Leszek Koltunski
      float[][] vertices = shape.getVertices();
270 e26eb4e7 Leszek Koltunski
      int[][] vertIndices= shape.getVertIndices();
271 3ee1d662 Leszek Koltunski
      float[][] bands    = face.getBands();
272
      int[] bandIndices  = face.getBandIndices();
273 e26eb4e7 Leszek Koltunski
274 1f264f3e Leszek Koltunski
      int num = vertIndices.length;
275
      if( num>mNumCubitFaces ) mNumCubitFaces=num;
276
277 e26eb4e7 Leszek Koltunski
      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 84a17011 Leszek Koltunski
      JSONArray verticesArr = generateVertices(vertices);
286 e26eb4e7 Leszek Koltunski
      shapeObj.put("vertices", verticesArr);
287 40e77224 Leszek Koltunski
      JSONArray facesArr = generateFaces(object,vertIndices,bandIndices,faceOuter[i],cubit,numLayers);
288 e26eb4e7 Leszek Koltunski
      shapeObj.put("faces", facesArr);
289
      JSONArray bandsArr = generateBands(bands);
290
      shapeObj.put("bands", bandsArr);
291 8bb3e677 Leszek Koltunski
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 e26eb4e7 Leszek Koltunski
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 7af68038 Leszek Koltunski
    int numStickers = object.getNumStickerTypes();
317 e26eb4e7 Leszek Koltunski
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 3a0a23bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 ff60e713 Leszek Koltunski
        int[] cubfac = objectStickerOverride.getCubitFaces();
363
        int color    = objectStickerOverride.getColor();
364
        JSONArray cubfacArray = new JSONArray();
365
        for (int cf : cubfac) cubfacArray.put(cf);
366 3a0a23bf Leszek Koltunski
367 ff60e713 Leszek Koltunski
        override.put("cubitfaces", cubfacArray);
368 3a0a23bf Leszek Koltunski
        override.put("color", color);
369
370
        overrideArray.put(override);
371
        }
372
373
      return overrideArray;
374
      }
375
376
    return null;
377
    }
378
379 e26eb4e7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
380
381
  private JSONObject generateMesh(TwistyObject object) throws JSONException
382
    {
383
    JSONObject mesh = new JSONObject();
384
385 1f264f3e Leszek Koltunski
    JSONArray shapes   = generateShapes(object);  // do this before cubits so we calculate numCubitFaces
386 e26eb4e7 Leszek Koltunski
    JSONArray cubits   = generateCubits(object);
387
    JSONArray stickers = generateStickers(object);
388
389
    mesh.put("shapes"  , shapes);
390 1f264f3e Leszek Koltunski
    mesh.put("cubits"  , cubits);
391 e26eb4e7 Leszek Koltunski
    mesh.put("stickers", stickers);
392 6db8fe2e Leszek Koltunski
    mesh.put("pillow"  , object.getPillowCoeff() );
393 e26eb4e7 Leszek Koltunski
394 3a0a23bf Leszek Koltunski
    JSONArray overrides = generateOverrides(object);
395
    if( overrides!=null ) mesh.put("overrides", overrides);
396
397 e26eb4e7 Leszek Koltunski
    return mesh;
398
    }
399
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401
402 8b3b1d85 Leszek Koltunski
  private JSONObject generateMetadata(TwistyObject object, int numScramble, int price) throws JSONException
403 e26eb4e7 Leszek Koltunski
    {
404
    JSONObject metadata = new JSONObject();
405
406 de4a7e02 Leszek Koltunski
    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 e85c8f90 Leszek Koltunski
    metadata.put("scrambles"  , numScramble );
412 5f54927b Leszek Koltunski
    metadata.put("shortname"  , object.getShortName() );
413 0f72365b Leszek Koltunski
    metadata.put("resetmaps"  , object.shouldResetTextureMaps() );
414 a72cd106 Leszek Koltunski
    metadata.put("num_faces"  , object.getNumFaces() );
415 8b3b1d85 Leszek Koltunski
    metadata.put("price"      , price );
416 1d581993 Leszek Koltunski
417 882a8142 Leszek Koltunski
    JSONArray sigArray = new JSONArray();
418
    long[] sig = object.getSignature().getArray();
419
    for (long l : sig) sigArray.put(l);
420
    metadata.put("signature" , sigArray );
421 e26eb4e7 Leszek Koltunski
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 beee90ab Leszek Koltunski
    int[][] basicAngle = object.getBasicAngles();
454 e26eb4e7 Leszek Koltunski
    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 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
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 9ba7f3f6 Leszek Koltunski
    int[][] algorithms = object.getScrambleAlgorithms();
492 e26eb4e7 Leszek Koltunski
493 9ba7f3f6 Leszek Koltunski
    if( algorithms!=null )
494 e26eb4e7 Leszek Koltunski
      {
495 9ba7f3f6 Leszek Koltunski
      JSONArray scrambleAlgorithms = new JSONArray();
496 e26eb4e7 Leszek Koltunski
497 9ba7f3f6 Leszek Koltunski
      for(int[] algorithm : algorithms)
498 e26eb4e7 Leszek Koltunski
        {
499 9ba7f3f6 Leszek Koltunski
        JSONArray algArray = new JSONArray();
500
        for (int entry : algorithm) algArray.put(entry);
501
        scrambleAlgorithms.put(algArray);
502
        }
503 e26eb4e7 Leszek Koltunski
504 9ba7f3f6 Leszek Koltunski
      scrambling.put("algorithms", scrambleAlgorithms);
505
      }
506 e26eb4e7 Leszek Koltunski
507 9ba7f3f6 Leszek Koltunski
    int[][] edges = object.getScrambleEdges();
508 e26eb4e7 Leszek Koltunski
509 9ba7f3f6 Leszek Koltunski
    if( edges!=null )
510
      {
511
      JSONArray scrambleEdges = new JSONArray();
512 e26eb4e7 Leszek Koltunski
513 9ba7f3f6 Leszek Koltunski
      for(int[] edge : edges)
514
        {
515
        JSONArray edgeArray = new JSONArray();
516
        for (int entry : edge) edgeArray.put(entry);
517
        scrambleEdges.put(edgeArray);
518 e26eb4e7 Leszek Koltunski
        }
519
520 9ba7f3f6 Leszek Koltunski
      scrambling.put("edges", scrambleEdges);
521 e26eb4e7 Leszek Koltunski
      }
522
523
    return scrambling;
524
    }
525
526
///////////////////////////////////////////////////////////////////////////////////////////////////
527
528
  private JSONObject generateTouchControl(TwistyObject object) throws JSONException
529
    {
530
    JSONObject touchControl = new JSONObject();
531
532 11fa413d Leszek Koltunski
    touchControl.put("movementType" , object.getTouchControlType() );
533
    touchControl.put("movementSplit", object.getTouchControlSplit() );
534 e26eb4e7 Leszek Koltunski
535
    int[][][] enabled = object.getEnabled();
536
537 82904e62 Leszek Koltunski
    if( enabled!=null )
538 e26eb4e7 Leszek Koltunski
      {
539 82904e62 Leszek Koltunski
      JSONArray enabledArray = new JSONArray();
540 e26eb4e7 Leszek Koltunski
541 82904e62 Leszek Koltunski
      for(int[][] faceEnabled : enabled)
542 e26eb4e7 Leszek Koltunski
        {
543 82904e62 Leszek Koltunski
        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 e26eb4e7 Leszek Koltunski
        }
553
554 82904e62 Leszek Koltunski
      touchControl.put("enabledAxis", enabledArray);
555
      }
556 e26eb4e7 Leszek Koltunski
557 82eb152a Leszek Koltunski
    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 e26eb4e7 Leszek Koltunski
    return touchControl;
568
    }
569
570 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 253e440f Leszek Koltunski
    jsonColors.put(object.getInternalColor());
579
580 82eb152a Leszek Koltunski
    return jsonColors;
581
    }
582
583 3c48fab9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 e26eb4e7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
610
611 8b3b1d85 Leszek Koltunski
  public String createObjectString(TwistyObject object,int numScramble, int price) throws JSONException
612 e26eb4e7 Leszek Koltunski
    {
613
    JSONObject json = new JSONObject();
614
615 8b3b1d85 Leszek Koltunski
    JSONObject metadata    = generateMetadata(object,numScramble,price);
616 e26eb4e7 Leszek Koltunski
    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 82eb152a Leszek Koltunski
    JSONArray  colors      = generateColors(object);
622 3c48fab9 Leszek Koltunski
    JSONObject solved      = generateSolved(object);
623 e26eb4e7 Leszek Koltunski
624 052e0362 Leszek Koltunski
    json.put("major"       , VERSION_OBJECT_MAJOR);
625
    json.put("minor"       , VERSION_OBJECT_MINOR);
626 e26eb4e7 Leszek Koltunski
    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 82eb152a Leszek Koltunski
    json.put("colors"      , colors);
633 3c48fab9 Leszek Koltunski
    json.put("solved"      , solved);
634 82eb152a Leszek Koltunski
635
    return json.toString();
636
    }
637 e26eb4e7 Leszek Koltunski
638 052e0362 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
639
640 4bf52f4f Leszek Koltunski
  public String createExtrasString(TwistyObject object) throws JSONException
641 052e0362 Leszek Koltunski
    {
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 4bf52f4f Leszek Koltunski
      json.put("major"     , VERSION_EXTRAS_MAJOR);
667
      json.put("minor"     , VERSION_EXTRAS_MINOR);
668 5f54927b Leszek Koltunski
      json.put("object"    , object.getShortName() );
669 052e0362 Leszek Koltunski
      json.put("tutorials" , tutorials);
670
671
      return json.toString();
672
      }
673
674
    return null;
675
    }
676
677 82eb152a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
678 e26eb4e7 Leszek Koltunski
679 82eb152a Leszek Koltunski
  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 e26eb4e7 Leszek Koltunski
    }
686
687
///////////////////////////////////////////////////////////////////////////////////////////////////
688
689
  public static JsonWriter getInstance()
690
    {
691
    if( mThis==null ) mThis = new JsonWriter();
692
    return mThis;
693
    }
694
}