// System using System; // Unity using UnityEngine; // GUPS - AntiCheat - Core using GUPS.AntiCheat.Core.Protected; // GUPS - AntiCheat using GUPS.AntiCheat.Detector; using GUPS.AntiCheat.Settings; namespace GUPS.AntiCheat.Protected { /// /// Represents a protected UInt64, designed to enhance integrity and security by obfuscating its value and incorporating anti-cheat measures. /// In most cases, this protected UInt64 can be used as a drop-in replacement for the default UInt64 type. /// [Serializable] public struct ProtectedUInt64 : IProtected, IDisposable, ISerializationCallbackReceiver { /// /// A struct does not have a default constructor that is called when the structure is created. Therefore, the protected primitive must return /// a default value if it does not have an assigned value. /// private bool isInitialized; /// /// Gets a value indicating whether the protected value has integrity, i.e., whether it has maintained its original state. /// private bool hasIntegrity; /// /// Gets a value indicating whether the protected value has integrity, i.e., whether it has maintained its original state. /// public bool HasIntegrity { get => hasIntegrity || !isInitialized; private set => hasIntegrity = value; } /// /// The encrypted true value. /// private UInt64 obfuscatedValue; /// /// A secret key the true value gets encrypted with. /// private UInt64 secret; /// /// A honeypot pretending to be the original value. If some user tries to change this value via a cheat/hack engine, you will get notified. /// The protected value will keep its true value. /// [SerializeField] private UInt64 fakeValue; /// /// Unity serialization hook. Ensures the correct values will be serialized. /// public void OnBeforeSerialize() { this.fakeValue = Value; } /// /// Unity deserialization hook. Ensures the correct values will be deserialized. /// public void OnAfterDeserialize() { this = this.fakeValue; } /// /// Creates a new protected UInt64 with the specified initial value. /// /// The initial value of the protected UInt64. public ProtectedUInt64(UInt64 _Value = 0) { // Initialization this.isInitialized = true; this.obfuscatedValue = 0; this.secret = (UInt32)GlobalSettings.RandomProvider.RandomInt32(1, Int32.MaxValue); this.fakeValue = 0; this.hasIntegrity = true; // Obfuscate the value. this.Obfuscate(_Value); } /// /// Gets and sets the true unencrypted field value. /// public UInt64 Value { get { if (!this.isInitialized) { return 0; } if (!this.CheckIntegrity()) { AntiCheatMonitor.Instance.GetDetector()?.OnNext(this); } return this.UnObfuscate(); } set { this.Obfuscate(value); } } /// /// Gets the true unencrypted field value. /// object IProtected.Value => this.Value; /// /// Obfuscates the specified value, encrypting it with the secret key. /// private void Obfuscate(UInt64 _Value) { // Obfuscate the value. this.obfuscatedValue = _Value ^ this.secret; // Assign the fake value. this.fakeValue = _Value; } /// /// Unobfuscates the secured value and returns the true unencrypted value. /// /// The true unencrypted value. private UInt64 UnObfuscate() { // Get the unobfuscated value. return (UInt64)(this.obfuscatedValue ^ this.secret); } /// /// Obfuscates the current value, generating a new random secret key. /// public void Obfuscate() { // Unobfuscate the secured value. UInt64 var_UnobfuscatedValue = this.UnObfuscate(); // Create a new random secret. this.secret = (UInt32)GlobalSettings.RandomProvider.RandomInt32(1, Int32.MaxValue); // Obfuscate the value. this.Obfuscate(var_UnobfuscatedValue); } /// /// Checks the integrity of the protected value, detecting if an attacher changed the honeypot fake value. /// /// True if the protected value has integrity; otherwise, false. public bool CheckIntegrity() { // Unobfuscate the secured value. UInt64 var_UnobfuscatedValue = this.UnObfuscate(); // Check if an attacher changed the honeypot fake value. if (this.fakeValue != var_UnobfuscatedValue) { this.HasIntegrity = false; } // Return the integrity status. return this.HasIntegrity; } /// /// Disposes of the secured and secret values. /// public void Dispose() { this.obfuscatedValue = 0; this.secret = 0; } /// /// Converts the protected UInt64 to its string representation. /// /// The string representation of the true value. public override string ToString() { return this.Value.ToString(); } /// /// Gets the hash code of the protected UInt64's true value. /// /// The hash code of the true value. public override int GetHashCode() { return (int)this.Value; } #region Implicit operator /// /// Implicitly converts an UInt64 value to a protected UInt64. /// /// The UInt64 value to convert. /// The corresponding protected UInt64. public static implicit operator ProtectedUInt64(UInt64 _Value) { return new ProtectedUInt64(_Value); } /// /// Implicitly converts a protected UInt64 to its UInt64 value. /// /// The protected UInt64 to convert. /// The UInt64 value of the protected UInt64. public static implicit operator UInt64(ProtectedUInt64 _Value) { return _Value.Value; } #endregion #region Calculation operator /// /// Adds two protected UInt64 values. /// /// The first protected UInt64. /// The second protected UInt64. /// The result of the addition. public static ProtectedUInt64 operator +(ProtectedUInt64 v1, ProtectedUInt64 v2) { return new ProtectedUInt64(v1.Value + v2.Value); } /// /// Subtracts the second protected UInt64 from the first. /// /// The first protected UInt64. /// The second protected UInt64. /// The result of the subtraction. public static ProtectedUInt64 operator -(ProtectedUInt64 v1, ProtectedUInt64 v2) { return new ProtectedUInt64(v1.Value - v2.Value); } /// /// Multiplies two protected UInt64 values. /// /// The first protected UInt64. /// The second protected UInt64. /// The result of the multiplication. public static ProtectedUInt64 operator *(ProtectedUInt64 v1, ProtectedUInt64 v2) { return new ProtectedUInt64(v1.Value * v2.Value); } /// /// Divides the first protected UInt64 by the second. /// /// The first protected UInt64. /// The second protected UInt64. /// The result of the division. public static ProtectedUInt64 operator /(ProtectedUInt64 v1, ProtectedUInt64 v2) { return new ProtectedUInt64(v1.Value / v2.Value); } #endregion #region Equality operator /// /// Checks if two protected UInt64 values are equal based on their true values. /// /// The first protected UInt64. /// The second protected UInt64. /// True if the true values are equal; otherwise, false. public static bool operator ==(ProtectedUInt64 v1, ProtectedUInt64 v2) { return v1.Value == v2.Value; } /// /// Checks if two protected UInt64 values are not equal based on their true values. /// /// The first protected UInt64. /// The second protected UInt64. /// True if the true values are not equal; otherwise, false. public static bool operator !=(ProtectedUInt64 v1, ProtectedUInt64 v2) { return v1.Value != v2.Value; } /// /// Checks if the protected UInt64 is equal to another object based on their true values. /// /// The object to compare with the protected UInt64. /// True if the true values are equal; otherwise, false. public override bool Equals(object obj) { if (obj is ProtectedUInt64) { return this.Value == ((ProtectedUInt64)obj).Value; } return this.Value.Equals(obj); } /// /// Compares two protected UInt64 values. /// /// The first protected UInt64. /// The second protected UInt64. /// True if the first value is less than the second; otherwise, false. public static bool operator <(ProtectedUInt64 v1, ProtectedUInt64 v2) { return v1.Value < v2.Value; } /// /// Checks if the first protected UInt64 is less than or equal to the second. /// /// The first protected UInt64. /// The second protected UInt64. /// True if the first value is less than or equal to the second; otherwise, false. public static bool operator <=(ProtectedUInt64 v1, ProtectedUInt64 v2) { return v1.Value <= v2.Value; } /// /// Checks if the first protected UInt64 is greater than the second. /// /// The first protected UInt64. /// The second protected UInt64. /// True if the first value is greater than the second; otherwise, false. public static bool operator >(ProtectedUInt64 v1, ProtectedUInt64 v2) { return v1.Value > v2.Value; } /// /// Checks if the first protected UInt64 is greater than or equal to the second. /// /// The first protected UInt64. /// The second protected UInt64. /// True if the first value is greater than or equal to the second; otherwise, false. public static bool operator >=(ProtectedUInt64 v1, ProtectedUInt64 v2) { return v1.Value >= v2.Value; } #endregion } }