This commit is contained in:
Stephen Gowen 2017-12-03 21:32:44 -05:00
parent 0b28d385db
commit be2dfc7ff4
4 changed files with 1893 additions and 5 deletions

View File

@ -31,9 +31,80 @@
#ifndef Spine_Json_h
#define Spine_Json_h
#ifndef SPINE_JSON_HAVE_PREV
/* Spine doesn't use the "prev" link in the Json sibling lists. */
#define SPINE_JSON_HAVE_PREV 0
#endif
namespace Spine
{
// TODO
class Json
{
public:
/* Json Types: */
static const int JSON_FALSE;
static const int JSON_TRUE;
static const int JSON_NULL;
static const int JSON_NUMBER;
static const int JSON_STRING;
static const int JSON_ARRAY;
static const int JSON_OBJECT;
/* Get item "string" from object. Case insensitive. */
static Json* getItem(Json *object, const char* string);
static const char* getString(Json *object, const char* name, const char* defaultValue);
static float getFloat(Json *object, const char* name, float defaultValue);
static int getInt(Json *object, const char* name, int defaultValue);
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when Json_create() returns 0. 0 when Json_create() succeeds. */
static const char* getError();
/* Supply a block of JSON, and this returns a Json object you can interrogate. Call Json_dispose when finished. */
Json(const char* value);
~Json();
private:
static const char* JSON_ERROR;
Json* _next;
#if SPINE_JSON_HAVE_PREV
Json* _prev; /* next/prev allow you to walk array/object chains. Alternatively, use getSize/getItem */
#endif
Json* _child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
int _type; /* The type of the item, as above. */
int _size; /* The number of children. */
const char* _valueString; /* The item's string, if type==JSON_STRING */
int _valueInt; /* The item's number, if type==JSON_NUMBER */
float _valueFloat; /* The item's number, if type==JSON_NUMBER */
const char* _name; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
/* Utility to jump whitespace and cr/lf */
static const char* skip(const char* inValue);
/* Parser core - when encountering text, process appropriately. */
static const char* parseValue(Json *item, const char* value);
/* Parse the input text into an unescaped cstring, and populate item. */
static const char* parseString(Json *item, const char* str);
/* Parse the input text to generate a number, and populate the result into item. */
static const char* parseNumber(Json *item, const char* num);
/* Build an array from input text. */
static const char* parseArray(Json *item, const char* value);
/* Build an object from the text. */
static const char* parseObject(Json *item, const char* value);
static int strcasecmp(const char* s1, const char* s2);
};
}
#endif /* Spine_Json_h */

View File

@ -33,8 +33,15 @@
#include <spine/Vector.h>
#include <string>
namespace Spine
{
class MeshAttachment;
class CurveTimeline;
class VertexAttachment;
class Animation;
class Json;
class SkeletonData;
class Atlas;
class AttachmentLoader;
@ -49,15 +56,28 @@ namespace Spine
~SkeletonJson();
SkeletonData* readSkeletonData(const char* json);
SkeletonData* readSkeletonDataFile(const char* path);
SkeletonData* readSkeletonData(const char* json);
private:
AttachmentLoader* _attachmentLoader;
Vector<LinkedMesh*> _linkedMeshes;
float _scale;
const bool _ownsLoader;
std::string _error;
static float toColor(const char* value, int index);
static void readCurve(Json* frame, CurveTimeline* timeline, int frameIndex);
void addLinkedMesh(MeshAttachment* mesh, const char* skin, int slotIndex, const char* parent);
Animation* readAnimation(Json* root, SkeletonData *skeletonData);
void readVertices(Json* attachmentMap, VertexAttachment* attachment, int verticesLength);
void setError(Json* root, const char* value1, const char* value2);
};
}

View File

