Advertisement
Guest User

Gw2MappingLink

a guest
Mar 1st, 2014
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.04 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.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Runtime.InteropServices;
  11. using System.IO.MemoryMappedFiles;
  12. using System.Threading;
  13. using System.Net;
  14. using System.IO;
  15. using Newtonsoft.Json;
  16. using Newtonsoft.Json.Linq;
  17.  
  18. namespace Gw2Mapping
  19. {
  20. public partial class Form1 : Form
  21. {
  22. public Form1()
  23. {
  24. InitializeComponent();
  25. }
  26.  
  27. private void Form1_Load(object sender, EventArgs e)
  28. {
  29. backgroundWorker1.RunWorkerAsync();
  30. }
  31. public unsafe struct LinkedMem
  32. {
  33. public uint uiVersion;
  34. public uint uiTick;
  35. public fixed float fAvatarPosition[3];
  36. public fixed float fAvatarFront[3];
  37. public fixed float fAvatarTop[3];
  38. public fixed byte name[512];
  39. public fixed float fCameraPosition[3];
  40. public fixed float fCameraFront[3];
  41. public fixed float fCameraTop[3];
  42. public fixed byte identity[512];
  43. public uint context_len;
  44. public fixed byte context[512];
  45. public fixed byte description[4096];
  46. };
  47.  
  48. public class PlayerInfo
  49. {
  50. public float x, y, z = 0;
  51. public int map = 0;
  52. public string identity = "";
  53. public double camRotationX = 0;
  54. public double camRotationY = 0;
  55. public double playerRotationX = 0;
  56. public double playerRotationY = 0;
  57. }
  58.  
  59. // Number of inches per meter
  60. const float InchesPerMeter = 39.37010F;
  61.  
  62. //
  63. static MemoryMappedFile mappedFile;
  64. static MemoryMappedViewAccessor accessor;
  65. static LinkedMem data = new LinkedMem();
  66. static PlayerInfo playerInfo = new PlayerInfo();
  67.  
  68. public static void OpenMumbleLink()
  69. {
  70. // Open the mapped memory file
  71. mappedFile = MemoryMappedFile.CreateOrOpen("MumbleLink", Marshal.SizeOf(data));
  72. accessor = mappedFile.CreateViewAccessor(0, Marshal.SizeOf(data));
  73. }
  74.  
  75. public static void GetData()
  76. {
  77. // Make sure the map memory file is loaded
  78. if (mappedFile == null) OpenMumbleLink();
  79.  
  80. // Read mapped memory file
  81. accessor.Read(0, out data);
  82.  
  83. unsafe
  84. {
  85. fixed (LinkedMem* _data = &data)
  86. {
  87. // Parse info
  88. playerInfo.x = (float)(_data->fAvatarPosition[0]) * InchesPerMeter;
  89. playerInfo.y = (float)(_data->fAvatarPosition[1]) * InchesPerMeter;
  90. playerInfo.z = (float)(_data->fAvatarPosition[2]) * InchesPerMeter;
  91. playerInfo.camRotationX = (double)(_data->fCameraFront[0]);
  92. playerInfo.camRotationY = (double)(_data->fCameraFront[2]);
  93. playerInfo.playerRotationX = (double)(_data->fAvatarFront[0]);
  94. playerInfo.playerRotationY = (double)(_data->fAvatarFront[2]);
  95. playerInfo.map = (int)_data->context[28] + ((int)_data->context[29] * 256);
  96.  
  97. }
  98. }
  99. }
  100.  
  101. void updateMap()
  102. {
  103. if (playerInfo.map != GlobalVars.currentMap)
  104. {
  105. //Get JSON data from gw2 map API
  106. string mapID = playerInfo.map.ToString();
  107. WebClient c = new WebClient();
  108. var data = c.DownloadString("https://api.guildwars2.com/v1/maps.json?map_id=" + mapID);
  109. JObject o = JObject.Parse(data);
  110. dynamic mapData = JsonConvert.DeserializeObject(data);
  111.  
  112. //Gets X,Y coordinates for map_rect and continent_rect
  113. GlobalVars.mLeft = mapData.maps[mapID].map_rect[0][0];
  114. GlobalVars.mTop = mapData.maps[mapID].map_rect[0][1];
  115. GlobalVars.mWidth = mapData.maps[mapID].map_rect[1][0] - mapData.maps[mapID].map_rect[0][0];
  116. GlobalVars.mHeight = mapData.maps[mapID].map_rect[1][1] - mapData.maps[mapID].map_rect[0][1];
  117. GlobalVars.cLeft = mapData.maps[mapID].continent_rect[0][0];
  118. GlobalVars.cTop = mapData.maps[mapID].continent_rect[0][1];
  119. GlobalVars.cWidth = mapData.maps[mapID].continent_rect[1][0] - mapData.maps[mapID].continent_rect[0][0];
  120. GlobalVars.cHeight = mapData.maps[mapID].continent_rect[1][1] - mapData.maps[mapID].continent_rect[0][1];
  121. GlobalVars.mapName = mapData.maps[mapID].map_name;
  122. }
  123.  
  124. //Creates the relative percent of the map for player X,Y
  125. float mapPctZero = ((playerInfo.x) - GlobalVars.mLeft) / GlobalVars.mWidth;
  126. float mapPctOne = ((playerInfo.z) - GlobalVars.mTop) / GlobalVars.mHeight;
  127.  
  128. GlobalVars.playerPosX = (GlobalVars.cLeft + (GlobalVars.cWidth * mapPctZero));
  129. GlobalVars.playerPosY = ((GlobalVars.cTop + GlobalVars.cHeight) - (GlobalVars.cHeight * mapPctOne));
  130. //Gets final continent coordinates for player position
  131. xLabel.Text = "X: " + GlobalVars.playerPosX.ToString();
  132. yLabel.Text = "Y: " + GlobalVars.playerPosY.ToString();
  133.  
  134. double camRadians = Math.Atan2(playerInfo.camRotationY, playerInfo.camRotationX);
  135. double camDegrees = (camRadians * (180 / Math.PI)) + 180 ;
  136. double playerRadians = Math.Atan2(playerInfo.playerRotationY, playerInfo.playerRotationX);
  137. double playerDegrees = (playerRadians * (180 / Math.PI)) + 180;
  138.  
  139. rotationLabel.Text = "Rotation: " + camDegrees.ToString();
  140. mapLabel.Text = "Map: " + GlobalVars.mapName;
  141.  
  142.  
  143. Coordinate coord = new Coordinate();
  144. coord.posX = GlobalVars.playerPosX.ToString();
  145. coord.posY = GlobalVars.playerPosY.ToString();
  146. coord.camRotation = camDegrees.ToString();
  147. coord.playerRotation = playerDegrees.ToString();
  148. coord.mapName = GlobalVars.mapName.ToString();
  149.  
  150. GlobalVars.json = JsonConvert.SerializeObject(coord);
  151. if (GlobalVars.startWeb != 1)
  152. {
  153. backgroundWorker2.RunWorkerAsync();
  154. GlobalVars.startWeb = 1;
  155. }
  156.  
  157.  
  158. }
  159. private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
  160. {
  161. updateMap();
  162. }
  163. private static class GlobalVars
  164. {
  165. public static int currentMap = 0;
  166. public static int mLeft;
  167. public static int mTop;
  168. public static int mWidth;
  169. public static int mHeight;
  170. public static int cLeft;
  171. public static int cTop;
  172. public static int cWidth;
  173. public static int cHeight;
  174. public static float playerPosX;
  175. public static float playerPosY;
  176. public static string json;
  177. public static string mapName = "";
  178. public static int startWeb = 0;
  179.  
  180.  
  181. }
  182. private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  183. {
  184. while (true)
  185. {
  186. System.Threading.Thread.Sleep(500);
  187. GetData();
  188. backgroundWorker1.ReportProgress(0, "Reporting in");
  189. }
  190. }
  191.  
  192. class Coordinate
  193. {
  194. public string posX { get; set; }
  195. public string posY { get; set; }
  196. public string camRotation { get; set; }
  197. public string playerRotation { get; set; }
  198. public string mapName { get; set; }
  199. }
  200.  
  201. private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
  202. {
  203.  
  204. HttpListener server = new HttpListener();
  205. server.Prefixes.Add("http://localhost:1337/");
  206.  
  207. server.Start();
  208.  
  209. Console.WriteLine("Listening...");
  210.  
  211. while (true)
  212. {
  213. HttpListenerContext context = server.GetContext();
  214. HttpListenerResponse response = context.Response;
  215. response.AppendHeader("Access-Control-Allow-Origin", "*");
  216.  
  217. string page = GlobalVars.json;
  218.  
  219. byte[] buffer = Encoding.UTF8.GetBytes(page);
  220. response.ContentLength64 = buffer.Length;
  221. Stream st = response.OutputStream;
  222. st.Write(buffer, 0, buffer.Length);
  223.  
  224. context.Response.Close();
  225.  
  226. }
  227.  
  228.  
  229. }
  230.  
  231. private void button1_Click(object sender, EventArgs e)
  232. {
  233. System.Diagnostics.Process.Start("http://mshelley.net/gw2");
  234. }
  235. }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement