SkeletonBinary.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. #include <spine/SkeletonBinary.h>
  31. #include <stdio.h>
  32. #include <spine/extension.h>
  33. #include <spine/AtlasAttachmentLoader.h>
  34. #include <spine/Animation.h>
  35. #include "kvec.h"
  36. typedef struct {
  37. const unsigned char* cursor;
  38. const unsigned char* end;
  39. } _dataInput;
  40. typedef struct {
  41. const char* parent;
  42. const char* skin;
  43. int slotIndex;
  44. spMeshAttachment* mesh;
  45. } _spLinkedMesh;
  46. typedef struct {
  47. spSkeletonBinary super;
  48. int ownsLoader;
  49. int linkedMeshCount;
  50. int linkedMeshCapacity;
  51. _spLinkedMesh* linkedMeshes;
  52. } _spSkeletonBinary;
  53. spSkeletonBinary* spSkeletonBinary_createWithLoader (spAttachmentLoader* attachmentLoader) {
  54. spSkeletonBinary* self = SUPER(NEW(_spSkeletonBinary));
  55. self->scale = 1;
  56. self->attachmentLoader = attachmentLoader;
  57. return self;
  58. }
  59. spSkeletonBinary* spSkeletonBinary_create (spAtlas* atlas) {
  60. spAtlasAttachmentLoader* attachmentLoader = spAtlasAttachmentLoader_create(atlas);
  61. spSkeletonBinary* self = spSkeletonBinary_createWithLoader(SUPER(attachmentLoader));
  62. SUB_CAST(_spSkeletonBinary, self)->ownsLoader = 1;
  63. return self;
  64. }
  65. void spSkeletonBinary_dispose (spSkeletonBinary* self) {
  66. int i;
  67. _spSkeletonBinary* internal = SUB_CAST(_spSkeletonBinary, self);
  68. if (internal->ownsLoader) spAttachmentLoader_dispose(self->attachmentLoader);
  69. for (i = 0; i < internal->linkedMeshCount; ++i) {
  70. FREE(internal->linkedMeshes[i].parent);
  71. FREE(internal->linkedMeshes[i].skin);
  72. }
  73. FREE(internal->linkedMeshes);
  74. FREE(self->error);
  75. FREE(self);
  76. }
  77. void _spSkeletonBinary_setError (spSkeletonBinary* self, const char* value1, const char* value2) {
  78. char message[256];
  79. int length;
  80. FREE(self->error);
  81. strcpy(message, value1);
  82. length = (int)strlen(value1);
  83. if (value2) strncat(message + length, value2, 255 - length);
  84. MALLOC_STR(self->error, message);
  85. }
  86. static unsigned char readByte (_dataInput* input) {
  87. return *input->cursor++;
  88. }
  89. static signed char readSByte (_dataInput* input) {
  90. return (signed char)readByte(input);
  91. }
  92. static int readBoolean (_dataInput* input) {
  93. return readByte(input) != 0;
  94. }
  95. static int readInt (_dataInput* input) {
  96. int result = readByte(input);
  97. result <<= 8;
  98. result |= readByte(input);
  99. result <<= 8;
  100. result |= readByte(input);
  101. result <<= 8;
  102. result |= readByte(input);
  103. return result;
  104. }
  105. static int readVarint (_dataInput* input, int/*bool*/optimizePositive) {
  106. unsigned char b = readByte(input);
  107. int value = b & 0x7F;
  108. if (b & 0x80) {
  109. b = readByte(input);
  110. value |= (b & 0x7F) << 7;
  111. if (b & 0x80) {
  112. b = readByte(input);
  113. value |= (b & 0x7F) << 14;
  114. if (b & 0x80) {
  115. b = readByte(input);
  116. value |= (b & 0x7F) << 21;
  117. if (b & 0x80) value |= (readByte(input) & 0x7F) << 28;
  118. }
  119. }
  120. }
  121. if (!optimizePositive) value = (((unsigned int)value >> 1) ^ -(value & 1));
  122. return value;
  123. }
  124. float readFloat (_dataInput* input) {
  125. union {
  126. int intValue;
  127. float floatValue;
  128. } intToFloat;
  129. intToFloat.intValue = readInt(input);
  130. return intToFloat.floatValue;
  131. }
  132. char* readString (_dataInput* input) {
  133. int length = readVarint(input, 1);
  134. char* string;
  135. if (length == 0) {
  136. return 0;
  137. }
  138. string = MALLOC(char, length);
  139. memcpy(string, input->cursor, length - 1);
  140. input->cursor += length - 1;
  141. string[length - 1] = '\0';
  142. return string;
  143. }
  144. static void readColor (_dataInput* input, float *r, float *g, float *b, float *a) {
  145. *r = readByte(input) / 255.0f;
  146. *g = readByte(input) / 255.0f;
  147. *b = readByte(input) / 255.0f;
  148. *a = readByte(input) / 255.0f;
  149. }
  150. #define ATTACHMENT_REGION 0
  151. #define ATTACHMENT_BOUNDING_BOX 1
  152. #define ATTACHMENT_MESH 2
  153. #define ATTACHMENT_LINKED_MESH 3
  154. #define ATTACHMENT_PATH 4
  155. #define BLEND_MODE_NORMAL 0
  156. #define BLEND_MODE_ADDITIVE 1
  157. #define BLEND_MODE_MULTIPLY 2
  158. #define BLEND_MODE_SCREEN 3
  159. #define CURVE_LINEAR 0
  160. #define CURVE_STEPPED 1
  161. #define CURVE_BEZIER 2
  162. #define BONE_ROTATE 0
  163. #define BONE_TRANSLATE 1
  164. #define BONE_SCALE 2
  165. #define BONE_SHEAR 3
  166. #define SLOT_ATTACHMENT 0
  167. #define SLOT_COLOR 1
  168. #define PATH_POSITION 0
  169. #define PATH_SPACING 1
  170. #define PATH_MIX 2
  171. #define PATH_POSITION_FIXED 0
  172. #define PATH_POSITION_PERCENT 1
  173. #define PATH_SPACING_LENGTH 0
  174. #define PATH_SPACING_FIXED 1
  175. #define PATH_SPACING_PERCENT 2
  176. #define PATH_ROTATE_TANGENT 0
  177. #define PATH_ROTATE_CHAIN 1
  178. #define PATH_ROTATE_CHAIN_SCALE 2
  179. static void readCurve (_dataInput* input, spCurveTimeline* timeline, int frameIndex) {
  180. switch (readByte(input)) {
  181. case CURVE_STEPPED: {
  182. spCurveTimeline_setStepped(timeline, frameIndex);
  183. break;
  184. }
  185. case CURVE_BEZIER: {
  186. float cx1 = readFloat(input);
  187. float cy1 = readFloat(input);
  188. float cx2 = readFloat(input);
  189. float cy2 = readFloat(input);
  190. spCurveTimeline_setCurve(timeline, frameIndex, cx1, cy1, cx2, cy2);
  191. break;
  192. }
  193. }
  194. }
  195. static void _spSkeletonBinary_addLinkedMesh (spSkeletonBinary* self, spMeshAttachment* mesh,
  196. const char* skin, int slotIndex, const char* parent) {
  197. _spLinkedMesh* linkedMesh;
  198. _spSkeletonBinary* internal = SUB_CAST(_spSkeletonBinary, self);
  199. if (internal->linkedMeshCount == internal->linkedMeshCapacity) {
  200. _spLinkedMesh* linkedMeshes;
  201. internal->linkedMeshCapacity *= 2;
  202. if (internal->linkedMeshCapacity < 8) internal->linkedMeshCapacity = 8;
  203. /* TODO Why not realloc? */
  204. linkedMeshes = MALLOC(_spLinkedMesh, internal->linkedMeshCapacity);
  205. memcpy(linkedMeshes, internal->linkedMeshes, sizeof(_spLinkedMesh) * internal->linkedMeshCount);
  206. FREE(internal->linkedMeshes);
  207. internal->linkedMeshes = linkedMeshes;
  208. }
  209. linkedMesh = internal->linkedMeshes + internal->linkedMeshCount++;
  210. linkedMesh->mesh = mesh;
  211. linkedMesh->skin = skin;
  212. linkedMesh->slotIndex = slotIndex;
  213. linkedMesh->parent = parent;
  214. }
  215. static spAnimation* _spSkeletonBinary_readAnimation (spSkeletonBinary* self, const char* name,
  216. _dataInput* input, spSkeletonData *skeletonData) {
  217. kvec_t(spTimeline*) timelines;
  218. float duration = 0;
  219. int i, n, ii, nn, iii, nnn;
  220. int frameIndex;
  221. int drawOrderCount, eventCount;
  222. spAnimation* animation;
  223. kv_init(timelines);
  224. /* Slot timelines. */
  225. for (i = 0, n = readVarint(input, 1); i < n; ++i) {
  226. int slotIndex = readVarint(input, 1);
  227. for (ii = 0, nn = readVarint(input, 1); ii < nn; ++ii) {
  228. unsigned char timelineType = readByte(input);
  229. int frameCount = readVarint(input, 1);
  230. switch (timelineType) {
  231. case SLOT_COLOR: {
  232. spColorTimeline* timeline = spColorTimeline_create(frameCount);
  233. timeline->slotIndex = slotIndex;
  234. for (frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
  235. float time = readFloat(input);
  236. float r, g, b, a;
  237. readColor(input, &r, &g, &b, &a);
  238. spColorTimeline_setFrame(timeline, frameIndex, time, r, g, b, a);
  239. if (frameIndex < frameCount - 1) readCurve(input, SUPER(timeline), frameIndex);
  240. }
  241. kv_push(spTimeline*, timelines, SUPER(SUPER(timeline)));
  242. duration = MAX(duration, timeline->frames[(frameCount - 1) * COLOR_ENTRIES]);
  243. break;
  244. }
  245. case SLOT_ATTACHMENT: {
  246. spAttachmentTimeline* timeline = spAttachmentTimeline_create(frameCount);
  247. timeline->slotIndex = slotIndex;
  248. for (frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
  249. float time = readFloat(input);
  250. const char* attachmentName = readString(input);
  251. /* TODO Avoid copying of attachmentName inside */
  252. spAttachmentTimeline_setFrame(timeline, frameIndex, time, attachmentName);
  253. FREE(attachmentName);
  254. }
  255. kv_push(spTimeline*, timelines, SUPER(timeline));
  256. duration = MAX(duration, timeline->frames[frameCount - 1]);
  257. break;
  258. }
  259. default: {
  260. int i;
  261. for (i = 0; i < kv_size(timelines); ++i)
  262. spTimeline_dispose(kv_A(timelines, i));
  263. kv_destroy(timelines);
  264. _spSkeletonBinary_setError(self, "Invalid timeline type for a slot: ", skeletonData->slots[slotIndex]->name);
  265. return 0;
  266. }
  267. }
  268. }
  269. }
  270. /* Bone timelines. */
  271. for (i = 0, n = readVarint(input, 1); i < n; ++i) {
  272. int boneIndex = readVarint(input, 1);
  273. for (ii = 0, nn = readVarint(input, 1); ii < nn; ++ii) {
  274. unsigned char timelineType = readByte(input);
  275. int frameCount = readVarint(input, 1);
  276. switch (timelineType) {
  277. case BONE_ROTATE: {
  278. spRotateTimeline *timeline = spRotateTimeline_create(frameCount);
  279. timeline->boneIndex = boneIndex;
  280. for (frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
  281. float time = readFloat(input);
  282. float degrees = readFloat(input);
  283. spRotateTimeline_setFrame(timeline, frameIndex, time, degrees);
  284. if (frameIndex < frameCount - 1) readCurve(input, SUPER(timeline), frameIndex);
  285. }
  286. kv_push(spTimeline*, timelines, SUPER(SUPER(timeline)));
  287. duration = MAX(duration, timeline->frames[(frameCount - 1) * ROTATE_ENTRIES]);
  288. break;
  289. }
  290. case BONE_TRANSLATE:
  291. case BONE_SCALE:
  292. case BONE_SHEAR: {
  293. float timelineScale = 1;
  294. spTranslateTimeline *timeline = 0;
  295. switch (timelineType) {
  296. case BONE_SCALE:
  297. timeline = spScaleTimeline_create(frameCount);
  298. break;
  299. case BONE_SHEAR:
  300. timeline = spShearTimeline_create(frameCount);
  301. break;
  302. case BONE_TRANSLATE:
  303. timeline = spTranslateTimeline_create(frameCount);
  304. timelineScale = self->scale;
  305. break;
  306. default:
  307. break;
  308. }
  309. timeline->boneIndex = boneIndex;
  310. for (frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
  311. float time = readFloat(input);
  312. float x = readFloat(input) * timelineScale;
  313. float y = readFloat(input) * timelineScale;
  314. spTranslateTimeline_setFrame(timeline, frameIndex, time, x, y);
  315. if (frameIndex < frameCount - 1) readCurve(input, SUPER(timeline), frameIndex);
  316. }
  317. kv_push(spTimeline*, timelines, SUPER_CAST(spTimeline, timeline));
  318. duration = MAX(duration, timeline->frames[(frameCount - 1) * TRANSLATE_ENTRIES]);
  319. break;
  320. }
  321. default: {
  322. int i;
  323. for (i = 0; i < kv_size(timelines); ++i)
  324. spTimeline_dispose(kv_A(timelines, i));
  325. kv_destroy(timelines);
  326. _spSkeletonBinary_setError(self, "Invalid timeline type for a bone: ", skeletonData->bones[boneIndex]->name);
  327. return 0;
  328. }
  329. }
  330. }
  331. }
  332. /* IK constraint timelines. */
  333. for (i = 0, n = readVarint(input, 1); i < n; ++i) {
  334. int index = readVarint(input, 1);
  335. int frameCount = readVarint(input, 1);
  336. spIkConstraintTimeline* timeline = spIkConstraintTimeline_create(frameCount);
  337. timeline->ikConstraintIndex = index;
  338. for (frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
  339. float time = readFloat(input);
  340. float mix = readFloat(input);
  341. char bendDirection = readSByte(input);
  342. spIkConstraintTimeline_setFrame(timeline, frameIndex, time, mix, bendDirection);
  343. if (frameIndex < frameCount - 1) readCurve(input, SUPER(timeline), frameIndex);
  344. }
  345. kv_push(spTimeline*, timelines, SUPER(SUPER(timeline)));
  346. duration = MAX(duration, timeline->frames[(frameCount - 1) * IKCONSTRAINT_ENTRIES]);
  347. }
  348. /* Transform constraint timelines. */
  349. for (i = 0, n = readVarint(input, 1); i < n; ++i) {
  350. int index = readVarint(input, 1);
  351. int frameCount = readVarint(input, 1);
  352. spTransformConstraintTimeline* timeline = spTransformConstraintTimeline_create(frameCount);
  353. timeline->transformConstraintIndex = index;
  354. for (frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
  355. float time = readFloat(input);
  356. float rotateMix = readFloat(input);
  357. float translateMix = readFloat(input);
  358. float scaleMix = readFloat(input);
  359. float shearMix = readFloat(input);
  360. spTransformConstraintTimeline_setFrame(timeline, frameIndex, time, rotateMix, translateMix,
  361. scaleMix, shearMix);
  362. if (frameIndex < frameCount - 1) readCurve(input, SUPER(timeline), frameIndex);
  363. }
  364. kv_push(spTimeline*, timelines, SUPER(SUPER(timeline)));
  365. duration = MAX(duration, timeline->frames[(frameCount - 1) * TRANSFORMCONSTRAINT_ENTRIES]);
  366. }
  367. /* Path constraint timelines. */
  368. for (i = 0, n = readVarint(input, 1); i < n; ++i) {
  369. int index = readVarint(input, 1);
  370. spPathConstraintData* data = skeletonData->pathConstraints[index];
  371. for (ii = 0, nn = readVarint(input, 1); ii < nn; ++ii) {
  372. unsigned char timelineType = readByte(input);
  373. int frameCount = readVarint(input, 1);
  374. switch (timelineType) {
  375. case PATH_POSITION:
  376. case PATH_SPACING: {
  377. spPathConstraintPositionTimeline* timeline = 0;
  378. float timelineScale = 1;
  379. if (timelineType == PATH_SPACING) {
  380. timeline = (spPathConstraintPositionTimeline*)spPathConstraintSpacingTimeline_create(frameCount);
  381. if (data->spacingMode == SP_SPACING_MODE_LENGTH || data->spacingMode == SP_SPACING_MODE_FIXED)
  382. timelineScale = self->scale;
  383. } else {
  384. timeline = spPathConstraintPositionTimeline_create(frameCount);
  385. if (data->positionMode == SP_POSITION_MODE_FIXED)
  386. timelineScale = self->scale;
  387. }
  388. timeline->pathConstraintIndex = index;
  389. for (frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
  390. float time = readFloat(input);
  391. float value = readFloat(input) * timelineScale;
  392. spPathConstraintPositionTimeline_setFrame(timeline, frameIndex, time, value);
  393. if (frameIndex < frameCount - 1) readCurve(input, SUPER(timeline), frameIndex);
  394. }
  395. kv_push(spTimeline*, timelines, SUPER(SUPER(timeline)));
  396. duration = MAX(duration, timeline->frames[(frameCount - 1) * PATHCONSTRAINTPOSITION_ENTRIES]);
  397. break;
  398. }
  399. case PATH_MIX: {
  400. spPathConstraintMixTimeline* timeline = spPathConstraintMixTimeline_create(frameCount);
  401. timeline->pathConstraintIndex = index;
  402. for (frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
  403. float time = readFloat(input);
  404. float rotateMix = readFloat(input);
  405. float translateMix = readFloat(input);
  406. spPathConstraintMixTimeline_setFrame(timeline, frameIndex, time, rotateMix, translateMix);
  407. if (frameIndex < frameCount - 1) readCurve(input, SUPER(timeline), frameIndex);
  408. }
  409. kv_push(spTimeline*, timelines, SUPER(SUPER(timeline)));
  410. duration = MAX(duration, timeline->frames[(frameCount - 1) * PATHCONSTRAINTMIX_ENTRIES]);
  411. }
  412. }
  413. }
  414. }
  415. /* Deform timelines. */
  416. for (i = 0, n = readVarint(input, 1); i < n; ++i) {
  417. spSkin* skin = skeletonData->skins[readVarint(input, 1)];
  418. for (ii = 0, nn = readVarint(input, 1); ii < nn; ++ii) {
  419. int slotIndex = readVarint(input, 1);
  420. for (iii = 0, nnn = readVarint(input, 1); iii < nnn; ++iii) {
  421. float* tempDeform;
  422. spDeformTimeline *timeline;
  423. int weighted, deformLength;
  424. const char* attachmentName = readString(input);
  425. int frameCount;
  426. spVertexAttachment* attachment = SUB_CAST(spVertexAttachment,
  427. spSkin_getAttachment(skin, slotIndex, attachmentName));
  428. if (!attachment) {
  429. int i;
  430. for (i = 0; i < kv_size(timelines); ++i)
  431. spTimeline_dispose(kv_A(timelines, i));
  432. kv_destroy(timelines);
  433. _spSkeletonBinary_setError(self, "Attachment not found: ", attachmentName);
  434. FREE(attachmentName);
  435. return 0;
  436. }
  437. FREE(attachmentName);
  438. weighted = attachment->bones != 0;
  439. deformLength = weighted ? attachment->verticesCount / 3 * 2 : attachment->verticesCount;
  440. tempDeform = MALLOC(float, deformLength);
  441. frameCount = readVarint(input, 1);
  442. timeline = spDeformTimeline_create(frameCount, deformLength);
  443. timeline->slotIndex = slotIndex;
  444. timeline->attachment = SUPER(attachment);
  445. for (frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
  446. float time = readFloat(input);
  447. float* deform;
  448. int end = readVarint(input, 1);
  449. if (!end) {
  450. if (weighted) {
  451. deform = tempDeform;
  452. memset(deform, 0, sizeof(float) * deformLength);
  453. } else
  454. deform = attachment->vertices;
  455. } else {
  456. int v, start = readVarint(input, 1);
  457. deform = tempDeform;
  458. memset(deform, 0, sizeof(float) * start);
  459. end += start;
  460. if (self->scale == 1) {
  461. for (v = start; v < end; ++v)
  462. deform[v] = readFloat(input);
  463. } else {
  464. for (v = start; v < end; ++v)
  465. deform[v] = readFloat(input) * self->scale;
  466. }
  467. memset(deform + v, 0, sizeof(float) * (deformLength - v));
  468. if (!weighted) {
  469. float* vertices = attachment->vertices;
  470. for (v = 0; v < deformLength; ++v)
  471. deform[v] += vertices[v];
  472. }
  473. }
  474. spDeformTimeline_setFrame(timeline, frameIndex, time, deform);
  475. if (frameIndex < frameCount - 1) readCurve(input, SUPER(timeline), frameIndex);
  476. }
  477. FREE(tempDeform);
  478. kv_push(spTimeline*, timelines, SUPER(SUPER(timeline)));
  479. duration = MAX(duration, timeline->frames[frameCount - 1]);
  480. }
  481. }
  482. }
  483. /* Draw order timeline. */
  484. drawOrderCount = readVarint(input, 1);
  485. if (drawOrderCount) {
  486. spDrawOrderTimeline* timeline = spDrawOrderTimeline_create(drawOrderCount, skeletonData->slotsCount);
  487. for (i = 0; i < drawOrderCount; ++i) {
  488. float time = readFloat(input);
  489. int offsetCount = readVarint(input, 1);
  490. int* drawOrder = MALLOC(int, skeletonData->slotsCount);
  491. int* unchanged = MALLOC(int, skeletonData->slotsCount - offsetCount);
  492. int originalIndex = 0, unchangedIndex = 0;
  493. memset(drawOrder, -1, sizeof(int) * skeletonData->slotsCount);
  494. for (ii = 0; ii < offsetCount; ++ii) {
  495. int slotIndex = readVarint(input, 1);
  496. /* Collect unchanged items. */
  497. while (originalIndex != slotIndex)
  498. unchanged[unchangedIndex++] = originalIndex++;
  499. /* Set changed items. */
  500. drawOrder[originalIndex + readVarint(input, 1)] = originalIndex;
  501. ++originalIndex;
  502. }
  503. /* Collect remaining unchanged items. */
  504. while (originalIndex < skeletonData->slotsCount)
  505. unchanged[unchangedIndex++] = originalIndex++;
  506. /* Fill in unchanged items. */
  507. for (ii = skeletonData->slotsCount - 1; ii >= 0; ii--)
  508. if (drawOrder[ii] == -1) drawOrder[ii] = unchanged[--unchangedIndex];
  509. FREE(unchanged);
  510. /* TODO Avoid copying of drawOrder inside */
  511. spDrawOrderTimeline_setFrame(timeline, i, time, drawOrder);
  512. FREE(drawOrder);
  513. }
  514. kv_push(spTimeline*, timelines, SUPER(timeline));
  515. duration = MAX(duration, timeline->frames[drawOrderCount - 1]);
  516. }
  517. /* Event timeline. */
  518. eventCount = readVarint(input, 1);
  519. if (eventCount) {
  520. spEventTimeline* timeline = spEventTimeline_create(eventCount);
  521. for (i = 0; i < eventCount; ++i) {
  522. float time = readFloat(input);
  523. spEventData* eventData = skeletonData->events[readVarint(input, 1)];
  524. spEvent* event = spEvent_create(time, eventData);
  525. event->intValue = readVarint(input, 0);
  526. event->floatValue = readFloat(input);
  527. if (readBoolean(input))
  528. event->stringValue = readString(input);
  529. else
  530. MALLOC_STR(event->stringValue, eventData->stringValue);
  531. spEventTimeline_setFrame(timeline, i, event);
  532. }
  533. kv_push(spTimeline*, timelines, SUPER(timeline));
  534. duration = MAX(duration, timeline->frames[eventCount - 1]);
  535. }
  536. kv_trim(spTimeline*, timelines);
  537. animation = spAnimation_create(name, 0);
  538. animation->duration = duration;
  539. animation->timelinesCount = kv_size(timelines);
  540. animation->timelines = kv_array(timelines);
  541. return animation;
  542. }
  543. static float* _readFloatArray(_dataInput *input, int n, float scale) {
  544. float* array = MALLOC(float, n);
  545. int i;
  546. if (scale == 1)
  547. for (i = 0; i < n; ++i)
  548. array[i] = readFloat(input);
  549. else
  550. for (i = 0; i < n; ++i)
  551. array[i] = readFloat(input) * scale;
  552. return array;
  553. }
  554. static short* _readShortArray(_dataInput *input, int *length) {
  555. int n = readVarint(input, 1);
  556. short* array = MALLOC(short, n);
  557. int i;
  558. *length = n;
  559. for (i = 0; i < n; ++i) {
  560. array[i] = readByte(input) << 8;
  561. array[i] |= readByte(input);
  562. }
  563. return array;
  564. }
  565. static void _readVertices(spSkeletonBinary* self, _dataInput* input, spVertexAttachment* attachment,
  566. int vertexCount) {
  567. int i, ii;
  568. int verticesLength = vertexCount << 1;
  569. kvec_t(float) weights;
  570. kvec_t(int) bones;
  571. attachment->worldVerticesLength = verticesLength;
  572. if (!readBoolean(input)) {
  573. attachment->verticesCount = verticesLength;
  574. attachment->vertices = _readFloatArray(input, verticesLength, self->scale);
  575. attachment->bonesCount = 0;
  576. attachment->bones = 0;
  577. return;
  578. }
  579. kv_init(weights);
  580. kv_resize(float, weights, verticesLength * 3 * 3);
  581. kv_init(bones);
  582. kv_resize(int, bones, verticesLength * 3);
  583. for (i = 0; i < vertexCount; ++i) {
  584. int boneCount = readVarint(input, 1);
  585. kv_push(int, bones, boneCount);
  586. for (ii = 0; ii < boneCount; ++ii) {
  587. kv_push(int, bones, readVarint(input, 1));
  588. kv_push(float, weights, readFloat(input) * self->scale);
  589. kv_push(float, weights, readFloat(input) * self->scale);
  590. kv_push(float, weights, readFloat(input));
  591. }
  592. }
  593. kv_trim(float, weights);
  594. attachment->verticesCount = kv_size(weights);
  595. attachment->vertices = kv_array(weights);
  596. kv_trim(int, bones);
  597. attachment->bonesCount = kv_size(bones);
  598. attachment->bones = kv_array(bones);
  599. }
  600. spAttachment* spSkeletonBinary_readAttachment(spSkeletonBinary* self, _dataInput* input,
  601. spSkin* skin, int slotIndex, const char* attachmentName, int/*bool*/ nonessential) {
  602. int i;
  603. spAttachmentType type;
  604. const char* name = readString(input);
  605. int freeName = name != 0;
  606. if (!name) {
  607. freeName = 0;
  608. name = attachmentName;
  609. }
  610. type = (spAttachmentType)readByte(input);
  611. switch (type) {
  612. case SP_ATTACHMENT_REGION: {
  613. const char* path = readString(input);
  614. spAttachment* attachment;
  615. spRegionAttachment* region;
  616. if (!path) MALLOC_STR(path, name);
  617. attachment = spAttachmentLoader_createAttachment(
  618. self->attachmentLoader, skin, type, name, path);
  619. region = SUB_CAST(spRegionAttachment, attachment);
  620. region->path = path;
  621. region->rotation = readFloat(input);
  622. region->x = readFloat(input) * self->scale;
  623. region->y = readFloat(input) * self->scale;
  624. region->scaleX = readFloat(input);
  625. region->scaleY = readFloat(input);
  626. region->width = readFloat(input) * self->scale;
  627. region->height = readFloat(input) * self->scale;
  628. readColor(input, &region->r, &region->g, &region->b, &region->a);
  629. spRegionAttachment_updateOffset(region);
  630. spAttachmentLoader_configureAttachment(self->attachmentLoader, attachment);
  631. if (freeName) FREE(name);
  632. return attachment;
  633. }
  634. case SP_ATTACHMENT_BOUNDING_BOX: {
  635. int vertexCount = readVarint(input, 1);
  636. spAttachment* attachment = spAttachmentLoader_createAttachment(
  637. self->attachmentLoader, skin, type, name, 0);
  638. _readVertices(self, input, SUB_CAST(spVertexAttachment, attachment), vertexCount);
  639. if (nonessential) readInt(input); /* Skip color. */
  640. spAttachmentLoader_configureAttachment(self->attachmentLoader, attachment);
  641. if (freeName) FREE(name);
  642. return attachment;
  643. }
  644. case SP_ATTACHMENT_MESH: {
  645. int vertexCount;
  646. spAttachment* attachment;
  647. spMeshAttachment* mesh;
  648. const char* path = readString(input);
  649. if (!path) MALLOC_STR(path, name);
  650. attachment = spAttachmentLoader_createAttachment(self->attachmentLoader, skin, type, name, path);
  651. mesh = SUB_CAST(spMeshAttachment, attachment);
  652. mesh->path = path;
  653. readColor(input, &mesh->r, &mesh->g, &mesh->b, &mesh->a);
  654. vertexCount = readVarint(input, 1);
  655. mesh->regionUVs = _readFloatArray(input, vertexCount << 1, 1);
  656. mesh->triangles = (unsigned short*)_readShortArray(input, &mesh->trianglesCount);
  657. _readVertices(self, input, SUPER(mesh), vertexCount);
  658. spMeshAttachment_updateUVs(mesh);
  659. mesh->hullLength = readVarint(input, 1) << 1;
  660. if (nonessential) {
  661. mesh->edges = (int*)_readShortArray(input, &mesh->edgesCount);
  662. mesh->width = readFloat(input) * self->scale;
  663. mesh->height = readFloat(input) * self->scale;
  664. } else {
  665. mesh->edges = 0;
  666. mesh->width = 0;
  667. mesh->height = 0;
  668. }
  669. spAttachmentLoader_configureAttachment(self->attachmentLoader, attachment);
  670. if (freeName) FREE(name);
  671. return attachment;
  672. }
  673. case SP_ATTACHMENT_LINKED_MESH: {
  674. const char* skinName;
  675. const char* parent;
  676. spAttachment* attachment;
  677. spMeshAttachment* mesh;
  678. const char* path = readString(input);
  679. if (!path) MALLOC_STR(path, name);
  680. attachment = spAttachmentLoader_createAttachment(self->attachmentLoader, skin, type, name, path);
  681. mesh = SUB_CAST(spMeshAttachment, attachment);
  682. mesh->path = path;
  683. readColor(input, &mesh->r, &mesh->g, &mesh->b, &mesh->a);
  684. skinName = readString(input);
  685. parent = readString(input);
  686. mesh->inheritDeform = readBoolean(input);
  687. if (nonessential) {
  688. mesh->width = readFloat(input) * self->scale;
  689. mesh->height = readFloat(input) * self->scale;
  690. }
  691. _spSkeletonBinary_addLinkedMesh(self, mesh, skinName, slotIndex, parent);
  692. if (freeName) FREE(name);
  693. return attachment;
  694. }
  695. case SP_ATTACHMENT_PATH: {
  696. spAttachment* attachment = spAttachmentLoader_createAttachment(
  697. self->attachmentLoader, skin, type, name, 0);
  698. spPathAttachment* path = SUB_CAST(spPathAttachment, attachment);
  699. int vertexCount = 0;
  700. path->closed = readBoolean(input);
  701. path->constantSpeed = readBoolean(input);
  702. vertexCount = readVarint(input, 1);
  703. _readVertices(self, input, SUPER(path), vertexCount);
  704. path->lengthsLength = vertexCount / 3;
  705. path->lengths = MALLOC(float, path->lengthsLength);
  706. for (i = 0; i < path->lengthsLength; ++i) {
  707. path->lengths[i] = readFloat(input) * self->scale;
  708. }
  709. if (nonessential) readInt(input); /* Skip color. */
  710. if (freeName) FREE(name);
  711. return attachment;
  712. }
  713. }
  714. if (freeName) FREE(name);
  715. return 0;
  716. }
  717. spSkin* spSkeletonBinary_readSkin(spSkeletonBinary* self, _dataInput* input,
  718. const char* skinName, int/*bool*/ nonessential) {
  719. spSkin* skin;
  720. int slotCount = readVarint(input, 1);
  721. int i, ii, nn;
  722. if (slotCount == 0)
  723. return 0;
  724. skin = spSkin_create(skinName);
  725. for (i = 0; i < slotCount; ++i) {
  726. int slotIndex = readVarint(input, 1);
  727. for (ii = 0, nn = readVarint(input, 1); ii < nn; ++ii) {
  728. const char* name = readString(input);
  729. spAttachment* attachment = spSkeletonBinary_readAttachment(self, input, skin, slotIndex, name, nonessential);
  730. if (attachment) spSkin_addAttachment(skin, slotIndex, name, attachment);
  731. FREE(name);
  732. }
  733. }
  734. return skin;
  735. }
  736. spSkeletonData* spSkeletonBinary_readSkeletonDataFile (spSkeletonBinary* self, const char* path) {
  737. int length;
  738. spSkeletonData* skeletonData;
  739. const char* binary = _spUtil_readFile(path, &length);
  740. if (length == 0 || !binary) {
  741. _spSkeletonBinary_setError(self, "Unable to read skeleton file: ", path);
  742. return 0;
  743. }
  744. skeletonData = spSkeletonBinary_readSkeletonData(self, (unsigned char*)binary, length);
  745. FREE(binary);
  746. return skeletonData;
  747. }
  748. spSkeletonData* spSkeletonBinary_readSkeletonData (spSkeletonBinary* self, const unsigned char* binary,
  749. const int length) {
  750. int i, ii, nonessential;
  751. spSkeletonData* skeletonData;
  752. _spSkeletonBinary* internal = SUB_CAST(_spSkeletonBinary, self);
  753. _dataInput* input = NEW(_dataInput);
  754. input->cursor = binary;
  755. input->end = binary + length;
  756. FREE(self->error);
  757. CONST_CAST(char*, self->error) = 0;
  758. internal->linkedMeshCount = 0;
  759. skeletonData = spSkeletonData_create();
  760. skeletonData->hash = readString(input);
  761. if (!strlen(skeletonData->hash)) {
  762. FREE(skeletonData->hash);
  763. skeletonData->hash = 0;
  764. }
  765. skeletonData->version = readString(input);
  766. if (!strlen(skeletonData->version)) {
  767. FREE(skeletonData->version);
  768. skeletonData->version = 0;
  769. }
  770. skeletonData->width = readFloat(input);
  771. skeletonData->height = readFloat(input);
  772. nonessential = readBoolean(input);
  773. if (nonessential) {
  774. /* Skip images path & fps */
  775. readFloat(input);
  776. FREE(readString(input));
  777. }
  778. /* Bones. */
  779. skeletonData->bonesCount = readVarint(input, 1);
  780. skeletonData->bones = MALLOC(spBoneData*, skeletonData->bonesCount);
  781. for (i = 0; i < skeletonData->bonesCount; ++i) {
  782. spBoneData* data;
  783. int mode;
  784. const char* name = readString(input);
  785. spBoneData* parent = i == 0 ? 0 : skeletonData->bones[readVarint(input, 1)];
  786. /* TODO Avoid copying of name */
  787. data = spBoneData_create(i, name, parent);
  788. FREE(name);
  789. data->rotation = readFloat(input);
  790. data->x = readFloat(input) * self->scale;
  791. data->y = readFloat(input) * self->scale;
  792. data->scaleX = readFloat(input);
  793. data->scaleY = readFloat(input);
  794. data->shearX = readFloat(input);
  795. data->shearY = readFloat(input);
  796. data->length = readFloat(input) * self->scale;
  797. mode = readVarint(input, 1);
  798. switch (mode) {
  799. case 0: data->transformMode = SP_TRANSFORMMODE_NORMAL; break;
  800. case 1: data->transformMode = SP_TRANSFORMMODE_ONLYTRANSLATION; break;
  801. case 2: data->transformMode = SP_TRANSFORMMODE_NOROTATIONORREFLECTION; break;
  802. case 3: data->transformMode = SP_TRANSFORMMODE_NOSCALE; break;
  803. case 4: data->transformMode = SP_TRANSFORMMODE_NOSCALEORREFLECTION; break;
  804. }
  805. if (nonessential) readInt(input); /* Skip bone color. */
  806. skeletonData->bones[i] = data;
  807. }
  808. /* Slots. */
  809. skeletonData->slotsCount = readVarint(input, 1);
  810. skeletonData->slots = MALLOC(spSlotData*, skeletonData->slotsCount);
  811. for (i = 0; i < skeletonData->slotsCount; ++i) {
  812. const char* slotName = readString(input);
  813. spBoneData* boneData = skeletonData->bones[readVarint(input, 1)];
  814. /* TODO Avoid copying of slotName */
  815. spSlotData* slotData = spSlotData_create(i, slotName, boneData);
  816. FREE(slotName);
  817. readColor(input, &slotData->r, &slotData->g, &slotData->b, &slotData->a);
  818. slotData->attachmentName = readString(input);
  819. slotData->blendMode = (spBlendMode)readVarint(input, 1);
  820. skeletonData->slots[i] = slotData;
  821. }
  822. /* IK constraints. */
  823. skeletonData->ikConstraintsCount = readVarint(input, 1);
  824. skeletonData->ikConstraints = MALLOC(spIkConstraintData*, skeletonData->ikConstraintsCount);
  825. for (i = 0; i < skeletonData->ikConstraintsCount; ++i) {
  826. const char* name = readString(input);
  827. /* TODO Avoid copying of name */
  828. spIkConstraintData* data = spIkConstraintData_create(name);
  829. data->order = readVarint(input, 1);
  830. FREE(name);
  831. data->bonesCount = readVarint(input, 1);
  832. data->bones = MALLOC(spBoneData*, data->bonesCount);
  833. for (ii = 0; ii < data->bonesCount; ++ii)
  834. data->bones[ii] = skeletonData->bones[readVarint(input, 1)];
  835. data->target = skeletonData->bones[readVarint(input, 1)];
  836. data->mix = readFloat(input);
  837. data->bendDirection = readSByte(input);
  838. skeletonData->ikConstraints[i] = data;
  839. }
  840. /* Transform constraints. */
  841. skeletonData->transformConstraintsCount = readVarint(input, 1);
  842. skeletonData->transformConstraints = MALLOC(
  843. spTransformConstraintData*, skeletonData->transformConstraintsCount);
  844. for (i = 0; i < skeletonData->transformConstraintsCount; ++i) {
  845. const char* name = readString(input);
  846. /* TODO Avoid copying of name */
  847. spTransformConstraintData* data = spTransformConstraintData_create(name);
  848. data->order = readVarint(input, 1);
  849. FREE(name);
  850. data->bonesCount = readVarint(input, 1);
  851. CONST_CAST(spBoneData**, data->bones) = MALLOC(spBoneData*, data->bonesCount);
  852. for (ii = 0; ii < data->bonesCount; ++ii)
  853. data->bones[ii] = skeletonData->bones[readVarint(input, 1)];
  854. data->target = skeletonData->bones[readVarint(input, 1)];
  855. data->offsetRotation = readFloat(input);
  856. data->offsetX = readFloat(input) * self->scale;
  857. data->offsetY = readFloat(input) * self->scale;
  858. data->offsetScaleX = readFloat(input);
  859. data->offsetScaleY = readFloat(input);
  860. data->offsetShearY = readFloat(input);
  861. data->rotateMix = readFloat(input);
  862. data->translateMix = readFloat(input);
  863. data->scaleMix = readFloat(input);
  864. data->shearMix = readFloat(input);
  865. skeletonData->transformConstraints[i] = data;
  866. }
  867. /* Path constraints */
  868. skeletonData->pathConstraintsCount = readVarint(input, 1);
  869. skeletonData->pathConstraints = MALLOC(spPathConstraintData*, skeletonData->pathConstraintsCount);
  870. for (i = 0; i < skeletonData->pathConstraintsCount; ++i) {
  871. const char* name = readString(input);
  872. /* TODO Avoid copying of name */
  873. spPathConstraintData* data = spPathConstraintData_create(name);
  874. data->order = readVarint(input, 1);
  875. FREE(name);
  876. data->bonesCount = readVarint(input, 1);
  877. CONST_CAST(spBoneData**, data->bones) = MALLOC(spBoneData*, data->bonesCount);
  878. for (ii = 0; ii < data->bonesCount; ++ii)
  879. data->bones[ii] = skeletonData->bones[readVarint(input, 1)];
  880. data->target = skeletonData->slots[readVarint(input, 1)];
  881. data->positionMode = (spPositionMode)readVarint(input, 1);
  882. data->spacingMode = (spSpacingMode)readVarint(input, 1);
  883. data->rotateMode = (spRotateMode)readVarint(input, 1);
  884. data->offsetRotation = readFloat(input);
  885. data->position = readFloat(input);
  886. if (data->positionMode == SP_POSITION_MODE_FIXED) data->position *= self->scale;
  887. data->spacing = readFloat(input);
  888. if (data->spacingMode == SP_SPACING_MODE_LENGTH || data->spacingMode == SP_SPACING_MODE_FIXED) data->spacing *= self->scale;
  889. data->rotateMix = readFloat(input);
  890. data->translateMix = readFloat(input);
  891. skeletonData->pathConstraints[i] = data;
  892. }
  893. /* Default skin. */
  894. skeletonData->defaultSkin = spSkeletonBinary_readSkin(self, input, "default", nonessential);
  895. skeletonData->skinsCount = readVarint(input, 1);
  896. if (skeletonData->defaultSkin)
  897. ++skeletonData->skinsCount;
  898. skeletonData->skins = MALLOC(spSkin*, skeletonData->skinsCount);
  899. if (skeletonData->defaultSkin)
  900. skeletonData->skins[0] = skeletonData->defaultSkin;
  901. /* Skins. */
  902. for (i = skeletonData->defaultSkin ? 1 : 0; i < skeletonData->skinsCount; ++i) {
  903. const char* skinName = readString(input);
  904. /* TODO Avoid copying of skinName */
  905. skeletonData->skins[i] = spSkeletonBinary_readSkin(self, input, skinName, nonessential);
  906. FREE(skinName);
  907. }
  908. /* Linked meshes. */
  909. for (i = 0; i < internal->linkedMeshCount; ++i) {
  910. _spLinkedMesh* linkedMesh = internal->linkedMeshes + i;
  911. spSkin* skin = !linkedMesh->skin ? skeletonData->defaultSkin : spSkeletonData_findSkin(skeletonData, linkedMesh->skin);
  912. spAttachment* parent;
  913. if (!skin) {
  914. FREE(input);
  915. spSkeletonData_dispose(skeletonData);
  916. _spSkeletonBinary_setError(self, "Skin not found: ", linkedMesh->skin);
  917. return 0;
  918. }
  919. parent = spSkin_getAttachment(skin, linkedMesh->slotIndex, linkedMesh->parent);
  920. if (!parent) {
  921. FREE(input);
  922. spSkeletonData_dispose(skeletonData);
  923. _spSkeletonBinary_setError(self, "Parent mesh not found: ", linkedMesh->parent);
  924. return 0;
  925. }
  926. spMeshAttachment_setParentMesh(linkedMesh->mesh, SUB_CAST(spMeshAttachment, parent));
  927. spMeshAttachment_updateUVs(linkedMesh->mesh);
  928. spAttachmentLoader_configureAttachment(self->attachmentLoader, SUPER(SUPER(linkedMesh->mesh)));
  929. }
  930. /* Events. */
  931. skeletonData->eventsCount = readVarint(input, 1);
  932. skeletonData->events = MALLOC(spEventData*, skeletonData->eventsCount);
  933. for (i = 0; i < skeletonData->eventsCount; ++i) {
  934. const char* name = readString(input);
  935. /* TODO Avoid copying of skinName */
  936. spEventData* eventData = spEventData_create(name);
  937. FREE(name);
  938. eventData->intValue = readVarint(input, 0);
  939. eventData->floatValue = readFloat(input);
  940. eventData->stringValue = readString(input);
  941. skeletonData->events[i] = eventData;
  942. }
  943. /* Animations. */
  944. skeletonData->animationsCount = readVarint(input, 1);
  945. skeletonData->animations = MALLOC(spAnimation*, skeletonData->animationsCount);
  946. for (i = 0; i < skeletonData->animationsCount; ++i) {
  947. const char* name = readString(input);
  948. spAnimation* animation = _spSkeletonBinary_readAnimation(self, name, input, skeletonData);
  949. FREE(name);
  950. if (!animation) {
  951. FREE(input);
  952. spSkeletonData_dispose(skeletonData);
  953. return 0;
  954. }
  955. skeletonData->animations[i] = animation;
  956. }
  957. FREE(input);
  958. return skeletonData;
  959. }