private static LLSD DeserializeNotationReal(StringReader reader) { int character; StringBuilder s = new StringBuilder(); if (((character = reader.Peek()) > 0) && ((char)character == '-' && (char)character == '+')) { s.Append((char)character); reader.Read(); } while (((character = reader.Peek()) > 0) && (Char.IsDigit((char)character) || (char)character == '.' || (char)character == 'e' || (char)character == 'E' || (char)character == '+' || (char)character == '-')) { s.Append((char)character); reader.Read(); } double dbl; if (!Utils.TryParseDouble(s.ToString(), out dbl)) { throw new LLSDException("Notation LLSD parsing: Can't parse real value: " + s.ToString()); } return(LLSD.FromReal(dbl)); }
public static LLSD FromVector2(Vector2 value) { LLSDArray array = new LLSDArray(); array.Add(LLSD.FromReal(value.X)); array.Add(LLSD.FromReal(value.Y)); return(array); }
public static LLSD FromColor4(Color4 value) { LLSDArray array = new LLSDArray(); array.Add(LLSD.FromReal(value.R)); array.Add(LLSD.FromReal(value.G)); array.Add(LLSD.FromReal(value.B)); array.Add(LLSD.FromReal(value.A)); return(array); }
public static LLSD FromQuaternion(Quaternion value) { LLSDArray array = new LLSDArray(); array.Add(LLSD.FromReal(value.X)); array.Add(LLSD.FromReal(value.Y)); array.Add(LLSD.FromReal(value.Z)); array.Add(LLSD.FromReal(value.W)); return(array); }
private static LLSD ParseBinaryElement(MemoryStream stream) { SkipWhiteSpace(stream); LLSD llsd; int marker = stream.ReadByte(); if (marker < 0) { throw new LLSDException("Binary LLSD parsing:Unexpected end of stream."); } switch ((byte)marker) { case undefBinaryValue: llsd = new LLSD(); break; case trueBinaryValue: llsd = LLSD.FromBoolean(true); break; case falseBinaryValue: llsd = LLSD.FromBoolean(false); break; case integerBinaryMarker: int integer = NetworkToHostInt(ConsumeBytes(stream, int32Length)); llsd = LLSD.FromInteger(integer); break; case realBinaryMarker: double dbl = NetworkToHostDouble(ConsumeBytes(stream, doubleLength)); llsd = LLSD.FromReal(dbl); break; case uuidBinaryMarker: llsd = LLSD.FromUUID(new UUID(ConsumeBytes(stream, 16), 0)); break; case binaryBinaryMarker: int binaryLength = NetworkToHostInt(ConsumeBytes(stream, int32Length)); llsd = LLSD.FromBinary(ConsumeBytes(stream, binaryLength)); break; case stringBinaryMarker: int stringLength = NetworkToHostInt(ConsumeBytes(stream, int32Length)); string ss = Encoding.UTF8.GetString(ConsumeBytes(stream, stringLength)); llsd = LLSD.FromString(ss); break; case uriBinaryMarker: int uriLength = NetworkToHostInt(ConsumeBytes(stream, int32Length)); string sUri = Encoding.UTF8.GetString(ConsumeBytes(stream, uriLength)); Uri uri; try { uri = new Uri(sUri, UriKind.RelativeOrAbsolute); } catch { throw new LLSDException("Binary LLSD parsing: Invalid Uri format detected."); } llsd = LLSD.FromUri(uri); break; case dateBinaryMarker: double timestamp = NetworkToHostDouble(ConsumeBytes(stream, doubleLength)); DateTime dateTime = DateTime.SpecifyKind(Utils.Epoch, DateTimeKind.Utc); dateTime = dateTime.AddSeconds(timestamp); llsd = LLSD.FromDate(dateTime.ToLocalTime()); break; case arrayBeginBinaryMarker: llsd = ParseBinaryArray(stream); break; case mapBeginBinaryMarker: llsd = ParseBinaryMap(stream); break; default: throw new LLSDException("Binary LLSD parsing: Unknown type marker."); } return(llsd); }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <returns></returns> private static LLSD ParseXmlElement(XmlTextReader reader) { SkipWhitespace(reader); if (reader.NodeType != XmlNodeType.Element) { throw new LLSDException("Expected an element"); } string type = reader.LocalName; LLSD ret; switch (type) { case "undef": if (reader.IsEmptyElement) { reader.Read(); return(new LLSD()); } reader.Read(); SkipWhitespace(reader); ret = new LLSD(); break; case "boolean": if (reader.IsEmptyElement) { reader.Read(); return(LLSD.FromBoolean(false)); } if (reader.Read()) { string s = reader.ReadString().Trim(); if (!String.IsNullOrEmpty(s) && (s == "true" || s == "1")) { ret = LLSD.FromBoolean(true); break; } } ret = LLSD.FromBoolean(false); break; case "integer": if (reader.IsEmptyElement) { reader.Read(); return(LLSD.FromInteger(0)); } if (reader.Read()) { int value = 0; Int32.TryParse(reader.ReadString().Trim(), out value); ret = LLSD.FromInteger(value); break; } ret = LLSD.FromInteger(0); break; case "real": if (reader.IsEmptyElement) { reader.Read(); return(LLSD.FromReal(0d)); } if (reader.Read()) { double value = 0d; string str = reader.ReadString().Trim().ToLower(); if (str == "nan") { value = Double.NaN; } else { Utils.TryParseDouble(str, out value); } ret = LLSD.FromReal(value); break; } ret = LLSD.FromReal(0d); break; case "uuid": if (reader.IsEmptyElement) { reader.Read(); return(LLSD.FromUUID(UUID.Zero)); } if (reader.Read()) { UUID value = UUID.Zero; UUID.TryParse(reader.ReadString().Trim(), out value); ret = LLSD.FromUUID(value); break; } ret = LLSD.FromUUID(UUID.Zero); break; case "date": if (reader.IsEmptyElement) { reader.Read(); return(LLSD.FromDate(Utils.Epoch)); } if (reader.Read()) { DateTime value = Utils.Epoch; DateTime.TryParse(reader.ReadString().Trim(), out value); ret = LLSD.FromDate(value); break; } ret = LLSD.FromDate(Utils.Epoch); break; case "string": if (reader.IsEmptyElement) { reader.Read(); return(LLSD.FromString(String.Empty)); } if (reader.Read()) { ret = LLSD.FromString(reader.ReadString()); break; } ret = LLSD.FromString(String.Empty); break; case "binary": if (reader.IsEmptyElement) { reader.Read(); return(LLSD.FromBinary(new byte[0])); } if (reader.GetAttribute("encoding") != null && reader.GetAttribute("encoding") != "base64") { throw new LLSDException("Unsupported binary encoding: " + reader.GetAttribute("encoding")); } if (reader.Read()) { try { ret = LLSD.FromBinary(Convert.FromBase64String(reader.ReadString().Trim())); break; } catch (FormatException ex) { throw new LLSDException("Binary decoding exception: " + ex.Message); } } ret = LLSD.FromBinary(new byte[0]); break; case "uri": if (reader.IsEmptyElement) { reader.Read(); return(LLSD.FromUri(new Uri(String.Empty, UriKind.RelativeOrAbsolute))); } if (reader.Read()) { ret = LLSD.FromUri(new Uri(reader.ReadString(), UriKind.RelativeOrAbsolute)); break; } ret = LLSD.FromUri(new Uri(String.Empty, UriKind.RelativeOrAbsolute)); break; case "map": return(ParseXmlMap(reader)); case "array": return(ParseXmlArray(reader)); default: reader.Read(); ret = null; break; } if (reader.NodeType != XmlNodeType.EndElement || reader.LocalName != type) { throw new LLSDException("Expected </" + type + ">"); } else { reader.Read(); return(ret); } }