Example usage for com.badlogic.gdx.utils SerializationException SerializationException

List of usage examples for com.badlogic.gdx.utils SerializationException SerializationException

Introduction

In this page you can find the example usage for com.badlogic.gdx.utils SerializationException SerializationException.

Prototype

public SerializationException(String message, Throwable cause) 

Source Link

Usage

From source file:com.austinerb.project0.engine.JsonReader.java

License:Apache License

public JsonValue parse(FileHandle file) {
    try {//from  w  w w  .ja v  a2s .  com
        return parse(file.reader("UTF-8"));
    } catch (Exception ex) {
        throw new SerializationException("Error parsing file: " + file, ex);
    }
}

From source file:com.austinerb.project0.engine.JsonReader.java

License:Apache License

public JsonValue parse(char[] data, int offset, int length) {
    int cs, p = offset, pe = length, eof = pe, top = 0;
    int[] stack = new int[4];

    int s = 0;/*from   w  w  w  . j  a v  a 2  s.  c  om*/
    Array<String> names = new Array(8);
    boolean needsUnescape = false, stringIsName = false, stringIsUnquoted = false;
    RuntimeException parseRuntimeEx = null;

    boolean debug = false;
    if (debug)
        System.out.println();

    try {

        // line 3 "JsonReader.java"
        {
            cs = json_start;
            top = 0;
        }

        // line 8 "JsonReader.java"
        {
            int _klen;
            int _trans = 0;
            int _acts;
            int _nacts;
            int _keys;
            int _goto_targ = 0;

            _goto: while (true) {
                switch (_goto_targ) {
                case 0:
                    if (p == pe) {
                        _goto_targ = 4;
                        continue _goto;
                    }
                    if (cs == 0) {
                        _goto_targ = 5;
                        continue _goto;
                    }
                case 1:
                    _match: do {
                        _keys = _json_key_offsets[cs];
                        _trans = _json_index_offsets[cs];
                        _klen = _json_single_lengths[cs];
                        if (_klen > 0) {
                            int _lower = _keys;
                            int _mid;
                            int _upper = _keys + _klen - 1;
                            while (true) {
                                if (_upper < _lower)
                                    break;

                                _mid = _lower + ((_upper - _lower) >> 1);
                                if (data[p] < _json_trans_keys[_mid])
                                    _upper = _mid - 1;
                                else if (data[p] > _json_trans_keys[_mid])
                                    _lower = _mid + 1;
                                else {
                                    _trans += (_mid - _keys);
                                    break _match;
                                }
                            }
                            _keys += _klen;
                            _trans += _klen;
                        }

                        _klen = _json_range_lengths[cs];
                        if (_klen > 0) {
                            int _lower = _keys;
                            int _mid;
                            int _upper = _keys + (_klen << 1) - 2;
                            while (true) {
                                if (_upper < _lower)
                                    break;

                                _mid = _lower + (((_upper - _lower) >> 1) & ~1);
                                if (data[p] < _json_trans_keys[_mid])
                                    _upper = _mid - 2;
                                else if (data[p] > _json_trans_keys[_mid + 1])
                                    _lower = _mid + 2;
                                else {
                                    _trans += ((_mid - _keys) >> 1);
                                    break _match;
                                }
                            }
                            _trans += _klen;
                        }
                    } while (false);

                    _trans = _json_indicies[_trans];
                    cs = _json_trans_targs[_trans];

                    if (_json_trans_actions[_trans] != 0) {
                        _acts = _json_trans_actions[_trans];
                        _nacts = (int) _json_actions[_acts++];
                        while (_nacts-- > 0) {
                            switch (_json_actions[_acts++]) {
                            case 0:
                            // line 104 "JsonReader.rl"
                            {
                                stringIsName = true;
                            }
                                break;
                            case 1:
                            // line 107 "JsonReader.rl"
                            {
                                String value = new String(data, s, p - s);
                                if (needsUnescape)
                                    value = unescape(value);
                                outer: if (stringIsName) {
                                    stringIsName = false;
                                    if (debug)
                                        System.out.println("name: " + value);
                                    names.add(value);
                                } else {
                                    String name = names.size > 0 ? names.pop() : null;
                                    if (stringIsUnquoted) {
                                        if (value.equals("true")) {
                                            if (debug)
                                                System.out.println("boolean: " + name + "=true");
                                            bool(name, true);
                                            break outer;
                                        } else if (value.equals("false")) {
                                            if (debug)
                                                System.out.println("boolean: " + name + "=false");
                                            bool(name, false);
                                            break outer;
                                        } else if (value.equals("null")) {
                                            string(name, null);
                                            break outer;
                                        }
                                        boolean couldBeDouble = false, couldBeLong = true;
                                        outer2: for (int i = s; i < p; i++) {
                                            switch (data[i]) {
                                            case '0':
                                            case '1':
                                            case '2':
                                            case '3':
                                            case '4':
                                            case '5':
                                            case '6':
                                            case '7':
                                            case '8':
                                            case '9':
                                            case '-':
                                            case '+':
                                                break;
                                            case '.':
                                            case 'e':
                                            case 'E':
                                                couldBeDouble = true;
                                                couldBeLong = false;
                                                break;
                                            default:
                                                couldBeDouble = false;
                                                couldBeLong = false;
                                                break outer2;
                                            }
                                        }
                                        if (couldBeDouble) {
                                            try {
                                                if (debug)
                                                    System.out.println("double: " + name + "="
                                                            + Double.parseDouble(value));
                                                number(name, Double.parseDouble(value), value);
                                                break outer;
                                            } catch (NumberFormatException ignored) {
                                            }
                                        } else if (couldBeLong) {
                                            if (debug)
                                                System.out.println(
                                                        "double: " + name + "=" + Double.parseDouble(value));
                                            try {
                                                number(name, Long.parseLong(value), value);
                                                break outer;
                                            } catch (NumberFormatException ignored) {
                                            }
                                        }
                                    }
                                    if (debug)
                                        System.out.println("string: " + name + "=" + value);
                                    string(name, value);
                                }
                                stringIsUnquoted = false;
                                s = p;
                            }
                                break;
                            case 2:
                            // line 181 "JsonReader.rl"
                            {
                                String name = names.size > 0 ? names.pop() : null;
                                if (debug)
                                    System.out.println("startObject: " + name);
                                startObject(name);
                                {
                                    if (top == stack.length) {
                                        int[] newStack = new int[stack.length * 2];
                                        System.arraycopy(stack, 0, newStack, 0, stack.length);
                                        stack = newStack;
                                    }
                                    {
                                        stack[top++] = cs;
                                        cs = 5;
                                        _goto_targ = 2;
                                        if (true)
                                            continue _goto;
                                    }
                                }
                            }
                                break;
                            case 3:
                            // line 187 "JsonReader.rl"
                            {
                                if (debug)
                                    System.out.println("endObject");
                                pop();
                                {
                                    cs = stack[--top];
                                    _goto_targ = 2;
                                    if (true)
                                        continue _goto;
                                }
                            }
                                break;
                            case 4:
                            // line 192 "JsonReader.rl"
                            {
                                String name = names.size > 0 ? names.pop() : null;
                                if (debug)
                                    System.out.println("startArray: " + name);
                                startArray(name);
                                {
                                    if (top == stack.length) {
                                        int[] newStack = new int[stack.length * 2];
                                        System.arraycopy(stack, 0, newStack, 0, stack.length);
                                        stack = newStack;
                                    }
                                    {
                                        stack[top++] = cs;
                                        cs = 23;
                                        _goto_targ = 2;
                                        if (true)
                                            continue _goto;
                                    }
                                }
                            }
                                break;
                            case 5:
                            // line 198 "JsonReader.rl"
                            {
                                if (debug)
                                    System.out.println("endArray");
                                pop();
                                {
                                    cs = stack[--top];
                                    _goto_targ = 2;
                                    if (true)
                                        continue _goto;
                                }
                            }
                                break;
                            case 6:
                            // line 203 "JsonReader.rl"
                            {
                                int start = p - 1;
                                if (data[p++] == '/') {
                                    while (p != eof && data[p] != '\n')
                                        p++;
                                    p--;
                                } else {
                                    while (p + 1 < eof && data[p] != '*' || data[p + 1] != '/')
                                        p++;
                                    p++;
                                }
                                if (debug)
                                    System.out.println("comment " + new String(data, start, p - start));
                            }
                                break;
                            case 7:
                            // line 216 "JsonReader.rl"
                            {
                                if (debug)
                                    System.out.println("unquotedChars");
                                s = p;
                                needsUnescape = false;
                                stringIsUnquoted = true;
                                if (stringIsName) {
                                    outer: while (true) {
                                        switch (data[p]) {
                                        case '\\':
                                            needsUnescape = true;
                                            break;
                                        case '/':
                                            if (p + 1 == eof)
                                                break;
                                            char c = data[p + 1];
                                            if (c == '/' || c == '*')
                                                break outer;
                                            break;
                                        case ':':
                                        case '\r':
                                        case '\n':
                                            break outer;
                                        }
                                        if (debug)
                                            System.out.println("unquotedChar (name): '" + data[p] + "'");
                                        p++;
                                        if (p == eof)
                                            break;
                                    }
                                } else {
                                    outer: while (true) {
                                        switch (data[p]) {
                                        case '\\':
                                            needsUnescape = true;
                                            break;
                                        case '/':
                                            if (p + 1 == eof)
                                                break;
                                            char c = data[p + 1];
                                            if (c == '/' || c == '*')
                                                break outer;
                                            break;
                                        case '}':
                                        case ']':
                                        case ',':
                                        case '\r':
                                        case '\n':
                                            break outer;
                                        }
                                        if (debug)
                                            System.out.println("unquotedChar (value): '" + data[p] + "'");
                                        p++;
                                        if (p == eof)
                                            break;
                                    }
                                }
                                p--;
                                while (data[p] == ' ')
                                    p--;
                            }
                                break;
                            case 8:
                            // line 270 "JsonReader.rl"
                            {
                                if (debug)
                                    System.out.println("quotedChars");
                                s = ++p;
                                needsUnescape = false;
                                outer: while (true) {
                                    switch (data[p]) {
                                    case '\\':
                                        needsUnescape = true;
                                        p++;
                                        break;
                                    case '"':
                                        break outer;
                                    }
                                    // if (debug) System.out.println("quotedChar: '" + data[p] + "'");
                                    p++;
                                    if (p == eof)
                                        break;
                                }
                                p--;
                            }
                                break;
                            // line 313 "JsonReader.java"
                            }
                        }
                    }

                case 2:
                    if (cs == 0) {
                        _goto_targ = 5;
                        continue _goto;
                    }
                    if (++p != pe) {
                        _goto_targ = 1;
                        continue _goto;
                    }
                case 4:
                    if (p == eof) {
                        int __acts = _json_eof_actions[cs];
                        int __nacts = (int) _json_actions[__acts++];
                        while (__nacts-- > 0) {
                            switch (_json_actions[__acts++]) {
                            case 1:
                            // line 107 "JsonReader.rl"
                            {
                                String value = new String(data, s, p - s);
                                if (needsUnescape)
                                    value = unescape(value);
                                outer: if (stringIsName) {
                                    stringIsName = false;
                                    if (debug)
                                        System.out.println("name: " + value);
                                    names.add(value);
                                } else {
                                    String name = names.size > 0 ? names.pop() : null;
                                    if (stringIsUnquoted) {
                                        if (value.equals("true")) {
                                            if (debug)
                                                System.out.println("boolean: " + name + "=true");
                                            bool(name, true);
                                            break outer;
                                        } else if (value.equals("false")) {
                                            if (debug)
                                                System.out.println("boolean: " + name + "=false");
                                            bool(name, false);
                                            break outer;
                                        } else if (value.equals("null")) {
                                            string(name, null);
                                            break outer;
                                        }
                                        boolean couldBeDouble = false, couldBeLong = true;
                                        outer2: for (int i = s; i < p; i++) {
                                            switch (data[i]) {
                                            case '0':
                                            case '1':
                                            case '2':
                                            case '3':
                                            case '4':
                                            case '5':
                                            case '6':
                                            case '7':
                                            case '8':
                                            case '9':
                                            case '-':
                                            case '+':
                                                break;
                                            case '.':
                                            case 'e':
                                            case 'E':
                                                couldBeDouble = true;
                                                couldBeLong = false;
                                                break;
                                            default:
                                                couldBeDouble = false;
                                                couldBeLong = false;
                                                break outer2;
                                            }
                                        }
                                        if (couldBeDouble) {
                                            try {
                                                if (debug)
                                                    System.out.println("double: " + name + "="
                                                            + Double.parseDouble(value));
                                                number(name, Double.parseDouble(value), value);
                                                break outer;
                                            } catch (NumberFormatException ignored) {
                                            }
                                        } else if (couldBeLong) {
                                            if (debug)
                                                System.out.println(
                                                        "double: " + name + "=" + Double.parseDouble(value));
                                            try {
                                                number(name, Long.parseLong(value), value);
                                                break outer;
                                            } catch (NumberFormatException ignored) {
                                            }
                                        }
                                    }
                                    if (debug)
                                        System.out.println("string: " + name + "=" + value);
                                    string(name, value);
                                }
                                stringIsUnquoted = false;
                                s = p;
                            }
                                break;
                            // line 411 "JsonReader.java"
                            }
                        }
                    }

                case 5:
                }
                break;
            }
        }

        // line 306 "JsonReader.rl"

    } catch (RuntimeException ex) {
        parseRuntimeEx = ex;
    }

    JsonValue root = this.root;
    this.root = null;
    current = null;
    lastChild.clear();

    if (p < pe) {
        int lineNumber = 1;
        for (int i = 0; i < p; i++)
            if (data[i] == '\n')
                lineNumber++;
        throw new SerializationException("Error parsing JSON on line " + lineNumber + " near: "
                + new String(data, p, Math.min(256, pe - p)), parseRuntimeEx);
    } else if (elements.size != 0) {
        JsonValue element = elements.peek();
        elements.clear();
        if (element != null && element.isObject())
            throw new SerializationException("Error parsing JSON, unmatched brace.");
        else
            throw new SerializationException("Error parsing JSON, unmatched bracket.");
    } else if (parseRuntimeEx != null) {
        throw new SerializationException("Error parsing JSON: " + new String(data), parseRuntimeEx);
    }
    return root;
}

From source file:com.company.minery.utils.spine.SkeletonBinary.java

License:Open Source License

public SkeletonData readSkeletonData(FileHandle file) {
    if (file == null)
        throw new IllegalArgumentException("file cannot be null.");

    float scale = this.scale;

    SkeletonData skeletonData = new SkeletonData();
    skeletonData.name = file.nameWithoutExtension();

    DataInput input = new DataInput(file.read(512));
    try {//  w  ww.  j a  v a  2s . com
        skeletonData.hash = input.readString();
        if (skeletonData.hash.isEmpty())
            skeletonData.hash = null;
        skeletonData.version = input.readString();
        if (skeletonData.version.isEmpty())
            skeletonData.version = null;
        skeletonData.width = input.readFloat();
        skeletonData.height = input.readFloat();

        boolean nonessential = input.readBoolean();

        if (nonessential) {
            skeletonData.imagesPath = input.readString();
            if (skeletonData.imagesPath.isEmpty())
                skeletonData.imagesPath = null;
        }

        // 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 boneData = new BoneData(name, parent);
            boneData.x = input.readFloat() * scale;
            boneData.y = input.readFloat() * scale;
            boneData.scaleX = input.readFloat();
            boneData.scaleY = input.readFloat();
            boneData.rotation = input.readFloat();
            boneData.length = input.readFloat() * scale;
            boneData.flipX = input.readBoolean();
            boneData.flipY = input.readBoolean();
            boneData.inheritScale = input.readBoolean();
            boneData.inheritRotation = input.readBoolean();
            if (nonessential)
                Color.rgba8888ToColor(boneData.color, input.readInt());
            skeletonData.bones.add(boneData);
        }

        // IK constraints.
        for (int i = 0, n = input.readInt(true); i < n; i++) {
            IkConstraintData ikConstraintData = new IkConstraintData(input.readString());
            for (int ii = 0, nn = input.readInt(true); ii < nn; ii++)
                ikConstraintData.bones.add(skeletonData.bones.get(input.readInt(true)));
            ikConstraintData.target = skeletonData.bones.get(input.readInt(true));
            ikConstraintData.mix = input.readFloat();
            ikConstraintData.bendDirection = input.readByte();
            skeletonData.ikConstraints.add(ikConstraintData);
        }

        // Slots.
        for (int i = 0, n = input.readInt(true); i < n; i++) {
            String slotName = input.readString();
            BoneData boneData = skeletonData.bones.get(input.readInt(true));
            SlotData slotData = new SlotData(slotName, boneData);
            Color.rgba8888ToColor(slotData.color, input.readInt());
            slotData.attachmentName = input.readString();
            slotData.additiveBlending = input.readBoolean();
            skeletonData.slots.add(slotData);
        }

        // Default skin.
        Skin defaultSkin = readSkin(input, "default", nonessential);
        if (defaultSkin != null) {
            skeletonData.defaultSkin = defaultSkin;
            skeletonData.skins.add(defaultSkin);
        }

        // Skins.
        for (int i = 0, n = input.readInt(true); i < n; i++)
            skeletonData.skins.add(readSkin(input, input.readString(), nonessential));

        // Events.
        for (int i = 0, n = input.readInt(true); i < n; i++) {
            EventData eventData = new EventData(input.readString());
            eventData.intValue = input.readInt(false);
            eventData.floatValue = input.readFloat();
            eventData.stringValue = input.readString();
            skeletonData.events.add(eventData);
        }

        // Animations.
        for (int i = 0, n = input.readInt(true); i < n; i++)
            readAnimation(input.readString(), input, skeletonData);

    } catch (IOException ex) {
        throw new SerializationException("Error reading skeleton file.", ex);
    } finally {
        try {
            input.close();
        } catch (IOException ignored) {
        }
    }

    skeletonData.bones.shrink();
    skeletonData.slots.shrink();
    skeletonData.skins.shrink();
    return skeletonData;
}

