DataCheatingDetector.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #define UNITY_5_4_PLUS
  2. #if UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_4_9 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2 || UNITY_5_3
  3. #undef UNITY_5_4_PLUS
  4. #endif
  5. #if UNITY_5_4_PLUS
  6. using UnityEngine.SceneManagement;
  7. #endif
  8. using System.IO;
  9. using UnityEngine;
  10. using UnityEngine.Events;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. public class DataCheatingDetector:ActDetectorBase
  14. {
  15. internal const string COMPONENT_NAME = "Injection Detector";
  16. internal const string FINAL_LOG_PREFIX = COMPONENT_NAME + ": ";
  17. private static int instancesInScene;
  18. /// <summary>
  19. /// 在加密和fake值之间最大的允许误差值
  20. /// </summary>
  21. public float floatEpsilon = 0.0001f;
  22. public static DataCheatingDetector Instance { get; private set; }
  23. private static DataCheatingDetector GetOrCreateInstance
  24. {
  25. get
  26. {
  27. if (Instance != null) return Instance;
  28. if (detectorsContainer == null)
  29. {
  30. detectorsContainer = new GameObject(CONTAINER_NAME);
  31. }
  32. Instance = detectorsContainer.AddComponent<DataCheatingDetector>();
  33. return Instance;
  34. }
  35. }
  36. public static void StartDetection()
  37. {
  38. if (Instance != null)
  39. {
  40. Instance.StartDetectionInternal(null);
  41. }
  42. else
  43. {
  44. //Log.e(FINAL_LOG_PREFIX + "can't be started since it doesn't exists in scene or not yet initialized!");
  45. }
  46. }
  47. public static void StartDetection(UnityAction callback)
  48. {
  49. GetOrCreateInstance.StartDetectionInternal(callback);
  50. }
  51. public static void StopDetection()
  52. {
  53. if (Instance != null) Instance.StopDetectionInternal();
  54. }
  55. public static void Dispose()
  56. {
  57. if (Instance != null) Instance.DisposeInternal();
  58. }
  59. internal static bool IsRunning
  60. {
  61. get
  62. {
  63. return ((object)Instance != null) && Instance.isRunning;
  64. }
  65. }
  66. private DataCheatingDetector() { } // prevents direct instantiation
  67. private void StartDetectionInternal(UnityAction callback)
  68. {
  69. if (isRunning)
  70. {
  71. Debug.LogWarning(FINAL_LOG_PREFIX + "already running!", this);
  72. return;
  73. }
  74. if (!enabled)
  75. {
  76. Debug.LogWarning(FINAL_LOG_PREFIX + "disabled but StartDetection still called from somewhere (see stack trace for this message)!", this);
  77. return;
  78. }
  79. if (callback != null && detectionEventHasListener)
  80. {
  81. Debug.LogWarning(FINAL_LOG_PREFIX + "has properly configured Detection Event in the inspector, but still get started with Action callback. Both Action and Detection Event will be called on detection. Are you sure you wish to do this?", this);
  82. }
  83. if (callback == null && !detectionEventHasListener)
  84. {
  85. Debug.LogWarning(FINAL_LOG_PREFIX + "was started without any callbacks. Please configure Detection Event in the inspector, or pass the callback Action to the StartDetection method.", this);
  86. enabled = false;
  87. return;
  88. }
  89. detectionAction = callback;
  90. started = true;
  91. isRunning = true;
  92. }
  93. private void Awake()
  94. {
  95. instancesInScene++;
  96. if (Init(Instance, COMPONENT_NAME))
  97. {
  98. Instance = this;
  99. }
  100. #if UNITY_5_4_PLUS
  101. SceneManager.sceneLoaded += OnLevelWasLoadedNew;
  102. #endif
  103. }
  104. protected override void OnDestroy()
  105. {
  106. base.OnDestroy();
  107. instancesInScene--;
  108. }
  109. #if UNITY_5_4_PLUS
  110. private void OnLevelWasLoadedNew(Scene scene, LoadSceneMode mode)
  111. {
  112. OnLevelLoadedCallback();
  113. }
  114. #else
  115. private void OnLevelWasLoaded()
  116. {
  117. OnLevelLoadedCallback();
  118. }
  119. #endif
  120. private void OnLevelLoadedCallback()
  121. {
  122. if (instancesInScene < 2)
  123. {
  124. if (!keepAlive)
  125. {
  126. DisposeInternal();
  127. }
  128. }
  129. else
  130. {
  131. if (!keepAlive && Instance != this)
  132. {
  133. DisposeInternal();
  134. }
  135. }
  136. }
  137. protected override void StartDetectionAutomatically()
  138. {
  139. StartDetectionInternal(null);
  140. }
  141. protected override void PauseDetector()
  142. {
  143. isRunning = false;
  144. }
  145. protected override void ResumeDetector()
  146. {
  147. if (detectionAction == null && !detectionEventHasListener) return;
  148. isRunning = true;
  149. }
  150. protected override void StopDetectionInternal()
  151. {
  152. if (!started)
  153. return;
  154. detectionAction = null;
  155. started = false;
  156. isRunning = false;
  157. }
  158. protected override void DisposeInternal()
  159. {
  160. base.DisposeInternal();
  161. if (Instance == this) Instance = null;
  162. }
  163. }