SkeletonBinary.cs 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated July 28, 2023. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2023, 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 or
  13. * 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 THE
  27. * 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.Collections.Generic;
  34. using System.IO;
  35. using System.Runtime.Serialization;
  36. #if WINDOWS_STOREAPP
  37. using System.Threading.Tasks;
  38. using Windows.Storage;
  39. #endif
  40. namespace Spine {
  41. public class SkeletonBinary : SkeletonLoader {
  42. public const int BONE_ROTATE = 0;
  43. public const int BONE_TRANSLATE = 1;
  44. public const int BONE_TRANSLATEX = 2;
  45. public const int BONE_TRANSLATEY = 3;
  46. public const int BONE_SCALE = 4;
  47. public const int BONE_SCALEX = 5;
  48. public const int BONE_SCALEY = 6;
  49. public const int BONE_SHEAR = 7;
  50. public const int BONE_SHEARX = 8;
  51. public const int BONE_SHEARY = 9;
  52. public const int BONE_INHERIT = 10;
  53. public const int SLOT_ATTACHMENT = 0;
  54. public const int SLOT_RGBA = 1;
  55. public const int SLOT_RGB = 2;
  56. public const int SLOT_RGBA2 = 3;
  57. public const int SLOT_RGB2 = 4;
  58. public const int SLOT_ALPHA = 5;
  59. public const int ATTACHMENT_DEFORM = 0;
  60. public const int ATTACHMENT_SEQUENCE = 1;
  61. public const int PATH_POSITION = 0;
  62. public const int PATH_SPACING = 1;
  63. public const int PATH_MIX = 2;
  64. public const int PHYSICS_INERTIA = 0;
  65. public const int PHYSICS_STRENGTH = 1;
  66. public const int PHYSICS_DAMPING = 2;
  67. public const int PHYSICS_MASS = 4;
  68. public const int PHYSICS_WIND = 5;
  69. public const int PHYSICS_GRAVITY = 6;
  70. public const int PHYSICS_MIX = 7;
  71. public const int PHYSICS_RESET = 8;
  72. public const int CURVE_LINEAR = 0;
  73. public const int CURVE_STEPPED = 1;
  74. public const int CURVE_BEZIER = 2;
  75. private readonly List<LinkedMesh> linkedMeshes = new List<LinkedMesh>();
  76. public SkeletonBinary (AttachmentLoader attachmentLoader)
  77. : base(attachmentLoader) {
  78. }
  79. public SkeletonBinary (params Atlas[] atlasArray)
  80. : base(atlasArray) {
  81. }
  82. #if !ISUNITY && WINDOWS_STOREAPP
  83. private async Task<SkeletonData> ReadFile(string path) {
  84. var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
  85. using (BufferedStream input = new BufferedStream(await folder.GetFileAsync(path).AsTask().ConfigureAwait(false))) {
  86. SkeletonData skeletonData = ReadSkeletonData(input);
  87. skeletonData.Name = Path.GetFileNameWithoutExtension(path);
  88. return skeletonData;
  89. }
  90. }
  91. public override SkeletonData ReadSkeletonData (string path) {
  92. return this.ReadFile(path).Result;
  93. }
  94. #else
  95. public override SkeletonData ReadSkeletonData (string path) {
  96. #if WINDOWS_PHONE
  97. using (BufferedStream input = new BufferedStream(Microsoft.Xna.Framework.TitleContainer.OpenStream(path))) {
  98. #else
  99. using (FileStream input = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) {
  100. #endif
  101. SkeletonData skeletonData = ReadSkeletonData(input);
  102. skeletonData.name = Path.GetFileNameWithoutExtension(path);
  103. return skeletonData;
  104. }
  105. }
  106. #endif // WINDOWS_STOREAPP
  107. /// <summary>Returns the version string of binary skeleton data.</summary>
  108. public static string GetVersionString (Stream file) {
  109. if (file == null) throw new ArgumentNullException("file");
  110. SkeletonInput input = new SkeletonInput(file);
  111. return input.GetVersionString();
  112. }
  113. public SkeletonData ReadSkeletonData (Stream file) {
  114. if (file == null) throw new ArgumentNullException("file");
  115. float scale = this.scale;
  116. SkeletonData skeletonData = new SkeletonData();
  117. SkeletonInput input = new SkeletonInput(file);
  118. long hash = input.ReadLong();
  119. skeletonData.hash = hash == 0 ? null : hash.ToString();
  120. skeletonData.version = input.ReadString();
  121. if (skeletonData.version.Length == 0) skeletonData.version = null;
  122. // early return for old 3.8 format instead of reading past the end
  123. if (skeletonData.version.Length > 13) return null;
  124. skeletonData.x = input.ReadFloat();
  125. skeletonData.y = input.ReadFloat();
  126. skeletonData.width = input.ReadFloat();
  127. skeletonData.height = input.ReadFloat();
  128. skeletonData.referenceScale = input.ReadFloat() * scale;
  129. bool nonessential = input.ReadBoolean();
  130. if (nonessential) {
  131. skeletonData.fps = input.ReadFloat();
  132. skeletonData.imagesPath = input.ReadString();
  133. if (string.IsNullOrEmpty(skeletonData.imagesPath)) skeletonData.imagesPath = null;
  134. skeletonData.audioPath = input.ReadString();
  135. if (string.IsNullOrEmpty(skeletonData.audioPath)) skeletonData.audioPath = null;
  136. }
  137. int n;
  138. Object[] o;
  139. // Strings.
  140. o = input.strings = new String[n = input.ReadInt(true)];
  141. for (int i = 0; i < n; i++)
  142. o[i] = input.ReadString();
  143. // Bones.
  144. BoneData[] bones = skeletonData.bones.Resize(n = input.ReadInt(true)).Items;
  145. for (int i = 0; i < n; i++) {
  146. String name = input.ReadString();
  147. BoneData parent = i == 0 ? null : bones[input.ReadInt(true)];
  148. BoneData data = new BoneData(i, name, parent);
  149. data.rotation = input.ReadFloat();
  150. data.x = input.ReadFloat() * scale;
  151. data.y = input.ReadFloat() * scale;
  152. data.scaleX = input.ReadFloat();
  153. data.scaleY = input.ReadFloat();
  154. data.shearX = input.ReadFloat();
  155. data.shearY = input.ReadFloat();
  156. data.Length = input.ReadFloat() * scale;
  157. data.inherit = InheritEnum.Values[input.ReadInt(true)];
  158. data.skinRequired = input.ReadBoolean();
  159. if (nonessential) { // discard non-essential data
  160. input.ReadInt(); // Color.rgba8888ToColor(data.color, input.readInt());
  161. input.ReadString(); // data.icon = input.readString();
  162. input.ReadBoolean(); // data.visible = input.readBoolean();
  163. }
  164. bones[i] = data;
  165. }
  166. // Slots.
  167. SlotData[] slots = skeletonData.slots.Resize(n = input.ReadInt(true)).Items;
  168. for (int i = 0; i < n; i++) {
  169. String slotName = input.ReadString();
  170. BoneData boneData = bones[input.ReadInt(true)];
  171. SlotData slotData = new SlotData(i, slotName, boneData);
  172. int color = input.ReadInt();
  173. slotData.r = ((color & 0xff000000) >> 24) / 255f;
  174. slotData.g = ((color & 0x00ff0000) >> 16) / 255f;
  175. slotData.b = ((color & 0x0000ff00) >> 8) / 255f;
  176. slotData.a = ((color & 0x000000ff)) / 255f;
  177. int darkColor = input.ReadInt(); // 0x00rrggbb
  178. if (darkColor != -1) {
  179. slotData.hasSecondColor = true;
  180. slotData.r2 = ((darkColor & 0x00ff0000) >> 16) / 255f;
  181. slotData.g2 = ((darkColor & 0x0000ff00) >> 8) / 255f;
  182. slotData.b2 = ((darkColor & 0x000000ff)) / 255f;
  183. }
  184. slotData.attachmentName = input.ReadStringRef();
  185. slotData.blendMode = (BlendMode)input.ReadInt(true);
  186. if (nonessential) {
  187. input.ReadBoolean(); // data.visible = input.readBoolean(); data.path = path;
  188. }
  189. slots[i] = slotData;
  190. }
  191. // IK constraints.
  192. o = skeletonData.ikConstraints.Resize(n = input.ReadInt(true)).Items;
  193. for (int i = 0, nn; i < n; i++) {
  194. IkConstraintData data = new IkConstraintData(input.ReadString());
  195. data.order = input.ReadInt(true);
  196. BoneData[] constraintBones = data.bones.Resize(nn = input.ReadInt(true)).Items;
  197. for (int ii = 0; ii < nn; ii++)
  198. constraintBones[ii] = bones[input.ReadInt(true)];
  199. data.target = bones[input.ReadInt(true)];
  200. int flags = input.Read();
  201. data.skinRequired = (flags & 1) != 0;
  202. data.bendDirection = (flags & 2) != 0 ? 1 : -1;
  203. data.compress = (flags & 4) != 0;
  204. data.stretch = (flags & 8) != 0;
  205. data.uniform = (flags & 16) != 0;
  206. if ((flags & 32) != 0) data.mix = (flags & 64) != 0 ? input.ReadFloat() : 1;
  207. if ((flags & 128) != 0) data.softness = input.ReadFloat() * scale;
  208. o[i] = data;
  209. }
  210. // Transform constraints.
  211. o = skeletonData.transformConstraints.Resize(n = input.ReadInt(true)).Items;
  212. for (int i = 0, nn; i < n; i++) {
  213. TransformConstraintData data = new TransformConstraintData(input.ReadString());
  214. data.order = input.ReadInt(true);
  215. BoneData[] constraintBones = data.bones.Resize(nn = input.ReadInt(true)).Items;
  216. for (int ii = 0; ii < nn; ii++)
  217. constraintBones[ii] = bones[input.ReadInt(true)];
  218. data.target = bones[input.ReadInt(true)];
  219. int flags = input.Read();
  220. data.skinRequired = (flags & 1) != 0;
  221. data.local = (flags & 2) != 0;
  222. data.relative = (flags & 4) != 0;
  223. if ((flags & 8) != 0) data.offsetRotation = input.ReadFloat();
  224. if ((flags & 16) != 0) data.offsetX = input.ReadFloat() * scale;
  225. if ((flags & 32) != 0) data.offsetY = input.ReadFloat() * scale;
  226. if ((flags & 64) != 0) data.offsetScaleX = input.ReadFloat();
  227. if ((flags & 128) != 0) data.offsetScaleY = input.ReadFloat();
  228. flags = input.Read();
  229. if ((flags & 1) != 0) data.offsetShearY = input.ReadFloat();
  230. if ((flags & 2) != 0) data.mixRotate = input.ReadFloat();
  231. if ((flags & 4) != 0) data.mixX = input.ReadFloat();
  232. if ((flags & 8) != 0) data.mixY = input.ReadFloat();
  233. if ((flags & 16) != 0) data.mixScaleX = input.ReadFloat();
  234. if ((flags & 32) != 0) data.mixScaleY = input.ReadFloat();
  235. if ((flags & 64) != 0) data.mixShearY = input.ReadFloat();
  236. o[i] = data;
  237. }
  238. // Path constraints
  239. o = skeletonData.pathConstraints.Resize(n = input.ReadInt(true)).Items;
  240. for (int i = 0, nn; i < n; i++) {
  241. PathConstraintData data = new PathConstraintData(input.ReadString());
  242. data.order = input.ReadInt(true);
  243. data.skinRequired = input.ReadBoolean();
  244. BoneData[] constraintBones = data.bones.Resize(nn = input.ReadInt(true)).Items;
  245. for (int ii = 0; ii < nn; ii++)
  246. constraintBones[ii] = bones[input.ReadInt(true)];
  247. data.target = slots[input.ReadInt(true)];
  248. int flags = input.Read();
  249. data.positionMode = (PositionMode)Enum.GetValues(typeof(PositionMode)).GetValue(flags & 1);
  250. data.spacingMode = (SpacingMode)Enum.GetValues(typeof(SpacingMode)).GetValue((flags >> 1) & 3);
  251. data.rotateMode = (RotateMode)Enum.GetValues(typeof(RotateMode)).GetValue((flags >> 3) & 3);
  252. if ((flags & 128) != 0) data.offsetRotation = input.ReadFloat();
  253. data.position = input.ReadFloat();
  254. if (data.positionMode == PositionMode.Fixed) data.position *= scale;
  255. data.spacing = input.ReadFloat();
  256. if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) data.spacing *= scale;
  257. data.mixRotate = input.ReadFloat();
  258. data.mixX = input.ReadFloat();
  259. data.mixY = input.ReadFloat();
  260. o[i] = data;
  261. }
  262. // Physics constraints.
  263. o = skeletonData.physicsConstraints.Resize(n = input.ReadInt(true)).Items;
  264. for (int i = 0; i < n; i++) {
  265. PhysicsConstraintData data = new PhysicsConstraintData(input.ReadString());
  266. data.order = input.ReadInt(true);
  267. data.bone = bones[input.ReadInt(true)];
  268. int flags = input.Read();
  269. data.skinRequired = (flags & 1) != 0;
  270. if ((flags & 2) != 0) data.x = input.ReadFloat();
  271. if ((flags & 4) != 0) data.y = input.ReadFloat();
  272. if ((flags & 8) != 0) data.rotate = input.ReadFloat();
  273. if ((flags & 16) != 0) data.scaleX = input.ReadFloat();
  274. if ((flags & 32) != 0) data.shearX = input.ReadFloat();
  275. data.limit = ((flags & 64) != 0 ? input.ReadFloat() : 5000) * scale;
  276. data.step = 1f / input.ReadUByte();
  277. data.inertia = input.ReadFloat();
  278. data.strength = input.ReadFloat();
  279. data.damping = input.ReadFloat();
  280. data.massInverse = (flags & 128) != 0 ? input.ReadFloat() : 1;
  281. data.wind = input.ReadFloat();
  282. data.gravity = input.ReadFloat();
  283. flags = input.Read();
  284. if ((flags & 1) != 0) data.inertiaGlobal = true;
  285. if ((flags & 2) != 0) data.strengthGlobal = true;
  286. if ((flags & 4) != 0) data.dampingGlobal = true;
  287. if ((flags & 8) != 0) data.massGlobal = true;
  288. if ((flags & 16) != 0) data.windGlobal = true;
  289. if ((flags & 32) != 0) data.gravityGlobal = true;
  290. if ((flags & 64) != 0) data.mixGlobal = true;
  291. data.mix = (flags & 128) != 0 ? input.ReadFloat() : 1;
  292. o[i] = data;
  293. }
  294. // Default skin.
  295. Skin defaultSkin = ReadSkin(input, skeletonData, true, nonessential);
  296. if (defaultSkin != null) {
  297. skeletonData.defaultSkin = defaultSkin;
  298. skeletonData.skins.Add(defaultSkin);
  299. }
  300. // Skins.
  301. {
  302. int i = skeletonData.skins.Count;
  303. o = skeletonData.skins.Resize(n = i + input.ReadInt(true)).Items;
  304. for (; i < n; i++)
  305. o[i] = ReadSkin(input, skeletonData, false, nonessential);
  306. }
  307. // Linked meshes.
  308. n = linkedMeshes.Count;
  309. for (int i = 0; i < n; i++) {
  310. LinkedMesh linkedMesh = linkedMeshes[i];
  311. Skin skin = skeletonData.skins.Items[linkedMesh.skinIndex];
  312. Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent);
  313. if (parent == null) throw new Exception("Parent mesh not found: " + linkedMesh.parent);
  314. linkedMesh.mesh.TimelineAttachment = linkedMesh.inheritTimelines ? (VertexAttachment)parent : linkedMesh.mesh;
  315. linkedMesh.mesh.ParentMesh = (MeshAttachment)parent;
  316. if (linkedMesh.mesh.Sequence == null) linkedMesh.mesh.UpdateRegion();
  317. }
  318. linkedMeshes.Clear();
  319. // Events.
  320. o = skeletonData.events.Resize(n = input.ReadInt(true)).Items;
  321. for (int i = 0; i < n; i++) {
  322. EventData data = new EventData(input.ReadString());
  323. data.Int = input.ReadInt(false);
  324. data.Float = input.ReadFloat();
  325. data.String = input.ReadString();
  326. data.AudioPath = input.ReadString();
  327. if (data.AudioPath != null) {
  328. data.Volume = input.ReadFloat();
  329. data.Balance = input.ReadFloat();
  330. }
  331. o[i] = data;
  332. }
  333. // Animations.
  334. o = skeletonData.animations.Resize(n = input.ReadInt(true)).Items;
  335. for (int i = 0; i < n; i++)
  336. o[i] = ReadAnimation(input.ReadString(), input, skeletonData);
  337. return skeletonData;
  338. }
  339. /// <returns>May be null.</returns>
  340. private Skin ReadSkin (SkeletonInput input, SkeletonData skeletonData, bool defaultSkin, bool nonessential) {
  341. Skin skin;
  342. int slotCount;
  343. if (defaultSkin) {
  344. slotCount = input.ReadInt(true);
  345. if (slotCount == 0) return null;
  346. skin = new Skin("default");
  347. } else {
  348. skin = new Skin(input.ReadString());
  349. if (nonessential) input.ReadInt(); // discard, Color.rgba8888ToColor(skin.color, input.readInt());
  350. Object[] bones = skin.bones.Resize(input.ReadInt(true)).Items;
  351. BoneData[] bonesItems = skeletonData.bones.Items;
  352. for (int i = 0, n = skin.bones.Count; i < n; i++)
  353. bones[i] = bonesItems[input.ReadInt(true)];
  354. IkConstraintData[] ikConstraintsItems = skeletonData.ikConstraints.Items;
  355. for (int i = 0, n = input.ReadInt(true); i < n; i++)
  356. skin.constraints.Add(ikConstraintsItems[input.ReadInt(true)]);
  357. TransformConstraintData[] transformConstraintsItems = skeletonData.transformConstraints.Items;
  358. for (int i = 0, n = input.ReadInt(true); i < n; i++)
  359. skin.constraints.Add(transformConstraintsItems[input.ReadInt(true)]);
  360. PathConstraintData[] pathConstraintsItems = skeletonData.pathConstraints.Items;
  361. for (int i = 0, n = input.ReadInt(true); i < n; i++)
  362. skin.constraints.Add(pathConstraintsItems[input.ReadInt(true)]);
  363. PhysicsConstraintData[] physicsConstraintsItems = skeletonData.physicsConstraints.Items;
  364. for (int i = 0, n = input.ReadInt(true); i < n; i++)
  365. skin.constraints.Add(physicsConstraintsItems[input.ReadInt(true)]);
  366. skin.constraints.TrimExcess();
  367. slotCount = input.ReadInt(true);
  368. }
  369. for (int i = 0; i < slotCount; i++) {
  370. int slotIndex = input.ReadInt(true);
  371. for (int ii = 0, nn = input.ReadInt(true); ii < nn; ii++) {
  372. String name = input.ReadStringRef();
  373. Attachment attachment = ReadAttachment(input, skeletonData, skin, slotIndex, name, nonessential);
  374. if (attachment != null) skin.SetAttachment(slotIndex, name, attachment);
  375. }
  376. }
  377. return skin;
  378. }
  379. private Attachment ReadAttachment (SkeletonInput input, SkeletonData skeletonData, Skin skin, int slotIndex,
  380. String attachmentName, bool nonessential) {
  381. float scale = this.scale;
  382. int flags = input.ReadUByte();
  383. string name = (flags & 8) != 0 ? input.ReadStringRef() : attachmentName;
  384. switch ((AttachmentType)(flags & 0x7)) { // 0b111
  385. case AttachmentType.Region: {
  386. string path = (flags & 16) != 0 ? input.ReadStringRef() : null;
  387. uint color = (flags & 32) != 0 ? (uint)input.ReadInt() : 0xffffffff;
  388. Sequence sequence = (flags & 64) != 0 ? ReadSequence(input) : null;
  389. float rotation = (flags & 128) != 0 ? input.ReadFloat() : 0;
  390. float x = input.ReadFloat();
  391. float y = input.ReadFloat();
  392. float scaleX = input.ReadFloat();
  393. float scaleY = input.ReadFloat();
  394. float width = input.ReadFloat();
  395. float height = input.ReadFloat();
  396. if (path == null) path = name;
  397. RegionAttachment region = attachmentLoader.NewRegionAttachment(skin, name, path, sequence);
  398. if (region == null) return null;
  399. region.Path = path;
  400. region.x = x * scale;
  401. region.y = y * scale;
  402. region.scaleX = scaleX;
  403. region.scaleY = scaleY;
  404. region.rotation = rotation;
  405. region.width = width * scale;
  406. region.height = height * scale;
  407. region.r = ((color & 0xff000000) >> 24) / 255f;
  408. region.g = ((color & 0x00ff0000) >> 16) / 255f;
  409. region.b = ((color & 0x0000ff00) >> 8) / 255f;
  410. region.a = ((color & 0x000000ff)) / 255f;
  411. region.sequence = sequence;
  412. if (sequence == null) region.UpdateRegion();
  413. return region;
  414. }
  415. case AttachmentType.Boundingbox: {
  416. Vertices vertices = ReadVertices(input, (flags & 16) != 0);
  417. if (nonessential) input.ReadInt(); // discard, int color = nonessential ? input.readInt() : 0;
  418. BoundingBoxAttachment box = attachmentLoader.NewBoundingBoxAttachment(skin, name);
  419. if (box == null) return null;
  420. box.worldVerticesLength = vertices.length;
  421. box.vertices = vertices.vertices;
  422. box.bones = vertices.bones;
  423. // skipped porting: if (nonessential) Color.rgba8888ToColor(box.getColor(), color);
  424. return box;
  425. }
  426. case AttachmentType.Mesh: {
  427. string path = (flags & 16) != 0 ? input.ReadStringRef() : name;
  428. uint color = (flags & 32) != 0 ? (uint)input.ReadInt() : 0xffffffff;
  429. Sequence sequence = (flags & 64) != 0 ? ReadSequence(input) : null;
  430. int hullLength = input.ReadInt(true);
  431. Vertices vertices = ReadVertices(input, (flags & 128) != 0);
  432. float[] uvs = ReadFloatArray(input, vertices.length, 1);
  433. int[] triangles = ReadShortArray(input, (vertices.length - hullLength - 2) * 3);
  434. int[] edges = null;
  435. float width = 0, height = 0;
  436. if (nonessential) {
  437. edges = ReadShortArray(input, input.ReadInt(true));
  438. width = input.ReadFloat();
  439. height = input.ReadFloat();
  440. }
  441. MeshAttachment mesh = attachmentLoader.NewMeshAttachment(skin, name, path, sequence);
  442. if (mesh == null) return null;
  443. mesh.Path = path;
  444. mesh.r = ((color & 0xff000000) >> 24) / 255f;
  445. mesh.g = ((color & 0x00ff0000) >> 16) / 255f;
  446. mesh.b = ((color & 0x0000ff00) >> 8) / 255f;
  447. mesh.a = ((color & 0x000000ff)) / 255f;
  448. mesh.bones = vertices.bones;
  449. mesh.vertices = vertices.vertices;
  450. mesh.WorldVerticesLength = vertices.length;
  451. mesh.triangles = triangles;
  452. mesh.regionUVs = uvs;
  453. if (sequence == null) mesh.UpdateRegion();
  454. mesh.HullLength = hullLength << 1;
  455. mesh.Sequence = sequence;
  456. if (nonessential) {
  457. mesh.Edges = edges;
  458. mesh.Width = width * scale;
  459. mesh.Height = height * scale;
  460. }
  461. return mesh;
  462. }
  463. case AttachmentType.Linkedmesh: {
  464. String path = (flags & 16) != 0 ? input.ReadStringRef() : name;
  465. uint color = (flags & 32) != 0 ? (uint)input.ReadInt() : 0xffffffff;
  466. Sequence sequence = (flags & 64) != 0 ? ReadSequence(input) : null;
  467. bool inheritTimelines = (flags & 128) != 0;
  468. int skinIndex = input.ReadInt(true);
  469. string parent = input.ReadStringRef();
  470. float width = 0, height = 0;
  471. if (nonessential) {
  472. width = input.ReadFloat();
  473. height = input.ReadFloat();
  474. }
  475. MeshAttachment mesh = attachmentLoader.NewMeshAttachment(skin, name, path, sequence);
  476. if (mesh == null) return null;
  477. mesh.Path = path;
  478. mesh.r = ((color & 0xff000000) >> 24) / 255f;
  479. mesh.g = ((color & 0x00ff0000) >> 16) / 255f;
  480. mesh.b = ((color & 0x0000ff00) >> 8) / 255f;
  481. mesh.a = ((color & 0x000000ff)) / 255f;
  482. mesh.Sequence = sequence;
  483. if (nonessential) {
  484. mesh.Width = width * scale;
  485. mesh.Height = height * scale;
  486. }
  487. linkedMeshes.Add(new LinkedMesh(mesh, skinIndex, slotIndex, parent, inheritTimelines));
  488. return mesh;
  489. }
  490. case AttachmentType.Path: {
  491. bool closed = (flags & 16) != 0;
  492. bool constantSpeed = (flags & 32) != 0;
  493. Vertices vertices = ReadVertices(input, (flags & 64) != 0);
  494. float[] lengths = new float[vertices.length / 6];
  495. for (int i = 0, n = lengths.Length; i < n; i++)
  496. lengths[i] = input.ReadFloat() * scale;
  497. if (nonessential) input.ReadInt(); //int color = nonessential ? input.ReadInt() : 0;
  498. PathAttachment path = attachmentLoader.NewPathAttachment(skin, name);
  499. if (path == null) return null;
  500. path.closed = closed;
  501. path.constantSpeed = constantSpeed;
  502. path.worldVerticesLength = vertices.length;
  503. path.vertices = vertices.vertices;
  504. path.bones = vertices.bones;
  505. path.lengths = lengths;
  506. // skipped porting: if (nonessential) Color.rgba8888ToColor(path.getColor(), color);
  507. return path;
  508. }
  509. case AttachmentType.Point: {
  510. float rotation = input.ReadFloat();
  511. float x = input.ReadFloat();
  512. float y = input.ReadFloat();
  513. if (nonessential) input.ReadInt(); //int color = nonessential ? input.ReadInt() : 0;
  514. PointAttachment point = attachmentLoader.NewPointAttachment(skin, name);
  515. if (point == null) return null;
  516. point.x = x * scale;
  517. point.y = y * scale;
  518. point.rotation = rotation;
  519. // skipped porting: if (nonessential) point.color = color;
  520. return point;
  521. }
  522. case AttachmentType.Clipping: {
  523. int endSlotIndex = input.ReadInt(true);
  524. Vertices vertices = ReadVertices(input, (flags & 16) != 0);
  525. if (nonessential) input.ReadInt();
  526. ClippingAttachment clip = attachmentLoader.NewClippingAttachment(skin, name);
  527. if (clip == null) return null;
  528. clip.EndSlot = skeletonData.slots.Items[endSlotIndex];
  529. clip.worldVerticesLength = vertices.length;
  530. clip.vertices = vertices.vertices;
  531. clip.bones = vertices.bones;
  532. // skipped porting: if (nonessential) Color.rgba8888ToColor(clip.getColor(), color);
  533. return clip;
  534. }
  535. }
  536. return null;
  537. }
  538. private Sequence ReadSequence (SkeletonInput input) {
  539. Sequence sequence = new Sequence(input.ReadInt(true));
  540. sequence.Start = input.ReadInt(true);
  541. sequence.Digits = input.ReadInt(true);
  542. sequence.SetupIndex = input.ReadInt(true);
  543. return sequence;
  544. }
  545. private Vertices ReadVertices (SkeletonInput input, bool weighted) {
  546. float scale = this.scale;
  547. int vertexCount = input.ReadInt(true);
  548. Vertices vertices = new Vertices();
  549. vertices.length = vertexCount << 1;
  550. if (!weighted) {
  551. vertices.vertices = ReadFloatArray(input, vertices.length, scale);
  552. return vertices;
  553. }
  554. ExposedList<float> weights = new ExposedList<float>(vertices.length * 3 * 3);
  555. ExposedList<int> bonesArray = new ExposedList<int>(vertices.length * 3);
  556. for (int i = 0; i < vertexCount; i++) {
  557. int boneCount = input.ReadInt(true);
  558. bonesArray.Add(boneCount);
  559. for (int ii = 0; ii < boneCount; ii++) {
  560. bonesArray.Add(input.ReadInt(true));
  561. weights.Add(input.ReadFloat() * scale);
  562. weights.Add(input.ReadFloat() * scale);
  563. weights.Add(input.ReadFloat());
  564. }
  565. }
  566. vertices.vertices = weights.ToArray();
  567. vertices.bones = bonesArray.ToArray();
  568. return vertices;
  569. }
  570. private float[] ReadFloatArray (SkeletonInput input, int n, float scale) {
  571. float[] array = new float[n];
  572. if (scale == 1) {
  573. for (int i = 0; i < n; i++)
  574. array[i] = input.ReadFloat();
  575. } else {
  576. for (int i = 0; i < n; i++)
  577. array[i] = input.ReadFloat() * scale;
  578. }
  579. return array;
  580. }
  581. private int[] ReadShortArray (SkeletonInput input, int n) {
  582. int[] array = new int[n];
  583. for (int i = 0; i < n; i++)
  584. array[i] = input.ReadInt(true);
  585. return array;
  586. }
  587. /// <exception cref="SerializationException">SerializationException will be thrown when a Vertex attachment is not found.</exception>
  588. /// <exception cref="IOException">Throws IOException when a read operation fails.</exception>
  589. private Animation ReadAnimation (String name, SkeletonInput input, SkeletonData skeletonData) {
  590. ExposedList<Timeline> timelines = new ExposedList<Timeline>(input.ReadInt(true));
  591. float scale = this.scale;
  592. // Slot timelines.
  593. for (int i = 0, n = input.ReadInt(true); i < n; i++) {
  594. int slotIndex = input.ReadInt(true);
  595. for (int ii = 0, nn = input.ReadInt(true); ii < nn; ii++) {
  596. int timelineType = input.ReadUByte(), frameCount = input.ReadInt(true), frameLast = frameCount - 1;
  597. switch (timelineType) {
  598. case SLOT_ATTACHMENT: {
  599. AttachmentTimeline timeline = new AttachmentTimeline(frameCount, slotIndex);
  600. for (int frame = 0; frame < frameCount; frame++)
  601. timeline.SetFrame(frame, input.ReadFloat(), input.ReadStringRef());
  602. timelines.Add(timeline);
  603. break;
  604. }
  605. case SLOT_RGBA: {
  606. RGBATimeline timeline = new RGBATimeline(frameCount, input.ReadInt(true), slotIndex);
  607. float time = input.ReadFloat();
  608. float r = input.Read() / 255f, g = input.Read() / 255f;
  609. float b = input.Read() / 255f, a = input.Read() / 255f;
  610. for (int frame = 0, bezier = 0; ; frame++) {
  611. timeline.SetFrame(frame, time, r, g, b, a);
  612. if (frame == frameLast) break;
  613. float time2 = input.ReadFloat();
  614. float r2 = input.Read() / 255f, g2 = input.Read() / 255f;
  615. float b2 = input.Read() / 255f, a2 = input.Read() / 255f;
  616. switch (input.ReadUByte()) {
  617. case CURVE_STEPPED:
  618. timeline.SetStepped(frame);
  619. break;
  620. case CURVE_BEZIER:
  621. SetBezier(input, timeline, bezier++, frame, 0, time, time2, r, r2, 1);
  622. SetBezier(input, timeline, bezier++, frame, 1, time, time2, g, g2, 1);
  623. SetBezier(input, timeline, bezier++, frame, 2, time, time2, b, b2, 1);
  624. SetBezier(input, timeline, bezier++, frame, 3, time, time2, a, a2, 1);
  625. break;
  626. }
  627. time = time2;
  628. r = r2;
  629. g = g2;
  630. b = b2;
  631. a = a2;
  632. }
  633. timelines.Add(timeline);
  634. break;
  635. }
  636. case SLOT_RGB: {
  637. RGBTimeline timeline = new RGBTimeline(frameCount, input.ReadInt(true), slotIndex);
  638. float time = input.ReadFloat();
  639. float r = input.Read() / 255f, g = input.Read() / 255f, b = input.Read() / 255f;
  640. for (int frame = 0, bezier = 0; ; frame++) {
  641. timeline.SetFrame(frame, time, r, g, b);
  642. if (frame == frameLast) break;
  643. float time2 = input.ReadFloat();
  644. float r2 = input.Read() / 255f, g2 = input.Read() / 255f, b2 = input.Read() / 255f;
  645. switch (input.ReadUByte()) {
  646. case CURVE_STEPPED:
  647. timeline.SetStepped(frame);
  648. break;
  649. case CURVE_BEZIER:
  650. SetBezier(input, timeline, bezier++, frame, 0, time, time2, r, r2, 1);
  651. SetBezier(input, timeline, bezier++, frame, 1, time, time2, g, g2, 1);
  652. SetBezier(input, timeline, bezier++, frame, 2, time, time2, b, b2, 1);
  653. break;
  654. }
  655. time = time2;
  656. r = r2;
  657. g = g2;
  658. b = b2;
  659. }
  660. timelines.Add(timeline);
  661. break;
  662. }
  663. case SLOT_RGBA2: {
  664. RGBA2Timeline timeline = new RGBA2Timeline(frameCount, input.ReadInt(true), slotIndex);
  665. float time = input.ReadFloat();
  666. float r = input.Read() / 255f, g = input.Read() / 255f;
  667. float b = input.Read() / 255f, a = input.Read() / 255f;
  668. float r2 = input.Read() / 255f, g2 = input.Read() / 255f, b2 = input.Read() / 255f;
  669. for (int frame = 0, bezier = 0; ; frame++) {
  670. timeline.SetFrame(frame, time, r, g, b, a, r2, g2, b2);
  671. if (frame == frameLast) break;
  672. float time2 = input.ReadFloat();
  673. float nr = input.Read() / 255f, ng = input.Read() / 255f;
  674. float nb = input.Read() / 255f, na = input.Read() / 255f;
  675. float nr2 = input.Read() / 255f, ng2 = input.Read() / 255f, nb2 = input.Read() / 255f;
  676. switch (input.ReadUByte()) {
  677. case CURVE_STEPPED:
  678. timeline.SetStepped(frame);
  679. break;
  680. case CURVE_BEZIER:
  681. SetBezier(input, timeline, bezier++, frame, 0, time, time2, r, nr, 1);
  682. SetBezier(input, timeline, bezier++, frame, 1, time, time2, g, ng, 1);
  683. SetBezier(input, timeline, bezier++, frame, 2, time, time2, b, nb, 1);
  684. SetBezier(input, timeline, bezier++, frame, 3, time, time2, a, na, 1);
  685. SetBezier(input, timeline, bezier++, frame, 4, time, time2, r2, nr2, 1);
  686. SetBezier(input, timeline, bezier++, frame, 5, time, time2, g2, ng2, 1);
  687. SetBezier(input, timeline, bezier++, frame, 6, time, time2, b2, nb2, 1);
  688. break;
  689. }
  690. time = time2;
  691. r = nr;
  692. g = ng;
  693. b = nb;
  694. a = na;
  695. r2 = nr2;
  696. g2 = ng2;
  697. b2 = nb2;
  698. }
  699. timelines.Add(timeline);
  700. break;
  701. }
  702. case SLOT_RGB2: {
  703. RGB2Timeline timeline = new RGB2Timeline(frameCount, input.ReadInt(true), slotIndex);
  704. float time = input.ReadFloat();
  705. float r = input.Read() / 255f, g = input.Read() / 255f, b = input.Read() / 255f;
  706. float r2 = input.Read() / 255f, g2 = input.Read() / 255f, b2 = input.Read() / 255f;
  707. for (int frame = 0, bezier = 0; ; frame++) {
  708. timeline.SetFrame(frame, time, r, g, b, r2, g2, b2);
  709. if (frame == frameLast) break;
  710. float time2 = input.ReadFloat();
  711. float nr = input.Read() / 255f, ng = input.Read() / 255f, nb = input.Read() / 255f;
  712. float nr2 = input.Read() / 255f, ng2 = input.Read() / 255f, nb2 = input.Read() / 255f;
  713. switch (input.ReadUByte()) {
  714. case CURVE_STEPPED:
  715. timeline.SetStepped(frame);
  716. break;
  717. case CURVE_BEZIER:
  718. SetBezier(input, timeline, bezier++, frame, 0, time, time2, r, nr, 1);
  719. SetBezier(input, timeline, bezier++, frame, 1, time, time2, g, ng, 1);
  720. SetBezier(input, timeline, bezier++, frame, 2, time, time2, b, nb, 1);
  721. SetBezier(input, timeline, bezier++, frame, 3, time, time2, r2, nr2, 1);
  722. SetBezier(input, timeline, bezier++, frame, 4, time, time2, g2, ng2, 1);
  723. SetBezier(input, timeline, bezier++, frame, 5, time, time2, b2, nb2, 1);
  724. break;
  725. }
  726. time = time2;
  727. r = nr;
  728. g = ng;
  729. b = nb;
  730. r2 = nr2;
  731. g2 = ng2;
  732. b2 = nb2;
  733. }
  734. timelines.Add(timeline);
  735. break;
  736. }
  737. case SLOT_ALPHA: {
  738. AlphaTimeline timeline = new AlphaTimeline(frameCount, input.ReadInt(true), slotIndex);
  739. float time = input.ReadFloat(), a = input.Read() / 255f;
  740. for (int frame = 0, bezier = 0; ; frame++) {
  741. timeline.SetFrame(frame, time, a);
  742. if (frame == frameLast) break;
  743. float time2 = input.ReadFloat();
  744. float a2 = input.Read() / 255f;
  745. switch (input.ReadUByte()) {
  746. case CURVE_STEPPED:
  747. timeline.SetStepped(frame);
  748. break;
  749. case CURVE_BEZIER:
  750. SetBezier(input, timeline, bezier++, frame, 0, time, time2, a, a2, 1);
  751. break;
  752. }
  753. time = time2;
  754. a = a2;
  755. }
  756. timelines.Add(timeline);
  757. break;
  758. }
  759. }
  760. }
  761. }
  762. // Bone timelines.
  763. for (int i = 0, n = input.ReadInt(true); i < n; i++) {
  764. int boneIndex = input.ReadInt(true);
  765. for (int ii = 0, nn = input.ReadInt(true); ii < nn; ii++) {
  766. int type = input.ReadUByte(), frameCount = input.ReadInt(true);
  767. if (type == BONE_INHERIT) {
  768. InheritTimeline timeline = new InheritTimeline(frameCount, boneIndex);
  769. for (int frame = 0; frame < frameCount; frame++)
  770. timeline.SetFrame(frame, input.ReadFloat(), InheritEnum.Values[input.ReadUByte()]);
  771. timelines.Add(timeline);
  772. continue;
  773. }
  774. int bezierCount = input.ReadInt(true);
  775. switch (type) {
  776. case BONE_ROTATE:
  777. ReadTimeline(input, timelines, new RotateTimeline(frameCount, bezierCount, boneIndex), 1);
  778. break;
  779. case BONE_TRANSLATE:
  780. ReadTimeline(input, timelines, new TranslateTimeline(frameCount, bezierCount, boneIndex), scale);
  781. break;
  782. case BONE_TRANSLATEX:
  783. ReadTimeline(input, timelines, new TranslateXTimeline(frameCount, bezierCount, boneIndex), scale);
  784. break;
  785. case BONE_TRANSLATEY:
  786. ReadTimeline(input, timelines, new TranslateYTimeline(frameCount, bezierCount, boneIndex), scale);
  787. break;
  788. case BONE_SCALE:
  789. ReadTimeline(input, timelines, new ScaleTimeline(frameCount, bezierCount, boneIndex), 1);
  790. break;
  791. case BONE_SCALEX:
  792. ReadTimeline(input, timelines, new ScaleXTimeline(frameCount, bezierCount, boneIndex), 1);
  793. break;
  794. case BONE_SCALEY:
  795. ReadTimeline(input, timelines, new ScaleYTimeline(frameCount, bezierCount, boneIndex), 1);
  796. break;
  797. case BONE_SHEAR:
  798. ReadTimeline(input, timelines, new ShearTimeline(frameCount, bezierCount, boneIndex), 1);
  799. break;
  800. case BONE_SHEARX:
  801. ReadTimeline(input, timelines, new ShearXTimeline(frameCount, bezierCount, boneIndex), 1);
  802. break;
  803. case BONE_SHEARY:
  804. ReadTimeline(input, timelines, new ShearYTimeline(frameCount, bezierCount, boneIndex), 1);
  805. break;
  806. }
  807. }
  808. }
  809. // IK constraint timelines.
  810. for (int i = 0, n = input.ReadInt(true); i < n; i++) {
  811. int index = input.ReadInt(true), frameCount = input.ReadInt(true), frameLast = frameCount - 1;
  812. IkConstraintTimeline timeline = new IkConstraintTimeline(frameCount, input.ReadInt(true), index);
  813. int flags = input.Read();
  814. float time = input.ReadFloat(), mix = (flags & 1) != 0 ? ((flags & 2) != 0 ? input.ReadFloat() : 1) : 0;
  815. float softness = (flags & 4) != 0 ? input.ReadFloat() * scale : 0;
  816. for (int frame = 0, bezier = 0; ; frame++) {
  817. timeline.SetFrame(frame, time, mix, softness, (flags & 8) != 0 ? 1 : -1, (flags & 16) != 0, (flags & 32) != 0);
  818. if (frame == frameLast) break;
  819. flags = input.Read();
  820. float time2 = input.ReadFloat(), mix2 = (flags & 1) != 0 ? ((flags & 2) != 0 ? input.ReadFloat() : 1) : 0;
  821. float softness2 = (flags & 4) != 0 ? input.ReadFloat() * scale : 0;
  822. if ((flags & 64) != 0)
  823. timeline.SetStepped(frame);
  824. else if ((flags & 128) != 0) {
  825. SetBezier(input, timeline, bezier++, frame, 0, time, time2, mix, mix2, 1);
  826. SetBezier(input, timeline, bezier++, frame, 1, time, time2, softness, softness2, scale);
  827. }
  828. time = time2;
  829. mix = mix2;
  830. softness = softness2;
  831. }
  832. timelines.Add(timeline);
  833. }
  834. // Transform constraint timelines.
  835. for (int i = 0, n = input.ReadInt(true); i < n; i++) {
  836. int index = input.ReadInt(true), frameCount = input.ReadInt(true), frameLast = frameCount - 1;
  837. TransformConstraintTimeline timeline = new TransformConstraintTimeline(frameCount, input.ReadInt(true), index);
  838. float time = input.ReadFloat(), mixRotate = input.ReadFloat(), mixX = input.ReadFloat(), mixY = input.ReadFloat(),
  839. mixScaleX = input.ReadFloat(), mixScaleY = input.ReadFloat(), mixShearY = input.ReadFloat();
  840. for (int frame = 0, bezier = 0; ; frame++) {
  841. timeline.SetFrame(frame, time, mixRotate, mixX, mixY, mixScaleX, mixScaleY, mixShearY);
  842. if (frame == frameLast) break;
  843. float time2 = input.ReadFloat(), mixRotate2 = input.ReadFloat(), mixX2 = input.ReadFloat(), mixY2 = input.ReadFloat(),
  844. mixScaleX2 = input.ReadFloat(), mixScaleY2 = input.ReadFloat(), mixShearY2 = input.ReadFloat();
  845. switch (input.ReadUByte()) {
  846. case CURVE_STEPPED:
  847. timeline.SetStepped(frame);
  848. break;
  849. case CURVE_BEZIER:
  850. SetBezier(input, timeline, bezier++, frame, 0, time, time2, mixRotate, mixRotate2, 1);
  851. SetBezier(input, timeline, bezier++, frame, 1, time, time2, mixX, mixX2, 1);
  852. SetBezier(input, timeline, bezier++, frame, 2, time, time2, mixY, mixY2, 1);
  853. SetBezier(input, timeline, bezier++, frame, 3, time, time2, mixScaleX, mixScaleX2, 1);
  854. SetBezier(input, timeline, bezier++, frame, 4, time, time2, mixScaleY, mixScaleY2, 1);
  855. SetBezier(input, timeline, bezier++, frame, 5, time, time2, mixShearY, mixShearY2, 1);
  856. break;
  857. }
  858. time = time2;
  859. mixRotate = mixRotate2;
  860. mixX = mixX2;
  861. mixY = mixY2;
  862. mixScaleX = mixScaleX2;
  863. mixScaleY = mixScaleY2;
  864. mixShearY = mixShearY2;
  865. }
  866. timelines.Add(timeline);
  867. }
  868. // Path constraint timelines.
  869. for (int i = 0, n = input.ReadInt(true); i < n; i++) {
  870. int index = input.ReadInt(true);
  871. PathConstraintData data = skeletonData.pathConstraints.Items[index];
  872. for (int ii = 0, nn = input.ReadInt(true); ii < nn; ii++) {
  873. int type = input.ReadUByte(), frameCount = input.ReadInt(true), bezierCount = input.ReadInt(true);
  874. switch (type) {
  875. case PATH_POSITION:
  876. ReadTimeline(input, timelines, new PathConstraintPositionTimeline(frameCount, bezierCount, index),
  877. data.positionMode == PositionMode.Fixed ? scale : 1);
  878. break;
  879. case PATH_SPACING:
  880. ReadTimeline(input, timelines, new PathConstraintSpacingTimeline(frameCount, bezierCount, index),
  881. data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed ? scale : 1);
  882. break;
  883. case PATH_MIX:
  884. PathConstraintMixTimeline timeline = new PathConstraintMixTimeline(frameCount, bezierCount, index);
  885. float time = input.ReadFloat(), mixRotate = input.ReadFloat(), mixX = input.ReadFloat(), mixY = input.ReadFloat();
  886. for (int frame = 0, bezier = 0, frameLast = timeline.FrameCount - 1; ; frame++) {
  887. timeline.SetFrame(frame, time, mixRotate, mixX, mixY);
  888. if (frame == frameLast) break;
  889. float time2 = input.ReadFloat(), mixRotate2 = input.ReadFloat(), mixX2 = input.ReadFloat(),
  890. mixY2 = input.ReadFloat();
  891. switch (input.ReadUByte()) {
  892. case CURVE_STEPPED:
  893. timeline.SetStepped(frame);
  894. break;
  895. case CURVE_BEZIER:
  896. SetBezier(input, timeline, bezier++, frame, 0, time, time2, mixRotate, mixRotate2, 1);
  897. SetBezier(input, timeline, bezier++, frame, 1, time, time2, mixX, mixX2, 1);
  898. SetBezier(input, timeline, bezier++, frame, 2, time, time2, mixY, mixY2, 1);
  899. break;
  900. }
  901. time = time2;
  902. mixRotate = mixRotate2;
  903. mixX = mixX2;
  904. mixY = mixY2;
  905. }
  906. timelines.Add(timeline);
  907. break;
  908. }
  909. }
  910. }
  911. // Physics timelines.
  912. for (int i = 0, n = input.ReadInt(true); i < n; i++) {
  913. int index = input.ReadInt(true) - 1;
  914. for (int ii = 0, nn = input.ReadInt(true); ii < nn; ii++) {
  915. int type = input.ReadUByte(), frameCount = input.ReadInt(true);
  916. if (type == PHYSICS_RESET) {
  917. PhysicsConstraintResetTimeline timeline = new PhysicsConstraintResetTimeline(frameCount, index);
  918. for (int frame = 0; frame < frameCount; frame++)
  919. timeline.SetFrame(frame, input.ReadFloat());
  920. timelines.Add(timeline);
  921. continue;
  922. }
  923. int bezierCount = input.ReadInt(true);
  924. switch (type) {
  925. case PHYSICS_INERTIA:
  926. ReadTimeline(input, timelines, new PhysicsConstraintInertiaTimeline(frameCount, bezierCount, index), 1);
  927. break;
  928. case PHYSICS_STRENGTH:
  929. ReadTimeline(input, timelines, new PhysicsConstraintStrengthTimeline(frameCount, bezierCount, index), 1);
  930. break;
  931. case PHYSICS_DAMPING:
  932. ReadTimeline(input, timelines, new PhysicsConstraintDampingTimeline(frameCount, bezierCount, index), 1);
  933. break;
  934. case PHYSICS_MASS:
  935. ReadTimeline(input, timelines, new PhysicsConstraintMassTimeline(frameCount, bezierCount, index), 1);
  936. break;
  937. case PHYSICS_WIND:
  938. ReadTimeline(input, timelines, new PhysicsConstraintWindTimeline(frameCount, bezierCount, index), 1);
  939. break;
  940. case PHYSICS_GRAVITY:
  941. ReadTimeline(input, timelines, new PhysicsConstraintGravityTimeline(frameCount, bezierCount, index), 1);
  942. break;
  943. case PHYSICS_MIX:
  944. ReadTimeline(input, timelines, new PhysicsConstraintMixTimeline(frameCount, bezierCount, index), 1);
  945. break;
  946. }
  947. }
  948. }
  949. // Attachment timelines.
  950. for (int i = 0, n = input.ReadInt(true); i < n; i++) {
  951. Skin skin = skeletonData.skins.Items[input.ReadInt(true)];
  952. for (int ii = 0, nn = input.ReadInt(true); ii < nn; ii++) {
  953. int slotIndex = input.ReadInt(true);
  954. for (int iii = 0, nnn = input.ReadInt(true); iii < nnn; iii++) {
  955. String attachmentName = input.ReadStringRef();
  956. Attachment attachment = skin.GetAttachment(slotIndex, attachmentName);
  957. if (attachment == null) throw new SerializationException("Timeline attachment not found: " + attachmentName);
  958. int timelineType = input.ReadUByte(), frameCount = input.ReadInt(true), frameLast = frameCount - 1;
  959. switch (timelineType) {
  960. case ATTACHMENT_DEFORM: {
  961. VertexAttachment vertexAttachment = (VertexAttachment)attachment;
  962. bool weighted = vertexAttachment.Bones != null;
  963. float[] vertices = vertexAttachment.Vertices;
  964. int deformLength = weighted ? (vertices.Length / 3) << 1 : vertices.Length;
  965. DeformTimeline timeline = new DeformTimeline(frameCount, input.ReadInt(true), slotIndex, vertexAttachment);
  966. float time = input.ReadFloat();
  967. for (int frame = 0, bezier = 0; ; frame++) {
  968. float[] deform;
  969. int end = input.ReadInt(true);
  970. if (end == 0)
  971. deform = weighted ? new float[deformLength] : vertices;
  972. else {
  973. deform = new float[deformLength];
  974. int start = input.ReadInt(true);
  975. end += start;
  976. if (scale == 1) {
  977. for (int v = start; v < end; v++)
  978. deform[v] = input.ReadFloat();
  979. } else {
  980. for (int v = start; v < end; v++)
  981. deform[v] = input.ReadFloat() * scale;
  982. }
  983. if (!weighted) {
  984. for (int v = 0, vn = deform.Length; v < vn; v++)
  985. deform[v] += vertices[v];
  986. }
  987. }
  988. timeline.SetFrame(frame, time, deform);
  989. if (frame == frameLast) break;
  990. float time2 = input.ReadFloat();
  991. switch (input.ReadUByte()) {
  992. case CURVE_STEPPED:
  993. timeline.SetStepped(frame);
  994. break;
  995. case CURVE_BEZIER:
  996. SetBezier(input, timeline, bezier++, frame, 0, time, time2, 0, 1, 1);
  997. break;
  998. }
  999. time = time2;
  1000. }
  1001. timelines.Add(timeline);
  1002. break;
  1003. }
  1004. case ATTACHMENT_SEQUENCE: {
  1005. SequenceTimeline timeline = new SequenceTimeline(frameCount, slotIndex, attachment);
  1006. for (int frame = 0; frame < frameCount; frame++) {
  1007. float time = input.ReadFloat();
  1008. int modeAndIndex = input.ReadInt();
  1009. timeline.SetFrame(frame, time, (SequenceMode)(modeAndIndex & 0xf), modeAndIndex >> 4,
  1010. input.ReadFloat());
  1011. }
  1012. timelines.Add(timeline);
  1013. break;
  1014. } // end case
  1015. } // end switch
  1016. }
  1017. }
  1018. }
  1019. // Draw order timeline.
  1020. int drawOrderCount = input.ReadInt(true);
  1021. if (drawOrderCount > 0) {
  1022. DrawOrderTimeline timeline = new DrawOrderTimeline(drawOrderCount);
  1023. int slotCount = skeletonData.slots.Count;
  1024. for (int i = 0; i < drawOrderCount; i++) {
  1025. float time = input.ReadFloat();
  1026. int offsetCount = input.ReadInt(true);
  1027. int[] drawOrder = new int[slotCount];
  1028. for (int ii = slotCount - 1; ii >= 0; ii--)
  1029. drawOrder[ii] = -1;
  1030. int[] unchanged = new int[slotCount - offsetCount];
  1031. int originalIndex = 0, unchangedIndex = 0;
  1032. for (int ii = 0; ii < offsetCount; ii++) {
  1033. int slotIndex = input.ReadInt(true);
  1034. // Collect unchanged items.
  1035. while (originalIndex != slotIndex)
  1036. unchanged[unchangedIndex++] = originalIndex++;
  1037. // Set changed items.
  1038. drawOrder[originalIndex + input.ReadInt(true)] = originalIndex++;
  1039. }
  1040. // Collect remaining unchanged items.
  1041. while (originalIndex < slotCount)
  1042. unchanged[unchangedIndex++] = originalIndex++;
  1043. // Fill in unchanged items.
  1044. for (int ii = slotCount - 1; ii >= 0; ii--)
  1045. if (drawOrder[ii] == -1) drawOrder[ii] = unchanged[--unchangedIndex];
  1046. timeline.SetFrame(i, time, drawOrder);
  1047. }
  1048. timelines.Add(timeline);
  1049. }
  1050. // Event timeline.
  1051. int eventCount = input.ReadInt(true);
  1052. if (eventCount > 0) {
  1053. EventTimeline timeline = new EventTimeline(eventCount);
  1054. for (int i = 0; i < eventCount; i++) {
  1055. float time = input.ReadFloat();
  1056. EventData eventData = skeletonData.events.Items[input.ReadInt(true)];
  1057. Event e = new Event(time, eventData);
  1058. e.intValue = input.ReadInt(false);
  1059. e.floatValue = input.ReadFloat();
  1060. e.stringValue = input.ReadString();
  1061. if (e.stringValue == null) e.stringValue = eventData.String;
  1062. if (e.Data.AudioPath != null) {
  1063. e.volume = input.ReadFloat();
  1064. e.balance = input.ReadFloat();
  1065. }
  1066. timeline.SetFrame(i, e);
  1067. }
  1068. timelines.Add(timeline);
  1069. }
  1070. float duration = 0;
  1071. Timeline[] items = timelines.Items;
  1072. for (int i = 0, n = timelines.Count; i < n; i++)
  1073. duration = Math.Max(duration, items[i].Duration);
  1074. return new Animation(name, timelines, duration);
  1075. }
  1076. /// <exception cref="IOException">Throws IOException when a read operation fails.</exception>
  1077. private void ReadTimeline (SkeletonInput input, ExposedList<Timeline> timelines, CurveTimeline1 timeline, float scale) {
  1078. float time = input.ReadFloat(), value = input.ReadFloat() * scale;
  1079. for (int frame = 0, bezier = 0, frameLast = timeline.FrameCount - 1; ; frame++) {
  1080. timeline.SetFrame(frame, time, value);
  1081. if (frame == frameLast) break;
  1082. float time2 = input.ReadFloat(), value2 = input.ReadFloat() * scale;
  1083. switch (input.ReadUByte()) {
  1084. case CURVE_STEPPED:
  1085. timeline.SetStepped(frame);
  1086. break;
  1087. case CURVE_BEZIER:
  1088. SetBezier(input, timeline, bezier++, frame, 0, time, time2, value, value2, scale);
  1089. break;
  1090. }
  1091. time = time2;
  1092. value = value2;
  1093. }
  1094. timelines.Add(timeline);
  1095. }
  1096. /// <exception cref="IOException">Throws IOException when a read operation fails.</exception>
  1097. private void ReadTimeline (SkeletonInput input, ExposedList<Timeline> timelines, CurveTimeline2 timeline, float scale) {
  1098. float time = input.ReadFloat(), value1 = input.ReadFloat() * scale, value2 = input.ReadFloat() * scale;
  1099. for (int frame = 0, bezier = 0, frameLast = timeline.FrameCount - 1; ; frame++) {
  1100. timeline.SetFrame(frame, time, value1, value2);
  1101. if (frame == frameLast) break;
  1102. float time2 = input.ReadFloat(), nvalue1 = input.ReadFloat() * scale, nvalue2 = input.ReadFloat() * scale;
  1103. switch (input.ReadUByte()) {
  1104. case CURVE_STEPPED:
  1105. timeline.SetStepped(frame);
  1106. break;
  1107. case CURVE_BEZIER:
  1108. SetBezier(input, timeline, bezier++, frame, 0, time, time2, value1, nvalue1, scale);
  1109. SetBezier(input, timeline, bezier++, frame, 1, time, time2, value2, nvalue2, scale);
  1110. break;
  1111. }
  1112. time = time2;
  1113. value1 = nvalue1;
  1114. value2 = nvalue2;
  1115. }
  1116. timelines.Add(timeline);
  1117. }
  1118. /// <exception cref="IOException">Throws IOException when a read operation fails.</exception>
  1119. void SetBezier (SkeletonInput input, CurveTimeline timeline, int bezier, int frame, int value, float time1, float time2,
  1120. float value1, float value2, float scale) {
  1121. timeline.SetBezier(bezier, frame, value, time1, value1, input.ReadFloat(), input.ReadFloat() * scale, input.ReadFloat(),
  1122. input.ReadFloat() * scale, time2, value2);
  1123. }
  1124. internal class Vertices {
  1125. public int length;
  1126. public int[] bones;
  1127. public float[] vertices;
  1128. }
  1129. internal class SkeletonInput {
  1130. private byte[] chars = new byte[32];
  1131. private byte[] bytesBigEndian = new byte[8];
  1132. internal string[] strings;
  1133. Stream input;
  1134. public SkeletonInput (Stream input) {
  1135. this.input = input;
  1136. }
  1137. public int Read () {
  1138. return input.ReadByte();
  1139. }
  1140. /// <summary>Explicit unsigned byte variant to prevent pitfalls porting Java reference implementation
  1141. /// where byte is signed vs C# where byte is unsigned.</summary>
  1142. public byte ReadUByte () {
  1143. return (byte)input.ReadByte();
  1144. }
  1145. /// <summary>Explicit signed byte variant to prevent pitfalls porting Java reference implementation
  1146. /// where byte is signed vs C# where byte is unsigned.</summary>
  1147. public sbyte ReadSByte () {
  1148. int value = input.ReadByte();
  1149. if (value == -1) throw new EndOfStreamException();
  1150. return (sbyte)value;
  1151. }
  1152. public bool ReadBoolean () {
  1153. return input.ReadByte() != 0;
  1154. }
  1155. public float ReadFloat () {
  1156. input.Read(bytesBigEndian, 0, 4);
  1157. chars[3] = bytesBigEndian[0];
  1158. chars[2] = bytesBigEndian[1];
  1159. chars[1] = bytesBigEndian[2];
  1160. chars[0] = bytesBigEndian[3];
  1161. return BitConverter.ToSingle(chars, 0);
  1162. }
  1163. public int ReadInt () {
  1164. input.Read(bytesBigEndian, 0, 4);
  1165. return (bytesBigEndian[0] << 24)
  1166. + (bytesBigEndian[1] << 16)
  1167. + (bytesBigEndian[2] << 8)
  1168. + bytesBigEndian[3];
  1169. }
  1170. public long ReadLong () {
  1171. input.Read(bytesBigEndian, 0, 8);
  1172. return ((long)(bytesBigEndian[0]) << 56)
  1173. + ((long)(bytesBigEndian[1]) << 48)
  1174. + ((long)(bytesBigEndian[2]) << 40)
  1175. + ((long)(bytesBigEndian[3]) << 32)
  1176. + ((long)(bytesBigEndian[4]) << 24)
  1177. + ((long)(bytesBigEndian[5]) << 16)
  1178. + ((long)(bytesBigEndian[6]) << 8)
  1179. + (long)(bytesBigEndian[7]);
  1180. }
  1181. public int ReadInt (bool optimizePositive) {
  1182. int b = input.ReadByte();
  1183. int result = b & 0x7F;
  1184. if ((b & 0x80) != 0) {
  1185. b = input.ReadByte();
  1186. result |= (b & 0x7F) << 7;
  1187. if ((b & 0x80) != 0) {
  1188. b = input.ReadByte();
  1189. result |= (b & 0x7F) << 14;
  1190. if ((b & 0x80) != 0) {
  1191. b = input.ReadByte();
  1192. result |= (b & 0x7F) << 21;
  1193. if ((b & 0x80) != 0) result |= (input.ReadByte() & 0x7F) << 28;
  1194. }
  1195. }
  1196. }
  1197. return optimizePositive ? result : ((result >> 1) ^ -(result & 1));
  1198. }
  1199. public string ReadString () {
  1200. int byteCount = ReadInt(true);
  1201. switch (byteCount) {
  1202. case 0:
  1203. return null;
  1204. case 1:
  1205. return "";
  1206. }
  1207. byteCount--;
  1208. byte[] buffer = this.chars;
  1209. if (buffer.Length < byteCount) buffer = new byte[byteCount];
  1210. ReadFully(buffer, 0, byteCount);
  1211. return System.Text.Encoding.UTF8.GetString(buffer, 0, byteCount);
  1212. }
  1213. /// <return>May be null.</return>
  1214. public String ReadStringRef () {
  1215. int index = ReadInt(true);
  1216. return index == 0 ? null : strings[index - 1];
  1217. }
  1218. public void ReadFully (byte[] buffer, int offset, int length) {
  1219. while (length > 0) {
  1220. int count = input.Read(buffer, offset, length);
  1221. if (count <= 0) throw new EndOfStreamException();
  1222. offset += count;
  1223. length -= count;
  1224. }
  1225. }
  1226. /// <summary>Returns the version string of binary skeleton data.</summary>
  1227. public string GetVersionString () {
  1228. try {
  1229. // try reading 4.0+ format
  1230. long initialPosition = input.Position;
  1231. ReadLong(); // long hash
  1232. long stringPosition = input.Position;
  1233. int stringByteCount = ReadInt(true);
  1234. input.Position = stringPosition;
  1235. if (stringByteCount <= 13) {
  1236. string version = ReadString();
  1237. if (char.IsDigit(version[0]))
  1238. return version;
  1239. }
  1240. // fallback to old version format
  1241. input.Position = initialPosition;
  1242. return GetVersionStringOld3X();
  1243. } catch (Exception e) {
  1244. throw new ArgumentException("Stream does not contain valid binary Skeleton Data.\n" + e, "input");
  1245. }
  1246. }
  1247. /// <summary>Returns old 3.8 and earlier format version string of binary skeleton data.</summary>
  1248. public string GetVersionStringOld3X () {
  1249. // Hash.
  1250. int byteCount = ReadInt(true);
  1251. if (byteCount > 1) input.Position += byteCount - 1;
  1252. // Version.
  1253. byteCount = ReadInt(true);
  1254. if (byteCount > 1 && byteCount <= 13) {
  1255. byteCount--;
  1256. byte[] buffer = new byte[byteCount];
  1257. ReadFully(buffer, 0, byteCount);
  1258. return System.Text.Encoding.UTF8.GetString(buffer, 0, byteCount);
  1259. }
  1260. throw new ArgumentException("Stream does not contain valid binary Skeleton Data.");
  1261. }
  1262. }
  1263. private class LinkedMesh {
  1264. internal string parent;
  1265. internal int skinIndex, slotIndex;
  1266. internal MeshAttachment mesh;
  1267. internal bool inheritTimelines;
  1268. public LinkedMesh (MeshAttachment mesh, int skinIndex, int slotIndex, string parent, bool inheritTimelines) {
  1269. this.mesh = mesh;
  1270. this.skinIndex = skinIndex;
  1271. this.slotIndex = slotIndex;
  1272. this.parent = parent;
  1273. this.inheritTimelines = inheritTimelines;
  1274. }
  1275. }
  1276. }
  1277. }