From source file:com.company.minery.utils.spine.SkeletonBinary.java

License:Open Source License

private void readAnimation(String name, DataInput input, SkeletonData skeletonData) {
    Array<Timeline> timelines = new Array();
    float scale = this.scale;
    float duration = 0;

    try {/*from  ww w .  j  ava 2 s .  c  o m*/
        // Slot timelines.
        for (int i = 0, n = input.readInt(true); i < n; i++) {
            int slotIndex = input.readInt(true);
            for (int ii = 0, nn = input.readInt(true); ii < nn; ii++) {
                int timelineType = input.readByte();
                int frameCount = input.readInt(true);
                switch (timelineType) {
                case TIMELINE_COLOR: {
                    ColorTimeline timeline = new ColorTimeline(frameCount);
                    timeline.slotIndex = slotIndex;
                    for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
                        float time = input.readFloat();
                        Color.rgba8888ToColor(tempColor, input.readInt());
                        timeline.setFrame(frameIndex, time, tempColor.r, tempColor.g, tempColor.b, tempColor.a);
                        if (frameIndex < frameCount - 1)
                            readCurve(input, frameIndex, timeline);
                    }
                    timelines.add(timeline);
                    duration = Math.max(duration, timeline.getFrames()[frameCount * 5 - 5]);
                    break;
                }
                case TIMELINE_ATTACHMENT:
                    AttachmentTimeline timeline = new AttachmentTimeline(frameCount);
                    timeline.slotIndex = slotIndex;
                    for (int frameIndex = 0; frameIndex < frameCount; frameIndex++)
                        timeline.setFrame(frameIndex, input.readFloat(), input.readString());
                    timelines.add(timeline);
                    duration = Math.max(duration, timeline.getFrames()[frameCount - 1]);
                    break;
                }
            }
        }

        // Bone timelines.
        for (int i = 0, n = input.readInt(true); i < n; i++) {
            int boneIndex = input.readInt(true);
            for (int ii = 0, nn = input.readInt(true); ii < nn; ii++) {
                int timelineType = input.readByte();
                int frameCount = input.readInt(true);
                switch (timelineType) {
                case TIMELINE_ROTATE: {
                    RotateTimeline timeline = new RotateTimeline(frameCount);
                    timeline.boneIndex = boneIndex;
                    for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
                        timeline.setFrame(frameIndex, input.readFloat(), input.readFloat());
                        if (frameIndex < frameCount - 1)
                            readCurve(input, frameIndex, timeline);
                    }
                    timelines.add(timeline);
                    duration = Math.max(duration, timeline.getFrames()[frameCount * 2 - 2]);
                    break;
                }
                case TIMELINE_TRANSLATE:
                case TIMELINE_SCALE: {
                    TranslateTimeline timeline;
                    float timelineScale = 1;
                    if (timelineType == TIMELINE_SCALE)
                        timeline = new ScaleTimeline(frameCount);
                    else {
                        timeline = new TranslateTimeline(frameCount);
                        timelineScale = scale;
                    }
                    timeline.boneIndex = boneIndex;
                    for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
                        timeline.setFrame(frameIndex, input.readFloat(), input.readFloat() * timelineScale,
                                input.readFloat() * timelineScale);
                        if (frameIndex < frameCount - 1)
                            readCurve(input, frameIndex, timeline);
                    }
                    timelines.add(timeline);
                    duration = Math.max(duration, timeline.getFrames()[frameCount * 3 - 3]);
                    break;
                }
                case TIMELINE_FLIPX:
                case TIMELINE_FLIPY: {
                    FlipXTimeline timeline = timelineType == TIMELINE_FLIPX ? new FlipXTimeline(frameCount)
                            : new FlipYTimeline(frameCount);
                    timeline.boneIndex = boneIndex;
                    for (int frameIndex = 0; frameIndex < frameCount; frameIndex++)
                        timeline.setFrame(frameIndex, input.readFloat(), input.readBoolean());
                    timelines.add(timeline);
                    duration = Math.max(duration, timeline.getFrames()[frameCount * 2 - 2]);
                    break;
                }
                }
            }
        }

        // IK timelines.
        for (int i = 0, n = input.readInt(true); i < n; i++) {
            IkConstraintData ikConstraint = skeletonData.ikConstraints.get(input.readInt(true));
            int frameCount = input.readInt(true);
            IkConstraintTimeline timeline = new IkConstraintTimeline(frameCount);
            timeline.ikConstraintIndex = skeletonData.getIkConstraints().indexOf(ikConstraint, true);
            for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
                timeline.setFrame(frameIndex, input.readFloat(), input.readFloat(), input.readByte());
                if (frameIndex < frameCount - 1)
                    readCurve(input, frameIndex, timeline);
            }
            timelines.add(timeline);
            duration = Math.max(duration, timeline.getFrames()[frameCount * 3 - 3]);
        }

        // FFD timelines.
        for (int i = 0, n = input.readInt(true); i < n; i++) {
            Skin skin = skeletonData.skins.get(input.readInt(true));
            for (int ii = 0, nn = input.readInt(true); ii < nn; ii++) {
                int slotIndex = input.readInt(true);
                for (int iii = 0, nnn = input.readInt(true); iii < nnn; iii++) {
                    Attachment attachment = skin.getAttachment(slotIndex, input.readString());
                    int frameCount = input.readInt(true);
                    FfdTimeline timeline = new FfdTimeline(frameCount);
                    timeline.slotIndex = slotIndex;
                    timeline.attachment = attachment;
                    for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
                        float time = input.readFloat();

                        float[] vertices;
                        int vertexCount;
                        if (attachment instanceof MeshAttachment)
                            vertexCount = ((MeshAttachment) attachment).getVertices().length;
                        else
                            vertexCount = ((SkinnedMeshAttachment) attachment).getWeights().length / 3 * 2;

                        int end = input.readInt(true);
                        if (end == 0) {
                            if (attachment instanceof MeshAttachment)
                                vertices = ((MeshAttachment) attachment).getVertices();
                            else
                                vertices = new float[vertexCount];
                        } else {
                            vertices = new float[vertexCount];
                            int start = input.readInt(true);
                            end += start;
                            if (scale == 1) {
                                for (int v = start; v < end; v++)
                                    vertices[v] = input.readFloat();
                            } else {
                                for (int v = start; v < end; v++)
                                    vertices[v] = input.readFloat() * scale;
                            }
                            if (attachment instanceof MeshAttachment) {
                                float[] meshVertices = ((MeshAttachment) attachment).getVertices();
                                for (int v = 0, vn = vertices.length; v < vn; v++)
                                    vertices[v] += meshVertices[v];
                            }
                        }

                        timeline.setFrame(frameIndex, time, vertices);
                        if (frameIndex < frameCount - 1)
                            readCurve(input, frameIndex, timeline);
                    }
                    timelines.add(timeline);
                    duration = Math.max(duration, timeline.getFrames()[frameCount - 1]);
                }
            }
        }

        // Draw order timeline.
        int drawOrderCount = input.readInt(true);
        if (drawOrderCount > 0) {
            DrawOrderTimeline timeline = new DrawOrderTimeline(drawOrderCount);
            int slotCount = skeletonData.slots.size;
            for (int i = 0; i < drawOrderCount; i++) {
                int offsetCount = input.readInt(true);
                int[] drawOrder = new int[slotCount];
                for (int ii = slotCount - 1; ii >= 0; ii--)
                    drawOrder[ii] = -1;
                int[] unchanged = new int[slotCount - offsetCount];
                int originalIndex = 0, unchangedIndex = 0;
                for (int ii = 0; ii < offsetCount; ii++) {
                    int slotIndex = input.readInt(true);
                    // Collect unchanged items.
                    while (originalIndex != slotIndex)
                        unchanged[unchangedIndex++] = originalIndex++;
                    // Set changed items.
                    drawOrder[originalIndex + input.readInt(true)] = originalIndex++;
                }
                // Collect remaining unchanged items.
                while (originalIndex < slotCount)
                    unchanged[unchangedIndex++] = originalIndex++;
                // Fill in unchanged items.
                for (int ii = slotCount - 1; ii >= 0; ii--)
                    if (drawOrder[ii] == -1)
                        drawOrder[ii] = unchanged[--unchangedIndex];
                timeline.setFrame(i, input.readFloat(), drawOrder);
            }
            timelines.add(timeline);
            duration = Math.max(duration, timeline.getFrames()[drawOrderCount - 1]);
        }

        // Event timeline.
        int eventCount = input.readInt(true);
        if (eventCount > 0) {
            EventTimeline timeline = new EventTimeline(eventCount);
            for (int i = 0; i < eventCount; i++) {
                float time = input.readFloat();
                EventData eventData = skeletonData.events.get(input.readInt(true));
                Event event = new Event(eventData);
                event.intValue = input.readInt(false);
                event.floatValue = input.readFloat();
                event.stringValue = input.readBoolean() ? input.readString() : eventData.stringValue;
                timeline.setFrame(i, time, event);
            }
            timelines.add(timeline);
            duration = Math.max(duration, timeline.getFrames()[eventCount - 1]);
        }
    } catch (IOException ex) {
        throw new SerializationException("Error reading skeleton file.", ex);
    }

    timelines.shrink();
    skeletonData.animations.add(new Animation(name, timelines, duration));
}

