SkeletonBinary.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #ifdef SPINE_UE4
  30. #include "SpinePluginPrivatePCH.h"
  31. #endif
  32. #include <spine/SkeletonBinary.h>
  33. #include <spine/SkeletonData.h>
  34. #include <spine/Atlas.h>
  35. #include <spine/AtlasAttachmentLoader.h>
  36. #include <spine/LinkedMesh.h>
  37. #include <spine/Skin.h>
  38. #include <spine/Attachment.h>
  39. #include <spine/VertexAttachment.h>
  40. #include <spine/Animation.h>
  41. #include <spine/CurveTimeline.h>
  42. #include <spine/ContainerUtil.h>
  43. #include <spine/BoneData.h>
  44. #include <spine/SlotData.h>
  45. #include <spine/IkConstraintData.h>
  46. #include <spine/TransformConstraintData.h>
  47. #include <spine/PathConstraintData.h>
  48. #include <spine/AttachmentType.h>
  49. #include <spine/RegionAttachment.h>
  50. #include <spine/BoundingBoxAttachment.h>
  51. #include <spine/MeshAttachment.h>
  52. #include <spine/PathAttachment.h>
  53. #include <spine/PointAttachment.h>
  54. #include <spine/ClippingAttachment.h>
  55. #include <spine/EventData.h>
  56. #include <spine/AttachmentTimeline.h>
  57. #include <spine/ColorTimeline.h>
  58. #include <spine/TwoColorTimeline.h>
  59. #include <spine/RotateTimeline.h>
  60. #include <spine/TranslateTimeline.h>
  61. #include <spine/ScaleTimeline.h>
  62. #include <spine/ShearTimeline.h>
  63. #include <spine/IkConstraintTimeline.h>
  64. #include <spine/TransformConstraintTimeline.h>
  65. #include <spine/PathConstraintPositionTimeline.h>
  66. #include <spine/PathConstraintSpacingTimeline.h>
  67. #include <spine/PathConstraintMixTimeline.h>
  68. #include <spine/DeformTimeline.h>
  69. #include <spine/DrawOrderTimeline.h>
  70. #include <spine/EventTimeline.h>
  71. #include <spine/Event.h>
  72. using namespace spine;
  73. const int SkeletonBinary::BONE_ROTATE = 0;
  74. const int SkeletonBinary::BONE_TRANSLATE = 1;
  75. const int SkeletonBinary::BONE_SCALE = 2;
  76. const int SkeletonBinary::BONE_SHEAR = 3;
  77. const int SkeletonBinary::SLOT_ATTACHMENT = 0;
  78. const int SkeletonBinary::SLOT_COLOR = 1;
  79. const int SkeletonBinary::SLOT_TWO_COLOR = 2;
  80. const int SkeletonBinary::PATH_POSITION = 0;
  81. const int SkeletonBinary::PATH_SPACING = 1;
  82. const int SkeletonBinary::PATH_MIX = 2;
  83. const int SkeletonBinary::CURVE_LINEAR = 0;
  84. const int SkeletonBinary::CURVE_STEPPED = 1;
  85. const int SkeletonBinary::CURVE_BEZIER = 2;
  86. SkeletonBinary::SkeletonBinary(Atlas *atlasArray) : _attachmentLoader(
  87. new(__FILE__, __LINE__) AtlasAttachmentLoader(atlasArray)), _error(), _scale(1), _ownsLoader(true) {
  88. }
  89. SkeletonBinary::SkeletonBinary(AttachmentLoader *attachmentLoader) : _attachmentLoader(attachmentLoader), _error(),
  90. _scale(1), _ownsLoader(false)
  91. {
  92. assert(_attachmentLoader != NULL);
  93. }
  94. SkeletonBinary::~SkeletonBinary() {
  95. ContainerUtil::cleanUpVectorOfPointers(_linkedMeshes);
  96. _linkedMeshes.clear();
  97. if (_ownsLoader) delete _attachmentLoader;
  98. }
  99. SkeletonData *SkeletonBinary::readSkeletonData(const unsigned char *binary, const int length) {
  100. bool nonessential;
  101. SkeletonData *skeletonData;
  102. DataInput *input = new(__FILE__, __LINE__) DataInput();
  103. input->cursor = binary;
  104. input->end = binary + length;
  105. _linkedMeshes.clear();
  106. skeletonData = new(__FILE__, __LINE__) SkeletonData();
  107. char *skeletonData_hash = readString(input);
  108. skeletonData->_hash.own(skeletonData_hash);
  109. char *skeletonData_version = readString(input);
  110. skeletonData->_version.own(skeletonData_version);
  111. if ("3.8.75" == skeletonData->_version) {
  112. delete input;
  113. delete skeletonData;
  114. setError("Unsupported skeleton data, please export with a newer version of Spine.", "");
  115. return NULL;
  116. }
  117. skeletonData->_x = readFloat(input);
  118. skeletonData->_y = readFloat(input);
  119. skeletonData->_width = readFloat(input);
  120. skeletonData->_height = readFloat(input);
  121. nonessential = readBoolean(input);
  122. if (nonessential) {
  123. /* Skip images path, audio path & fps */
  124. skeletonData->_fps = readFloat(input);
  125. skeletonData->_imagesPath.own(readString(input));
  126. skeletonData->_audioPath.own(readString(input));
  127. }
  128. int numStrings = readVarint(input, true);
  129. for (int i = 0; i < numStrings; i++)
  130. skeletonData->_strings.add(readString(input));
  131. /* Bones. */
  132. int numBones = readVarint(input, true);
  133. skeletonData->_bones.setSize(numBones, 0);
  134. for (int i = 0; i < numBones; ++i) {
  135. const char *name = readString(input);
  136. BoneData *parent = i == 0 ? 0 : skeletonData->_bones[readVarint(input, true)];
  137. BoneData *data = new(__FILE__, __LINE__) BoneData(i, String(name, true), parent);
  138. data->_rotation = readFloat(input);
  139. data->_x = readFloat(input) * _scale;
  140. data->_y = readFloat(input) * _scale;
  141. data->_scaleX = readFloat(input);
  142. data->_scaleY = readFloat(input);
  143. data->_shearX = readFloat(input);
  144. data->_shearY = readFloat(input);
  145. data->_length = readFloat(input) * _scale;
  146. data->_transformMode = static_cast<TransformMode>(readVarint(input, true));
  147. data->_skinRequired = readBoolean(input);
  148. if (nonessential) readInt(input); /* Skip bone color. */
  149. skeletonData->_bones[i] = data;
  150. }
  151. /* Slots. */
  152. int slotsCount = readVarint(input, true);
  153. skeletonData->_slots.setSize(slotsCount, 0);
  154. for (int i = 0; i < slotsCount; ++i) {
  155. const char *slotName = readString(input);
  156. BoneData *boneData = skeletonData->_bones[readVarint(input, true)];
  157. SlotData *slotData = new(__FILE__, __LINE__) SlotData(i, String(slotName, true), *boneData);
  158. readColor(input, slotData->getColor());
  159. unsigned char r = readByte(input);
  160. unsigned char g = readByte(input);
  161. unsigned char b = readByte(input);
  162. unsigned char a = readByte(input);
  163. if (!(r == 0xff && g == 0xff && b == 0xff && a == 0xff)) {
  164. slotData->getDarkColor().set(r / 255.0f, g / 255.0f, b / 255.0f, 1);
  165. slotData->setHasDarkColor(true);
  166. }
  167. slotData->_attachmentName = readStringRef(input, skeletonData);
  168. slotData->_blendMode = static_cast<BlendMode>(readVarint(input, true));
  169. skeletonData->_slots[i] = slotData;
  170. }
  171. /* IK constraints. */
  172. int ikConstraintsCount = readVarint(input, true);
  173. skeletonData->_ikConstraints.setSize(ikConstraintsCount, 0);
  174. for (int i = 0; i < ikConstraintsCount; ++i) {
  175. const char *name = readString(input);
  176. IkConstraintData *data = new(__FILE__, __LINE__) IkConstraintData(String(name, true));
  177. data->setOrder(readVarint(input, true));
  178. data->setSkinRequired(readBoolean(input));
  179. int bonesCount = readVarint(input, true);
  180. data->_bones.setSize(bonesCount, 0);
  181. for (int ii = 0; ii < bonesCount; ++ii)
  182. data->_bones[ii] = skeletonData->_bones[readVarint(input, true)];
  183. data->_target = skeletonData->_bones[readVarint(input, true)];
  184. data->_mix = readFloat(input);
  185. data->_softness = readFloat(input) * _scale;
  186. data->_bendDirection = readSByte(input);
  187. data->_compress = readBoolean(input);
  188. data->_stretch = readBoolean(input);
  189. data->_uniform = readBoolean(input);
  190. skeletonData->_ikConstraints[i] = data;
  191. }
  192. /* Transform constraints. */
  193. int transformConstraintsCount = readVarint(input, true);
  194. skeletonData->_transformConstraints.setSize(transformConstraintsCount, 0);
  195. for (int i = 0; i < transformConstraintsCount; ++i) {
  196. const char *name = readString(input);
  197. TransformConstraintData *data = new(__FILE__, __LINE__) TransformConstraintData(String(name, true));
  198. data->setOrder(readVarint(input, true));
  199. data->setSkinRequired(readBoolean(input));
  200. int bonesCount = readVarint(input, true);
  201. data->_bones.setSize(bonesCount, 0);
  202. for (int ii = 0; ii < bonesCount; ++ii)
  203. data->_bones[ii] = skeletonData->_bones[readVarint(input, true)];
  204. data->_target = skeletonData->_bones[readVarint(input, true)];
  205. data->_local = readBoolean(input);
  206. data->_relative = readBoolean(input);
  207. data->_offsetRotation = readFloat(input);
  208. data->_offsetX = readFloat(input) * _scale;
  209. data->_offsetY = readFloat(input) * _scale;
  210. data->_offsetScaleX = readFloat(input);
  211. data->_offsetScaleY = readFloat(input);
  212. data->_offsetShearY = readFloat(input);
  213. data->_rotateMix = readFloat(input);
  214. data->_translateMix = readFloat(input);
  215. data->_scaleMix = readFloat(input);
  216. data->_shearMix = readFloat(input);
  217. skeletonData->_transformConstraints[i] = data;
  218. }
  219. /* Path constraints */
  220. int pathConstraintsCount = readVarint(input, true);
  221. skeletonData->_pathConstraints.setSize(pathConstraintsCount, 0);
  222. for (int i = 0; i < pathConstraintsCount; ++i) {
  223. const char *name = readString(input);
  224. PathConstraintData *data = new(__FILE__, __LINE__) PathConstraintData(String(name, true));
  225. data->setOrder(readVarint(input, true));
  226. data->setSkinRequired(readBoolean(input));
  227. int bonesCount = readVarint(input, true);
  228. data->_bones.setSize(bonesCount, 0);
  229. for (int ii = 0; ii < bonesCount; ++ii)
  230. data->_bones[ii] = skeletonData->_bones[readVarint(input, true)];
  231. data->_target = skeletonData->_slots[readVarint(input, true)];
  232. data->_positionMode = static_cast<PositionMode>(readVarint(input, true));
  233. data->_spacingMode = static_cast<SpacingMode>(readVarint(input, true));
  234. data->_rotateMode = static_cast<RotateMode>(readVarint(input, true));
  235. data->_offsetRotation = readFloat(input);
  236. data->_position = readFloat(input);
  237. if (data->_positionMode == PositionMode_Fixed) data->_position *= _scale;
  238. data->_spacing = readFloat(input);
  239. if (data->_spacingMode == SpacingMode_Length || data->_spacingMode == SpacingMode_Fixed)
  240. data->_spacing *= _scale;
  241. data->_rotateMix = readFloat(input);
  242. data->_translateMix = readFloat(input);
  243. skeletonData->_pathConstraints[i] = data;
  244. }
  245. /* Default skin. */
  246. Skin* defaultSkin = readSkin(input, true, skeletonData, nonessential);
  247. if (defaultSkin) {
  248. skeletonData->_defaultSkin = defaultSkin;
  249. skeletonData->_skins.add(defaultSkin);
  250. }
  251. /* Skins. */
  252. for (size_t i = 0, n = (size_t)readVarint(input, true); i < n; ++i)
  253. skeletonData->_skins.add(readSkin(input, false, skeletonData, nonessential));
  254. /* Linked meshes. */
  255. for (int i = 0, n = _linkedMeshes.size(); i < n; ++i) {
  256. LinkedMesh *linkedMesh = _linkedMeshes[i];
  257. Skin *skin = linkedMesh->_skin.length() == 0 ? skeletonData->getDefaultSkin() : skeletonData->findSkin(
  258. linkedMesh->_skin);
  259. if (skin == NULL) {
  260. delete input;
  261. delete skeletonData;
  262. setError("Skin not found: ", linkedMesh->_skin.buffer());
  263. return NULL;
  264. }
  265. Attachment *parent = skin->getAttachment(linkedMesh->_slotIndex, linkedMesh->_parent);
  266. if (parent == NULL) {
  267. delete input;
  268. delete skeletonData;
  269. setError("Parent mesh not found: ", linkedMesh->_parent.buffer());
  270. return NULL;
  271. }
  272. linkedMesh->_mesh->_deformAttachment = linkedMesh->_inheritDeform ? static_cast<VertexAttachment*>(parent) : linkedMesh->_mesh;
  273. linkedMesh->_mesh->setParentMesh(static_cast<MeshAttachment *>(parent));
  274. linkedMesh->_mesh->updateUVs();
  275. _attachmentLoader->configureAttachment(linkedMesh->_mesh);
  276. }
  277. ContainerUtil::cleanUpVectorOfPointers(_linkedMeshes);
  278. _linkedMeshes.clear();
  279. /* Events. */
  280. int eventsCount = readVarint(input, true);
  281. skeletonData->_events.setSize(eventsCount, 0);
  282. for (int i = 0; i < eventsCount; ++i) {
  283. const char *name = readStringRef(input, skeletonData);
  284. EventData *eventData = new(__FILE__, __LINE__) EventData(String(name));
  285. eventData->_intValue = readVarint(input, false);
  286. eventData->_floatValue = readFloat(input);
  287. eventData->_stringValue.own(readString(input));
  288. eventData->_audioPath.own(readString(input)); // skip audio path
  289. if (!eventData->_audioPath.isEmpty()) {
  290. eventData->_volume = readFloat(input);
  291. eventData->_balance = readFloat(input);
  292. }
  293. skeletonData->_events[i] = eventData;
  294. }
  295. /* Animations. */
  296. int animationsCount = readVarint(input, true);
  297. skeletonData->_animations.setSize(animationsCount, 0);
  298. for (int i = 0; i < animationsCount; ++i) {
  299. String name(readString(input), true);
  300. Animation *animation = readAnimation(name, input, skeletonData);
  301. if (!animation) {
  302. delete input;
  303. delete skeletonData;
  304. return NULL;
  305. }
  306. skeletonData->_animations[i] = animation;
  307. }
  308. delete input;
  309. return skeletonData;
  310. }
  311. SkeletonData *SkeletonBinary::readSkeletonDataFile(const String &path) {
  312. int length;
  313. SkeletonData *skeletonData;
  314. const char *binary = SpineExtension::readFile(path.buffer(), &length);
  315. if (length == 0 || !binary) {
  316. setError("Unable to read skeleton file: ", path.buffer());
  317. return NULL;
  318. }
  319. skeletonData = readSkeletonData((unsigned char *) binary, length);
  320. SpineExtension::free(binary, __FILE__, __LINE__);
  321. return skeletonData;
  322. }
  323. void SkeletonBinary::setError(const char *value1, const char *value2) {
  324. char message[256];
  325. int length;
  326. strcpy(message, value1);
  327. length = (int) strlen(value1);
  328. if (value2) strncat(message + length, value2, 255 - length);
  329. _error = String(message);
  330. }
  331. char *SkeletonBinary::readString(DataInput *input) {
  332. int length = readVarint(input, true);
  333. char *string;
  334. if (length == 0) return NULL;
  335. string = SpineExtension::alloc<char>(length, __FILE__, __LINE__);
  336. memcpy(string, input->cursor, length - 1);
  337. input->cursor += length - 1;
  338. string[length - 1] = '\0';
  339. return string;
  340. }
  341. char* SkeletonBinary::readStringRef(DataInput* input, SkeletonData* skeletonData) {
  342. int index = readVarint(input, true);
  343. return index == 0 ? nullptr : skeletonData->_strings[index - 1];
  344. }
  345. float SkeletonBinary::readFloat(DataInput *input) {
  346. union {
  347. int intValue;
  348. float floatValue;
  349. } intToFloat;
  350. intToFloat.intValue = readInt(input);
  351. return intToFloat.floatValue;
  352. }
  353. unsigned char SkeletonBinary::readByte(DataInput *input) {
  354. return *input->cursor++;
  355. }
  356. signed char SkeletonBinary::readSByte(DataInput *input) {
  357. return (signed char) readByte(input);
  358. }
  359. bool SkeletonBinary::readBoolean(DataInput *input) {
  360. return readByte(input) != 0;
  361. }
  362. int SkeletonBinary::readInt(DataInput *input) {
  363. int result = readByte(input);
  364. result <<= 8;
  365. result |= readByte(input);
  366. result <<= 8;
  367. result |= readByte(input);
  368. result <<= 8;
  369. result |= readByte(input);
  370. return result;
  371. }
  372. void SkeletonBinary::readColor(DataInput *input, Color &color) {
  373. color.r = readByte(input) / 255.0f;
  374. color.g = readByte(input) / 255.0f;
  375. color.b = readByte(input) / 255.0f;
  376. color.a = readByte(input) / 255.0f;
  377. }
  378. int SkeletonBinary::readVarint(DataInput *input, bool optimizePositive) {
  379. unsigned char b = readByte(input);
  380. int value = b & 0x7F;
  381. if (b & 0x80) {
  382. b = readByte(input);
  383. value |= (b & 0x7F) << 7;
  384. if (b & 0x80) {
  385. b = readByte(input);
  386. value |= (b & 0x7F) << 14;
  387. if (b & 0x80) {
  388. b = readByte(input);
  389. value |= (b & 0x7F) << 21;
  390. if (b & 0x80) value |= (readByte(input) & 0x7F) << 28;
  391. }
  392. }
  393. }
  394. if (!optimizePositive) value = (((unsigned int) value >> 1) ^ -(value & 1));
  395. return value;
  396. }
  397. Skin *SkeletonBinary::readSkin(DataInput *input, bool defaultSkin, SkeletonData *skeletonData, bool nonessential) {
  398. Skin *skin;
  399. int slotCount = 0;
  400. if (defaultSkin) {
  401. slotCount = readVarint(input, true);
  402. if (slotCount == 0) return NULL;
  403. skin = new(__FILE__, __LINE__) Skin("default");
  404. } else {
  405. skin = new(__FILE__, __LINE__) Skin(readStringRef(input, skeletonData));
  406. for (int i = 0, n = readVarint(input, true); i < n; i++)
  407. skin->getBones().add(skeletonData->_bones[readVarint(input, true)]);
  408. for (int i = 0, n = readVarint(input, true); i < n; i++)
  409. skin->getConstraints().add(skeletonData->_ikConstraints[readVarint(input, true)]);
  410. for (int i = 0, n = readVarint(input, true); i < n; i++)
  411. skin->getConstraints().add(skeletonData->_transformConstraints[readVarint(input, true)]);
  412. for (int i = 0, n = readVarint(input, true); i < n; i++)
  413. skin->getConstraints().add(skeletonData->_pathConstraints[readVarint(input, true)]);
  414. slotCount = readVarint(input, true);
  415. }
  416. for (int i = 0; i < slotCount; ++i) {
  417. int slotIndex = readVarint(input, true);
  418. for (int ii = 0, nn = readVarint(input, true); ii < nn; ++ii) {
  419. String name(readStringRef(input, skeletonData));
  420. Attachment *attachment = readAttachment(input, skin, slotIndex, name, skeletonData, nonessential);
  421. if (attachment) skin->setAttachment(slotIndex, String(name), attachment);
  422. }
  423. }
  424. return skin;
  425. }
  426. Attachment *SkeletonBinary::readAttachment(DataInput *input, Skin *skin, int slotIndex, const String &attachmentName,
  427. SkeletonData *skeletonData, bool nonessential
  428. ) {
  429. String name(readStringRef(input, skeletonData));
  430. if (name.isEmpty()) name = attachmentName;
  431. AttachmentType type = static_cast<AttachmentType>(readByte(input));
  432. switch (type) {
  433. case AttachmentType_Region: {
  434. String path(readStringRef(input, skeletonData));
  435. if (path.isEmpty()) path = name;
  436. RegionAttachment *region = _attachmentLoader->newRegionAttachment(*skin, String(name), String(path));
  437. region->_path = path;
  438. region->_rotation = readFloat(input);
  439. region->_x = readFloat(input) * _scale;
  440. region->_y = readFloat(input) * _scale;
  441. region->_scaleX = readFloat(input);
  442. region->_scaleY = readFloat(input);
  443. region->_width = readFloat(input) * _scale;
  444. region->_height = readFloat(input) * _scale;
  445. readColor(input, region->getColor());
  446. region->updateOffset();
  447. _attachmentLoader->configureAttachment(region);
  448. return region;
  449. }
  450. case AttachmentType_Boundingbox: {
  451. int vertexCount = readVarint(input, true);
  452. BoundingBoxAttachment *box = _attachmentLoader->newBoundingBoxAttachment(*skin, String(name));
  453. readVertices(input, static_cast<VertexAttachment *>(box), vertexCount);
  454. if (nonessential) {
  455. /* Skip color. */
  456. readInt(input);
  457. }
  458. _attachmentLoader->configureAttachment(box);
  459. return box;
  460. }
  461. case AttachmentType_Mesh: {
  462. int vertexCount;
  463. MeshAttachment *mesh;
  464. String path(readStringRef(input, skeletonData));
  465. if (path.isEmpty()) path = name;
  466. mesh = _attachmentLoader->newMeshAttachment(*skin, String(name), String(path));
  467. mesh->_path = path;
  468. readColor(input, mesh->getColor());
  469. vertexCount = readVarint(input, true);
  470. readFloatArray(input, vertexCount << 1, 1, mesh->getRegionUVs());
  471. readShortArray(input, mesh->getTriangles());
  472. readVertices(input, static_cast<VertexAttachment *>(mesh), vertexCount);
  473. mesh->updateUVs();
  474. mesh->_hullLength = readVarint(input, true) << 1;
  475. if (nonessential) {
  476. readShortArray(input, mesh->getEdges());
  477. mesh->_width = readFloat(input) * _scale;
  478. mesh->_height = readFloat(input) * _scale;
  479. } else {
  480. mesh->_width = 0;
  481. mesh->_height = 0;
  482. }
  483. _attachmentLoader->configureAttachment(mesh);
  484. return mesh;
  485. }
  486. case AttachmentType_Linkedmesh: {
  487. String path(readStringRef(input, skeletonData));
  488. if (path.isEmpty()) path = name;
  489. MeshAttachment *mesh = _attachmentLoader->newMeshAttachment(*skin, String(name), String(path));
  490. mesh->_path = path;
  491. readColor(input, mesh->getColor());
  492. String skinName(readStringRef(input, skeletonData));
  493. String parent(readStringRef(input, skeletonData));
  494. bool inheritDeform = readBoolean(input);
  495. if (nonessential) {
  496. mesh->_width = readFloat(input) * _scale;
  497. mesh->_height = readFloat(input) * _scale;
  498. }
  499. LinkedMesh *linkedMesh = new(__FILE__, __LINE__) LinkedMesh(mesh, String(skinName), slotIndex,
  500. String(parent), inheritDeform);
  501. _linkedMeshes.add(linkedMesh);
  502. return mesh;
  503. }
  504. case AttachmentType_Path: {
  505. PathAttachment *path = _attachmentLoader->newPathAttachment(*skin, String(name));
  506. path->_closed = readBoolean(input);
  507. path->_constantSpeed = readBoolean(input);
  508. int vertexCount = readVarint(input, true);
  509. readVertices(input, static_cast<VertexAttachment *>(path), vertexCount);
  510. int lengthsLength = vertexCount / 3;
  511. path->_lengths.setSize(lengthsLength, 0);
  512. for (int i = 0; i < lengthsLength; ++i) {
  513. path->_lengths[i] = readFloat(input) * _scale;
  514. }
  515. if (nonessential) {
  516. /* Skip color. */
  517. readInt(input);
  518. }
  519. _attachmentLoader->configureAttachment(path);
  520. return path;
  521. }
  522. case AttachmentType_Point: {
  523. PointAttachment *point = _attachmentLoader->newPointAttachment(*skin, String(name));
  524. point->_rotation = readFloat(input);
  525. point->_x = readFloat(input) * _scale;
  526. point->_y = readFloat(input) * _scale;
  527. if (nonessential) {
  528. /* Skip color. */
  529. readInt(input);
  530. }
  531. _attachmentLoader->configureAttachment(point);
  532. return point;
  533. }
  534. case AttachmentType_Clipping: {
  535. int endSlotIndex = readVarint(input, true);
  536. int vertexCount = readVarint(input, true);
  537. ClippingAttachment *clip = _attachmentLoader->newClippingAttachment(*skin, name);
  538. readVertices(input, static_cast<VertexAttachment *>(clip), vertexCount);
  539. clip->_endSlot = skeletonData->_slots[endSlotIndex];
  540. if (nonessential) {
  541. /* Skip color. */
  542. readInt(input);
  543. }
  544. _attachmentLoader->configureAttachment(clip);
  545. return clip;
  546. }
  547. }
  548. return NULL;
  549. }
  550. void SkeletonBinary::readVertices(DataInput *input, VertexAttachment *attachment, int vertexCount) {
  551. float scale = _scale;
  552. int verticesLength = vertexCount << 1;
  553. attachment->setWorldVerticesLength(vertexCount << 1);
  554. if (!readBoolean(input)) {
  555. readFloatArray(input, verticesLength, scale, attachment->getVertices());
  556. return;
  557. }
  558. Vector<float> &vertices = attachment->getVertices();
  559. Vector<size_t> &bones = attachment->getBones();
  560. vertices.ensureCapacity(verticesLength * 3 * 3);
  561. bones.ensureCapacity(verticesLength * 3);
  562. for (int i = 0; i < vertexCount; ++i) {
  563. int boneCount = readVarint(input, true);
  564. bones.add(boneCount);
  565. for (int ii = 0; ii < boneCount; ++ii) {
  566. bones.add(readVarint(input, true));
  567. vertices.add(readFloat(input) * scale);
  568. vertices.add(readFloat(input) * scale);
  569. vertices.add(readFloat(input));
  570. }
  571. }
  572. }
  573. void SkeletonBinary::readFloatArray(DataInput *input, int n, float scale, Vector<float> &array) {
  574. array.setSize(n, 0);
  575. int i;
  576. if (scale == 1) {
  577. for (i = 0; i < n; ++i) {
  578. array[i] = readFloat(input);
  579. }
  580. } else {
  581. for (i = 0; i < n; ++i) {
  582. array[i] = readFloat(input) * scale;
  583. }
  584. }
  585. }
  586. void SkeletonBinary::readShortArray(DataInput *input, Vector<unsigned short> &array) {
  587. int n = readVarint(input, true);
  588. array.setSize(n, 0);
  589. int i;
  590. for (i = 0; i < n; ++i) {
  591. array[i] = readByte(input) << 8;
  592. array[i] |= readByte(input);
  593. }
  594. }
  595. Animation *SkeletonBinary::readAnimation(const String &name, DataInput *input, SkeletonData *skeletonData) {
  596. Vector<Timeline *> timelines;
  597. float scale = _scale;
  598. float duration = 0;
  599. // Slot timelines.
  600. for (int i = 0, n = readVarint(input, true); i < n; ++i) {
  601. int slotIndex = readVarint(input, true);
  602. for (int ii = 0, nn = readVarint(input, true); ii < nn; ++ii) {
  603. unsigned char timelineType = readByte(input);
  604. int frameCount = readVarint(input, true);
  605. switch (timelineType) {
  606. case SLOT_ATTACHMENT: {
  607. AttachmentTimeline *timeline = new(__FILE__, __LINE__) AttachmentTimeline(frameCount);
  608. timeline->_slotIndex = slotIndex;
  609. for (int frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
  610. float time = readFloat(input);
  611. String attachmentName(readStringRef(input, skeletonData));
  612. timeline->setFrame(frameIndex, time, attachmentName);
  613. }
  614. timelines.add(timeline);
  615. duration = MathUtil::max(duration, timeline->_frames[frameCount - 1]);
  616. break;
  617. }
  618. case SLOT_COLOR: {
  619. ColorTimeline *timeline = new(__FILE__, __LINE__) ColorTimeline(frameCount);
  620. timeline->_slotIndex = slotIndex;
  621. for (int frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
  622. float time = readFloat(input);
  623. int color = readInt(input);
  624. float r = ((color & 0xff000000) >> 24) / 255.0f;
  625. float g = ((color & 0x00ff0000) >> 16) / 255.0f;
  626. float b = ((color & 0x0000ff00) >> 8) / 255.0f;
  627. float a = ((color & 0x000000ff)) / 255.0f;
  628. timeline->setFrame(frameIndex, time, r, g, b, a);
  629. if (frameIndex < frameCount - 1) readCurve(input, frameIndex, timeline);
  630. }
  631. timelines.add(timeline);
  632. duration = MathUtil::max(duration, timeline->_frames[(frameCount - 1) * ColorTimeline::ENTRIES]);
  633. break;
  634. }
  635. case SLOT_TWO_COLOR: {
  636. TwoColorTimeline *timeline = new(__FILE__, __LINE__) TwoColorTimeline(frameCount);
  637. timeline->_slotIndex = slotIndex;
  638. for (int frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
  639. float time = readFloat(input);
  640. int color = readInt(input);
  641. float r = ((color & 0xff000000) >> 24) / 255.0f;
  642. float g = ((color & 0x00ff0000) >> 16) / 255.0f;
  643. float b = ((color & 0x0000ff00) >> 8) / 255.0f;
  644. float a = ((color & 0x000000ff)) / 255.0f;
  645. int color2 = readInt(input); // 0x00rrggbb
  646. float r2 = ((color2 & 0x00ff0000) >> 16) / 255.0f;
  647. float g2 = ((color2 & 0x0000ff00) >> 8) / 255.0f;
  648. float b2 = ((color2 & 0x000000ff)) / 255.0f;
  649. timeline->setFrame(frameIndex, time, r, g, b, a, r2, g2, b2);
  650. if (frameIndex < frameCount - 1) readCurve(input, frameIndex, timeline);
  651. }
  652. timelines.add(timeline);
  653. duration = MathUtil::max(duration, timeline->_frames[(frameCount - 1) * TwoColorTimeline::ENTRIES]);
  654. break;
  655. }
  656. default: {
  657. ContainerUtil::cleanUpVectorOfPointers(timelines);
  658. setError("Invalid timeline type for a slot: ", skeletonData->_slots[slotIndex]->_name.buffer());
  659. return NULL;
  660. }
  661. }
  662. }
  663. }
  664. // Bone timelines.
  665. for (int i = 0, n = readVarint(input, true); i < n; ++i) {
  666. int boneIndex = readVarint(input, true);
  667. for (int ii = 0, nn = readVarint(input, true); ii < nn; ++ii) {
  668. unsigned char timelineType = readByte(input);
  669. int frameCount = readVarint(input, true);
  670. switch (timelineType) {
  671. case BONE_ROTATE: {
  672. RotateTimeline *timeline = new(__FILE__, __LINE__) RotateTimeline(frameCount);
  673. timeline->_boneIndex = boneIndex;
  674. for (int frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
  675. float time = readFloat(input);
  676. float degrees = readFloat(input);
  677. timeline->setFrame(frameIndex, time, degrees);
  678. if (frameIndex < frameCount - 1) readCurve(input, frameIndex, timeline);
  679. }
  680. timelines.add(timeline);
  681. duration = MathUtil::max(duration, timeline->_frames[(frameCount - 1) * RotateTimeline::ENTRIES]);
  682. break;
  683. }
  684. case BONE_TRANSLATE:
  685. case BONE_SCALE:
  686. case BONE_SHEAR: {
  687. TranslateTimeline *timeline;
  688. float timelineScale = 1;
  689. if (timelineType == BONE_SCALE) {
  690. timeline = new(__FILE__, __LINE__) ScaleTimeline(frameCount);
  691. } else if (timelineType == BONE_SHEAR) {
  692. timeline = new(__FILE__, __LINE__) ShearTimeline(frameCount);
  693. } else {
  694. timeline = new(__FILE__, __LINE__) TranslateTimeline(frameCount);
  695. timelineScale = scale;
  696. }
  697. timeline->_boneIndex = boneIndex;
  698. for (int frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
  699. float time = readFloat(input);
  700. float x = readFloat(input) * timelineScale;
  701. float y = readFloat(input) * timelineScale;
  702. timeline->setFrame(frameIndex, time, x, y);
  703. if (frameIndex < frameCount - 1) {
  704. readCurve(input, frameIndex, timeline);
  705. }
  706. }
  707. timelines.add(timeline);
  708. duration = MathUtil::max(duration, timeline->_frames[(frameCount - 1) * TranslateTimeline::ENTRIES]);
  709. break;
  710. }
  711. default: {
  712. ContainerUtil::cleanUpVectorOfPointers(timelines);
  713. setError("Invalid timeline type for a bone: ", skeletonData->_bones[boneIndex]->_name.buffer());
  714. return NULL;
  715. }
  716. }
  717. }
  718. }
  719. // IK timelines.
  720. for (int i = 0, n = readVarint(input, true); i < n; ++i) {
  721. int index = readVarint(input, true);
  722. int frameCount = readVarint(input, true);
  723. IkConstraintTimeline *timeline = new(__FILE__, __LINE__) IkConstraintTimeline(frameCount);
  724. timeline->_ikConstraintIndex = index;
  725. for (int frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
  726. float time = readFloat(input);
  727. float mix = readFloat(input);
  728. float softness = readFloat(input) * _scale;
  729. signed char bendDirection = readSByte(input);
  730. bool compress = readBoolean(input);
  731. bool stretch = readBoolean(input);
  732. timeline->setFrame(frameIndex, time, mix, softness, bendDirection, compress, stretch);
  733. if (frameIndex < frameCount - 1) readCurve(input, frameIndex, timeline);
  734. }
  735. timelines.add(timeline);
  736. duration = MathUtil::max(duration, timeline->_frames[(frameCount - 1) * IkConstraintTimeline::ENTRIES]);
  737. }
  738. // Transform constraint timelines.
  739. for (int i = 0, n = readVarint(input, true); i < n; ++i) {
  740. int index = readVarint(input, true);
  741. int frameCount = readVarint(input, true);
  742. TransformConstraintTimeline *timeline = new(__FILE__, __LINE__) TransformConstraintTimeline(frameCount);
  743. timeline->_transformConstraintIndex = index;
  744. for (int frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
  745. float time = readFloat(input);
  746. float rotateMix = readFloat(input);
  747. float translateMix = readFloat(input);
  748. float scaleMix = readFloat(input);
  749. float shearMix = readFloat(input);
  750. timeline->setFrame(frameIndex, time, rotateMix, translateMix, scaleMix, shearMix);
  751. if (frameIndex < frameCount - 1) readCurve(input, frameIndex, timeline);
  752. }
  753. timelines.add(timeline);
  754. duration = MathUtil::max(duration, timeline->_frames[(frameCount - 1) * TransformConstraintTimeline::ENTRIES]);
  755. }
  756. // Path constraint timelines.
  757. for (int i = 0, n = readVarint(input, true); i < n; ++i) {
  758. int index = readVarint(input, true);
  759. PathConstraintData *data = skeletonData->_pathConstraints[index];
  760. for (int ii = 0, nn = readVarint(input, true); ii < nn; ++ii) {
  761. int timelineType = readSByte(input);
  762. int frameCount = readVarint(input, true);
  763. switch (timelineType) {
  764. case PATH_POSITION:
  765. case PATH_SPACING: {
  766. PathConstraintPositionTimeline *timeline;
  767. float timelineScale = 1;
  768. if (timelineType == PATH_SPACING) {
  769. timeline = new(__FILE__, __LINE__) PathConstraintSpacingTimeline(frameCount);
  770. if (data->_spacingMode == SpacingMode_Length || data->_spacingMode == SpacingMode_Fixed) timelineScale = scale;
  771. } else {
  772. timeline = new(__FILE__, __LINE__) PathConstraintPositionTimeline(frameCount);
  773. if (data->_positionMode == PositionMode_Fixed) timelineScale = scale;
  774. }
  775. timeline->_pathConstraintIndex = index;
  776. for (int frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
  777. float time = readFloat(input);
  778. float value = readFloat(input) * timelineScale;
  779. timeline->setFrame(frameIndex, time, value);
  780. if (frameIndex < frameCount - 1) readCurve(input, frameIndex, timeline);
  781. }
  782. timelines.add(timeline);
  783. duration = MathUtil::max(duration, timeline->_frames[(frameCount - 1) * PathConstraintPositionTimeline::ENTRIES]);
  784. break;
  785. }
  786. case PATH_MIX: {
  787. PathConstraintMixTimeline *timeline = new(__FILE__, __LINE__) PathConstraintMixTimeline(frameCount);
  788. timeline->_pathConstraintIndex = index;
  789. for (int frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
  790. float time = readFloat(input);
  791. float rotateMix = readFloat(input);
  792. float translateMix = readFloat(input);
  793. timeline->setFrame(frameIndex, time, rotateMix, translateMix);
  794. if (frameIndex < frameCount - 1) readCurve(input, frameIndex, timeline);
  795. }
  796. timelines.add(timeline);
  797. duration = MathUtil::max(duration, timeline->_frames[(frameCount - 1) * PathConstraintMixTimeline::ENTRIES]);
  798. break;
  799. }
  800. }
  801. }
  802. }
  803. // Deform timelines.
  804. for (int i = 0, n = readVarint(input, true); i < n; ++i) {
  805. Skin *skin = skeletonData->_skins[readVarint(input, true)];
  806. for (int ii = 0, nn = readVarint(input, true); ii < nn; ++ii) {
  807. int slotIndex = readVarint(input, true);
  808. for (int iii = 0, nnn = readVarint(input, true); iii < nnn; iii++) {
  809. const char *attachmentName = readStringRef(input, skeletonData);
  810. Attachment *baseAttachment = skin->getAttachment(slotIndex, String(attachmentName));
  811. if (!baseAttachment) {
  812. ContainerUtil::cleanUpVectorOfPointers(timelines);
  813. setError("Attachment not found: ", attachmentName);
  814. return NULL;
  815. }
  816. VertexAttachment *attachment = static_cast<VertexAttachment *>(baseAttachment);
  817. bool weighted = attachment->_bones.size() > 0;
  818. Vector<float> &vertices = attachment->_vertices;
  819. size_t deformLength = weighted ? vertices.size() / 3 * 2 : vertices.size();
  820. size_t frameCount = (size_t)readVarint(input, true);
  821. DeformTimeline *timeline = new(__FILE__, __LINE__) DeformTimeline(frameCount);
  822. timeline->_slotIndex = slotIndex;
  823. timeline->_attachment = attachment;
  824. for (size_t frameIndex = 0; frameIndex < frameCount; ++frameIndex) {
  825. float time = readFloat(input);
  826. Vector<float> deform;
  827. size_t end = (size_t)readVarint(input, true);
  828. if (end == 0) {
  829. if (weighted) {
  830. deform.setSize(deformLength, 0);
  831. for (size_t iiii = 0; iiii < deformLength; ++iiii)
  832. deform[iiii] = 0;
  833. } else {
  834. deform.clearAndAddAll(vertices);
  835. }
  836. } else {
  837. deform.setSize(deformLength, 0);
  838. size_t start = (size_t)readVarint(input, true);
  839. end += start;
  840. if (scale == 1) {
  841. for (size_t v = start; v < end; ++v)
  842. deform[v] = readFloat(input);
  843. } else {
  844. for (size_t v = start; v < end; ++v)
  845. deform[v] = readFloat(input) * scale;
  846. }
  847. if (!weighted) {
  848. for (size_t v = 0, vn = deform.size(); v < vn; ++v)
  849. deform[v] += vertices[v];
  850. }
  851. }
  852. timeline->setFrame(frameIndex, time, deform);
  853. if (frameIndex < frameCount - 1) readCurve(input, frameIndex, timeline);
  854. }
  855. timelines.add(timeline);
  856. duration = MathUtil::max(duration, timeline->_frames[frameCount - 1]);
  857. }
  858. }
  859. }
  860. // Draw order timeline.
  861. size_t drawOrderCount = (size_t)readVarint(input, true);
  862. if (drawOrderCount > 0) {
  863. DrawOrderTimeline *timeline = new(__FILE__, __LINE__) DrawOrderTimeline(drawOrderCount);
  864. size_t slotCount = skeletonData->_slots.size();
  865. for (size_t i = 0; i < drawOrderCount; ++i) {
  866. float time = readFloat(input);
  867. size_t offsetCount = (size_t)readVarint(input, true);
  868. Vector<int> drawOrder;
  869. drawOrder.setSize(slotCount, 0);
  870. for (int ii = (int)slotCount - 1; ii >= 0; --ii)
  871. drawOrder[ii] = -1;
  872. Vector<int> unchanged;
  873. unchanged.setSize(slotCount - offsetCount, 0);
  874. size_t originalIndex = 0, unchangedIndex = 0;
  875. for (size_t ii = 0; ii < offsetCount; ++ii) {
  876. size_t slotIndex = (size_t)readVarint(input, true);
  877. // Collect unchanged items.
  878. while (originalIndex != slotIndex)
  879. unchanged[unchangedIndex++] = originalIndex++;
  880. // Set changed items.
  881. size_t index = originalIndex;
  882. drawOrder[index + (size_t)readVarint(input, true)] = originalIndex++;
  883. }
  884. // Collect remaining unchanged items.
  885. while (originalIndex < slotCount) {
  886. unchanged[unchangedIndex++] = originalIndex++;
  887. }
  888. // Fill in unchanged items.
  889. for (int ii = (int)slotCount - 1; ii >= 0; --ii)
  890. if (drawOrder[ii] == -1) drawOrder[ii] = unchanged[--unchangedIndex];
  891. timeline->setFrame(i, time, drawOrder);
  892. }
  893. timelines.add(timeline);
  894. duration = MathUtil::max(duration, timeline->_frames[drawOrderCount - 1]);
  895. }
  896. // Event timeline.
  897. int eventCount = readVarint(input, true);
  898. if (eventCount > 0) {
  899. EventTimeline *timeline = new(__FILE__, __LINE__) EventTimeline(eventCount);
  900. for (int i = 0; i < eventCount; ++i) {
  901. float time = readFloat(input);
  902. EventData *eventData = skeletonData->_events[readVarint(input, true)];
  903. Event *event = new(__FILE__, __LINE__) Event(time, *eventData);
  904. event->_intValue = readVarint(input, false);
  905. event->_floatValue = readFloat(input);
  906. bool freeString = readBoolean(input);
  907. const char *event_stringValue = freeString ? readString(input) : eventData->_stringValue.buffer();
  908. event->_stringValue = String(event_stringValue);
  909. if (freeString) SpineExtension::free(event_stringValue, __FILE__, __LINE__);
  910. if (!eventData->_audioPath.isEmpty()) {
  911. event->_volume = readFloat(input);
  912. event->_balance = readFloat(input);
  913. }
  914. timeline->setFrame(i, event);
  915. }
  916. timelines.add(timeline);
  917. duration = MathUtil::max(duration, timeline->_frames[eventCount - 1]);
  918. }
  919. return new(__FILE__, __LINE__) Animation(String(name), timelines, duration);
  920. }
  921. void SkeletonBinary::readCurve(DataInput *input, int frameIndex, CurveTimeline *timeline) {
  922. switch (readByte(input)) {
  923. case CURVE_STEPPED: {
  924. timeline->setStepped(frameIndex);
  925. break;
  926. }
  927. case CURVE_BEZIER: {
  928. float cx1 = readFloat(input);
  929. float cy1 = readFloat(input);
  930. float cx2 = readFloat(input);
  931. float cy2 = readFloat(input);
  932. timeline->setCurve(frameIndex, cx1, cy1, cx2, cy2);
  933. break;
  934. }
  935. }
  936. }