Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Runtime.CompilerServices;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using Machina;
  13. using Machina.FFXIV;
  14.  
  15. namespace SubstatStuff
  16. {
  17. public class MachinaCaptureWorker
  18. {
  19. [Flags]
  20. public enum ConfigFlags
  21. {
  22. None = 0,
  23. StripHeaderActors = 1 << 0,
  24. DontUsePacketTimestamp = 1 << 1
  25. }
  26.  
  27. //private readonly XivMonTab _myTab;
  28. private readonly TCPNetworkMonitor.NetworkMonitorType _monitorType;
  29. private readonly ConfigFlags _configFlags;
  30.  
  31. private volatile bool _shouldStop;
  32.  
  33. public MachinaCaptureWorker(/*XivMonTab window,*/ TCPNetworkMonitor.NetworkMonitorType monitorType, ConfigFlags flags)
  34. {
  35. //this._myTab = window;
  36. this._monitorType = monitorType;
  37. this._configFlags = flags;
  38. }
  39.  
  40. private void MessageReceived(long epoch, byte[] message, int set)
  41. {
  42. var res = Parse(message);
  43.  
  44. /*PacketListItem item = new PacketListItem()
  45. {
  46. IsVisible = true,
  47. ActorControl = -1,
  48. Data = message,
  49. MessageCol = res.header.MessageType.ToString("X4"),
  50. DirectionCol = "S",
  51. CategoryCol = set.ToString(),
  52. TimeStampCol = Util.UnixTimeStampToDateTime(res.header.Seconds).ToString(@"MM\/dd\/yyyy HH:mm:ss"),
  53. SizeCol = res.header.MessageLength.ToString(),
  54. Set = set,
  55. RouteIdCol = res.header.RouteID.ToString(),
  56. PacketUnixTime = res.header.Seconds,
  57. SystemMsTime = Millis()
  58. };*/
  59.  
  60. if (_configFlags.HasFlag(ConfigFlags.DontUsePacketTimestamp))
  61. {
  62. item.TimeStampCol = DateTime.Now.ToString(@"MM\/dd\/yyyy HH:mm:ss.fff tt");
  63. }
  64.  
  65. //_myTab.Dispatcher.Invoke(new Action(() => { _myTab.AddPacketToListView(item); }));
  66. }
  67.  
  68. private void MessageSent(long epoch, byte[] message, int set)
  69. {
  70. var res = Parse(message);
  71.  
  72. /*PacketListItem item = new PacketListItem()
  73. {
  74. IsVisible = true,
  75. ActorControl = -1,
  76. Data = message,
  77. MessageCol = res.header.MessageType.ToString("X4"),
  78. DirectionCol = "C",
  79. CategoryCol = set.ToString(),
  80. TimeStampCol = Util.UnixTimeStampToDateTime(res.header.Seconds).ToString(@"MM\/dd\/yyyy HH:mm:ss"),
  81. SizeCol = res.header.MessageLength.ToString(),
  82. Set = set,
  83. RouteIdCol = res.header.RouteID.ToString(),
  84. PacketUnixTime = res.header.Seconds,
  85. SystemMsTime = Millis()
  86. };*/
  87.  
  88. if (_configFlags.HasFlag(ConfigFlags.DontUsePacketTimestamp))
  89. {
  90. item.TimeStampCol = DateTime.Now.ToString(@"MM\/dd\/yyyy HH:mm:ss.fff tt");
  91. }
  92.  
  93. //_myTab.Dispatcher.Invoke(new Action(() => { _myTab.AddPacketToListView(item); }));
  94. }
  95.  
  96. private static ParseResult Parse(byte[] data)
  97. {
  98. GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
  99. FFXIVMessageHeader head = (FFXIVMessageHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(FFXIVMessageHeader));
  100. handle.Free();
  101.  
  102. ParseResult result = new ParseResult();
  103. result.header = head;
  104. result.data = data;
  105.  
  106. return result;
  107. }
  108.  
  109. public void Run()
  110. {
  111. FFXIVNetworkMonitor monitor = new FFXIVNetworkMonitor();
  112. monitor.MonitorType = _monitorType;
  113. monitor.MessageReceived = (long epoch, byte[] message, int set) => MessageReceived(epoch, message, set);
  114. monitor.MessageSent = (long epoch, byte[] message, int set) => MessageSent(epoch, message, set);
  115. monitor.Start();
  116.  
  117. while (!_shouldStop)
  118. {
  119. // So don't burn the cpu while doing nothing
  120. Thread.Sleep(1);
  121. }
  122.  
  123. Console.WriteLine("MachinaCaptureWorker: Terminating");
  124. monitor.Stop();
  125. }
  126.  
  127. public void Stop()
  128. {
  129. _shouldStop = true;
  130. }
  131.  
  132. internal class ParseResult
  133. {
  134. public FFXIVMessageHeader header;
  135. public byte[] data;
  136. }
  137.  
  138. private long Millis()
  139. {
  140. return (long.MaxValue + DateTime.Now.ToBinary()) / 10000;
  141. }
  142. }
  143.  
  144. /*public class PacketListItem
  145. {
  146. public byte[] Data;
  147. public bool IsVisible { get; set; } = true;
  148. public int ActorControl { get; set; }
  149. public int Set { get; set; }
  150. public uint PacketUnixTime { get; set; }
  151. public long SystemMsTime { get; set; }
  152.  
  153. public string DirectionCol { get; set; }
  154. public string MessageCol { get; set; }
  155. public string NameCol { get; set; }
  156. public string RouteIdCol { get; set; }
  157. public string CommentCol { get; set; }
  158. public string SizeCol { get; set; }
  159. public string CategoryCol { get; set; }
  160. public string TimeStampCol { get; set; }
  161.  
  162. }*/
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement