少年修仙传客户端代码仓库
client_Wu Xijin
2019-06-13 033958214c0b16d7e7b93cc821b018c295251867
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/******************************************************************************
 * Spine Runtimes Software License v2.5
 *
 * Copyright (c) 2013-2016, Esoteric Software
 * All rights reserved.
 *
 * You are granted a perpetual, non-exclusive, non-sublicensable, and
 * non-transferable license to use, install, execute, and perform the Spine
 * Runtimes software and derivative works solely for personal or internal
 * use. Without the written permission of Esoteric Software (see Section 2 of
 * the Spine Software License Agreement), you may not (a) modify, translate,
 * adapt, or develop new applications using the Spine Runtimes or otherwise
 * create derivative works or improvements of the Spine Runtimes or (b) remove,
 * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
 * or other intellectual property or proprietary rights notices on or in the
 * Software, including any copy thereof. Redistributions in binary or source
 * form must include this license and terms.
 *
 * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
 * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *****************************************************************************/
 
#if (UNITY_5 || UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_WSA || UNITY_WP8 || UNITY_WP8_1)
#define IS_UNITY
#endif
 
using System;
using System.IO;
using System.Collections.Generic;
 
#if WINDOWS_STOREAPP
using System.Threading.Tasks;
using Windows.Storage;
#endif
 
namespace Spine {
    public class SkeletonJson {
        public float Scale { get; set; }
 
        private AttachmentLoader attachmentLoader;
        private List<LinkedMesh> linkedMeshes = new List<LinkedMesh>();
 
        public SkeletonJson (params Atlas[] atlasArray)
            : this(new AtlasAttachmentLoader(atlasArray)) {
        }
 
        public SkeletonJson (AttachmentLoader attachmentLoader) {
            if (attachmentLoader == null) throw new ArgumentNullException("attachmentLoader", "attachmentLoader cannot be null.");
            this.attachmentLoader = attachmentLoader;
            Scale = 1;
        }
 
        #if !IS_UNITY && WINDOWS_STOREAPP
        private async Task<SkeletonData> ReadFile(string path) {
            var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            var file = await folder.GetFileAsync(path).AsTask().ConfigureAwait(false);
            using (var reader = new StreamReader(await file.OpenStreamForReadAsync().ConfigureAwait(false))) {
                SkeletonData skeletonData = ReadSkeletonData(reader);
                skeletonData.Name = Path.GetFileNameWithoutExtension(path);
                return skeletonData;
            }
        }
 
        public SkeletonData ReadSkeletonData (String path) {
            return this.ReadFile(path).Result;
        }
        #else
        public SkeletonData ReadSkeletonData (String path) {
        #if WINDOWS_PHONE
            using (var reader = new StreamReader(Microsoft.Xna.Framework.TitleContainer.OpenStream(path))) {
        #else
            using (var reader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))) {
        #endif
                SkeletonData skeletonData = ReadSkeletonData(reader);
                skeletonData.name = Path.GetFileNameWithoutExtension(path);
                return skeletonData;
            }
        }
        #endif
 
