SkeletonBinary.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #if (UNITY_5 || UNITY_5_3_OR_NEWER || UNITY_WSA || UNITY_WP8 || UNITY_WP8_1)
  30. #define IS_UNITY
  31. #endif
  32. using System;
  33. using System.IO;
  34. using System.Collections.Generic;
  35. #if WINDOWS_STOREAPP
  36. using System.Threading.Tasks;
  37. using Windows.Storage;
  38. #endif
  39. namespace Spine {
  40. public class SkeletonBinary {
  41. public const int BONE_ROTATE = 0;
  42. public const int BONE_TRANSLATE = 1;
  43. public const int BONE_SCALE = 2;
  44. public const int BONE_SHEAR = 3;
  45. public const int SLOT_ATTACHMENT = 0;
  46. public const int SLOT_COLOR = 1;
  47. public const int SLOT_TWO_COLOR = 2;
  48. public const int PATH_POSITION = 0;
  49. public const int PATH_SPACING = 1;
  50. public const int PATH_MIX = 2;
  51. public const int CURVE_LINEAR = 0;
  52. public const int CURVE_STEPPED = 1;
  53. public const int CURVE_BEZIER = 2;
  54. public float Scale { get; set; }
  55. private AttachmentLoader attachmentLoader;
  56. private List<SkeletonJson.LinkedMesh> linkedMeshes = new List<SkeletonJson.LinkedMesh>();
  57. public SkeletonBinary (params Atlas[] atlasArray)
  58. : this(new AtlasAttachmentLoader(atlasArray)) {
  59. }
  60. public SkeletonBinary (AttachmentLoader attachmentLoader) {
  61. if (attachmentLoader == null) throw new ArgumentNullException("attachmentLoader");
  62. this.attachmentLoader = attachmentLoader;
  63. Scale = 1;
  64. }
  65. #if !ISUNITY && WINDOWS_STOREAPP
  66. private async Task<SkeletonData> ReadFile(string path) {
  67. var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
  68. using (var input = new BufferedStream(await folder.GetFileAsync(path).AsTask().ConfigureAwait(false))) {
  69. SkeletonData skeletonData = ReadSkeletonData(input);
  70. skeletonData.Name = Path.GetFileNameWithoutExtension(path);
  71. return skeletonData;
  72. }
  73. }
  74. public SkeletonData ReadSkeletonData (String path) {
  75. return this.ReadFile(path).Result;
  76. }
  77. #else
  78. public SkeletonData ReadSkeletonData (String path) {
  79. #if WINDOWS_PHONE
  80. using (var input = new BufferedStream(Microsoft.Xna.Framework.TitleContainer.OpenStream(path))) {
  81. #else
  82. using (var input = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) {
  83. #endif
  84. SkeletonData skeletonData = ReadSkeletonData(input);
  85. skeletonData.name = Path.GetFileNameWithoutExtension(path);
  86. return skeletonData;
  87. }
  88. }
  89. #endif // WINDOWS_STOREAPP
  90. public static readonly TransformMode[] TransformModeValues = {
  91. TransformMode.Normal,
  92. TransformMode.OnlyTranslation,
  93. TransformMode.NoRotationOrReflection,
  94. TransformMode.NoScale,
  95. TransformMode.NoScaleOrReflection
  96. };
  97. /// <summary>Returns the version string of binary skeleton data.</summary>
  98. public static string GetVersionString (Stream file) {
  99. if (file == null) throw new ArgumentNullException("file");
  100. SkeletonInput input = new SkeletonInput(file);
  101. return input.GetVersionString();
  102. }
  103. public SkeletonData ReadSkeletonData (Stream file) {
  104. if (file == null) throw new ArgumentNullException("file");
  105. float scale = Scale;
  106. var skeletonData = new SkeletonData();
  107. SkeletonInput input = new SkeletonInput(file);
  108. skeletonData.hash = input.ReadString();
  109. if (skeletonData.hash.Length == 0) skeletonData.hash = null;
  110. skeletonData.version = input.ReadString();
  111. if (skeletonData.version.Length == 0) skeletonData.version = null;
  112. if ("3.8.75" == skeletonData.version)
  113. throw new Exception("Unsupported skeleton data, please export with a newer version of Spine.");
  114. skeletonData.x = input.ReadFloat();
  115. skeletonData.y = input.ReadFloat();
  116. skeletonData.width = input.ReadFloat();
  117. skeletonData.height = input.ReadFloat();
  118. bool nonessential = input.ReadBoolean();
  119. if (nonessential) {
  120. skeletonData.fps = input.ReadFloat();
  121. skeletonData.imagesPath = input.ReadString();
  122. if (string.IsNullOrEmpty(skeletonData.imagesPath)) skeletonData.imagesPath = null;
  123. skeletonData.audioPath = input.ReadString();
  124. if (string.IsNullOrEmpty(skeletonData.audioPath)) skeletonData.audioPath = null;
  125. }
  126. int n;
  127. Object[] o;
  128. // Strings.
  129. input.strings = new ExposedList<string>(n = input.ReadInt(true));
  130. o = input.strings.Resize(n).Items;
  131. for (int i = 0; i < n; i++)
  132. o[i] = input.ReadString();
  133. // Bones.
  134. o = skeletonData.bones.Resize(n = input.ReadInt(true)).Items;
  135. for (int i = 0; i < n; i++) {
  136. String name = input.ReadString();
  137. BoneData parent = i == 0 ? null : skeletonData.bones.Items[input.ReadInt(true)];
  138. BoneData data = new BoneData(i, name, parent);
  139. data.rotation = input.ReadFloat();
  140. data.x = input.ReadFloat() * scale;
  141. data.y = input.ReadFloat() * scale;
  142. data.scaleX = input.ReadFloat();
  143. data.scaleY = input.ReadFloat();
  144. data.shearX = input.ReadFloat();
  145. data.shearY = input.ReadFloat();
  146. data.length = input.ReadFloat() * scale;
  147. data.transformMode = TransformModeValues[input.ReadInt(true)];
  148. data.skinRequired = input.ReadBoolean();
  149. if (nonessential) input.ReadInt(); // Skip bone color.
  150. o[i] = data;
  151. }
  152. // Slots.
  153. o = skeletonData.slots.Resize(n = input.ReadInt(true)).Items;
  154. for (int i = 0; i < n; i++) {
  155. String slotName = input.ReadString();
  156. BoneData boneData = skeletonData.bones.Items[input.ReadInt(true)];
  157. SlotData slotData = new SlotData(i, slotName, boneData);
  158. int color = input.ReadInt();
  159. slotData.r = ((color & 0xff000000) >> 24) / 255f;
  160. slotData.g = ((color & 0x00ff0000) >> 16) / 255f;
  161. slotData.b = ((color & 0x0000ff00) >> 8) / 255f;
  162. slotData.a = ((color & 0x000000ff)) / 255f;
  163. int darkColor = input.ReadInt(); // 0x00rrggbb
  164. if (darkColor != -1) {
  165. slotData.hasSecondColor = true;
  166. slotData.r2 = ((darkColor & 0x00ff0000) >> 16) / 255f;
  167. slotData.g2 = ((darkColor & 0x0000ff00) >> 8) / 255f;
  168. slotData.b2 = ((darkColor & 0x000000ff)) / 255f;
  169. }
  170. slotData.attachmentName = input.ReadStringRef();
  171. slotData.blendMode = (BlendMode)input.ReadInt(true);
  172. o[i] = slotData;
  173. }
  174. // IK constraints.
  175. o = skeletonData.ikConstraints.Resize(n = input.ReadInt(true)).Items;
  176. for (int i = 0, nn; i < n; i++) {
  177. IkConstraintData data = new IkConstraintData(input.ReadString());
  178. data.order = input.ReadInt(true);
  179. data.skinRequired = input.ReadBoolean();
  180. Object[] bones = data.bones.Resize(nn = input.ReadInt(true)).Items;
  181. for (int ii = 0; ii < nn; ii++)
  182. bones[ii] = skeletonData.bones.Items[input.ReadInt(true)];
  183. data.target = skeletonData.bones.Items[input.ReadInt(true)];
  184. data.mix = input.ReadFloat();
  185. data.softness = input.ReadFloat() * scale;
  186. data.bendDirection = input.ReadSByte();
  187. data.compress = input.ReadBoolean();
  188. data.stretch = input.ReadBoolean();
  189. data.uniform = input.ReadBoolean();
  190. o[i] = data;
  191. }
  192. // Transform constraints.
  193. o = skeletonData.transformConstraints.Resize(n = input.ReadInt(true)).Items;
  194. for (int i = 0, nn; i < n; i++) {
  195. TransformConstraintData data = new TransformConstraintData(input.ReadString());
  196. data.order = input.ReadInt(true);
  197. data.skinRequired = input.ReadBoolean();
  198. Object[] bones = data.bones.Resize(nn = input.ReadInt(true)).Items;
  199. for (int ii = 0; ii < nn; ii++)
  200. bones[ii] = skeletonData.bones.Items[input.ReadInt(true)];
  201. data.target = skeletonData.bones.Items[input.ReadInt(true)];
  202. data.local = input.ReadBoolean();
  203. data.relative = input.ReadBoolean();
  204. data.offsetRotation = input.ReadFloat();
  205. data.offsetX = input.ReadFloat() * scale;
  206. data.offsetY = input.ReadFloat() * scale;
  207. data.offsetScaleX = input.ReadFloat();
  208. data.offsetScaleY = input.ReadFloat();
  209. data.offsetShearY = input.ReadFloat();
  210. data.rotateMix = input.ReadFloat();
  211. data.translateMix = input.ReadFloat();
  212. data.scaleMix = input.ReadFloat();
  213. data.shearMix = input.ReadFloat();
  214. o[i] = data;
  215. }
  216. // Path constraints
  217. o = skeletonData.pathConstraints.Resize(n = input.ReadInt(true)).Items;
  218. for (int i = 0, nn; i < n; i++) {
  219. PathConstraintData data = new PathConstraintData(input.ReadString());
  220. data.order = input.ReadInt(true);
  221. data.skinRequired = input.ReadBoolean();
  222. Object[] bones = data.bones.Resize(nn = input.ReadInt(true)).Items;
  223. for (int ii = 0; ii < nn; ii++)
  224. bones[ii] = skeletonData.bones.Items[input.ReadInt(true)];
  225. data.target = skeletonData.slots.Items[input.ReadInt(true)];
  226. data.positionMode = (PositionMode)Enum.GetValues(typeof(PositionMode)).GetValue(input.ReadInt(true));
  227. data.spacingMode = (SpacingMode)Enum.GetValues(typeof(SpacingMode)).GetValue(input.ReadInt(true));
  228. data.rotateMode = (RotateMode)Enum.GetValues(typeof(RotateMode)).GetValue(input.ReadInt(true));
  229. data.offsetRotation = input.ReadFloat();
  230. data.position = input.ReadFloat();
  231. if (data.positionMode == PositionMode.Fixed) data.position *= scale;
  232. data.spacing = input.ReadFloat();
  233. if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) data.spacing *= scale;
  234. data.rotateMix = input.ReadFloat();
  235. data.translateMix = input.ReadFloat();
  236. o[i] = data;
  237. }
  238. // Default skin.
  239. Skin defaultSkin = ReadSkin(input, skeletonData, true, nonessential);
  240. if (defaultSkin != null) {
  241. skeletonData.defaultSkin = defaultSkin;
  242. skeletonData.skins.Add(defaultSkin);
  243. }
  244. // Skins.
  245. {
  246. int i = skeletonData.skins.Count;
  247. o = skeletonData.skins.Resize(n = i + input.ReadInt(true)).Items;
  248. for (; i < n; i++)
  249. o[i] = ReadSkin(input, skeletonData, false, nonessential);
  250. }
  251. // Linked meshes.
  252. n = linkedMeshes.Count;
  253. for (int i = 0; i < n; i++) {
  254. SkeletonJson.LinkedMesh linkedMesh = linkedMeshes[i];
  255. Skin skin = linkedMesh.skin == null ? skeletonData.DefaultSkin : skeletonData.FindSkin(linkedMesh.skin);
  256. if (skin == null) throw new Exception("Skin not found: " + linkedMesh.skin);
  257. Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent);
  258. if (parent == null) throw new Exception("Parent mesh not found: " + linkedMesh.parent);
  259. linkedMesh.mesh.DeformAttachment = linkedMesh.inheritDeform ? (VertexAttachment)parent : linkedMesh.mesh;
  260. linkedMesh.mesh.ParentMesh = (MeshAttachment)parent;
  261. linkedMesh.mesh.UpdateUVs();
  262. }
  263. linkedMeshes.Clear();
  264. // Events.
  265. o = skeletonData.events.Resize(n = input.ReadInt(true)).Items;
  266. for (int i = 0; i < n; i++) {
  267. EventData data = new EventData(input.ReadStringRef());
  268. data.Int = input.ReadInt(false);
  269. data.Float = input.ReadFloat();
  270. data.String = input.ReadString();
  271. data.AudioPath = input.ReadString();
  272. if (data.AudioPath != null) {
  273. data.Volume = input.ReadFloat();
  274. data.Balance = input.ReadFloat();
  275. }
  276. o[i] = data;
  277. }
  278. // Animations.
  279. o = skeletonData.animations.Resize(n = input.ReadInt(true)).Items;
  280. for (int i = 0; i < n; i++)
  281. o[i] = ReadAnimation(input.ReadString(), input, skeletonData);
  282. return skeletonData;
  283. }
  284. /// <returns>May be null.</returns>
  285. private Skin ReadSkin (SkeletonInput input, SkeletonData skeletonData, bool defaultSkin, bool nonessential) {
  286. Skin skin;
  287. int slotCount;
  288. if (defaultSkin) {
  289. slotCount = input.ReadInt(true);
  290. if (slotCount == 0) return null;
  291. skin = new Skin("default");
  292. } else {
  293. skin = new Skin(input.ReadStringRef());
  294. Object[] bones = skin.bones.Resize(input.ReadInt(true)).Items;
  295. for (int i = 0, n = skin.bones.Count; i < n; i++)
  296. bones[i] = skeletonData.bones.Items[input.ReadInt(true)];
  297. for (int i = 0, n = input.ReadInt(true); i < n; i++)
  298. skin.constraints.Add(skeletonData.ikConstraints.Items[input.ReadInt(true)]);
  299. for (int i = 0, n = input.ReadInt(true); i < n; i++)
  300. skin.constraints.Add(skeletonData.transformConstraints.Items[input.ReadInt(true)]);
  301. for (int i = 0, n = input.ReadInt(true); i < n; i++)
  302. skin.constraints.Add(skeletonData.pathConstraints.Items[input.ReadInt(true)]);
  303. skin.constraints.TrimExcess();
  304. slotCount = input.ReadInt(true);
  305. }
  306. for (int i = 0; i < slotCount; i++) {
  307. int slotIndex = input.ReadInt(true);
  308. for (int ii = 0, nn = input.ReadInt(true); ii < nn; ii++) {
  309. String name = input.ReadStringRef();
  310. Attachment attachment = ReadAttachment(input, skeletonData, skin, slotIndex, name, nonessential);
  311. if (attachment != null) skin.SetAttachment(slotIndex, name, attachment);
  312. }
  313. }
  314. return skin;
  315. }
  316. private Attachment ReadAttachment (SkeletonInput input, SkeletonData skeletonData, Skin skin, int slotIndex,
  317. String attachmentName, bool nonessential) {
  318. float scale = Scale;
  319. String name = input.ReadStringRef();
  320. if (name == null) name = attachmentName;
  321. AttachmentType type = (AttachmentType)input.ReadByte();
  322. switch (type) {
  323. case AttachmentType.Region: {
  324. String path = input.ReadStringRef();
  325. float rotation = input.ReadFloat();
  326. float x = input.ReadFloat();
  327. float y = input.ReadFloat();
  328. float scaleX = input.ReadFloat();
  329. float scaleY = input.ReadFloat();
  330. float width = input.ReadFloat();
  331. float height = input.ReadFloat();
  332. int color = input.ReadInt();
  333. if (path == null) path = name;
  334. RegionAttachment region = attachmentLoader.NewRegionAttachment(skin, name, path);
  335. if (region == null) return null;
  336. region.Path = path;
  337. region.x = x * scale;
  338. region.y = y * scale;
  339. region.scaleX = scaleX;
  340. region.scaleY = scaleY;
  341. region.rotation = rotation;
  342. region.width = width * scale;
  343. region.height = height * scale;
  344. region.r = ((color & 0xff000000) >> 24) / 255f;
  345. region.g = ((color & 0x00ff0000) >> 16) / 255f;
  346. region.b = ((color & 0x0000ff00) >> 8) / 255f;
  347. region.a = ((color & 0x000000ff)) / 255f;
  348. region.UpdateOffset();
  349. return region;
  350. }
  351. case AttachmentType.Boundingbox: {
  352. int vertexCount = input.ReadInt(true);
  353. Vertices vertices = ReadVertices(input, vertexCount);
  354. if (nonessential) input.ReadInt(); //int color = nonessential ? input.ReadInt() : 0; // Avoid unused local warning.
  355. BoundingBoxAttachment box = attachmentLoader.NewBoundingBoxAttachment(skin, name);
  356. if (box == null) return null;
  357. box.worldVerticesLength = vertexCount << 1;
  358. box.vertices = vertices.vertices;
  359. box.bones = vertices.bones;
  360. // skipped porting: if (nonessential) Color.rgba8888ToColor(box.getColor(), color);
  361. return box;
  362. }
  363. case AttachmentType.Mesh: {
  364. String path = input.ReadStringRef();
  365. int color = input.ReadInt();
  366. int vertexCount = input.ReadInt(true);
  367. float[] uvs = ReadFloatArray(input, vertexCount << 1, 1);
  368. int[] triangles = ReadShortArray(input);
  369. Vertices vertices = ReadVertices(input, vertexCount);
  370. int hullLength = input.ReadInt(true);
  371. int[] edges = null;
  372. float width = 0, height = 0;
  373. if (nonessential) {
  374. edges = ReadShortArray(input);
  375. width = input.ReadFloat();
  376. height = input.ReadFloat();
  377. }
  378. if (path == null) path = name;
  379. MeshAttachment mesh = attachmentLoader.NewMeshAttachment(skin, name, path);
  380. if (mesh == null) return null;
  381. mesh.Path = path;
  382. mesh.r = ((color & 0xff000000) >> 24) / 255f;
  383. mesh.g = ((color & 0x00ff0000) >> 16) / 255f;
  384. mesh.b = ((color & 0x0000ff00) >> 8) / 255f;
  385. mesh.a = ((color & 0x000000ff)) / 255f;
  386. mesh.bones = vertices.bones;
  387. mesh.vertices = vertices.vertices;
  388. mesh.WorldVerticesLength = vertexCount << 1;
  389. mesh.triangles = triangles;
  390. mesh.regionUVs = uvs;
  391. mesh.UpdateUVs();
  392. mesh.HullLength = hullLength << 1;
  393. if (nonessential) {
  394. mesh.Edges = edges;
  395. mesh.Width = width * scale;
  396. mesh.Height = height * scale;
  397. }
  398. return mesh;
  399. }
  400. case AttachmentType.Linkedmesh: {
  401. String path = input.ReadStringRef();
  402. int color = input.ReadInt();
  403. String skinName = input.ReadStringRef();
  404. String parent = input.ReadStringRef();
  405. bool inheritDeform = input.ReadBoolean();
  406. float width = 0, height = 0;
  407. if (nonessential) {
  408. width = input.ReadFloat();
  409. height = input.ReadFloat();
  410. }
  411. if (path == null) path = name;
  412. MeshAttachment mesh = attachmentLoader.NewMeshAttachment(skin, name, path);
  413. if (mesh == null) return null;
  414. mesh.Path = path;
  415. mesh.r = ((color & 0xff000000) >> 24) / 255f;
  416. mesh.g = ((color & 0x00ff0000) >> 16) / 255f;
  417. mesh.b = ((color & 0x0000ff00) >> 8) / 255f;
  418. mesh.a = ((color & 0x000000ff)) / 255f;
  419. if (nonessential) {
  420. mesh.Width = width * scale;
  421. mesh.Height = height * scale;
  422. }
  423. linkedMeshes.Add(new SkeletonJson.LinkedMesh(mesh, skinName, slotIndex, parent, inheritDeform));
  424. return mesh;
  425. }
  426. case AttachmentType.Path: {
  427. bool closed = input.ReadBoolean();
  428. bool constantSpeed = input.ReadBoolean();
  429. int vertexCount = input.ReadInt(true);
  430. Vertices vertices = ReadVertices(input, vertexCount);
  431. float[] lengths = new float[vertexCount / 3];
  432. for (int i = 0, n = lengths.Length; i < n; i++)
  433. lengths[i] = input.ReadFloat() * scale;
  434. if (nonessential) input.ReadInt(); //int color = nonessential ? input.ReadInt() : 0;
  435. PathAttachment path = attachmentLoader.NewPathAttachment(skin, name);
  436. if (path == null) return null;
  437. path.closed = closed;
  438. path.constantSpeed = constantSpeed;
  439. path.worldVerticesLength = vertexCount << 1;
  440. path.vertices = vertices.vertices;
  441. path.bones = vertices.bones;
  442. path.lengths = lengths;
  443. // skipped porting: if (nonessential) Color.rgba8888ToColor(path.getColor(), color);
  444. return path;
  445. }
  446. case AttachmentType.Point: {
  447. float rotation = input.ReadFloat();
  448. float x = input.ReadFloat();
  449. float y = input.ReadFloat();
  450. if (nonessential) input.ReadInt(); //int color = nonessential ? input.ReadInt() : 0;
  451. PointAttachment point = attachmentLoader.NewPointAttachment(skin, name);
  452. if (point == null) return null;
  453. point.x = x * scale;
  454. point.y = y * scale;
  455. point.rotation = rotation;
  456. // skipped porting: if (nonessential) point.color = color;
  457. return point;
  458. }
  459. case AttachmentType.Clipping: {
  460. int endSlotIndex = input.ReadInt(true);
  461. int vertexCount = input.ReadInt(true);
  462. Vertices vertices = ReadVertices(input, vertexCount);
  463. if (nonessential) input.ReadInt();
  464. ClippingAttachment clip = attachmentLoader.NewClippingAttachment(skin, name);
  465. if (clip == null) return null;
  466. clip.EndSlot = skeletonData.slots.Items[endSlotIndex];
  467. clip.worldVerticesLength = vertexCount << 1;
  468. clip.vertices = vertices.vertices;
  469. clip.bones = vertices.bones;
  470. // skipped porting: if (nonessential) Color.rgba8888ToColor(clip.getColor(), color);
  471. return clip;
  472. }
  473. }
  474. return null;
  475. }
  476. private Vertices ReadVertices (SkeletonInput input, int vertexCount) {
  477. float scale = Scale;
  478. int verticesLength = vertexCount << 1;
  479. Vertices vertices = new Vertices();
  480. if(!input.ReadBoolean()) {
  481. vertices.vertices = ReadFloatArray(input, verticesLength, scale);
  482. return vertices;
  483. }
  484. var weights = new ExposedList<float>(verticesLength * 3 * 3);
  485. var bonesArray = new ExposedList<int>(verticesLength * 3);
  486. for (int i = 0; i < vertexCount; i++) {
  487. int boneCount = input.ReadInt(true);
  488. bonesArray.Add(boneCount);
  489. for (int ii = 0; ii < boneCount; ii++) {
  490. bonesArray.Add(input.ReadInt(true));
  491. weights.Add(input.ReadFloat() * scale);
  492. weights.Add(input.ReadFloat() * scale);
  493. weights.Add(input.ReadFloat());
  494. }
  495. }
  496. vertices.vertices = weights.ToArray();
  497. vertices.bones = bonesArray.ToArray();
  498. return vertices;
  499. }
  500. private float[] ReadFloatArray (SkeletonInput input, int n, float scale) {
  501. float[] array = new float[n];
  502. if (scale == 1) {
  503. for (int i = 0; i < n; i++)
  504. array[i] = input.ReadFloat();
  505. } else {
  506. for (int i = 0; i < n; i++)
  507. array[i] = input.ReadFloat() * scale;
  508. }
  509. return array;
  510. }
  511. private int[] ReadShortArray (SkeletonInput input) {
  512. int n = input.ReadInt(true);
  513. int[] array = new int[n];
  514. for (int i = 0; i < n; i++)
  515. array[i] = (input.ReadByte() << 8) | input.ReadByte();
  516. return array;
  517. }
  518. private Animation ReadAnimation (String name, SkeletonInput input, SkeletonData skeletonData) {
  519. var timelines = new ExposedList<Timeline>(32);
  520. float scale = Scale;
  521. float duration = 0;
  522. // Slot timelines.
  523. for (int i = 0, n = input.ReadInt(true); i < n; i++) {
  524. int slotIndex = input.ReadInt(true);
  525. for (int ii = 0, nn = input.ReadInt(true); ii < nn; ii++) {
  526. int timelineType = input.ReadByte();
  527. int frameCount = input.ReadInt(true);
  528. switch (timelineType) {
  529. case SLOT_ATTACHMENT: {
  530. AttachmentTimeline timeline = new AttachmentTimeline(frameCount);
  531. timeline.slotIndex = slotIndex;
  532. for (int frameIndex = 0; frameIndex < frameCount; frameIndex++)
  533. timeline.SetFrame(frameIndex, input.ReadFloat(), input.ReadStringRef());
  534. timelines.Add(timeline);
  535. duration = Math.Max(duration, timeline.frames[frameCount - 1]);
  536. break;
  537. }
  538. case SLOT_COLOR: {
  539. ColorTimeline timeline = new ColorTimeline(frameCount);
  540. timeline.slotIndex = slotIndex;
  541. for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
  542. float time = input.ReadFloat();
  543. int color = input.ReadInt();
  544. float r = ((color & 0xff000000) >> 24) / 255f;
  545. float g = ((color & 0x00ff0000) >> 16) / 255f;
  546. float b = ((color & 0x0000ff00) >> 8) / 255f;
  547. float a = ((color & 0x000000ff)) / 255f;
  548. timeline.SetFrame(frameIndex, time, r, g, b, a);
  549. if (frameIndex < frameCount - 1) ReadCurve(input, frameIndex, timeline);
  550. }
  551. timelines.Add(timeline);
  552. duration = Math.Max(duration, timeline.frames[(frameCount - 1) * ColorTimeline.ENTRIES]);
  553. break;
  554. }
  555. case SLOT_TWO_COLOR: {
  556. TwoColorTimeline timeline = new TwoColorTimeline(frameCount);
  557. timeline.slotIndex = slotIndex;
  558. for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
  559. float time = input.ReadFloat();
  560. int color = input.ReadInt();
  561. float r = ((color & 0xff000000) >> 24) / 255f;
  562. float g = ((color & 0x00ff0000) >> 16) / 255f;
  563. float b = ((color & 0x0000ff00) >> 8) / 255f;
  564. float a = ((color & 0x000000ff)) / 255f;
  565. int color2 = input.ReadInt(); // 0x00rrggbb
  566. float r2 = ((color2 & 0x00ff0000) >> 16) / 255f;
  567. float g2 = ((color2 & 0x0000ff00) >> 8) / 255f;
  568. float b2 = ((color2 & 0x000000ff)) / 255f;
  569. timeline.SetFrame(frameIndex, time, r, g, b, a, r2, g2, b2);
  570. if (frameIndex < frameCount - 1) ReadCurve(input, frameIndex, timeline);
  571. }
  572. timelines.Add(timeline);
  573. duration = Math.Max(duration, timeline.frames[(frameCount - 1) * TwoColorTimeline.ENTRIES]);
  574. break;
  575. }
  576. }
  577. }
  578. }
  579. // Bone timelines.
  580. for (int i = 0, n = input.ReadInt(true); i < n; i++) {
  581. int boneIndex = input.ReadInt(true);
  582. for (int ii = 0, nn = input.ReadInt(true); ii < nn; ii++) {
  583. int timelineType = input.ReadByte();
  584. int frameCount = input.ReadInt(true);
  585. switch (timelineType) {
  586. case BONE_ROTATE: {
  587. RotateTimeline timeline = new RotateTimeline(frameCount);
  588. timeline.boneIndex = boneIndex;
  589. for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
  590. timeline.SetFrame(frameIndex, input.ReadFloat(), input.ReadFloat());
  591. if (frameIndex < frameCount - 1) ReadCurve(input, frameIndex, timeline);
  592. }
  593. timelines.Add(timeline);
  594. duration = Math.Max(duration, timeline.frames[(frameCount - 1) * RotateTimeline.ENTRIES]);
  595. break;
  596. }
  597. case BONE_TRANSLATE:
  598. case BONE_SCALE:
  599. case BONE_SHEAR: {
  600. TranslateTimeline timeline;
  601. float timelineScale = 1;
  602. if (timelineType == BONE_SCALE)
  603. timeline = new ScaleTimeline(frameCount);
  604. else if (timelineType == BONE_SHEAR)
  605. timeline = new ShearTimeline(frameCount);
  606. else {
  607. timeline = new TranslateTimeline(frameCount);
  608. timelineScale = scale;
  609. }
  610. timeline.boneIndex = boneIndex;
  611. for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
  612. timeline.SetFrame(frameIndex, input.ReadFloat(), input.ReadFloat() * timelineScale,
  613. input.ReadFloat() * timelineScale);
  614. if (frameIndex < frameCount - 1) ReadCurve(input, frameIndex, timeline);
  615. }
  616. timelines.Add(timeline);
  617. duration = Math.Max(duration, timeline.frames[(frameCount - 1) * TranslateTimeline.ENTRIES]);
  618. break;
  619. }
  620. }
  621. }
  622. }
  623. // IK constraint timelines.
  624. for (int i = 0, n = input.ReadInt(true); i < n; i++) {
  625. int index = input.ReadInt(true);
  626. int frameCount = input.ReadInt(true);
  627. IkConstraintTimeline timeline = new IkConstraintTimeline(frameCount) {
  628. ikConstraintIndex = index
  629. };
  630. for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
  631. timeline.SetFrame(frameIndex, input.ReadFloat(), input.ReadFloat(), input.ReadFloat() * scale, input.ReadSByte(), input.ReadBoolean(),
  632. input.ReadBoolean());
  633. if (frameIndex < frameCount - 1) ReadCurve(input, frameIndex, timeline);
  634. }
  635. timelines.Add(timeline);
  636. duration = Math.Max(duration, timeline.frames[(frameCount - 1) * IkConstraintTimeline.ENTRIES]);
  637. }
  638. // Transform constraint timelines.
  639. for (int i = 0, n = input.ReadInt(true); i < n; i++) {
  640. int index = input.ReadInt(true);
  641. int frameCount = input.ReadInt(true);
  642. TransformConstraintTimeline timeline = new TransformConstraintTimeline(frameCount);
  643. timeline.transformConstraintIndex = index;
  644. for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
  645. timeline.SetFrame(frameIndex, input.ReadFloat(), input.ReadFloat(), input.ReadFloat(), input.ReadFloat(),
  646. input.ReadFloat());
  647. if (frameIndex < frameCount - 1) ReadCurve(input, frameIndex, timeline);
  648. }
  649. timelines.Add(timeline);
  650. duration = Math.Max(duration, timeline.frames[(frameCount - 1) * TransformConstraintTimeline.ENTRIES]);
  651. }
  652. // Path constraint timelines.
  653. for (int i = 0, n = input.ReadInt(true); i < n; i++) {
  654. int index = input.ReadInt(true);
  655. PathConstraintData data = skeletonData.pathConstraints.Items[index];
  656. for (int ii = 0, nn = input.ReadInt(true); ii < nn; ii++) {
  657. int timelineType = input.ReadSByte();
  658. int frameCount = input.ReadInt(true);
  659. switch(timelineType) {
  660. case PATH_POSITION:
  661. case PATH_SPACING: {
  662. PathConstraintPositionTimeline timeline;
  663. float timelineScale = 1;
  664. if (timelineType == PATH_SPACING) {
  665. timeline = new PathConstraintSpacingTimeline(frameCount);
  666. if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) timelineScale = scale;
  667. } else {
  668. timeline = new PathConstraintPositionTimeline(frameCount);
  669. if (data.positionMode == PositionMode.Fixed) timelineScale = scale;
  670. }
  671. timeline.pathConstraintIndex = index;
  672. for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
  673. timeline.SetFrame(frameIndex, input.ReadFloat(), input.ReadFloat() * timelineScale);
  674. if (frameIndex < frameCount - 1) ReadCurve(input, frameIndex, timeline);
  675. }
  676. timelines.Add(timeline);
  677. duration = Math.Max(duration, timeline.frames[(frameCount - 1) * PathConstraintPositionTimeline.ENTRIES]);
  678. break;
  679. }
  680. case PATH_MIX: {
  681. PathConstraintMixTimeline timeline = new PathConstraintMixTimeline(frameCount);
  682. timeline.pathConstraintIndex = index;
  683. for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
  684. timeline.SetFrame(frameIndex, input.ReadFloat(), input.ReadFloat(), input.ReadFloat());
  685. if (frameIndex < frameCount - 1) ReadCurve(input, frameIndex, timeline);
  686. }
  687. timelines.Add(timeline);
  688. duration = Math.Max(duration, timeline.frames[(frameCount - 1) * PathConstraintMixTimeline.ENTRIES]);
  689. break;
  690. }
  691. }
  692. }
  693. }
  694. // Deform timelines.
  695. for (int i = 0, n = input.ReadInt(true); i < n; i++) {
  696. Skin skin = skeletonData.skins.Items[input.ReadInt(true)];
  697. for (int ii = 0, nn = input.ReadInt(true); ii < nn; ii++) {
  698. int slotIndex = input.ReadInt(true);
  699. for (int iii = 0, nnn = input.ReadInt(true); iii < nnn; iii++) {
  700. VertexAttachment attachment = (VertexAttachment)skin.GetAttachment(slotIndex, input.ReadStringRef());
  701. bool weighted = attachment.bones != null;
  702. float[] vertices = attachment.vertices;
  703. int deformLength = weighted ? vertices.Length / 3 * 2 : vertices.Length;
  704. int frameCount = input.ReadInt(true);
  705. DeformTimeline timeline = new DeformTimeline(frameCount);
  706. timeline.slotIndex = slotIndex;
  707. timeline.attachment = attachment;
  708. for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
  709. float time = input.ReadFloat();
  710. float[] deform;
  711. int end = input.ReadInt(true);
  712. if (end == 0)
  713. deform = weighted ? new float[deformLength] : vertices;
  714. else {
  715. deform = new float[deformLength];
  716. int start = input.ReadInt(true);
  717. end += start;
  718. if (scale == 1) {
  719. for (int v = start; v < end; v++)
  720. deform[v] = input.ReadFloat();
  721. } else {
  722. for (int v = start; v < end; v++)
  723. deform[v] = input.ReadFloat() * scale;
  724. }
  725. if (!weighted) {
  726. for (int v = 0, vn = deform.Length; v < vn; v++)
  727. deform[v] += vertices[v];
  728. }
  729. }
  730. timeline.SetFrame(frameIndex, time, deform);
  731. if (frameIndex < frameCount - 1) ReadCurve(input, frameIndex, timeline);
  732. }
  733. timelines.Add(timeline);
  734. duration = Math.Max(duration, timeline.frames[frameCount - 1]);
  735. }
  736. }
  737. }
  738. // Draw order timeline.
  739. int drawOrderCount = input.ReadInt(true);
  740. if (drawOrderCount > 0) {
  741. DrawOrderTimeline timeline = new DrawOrderTimeline(drawOrderCount);
  742. int slotCount = skeletonData.slots.Count;
  743. for (int i = 0; i < drawOrderCount; i++) {
  744. float time = input.ReadFloat();
  745. int offsetCount = input.ReadInt(true);
  746. int[] drawOrder = new int[slotCount];
  747. for (int ii = slotCount - 1; ii >= 0; ii--)
  748. drawOrder[ii] = -1;
  749. int[] unchanged = new int[slotCount - offsetCount];
  750. int originalIndex = 0, unchangedIndex = 0;
  751. for (int ii = 0; ii < offsetCount; ii++) {
  752. int slotIndex = input.ReadInt(true);
  753. // Collect unchanged items.
  754. while (originalIndex != slotIndex)
  755. unchanged[unchangedIndex++] = originalIndex++;
  756. // Set changed items.
  757. drawOrder[originalIndex + input.ReadInt(true)] = originalIndex++;
  758. }
  759. // Collect remaining unchanged items.
  760. while (originalIndex < slotCount)
  761. unchanged[unchangedIndex++] = originalIndex++;
  762. // Fill in unchanged items.
  763. for (int ii = slotCount - 1; ii >= 0; ii--)
  764. if (drawOrder[ii] == -1) drawOrder[ii] = unchanged[--unchangedIndex];
  765. timeline.SetFrame(i, time, drawOrder);
  766. }
  767. timelines.Add(timeline);
  768. duration = Math.Max(duration, timeline.frames[drawOrderCount - 1]);
  769. }
  770. // Event timeline.
  771. int eventCount = input.ReadInt(true);
  772. if (eventCount > 0) {
  773. EventTimeline timeline = new EventTimeline(eventCount);
  774. for (int i = 0; i < eventCount; i++) {
  775. float time = input.ReadFloat();
  776. EventData eventData = skeletonData.events.Items[input.ReadInt(true)];
  777. Event e = new Event(time, eventData) {
  778. Int = input.ReadInt(false),
  779. Float = input.ReadFloat(),
  780. String = input.ReadBoolean() ? input.ReadString() : eventData.String
  781. };
  782. if (e.data.AudioPath != null) {
  783. e.volume = input.ReadFloat();
  784. e.balance = input.ReadFloat();
  785. }
  786. timeline.SetFrame(i, e);
  787. }
  788. timelines.Add(timeline);
  789. duration = Math.Max(duration, timeline.frames[eventCount - 1]);
  790. }
  791. timelines.TrimExcess();
  792. return new Animation(name, timelines, duration);
  793. }
  794. private void ReadCurve (SkeletonInput input, int frameIndex, CurveTimeline timeline) {
  795. switch (input.ReadByte()) {
  796. case CURVE_STEPPED:
  797. timeline.SetStepped(frameIndex);
  798. break;
  799. case CURVE_BEZIER:
  800. timeline.SetCurve(frameIndex, input.ReadFloat(), input.ReadFloat(), input.ReadFloat(), input.ReadFloat());
  801. break;
  802. }
  803. }
  804. internal class Vertices
  805. {
  806. public int[] bones;
  807. public float[] vertices;
  808. }
  809. internal class SkeletonInput {
  810. private byte[] chars = new byte[32];
  811. private byte[] bytesBigEndian = new byte[4];
  812. internal ExposedList<String> strings;
  813. Stream input;
  814. public SkeletonInput (Stream input) {
  815. this.input = input;
  816. }
  817. public byte ReadByte () {
  818. return (byte)input.ReadByte();
  819. }
  820. public sbyte ReadSByte () {
  821. int value = input.ReadByte();
  822. if (value == -1) throw new EndOfStreamException();
  823. return (sbyte)value;
  824. }
  825. public bool ReadBoolean () {
  826. return input.ReadByte() != 0;
  827. }
  828. public float ReadFloat () {
  829. input.Read(bytesBigEndian, 0, 4);
  830. chars[3] = bytesBigEndian[0];
  831. chars[2] = bytesBigEndian[1];
  832. chars[1] = bytesBigEndian[2];
  833. chars[0] = bytesBigEndian[3];
  834. return BitConverter.ToSingle(chars, 0);
  835. }
  836. public int ReadInt () {
  837. input.Read(bytesBigEndian, 0, 4);
  838. return (bytesBigEndian[0] << 24)
  839. + (bytesBigEndian[1] << 16)
  840. + (bytesBigEndian[2] << 8)
  841. + bytesBigEndian[3];
  842. }
  843. public int ReadInt (bool optimizePositive) {
  844. int b = input.ReadByte();
  845. int result = b & 0x7F;
  846. if ((b & 0x80) != 0) {
  847. b = input.ReadByte();
  848. result |= (b & 0x7F) << 7;
  849. if ((b & 0x80) != 0) {
  850. b = input.ReadByte();
  851. result |= (b & 0x7F) << 14;
  852. if ((b & 0x80) != 0) {
  853. b = input.ReadByte();
  854. result |= (b & 0x7F) << 21;
  855. if ((b & 0x80) != 0) result |= (input.ReadByte() & 0x7F) << 28;
  856. }
  857. }
  858. }
  859. return optimizePositive ? result : ((result >> 1) ^ -(result & 1));
  860. }
  861. public string ReadString () {
  862. int byteCount = ReadInt(true);
  863. switch (byteCount) {
  864. case 0:
  865. return null;
  866. case 1:
  867. return "";
  868. }
  869. byteCount--;
  870. byte[] buffer = this.chars;
  871. if (buffer.Length < byteCount) buffer = new byte[byteCount];
  872. ReadFully(buffer, 0, byteCount);
  873. return System.Text.Encoding.UTF8.GetString(buffer, 0, byteCount);
  874. }
  875. ///<return>May be null.</return>
  876. public String ReadStringRef () {
  877. int index = ReadInt(true);
  878. return index == 0 ? null : strings.Items[index - 1];
  879. }
  880. public void ReadFully (byte[] buffer, int offset, int length) {
  881. while (length > 0) {
  882. int count = input.Read(buffer, offset, length);
  883. if (count <= 0) throw new EndOfStreamException();
  884. offset += count;
  885. length -= count;
  886. }
  887. }
  888. /// <summary>Returns the version string of binary skeleton data.</summary>
  889. public string GetVersionString () {
  890. try {
  891. // Hash.
  892. int byteCount = ReadInt(true);
  893. if (byteCount > 1) input.Position += byteCount - 1;
  894. // Version.
  895. byteCount = ReadInt(true);
  896. if (byteCount > 1) {
  897. byteCount--;
  898. var buffer = new byte[byteCount];
  899. ReadFully(buffer, 0, byteCount);
  900. return System.Text.Encoding.UTF8.GetString(buffer, 0, byteCount);
  901. }
  902. throw new ArgumentException("Stream does not contain a valid binary Skeleton Data.", "input");
  903. } catch (Exception e) {
  904. throw new ArgumentException("Stream does not contain a valid binary Skeleton Data.\n" + e, "input");
  905. }
  906. }
  907. }
  908. }
  909. }