Пример #1
0
 public static object ToObject(Type type, LLSD value)
 {
     if (type == typeof(ulong))
     {
         if (value.Type == LLSDType.Binary)
         {
             byte[] bytes = value.AsBinary();
             return(Utils.BytesToUInt64(bytes));
         }
         else
         {
             return((ulong)value.AsInteger());
         }
     }
     else if (type == typeof(uint))
     {
         if (value.Type == LLSDType.Binary)
         {
             byte[] bytes = value.AsBinary();
             return(Utils.BytesToUInt(bytes));
         }
         else
         {
             return((uint)value.AsInteger());
         }
     }
     else if (type == typeof(ushort))
     {
         return((ushort)value.AsInteger());
     }
     else if (type == typeof(byte))
     {
         return((byte)value.AsInteger());
     }
     else if (type == typeof(short))
     {
         return((short)value.AsInteger());
     }
     else if (type == typeof(string))
     {
         return(value.AsString());
     }
     else if (type == typeof(bool))
     {
         return(value.AsBoolean());
     }
     else if (type == typeof(float))
     {
         return((float)value.AsReal());
     }
     else if (type == typeof(double))
     {
         return(value.AsReal());
     }
     else if (type == typeof(int))
     {
         return(value.AsInteger());
     }
     else if (type == typeof(UUID))
     {
         return(value.AsUUID());
     }
     else if (type == typeof(Vector3))
     {
         if (value.Type == LLSDType.Array)
         {
             return(((LLSDArray)value).AsVector3());
         }
         else
         {
             return(Vector3.Zero);
         }
     }
     else if (type == typeof(Vector4))
     {
         if (value.Type == LLSDType.Array)
         {
             return(((LLSDArray)value).AsVector4());
         }
         else
         {
             return(Vector4.Zero);
         }
     }
     else if (type == typeof(Quaternion))
     {
         if (value.Type == LLSDType.Array)
         {
             return(((LLSDArray)value).AsQuaternion());
         }
         else
         {
             return(Quaternion.Identity);
         }
     }
     else
     {
         return(null);
     }
 }
Пример #2
0
        private static void SerializeBinaryElement(MemoryStream stream, LLSD llsd)
        {
            switch (llsd.Type)
            {
            case LLSDType.Unknown:
                stream.WriteByte(undefBinaryValue);
                break;

            case LLSDType.Boolean:
                stream.Write(llsd.AsBinary(), 0, 1);
                break;

            case LLSDType.Integer:
                stream.WriteByte(integerBinaryMarker);
                stream.Write(llsd.AsBinary(), 0, int32Length);
                break;

            case LLSDType.Real:
                stream.WriteByte(realBinaryMarker);
                stream.Write(llsd.AsBinary(), 0, doubleLength);
                break;

            case LLSDType.UUID:
                stream.WriteByte(uuidBinaryMarker);
                stream.Write(llsd.AsBinary(), 0, 16);
                break;

            case LLSDType.String:
                stream.WriteByte(stringBinaryMarker);
                byte[] rawString          = llsd.AsBinary();
                byte[] stringLengthNetEnd = HostToNetworkIntBytes(rawString.Length);
                stream.Write(stringLengthNetEnd, 0, int32Length);
                stream.Write(rawString, 0, rawString.Length);
                break;

            case LLSDType.Binary:
                stream.WriteByte(binaryBinaryMarker);
                byte[] rawBinary          = llsd.AsBinary();
                byte[] binaryLengthNetEnd = HostToNetworkIntBytes(rawBinary.Length);
                stream.Write(binaryLengthNetEnd, 0, int32Length);
                stream.Write(rawBinary, 0, rawBinary.Length);
                break;

            case LLSDType.Date:
                stream.WriteByte(dateBinaryMarker);
                stream.Write(llsd.AsBinary(), 0, doubleLength);
                break;

            case LLSDType.URI:
                stream.WriteByte(uriBinaryMarker);
                byte[] rawURI          = llsd.AsBinary();
                byte[] uriLengthNetEnd = HostToNetworkIntBytes(rawURI.Length);
                stream.Write(uriLengthNetEnd, 0, int32Length);
                stream.Write(rawURI, 0, rawURI.Length);
                break;

            case LLSDType.Array:
                SerializeBinaryArray(stream, (LLSDArray)llsd);
                break;

            case LLSDType.Map:
                SerializeBinaryMap(stream, (LLSDMap)llsd);
                break;

            default:
                throw new LLSDException("Binary serialization: Not existing element discovered.");
            }
        }