Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace PacketAnalyzer
- {
- public partial class frmMain : Form
- {
- public frmMain()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- using (var ms = new System.IO.MemoryStream(HexStringToBinary(textBox1.Text)))
- using (var br = new System.IO.BinaryReader(ms))
- {
- //int i = 0;
- //while (br.BaseStream.Position != br.BaseStream.Length)
- //{
- // br.BaseStream.Position = i;
- // i++;
- // var timestamp = popGUID(br);// br.ReadUInt32();//br.ReadUInt16();//br.ReadByte(); //GetDateTimeFromGameTime( br.ReadInt32() ); //GetDateTimeFromUnixTime(br.ReadUInt32());
- // System.Diagnostics.Debug.WriteLine(string.Format("{0} {1}", i, timestamp));
- //}
- br.BaseStream.Position = 0;
- System.Diagnostics.Debug.WriteLine(string.Format("opcode: {0}", br.ReadUInt16()));
- System.Diagnostics.Debug.WriteLine(string.Format("pageid: {0}", br.ReadInt32()));
- System.Diagnostics.Debug.WriteLine(string.Format("guid: {0}", new PacketAnalyzer.Guid(br.ReadUInt64())));
- }
- }
- public static UInt64 popGUID(System.IO.BinaryReader br)
- {
- byte mask = br.ReadByte();
- if (mask == 0) { return 0; }
- byte tmpMask = 1;
- byte[] tBytes = new byte[8];
- for (byte i = 0; i < 8; i++)
- {
- if ((mask & tmpMask) > 0)
- {
- byte tmp = br.ReadByte();
- tBytes[i] = tmp;
- }
- tmpMask *= 2;
- }
- return BitConverter.ToUInt64(tBytes, 0);
- }
- private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
- public static DateTime GetDateTimeFromGameTime(int packedDate)
- {
- var minute = packedDate & 0x3F;
- var hour = (packedDate >> 6) & 0x1F;
- // var weekDay = (packedDate >> 11) & 7;
- var day = (packedDate >> 14) & 0x3F;
- var month = (packedDate >> 20) & 0xF;
- var year = (packedDate >> 24) & 0x1F;
- // var something2 = (packedDate >> 29) & 3; always 0
- return new DateTime(2000, 1, 1).AddYears(year).AddMonths(month).AddDays(day).AddHours(hour).AddMinutes(minute);
- }
- public static DateTime GetDateTimeFromUnixTime(double unixTime)
- {
- return Epoch.AddSeconds(unixTime);
- }
- public static byte[] HexStringToBinary(string data)
- {
- data = data.Replace(" ", string.Empty);
- var bytes = new List<byte>();
- byte result;
- for (var i = 0; i < data.Length; i += 2)
- if (Byte.TryParse(data.Substring(i, 2), NumberStyles.HexNumber, null, out result))
- bytes.Add(result);
- return bytes.ToArray();
- }
- public static DateTime GetDateTimeFromUnixTime(int unixTime)
- {
- return new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(unixTime);
- }
- public static DateTime GetDateTimeFromUnixTime(long unixTime)
- {
- return new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(unixTime);
- }
- }
- public enum HighGuidType
- {
- None = -1,
- Player = 0x000, // Seen 0x280 for players too
- BattleGround1 = 0x101,
- InstanceSave = 0x104,
- Group = 0x105,
- BattleGround2 = 0x109,
- MOTransport = 0x10C,
- Guild = 0x10F,
- Item = 0x400, // Container
- DynObject = 0xF00, // Corpses
- GameObject = 0xF01,
- Transport = 0xF02,
- Unit = 0xF03,
- Pet = 0xF04,
- Vehicle = 0xF05,
- }
- public enum ObjectType
- {
- Object = 0,
- Item = 1,
- Container = 2,
- Unit = 3,
- Player = 4,
- GameObject = 5,
- DynamicObject = 6,
- Corpse = 7
- }
- public struct Guid
- {
- public readonly ulong Full;
- public Guid(ulong id)
- : this()
- {
- Full = id;
- }
- public bool HasEntry()
- {
- switch (GetHighType())
- {
- case HighGuidType.Unit:
- case HighGuidType.GameObject:
- case HighGuidType.Vehicle:
- return true;
- default:
- return false;
- }
- }
- public ulong GetLow()
- {
- switch (GetHighType())
- {
- case HighGuidType.Player:
- case HighGuidType.DynObject:
- case HighGuidType.Group:
- case HighGuidType.Item:
- return Full & 0x000FFFFFFFFFFFFF;
- case HighGuidType.GameObject:
- case HighGuidType.Transport:
- case HighGuidType.MOTransport:
- case HighGuidType.Vehicle:
- case HighGuidType.Unit:
- case HighGuidType.Pet:
- return Full & 0x0000000000FFFFFF;
- }
- // TODO: check if entryless guids dont use now more bytes
- return Full & 0x00000000FFFFFFFF;
- }
- public uint GetEntry()
- {
- if (!HasEntry())
- return 0;
- return (uint)((Full & 0x000FFFFFFF000000) >> 24);
- }
- public HighGuidType GetHighType()
- {
- if (Full == 0)
- return HighGuidType.None;
- var highGUID = (HighGuidType)((Full & 0xF0F0000000000000) >> 52);
- return highGUID == 0 ? HighGuidType.Player : highGUID;
- }
- public ObjectType GetObjectType()
- {
- switch (GetHighType())
- {
- case HighGuidType.Player:
- return ObjectType.Player;
- case HighGuidType.DynObject:
- return ObjectType.DynamicObject;
- case HighGuidType.Item:
- return ObjectType.Item;
- case HighGuidType.GameObject:
- case HighGuidType.Transport:
- case HighGuidType.MOTransport:
- return ObjectType.GameObject;
- case HighGuidType.Vehicle:
- case HighGuidType.Unit:
- case HighGuidType.Pet:
- return ObjectType.Unit;
- default:
- return ObjectType.Object;
- }
- }
- public static bool operator ==(Guid first, Guid other)
- {
- return first.Full == other.Full;
- }
- public static bool operator !=(Guid first, Guid other)
- {
- return !(first == other);
- }
- public override bool Equals(object obj)
- {
- return obj != null && obj is Guid && Equals((Guid)obj);
- }
- public bool Equals(Guid other)
- {
- return other.Full == Full;
- }
- public override int GetHashCode()
- {
- return Full.GetHashCode();
- }
- public override string ToString()
- {
- if (Full == 0)
- return "0x0";
- // If our guid has an entry and it is an unit or a GO, print its
- // name next to the entry (from a database, if enabled)
- if (HasEntry())
- {
- return "Full: 0x" + Full.ToString("X8") + " Type: " + GetHighType()
- + " Entry: " + (int)GetEntry() + " Low: " + GetLow();
- }
- return "Full: 0x" + Full.ToString("X8") + " Type: " + GetHighType()
- + " Low: " + GetLow();
- }
- }
- }
Add Comment
Please, Sign In to add comment