FirebaseRoot.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System.Security.Cryptography.X509Certificates;
  2. using System.Net.Security;
  3. using System.Net;
  4. using System.Collections;
  5. using UnityEngine;
  6. namespace SimpleFirebaseUnity
  7. {
  8. #if UNITY_EDITOR
  9. using EditorCoroutines;
  10. #endif
  11. internal class FirebaseRoot : FirebaseDatabase
  12. {
  13. protected static bool firstTimeInitiated = true;
  14. protected string host;
  15. protected string cred;
  16. /// <summary>
  17. /// Returns .json endpoint to this Firebase point
  18. /// </summary>
  19. public override string Endpoint
  20. {
  21. get
  22. {
  23. return "https://" + root.Host + "/.json";
  24. }
  25. }
  26. /// <summary>
  27. /// Credential for auth parameter
  28. /// </summary>
  29. public override string Credential
  30. {
  31. get
  32. {
  33. return cred;
  34. }
  35. set
  36. {
  37. cred = value;
  38. }
  39. }
  40. /// <summary>
  41. /// Returns .json endpoint to Firebase Rules.
  42. /// </summary>
  43. public override string RulesEndpoint
  44. {
  45. get
  46. {
  47. return "https://" + root.Host + "/.settings/rules.json";
  48. }
  49. }
  50. /// <summary>
  51. /// Copy this instance.
  52. /// </summary>
  53. public FirebaseRoot Copy()
  54. {
  55. return new FirebaseRoot (host, cred);
  56. }
  57. /// <summary>
  58. /// Initializes a new instance of the <see cref="SimpleFirebaseUnity.FirebaseRoot"/> class.
  59. /// </summary>
  60. /// <param name="_host">Host.</param>
  61. /// <param name="_cred">Cred.</param>
  62. public FirebaseRoot(string _host, string _cred = "")
  63. {
  64. if (firstTimeInitiated)
  65. {
  66. ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidationCallback;
  67. firstTimeInitiated = false;
  68. }
  69. root = this;
  70. host = _host;
  71. cred = _cred;
  72. }
  73. /// <summary>
  74. /// Returns main host of Firebase
  75. /// </summary>
  76. public override string Host
  77. {
  78. get
  79. {
  80. return host;
  81. }
  82. }
  83. private static bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
  84. {
  85. return true; // override certificate, we trust Firebase :D
  86. }
  87. /// <summary>
  88. /// Starts the coroutine.
  89. /// </summary>
  90. /// <param name="routine">Routine.</param>
  91. public void StartCoroutine(IEnumerator routine)
  92. {
  93. if (!Application.isPlaying)
  94. {
  95. #if UNITY_EDITOR
  96. EditorCoroutines.StartCoroutine(routine, this);
  97. #endif
  98. }
  99. else
  100. {
  101. FirebaseManager.Instance.StartCoroutine(routine);
  102. }
  103. }
  104. /// <summary>
  105. /// Stops the coroutine.
  106. /// </summary>
  107. /// <param name="routine">Routine.</param>
  108. public void StopCoroutine(IEnumerator routine)
  109. {
  110. if (!Application.isPlaying)
  111. {
  112. #if UNITY_EDITOR
  113. EditorCoroutines.StartCoroutine(routine, this);
  114. #endif
  115. }
  116. else
  117. {
  118. FirebaseManager.Instance.StopCoroutine(routine);
  119. }
  120. }
  121. }
  122. }