/// <summary> /// write a guid struct /// </summary> /// <param name="data"></param> public void Write(ref Guid data) { if (Length + 16 > _Buffer.Length) { _capacity += 16; Resize(); } var value = new GuidStruct() { Value = data }; _Buffer[Length] = value.Byte0; _Buffer[Length + 1] = value.Byte1; _Buffer[Length + 2] = value.Byte2; _Buffer[Length + 3] = value.Byte3; _Buffer[Length + 4] = value.Byte4; _Buffer[Length + 5] = value.Byte5; _Buffer[Length + 6] = value.Byte6; _Buffer[Length + 7] = value.Byte7; _Buffer[Length + 8] = value.Byte8; _Buffer[Length + 9] = value.Byte9; _Buffer[Length + 10] = value.Byte10; _Buffer[Length + 11] = value.Byte11; _Buffer[Length + 12] = value.Byte12; _Buffer[Length + 13] = value.Byte13; _Buffer[Length + 14] = value.Byte14; _Buffer[Length + 15] = value.Byte15; Length += 16; }
public static DateTime ExtractDateTimeUtc(this Guid g) { var guidStruct = new GuidStruct { Value = g }; var longStruct = new LongStruct { B2 = guidStruct.BF, B3 = guidStruct.BE, B4 = guidStruct.BD, B5 = guidStruct.BC, B6 = guidStruct.BB, B7 = (byte)(guidStruct.BA & 0x0F) }; return new DateTime(longStruct.Value + UuidStartTicks, DateTimeKind.Utc); }// ExtractDateTimeUtc()
}// ExtractDateTimeUtc() #if Experimental /// <summary> /// Returns SMALLEST guid with an approximate utc timestamp. Useful for time-based database range searches. /// </summary> public static Guid LowerGuidForDateTimeUtc(DateTime utc) { var longStruct = new LongStruct { Value = utc.Ticks - UuidStartTicks }; var guidStruct = new GuidStruct { BA = (byte)(longStruct.B7 & 0x0F), BB = longStruct.B6, BC = longStruct.B5, BD = longStruct.B4, BE = longStruct.B3, BF = longStruct.B2 }; return guidStruct.Value; }// LowerGuidForDateTimeUtc()
public static Guid NewSequentialGuid() { var guidStruct = new GuidStruct { Value = Guid.NewGuid() }; var uuidStruct = new GuidStruct { Value = GetSequentialUuid() }; return new Guid( a: guidStruct.IntValue0123, b: guidStruct.ShortValue45, c: guidStruct.ShortValueAB, d: uuidStruct.B1, e: uuidStruct.B0, f: uuidStruct.B7, g: uuidStruct.B6, h: uuidStruct.B5, i: uuidStruct.B4, j: uuidStruct.B3, k: uuidStruct.B2); }// NewSequentialGuid()
}// LowerGuidForDateTimeUtc() /// <summary> /// Returns LARGEST guid with an approximate utc timestamp. Useful for time-based database range searches. /// </summary> public static Guid UpperGuidForDateTimeUtc(DateTime utc) { var longStruct = new LongStruct { Value = utc.Ticks - UuidStartTicks }; var guidStruct = new GuidStruct { IntValue0123 = -1, // 0xFF_FF_FF_FF ShortValue45 = -1, // 0xFF_FF B6 = 0xFF, B7 = 0xFF, B8 = 0xFF, B9 = 0xFF, BA = (byte)(longStruct.B7 | 0xF0), BB = longStruct.B6, BC = longStruct.B5, BD = longStruct.B4, BE = longStruct.B3, BF = longStruct.B2 }; return guidStruct.Value; }// UpperGuidForDateTimeUtc()
static Guid _ReadGuid(TextReader reader) { // 1314FAD4-7505-439D-ABD2-DBD89242928C // 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ // // Guid is guaranteed to be a 36 character string // Bytes are in a different order than you might expect // For: 35 91 8b c9 - 19 6d - 40 ea - 97 79 - 88 9d 79 b7 53 f0 // Get: C9 8B 91 35 6D 19 EA 40 97 79 88 9D 79 B7 53 F0 // Ix: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // // And we have to account for dashes // // So the map is like so: // chars[0] -> bytes[3] -> buffer[ 0, 1] // chars[1] -> bytes[2] -> buffer[ 2, 3] // chars[2] -> bytes[1] -> buffer[ 4, 5] // chars[3] -> bytes[0] -> buffer[ 6, 7] // chars[4] -> bytes[5] -> buffer[ 9,10] // chars[5] -> bytes[4] -> buffer[11,12] // chars[6] -> bytes[7] -> buffer[14,15] // chars[7] -> bytes[6] -> buffer[16,17] // chars[8] -> bytes[8] -> buffer[19,20] // chars[9] -> bytes[9] -> buffer[21,22] // chars[10] -> bytes[10] -> buffer[24,25] // chars[11] -> bytes[11] -> buffer[26,27] // chars[12] -> bytes[12] -> buffer[28,29] // chars[13] -> bytes[13] -> buffer[30,31] // chars[14] -> bytes[14] -> buffer[32,33] // chars[15] -> bytes[15] -> buffer[34,35] var asStruct = new GuidStruct(); asStruct.B03 = ReadGuidByte(reader); asStruct.B02 = ReadGuidByte(reader); asStruct.B01 = ReadGuidByte(reader); asStruct.B00 = ReadGuidByte(reader); if (reader.Read() != '-') { throw new DeserializationException("Expected -", reader); } asStruct.B05 = ReadGuidByte(reader); asStruct.B04 = ReadGuidByte(reader); if (reader.Read() != '-') { throw new DeserializationException("Expected -", reader); } asStruct.B07 = ReadGuidByte(reader); asStruct.B06 = ReadGuidByte(reader); if (reader.Read() != '-') { throw new DeserializationException("Expected -", reader); } asStruct.B08 = ReadGuidByte(reader); asStruct.B09 = ReadGuidByte(reader); if (reader.Read() != '-') { throw new DeserializationException("Expected -", reader); } asStruct.B10 = ReadGuidByte(reader); asStruct.B11 = ReadGuidByte(reader); asStruct.B12 = ReadGuidByte(reader); asStruct.B13 = ReadGuidByte(reader); asStruct.B14 = ReadGuidByte(reader); asStruct.B15 = ReadGuidByte(reader); return(asStruct.Value); }
internal static void WriteValue(Guid value, JsonSerializerHandler handler) { handler.Writer.Append("\""); char[] buffer = new char[36]; // 1314FAD4-7505-439D-ABD2-DBD89242928C // 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ // 8 4 4 4 12,36 char,64byte // guid -> int short short 8byte => 8byte 4byte 4byte 8byte // Guid is guaranteed to be a 36 character string // get all the dashes in place buffer[8] = '-'; buffer[13] = '-'; buffer[18] = '-'; buffer[23] = '-'; // Bytes are in a different order than you might expect // For: 35 91 8b c9 - 19 6d - 40 ea - 97 79 - 88 9d 79 b7 53 f0 // Get: C9 8B 91 35 6D 19 EA 40 97 79 88 9D 79 B7 53 F0 // Ix: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // // And we have to account for dashes // // So the map is like so: // bytes[0] -> chars[3] -> buffer[ 6, 7] // bytes[1] -> chars[2] -> buffer[ 4, 5] // bytes[2] -> chars[1] -> buffer[ 2, 3] // bytes[3] -> chars[0] -> buffer[ 0, 1] // bytes[4] -> chars[5] -> buffer[11,12] // bytes[5] -> chars[4] -> buffer[ 9,10] // bytes[6] -> chars[7] -> buffer[16,17] // bytes[7] -> chars[6] -> buffer[14,15] // bytes[8] -> chars[8] -> buffer[19,20] // bytes[9] -> chars[9] -> buffer[21,22] // bytes[10] -> chars[10] -> buffer[24,25] // bytes[11] -> chars[11] -> buffer[26,27] // bytes[12] -> chars[12] -> buffer[28,29] // bytes[13] -> chars[13] -> buffer[30,31] // bytes[14] -> chars[14] -> buffer[32,33] // bytes[15] -> chars[15] -> buffer[34,35] var visibleMembers = new GuidStruct(value); // bytes[0] var b = visibleMembers.B00 * 2; buffer[6] = WriteGuidLookup[b]; buffer[7] = WriteGuidLookup[b + 1]; // bytes[1] b = visibleMembers.B01 * 2; buffer[4] = WriteGuidLookup[b]; buffer[5] = WriteGuidLookup[b + 1]; // bytes[2] b = visibleMembers.B02 * 2; buffer[2] = WriteGuidLookup[b]; buffer[3] = WriteGuidLookup[b + 1]; // bytes[3] b = visibleMembers.B03 * 2; buffer[0] = WriteGuidLookup[b]; buffer[1] = WriteGuidLookup[b + 1]; // bytes[4] b = visibleMembers.B04 * 2; buffer[11] = WriteGuidLookup[b]; buffer[12] = WriteGuidLookup[b + 1]; // bytes[5] b = visibleMembers.B05 * 2; buffer[9] = WriteGuidLookup[b]; buffer[10] = WriteGuidLookup[b + 1]; // bytes[6] b = visibleMembers.B06 * 2; buffer[16] = WriteGuidLookup[b]; buffer[17] = WriteGuidLookup[b + 1]; // bytes[7] b = visibleMembers.B07 * 2; buffer[14] = WriteGuidLookup[b]; buffer[15] = WriteGuidLookup[b + 1]; // bytes[8] b = visibleMembers.B08 * 2; buffer[19] = WriteGuidLookup[b]; buffer[20] = WriteGuidLookup[b + 1]; // bytes[9] b = visibleMembers.B09 * 2; buffer[21] = WriteGuidLookup[b]; buffer[22] = WriteGuidLookup[b + 1]; // bytes[10] b = visibleMembers.B10 * 2; buffer[24] = WriteGuidLookup[b]; buffer[25] = WriteGuidLookup[b + 1]; // bytes[11] b = visibleMembers.B11 * 2; buffer[26] = WriteGuidLookup[b]; buffer[27] = WriteGuidLookup[b + 1]; // bytes[12] b = visibleMembers.B12 * 2; buffer[28] = WriteGuidLookup[b]; buffer[29] = WriteGuidLookup[b + 1]; // bytes[13] b = visibleMembers.B13 * 2; buffer[30] = WriteGuidLookup[b]; buffer[31] = WriteGuidLookup[b + 1]; // bytes[14] b = visibleMembers.B14 * 2; buffer[32] = WriteGuidLookup[b]; buffer[33] = WriteGuidLookup[b + 1]; // bytes[15] b = visibleMembers.B15 * 2; buffer[34] = WriteGuidLookup[b]; buffer[35] = WriteGuidLookup[b + 1]; handler.Writer.Append(buffer, 0, 36); handler.Writer.Append("\""); }
internal static Guid ReadGuid(JsonReader reader, JsonDeserializeHandler handler) { var c = reader.BeforAnnotation(); if (c == '"') { if (reader.Remaining > 36) { // 1314FAD4-7505-439D-ABD2-DBD89242928C // 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ // // Guid is guaranteed to be a 36 character string // Bytes are in a different order than you might expect // For: 35 91 8b c9 - 19 6d - 40 ea - 97 79 - 88 9d 79 b7 53 f0 // Get: C9 8B 91 35 6D 19 EA 40 97 79 88 9D 79 B7 53 F0 // Ix: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 var asStruct = new GuidStruct { B03 = ReadGuidByte(reader), B02 = ReadGuidByte(reader), B01 = ReadGuidByte(reader), B00 = ReadGuidByte(reader) }; c = reader.GetChar(); if (c != '-') { throw new JsonWrongCharacterException(reader, "Guid format error"); } asStruct.B05 = ReadGuidByte(reader); asStruct.B04 = ReadGuidByte(reader); c = reader.GetChar(); if (c != '-') { throw new JsonWrongCharacterException(reader, "Guid format error"); } asStruct.B07 = ReadGuidByte(reader); asStruct.B06 = ReadGuidByte(reader); c = reader.GetChar(); if (c != '-') { throw new JsonWrongCharacterException(reader, "Guid format error"); } asStruct.B08 = ReadGuidByte(reader); asStruct.B09 = ReadGuidByte(reader); c = reader.GetChar(); if (c != '-') { throw new JsonWrongCharacterException(reader, "Guid format error"); } asStruct.B10 = ReadGuidByte(reader); asStruct.B11 = ReadGuidByte(reader); asStruct.B12 = ReadGuidByte(reader); asStruct.B13 = ReadGuidByte(reader); asStruct.B14 = ReadGuidByte(reader); asStruct.B15 = ReadGuidByte(reader); if (reader.GetChar() == '"') { return(asStruct.Value); } } } throw new JsonDeserializationTypeResolutionException(reader, typeof(Guid)); }
public static string ToString(Guid guid, string format) { if (guid == null) { throw new ArgumentNullException("guid"); } if (format == null || format.Length == 0) { format = "d"; } if (format.Length != 1) { throw new FormatException(RS.InvalidGuidFormatChar); } string guidString; char formatChar = format[0]; if (formatChar == 'd' || formatChar == 'D') { // 1314FAD4-7505-439D-ABD2-DBD89242928C // 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ // // Guid is guaranteed to be a 36 character string guidString = new string('\0', 36); unsafe { fixed(char *guidChars = guidString) { // get all the consts in place guidChars[8] = '-'; guidChars[13] = '-'; guidChars[18] = '-'; guidChars[23] = '-'; // Bytes are in a different order than you might expect // For: 35 91 8b c9 - 19 6d - 40 ea - 97 79 - 88 9d 79 b7 53 f0 // Get: C9 8B 91 35 6D 19 EA 40 97 79 88 9D 79 B7 53 F0 // Ix: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // // And we have to account for dashes // // So the map is like so: // bytes[0] -> chars[3] -> buffer[ 6, 7] // bytes[1] -> chars[2] -> buffer[ 4, 5] // bytes[2] -> chars[1] -> buffer[ 2, 3] // bytes[3] -> chars[0] -> buffer[ 0, 1] // bytes[4] -> chars[5] -> buffer[11,12] // bytes[5] -> chars[4] -> buffer[ 9,10] // bytes[6] -> chars[7] -> buffer[16,17] // bytes[7] -> chars[6] -> buffer[14,15] // bytes[8] -> chars[8] -> buffer[19,20] // bytes[9] -> chars[9] -> buffer[21,22] // bytes[10] -> chars[10] -> buffer[24,25] // bytes[11] -> chars[11] -> buffer[26,27] // bytes[12] -> chars[12] -> buffer[28,29] // bytes[13] -> chars[13] -> buffer[30,31] // bytes[14] -> chars[14] -> buffer[32,33] // bytes[15] -> chars[15] -> buffer[34,35] GuidStruct visibleMembers = new GuidStruct(guid); // bytes[0] var b = visibleMembers.B00 * 2; guidChars[6] = WriteGuidLookup[b]; guidChars[7] = WriteGuidLookup[b + 1]; // bytes[1] b = visibleMembers.B01 * 2; guidChars[4] = WriteGuidLookup[b]; guidChars[5] = WriteGuidLookup[b + 1]; // bytes[2] b = visibleMembers.B02 * 2; guidChars[2] = WriteGuidLookup[b]; guidChars[3] = WriteGuidLookup[b + 1]; // bytes[3] b = visibleMembers.B03 * 2; guidChars[0] = WriteGuidLookup[b]; guidChars[1] = WriteGuidLookup[b + 1]; // bytes[4] b = visibleMembers.B04 * 2; guidChars[11] = WriteGuidLookup[b]; guidChars[12] = WriteGuidLookup[b + 1]; // bytes[5] b = visibleMembers.B05 * 2; guidChars[9] = WriteGuidLookup[b]; guidChars[10] = WriteGuidLookup[b + 1]; // bytes[6] b = visibleMembers.B06 * 2; guidChars[16] = WriteGuidLookup[b]; guidChars[17] = WriteGuidLookup[b + 1]; // bytes[7] b = visibleMembers.B07 * 2; guidChars[14] = WriteGuidLookup[b]; guidChars[15] = WriteGuidLookup[b + 1]; // bytes[8] b = visibleMembers.B08 * 2; guidChars[19] = WriteGuidLookup[b]; guidChars[20] = WriteGuidLookup[b + 1]; // bytes[9] b = visibleMembers.B09 * 2; guidChars[21] = WriteGuidLookup[b]; guidChars[22] = WriteGuidLookup[b + 1]; // bytes[10] b = visibleMembers.B10 * 2; guidChars[24] = WriteGuidLookup[b]; guidChars[25] = WriteGuidLookup[b + 1]; // bytes[11] b = visibleMembers.B11 * 2; guidChars[26] = WriteGuidLookup[b]; guidChars[27] = WriteGuidLookup[b + 1]; // bytes[12] b = visibleMembers.B12 * 2; guidChars[28] = WriteGuidLookup[b]; guidChars[29] = WriteGuidLookup[b + 1]; // bytes[13] b = visibleMembers.B13 * 2; guidChars[30] = WriteGuidLookup[b]; guidChars[31] = WriteGuidLookup[b + 1]; // bytes[14] b = visibleMembers.B14 * 2; guidChars[32] = WriteGuidLookup[b]; guidChars[33] = WriteGuidLookup[b + 1]; // bytes[15] b = visibleMembers.B15 * 2; guidChars[34] = WriteGuidLookup[b]; guidChars[35] = WriteGuidLookup[b + 1]; } } } else if (formatChar == 'n' || formatChar == 'N') { // 1314FAD47505439DABD2DBD89242928C // 0123456789ABCDEFGHIJKLMNOPQRSTUV // // Guid is guaranteed to be a 32 character string guidString = new string('\0', 32); unsafe { fixed(char *guidChars = guidString) { GuidStruct visibleMembers = new GuidStruct(guid); // bytes[0] var b = visibleMembers.B00 * 2; guidChars[6] = WriteGuidLookup[b]; guidChars[7] = WriteGuidLookup[b + 1]; // bytes[1] b = visibleMembers.B01 * 2; guidChars[4] = WriteGuidLookup[b]; guidChars[5] = WriteGuidLookup[b + 1]; // bytes[2] b = visibleMembers.B02 * 2; guidChars[2] = WriteGuidLookup[b]; guidChars[3] = WriteGuidLookup[b + 1]; // bytes[3] b = visibleMembers.B03 * 2; guidChars[0] = WriteGuidLookup[b]; guidChars[1] = WriteGuidLookup[b + 1]; // bytes[4] b = visibleMembers.B04 * 2; guidChars[10] = WriteGuidLookup[b]; guidChars[11] = WriteGuidLookup[b + 1]; // bytes[5] b = visibleMembers.B05 * 2; guidChars[8] = WriteGuidLookup[b]; guidChars[9] = WriteGuidLookup[b + 1]; // bytes[6] b = visibleMembers.B06 * 2; guidChars[14] = WriteGuidLookup[b]; guidChars[15] = WriteGuidLookup[b + 1]; // bytes[7] b = visibleMembers.B07 * 2; guidChars[12] = WriteGuidLookup[b]; guidChars[13] = WriteGuidLookup[b + 1]; // bytes[8] b = visibleMembers.B08 * 2; guidChars[16] = WriteGuidLookup[b]; guidChars[17] = WriteGuidLookup[b + 1]; // bytes[9] b = visibleMembers.B09 * 2; guidChars[18] = WriteGuidLookup[b]; guidChars[19] = WriteGuidLookup[b + 1]; // bytes[10] b = visibleMembers.B10 * 2; guidChars[20] = WriteGuidLookup[b]; guidChars[21] = WriteGuidLookup[b + 1]; // bytes[11] b = visibleMembers.B11 * 2; guidChars[22] = WriteGuidLookup[b]; guidChars[23] = WriteGuidLookup[b + 1]; // bytes[12] b = visibleMembers.B12 * 2; guidChars[24] = WriteGuidLookup[b]; guidChars[25] = WriteGuidLookup[b + 1]; // bytes[13] b = visibleMembers.B13 * 2; guidChars[26] = WriteGuidLookup[b]; guidChars[27] = WriteGuidLookup[b + 1]; // bytes[14] b = visibleMembers.B14 * 2; guidChars[28] = WriteGuidLookup[b]; guidChars[29] = WriteGuidLookup[b + 1]; // bytes[15] b = visibleMembers.B15 * 2; guidChars[30] = WriteGuidLookup[b]; guidChars[31] = WriteGuidLookup[b + 1]; } } } else if (formatChar == 'b' || formatChar == 'B') { // {1314FAD4-7505-439D-ABD2-DBD89242928C} // 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZab // // Guid is guaranteed to be a 38 character string guidString = new string('\0', 38); unsafe { fixed(char *guidChars = guidString) { // get all the consts in place guidChars[0] = '{'; guidChars[9] = '-'; guidChars[14] = '-'; guidChars[19] = '-'; guidChars[24] = '-'; guidChars[37] = '}'; GuidStruct visibleMembers = new GuidStruct(guid); // bytes[0] var b = visibleMembers.B00 * 2; guidChars[7] = WriteGuidLookup[b]; guidChars[8] = WriteGuidLookup[b + 1]; // bytes[1] b = visibleMembers.B01 * 2; guidChars[5] = WriteGuidLookup[b]; guidChars[6] = WriteGuidLookup[b + 1]; // bytes[2] b = visibleMembers.B02 * 2; guidChars[3] = WriteGuidLookup[b]; guidChars[4] = WriteGuidLookup[b + 1]; // bytes[3] b = visibleMembers.B03 * 2; guidChars[1] = WriteGuidLookup[b]; guidChars[2] = WriteGuidLookup[b + 1]; // bytes[4] b = visibleMembers.B04 * 2; guidChars[12] = WriteGuidLookup[b]; guidChars[13] = WriteGuidLookup[b + 1]; // bytes[5] b = visibleMembers.B05 * 2; guidChars[10] = WriteGuidLookup[b]; guidChars[11] = WriteGuidLookup[b + 1]; // bytes[6] b = visibleMembers.B06 * 2; guidChars[17] = WriteGuidLookup[b]; guidChars[18] = WriteGuidLookup[b + 1]; // bytes[7] b = visibleMembers.B07 * 2; guidChars[15] = WriteGuidLookup[b]; guidChars[16] = WriteGuidLookup[b + 1]; // bytes[8] b = visibleMembers.B08 * 2; guidChars[20] = WriteGuidLookup[b]; guidChars[21] = WriteGuidLookup[b + 1]; // bytes[9] b = visibleMembers.B09 * 2; guidChars[22] = WriteGuidLookup[b]; guidChars[23] = WriteGuidLookup[b + 1]; // bytes[10] b = visibleMembers.B10 * 2; guidChars[25] = WriteGuidLookup[b]; guidChars[26] = WriteGuidLookup[b + 1]; // bytes[11] b = visibleMembers.B11 * 2; guidChars[27] = WriteGuidLookup[b]; guidChars[28] = WriteGuidLookup[b + 1]; // bytes[12] b = visibleMembers.B12 * 2; guidChars[29] = WriteGuidLookup[b]; guidChars[30] = WriteGuidLookup[b + 1]; // bytes[13] b = visibleMembers.B13 * 2; guidChars[31] = WriteGuidLookup[b]; guidChars[32] = WriteGuidLookup[b + 1]; // bytes[14] b = visibleMembers.B14 * 2; guidChars[33] = WriteGuidLookup[b]; guidChars[34] = WriteGuidLookup[b + 1]; // bytes[15] b = visibleMembers.B15 * 2; guidChars[35] = WriteGuidLookup[b]; guidChars[36] = WriteGuidLookup[b + 1]; } } } else if (formatChar == 'p' || formatChar == 'P') { // (1314FAD4-7505-439D-ABD2-DBD89242928C) // 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZab // // Guid is guaranteed to be a 38 character string guidString = new string('\0', 38); unsafe { fixed(char *guidChars = guidString) { // get all the consts in place guidChars[0] = '('; guidChars[9] = '-'; guidChars[14] = '-'; guidChars[19] = '-'; guidChars[24] = '-'; guidChars[37] = ')'; GuidStruct visibleMembers = new GuidStruct(guid); // bytes[0] var b = visibleMembers.B00 * 2; guidChars[7] = WriteGuidLookup[b]; guidChars[8] = WriteGuidLookup[b + 1]; // bytes[1] b = visibleMembers.B01 * 2; guidChars[5] = WriteGuidLookup[b]; guidChars[6] = WriteGuidLookup[b + 1]; // bytes[2] b = visibleMembers.B02 * 2; guidChars[3] = WriteGuidLookup[b]; guidChars[4] = WriteGuidLookup[b + 1]; // bytes[3] b = visibleMembers.B03 * 2; guidChars[1] = WriteGuidLookup[b]; guidChars[2] = WriteGuidLookup[b + 1]; // bytes[4] b = visibleMembers.B04 * 2; guidChars[12] = WriteGuidLookup[b]; guidChars[13] = WriteGuidLookup[b + 1]; // bytes[5] b = visibleMembers.B05 * 2; guidChars[10] = WriteGuidLookup[b]; guidChars[11] = WriteGuidLookup[b + 1]; // bytes[6] b = visibleMembers.B06 * 2; guidChars[17] = WriteGuidLookup[b]; guidChars[18] = WriteGuidLookup[b + 1]; // bytes[7] b = visibleMembers.B07 * 2; guidChars[15] = WriteGuidLookup[b]; guidChars[16] = WriteGuidLookup[b + 1]; // bytes[8] b = visibleMembers.B08 * 2; guidChars[20] = WriteGuidLookup[b]; guidChars[21] = WriteGuidLookup[b + 1]; // bytes[9] b = visibleMembers.B09 * 2; guidChars[22] = WriteGuidLookup[b]; guidChars[23] = WriteGuidLookup[b + 1]; // bytes[10] b = visibleMembers.B10 * 2; guidChars[25] = WriteGuidLookup[b]; guidChars[26] = WriteGuidLookup[b + 1]; // bytes[11] b = visibleMembers.B11 * 2; guidChars[27] = WriteGuidLookup[b]; guidChars[28] = WriteGuidLookup[b + 1]; // bytes[12] b = visibleMembers.B12 * 2; guidChars[29] = WriteGuidLookup[b]; guidChars[30] = WriteGuidLookup[b + 1]; // bytes[13] b = visibleMembers.B13 * 2; guidChars[31] = WriteGuidLookup[b]; guidChars[32] = WriteGuidLookup[b + 1]; // bytes[14] b = visibleMembers.B14 * 2; guidChars[33] = WriteGuidLookup[b]; guidChars[34] = WriteGuidLookup[b + 1]; // bytes[15] b = visibleMembers.B15 * 2; guidChars[35] = WriteGuidLookup[b]; guidChars[36] = WriteGuidLookup[b + 1]; } } } else if (formatChar == 'x' || formatChar == 'X') { // {0x409cb578,0x5dfc,0x436c,{0xb0,0xaa,0x95,0x56,0x18,0x69,0x0e,0x38}} // 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#@$*<> // // Guid is guaranteed to be a 68 character string guidString = new string('\0', 68); unsafe { fixed(char *guidChars = guidString) { // get all the consts in place guidChars[0] = '{'; guidChars[1] = '0'; guidChars[2] = 'x'; guidChars[11] = ','; guidChars[12] = '0'; guidChars[13] = 'x'; guidChars[18] = ','; guidChars[19] = '0'; guidChars[20] = 'x'; guidChars[25] = ','; guidChars[26] = '{'; guidChars[27] = '0'; guidChars[28] = 'x'; guidChars[31] = ','; guidChars[32] = '0'; guidChars[33] = 'x'; guidChars[36] = ','; guidChars[37] = '0'; guidChars[38] = 'x'; guidChars[41] = ','; guidChars[42] = '0'; guidChars[43] = 'x'; guidChars[46] = ','; guidChars[47] = '0'; guidChars[48] = 'x'; guidChars[51] = ','; guidChars[52] = '0'; guidChars[53] = 'x'; guidChars[56] = ','; guidChars[57] = '0'; guidChars[58] = 'x'; guidChars[61] = ','; guidChars[62] = '0'; guidChars[63] = 'x'; guidChars[66] = '}'; guidChars[67] = '}'; GuidStruct visibleMembers = new GuidStruct(guid); // bytes[0] var b = visibleMembers.B00 * 2; guidChars[9] = WriteGuidLookup[b]; guidChars[10] = WriteGuidLookup[b + 1]; // bytes[1] b = visibleMembers.B01 * 2; guidChars[7] = WriteGuidLookup[b]; guidChars[8] = WriteGuidLookup[b + 1]; // bytes[2] b = visibleMembers.B02 * 2; guidChars[5] = WriteGuidLookup[b]; guidChars[6] = WriteGuidLookup[b + 1]; // bytes[3] b = visibleMembers.B03 * 2; guidChars[3] = WriteGuidLookup[b]; guidChars[4] = WriteGuidLookup[b + 1]; // bytes[4] b = visibleMembers.B04 * 2; guidChars[16] = WriteGuidLookup[b]; guidChars[17] = WriteGuidLookup[b + 1]; // bytes[5] b = visibleMembers.B05 * 2; guidChars[14] = WriteGuidLookup[b]; guidChars[15] = WriteGuidLookup[b + 1]; // bytes[6] b = visibleMembers.B06 * 2; guidChars[23] = WriteGuidLookup[b]; guidChars[24] = WriteGuidLookup[b + 1]; // bytes[7] b = visibleMembers.B07 * 2; guidChars[21] = WriteGuidLookup[b]; guidChars[22] = WriteGuidLookup[b + 1]; // bytes[8] b = visibleMembers.B08 * 2; guidChars[29] = WriteGuidLookup[b]; guidChars[30] = WriteGuidLookup[b + 1]; // bytes[9] b = visibleMembers.B09 * 2; guidChars[34] = WriteGuidLookup[b]; guidChars[35] = WriteGuidLookup[b + 1]; // bytes[10] b = visibleMembers.B10 * 2; guidChars[39] = WriteGuidLookup[b]; guidChars[40] = WriteGuidLookup[b + 1]; // bytes[11] b = visibleMembers.B11 * 2; guidChars[44] = WriteGuidLookup[b]; guidChars[45] = WriteGuidLookup[b + 1]; // bytes[12] b = visibleMembers.B12 * 2; guidChars[49] = WriteGuidLookup[b]; guidChars[50] = WriteGuidLookup[b + 1]; // bytes[13] b = visibleMembers.B13 * 2; guidChars[54] = WriteGuidLookup[b]; guidChars[55] = WriteGuidLookup[b + 1]; // bytes[14] b = visibleMembers.B14 * 2; guidChars[59] = WriteGuidLookup[b]; guidChars[60] = WriteGuidLookup[b + 1]; // bytes[15] b = visibleMembers.B15 * 2; guidChars[64] = WriteGuidLookup[b]; guidChars[65] = WriteGuidLookup[b + 1]; } } } else { throw new FormatException(RS.InvalidGuidFormatChar); } return(guidString); }
static void _WriteGuid(TextWriter writer, Guid guid, char[] buffer) { // 1314FAD4-7505-439D-ABD2-DBD89242928C // 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ // // Guid is guaranteed to be a 36 character string // get all the dashes in place buffer[8] = '-'; buffer[13] = '-'; buffer[18] = '-'; buffer[23] = '-'; // Bytes are in a different order than you might expect // For: 35 91 8b c9 - 19 6d - 40 ea - 97 79 - 88 9d 79 b7 53 f0 // Get: C9 8B 91 35 6D 19 EA 40 97 79 88 9D 79 B7 53 F0 // Ix: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // // And we have to account for dashes // // So the map is like so: // bytes[0] -> chars[3] -> buffer[ 6, 7] // bytes[1] -> chars[2] -> buffer[ 4, 5] // bytes[2] -> chars[1] -> buffer[ 2, 3] // bytes[3] -> chars[0] -> buffer[ 0, 1] // bytes[4] -> chars[5] -> buffer[11,12] // bytes[5] -> chars[4] -> buffer[ 9,10] // bytes[6] -> chars[7] -> buffer[16,17] // bytes[7] -> chars[6] -> buffer[14,15] // bytes[8] -> chars[8] -> buffer[19,20] // bytes[9] -> chars[9] -> buffer[21,22] // bytes[10] -> chars[10] -> buffer[24,25] // bytes[11] -> chars[11] -> buffer[26,27] // bytes[12] -> chars[12] -> buffer[28,29] // bytes[13] -> chars[13] -> buffer[30,31] // bytes[14] -> chars[14] -> buffer[32,33] // bytes[15] -> chars[15] -> buffer[34,35] var visibleMembers = new GuidStruct(guid); // bytes[0] var b = visibleMembers.B00 * 2; buffer[6] = WriteGuidLookup[b]; buffer[7] = WriteGuidLookup[b + 1]; // bytes[1] b = visibleMembers.B01 * 2; buffer[4] = WriteGuidLookup[b]; buffer[5] = WriteGuidLookup[b + 1]; // bytes[2] b = visibleMembers.B02 * 2; buffer[2] = WriteGuidLookup[b]; buffer[3] = WriteGuidLookup[b + 1]; // bytes[3] b = visibleMembers.B03 * 2; buffer[0] = WriteGuidLookup[b]; buffer[1] = WriteGuidLookup[b + 1]; // bytes[4] b = visibleMembers.B04 * 2; buffer[11] = WriteGuidLookup[b]; buffer[12] = WriteGuidLookup[b + 1]; // bytes[5] b = visibleMembers.B05 * 2; buffer[9] = WriteGuidLookup[b]; buffer[10] = WriteGuidLookup[b + 1]; // bytes[6] b = visibleMembers.B06 * 2; buffer[16] = WriteGuidLookup[b]; buffer[17] = WriteGuidLookup[b + 1]; // bytes[7] b = visibleMembers.B07 * 2; buffer[14] = WriteGuidLookup[b]; buffer[15] = WriteGuidLookup[b + 1]; // bytes[8] b = visibleMembers.B08 * 2; buffer[19] = WriteGuidLookup[b]; buffer[20] = WriteGuidLookup[b + 1]; // bytes[9] b = visibleMembers.B09 * 2; buffer[21] = WriteGuidLookup[b]; buffer[22] = WriteGuidLookup[b + 1]; // bytes[10] b = visibleMembers.B10 * 2; buffer[24] = WriteGuidLookup[b]; buffer[25] = WriteGuidLookup[b + 1]; // bytes[11] b = visibleMembers.B11 * 2; buffer[26] = WriteGuidLookup[b]; buffer[27] = WriteGuidLookup[b + 1]; // bytes[12] b = visibleMembers.B12 * 2; buffer[28] = WriteGuidLookup[b]; buffer[29] = WriteGuidLookup[b + 1]; // bytes[13] b = visibleMembers.B13 * 2; buffer[30] = WriteGuidLookup[b]; buffer[31] = WriteGuidLookup[b + 1]; // bytes[14] b = visibleMembers.B14 * 2; buffer[32] = WriteGuidLookup[b]; buffer[33] = WriteGuidLookup[b + 1]; // bytes[15] b = visibleMembers.B15 * 2; buffer[34] = WriteGuidLookup[b]; buffer[35] = WriteGuidLookup[b + 1]; writer.Write(buffer, 0, 36); }
public override void Read(IOMemoryStream ms, UAssetFile f) { //Is we're in an array, this will **ALWAYS** be a prop list struct. UStruct st; if (isArray) { structType = "(array)"; unknown = 0; st = new PropListStruct(); f.Debug("Read Array", $"====READ STRUCT BEGIN @ {ms.position} ({structType}, {unknown})====", ConsoleColor.Yellow); } else { structType = ms.ReadNameTableEntry(f); unknown = ms.ReadInt(); f.Debug("Read Array", $"====READ STRUCT BEGIN @ {ms.position} ({structType}, {unknown})====", ConsoleColor.Yellow); //Find the struct type. Unknown if real: ItemStatInfo if (structType == "ItemStatInfo" || structType == "ItemNetID" || structType == "ItemNetInfo" || structType == "Transform" || structType == "PrimalPlayerDataStruct" || structType == "PrimalPlayerCharacterConfigStruct" || structType == "PrimalPersistentCharacterStatsStruct" || structType == "TribeData" || structType == "TribeGovernment" || structType == "TerrainInfo" || structType == "ArkInventoryData" || structType == "DinoOrderGroup" || structType == "ARKDinoData") { //Open this as a struct property list. st = new PropListStruct(); } else if (structType == "Vector" || structType == "Rotator") { //3d vector or rotor st = new Vector3Struct(); } else if (structType == "Vector2D") { //2d vector st = new Vector2Struct(); } else if (structType == "Quat") { //Quat st = new QuatStruct(); } else if (structType == "Color") { //Color st = new ColorStruct(); } else if (structType == "LinearColor") { //Linear color st = new LinearColorStruct(); } else if (structType == "UniqueNetIdRepl") { //Some net stuff st = new UniqueNetIdStruct(); } else if (structType == "Guid") { //Some net stuff st = new GuidStruct(); } else if (structType == "IntPoint") { //Some net stuff st = new IntPointStruct(); } else if (structType == "StringAssetReference") { st = new StringAssetReferenceStruct(); } else { //Interpet this as a struct property list. Maybe raise a warning later? f.Warn("Struct Warning", $"Unknown type '{structType}'. Interpeting as a struct property list..."); st = new PropListStruct(); } } //Read st.ReadStruct(ms, f, this); data = st; f.Debug("Read Struct", $"====READ STRUCT END @ {ms.position}====", ConsoleColor.Yellow); }
public static Guid NewSequentialGuid() => GuidStruct.NewSequentialGuid();
public static unsafe void ToString(Guid value, CharStreamPlus jsonStream) { var guid = new GuidStruct {Value = value}; jsonStream.PrepLength(38); var data = (byte*) jsonStream.CurrentChar; int high = guid.Byte3 >> 4, low = guid.Byte3 & 15; *(char*) data = Quote; *(char*) (data + sizeof (char)) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1))); *(char*) (data + sizeof (char)*2) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1))); high = guid.Byte2 >> 4; low = guid.Byte2 & 15; *(char*) (data + sizeof (char)*3) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1))); *(char*) (data + sizeof (char)*4) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1))); high = guid.Byte1 >> 4; low = guid.Byte1 & 15; *(char*) (data + sizeof (char)*5) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1))); *(char*) (data + sizeof (char)*6) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1))); high = guid.Byte0 >> 4; low = guid.Byte0 & 15; *(char*) (data + sizeof (char)*7) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1))); *(char*) (data + sizeof (char)*8) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1))); *(char*) (data + sizeof (char)*9) = '-'; high = guid.Byte5 >> 4; low = guid.Byte5 & 15; *(char*) (data + sizeof (char)*10) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1))); *(char*) (data + sizeof (char)*11) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1))); high = guid.Byte4 >> 4; low = guid.Byte4 & 15; *(char*) (data + sizeof (char)*12) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1))); *(char*) (data + sizeof (char)*13) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1))); *(char*) (data + sizeof (char)*14) = '-'; high = guid.Byte7 >> 4; low = guid.Byte7 & 15; *(char*) (data + sizeof (char)*15) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1))); *(char*) (data + sizeof (char)*16) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1))); high = guid.Byte6 >> 4; low = guid.Byte6 & 15; *(char*) (data + sizeof (char)*17) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1))); *(char*) (data + sizeof (char)*18) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1))); *(char*) (data + sizeof (char)*19) = '-'; high = guid.Byte8 >> 4; low = guid.Byte8 & 15; *(char*) (data + sizeof (char)*20) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1))); *(char*) (data + sizeof (char)*21) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1))); high = guid.Byte9 >> 4; low = guid.Byte9 & 15; *(char*) (data + sizeof (char)*22) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1))); *(char*) (data + sizeof (char)*23) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1))); *(char*) (data + sizeof (char)*24) = '-'; high = guid.Byte10 >> 4; low = guid.Byte10 & 15; *(char*) (data + sizeof (char)*25) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1))); *(char*) (data + sizeof (char)*26) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1))); high = guid.Byte11 >> 4; low = guid.Byte11 & 15; *(char*) (data + sizeof (char)*27) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1))); *(char*) (data + sizeof (char)*28) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1))); high = guid.Byte12 >> 4; low = guid.Byte12 & 15; *(char*) (data + sizeof (char)*29) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1))); *(char*) (data + sizeof (char)*30) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1))); high = guid.Byte13 >> 4; low = guid.Byte13 & 15; *(char*) (data + sizeof (char)*31) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1))); *(char*) (data + sizeof (char)*32) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1))); high = guid.Byte14 >> 4; low = guid.Byte14 & 15; *(char*) (data + sizeof (char)*33) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1))); *(char*) (data + sizeof (char)*34) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1))); high = guid.Byte15 >> 4; low = guid.Byte15 & 15; *(char*) (data + sizeof (char)*35) = (char) (high < 10 ? high + '0' : (high + ('0' + 'A' - '9' - 1))); *(char*) (data + sizeof (char)*36) = (char) (low < 10 ? low + '0' : (low + ('0' + 'A' - '9' - 1))); *(char*) (data + sizeof (char)*37) = Quote; jsonStream.Unsafer.AddLength(38); }