예제 #1
0
        private static string GetNativeType(NbtTag t)
        {
            switch (t.GetType().Name)
            {
            case "NbtByte":
                return("byte");

            case "NbtByteArray":
                return("byte[]");

            case "NbtCompound":
                return("NbtCompound");    // Note to myself to fix shit

            case "NbtDouble":
                return("double");

            case "NbtFloat":
                return("float");

            case "NbtInt":
                return("int");

            case "NbtList":
                return("NbtList");    // Note to myself again

            case "NbtLong":
                return("long");

            case "NbtShort":
                return("short");

            case "NbtString":
                return("string");

            default:
                return(t.GetType().Name);
            }
        }
예제 #2
0
 private static string GetNativeType(NbtTag t)
 {
     switch (t.GetType().Name)
     {
         case "NbtByte":
             return "byte";
         case "NbtByteArray":
             return "byte[]";
         case "NbtCompound":
             return "NbtCompound"; // Note to myself to fix shit
         case "NbtDouble":
             return "double";
         case "NbtFloat":
             return "float";
         case "NbtInt":
             return "int";
         case "NbtList":
             return "NbtList"; // Note to myself again
         case "NbtLong":
             return "long";
         case "NbtShort":
             return "short";
         case "NbtString":
             return "string";
         default:
             return t.GetType().Name;
     }
 }
예제 #3
0
        /**
         * AFTER the opening [
         */
        private NbtList ParseList()
        {
            bool    more    = true; //I'm sensing a pattern here...
            NbtList list    = new NbtList();
            NbtTag  current = null;
            int     index   = -1;

            while (more)
            {
                char next;
                try
                {
                    next = NextChar();
                }
                catch (Exception)
                {
                    throw new Exception(@"Unexpected EOF, expected ']'");
                }
                switch (next)
                {
                case ']':
                    more = false;
                    goto case ',';     //f****n fall through.

                case ',':
                    if (current != null)
                    {
                        if (index != -1)
                        {
                            if (index >= list.Count)
                            {
                                for (int i = list.Count; i <= index; i++)
                                {
                                    list.Add((NbtTag)current.GetType().GetConstructor(new Type[0]).Invoke(new object[0]));     //HOLY FUCKNUGGETS
                                }
                            }
                            list[index] = current;
                        }
                        else
                        {
                            list.Add(current);
                        }
                        index   = -1;
                        current = null;
                    }
                    break;

                case ' ':
                case '\t':
                case '\r':
                case '\n':
                    break;

                case ':':     //oh noes explicit index, doesn't even have to be in order.
                    index   = current.IntValue;
                    current = null;
                    break;

                default:
                    Position--;
                    current = ParseTag();
                    break;
                }
            }
            return(list);
        }
예제 #4
0
        public object Deserialize(NbtTag value)
        {
            if (value is NbtByte)
                return ((NbtByte)value).Value;
            else if (value is NbtByteArray)
                return ((NbtByteArray)value).Value;
            else if (value is NbtDouble)
                return ((NbtDouble)value).Value;
            else if (value is NbtFloat)
                return ((NbtFloat)value).Value;
            else if (value is NbtInt)
                return ((NbtInt)value).Value;
            else if (value is NbtIntArray)
                return ((NbtIntArray)value).Value;
            else if (value is NbtLong)
                return ((NbtLong)value).Value;
            else if (value is NbtShort)
                return ((NbtShort)value).Value;
            else if (value is NbtString)
                return ((NbtString)value).Value;
            else if(value is NbtCompound)
            {
                var compound = value as NbtCompound;
                var properties = Type.GetProperties().Where(p =>
                    !Attribute.GetCustomAttributes(p, typeof(NbtIgnoreAttribute)).Any());
                var resultObject = Activator.CreateInstance(Type);
                foreach (var property in properties)
                {
                    if (!property.CanWrite)
                        continue;
                    string name = property.Name;
                    var nameAttributes = Attribute.GetCustomAttributes(property, typeof(TagNameAttribute));

                    if (nameAttributes.Length != 0)
                        name = ((TagNameAttribute)nameAttributes[0]).Name;
                    var node = compound.Tags.SingleOrDefault(a => a.Name == name);
                    if (node == null) continue;
                    var data = new NbtSerializer(property.PropertyType).Deserialize(node);

                    if (property.PropertyType == typeof(bool)
                        && data is byte)
                        data = (byte)data == 1;

                    property.SetValue(resultObject, data, null);
                }
                
                return resultObject;
            }
            
            throw new NotSupportedException("The node type '" + value.GetType() + "' is not supported.");
        }