@ -28,7 +28,621 @@
* POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
/* Json */
/* JSON parser in CPP, shamelessly ripped from json.c in the spine-c runtime */
#ifndef _DEFAULT_SOURCE
/* Bring strings.h definitions into string.h, where appropriate */
#define _DEFAULT_SOURCE
#endif
#ifndef _BSD_SOURCE
/* Bring strings.h definitions into string.h, where appropriate */
#define _BSD_SOURCE
#endif
#include <spine/Json.h>
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h> /* strtod (C89), strtof (C99) */
#include <string.h> /* strcasecmp (4.4BSD - compatibility), _stricmp (_WIN32) */
#include <spine/Extension.h>
#include <new>
namespace Spine
{
// TODO
const int Json::JSON_FALSE = 0;
const int Json::JSON_TRUE = 1;
const int Json::JSON_NULL = 2;
const int Json::JSON_NUMBER = 3;
const int Json::JSON_STRING = 4;
const int Json::JSON_ARRAY = 5;
const int Json::JSON_OBJECT = 6;
const char* Json::JSON_ERROR = NULL;
Json* Json::getItem(Json *object, const char* string)
{
Json *c = object->_child;
while (c && strcasecmp(c->_name, string))
{
c = c->_next;
}
return c;
}
const char* Json::getString(Json *object, const char* name, const char* defaultValue)
{
object = getItem(object, name);
if (object)
{
return object->_valueString;
}
return defaultValue;
}
float Json::getFloat(Json *value, const char* name, float defaultValue)
{
value = getItem(value, name);
return value ? value->_valueFloat : defaultValue;
}
int Json::getInt(Json *value, const char* name, int defaultValue)
{
value = getItem(value, name);
return value ? value->_valueInt : defaultValue;
}
const char* Json::getError()
{
return JSON_ERROR;
}
Json::Json(const char* value) :
_next(NULL),
#if SPINE_JSON_HAVE_PREV
_prev(NULL),
#endif
_child(NULL),
_type(0),
_size(0),
_valueString(NULL),
_valueInt(0),
_valueFloat(0),
_name(NULL)
{
if (value)
{
value = parseValue(this, skip(value));
assert(value);
}
}
Json::~Json()
{
if (_child)
{
DESTROY(Json, _child);
}
if (_valueString)
{
FREE(_valueString);
}
if (_name)
{
FREE(_name);
}
if (_next)
{
DESTROY(Json, _next);
}
}
const char* Json::skip(const char* inValue)
{
if (!inValue)
{
/* must propagate NULL since it's often called in skip(f(...)) form */
return NULL;
}
while (*inValue && (unsigned char)*inValue <= 32)
{
inValue++;
}
return inValue;
}
const char* Json::parseValue(Json *item, const char* value)
{
/* Referenced by constructor, parseArray(), and parseObject(). */
/* Always called with the result of skip(). */
#if SPINE_JSON_DEBUG /* Checked at entry to graph, constructor, and after every parse call. */
if (!value)
{
/* Fail on null. */
return NULL;
}
#endif
switch (*value)
{
case 'n':
{
if (!strncmp(value + 1, "ull", 3))
{
item->_type = JSON_NULL;
return value + 4;
}
break;
}
case 'f':
{
if (!strncmp(value + 1, "alse", 4))
{
item->_type = JSON_FALSE;
/* calloc prevents us needing item->_type = JSON_FALSE or valueInt = 0 here */
return value + 5;
}
break;
}
case 't':
{
if (!strncmp(value + 1, "rue", 3))
{
item->_type = JSON_TRUE;
item->_valueInt = 1;
return value + 4;
}
break;
}
case '\"':
return parseString(item, value);
case '[':
return parseArray(item, value);
case '{':
return parseObject(item, value);
case '-': /* fallthrough */
case '0': /* fallthrough */
case '1': /* fallthrough */
case '2': /* fallthrough */
case '3': /* fallthrough */
case '4': /* fallthrough */
case '5': /* fallthrough */
case '6': /* fallthrough */
case '7': /* fallthrough */
case '8': /* fallthrough */
case '9':
return parseNumber(item, value);
default:
break;
}
JSON_ERROR = value;
return NULL; /* failure. */
}
static const unsigned char firstByteMark[7] = {0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC};
const char* Json::parseString(Json *item, const char* str)
{
const char* ptr = str + 1;
char* ptr2;
char* out;
int len = 0;
unsigned uc, uc2;
if (*str != '\"')
{
/* TODO: don't need this check when called from parseValue, but do need from parseObject */
JSON_ERROR = str;
return 0;
} /* not a string! */
while (*ptr != '\"' && *ptr && ++len)
{
if (*ptr++ == '\\')
{
ptr++; /* Skip escaped quotes. */
}
}
out = MALLOC(char, len + 1); /* The length needed for the string, roughly. */
if (!out)
{
return 0;
}
ptr = str + 1;
ptr2 = out;
while (*ptr != '\"' && *ptr)
{
if (*ptr != '\\')
{
*ptr2++ = *ptr++;
}
else
{
ptr++;
switch (*ptr)
{
case 'b':
*ptr2++ = '\b';
break;
case 'f':
*ptr2++ = '\f';
break;
case 'n':
*ptr2++ = '\n';
break;
case 'r':
*ptr2++ = '\r';
break;
case 't':
*ptr2++ = '\t';
break;
case 'u':
{
/* transcode utf16 to utf8. */
sscanf(ptr + 1, "%4x", &uc);
ptr += 4; /* get the unicode char. */
if ((uc >= 0xDC00 && uc <= 0xDFFF) || uc == 0)
{
break; /* check for invalid. */
}
/* TODO provide an option to ignore surrogates, use unicode replacement character? */
if (uc >= 0xD800 && uc <= 0xDBFF) /* UTF16 surrogate pairs. */
{
if (ptr[1] != '\\' || ptr[2] != 'u')
{
break; /* missing second-half of surrogate. */
}
sscanf(ptr + 3, "%4x", &uc2);
ptr += 6;
if (uc2 < 0xDC00 || uc2 > 0xDFFF)
{
break; /* invalid second-half of surrogate. */
}
uc = 0x10000 + (((uc & 0x3FF) << 10) | (uc2 & 0x3FF));
}
len = 4;
if (uc < 0x80)
{
len = 1;
}
else if (uc < 0x800)
{
len = 2;
}
else if (uc < 0x10000)
{
len = 3;
}
ptr2 += len;
switch (len)
{
case 4:
*--ptr2 = ((uc | 0x80) & 0xBF);
uc >>= 6;
/* fallthrough */
case 3:
*--ptr2 = ((uc | 0x80) & 0xBF);
uc >>= 6;
/* fallthrough */
case 2:
*--ptr2 = ((uc | 0x80) & 0xBF);
uc >>= 6;
/* fallthrough */
case 1:
*--ptr2 = (uc | firstByteMark[len]);
}
ptr2 += len;
break;
}
default:
*ptr2++ = *ptr;
break;
}
ptr++;
}
}
*ptr2 = NULL;
if (*ptr == '\"')
{
ptr++; /* TODO error handling if not \" or \0 ? */
}
item->_valueString = out;
item->_type = JSON_STRING;
return ptr;
}
const char* Json::parseNumber(Json *item, const char* num)
{
double result = 0.0;
int negative = 0;
char* ptr = (char*)num;
if (*ptr == '-')
{
negative = -1;
++ptr;
}
while (*ptr >= '0' && *ptr <= '9')
{
result = result * 10.0 + (*ptr - '0');
++ptr;
}
if (*ptr == '.')
{
double fraction = 0.0;
int n = 0;
++ptr;
while (*ptr >= '0' && *ptr <= '9')
{
fraction = (fraction * 10.0) + (*ptr - '0');
++ptr;
++n;
}
result += fraction / pow(10.0, n);
}
if (negative)
{
result = -result;
}
if (*ptr == 'e' || *ptr == 'E')
{
double exponent = 0;
int expNegative = 0;
int n = 0;
++ptr;
if (*ptr == '-')
{
expNegative = -1;
++ptr;
}
else if (*ptr == '+')
{
++ptr;
}
while (*ptr >= '0' && *ptr <= '9')
{
exponent = (exponent * 10.0) + (*ptr - '0');
++ptr;
++n;
}
if (expNegative)
{
result = result / pow(10, exponent);
}
else
{
result = result * pow(10, exponent);
}
}
if (ptr != num)
{
/* Parse success, number found. */
item->_valueFloat = result;
item->_valueInt = static_cast<int>(result);
item->_type = JSON_NUMBER;
return ptr;
}
else
{
/* Parse failure, JSON_ERROR is set. */
JSON_ERROR = num;
return NULL;
}
}
const char* Json::parseArray(Json *item, const char* value)
{
Json *child;
#if SPINE_JSON_DEBUG /* unnecessary, only callsite (parse_value) verifies this */
if (*value != '[')
{
ep = value;
return 0;
} /* not an array! */
#endif
item->_type = JSON_ARRAY;
value = skip(value + 1);
if (*value == ']')
{
return value + 1; /* empty array. */
}
item->_child = child = NEW(Json);
new (item->_child) Json(NULL);
if (!item->_child)
{
return NULL; /* memory fail */
}
value = skip(parseValue(child, skip(value))); /* skip any spacing, get the value. */
if (!value)
{
return NULL;
}
item->_size = 1;
while (*value == ',')
{
Json *new_item = NEW(Json);
new (new_item) Json(NULL);
if (!new_item)
{
return NULL; /* memory fail */
}
child->_next = new_item;
#if SPINE_JSON_HAVE_PREV
new_item->prev = child;
#endif
child = new_item;
value = skip(parseValue(child, skip(value + 1)));
if (!value)
{
return NULL; /* parse fail */
}
item->_size++;
}
if (*value == ']')
{
return value + 1; /* end of array */
}
JSON_ERROR = value;
return NULL; /* malformed. */
}
/* Build an object from the text. */
const char* Json::parseObject(Json *item, const char* value)
{
Json *child;
#if SPINE_JSON_DEBUG /* unnecessary, only callsite (parse_value) verifies this */
if (*value != '{')
{
ep = value;
return 0;
} /* not an object! */
#endif
item->_type = JSON_OBJECT;
value = skip(value + 1);
if (*value == '}')
{
return value + 1; /* empty array. */
}
item->_child = child = NEW(Json);
new (item->_child) Json(NULL);
if (!item->_child)
{
return NULL;
}
value = skip(parseString(child, skip(value)));
if (!value)
{
return NULL;
}
child->_name = child->_valueString;
child->_valueString = 0;
if (*value != ':')
{
JSON_ERROR = value;
return NULL;
} /* fail! */
value = skip(parseValue(child, skip(value + 1))); /* skip any spacing, get the value. */
if (!value)
{
return NULL;
}
item->_size = 1;
while (*value == ',')
{
Json *new_item = NEW(Json);
new (new_item) Json(NULL);
if (!new_item)
{
return NULL; /* memory fail */
}
child->_next = new_item;
#if SPINE_JSON_HAVE_PREV
new_item->prev = child;
#endif
child = new_item;
value = skip(parseString(child, skip(value + 1)));
if (!value)
{
return NULL;
}
child->_name = child->_valueString;
child->_valueString = 0;
if (*value != ':')
{
JSON_ERROR = value;
return NULL;
} /* fail! */
value = skip(parseValue(child, skip(value + 1))); /* skip any spacing, get the value. */
if (!value)
{
return NULL;
}
item->_size++;
}
if (*value == '}')
{
return value + 1; /* end of array */
}
JSON_ERROR = value;
return NULL; /* malformed. */
}
int Json::strcasecmp(const char* s1, const char* s2)
{
/* TODO we may be able to elide these NULL checks if we can prove
* the graph and input (only callsite is Json_getItem) should not have NULLs
*/
if (s1 && s2)
{
#if defined(_WIN32)
return _stricmp(s1, s2);
#else
return strcasecmp( s1, s2 );
#endif
}
else
{
if (s1 < s2)
{
return -1; /* s1 is null, s2 is not */
}
else if (s1 == s2)
{
return 0; /* both are null */
}
else
{
return 1; /* s2 is nul s1 is not */
}
}
}
}

File diff suppressed because it is too large Load Diff