PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/GearFoundry/Snippets/Util.cs

https://github.com/AlincoRecoders/GearFoundry
C# | 363 lines | 307 code | 55 blank | 1 comment | 48 complexity | bcf4a4a55d75b4e1f5193f4d231dd997 MD5 | raw file
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Xml;
  6. using System.Xml.Serialization;
  7. using System.Diagnostics;
  8. using System.Net;
  9. using Decal;
  10. using Decal.Adapter;
  11. using Decal.Interop.D3DService;
  12. using Decal.Interop;
  13. using System.Media;
  14. using System.Text;
  15. using VirindiViewService;
  16. using System.Resources;
  17. using System.Drawing;
  18. using System.ComponentModel;
  19. using System.Xml.Linq;
  20. using System.Linq;
  21. using System.Reflection;
  22. using System.Windows.Forms;
  23. using System.Threading;
  24. using ProtoBuf;
  25. using GearData;
  26. using GearEnums;
  27. namespace GearFoundry
  28. {
  29. public partial class PluginCore
  30. {
  31. public const int ERRORLINK_ID = 221113;
  32. public static string docPath = string.Empty;
  33. public static string dllversion = string.Empty;
  34. public static string wavPath = string.Empty;
  35. public static int TotalErrors;
  36. public static void LogError(Exception ex)
  37. {
  38. try
  39. {
  40. FileInfo ErrorLog = new FileInfo(GearFiles.GearDir + GearFiles.PluginName + "errors.txt");
  41. if(ErrorLog.Exists)
  42. {
  43. if(ErrorLog.Length > 1000000) {ErrorLog.Delete();}
  44. }
  45. using (StreamWriter writer = new StreamWriter(GearFiles.GearDir + GearFiles.PluginName + "errors.txt", true))
  46. {
  47. writer.WriteLine("============================================================================");
  48. writer.WriteLine(DateTime.Now.ToString());
  49. writer.WriteLine("Error: " + ex.Message);
  50. writer.WriteLine("Source: " + ex.Source);
  51. writer.WriteLine("Stack: " + ex.StackTrace);
  52. if (ex.InnerException != null)
  53. {
  54. writer.WriteLine("Inner: " + ex.InnerException.Message);
  55. writer.WriteLine("Inner Stack: " + ex.InnerException.StackTrace);
  56. }
  57. writer.WriteLine("============================================================================");
  58. writer.WriteLine("");
  59. writer.Close();
  60. }
  61. }
  62. catch
  63. {
  64. }
  65. }
  66. public static void ListItems(int i, int id, String Item)
  67. {
  68. try
  69. {
  70. FileInfo ListItems = new FileInfo(GearFiles.GearDir + GearFiles.PluginName + "listitems.txt");
  71. if (ListItems.Exists)
  72. {
  73. if (ListItems.Length > 1000000) { ListItems.Delete(); }
  74. }
  75. using (StreamWriter writer = new StreamWriter(GearFiles.GearDir + GearFiles.PluginName + "listitems.txt", true))
  76. {
  77. writer.WriteLine("============================================================================");
  78. writer.WriteLine(DateTime.Now.ToString());
  79. writer.WriteLine("index: " + i.ToString() + "; Item: " + Item + "; ID: " + id.ToString());
  80. writer.Close();
  81. }
  82. }
  83. catch
  84. {
  85. }
  86. }
  87. public static void WriteToChat(string message)
  88. {
  89. try
  90. {
  91. CoreManager.Current.Actions.AddChatText("#" + GearFiles.PluginName + "#: " + message, 2);
  92. }
  93. catch (Exception ex) { LogError(ex); }
  94. }
  95. public static void HudToChat(string report, int color)
  96. {
  97. try
  98. {
  99. CoreManager.Current.Actions.AddChatText(report, color);
  100. }
  101. catch (Exception ex) { LogError(ex); }
  102. }
  103. public static void ReportStringToChat(string report)
  104. {
  105. try
  106. {
  107. CoreManager.Current.Actions.AddChatText(report, 2);
  108. }
  109. catch (Exception ex) { LogError(ex); }
  110. }
  111. //Used to read embedded .xml files
  112. public string GetResourceTextFile(string filename)
  113. {
  114. string result = string.Empty;
  115. using (Stream stream = this.GetType().Assembly.GetManifestResourceStream(filename))
  116. {
  117. using (StreamReader sr = new StreamReader(stream))
  118. {
  119. result = sr.ReadToEnd();
  120. }
  121. }
  122. return result;
  123. }
  124. public DataTable GetDataTableResource()
  125. {
  126. try
  127. {
  128. using(Stream stream = this.GetType().Assembly.GetManifestResourceStream("DataPack.pb"))
  129. {
  130. Serializer.PrepareSerializer<DataTable>();
  131. return Serializer.Deserialize<DataTable>(stream);
  132. }
  133. }catch(Exception ex){LogError(ex); return new DataTable();}
  134. }
  135. DateTime ArrowKill = DateTime.MinValue;
  136. private void ArrowInitiator()
  137. {
  138. ArrowKill = DateTime.Now;
  139. Core.RenderFrame += new EventHandler<EventArgs>(FireTheArrow);
  140. }
  141. void FireTheArrow(object sender, EventArgs e)
  142. {
  143. if ((DateTime.Now - ArrowKill).TotalSeconds > 3)
  144. {
  145. Core.RenderFrame -= FireTheArrow;
  146. }
  147. else
  148. {
  149. DoShowArrow();
  150. }
  151. }
  152. private void DoShowArrow()
  153. {
  154. Decal.Adapter.Wrappers.D3DObj mMarkObject;
  155. mMarkObject = Core.D3DService.PointToObject(GearFiles.ArrowTargetId, (unchecked((int)0xFFBB0000)));
  156. }
  157. private List<int> _ChangeAppliesToFlagToIntList(int AppliesToFlag)
  158. {
  159. try
  160. {
  161. List<int> AppliesToList = new List<int>();
  162. foreach(int item in tDataTable.AppliesToList.Select(x => x.ID))
  163. {
  164. if((item & AppliesToFlag) != 0){ AppliesToList.Add(item);}
  165. }
  166. return AppliesToList;
  167. }catch(Exception ex){LogError(ex); return new List<int>();}
  168. }
  169. private List<int> _ChangeDamageFlagToIntList(int DamageFlag)
  170. {
  171. try
  172. {
  173. List<int> DamageList = new List<int>();
  174. foreach(int item in tDataTable.ElementalList.Select(x => x.ID))
  175. {
  176. if((item & DamageFlag) != 0){ DamageList.Add(item);}
  177. }
  178. return DamageList;
  179. }catch(Exception ex){LogError(ex); return new List<int>();}
  180. }
  181. private List<int> _ChangeSlotsFlagToIntList(int SlotsFlag)
  182. {
  183. try
  184. {
  185. List<int> SlotsList = new List<int>();
  186. foreach(int item in tDataTable.SlotList.Select(x => x.ID))
  187. {
  188. if((item & SlotsFlag) != 0){ SlotsList.Add(item);}
  189. }
  190. return SlotsList;
  191. }catch(Exception ex){LogError(ex); return new List<int>();}
  192. }
  193. private List<int> _ConvertCommaStringToIntList(string BaseString)
  194. {
  195. try
  196. {
  197. List<int> Ids = new List<int>();
  198. if(BaseString != String.Empty && BaseString != "")
  199. {
  200. string[] SplitString = BaseString.Split(',');
  201. foreach(string str in SplitString)
  202. {
  203. Ids.Add(Convert.ToInt32(str));
  204. }
  205. }
  206. return Ids;
  207. }catch(Exception ex){LogError(ex); return new List<int>();}
  208. }
  209. private string _ConvertIntListToCommaString(List<int> BaseList)
  210. {
  211. try
  212. {
  213. string result = String.Empty;
  214. if(BaseList.Count != 0)
  215. {
  216. BaseList.Sort();
  217. for(int i = 0; i < BaseList.Count; i++)
  218. {
  219. result += BaseList[i].ToString();
  220. if(i != BaseList.Count -1) {result += ",";}
  221. }
  222. }
  223. return result;
  224. }catch(Exception ex){LogError(ex); return String.Empty;}
  225. }
  226. private List<advsettings> _ConvertAdvStringToAdvanced(string BaseString)
  227. {
  228. try
  229. {
  230. List<advsettings> Advanced = new List<advsettings>();
  231. if(BaseString != String.Empty && BaseString != "")
  232. {
  233. if(BaseString.StartsWith("false"))
  234. {
  235. return Advanced;
  236. }
  237. string[] SplitString = BaseString.Split(',');
  238. List<string> SplitList = new List<string>();
  239. foreach(string str in SplitString)
  240. {
  241. SplitList.Add(str);
  242. }
  243. SplitList.RemoveAt(0);
  244. foreach(string str in SplitList)
  245. {
  246. string[] splitcolons = str.Split(':');
  247. advsettings adv = new advsettings();
  248. if(splitcolons[0] == "Long") {adv.keytype = 1;}
  249. else{adv.keytype = 0;}
  250. adv.key = Convert.ToInt32(splitcolons[1]);
  251. if(splitcolons[2] == "Equals") {adv.keycompare = 0;}
  252. else if(splitcolons[2] == "Not Equals") {adv.keycompare = 1;}
  253. else if(splitcolons[2] == "Equals or Greater") {adv.keycompare = 2;}
  254. else if(splitcolons[2] == "Equals or Less") {adv.keycompare = 3;}
  255. adv.keyvalue = Convert.ToDouble(splitcolons[3]);
  256. if(splitcolons[4] == "Or") {adv.keylink = 2;}
  257. else if(splitcolons[4] == "And") {adv.keylink = 1;}
  258. else {adv.keylink = 0;}
  259. Advanced.Add(adv);
  260. }
  261. }
  262. return Advanced;
  263. }catch(Exception ex){LogError(ex); return new List<advsettings>();}
  264. }
  265. public string CoordsStringLink(string inputcoords)
  266. {
  267. return " (" + "<Tell:IIDString:" + InteropCodes.GOARROWLINK_ID + ":" + inputcoords + ">" + inputcoords + "<\\Tell>" + ")";
  268. }
  269. private ACImage CreateIconFromResource(string pngname)
  270. {
  271. try
  272. {
  273. Stream imagestream = this.GetType().Assembly.GetManifestResourceStream(pngname);
  274. Image tempimage = new Bitmap(imagestream);
  275. return (ACImage)tempimage;
  276. }
  277. catch (Exception ex) { LogError(ex); return new ACImage(Color.Black);}
  278. }
  279. private ItemRule CreateNewRule()
  280. {
  281. List<int> NumList = new List<int>();
  282. foreach(ItemRule r in mSearchListHolder.ItemRulesHash)
  283. {
  284. NumList.Add(r.RuleNumber);
  285. }
  286. int NewRuleNumber = 0;
  287. for(NewRuleNumber = 0; NewRuleNumber < NumList.Count; )
  288. {
  289. if(!NumList.Contains(NewRuleNumber)){break;}
  290. else{NewRuleNumber++;}
  291. }
  292. ItemRule nRule = new ItemRule();
  293. nRule.RuleNumber = NewRuleNumber;
  294. return nRule;
  295. }
  296. private static void LogToFile(string nput)
  297. {
  298. try
  299. {
  300. using (StreamWriter writer = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\Decal Plugins\GearFoundry\Operation.log", true))
  301. {
  302. writer.WriteLine(nput);
  303. writer.Close();
  304. }
  305. }catch(Exception ex){LogError(ex);}
  306. }
  307. }
  308. }