예제 #5
0
        public object Deserialize(NbtTag value)
        {
            if (value is NbtByte)
            {
                return(((NbtByte)value).Value);
            }
            else if (value is NbtByteArray)
            {
                return(((NbtByteArray)value).Value);
            }
            else if (value is NbtDouble)
            {
                return(((NbtDouble)value).Value);
            }
            else if (value is NbtFloat)
            {
                return(((NbtFloat)value).Value);
            }
            else if (value is NbtInt)
            {
                return(((NbtInt)value).Value);
            }
            else if (value is NbtIntArray)
            {
                return(((NbtIntArray)value).Value);
            }
            else if (value is NbtLong)
            {
                return(((NbtLong)value).Value);
            }
            else if (value is NbtShort)
            {
                return(((NbtShort)value).Value);
            }
            else if (value is NbtString)
            {
                return(((NbtString)value).Value);
            }
            else if (value is NbtCompound)
            {
                var compound   = value as NbtCompound;
                var properties = Type.GetProperties().Where(p =>
                                                            !Attribute.GetCustomAttributes(p, typeof(NbtIgnoreAttribute)).Any());
                var resultObject = Activator.CreateInstance(Type);
                foreach (var property in properties)
                {
                    if (!property.CanWrite)
                    {
                        continue;
                    }
                    string name           = property.Name;
                    var    nameAttributes = Attribute.GetCustomAttributes(property, typeof(TagNameAttribute));

                    if (nameAttributes.Length != 0)
                    {
                        name = ((TagNameAttribute)nameAttributes[0]).Name;
                    }
                    var node = compound.Tags.SingleOrDefault(a => a.Name == name);
                    if (node == null)
                    {
                        continue;
                    }
                    var data = new NbtSerializer(property.PropertyType).Deserialize(node);

                    if (property.PropertyType == typeof(bool) &&
                        data is byte)
                    {
                        data = (byte)data == 1;
                    }

                    property.SetValue(resultObject, data, null);
                }

                return(resultObject);
            }

            throw new NotSupportedException("The node type '" + value.GetType() + "' is not supported.");
        }
예제 #6
0
        public object Deserialize(NbtTag value, bool skipInterfaceCheck = false)
        {
            if (!skipInterfaceCheck && typeof(INbtSerializable).IsAssignableFrom(Type))
            {
                var instance = (INbtSerializable)Activator.CreateInstance(Type);
                instance.Deserialize(value);
                return(instance);
            }
            if (value is NbtByte)
            {
                return(((NbtByte)value).Value);
            }
            else if (value is NbtByteArray)
            {
                return(((NbtByteArray)value).Value);
            }
            else if (value is NbtDouble)
            {
                return(((NbtDouble)value).Value);
            }
            else if (value is NbtFloat)
            {
                return(((NbtFloat)value).Value);
            }
            else if (value is NbtInt)
            {
                return(((NbtInt)value).Value);
            }
            else if (value is NbtIntArray)
            {
                return(((NbtIntArray)value).Value);
            }
            else if (value is NbtLong)
            {
                return(((NbtLong)value).Value);
            }
            else if (value is NbtShort)
            {
                return(((NbtShort)value).Value);
            }
            else if (value is NbtString)
            {
                return(((NbtString)value).Value);
            }
            else if (value is NbtList)
            {
                var list = (NbtList)value;
                var type = typeof(object);
                if (list.ListType == NbtTagType.Byte)
                {
                    type = typeof(byte);
                }
                else if (list.ListType == NbtTagType.ByteArray)
                {
                    type = typeof(byte[]);
                }
                else if (list.ListType == NbtTagType.Compound)
                {
                    if (Type.IsArray)
                    {
                        type = Type.GetElementType();
                    }
                    else
                    {
                        type = typeof(object);
                    }
                }
                else if (list.ListType == NbtTagType.Double)
                {
                    type = typeof(double);
                }
                else if (list.ListType == NbtTagType.Float)
                {
                    type = typeof(float);
                }
                else if (list.ListType == NbtTagType.Int)
                {
                    type = typeof(int);
                }
                else if (list.ListType == NbtTagType.IntArray)
                {
                    type = typeof(int[]);
                }
                else if (list.ListType == NbtTagType.Long)
                {
                    type = typeof(long);
                }
                else if (list.ListType == NbtTagType.Short)
                {
                    type = typeof(short);
                }
                else if (list.ListType == NbtTagType.String)
                {
                    type = typeof(string);
                }
                else
                {
                    throw new NotSupportedException("The NBT list type '" + list.TagType + "' is not supported.");
                }
                var array           = Array.CreateInstance(type, list.Count);
                var innerSerializer = new NbtSerializer(type);
                for (int i = 0; i < array.Length; i++)
                {
                    array.SetValue(innerSerializer.Deserialize(list[i]), i);
                }
                return(array);
            }
            else if (value is NbtCompound)
            {
                var compound   = value as NbtCompound;
                var properties = Type.GetProperties().Where(p =>
                                                            !Attribute.GetCustomAttributes(p, typeof(NbtIgnoreAttribute)).Any());
                var resultObject = Activator.CreateInstance(Type);
                foreach (var property in properties)
                {
                    if (!property.CanWrite)
                    {
                        continue;
                    }
                    string name           = property.Name;
                    var    nameAttributes = Attribute.GetCustomAttributes(property, typeof(TagNameAttribute));

                    if (nameAttributes.Length != 0)
                    {
                        name = ((TagNameAttribute)nameAttributes[0]).Name;
                    }
                    var node = compound.Tags.SingleOrDefault(a => a.Name == name);
                    if (node == null)
                    {
                        continue;
                    }
                    object data;
                    if (typeof(INbtSerializable).IsAssignableFrom(property.PropertyType))
                    {
                        data = Activator.CreateInstance(property.PropertyType);
                        ((INbtSerializable)data).Deserialize(node);
                    }
                    else
                    {
                        data = new NbtSerializer(property.PropertyType).Deserialize(node);
                    }

                    // Some manual casting for edge cases
                    if (property.PropertyType == typeof(bool) &&
                        data is byte)
                    {
                        data = (byte)data == 1;
                    }
                    if (property.PropertyType == typeof(sbyte) && data is byte)
                    {
                        data = (sbyte)(byte)data;
                    }

                    property.SetValue(resultObject, data, null);
                }

                return(resultObject);
            }

            throw new NotSupportedException("The node type '" + value.GetType() + "' is not supported.");
        }
