From 3a06b829cc81203ebeec996efad33e4bef6cc8c7 Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Fri, 8 Apr 2016 20:51:32 +0200 Subject: [PATCH] Changed binary format for 3.1.05. * String encoding simplified for reading by using number of bytes rather than number of UTF-8 characters. * Parent bone index is no longer minus 1 and omitted for root. * Mesh and weighted mesh vertices array is no longer preceeded by a length. UVs array length is vertex count, not vertex array length. Edges changed to short. * Bounding box vertices array length is vertex count, not vertex array length. --- .../spine/SkeletonBinary.java | 78 +++++++++++++------ .../esotericsoftware/spine/SkeletonJson.java | 4 +- .../spine/attachments/MeshAttachment.java | 6 +- .../attachments/WeightedMeshAttachment.java | 6 +- 4 files changed, 62 insertions(+), 32 deletions(-) diff --git a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonBinary.java b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonBinary.java index 1b64ae437..9dbf3c6ac 100644 --- a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonBinary.java +++ b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonBinary.java @@ -104,7 +104,47 @@ public class SkeletonBinary { SkeletonData skeletonData = new SkeletonData(); skeletonData.name = file.nameWithoutExtension(); - DataInput input = new DataInput(file.read(512)); + DataInput input = new DataInput(file.read(512)) { + private char[] chars = new char[32]; + + public String readString () throws IOException { + int byteCount = readInt(true); + switch (byteCount) { + case 0: + return null; + case 1: + return ""; + } + byteCount--; + if (chars.length < byteCount) chars = new char[byteCount]; + int charCount = 0; + for (int i = 0; i < byteCount; i++, charCount++) { + int b = read(); + switch (b >> 4) { + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + case 7: + chars[charCount] = (char)b; + break; + case 12: + case 13: + chars[charCount] = (char)((b & 0x1F) << 6 | read() & 0x3F); + i++; + break; + case 14: + chars[charCount] = (char)((b & 0x0F) << 12 | (read() & 0x3F) << 6 | read() & 0x3F); + i += 2; + break; + } + } + return new String(chars, 0, charCount); + } + }; try { skeletonData.hash = input.readString(); if (skeletonData.hash.isEmpty()) skeletonData.hash = null; @@ -123,9 +163,7 @@ public class SkeletonBinary { // Bones. for (int i = 0, n = input.readInt(true); i < n; i++) { String name = input.readString(); - BoneData parent = null; - int parentIndex = input.readInt(true) - 1; - if (parentIndex != -1) parent = skeletonData.bones.get(parentIndex); + BoneData parent = i == 0 ? null : skeletonData.bones.get(input.readInt(true)); BoneData boneData = new BoneData(name, parent); boneData.x = input.readFloat() * scale; boneData.y = input.readFloat() * scale; @@ -284,7 +322,7 @@ public class SkeletonBinary { return region; } case boundingbox: { - float[] vertices = readFloatArray(input, scale); + float[] vertices = readFloatArray(input, input.readInt(true) * 2, scale); BoundingBoxAttachment box = attachmentLoader.newBoundingBoxAttachment(skin, name); if (box == null) return null; box.setVertices(vertices); @@ -294,14 +332,15 @@ public class SkeletonBinary { String path = input.readString(); int color = input.readInt(); int hullLength = 0; - float[] uvs = readFloatArray(input, 1); + int verticesLength = input.readInt(true) * 2; + float[] uvs = readFloatArray(input, verticesLength, 1); short[] triangles = readShortArray(input); - float[] vertices = readFloatArray(input, scale); + float[] vertices = readFloatArray(input, verticesLength, scale); hullLength = input.readInt(true); - int[] edges = null; + short[] edges = null; float width = 0, height = 0; if (nonessential) { - edges = readIntArray(input); + edges = readShortArray(input); width = input.readFloat(); height = input.readFloat(); } @@ -351,15 +390,15 @@ public class SkeletonBinary { case weightedmesh: { String path = input.readString(); int color = input.readInt(); - float[] uvs = readFloatArray(input, 1); - short[] triangles = readShortArray(input); int vertexCount = input.readInt(true); + float[] uvs = readFloatArray(input, vertexCount * 2, 1); + short[] triangles = readShortArray(input); FloatArray weights = new FloatArray(uvs.length * 3 * 3); IntArray bones = new IntArray(uvs.length * 3); for (int i = 0; i < vertexCount; i++) { int boneCount = (int)input.readFloat(); bones.add(boneCount); - for (int nn = i + boneCount * 4; i < nn; i += 4) { + for (int ii = 0, nn = boneCount; ii < nn; ii++) { bones.add((int)input.readFloat()); weights.add(input.readFloat() * scale); weights.add(input.readFloat() * scale); @@ -367,10 +406,10 @@ public class SkeletonBinary { } } int hullLength = input.readInt(true); - int[] edges = null; + short[] edges = null; float width = 0, height = 0; if (nonessential) { - edges = readIntArray(input); + edges = readShortArray(input); width = input.readFloat(); height = input.readFloat(); } @@ -422,8 +461,7 @@ public class SkeletonBinary { return null; } - private float[] readFloatArray (DataInput input, float scale) throws IOException { - int n = input.readInt(true); + private float[] readFloatArray (DataInput input, int n, float scale) throws IOException { float[] array = new float[n]; if (scale == 1) { for (int i = 0; i < n; i++) @@ -443,14 +481,6 @@ public class SkeletonBinary { return array; } - private int[] readIntArray (DataInput input) throws IOException { - int n = input.readInt(true); - int[] array = new int[n]; - for (int i = 0; i < n; i++) - array[i] = input.readInt(true); - return array; - } - private void readAnimation (String name, DataInput input, SkeletonData skeletonData) { Array timelines = new Array(); float scale = this.scale; diff --git a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonJson.java b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonJson.java index 76ab1d58e..cf6444ea2 100644 --- a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonJson.java +++ b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/SkeletonJson.java @@ -300,7 +300,7 @@ public class SkeletonJson { mesh.updateUVs(); if (map.has("hull")) mesh.setHullLength(map.require("hull").asInt() * 2); - if (map.has("edges")) mesh.setEdges(map.require("edges").asIntArray()); + if (map.has("edges")) mesh.setEdges(map.require("edges").asShortArray()); } else { mesh.setInheritFFD(map.getBoolean("ffd", true)); linkedMeshes.add(new LinkedMesh(mesh, map.getString("skin", null), slotIndex, parent)); @@ -343,7 +343,7 @@ public class SkeletonJson { mesh.updateUVs(); if (map.has("hull")) mesh.setHullLength(map.require("hull").asInt() * 2); - if (map.has("edges")) mesh.setEdges(map.require("edges").asIntArray()); + if (map.has("edges")) mesh.setEdges(map.require("edges").asShortArray()); } else { mesh.setInheritFFD(map.getBoolean("ffd", true)); linkedMeshes.add(new LinkedMesh(mesh, map.getString("skin", null), slotIndex, parent)); diff --git a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/attachments/MeshAttachment.java b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/attachments/MeshAttachment.java index 9f6515d5f..22e2034ab 100644 --- a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/attachments/MeshAttachment.java +++ b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/attachments/MeshAttachment.java @@ -53,7 +53,7 @@ public class MeshAttachment extends Attachment implements FfdAttachment { private boolean inheritFFD; // Nonessential. - private int[] edges; + private short[] edges; private float width, height; public MeshAttachment (String name) { @@ -182,11 +182,11 @@ public class MeshAttachment extends Attachment implements FfdAttachment { this.hullLength = hullLength; } - public int[] getEdges () { + public short[] getEdges () { return edges; } - public void setEdges (int[] edges) { + public void setEdges (short[] edges) { this.edges = edges; } diff --git a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/attachments/WeightedMeshAttachment.java b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/attachments/WeightedMeshAttachment.java index 12bf4eeec..e0f049b52 100644 --- a/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/attachments/WeightedMeshAttachment.java +++ b/spine-libgdx/spine-libgdx/src/com/esotericsoftware/spine/attachments/WeightedMeshAttachment.java @@ -55,7 +55,7 @@ public class WeightedMeshAttachment extends Attachment implements FfdAttachment private boolean inheritFFD; // Nonessential. - private int[] edges; + private short[] edges; private float width, height; public WeightedMeshAttachment (String name) { @@ -220,11 +220,11 @@ public class WeightedMeshAttachment extends Attachment implements FfdAttachment this.hullLength = hullLength; } - public void setEdges (int[] edges) { + public void setEdges (short[] edges) { this.edges = edges; } - public int[] getEdges () { + public short[] getEdges () { return edges; }