SystemTimeAntiCheat.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. //检测系统时间修改
  5. public class SystemTimeAntiCheat : AntiCheatBase
  6. {
  7. private int m_nLastServerTime = -1;
  8. private int m_nLastLocalTime = -1;
  9. private bool bIsBeCheat = false;
  10. protected override void OnInit()
  11. {
  12. Reset();
  13. TimerManager.Instance.EvtChangeTime += OnChangeServerTime; //平均1分请求一次
  14. }
  15. protected override bool IsCheckAntiCheat()
  16. {
  17. //断网情况
  18. bool bIsConnect = NetworkMgr.Instance.GetConnectStatus();
  19. if(!bIsConnect)
  20. {
  21. Reset();
  22. }
  23. return bIsBeCheat;
  24. }
  25. protected override void OnAntiCheat()
  26. {
  27. //检测到非法运行速度 显示提示框
  28. Reset();
  29. LuaInterface.LuaState pLuaState = LuaMgr.GetMainState();
  30. if (null != pLuaState)
  31. {
  32. bool bIsConnect = NetworkMgr.Instance.GetConnectStatus();
  33. if (!bIsConnect)
  34. LuaMgr.GetMainState().DoString("ManagerContainer.LuaBattleMgr:ShowAntiCheatMsgBox(0)");
  35. else
  36. LuaMgr.GetMainState().DoString("ManagerContainer.LuaBattleMgr:SendAntiCheat(0)");
  37. BattleMgr.Instance.DoPauseFighting(true);
  38. if (Time.timeScale > GameMgr.Instance.GetGameSpeed())
  39. {
  40. Time.timeScale = GameMgr.Instance.GetGameSpeed();
  41. }
  42. }
  43. else
  44. {
  45. TimerManager.Instance.AddTimer(3000, 1, (nTime) =>
  46. {
  47. GameMgr.Instance.QuitGame();
  48. });
  49. }
  50. Debug.Log("非法速度");
  51. }
  52. protected override void OnDispos()
  53. {
  54. base.OnDispos();
  55. TimerManager.Instance.EvtChangeTime -= OnChangeServerTime;
  56. }
  57. private void Reset()
  58. {
  59. m_nLastServerTime = -1;
  60. m_nLastLocalTime = -1;
  61. bIsBeCheat = false;
  62. }
  63. private void OnChangeServerTime(int nServerTime)
  64. {
  65. int nLocalTime = (int)Time.realtimeSinceStartup;
  66. if (-1 == m_nLastServerTime)
  67. {
  68. m_nLastServerTime = nServerTime;
  69. m_nLastLocalTime = nLocalTime;
  70. }
  71. int nDeltaServerTime = nServerTime - m_nLastServerTime;
  72. int nDeltaLocalTime = nLocalTime - m_nLastLocalTime;
  73. int nDeltaTime = nDeltaLocalTime - nDeltaServerTime;
  74. if (bIsBeCheat)
  75. return;
  76. if(nDeltaServerTime < AntiCheatCfg.c_fDeltaServerTime)//排除網絡延遲較大的波動誤判
  77. {
  78. if (nDeltaTime >= AntiCheatCfg.c_nTimeSysteMaxDurationTime)//加速判断
  79. {
  80. bIsBeCheat = true;
  81. }
  82. }
  83. m_nLastServerTime = nServerTime;
  84. m_nLastLocalTime = nLocalTime;
  85. }
  86. }