        public SkeletonData ReadSkeletonData (TextReader reader) {
            if (reader == null) throw new ArgumentNullException("reader", "reader cannot be null.");
 
            var scale = this.Scale;
            var skeletonData = new SkeletonData();
 
            var root = Json.Deserialize(reader) as Dictionary<String, Object>;
            if (root == null) throw new Exception("Invalid JSON.");
 
            // Skeleton.
            if (root.ContainsKey("skeleton")) {
                var skeletonMap = (Dictionary<String, Object>)root["skeleton"];
                skeletonData.hash = (String)skeletonMap["hash"];
                skeletonData.version = (String)skeletonMap["spine"];
                skeletonData.width = GetFloat(skeletonMap, "width", 0);
                skeletonData.height = GetFloat(skeletonMap, "height", 0);
                skeletonData.fps = GetFloat(skeletonMap, "fps", 0);
                skeletonData.imagesPath = GetString(skeletonMap, "images", null);
            }
 
            // Bones.
            foreach (Dictionary<String, Object> boneMap in (List<Object>)root["bones"]) {
                BoneData parent = null;
                if (boneMap.ContainsKey("parent")) {
                    parent = skeletonData.FindBone((String)boneMap["parent"]);
                    if (parent == null)
                        throw new Exception("Parent bone not found: " + boneMap["parent"]);
                }
                var data = new BoneData(skeletonData.Bones.Count, (String)boneMap["name"], parent);
                data.length = GetFloat(boneMap, "length", 0) * scale;
                data.x = GetFloat(boneMap, "x", 0) * scale;
                data.y = GetFloat(boneMap, "y", 0) * scale;
                data.rotation = GetFloat(boneMap, "rotation", 0);
                data.scaleX = GetFloat(boneMap, "scaleX", 1);
                data.scaleY = GetFloat(boneMap, "scaleY", 1);
                data.shearX = GetFloat(boneMap, "shearX", 0);
                data.shearY = GetFloat(boneMap, "shearY", 0);
 
                string tm = GetString(boneMap, "transform", TransformMode.Normal.ToString());
                data.transformMode = (TransformMode)Enum.Parse(typeof(TransformMode), tm, true);
 
                skeletonData.bones.Add(data);
            }
 
            // Slots.
            if (root.ContainsKey("slots")) {
                foreach (Dictionary<String, Object> slotMap in (List<Object>)root["slots"]) {
                    var slotName = (String)slotMap["name"];
                    var boneName = (String)slotMap["bone"];
                    BoneData boneData = skeletonData.FindBone(boneName);
                    if (boneData == null) throw new Exception("Slot bone not found: " + boneName);
                    var data = new SlotData(skeletonData.Slots.Count, slotName, boneData);
 
                    if (slotMap.ContainsKey("color")) {
                        var color = (String)slotMap["color"];
                        data.r = ToColor(color, 0);
                        data.g = ToColor(color, 1);
                        data.b = ToColor(color, 2);
                        data.a = ToColor(color, 3);
                    }
                        
                    data.attachmentName = GetString(slotMap, "attachment", null);
                    if (slotMap.ContainsKey("blend"))
                        data.blendMode = (BlendMode)Enum.Parse(typeof(BlendMode), (String)slotMap["blend"], false);
                    else
                        data.blendMode = BlendMode.normal;
                    skeletonData.slots.Add(data);
                }
            }
 
            // IK constraints.
            if (root.ContainsKey("ik")) {
                foreach (Dictionary<String, Object> constraintMap in (List<Object>)root["ik"]) {
                    IkConstraintData data = new IkConstraintData((String)constraintMap["name"]);
                    data.order = GetInt(constraintMap, "order", 0);
 
                    foreach (String boneName in (List<Object>)constraintMap["bones"]) {
                        BoneData bone = skeletonData.FindBone(boneName);
                        if (bone == null) throw new Exception("IK constraint bone not found: " + boneName);
                        data.bones.Add(bone);
                    }
                    
                    String targetName = (String)constraintMap["target"];
                    data.target = skeletonData.FindBone(targetName);
                    if (data.target == null) throw new Exception("Target bone not found: " + targetName);
 
                    data.bendDirection = GetBoolean(constraintMap, "bendPositive", true) ? 1 : -1;
                    data.mix = GetFloat(constraintMap, "mix", 1);
 
                    skeletonData.ikConstraints.Add(data);
                }
            }
 
            // Transform constraints.
            if (root.ContainsKey("transform")) {
                foreach (Dictionary<String, Object> constraintMap in (List<Object>)root["transform"]) {
                    TransformConstraintData data = new TransformConstraintData((String)constraintMap["name"]);
                    data.order = GetInt(constraintMap, "order", 0);
 
                    foreach (String boneName in (List<Object>)constraintMap["bones"]) {
                        BoneData bone = skeletonData.FindBone(boneName);
                        if (bone == null) throw new Exception("Transform constraint bone not found: " + boneName);
                        data.bones.Add(bone);
                    }
 
                    String targetName = (String)constraintMap["target"];
                    data.target = skeletonData.FindBone(targetName);
                    if (data.target == null) throw new Exception("Target bone not found: " + targetName);
 
                    data.offsetRotation = GetFloat(constraintMap, "rotation", 0);
                    data.offsetX = GetFloat(constraintMap, "x", 0) * scale;
                    data.offsetY = GetFloat(constraintMap, "y", 0) * scale;
                    data.offsetScaleX = GetFloat(constraintMap, "scaleX", 0);
                    data.offsetScaleY = GetFloat(constraintMap, "scaleY", 0);
                    data.offsetShearY = GetFloat(constraintMap, "shearY", 0);
 
                    data.rotateMix = GetFloat(constraintMap, "rotateMix", 1);
                    data.translateMix = GetFloat(constraintMap, "translateMix", 1);
                    data.scaleMix = GetFloat(constraintMap, "scaleMix", 1);
                    data.shearMix = GetFloat(constraintMap, "shearMix", 1);
 
                    skeletonData.transformConstraints.Add(data);
                }
            }
 
            // Path constraints.
            if(root.ContainsKey("path")) {
                foreach (Dictionary<String, Object> constraintMap in (List<Object>)root["path"]) {
                    PathConstraintData data = new PathConstraintData((String)constraintMap["name"]);
                    data.order = GetInt(constraintMap, "order", 0);
 
                    foreach (String boneName in (List<Object>)constraintMap["bones"]) {
                        BoneData bone = skeletonData.FindBone(boneName);
                        if (bone == null) throw new Exception("Path bone not found: " + boneName);
                        data.bones.Add(bone);
                    }
 
                    String targetName = (String)constraintMap["target"];
                    data.target = skeletonData.FindSlot(targetName);
                    if (data.target == null) throw new Exception("Target slot not found: " + targetName);
 
                    data.positionMode = (PositionMode)Enum.Parse(typeof(PositionMode), GetString(constraintMap, "positionMode", "percent"), true);
                    data.spacingMode = (SpacingMode)Enum.Parse(typeof(SpacingMode), GetString(constraintMap, "spacingMode", "length"), true);
                    data.rotateMode = (RotateMode)Enum.Parse(typeof(RotateMode), GetString(constraintMap, "rotateMode", "tangent"), true);
                    data.offsetRotation = GetFloat(constraintMap, "rotation", 0);
                    data.position = GetFloat(constraintMap, "position", 0);
                    if (data.positionMode == PositionMode.Fixed) data.position *= scale;
                    data.spacing = GetFloat(constraintMap, "spacing", 0);
                    if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) data.spacing *= scale;
                    data.rotateMix = GetFloat(constraintMap, "rotateMix", 1);
                    data.translateMix = GetFloat(constraintMap, "translateMix", 1);
 
                    skeletonData.pathConstraints.Add(data);
                }
            }
 
