Guest User

Untitled

a guest
Dec 14th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Globalization;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Windows.Forms;
  11.  
  12. namespace PacketAnalyzer
  13. {
  14.     public partial class frmMain : Form
  15.     {
  16.         public frmMain()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         private void button1_Click(object sender, EventArgs e)
  22.         {
  23.  
  24.             using (var ms = new System.IO.MemoryStream(HexStringToBinary(textBox1.Text)))
  25.             using (var br = new System.IO.BinaryReader(ms))
  26.             {
  27.                 //int i = 0;
  28.                 //while (br.BaseStream.Position != br.BaseStream.Length)
  29.                 //{
  30.                 //    br.BaseStream.Position = i;
  31.                 //    i++;
  32.                 //    var timestamp = popGUID(br);// br.ReadUInt32();//br.ReadUInt16();//br.ReadByte(); //GetDateTimeFromGameTime( br.ReadInt32()  ); //GetDateTimeFromUnixTime(br.ReadUInt32());
  33.                 //    System.Diagnostics.Debug.WriteLine(string.Format("{0} {1}", i, timestamp));
  34.                 //}
  35.  
  36.                 br.BaseStream.Position = 0;
  37.  
  38.                 System.Diagnostics.Debug.WriteLine(string.Format("opcode: {0}", br.ReadUInt16()));
  39.                 System.Diagnostics.Debug.WriteLine(string.Format("pageid: {0}", br.ReadInt32()));
  40.                 System.Diagnostics.Debug.WriteLine(string.Format("guid: {0}", new PacketAnalyzer.Guid(br.ReadUInt64())));
  41.             }
  42.  
  43.  
  44.         }
  45.  
  46.         public static UInt64 popGUID(System.IO.BinaryReader br)
  47.         {
  48.             byte mask = br.ReadByte();
  49.             if (mask == 0) { return 0; }
  50.             byte tmpMask = 1;
  51.             byte[] tBytes = new byte[8];
  52.  
  53.             for (byte i = 0; i < 8; i++)
  54.             {
  55.                 if ((mask & tmpMask) > 0)
  56.                 {
  57.                     byte tmp = br.ReadByte();
  58.                     tBytes[i] = tmp;
  59.                 }
  60.                 tmpMask *= 2;
  61.             }
  62.  
  63.  
  64.             return BitConverter.ToUInt64(tBytes, 0);
  65.         }
  66.  
  67.         private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  68.  
  69.         public static DateTime GetDateTimeFromGameTime(int packedDate)
  70.         {
  71.             var minute = packedDate & 0x3F;
  72.             var hour = (packedDate >> 6) & 0x1F;
  73.             // var weekDay = (packedDate >> 11) & 7;
  74.             var day = (packedDate >> 14) & 0x3F;
  75.             var month = (packedDate >> 20) & 0xF;
  76.             var year = (packedDate >> 24) & 0x1F;
  77.             // var something2 = (packedDate >> 29) & 3; always 0
  78.  
  79.             return new DateTime(2000, 1, 1).AddYears(year).AddMonths(month).AddDays(day).AddHours(hour).AddMinutes(minute);
  80.         }
  81.  
  82.         public static DateTime GetDateTimeFromUnixTime(double unixTime)
  83.         {
  84.             return Epoch.AddSeconds(unixTime);
  85.         }
  86.  
  87.         public static byte[] HexStringToBinary(string data)
  88.         {
  89.             data = data.Replace(" ", string.Empty);
  90.             var bytes = new List<byte>();
  91.             byte result;
  92.             for (var i = 0; i < data.Length; i += 2)
  93.                 if (Byte.TryParse(data.Substring(i, 2), NumberStyles.HexNumber, null, out result))
  94.                     bytes.Add(result);
  95.  
  96.             return bytes.ToArray();
  97.         }
  98.  
  99.  
  100.         public static DateTime GetDateTimeFromUnixTime(int unixTime)
  101.         {
  102.             return new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(unixTime);
  103.         }
  104.  
  105.         public static DateTime GetDateTimeFromUnixTime(long unixTime)
  106.         {
  107.             return new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(unixTime);
  108.         }
  109.     }
  110.  
  111.     public enum HighGuidType
  112.     {
  113.         None = -1,
  114.         Player = 0x000, // Seen 0x280 for players too
  115.         BattleGround1 = 0x101,
  116.         InstanceSave = 0x104,
  117.         Group = 0x105,
  118.         BattleGround2 = 0x109,
  119.         MOTransport = 0x10C,
  120.         Guild = 0x10F,
  121.         Item = 0x400, // Container
  122.         DynObject = 0xF00, // Corpses
  123.         GameObject = 0xF01,
  124.         Transport = 0xF02,
  125.         Unit = 0xF03,
  126.         Pet = 0xF04,
  127.         Vehicle = 0xF05,
  128.     }
  129.  
  130.     public enum ObjectType
  131.     {
  132.         Object = 0,
  133.         Item = 1,
  134.         Container = 2,
  135.         Unit = 3,
  136.         Player = 4,
  137.         GameObject = 5,
  138.         DynamicObject = 6,
  139.         Corpse = 7
  140.     }
  141.  
  142.     public struct Guid
  143.     {
  144.         public readonly ulong Full;
  145.  
  146.         public Guid(ulong id)
  147.             : this()
  148.         {
  149.             Full = id;
  150.         }
  151.  
  152.         public bool HasEntry()
  153.         {
  154.             switch (GetHighType())
  155.             {
  156.                 case HighGuidType.Unit:
  157.                 case HighGuidType.GameObject:
  158.                 case HighGuidType.Vehicle:
  159.                     return true;
  160.                 default:
  161.                     return false;
  162.             }
  163.         }
  164.  
  165.         public ulong GetLow()
  166.         {
  167.             switch (GetHighType())
  168.             {
  169.                 case HighGuidType.Player:
  170.                 case HighGuidType.DynObject:
  171.                 case HighGuidType.Group:
  172.                 case HighGuidType.Item:
  173.                     return Full & 0x000FFFFFFFFFFFFF;
  174.                 case HighGuidType.GameObject:
  175.                 case HighGuidType.Transport:
  176.                 case HighGuidType.MOTransport:
  177.                 case HighGuidType.Vehicle:
  178.                 case HighGuidType.Unit:
  179.                 case HighGuidType.Pet:
  180.  
  181.                     return Full & 0x0000000000FFFFFF;
  182.             }
  183.  
  184.             // TODO: check if entryless guids dont use now more bytes
  185.             return Full & 0x00000000FFFFFFFF;
  186.         }
  187.  
  188.         public uint GetEntry()
  189.         {
  190.             if (!HasEntry())
  191.                 return 0;
  192.  
  193.             return (uint)((Full & 0x000FFFFFFF000000) >> 24);
  194.         }
  195.  
  196.         public HighGuidType GetHighType()
  197.         {
  198.             if (Full == 0)
  199.                 return HighGuidType.None;
  200.  
  201.             var highGUID = (HighGuidType)((Full & 0xF0F0000000000000) >> 52);
  202.  
  203.             return highGUID == 0 ? HighGuidType.Player : highGUID;
  204.         }
  205.  
  206.         public ObjectType GetObjectType()
  207.         {
  208.             switch (GetHighType())
  209.             {
  210.                 case HighGuidType.Player:
  211.                     return ObjectType.Player;
  212.                 case HighGuidType.DynObject:
  213.                     return ObjectType.DynamicObject;
  214.                 case HighGuidType.Item:
  215.                     return ObjectType.Item;
  216.                 case HighGuidType.GameObject:
  217.                 case HighGuidType.Transport:
  218.                 case HighGuidType.MOTransport:
  219.                     return ObjectType.GameObject;
  220.                 case HighGuidType.Vehicle:
  221.                 case HighGuidType.Unit:
  222.                 case HighGuidType.Pet:
  223.                     return ObjectType.Unit;
  224.                 default:
  225.                     return ObjectType.Object;
  226.             }
  227.         }
  228.  
  229.         public static bool operator ==(Guid first, Guid other)
  230.         {
  231.             return first.Full == other.Full;
  232.         }
  233.  
  234.         public static bool operator !=(Guid first, Guid other)
  235.         {
  236.             return !(first == other);
  237.         }
  238.  
  239.         public override bool Equals(object obj)
  240.         {
  241.             return obj != null && obj is Guid && Equals((Guid)obj);
  242.         }
  243.  
  244.         public bool Equals(Guid other)
  245.         {
  246.             return other.Full == Full;
  247.         }
  248.  
  249.         public override int GetHashCode()
  250.         {
  251.             return Full.GetHashCode();
  252.         }
  253.  
  254.         public override string ToString()
  255.         {
  256.             if (Full == 0)
  257.                 return "0x0";
  258.  
  259.             // If our guid has an entry and it is an unit or a GO, print its
  260.             // name next to the entry (from a database, if enabled)
  261.             if (HasEntry())
  262.             {
  263.  
  264.                 return "Full: 0x" + Full.ToString("X8") + " Type: " + GetHighType()
  265.                     + " Entry: " + (int)GetEntry() + " Low: " + GetLow();
  266.             }
  267.  
  268.  
  269.             return "Full: 0x" + Full.ToString("X8") + " Type: " + GetHighType()
  270.                 + " Low: " + GetLow();
  271.         }
  272.     }
  273. }
Add Comment
Please, Sign In to add comment