BugReportApi.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using SRDebugger.Services;
  6. using SRF;
  7. using UnityEngine;
  8. namespace SRDebugger.Internal
  9. {
  10. public class BugReportApi
  11. {
  12. public BugReportApi(BugReport report, string apiKey)
  13. {
  14. this._bugReport = report;
  15. this._apiKey = apiKey;
  16. }
  17. public bool IsComplete { get; private set; }
  18. public bool WasSuccessful { get; private set; }
  19. public string ErrorMessage { get; private set; }
  20. public float Progress
  21. {
  22. get
  23. {
  24. if (this._www == null)
  25. {
  26. return 0f;
  27. }
  28. return Mathf.Clamp01(this._www.progress + this._www.uploadProgress);
  29. }
  30. }
  31. public IEnumerator Submit()
  32. {
  33. if (this._isBusy)
  34. {
  35. throw new InvalidOperationException("BugReportApi is already sending a bug report");
  36. }
  37. this._isBusy = true;
  38. this.ErrorMessage = string.Empty;
  39. this.IsComplete = false;
  40. this.WasSuccessful = false;
  41. this._www = null;
  42. try
  43. {
  44. string s = BugReportApi.BuildJsonRequest(this._bugReport);
  45. byte[] bytes = Encoding.UTF8.GetBytes(s);
  46. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  47. dictionary["Content-Type"] = "application/json";
  48. dictionary["Accept"] = "application/json";
  49. dictionary["Method"] = "POST";
  50. dictionary["X-ApiKey"] = this._apiKey;
  51. this._www = new WWW("http://srdebugger.stompyrobot.uk/report/submit", bytes, dictionary);
  52. }
  53. catch (Exception ex)
  54. {
  55. this.ErrorMessage = ex.Message;
  56. }
  57. if (this._www == null)
  58. {
  59. this.SetCompletionState(false);
  60. yield break;
  61. }
  62. yield return this._www;
  63. if (!string.IsNullOrEmpty(this._www.error))
  64. {
  65. this.ErrorMessage = this._www.error;
  66. this.SetCompletionState(false);
  67. yield break;
  68. }
  69. if (!this._www.responseHeaders.ContainsKey("X-STATUS"))
  70. {
  71. this.ErrorMessage = "Completion State Unknown";
  72. this.SetCompletionState(false);
  73. yield break;
  74. }
  75. string status = this._www.responseHeaders["X-STATUS"];
  76. if (!status.Contains("200"))
  77. {
  78. this.ErrorMessage = SRDebugApiUtil.ParseErrorResponse(this._www.text, status);
  79. this.SetCompletionState(false);
  80. yield break;
  81. }
  82. this.SetCompletionState(true);
  83. yield break;
  84. }
  85. private void SetCompletionState(bool wasSuccessful)
  86. {
  87. this._bugReport.ScreenshotData = null;
  88. this.WasSuccessful = wasSuccessful;
  89. this.IsComplete = true;
  90. this._isBusy = false;
  91. if (!wasSuccessful)
  92. {
  93. UnityEngine.Debug.LogError("Bug Reporter Error: " + this.ErrorMessage);
  94. }
  95. }
  96. private static string BuildJsonRequest(BugReport report)
  97. {
  98. Hashtable hashtable = new Hashtable();
  99. hashtable.Add("userEmail", report.Email);
  100. hashtable.Add("userDescription", report.UserDescription);
  101. hashtable.Add("console", BugReportApi.CreateConsoleDump());
  102. hashtable.Add("systemInformation", report.SystemInformation);
  103. if (report.ScreenshotData != null)
  104. {
  105. hashtable.Add("screenshot", Convert.ToBase64String(report.ScreenshotData));
  106. }
  107. return Json.Serialize(hashtable);
  108. }
  109. private static IList<IList<string>> CreateConsoleDump()
  110. {
  111. List<IList<string>> list = new List<IList<string>>();
  112. IReadOnlyList<ConsoleEntry> entries = Service.Console.Entries;
  113. foreach (ConsoleEntry consoleEntry in entries)
  114. {
  115. List<string> list2 = new List<string>();
  116. list2.Add(consoleEntry.LogType.ToString());
  117. list2.Add(consoleEntry.Message);
  118. list2.Add(consoleEntry.StackTrace);
  119. if (consoleEntry.Count > 1)
  120. {
  121. list2.Add(consoleEntry.Count.ToString());
  122. }
  123. list.Add(list2);
  124. }
  125. return list;
  126. }
  127. private readonly string _apiKey;
  128. private readonly BugReport _bugReport;
  129. private bool _isBusy;
  130. private WWW _www;
  131. }
  132. }