            // Skins.
            if (root.ContainsKey("skins")) {
                    foreach (KeyValuePair<String, Object> skinMap in (Dictionary<String, Object>)root["skins"]) {
                    var skin = new Skin(skinMap.Key);
                    foreach (KeyValuePair<String, Object> slotEntry in (Dictionary<String, Object>)skinMap.Value) {
                        int slotIndex = skeletonData.FindSlotIndex(slotEntry.Key);
                        foreach (KeyValuePair<String, Object> entry in ((Dictionary<String, Object>)slotEntry.Value)) {
                            try {
                                Attachment attachment = ReadAttachment((Dictionary<String, Object>)entry.Value, skin, slotIndex, entry.Key);
                                if (attachment != null) skin.AddAttachment(slotIndex, entry.Key, attachment);
                            } catch (Exception e) {
                                throw new Exception("Error reading attachment: " + entry.Key + ", skin: " + skin, e);
                            }
                        } 
                    }
                    skeletonData.skins.Add(skin);
                    if (skin.name == "default") skeletonData.defaultSkin = skin;
                }
            }
 
            // Linked meshes.
            for (int i = 0, n = linkedMeshes.Count; i < n; i++) {
                LinkedMesh linkedMesh = linkedMeshes[i];
                Skin skin = linkedMesh.skin == null ? skeletonData.defaultSkin : skeletonData.FindSkin(linkedMesh.skin);
                if (skin == null) throw new Exception("Slot not found: " + linkedMesh.skin);
                Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent);
                if (parent == null) throw new Exception("Parent mesh not found: " + linkedMesh.parent);
                linkedMesh.mesh.ParentMesh = (MeshAttachment)parent;
                linkedMesh.mesh.UpdateUVs();
            }
            linkedMeshes.Clear();
 
            // Events.
            if (root.ContainsKey("events")) {
                foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["events"]) {
                    var entryMap = (Dictionary<String, Object>)entry.Value;
                    var data = new EventData(entry.Key);
                    data.Int = GetInt(entryMap, "int", 0);
                    data.Float = GetFloat(entryMap, "float", 0);
                    data.String = GetString(entryMap, "string", string.Empty);
                    skeletonData.events.Add(data);
                }
            }
 
            // Animations.
            if (root.ContainsKey("animations")) {
                foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["animations"]) {
                    try {
                        ReadAnimation((Dictionary<String, Object>)entry.Value, entry.Key, skeletonData);
                    } catch (Exception e) {
                        throw new Exception("Error reading animation: " + entry.Key, e);
                    }
                }   
            }
 
            skeletonData.bones.TrimExcess();
            skeletonData.slots.TrimExcess();
            skeletonData.skins.TrimExcess();
            skeletonData.events.TrimExcess();
            skeletonData.animations.TrimExcess();
            skeletonData.ikConstraints.TrimExcess();
            return skeletonData;
        }
 
        private Attachment ReadAttachment (Dictionary<String, Object> map, Skin skin, int slotIndex, String name) {
            var scale = this.Scale;
            name = GetString(map, "name", name);
 
            var typeName = GetString(map, "type", "region");
            if (typeName == "skinnedmesh") typeName = "weightedmesh";
            if (typeName == "weightedmesh") typeName = "mesh";
            if (typeName == "weightedlinkedmesh") typeName = "linkedmesh";
            var type = (AttachmentType)Enum.Parse(typeof(AttachmentType), typeName, true);
 
            String path = GetString(map, "path", name);
 
            switch (type) {
            case AttachmentType.Region:
                RegionAttachment region = attachmentLoader.NewRegionAttachment(skin, name, path);
                if (region == null) return null;
                region.Path = path;
                region.x = GetFloat(map, "x", 0) * scale;
                region.y = GetFloat(map, "y", 0) * scale;
                region.scaleX = GetFloat(map, "scaleX", 1);
                region.scaleY = GetFloat(map, "scaleY", 1);
                region.rotation = GetFloat(map, "rotation", 0);
                region.width = GetFloat(map, "width", 32) * scale;
                region.height = GetFloat(map, "height", 32) * scale;
                region.UpdateOffset();
 
                if (map.ContainsKey("color")) {
                    var color = (String)map["color"];
                    region.r = ToColor(color, 0);
                    region.g = ToColor(color, 1);
                    region.b = ToColor(color, 2);
                    region.a = ToColor(color, 3);
                }
 
                region.UpdateOffset();
                return region;
            case AttachmentType.Boundingbox:
                BoundingBoxAttachment box = attachmentLoader.NewBoundingBoxAttachment(skin, name);
                if (box == null) return null;
                ReadVertices(map, box, GetInt(map, "vertexCount", 0) << 1);
                return box;
            case AttachmentType.Mesh:
            case AttachmentType.Linkedmesh: {
                    MeshAttachment mesh = attachmentLoader.NewMeshAttachment(skin, name, path);
                    if (mesh == null) return null;
                    mesh.Path = path;
 
                    if (map.ContainsKey("color")) {
                        var color = (String)map["color"];
                        mesh.r = ToColor(color, 0);
                        mesh.g = ToColor(color, 1);
                        mesh.b = ToColor(color, 2);
                        mesh.a = ToColor(color, 3);
                    }
 
                    mesh.Width = GetFloat(map, "width", 0) * scale;
                    mesh.Height = GetFloat(map, "height", 0) * scale;
 
                    String parent = GetString(map, "parent", null);
                    if (parent != null) {
                        mesh.InheritDeform = GetBoolean(map, "deform", true);
                        linkedMeshes.Add(new LinkedMesh(mesh, GetString(map, "skin", null), slotIndex, parent));
                        return mesh;
                    }
 
                    float[] uvs = GetFloatArray(map, "uvs", 1);
                    ReadVertices(map, mesh, uvs.Length);
                    mesh.triangles = GetIntArray(map, "triangles");
                    mesh.regionUVs = uvs;
                    mesh.UpdateUVs();
 
                    if (map.ContainsKey("hull")) mesh.HullLength = GetInt(map, "hull", 0) * 2;
                    if (map.ContainsKey("edges")) mesh.Edges = GetIntArray(map, "edges");
                    return mesh;
                }
            case AttachmentType.Path: {
                    PathAttachment pathAttachment = attachmentLoader.NewPathAttachment(skin, name);
                    if (pathAttachment == null) return null;
                    pathAttachment.closed = GetBoolean(map, "closed", false);
                    pathAttachment.constantSpeed = GetBoolean(map, "constantSpeed", true);
 
                    int vertexCount = GetInt(map, "vertexCount", 0);
                    ReadVertices(map, pathAttachment, vertexCount << 1);
 
                    // potential BOZO see Java impl
                    pathAttachment.lengths = GetFloatArray(map, "lengths", scale);
                    return pathAttachment;
                }
            }
            return null;
        }
 
        private void ReadVertices (Dictionary<String, Object> map, VertexAttachment attachment, int verticesLength) {
            attachment.WorldVerticesLength = verticesLength;
            float[] vertices = GetFloatArray(map, "vertices", 1);
            float scale = Scale;
            if (verticesLength == vertices.Length) {
                if (scale != 1) {
                    for (int i = 0; i < vertices.Length; i++) {
                        vertices[i] *= scale;
                    }
                }
                attachment.vertices = vertices;
                return;
            }
            ExposedList<float> weights = new ExposedList<float>(verticesLength * 3 * 3);
            ExposedList<int> bones = new ExposedList<int>(verticesLength * 3);
            for (int i = 0, n = vertices.Length; i < n;) {
                int boneCount = (int)vertices[i++];
                bones.Add(boneCount);
                for (int nn = i + boneCount * 4; i < nn; i += 4) {
                    bones.Add((int)vertices[i]);
                    weights.Add(vertices[i + 1] * this.Scale);
                    weights.Add(vertices[i + 2] * this.Scale);
                    weights.Add(vertices[i + 3]);
                }
            }
            attachment.bones = bones.ToArray();
            attachment.vertices = weights.ToArray();
        }
 
        private void ReadAnimation (Dictionary<String, Object> map, String name, SkeletonData skeletonData) {
            var scale = this.Scale;
            var timelines = new ExposedList<Timeline>();
            float duration = 0;
 
            // Slot timelines.
            if (map.ContainsKey("slots")) {
                foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)map["slots"]) {
                    String slotName = entry.Key;
                    int slotIndex = skeletonData.FindSlotIndex(slotName);
                    var timelineMap = (Dictionary<String, Object>)entry.Value;
                    foreach (KeyValuePair<String, Object> timelineEntry in timelineMap) {
                        var values = (List<Object>)timelineEntry.Value;
                        var timelineName = (String)timelineEntry.Key;
                        if (timelineName == "color") {
                            var timeline = new ColorTimeline(values.Count);
                            timeline.slotIndex = slotIndex;
 
                            int frameIndex = 0;
                            foreach (Dictionary<String, Object> valueMap in values) {
                                float time = (float)valueMap["time"];
                                String c = (String)valueMap["color"];
                                timeline.SetFrame(frameIndex, time, ToColor(c, 0), ToColor(c, 1), ToColor(c, 2), ToColor(c, 3));
                                ReadCurve(valueMap, timeline, frameIndex);
                                frameIndex++;
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[(timeline.FrameCount - 1) * ColorTimeline.ENTRIES]);
 
                        } else if (timelineName == "attachment") {
                            var timeline = new AttachmentTimeline(values.Count);
                            timeline.slotIndex = slotIndex;
 
                            int frameIndex = 0;
                            foreach (Dictionary<String, Object> valueMap in values) {
                                float time = (float)valueMap["time"];
                                timeline.SetFrame(frameIndex++, time, (String)valueMap["name"]);
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[timeline.FrameCount - 1]);
 
                        } else
                            throw new Exception("Invalid timeline type for a slot: " + timelineName + " (" + slotName + ")");
                    }
                }
            }
 
            // Bone timelines.
            if (map.ContainsKey("bones")) {
                foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)map["bones"]) {
                    String boneName = entry.Key;
                    int boneIndex = skeletonData.FindBoneIndex(boneName);
                    if (boneIndex == -1) throw new Exception("Bone not found: " + boneName);
                    var timelineMap = (Dictionary<String, Object>)entry.Value;
                    foreach (KeyValuePair<String, Object> timelineEntry in timelineMap) {
                        var values = (List<Object>)timelineEntry.Value;
                        var timelineName = (String)timelineEntry.Key;
                        if (timelineName == "rotate") {
                            var timeline = new RotateTimeline(values.Count);
                            timeline.boneIndex = boneIndex;
 
                            int frameIndex = 0;
                            foreach (Dictionary<String, Object> valueMap in values) {
                                timeline.SetFrame(frameIndex, (float)valueMap["time"], (float)valueMap["angle"]);
                                ReadCurve(valueMap, timeline, frameIndex);
                                frameIndex++;
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[(timeline.FrameCount - 1) * RotateTimeline.ENTRIES]);
 
                        } else if (timelineName == "translate" || timelineName == "scale" || timelineName == "shear") {
                            TranslateTimeline timeline;
                            float timelineScale = 1;
                            if (timelineName == "scale")
                                timeline = new ScaleTimeline(values.Count);
                            else if (timelineName == "shear")
                                timeline = new ShearTimeline(values.Count);
                            else {
                                timeline = new TranslateTimeline(values.Count);
                                timelineScale = scale;
                            }
                            timeline.boneIndex = boneIndex;
 
                            int frameIndex = 0;
                            foreach (Dictionary<String, Object> valueMap in values) {
                                float time = (float)valueMap["time"];
                                float x = GetFloat(valueMap, "x", 0);
                                float y = GetFloat(valueMap, "y", 0);
                                timeline.SetFrame(frameIndex, time, x * timelineScale, y * timelineScale);
                                ReadCurve(valueMap, timeline, frameIndex);
                                frameIndex++;
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[(timeline.FrameCount - 1) * TranslateTimeline.ENTRIES]);
 
                        } else
                            throw new Exception("Invalid timeline type for a bone: " + timelineName + " (" + boneName + ")");
                    }
                }
            }
 
            // IK constraint timelines.
            if (map.ContainsKey("ik")) {
                foreach (KeyValuePair<String, Object> constraintMap in (Dictionary<String, Object>)map["ik"]) {
                    IkConstraintData constraint = skeletonData.FindIkConstraint(constraintMap.Key);
                    var values = (List<Object>)constraintMap.Value;
                    var timeline = new IkConstraintTimeline(values.Count);
                    timeline.ikConstraintIndex = skeletonData.ikConstraints.IndexOf(constraint);
                    int frameIndex = 0;
                    foreach (Dictionary<String, Object> valueMap in values) {
                        float time = (float)valueMap["time"];
                        float mix = GetFloat(valueMap, "mix", 1);
                        bool bendPositive = GetBoolean(valueMap, "bendPositive", true);
                        timeline.SetFrame(frameIndex, time, mix, bendPositive ? 1 : -1);
                        ReadCurve(valueMap, timeline, frameIndex);
                        frameIndex++;
                    }
                    timelines.Add(timeline);
                    duration = Math.Max(duration, timeline.frames[(timeline.FrameCount - 1) * IkConstraintTimeline.ENTRIES]);
                }
            }
 
            // Transform constraint timelines.
            if (map.ContainsKey("transform")) {
                foreach (KeyValuePair<String, Object> constraintMap in (Dictionary<String, Object>)map["transform"]) {
                    TransformConstraintData constraint = skeletonData.FindTransformConstraint(constraintMap.Key);
                    var values = (List<Object>)constraintMap.Value;
                    var timeline = new TransformConstraintTimeline(values.Count);
                    timeline.transformConstraintIndex = skeletonData.transformConstraints.IndexOf(constraint);
                    int frameIndex = 0;
                    foreach (Dictionary<String, Object> valueMap in values) {
                        float time = (float)valueMap["time"];
                        float rotateMix = GetFloat(valueMap, "rotateMix", 1);
                        float translateMix = GetFloat(valueMap, "translateMix", 1);
                        float scaleMix = GetFloat(valueMap, "scaleMix", 1);
                        float shearMix = GetFloat(valueMap, "shearMix", 1);
                        timeline.SetFrame(frameIndex, time, rotateMix, translateMix, scaleMix, shearMix);
                        ReadCurve(valueMap, timeline, frameIndex);
                        frameIndex++;
                    }
                    timelines.Add(timeline);
                    duration = Math.Max(duration, timeline.frames[(timeline.FrameCount - 1) * TransformConstraintTimeline.ENTRIES]);
                }
            }
 
            // Path constraint timelines.
            if (map.ContainsKey("paths")) {
                foreach (KeyValuePair<String, Object> constraintMap in (Dictionary<String, Object>)map["paths"]) {
                    int index = skeletonData.FindPathConstraintIndex(constraintMap.Key);
                    if (index == -1) throw new Exception("Path constraint not found: " + constraintMap.Key);
                    PathConstraintData data = skeletonData.pathConstraints.Items[index];
                    var timelineMap = (Dictionary<String, Object>)constraintMap.Value;
                    foreach (KeyValuePair<String, Object> timelineEntry in timelineMap) {
                        var values = (List<Object>)timelineEntry.Value;
                        var timelineName = (String)timelineEntry.Key;
                        if (timelineName == "position" || timelineName == "spacing") {
                            PathConstraintPositionTimeline timeline;
                            float timelineScale = 1;
                            if (timelineName == "spacing") {
                                timeline = new PathConstraintSpacingTimeline(values.Count);
                                if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) timelineScale = scale;
                            }
                            else {
                                timeline = new PathConstraintPositionTimeline(values.Count);
                                if (data.positionMode == PositionMode.Fixed) timelineScale = scale;
                            }
                            timeline.pathConstraintIndex = index;
                            int frameIndex = 0;
                            foreach (Dictionary<String, Object> valueMap in values) {
                                timeline.SetFrame(frameIndex, (float)valueMap["time"], GetFloat(valueMap, timelineName, 0) * timelineScale);
                                ReadCurve(valueMap, timeline, frameIndex);
                                frameIndex++;
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[(timeline.FrameCount - 1) * PathConstraintPositionTimeline.ENTRIES]);
                        }
                        else if (timelineName == "mix") {
                            PathConstraintMixTimeline timeline = new PathConstraintMixTimeline(values.Count);
                            timeline.pathConstraintIndex = index;
                            int frameIndex = 0;
                            foreach (Dictionary<String, Object> valueMap in values) {
                                timeline.SetFrame(frameIndex, (float)valueMap["time"], GetFloat(valueMap, "rotateMix", 1), GetFloat(valueMap, "translateMix", 1));
                                ReadCurve(valueMap, timeline, frameIndex);
                                frameIndex++;
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[(timeline.FrameCount - 1) * PathConstraintMixTimeline.ENTRIES]);
                        }
                    }
                }
            }
 
            // Deform timelines.
            if (map.ContainsKey("deform")) {
                foreach (KeyValuePair<String, Object> deformMap in (Dictionary<String, Object>)map["deform"]) {
                    Skin skin = skeletonData.FindSkin(deformMap.Key);
                    foreach (KeyValuePair<String, Object> slotMap in (Dictionary<String, Object>)deformMap.Value) {
                        int slotIndex = skeletonData.FindSlotIndex(slotMap.Key);
                        if (slotIndex == -1) throw new Exception("Slot not found: " + slotMap.Key);
                        foreach (KeyValuePair<String, Object> timelineMap in (Dictionary<String, Object>)slotMap.Value) {
                            var values = (List<Object>)timelineMap.Value;
                            VertexAttachment attachment = (VertexAttachment)skin.GetAttachment(slotIndex, timelineMap.Key);
                            if (attachment == null) throw new Exception("Deform attachment not found: " + timelineMap.Key);
                            bool weighted = attachment.bones != null;
                            float[] vertices = attachment.vertices;
                            int deformLength = weighted ? vertices.Length / 3 * 2 : vertices.Length;
 
                            var timeline = new DeformTimeline(values.Count);
                            timeline.slotIndex = slotIndex;
                            timeline.attachment = attachment;
                            
                            int frameIndex = 0;
                            foreach (Dictionary<String, Object> valueMap in values) {
                                float[] deform;
                                if (!valueMap.ContainsKey("vertices")) {
                                    deform = weighted ? new float[deformLength] : vertices;
                                } else {
                                    deform = new float[deformLength];
                                    int start = GetInt(valueMap, "offset", 0);
                                    float[] verticesValue = GetFloatArray(valueMap, "vertices", 1);
                                    Array.Copy(verticesValue, 0, deform, start, verticesValue.Length);
                                    if (scale != 1) {
                                        for (int i = start, n = i + verticesValue.Length; i < n; i++)
                                            deform[i] *= scale;
                                    }
 
                                    if (!weighted) {
                                        for (int i = 0; i < deformLength; i++)
                                            deform[i] += vertices[i];
                                    }
                                }
 
                                timeline.SetFrame(frameIndex, (float)valueMap["time"], deform);
                                ReadCurve(valueMap, timeline, frameIndex);
                                frameIndex++;
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[timeline.FrameCount - 1]);
                        }
                    }
                }
            }
 
            // Draw order timeline.
            if (map.ContainsKey("drawOrder") || map.ContainsKey("draworder")) {
                var values = (List<Object>)map[map.ContainsKey("drawOrder") ? "drawOrder" : "draworder"];
                var timeline = new DrawOrderTimeline(values.Count);
                int slotCount = skeletonData.slots.Count;
                int frameIndex = 0;
                foreach (Dictionary<String, Object> drawOrderMap in values) {
                    int[] drawOrder = null;
                    if (drawOrderMap.ContainsKey("offsets")) {
                        drawOrder = new int[slotCount];
                        for (int i = slotCount - 1; i >= 0; i--)
                            drawOrder[i] = -1;
                        var offsets = (List<Object>)drawOrderMap["offsets"];
                        int[] unchanged = new int[slotCount - offsets.Count];
                        int originalIndex = 0, unchangedIndex = 0;
                        foreach (Dictionary<String, Object> offsetMap in offsets) {
                            int slotIndex = skeletonData.FindSlotIndex((String)offsetMap["slot"]);
                            if (slotIndex == -1) throw new Exception("Slot not found: " + offsetMap["slot"]);
                            // Collect unchanged items.
                            while (originalIndex != slotIndex)
                                unchanged[unchangedIndex++] = originalIndex++;
                            // Set changed items.
                            int index = originalIndex + (int)(float)offsetMap["offset"];
                            drawOrder[index] = originalIndex++;
                        }
                        // Collect remaining unchanged items.
                        while (originalIndex < slotCount)
                            unchanged[unchangedIndex++] = originalIndex++;
                        // Fill in unchanged items.
                        for (int i = slotCount - 1; i >= 0; i--)
                            if (drawOrder[i] == -1) drawOrder[i] = unchanged[--unchangedIndex];
                    }
                    timeline.SetFrame(frameIndex++, (float)drawOrderMap["time"], drawOrder);
                }
                timelines.Add(timeline);
                duration = Math.Max(duration, timeline.frames[timeline.FrameCount - 1]);
            }
 
            // Event timeline.
            if (map.ContainsKey("events")) {
                var eventsMap = (List<Object>)map["events"];
                var timeline = new EventTimeline(eventsMap.Count);
                int frameIndex = 0;
                foreach (Dictionary<String, Object> eventMap in eventsMap) {
                    EventData eventData = skeletonData.FindEvent((String)eventMap["name"]);
                    if (eventData == null) throw new Exception("Event not found: " + eventMap["name"]);
                    var e = new Event((float)eventMap["time"], eventData);
                    e.Int = GetInt(eventMap, "int", eventData.Int);
                    e.Float = GetFloat(eventMap, "float", eventData.Float);
                    e.String = GetString(eventMap, "string", eventData.String);
                    timeline.SetFrame(frameIndex++, e);
                }
                timelines.Add(timeline);
                duration = Math.Max(duration, timeline.frames[timeline.FrameCount - 1]);
            }
 
            timelines.TrimExcess();
            skeletonData.animations.Add(new Animation(name, timelines, duration));
        }
 
        static void ReadCurve (Dictionary<String, Object> valueMap, CurveTimeline timeline, int frameIndex) {
            if (!valueMap.ContainsKey("curve"))
                return;
            Object curveObject = valueMap["curve"];
            if (curveObject.Equals("stepped"))
                timeline.SetStepped(frameIndex);
            else {
                var curve = curveObject as List<Object>;
                if (curve != null)
                    timeline.SetCurve(frameIndex, (float)curve[0], (float)curve[1], (float)curve[2], (float)curve[3]);
            }
        }
 
        internal class LinkedMesh {
            internal String parent, skin;
            internal int slotIndex;
            internal MeshAttachment mesh;
 
            public LinkedMesh (MeshAttachment mesh, String skin, int slotIndex, String parent) {
                this.mesh = mesh;
                this.skin = skin;
                this.slotIndex = slotIndex;
                this.parent = parent;
            }
        }
 
        static float[] GetFloatArray(Dictionary<String, Object> map, String name, float scale) {
            var list = (List<Object>)map[name];
            var values = new float[list.Count];
            if (scale == 1) {
                for (int i = 0, n = list.Count; i < n; i++)
                    values[i] = (float)list[i];
            } else {
                for (int i = 0, n = list.Count; i < n; i++)
                    values[i] = (float)list[i] * scale;
            }
            return values;
        }
 
        static int[] GetIntArray(Dictionary<String, Object> map, String name) {
            var list = (List<Object>)map[name];
            var values = new int[list.Count];
            for (int i = 0, n = list.Count; i < n; i++)
                values[i] = (int)(float)list[i];
            return values;
        }
 
        static float GetFloat(Dictionary<String, Object> map, String name, float defaultValue) {
            if (!map.ContainsKey(name))
                return defaultValue;
            return (float)map[name];
        }
 
        static int GetInt(Dictionary<String, Object> map, String name, int defaultValue) {
            if (!map.ContainsKey(name))
                return defaultValue;
            return (int)(float)map[name];
        }
 
        static bool GetBoolean(Dictionary<String, Object> map, String name, bool defaultValue) {
            if (!map.ContainsKey(name))
                return defaultValue;
            return (bool)map[name];
        }
 
        static String GetString(Dictionary<String, Object> map, String name, String defaultValue) {
            if (!map.ContainsKey(name))
                return defaultValue;
            return (String)map[name];
        }
 
        static float ToColor(String hexString, int colorIndex) {
            if (hexString.Length != 8)
                throw new ArgumentException("Color hexidecimal length must be 8, recieved: " + hexString, "hexString");
            return Convert.ToInt32(hexString.Substring(colorIndex * 2, 2), 16) / (float)255;
        }
    }
}