/**
* The MIT License (MIT)
*
* Copyright (c) 2012-2017 DragonBones team and other contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.IO;
using System.Collections.Generic;
namespace DragonBones
{
///
///
public class BinaryDataParser : ObjectDataParser
{
//JsonParse
public delegate object JsonParseDelegate(string json);
public static JsonParseDelegate jsonParseDelegate;
private int _binaryOffset;
private byte[] _binary;
private short[] _intArrayBuffer;
private float[] _floatArrayBuffer;
private short[] _frameIntArrayBuffer;
private float[] _frameFloatArrayBuffer;
private short[] _frameArrayBuffer;
private ushort[] _timelineArrayBuffer;
private TimelineData _ParseBinaryTimeline(TimelineType type, uint offset, TimelineData timelineData = null)
{
var timeline = timelineData != null ? timelineData : BaseObject.BorrowObject();
timeline.type = type;
timeline.offset = offset;
this._timeline = timeline;
var keyFrameCount = this._timelineArrayBuffer[timeline.offset + (int)BinaryOffset.TimelineKeyFrameCount];
if (keyFrameCount == 1)
{
timeline.frameIndicesOffset = -1;
}
else
{
// One more frame than animation.
var totalFrameCount = this._animation.frameCount + 1;
var frameIndices = this._data.frameIndices;
timeline.frameIndicesOffset = frameIndices.Count;
frameIndices.ResizeList(frameIndices.Count + (int)totalFrameCount);
for (int i = 0, iK = 0, frameStart = 0, frameCount = 0; i < totalFrameCount; ++i)
{
if (frameStart + frameCount <= i && iK < keyFrameCount)
{
frameStart = this._frameArrayBuffer[this._animation.frameOffset + this._timelineArrayBuffer[timeline.offset + (int)BinaryOffset.TimelineFrameOffset + iK]];
if (iK == keyFrameCount - 1)
{
frameCount = (int)this._animation.frameCount - frameStart;
}
else
{
frameCount = this._frameArrayBuffer[this._animation.frameOffset + this._timelineArrayBuffer[timeline.offset + (int)BinaryOffset.TimelineFrameOffset + iK + 1]] - frameStart;
}
iK++;
}
frameIndices[timeline.frameIndicesOffset + i] = (uint)(iK - 1);
}
}
this._timeline = null; //
return timeline;
}
private void _ParseVertices(Dictionary rawData, VerticesData vertices)
{
vertices.offset = int.Parse(rawData[DataParser.OFFSET].ToString());
var weightOffset = this._intArrayBuffer[vertices.offset + (int)BinaryOffset.MeshWeightOffset];
if (weightOffset >= 0)
{
var weight = BaseObject.BorrowObject();
var vertexCount = this._intArrayBuffer[vertices.offset + (int)BinaryOffset.MeshVertexCount];
var boneCount = this._intArrayBuffer[weightOffset + (int)BinaryOffset.WeigthBoneCount];
weight.offset = weightOffset;
for (int i = 0; i < boneCount; ++i)
{
var boneIndex = this._intArrayBuffer[weightOffset + (int)BinaryOffset.WeigthBoneIndices + i];
weight.AddBone(this._rawBones[boneIndex]);
}
var boneIndicesOffset = weightOffset + (short)BinaryOffset.WeigthBoneIndices + boneCount;
var weightCount = 0;
for (int i = 0, l = vertexCount; i < l; ++i)
{
var vertexBoneCount = this._intArrayBuffer[boneIndicesOffset++];
weightCount += vertexBoneCount;
boneIndicesOffset += vertexBoneCount;
}
weight.count = weightCount;
vertices.weight = weight;
}
}
protected override void _ParseMesh(Dictionary rawData, MeshDisplayData mesh)
{
this._ParseVertices(rawData, mesh.vertices);
}
protected override AnimationData _ParseAnimation(Dictionary rawData)
{
var animation = BaseObject.BorrowObject();
animation.frameCount = (uint)Math.Max(ObjectDataParser._GetNumber(rawData, DataParser.DURATION, 1), 1);
animation.playTimes = (uint)ObjectDataParser._GetNumber(rawData, DataParser.PLAY_TIMES, 1);
animation.duration = (float)animation.frameCount / (float)this._armature.frameRate;//Must float
animation.fadeInTime = ObjectDataParser._GetNumber(rawData, DataParser.FADE_IN_TIME, 0.0f);
animation.scale = ObjectDataParser._GetNumber(rawData, DataParser.SCALE, 1.0f);
animation.name = ObjectDataParser._GetString(rawData, DataParser.NAME, DataParser.DEFAULT_NAME);
if (animation.name.Length == 0)
{
animation.name = DataParser.DEFAULT_NAME;
}
// Offsets.
var offsets = rawData[DataParser.OFFSET] as List