BugReportApi.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. 
  2. using System.IO;
  3. using System.Text;
  4. using UnityEngine.Networking;
  5. #if NETFX_CORE
  6. using UnityEngine.Windows;
  7. #endif
  8. namespace SRDebugger.Internal
  9. {
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using Services;
  14. using SRF;
  15. using UnityEngine;
  16. public class BugReportApi
  17. {
  18. private readonly string _apiKey;
  19. private readonly BugReport _bugReport;
  20. private bool _isBusy;
  21. private UnityWebRequest _webRequest;
  22. public BugReportApi(BugReport report, string apiKey)
  23. {
  24. _bugReport = report;
  25. _apiKey = apiKey;
  26. }
  27. public bool IsComplete { get; private set; }
  28. public bool WasSuccessful { get; private set; }
  29. public string ErrorMessage { get; private set; }
  30. public float Progress
  31. {
  32. get
  33. {
  34. if (_webRequest == null)
  35. {
  36. return 0;
  37. }
  38. return Mathf.Clamp01(_webRequest.uploadProgress);
  39. }
  40. }
  41. public IEnumerator Submit()
  42. {
  43. //Debug.Log("[BugReportApi] Submit()");
  44. if (_isBusy)
  45. {
  46. throw new InvalidOperationException("BugReportApi is already sending a bug report");
  47. }
  48. // Reset state
  49. _isBusy = true;
  50. ErrorMessage = "";
  51. IsComplete = false;
  52. WasSuccessful = false;
  53. _webRequest = null;
  54. string json;
  55. byte[] jsonBytes;
  56. try
  57. {
  58. json = BuildJsonRequest(_bugReport);
  59. jsonBytes = Encoding.UTF8.GetBytes(json);
  60. }
  61. catch (Exception e)
  62. {
  63. ErrorMessage = "Error building bug report.";
  64. Debug.LogError(e);
  65. SetCompletionState(false);
  66. yield break;
  67. }
  68. try
  69. {
  70. const string jsonContentType = "application/json";
  71. _webRequest = new UnityWebRequest(SRDebugApi.BugReportEndPoint, UnityWebRequest.kHttpVerbPOST,
  72. new DownloadHandlerBuffer(), new UploadHandlerRaw(jsonBytes)
  73. {
  74. contentType = jsonContentType
  75. });
  76. _webRequest.SetRequestHeader("Accept", jsonContentType);
  77. _webRequest.SetRequestHeader("X-ApiKey", _apiKey);
  78. }
  79. catch (Exception e)
  80. {
  81. ErrorMessage = "Error building bug report request.";
  82. Debug.LogError(e);
  83. if (_webRequest != null)
  84. {
  85. _webRequest.Dispose();
  86. }
  87. SetCompletionState(false);
  88. }
  89. if (_webRequest == null)
  90. {
  91. SetCompletionState(false);
  92. yield break;
  93. }
  94. #if !UNITY_2017_2_OR_NEWER
  95. yield return _webRequest.Send();
  96. #else
  97. yield return _webRequest.SendWebRequest();
  98. #endif
  99. #if !UNITY_2017_1_OR_NEWER
  100. if(_webRequest.isError)
  101. #else
  102. if (_webRequest.isNetworkError)
  103. #endif
  104. {
  105. ErrorMessage = "Request Error: " + _webRequest.error;
  106. SetCompletionState(false);
  107. _webRequest.Dispose();
  108. yield break;
  109. }
  110. long responseCode = _webRequest.responseCode;
  111. var responseJson = _webRequest.downloadHandler.text;
  112. _webRequest.Dispose();
  113. if (responseCode != 200)
  114. {
  115. ErrorMessage = "Server: " + SRDebugApiUtil.ParseErrorResponse(responseJson, "Unknown response from server");
  116. SetCompletionState(false);
  117. yield break;
  118. }
  119. SetCompletionState(true);
  120. }
  121. private void SetCompletionState(bool wasSuccessful)
  122. {
  123. _bugReport.ScreenshotData = null;
  124. WasSuccessful = wasSuccessful;
  125. IsComplete = true;
  126. _isBusy = false;
  127. if (!wasSuccessful)
  128. {
  129. Debug.LogError("Bug Reporter Error: " + ErrorMessage);
  130. }
  131. }
  132. private static string BuildJsonRequest(BugReport report)
  133. {
  134. var ht = new Hashtable();
  135. ht.Add("userEmail", report.Email);
  136. ht.Add("userDescription", report.UserDescription);
  137. ht.Add("console", CreateConsoleDump());
  138. ht.Add("systemInformation", report.SystemInformation);
  139. if (report.ScreenshotData != null)
  140. {
  141. ht.Add("screenshot", Convert.ToBase64String(report.ScreenshotData));
  142. }
  143. var json = Json.Serialize(ht);
  144. return json;
  145. }
  146. private static IList<IList<string>> CreateConsoleDump()
  147. {
  148. var list = new List<IList<string>>();
  149. var consoleLog = Service.Console.AllEntries;
  150. foreach (var consoleEntry in consoleLog)
  151. {
  152. var entry = new List<string>();
  153. entry.Add(consoleEntry.LogType.ToString());
  154. entry.Add(consoleEntry.Message);
  155. entry.Add(consoleEntry.StackTrace);
  156. if (consoleEntry.Count > 1)
  157. {
  158. entry.Add(consoleEntry.Count.ToString());
  159. }
  160. list.Add(entry);
  161. }
  162. return list;
  163. }
  164. }
  165. }