CrypticInt32.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. /// <summary>
  5. /// 神秘数字
  6. /// </summary>
  7. public struct CrypticInt32
  8. {
  9. private int hiddenValue;
  10. private int _Decrypt;
  11. private int fakeValue;
  12. private bool inited;
  13. public CrypticInt32(int nValue)
  14. {
  15. hiddenValue = nValue;
  16. fakeValue = 0;
  17. _Decrypt = 0;
  18. inited = true;
  19. }
  20. public static int Encrypt(int value, out int decrypt)
  21. {
  22. decrypt = GameMgr.RandSeed;
  23. return value ^ decrypt;
  24. }
  25. public static int Decrypt(int value, int decrypt)
  26. {
  27. return value ^ decrypt;
  28. }
  29. int InternalDecrypt()
  30. {
  31. if (!inited)
  32. {
  33. hiddenValue = Encrypt(0, out _Decrypt);
  34. fakeValue = 0;
  35. inited = true;
  36. }
  37. int decrypted = Decrypt(hiddenValue, _Decrypt);
  38. if (DataCheatingDetector.IsRunning && fakeValue != 0 && decrypted != fakeValue)
  39. DataCheatingDetector.Instance.OnCheatingDetected();
  40. return decrypted;
  41. }
  42. public static implicit operator CrypticInt32(int nValue)
  43. {
  44. int decrypt = 0;
  45. CrypticInt32 obscured = new CrypticInt32(Encrypt(nValue, out decrypt));
  46. if (DataCheatingDetector.IsRunning)
  47. {
  48. obscured.fakeValue = nValue;
  49. }
  50. obscured._Decrypt = decrypt;
  51. return obscured;
  52. }
  53. public static implicit operator int(CrypticInt32 inData)
  54. {
  55. return inData.InternalDecrypt();
  56. }
  57. public static explicit operator UInt32(CrypticInt32 inData)
  58. {
  59. return (UInt32)inData.InternalDecrypt();
  60. }
  61. public static explicit operator UInt16(CrypticInt32 inData)
  62. {
  63. return (UInt16)inData.InternalDecrypt();
  64. }
  65. public override bool Equals(object obj)
  66. {
  67. return (obj != null && this.GetType() == obj.GetType() && ((int)this == (int)((CrypticInt32)obj)));
  68. }
  69. public int ToInt()
  70. {
  71. return (int)this;
  72. }
  73. public uint ToUint()
  74. {
  75. return (uint)this;
  76. }
  77. public override string ToString()
  78. {
  79. return ToInt().ToString();
  80. }
  81. public string ToUintString()
  82. {
  83. return ToUint().ToString();
  84. }
  85. }