예제 #7
0
        /// <summary>
        /// Serialize an fNbt.NbtTag to a JSON-ish representation Minecraft likes
        /// </summary>
        /// <param name="tag">The tag to serialize</param>
        /// <returns>The serialized tag</returns>
        public static string Serialize(NbtTag tag)
        {
            StringBuilder sb = new StringBuilder();

            switch (tag.TagType)
            {
            case NbtTagType.List:
                if (((NbtList)tag).Count == 0)
                {
                    sb.Append("[]");
                }
                else
                {
                    sb.Append("[");
                    NbtList list = (NbtList)tag;
                    for (int i = 0; i < list.Count; i++)
                    {
                        NbtTag subTag = list[i];
                        sb.Append(i);
                        sb.Append(":");
                        sb.Append(Serialize(subTag));
                        sb.Append(",");
                    }
                    sb.Remove(sb.Length - 1, 1);
                    sb.Append("]");
                }
                break;

            case NbtTagType.Compound:
                if (((NbtCompound)tag).Count == 0)
                {
                    sb.Append("{}");
                }
                else
                {
                    sb.Append("{");
                    foreach (NbtTag subTag in (NbtCompound)tag)
                    {
                        sb.Append(subTag.Name);     //fine mojang. you win. dont quote your suckin names.
                                                    //btw did vs seriously just autocorrect that to suckin or am i high?
                        sb.Append(":");
                        sb.Append(Serialize(subTag));
                        sb.Append(",");
                    }
                    sb.Remove(sb.Length - 1, 1);
                    sb.Append("}");
                }
                break;

            case NbtTagType.String:
                sb.Append('"');
                sb.Append(tag.StringValue.Replace("\"", "\\\"").Replace("\\", "\\\\"));
                sb.Append('"');
                break;

            case NbtTagType.Int:
            case NbtTagType.Double:
                sb.Append(tag.StringValue);
                break;

            case NbtTagType.Long:
                sb.Append(tag.StringValue);
                sb.Append("l");
                break;

            case NbtTagType.Byte:
                sb.Append(tag.StringValue);
                sb.Append("b");
                break;

            case NbtTagType.Float:
                sb.Append(tag.StringValue);
                sb.Append("f");
                break;

            default:
                //Not supposed to happen
                throw new NotImplementedException("Emitting a " + tag.GetType().ToString());
            }
            return(sb.ToString());
        }