diff --git a/libsecondlife/AssetSystem/AssetWearable.cs b/libsecondlife/AssetSystem/AssetWearable.cs index cfa5cdd0..b1923daf 100644 --- a/libsecondlife/AssetSystem/AssetWearable.cs +++ b/libsecondlife/AssetSystem/AssetWearable.cs @@ -435,7 +435,7 @@ namespace libsecondlife.AssetSystem try { uint id = UInt32.Parse(fields[0]); - LLUUID texture = LLUUID.Parse(fields[1]); + LLUUID texture = new LLUUID(fields[1]); _Textures[id] = texture; } diff --git a/libsecondlife/LLSD.cs b/libsecondlife/LLSD.cs index 2f0117a4..7e1ee6de 100644 --- a/libsecondlife/LLSD.cs +++ b/libsecondlife/LLSD.cs @@ -129,7 +129,7 @@ namespace libsecondlife } else if (obj is LLUUID) { - LLUUID u = obj as LLUUID; + LLUUID u = (LLUUID) obj; writer.WriteStartElement(String.Empty, "uuid", String.Empty); writer.WriteString(u.ToStringHyphenated()); writer.WriteEndElement(); diff --git a/libsecondlife/Types.cs b/libsecondlife/Types.cs index 90feb798..4a9fd8ee 100644 --- a/libsecondlife/Types.cs +++ b/libsecondlife/Types.cs @@ -1,9 +1,8 @@ /* - * Copyright (c) 2006, Second Life Reverse Engineering Team - * All rights reserved. + * Copyright (c) 2006, 2007 Second Life Reverse Engineering Team * - * - Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. @@ -35,34 +34,23 @@ namespace libsecondlife /// A 128-bit Universally Unique Identifier, used throughout the Second /// Life networking protocol /// - [Serializable] - public class LLUUID : IXmlSerializable - { - /// Get a byte array of the 16 raw bytes making up the UUID - public byte[] Data { get { return data; } } - - /// The 16 bytes that make up the UUID - protected byte[] data; - - - /// - /// Default constructor - /// - public LLUUID() - { - data = new byte[16]; - } - - /// - /// Constructor that takes a string UUID representation - /// - /// A string representation of a UUID, case - /// insensitive and can either be hyphenated or non-hyphenated - /// LLUUID("11f8aa9c-b071-4242-836b-13b7abe0d489") - public LLUUID(string val) - { - data = StringToBytes(val); - } + public struct LLUUID + { + /// The Guid we are wrapping to make an LLUUID + public Guid id; + + /// Get a byte array of the 16 raw bytes making up the UUID + public byte[] Data { get { return id.ToByteArray(); } } + + /// + /// Constructor that takes a string UUID representation + /// + /// A string representation of a UUID, case + /// insensitive and can either be hyphenated or non-hyphenated + /// LLUUID("11f8aa9c-b071-4242-836b-13b7abe0d489") + public LLUUID(string val) { + id = new Guid(val); + } /// /// Constructor that takes a byte array containing a UUID @@ -71,8 +59,9 @@ namespace libsecondlife /// Beginning offset in the array public LLUUID(byte[] byteArray, int pos) { - data = new byte[16]; + byte[] data = new byte[16]; Array.Copy(byteArray, pos, data, 0, 16); + id=new Guid(data); } /// @@ -82,9 +71,10 @@ namespace libsecondlife /// 64-bit unsigned integer to convert to a UUID public LLUUID(ulong val) { - data = new byte[16]; + byte[] data = new byte[16]; byte[] bytes = BitConverter.GetBytes(val); Array.Copy(bytes, data, bytes.Length); + id=new Guid(data); } /// @@ -93,24 +83,24 @@ namespace libsecondlife /// A 16 byte array containing this UUID public byte[] GetBytes() { - return data; + return Data; } + + /// + /// Calculate an LLCRC (cyclic redundancy check) for this LLUUID + /// + /// The CRC checksum for this LLUUID + public uint CRC() + { + uint retval = 0; + byte[] _data=GetBytes(); + retval += (uint)((_data[3] << 24) + (_data[2] << 16) + (_data[1] << 8) + _data[0]); + retval += (uint)((_data[7] << 24) + (_data[6] << 16) + (_data[5] << 8) + _data[4]); + retval += (uint)((_data[11] << 24) + (_data[10] << 16) + (_data[9] << 8) + _data[8]); + retval += (uint)((_data[15] << 24) + (_data[14] << 16) + (_data[13] << 8) + _data[12]); - /// - /// Calculate an LLCRC (cyclic redundancy check) for this LLUUID - /// - /// The CRC checksum for this LLUUID - public uint CRC() - { - uint retval = 0; - - retval += (uint)((Data[3] << 24) + (Data[2] << 16) + (Data[1] << 8) + Data[0]); - retval += (uint)((Data[7] << 24) + (Data[6] << 16) + (Data[5] << 8) + Data[4]); - retval += (uint)((Data[11] << 24) + (Data[10] << 16) + (Data[9] << 8) + Data[8]); - retval += (uint)((Data[15] << 24) + (Data[14] << 16) + (Data[13] << 8) + Data[12]); - - return retval; - } + return retval; + } /// /// Combine two UUIDs together by taking the MD5 hash of a byte array @@ -122,43 +112,12 @@ namespace libsecondlife { // Build the buffer to MD5 byte[] input = new byte[32]; - Array.Copy(data, input, 16); - Array.Copy(other.Data, 0, input, 16, 16); + Array.Copy(GetBytes(), input, 16); + Array.Copy(other.GetBytes(), 0, input, 16, 16); return new LLUUID(Helpers.MD5Builder.ComputeHash(input), 0); } - /// - /// Parse the bytes for a LLUUID from a string - /// - /// String containing 32 (or 36 with hyphens) character UUID - /// - protected static byte[] StringToBytes(string val) - { - if (val.Length == 36) val = val.Replace("-", ""); - if (val.Length != 32) throw new Exception("Malformed LLUUID: " + val); - - byte[] parseData = new byte[16]; - - for (int i = 0; i < 16; ++i) - { - parseData[i] = Convert.ToByte(val.Substring(i * 2, 2), 16); - } - - return parseData; - } - - /// - /// Generate a LLUUID from a string - /// - /// A string representation of a UUID, case - /// insensitive and can either be hyphenated or non-hyphenated - /// LLUUID.Parse("11f8aa9c-b071-4242-836b-13b7abe0d489") - public static LLUUID Parse(string val) - { - return new LLUUID(StringToBytes(val), 0); - } - /// /// Generate a LLUUID from a string /// @@ -172,12 +131,12 @@ namespace libsecondlife { try { - result = Parse(val); + result = new LLUUID(val); return true; } catch (Exception) { - result = null; + result = new LLUUID(0); return false; } } @@ -191,64 +150,6 @@ namespace libsecondlife return new LLUUID(Guid.NewGuid().ToByteArray(), 0); } - /// - /// Required implementation for XML serialization - /// - /// null - public System.Xml.Schema.XmlSchema GetSchema() - { - return null; - } - - /// - /// Deserializes an XML UUID - /// - /// XmlReader containing the UUID to deserialize - public void ReadXml(System.Xml.XmlReader reader) - { - string val = reader.ReadString(); - - if (val.Length > 0) - { - if (val.Length == 36) val = val.Replace("-", ""); - - if (val.Length != 32) throw new Exception("Malformed data passed to LLUUID constructor: " + val); - - for (int i = 0; i < 16; ++i) - { - data[i] = Convert.ToByte(val.Substring(i * 2, 2), 16); - } - } - - reader.Skip(); - } - - /// - /// Serialize this UUID to XML - /// - /// XmlWriter to serialize to - public void WriteXml(System.Xml.XmlWriter writer) - { - if (this != LLUUID.Zero) - writer.WriteString(this.ToString()); - } - - /// - /// Return a hash code for this UUID, used by .NET for hash tables - /// - /// An integer composed of all the UUID bytes XORed together - public override int GetHashCode() - { - int hash = data[0]; - - for (int i = 1; i < 16; i++) - { - hash ^= data[i]; - } - - return hash; - } - /// /// Comparison function /// @@ -261,12 +162,7 @@ namespace libsecondlife LLUUID uuid = (LLUUID)o; - for (int i = 0; i < 16; ++i) - { - if (Data[i] != uuid.Data[i]) return false; - } - - return true; + return uuid.id.Equals(id); } /// @@ -275,38 +171,21 @@ namespace libsecondlife /// First LLUUID for comparison /// Second LLUUID for comparison /// True if the UUIDs are byte for byte equal, otherwise false - public static bool operator==(LLUUID lhs, LLUUID rhs) - { - // If both are null, or both are same instance, return true - if (System.Object.ReferenceEquals(lhs, rhs)) - { - return true; - } - - // If one is null, but not both, return false. - if (((object)lhs == null) || ((object)rhs == null)) - { - return false; - } - - for (int i = 0; i < 16; ++i) - { - if (lhs.Data[i] != rhs.Data[i]) return false; - } - - return true; - } - + public static bool operator==(LLUUID lhs, LLUUID rhs) + { + return lhs.Equals(rhs); + } + /// /// Not equals operator /// /// First LLUUID for comparison /// Second LLUUID for comparison /// True if the UUIDs are not equal, otherwise true - public static bool operator!=(LLUUID lhs, LLUUID rhs) - { - return !(lhs == rhs); - } + public static bool operator!=(LLUUID lhs, LLUUID rhs) + { + return !(lhs == rhs); + } /// /// XOR operator @@ -332,10 +211,10 @@ namespace libsecondlife /// A UUID in string form. Case insensitive, /// hyphenated or non-hyphenated /// A UUID built from the string representation - public static implicit operator LLUUID(string val) - { - return new LLUUID(val); - } + public static implicit operator LLUUID(string val) + { + return new LLUUID(val); + } /// /// Get a string representation of this UUID @@ -343,17 +222,10 @@ namespace libsecondlife /// A string representation of this UUID, lowercase and /// without hyphens /// 11f8aa9cb0714242836b13b7abe0d489 - public override string ToString() - { - string uuid = String.Empty; - - for (int i = 0; i < 16; ++i) - { - uuid += Data[i].ToString("x2"); - } - - return uuid; - } + public override string ToString() + { + return id.ToString("N"); + } /// /// Get a hyphenated string representation of this UUID @@ -361,27 +233,21 @@ namespace libsecondlife /// A string representation of this UUID, lowercase and /// with hyphens /// 11f8aa9c-b071-4242-836b-13b7abe0d489 - public string ToStringHyphenated() - { - string uuid = String.Empty; - - for (int i = 0; i < 16; ++i) - { - uuid += Data[i].ToString("x2"); - } - uuid = uuid.Insert(20, "-"); - uuid = uuid.Insert(16, "-"); - uuid = uuid.Insert(12, "-"); - uuid = uuid.Insert(8, "-"); - - return uuid; - } - + public string ToStringHyphenated() + { + return id.ToString("D"); + } + + public override int GetHashCode() + { + return id.GetHashCode(); + } + /// /// An LLUUID with a value of all zeroes /// public static readonly LLUUID Zero = new LLUUID(); - } + } /// /// A three-dimensional vector with floating-point values diff --git a/libsecondlife/libsecondlife.Utilities/Appearance.cs b/libsecondlife/libsecondlife.Utilities/Appearance.cs index 75b0111d..4e4fb2b1 100644 --- a/libsecondlife/libsecondlife.Utilities/Appearance.cs +++ b/libsecondlife/libsecondlife.Utilities/Appearance.cs @@ -297,7 +297,7 @@ namespace libsecondlife.Utilities.Appearance try { int id = Int32.Parse(fields[0]); - LLUUID texture = LLUUID.Parse(fields[1]); + LLUUID texture = new LLUUID(fields[1]); Textures[id] = texture; } diff --git a/libsecondlife/libsecondlife.build b/libsecondlife/libsecondlife.build index c3b5a71a..7c9f9621 100644 --- a/libsecondlife/libsecondlife.build +++ b/libsecondlife/libsecondlife.build @@ -114,11 +114,12 @@ - + - + +