From source file:mt.Json.java

License:Apache License

/** @param knownType May be null if the type is unknown.
 * @param elementType May be null if the type is unknown. */
public void toJson(Object object, Class knownType, Class elementType, FileHandle file) {
    Writer writer = null;/*from  w w w . j  a  v a2 s  .  co  m*/
    try {
        writer = file.writer(false, "UTF-8");
        toJson(object, knownType, elementType, writer);
    } catch (Exception ex) {
        throw new SerializationException("Error writing file: " + file, ex);
    } finally {
        StreamUtils.closeQuietly(writer);
    }
}

From source file:mt.Json.java

License:Apache License

/** Writes all fields of the specified object to the current JSON object. */
public void writeFields(Object object) {
    Class type = object.getClass();

    Object[] defaultValues = getDefaultValues(type);

    ObjectMap<String, FieldMetadata> fields = typeToFields.get(type);
    if (fields == null)
        fields = cacheFields(type);// w ww. j  av a  2  s  .c  o m
    int i = 0;
    for (FieldMetadata metadata : new Values<FieldMetadata>(fields)) {
        Field field = metadata.field;
        try {
            Object value = field.get(object);
            if (defaultValues != null) {
                Object defaultValue = defaultValues[i++];
                if (value == null && defaultValue == null)
                    continue;
                if (value != null && defaultValue != null && value.equals(defaultValue))
                    continue;
            }

            if (debug)
                System.out.println("Writing field: " + field.getName() + " (" + type.getName() + ")");
            writer.name(field.getName());
            writeValue(value, field.getType(), metadata.elementType);
        } catch (ReflectionException ex) {
            throw new SerializationException(
                    "Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex);
        } catch (SerializationException ex) {
            ex.addTrace(field + " (" + type.getName() + ")");
            throw ex;
        } catch (Exception runtimeEx) {
            SerializationException ex = new SerializationException(runtimeEx);
            ex.addTrace(field + " (" + type.getName() + ")");
            throw ex;
        }
    }
}

From source file:mt.Json.java

License:Apache License

private Object[] getDefaultValues(Class type) {
    if (!usePrototypes)
        return null;
    if (classToDefaultValues.containsKey(type))
        return classToDefaultValues.get(type);
    Object object;/*from   ww  w  . j  ava  2 s.c o  m*/
    try {
        object = newInstance(type);
    } catch (Exception ex) {
        classToDefaultValues.put(type, null);
        return null;
    }

    ObjectMap<String, FieldMetadata> fields = typeToFields.get(type);
    if (fields == null)
        fields = cacheFields(type);

    Object[] values = new Object[fields.size];
    classToDefaultValues.put(type, values);

    int i = 0;
    for (FieldMetadata metadata : fields.values()) {
        Field field = metadata.field;
        try {
            values[i++] = field.get(object);
        } catch (ReflectionException ex) {
            throw new SerializationException(
                    "Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex);
        } catch (SerializationException ex) {
            ex.addTrace(field + " (" + type.getName() + ")");
            throw ex;
        } catch (RuntimeException runtimeEx) {
            SerializationException ex = new SerializationException(runtimeEx);
            ex.addTrace(field + " (" + type.getName() + ")");
            throw ex;
        }
    }
    return values;
}

From source file:mt.Json.java

License:Apache License

/** @param type May be null if the type is unknown.
 * @return May be null. *///w w w  .  j av a2s .  c  o m
public <T> T fromJson(Class<T> type, FileHandle file) {
    try {
        return (T) readValue(type, null, new JsonReader().parse(file));
    } catch (Exception ex) {
        throw new SerializationException("Error reading file: " + file, ex);
    }
}

From source file:mt.Json.java

License:Apache License

/** @param type May be null if the type is unknown.
 * @param elementType May be null if the type is unknown.
 * @return May be null. *///from   w  ww .j  av a  2 s .c o m
public <T> T fromJson(Class<T> type, Class elementType, FileHandle file) {
    try {
        return (T) readValue(type, elementType, new JsonReader().parse(file));
    } catch (Exception ex) {
        throw new SerializationException("Error reading file: " + file, ex);
    }
}

From source file:mt.Json.java

License:Apache License

protected Object newInstance(Class type) {
    try {/*from  w  ww. ja  v  a 2s .  c  om*/
        return ClassReflection.newInstance(type);
    } catch (Exception ex) {
        try {
            // Try a private constructor.
            Constructor constructor = ClassReflection.getDeclaredConstructor(type);
            constructor.setAccessible(true);
            return constructor.newInstance();
        } catch (SecurityException ignored) {
        } catch (ReflectionException ignored) {
            if (type.isEnum()) {
                return type.getEnumConstants()[0];
            }
            if (type.isArray())
                throw new SerializationException(
                        "Encountered JSON object when expected array of type: " + type.getName(), ex);
            else if (ClassReflection.isMemberClass(type) && !ClassReflection.isStaticClass(type))
                throw new SerializationException(
                        "Class cannot be created (non-static member class): " + type.getName(), ex);
            else
                throw new SerializationException(
                        "Class cannot be created (missing no-arg constructor): " + type.getName(), ex);
        } catch (Exception privateConstructorException) {
            ex = privateConstructorException;
        }
        throw new SerializationException("Error constructing instance of class: " + type.getName(), ex);
    }
}