// 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 UInt16, designed to enhance integrity and security by obfuscating its value and incorporating anti-cheat measures. /// In most cases, this protected UInt16 can be used as a drop-in replacement for the default UInt16 type. /// [Serializable] public struct ProtectedUInt16 : 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 UInt16 obfuscatedValue; /// /// A secret key the true value gets encrypted with. /// private UInt16 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 UInt16 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 UInt16 with the specified initial value. /// /// The initial value of the protected UInt16. public ProtectedUInt16(UInt16 _Value = 0) { // Initialization this.isInitialized = true; this.obfuscatedValue = 0; this.secret = (UInt16)GlobalSettings.RandomProvider.RandomInt32(1, UInt16.MaxValue); this.fakeValue = 0; this.hasIntegrity = true; // Obfuscate the value. this.Obfuscate(_Value); } /// /// Gets and sets the true unencrypted field value. /// public UInt16 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(UInt16 _Value) { // Obfuscate the value. this.obfuscatedValue = (UInt16)(_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 UInt16 UnObfuscate() { // Get the unobfuscated value. return (UInt16)(this.obfuscatedValue ^ this.secret); } /// /// Obfuscates the current value, generating a new random secret key. /// public void Obfuscate() { // Unobfuscate the secured value. UInt16 var_UnobfuscatedValue = this.UnObfuscate(); // Create a new random secret. this.secret = (UInt16)GlobalSettings.RandomProvider.RandomInt32(1, UInt16.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. UInt16 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 UInt16 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 UInt16's true value. /// /// The hash code of the true value. public override int GetHashCode() { return (int)this.Value; } #region Implicit operator /// /// Implicitly converts an UInt16 value to a protected UInt16. /// /// The UInt16 value to convert. /// The corresponding protected UInt16. public static implicit operator ProtectedUInt16(UInt16 _Value) { return new ProtectedUInt16(_Value); } /// /// Implicitly converts a protected UInt16 to its UInt16 value. /// /// The protected UInt16 to convert. /// The UInt16 value of the protected UInt16. public static implicit operator UInt16(ProtectedUInt16 _Value) { return _Value.Value; } #endregion #region Calculation operator /// /// Adds two protected UInt16 values. /// /// The first protected UInt16. /// The second protected UInt16. /// The result of the addition. public static ProtectedUInt16 operator +(ProtectedUInt16 v1, ProtectedUInt16 v2) { return new ProtectedUInt16((UInt16)(v1.Value + v2.Value)); } /// /// Subtracts the second protected UInt16 from the first. /// /// The first protected UInt16. /// The second protected UInt16. /// The result of the subtraction. public static ProtectedUInt16 operator -(ProtectedUInt16 v1, ProtectedUInt16 v2) { return new ProtectedUInt16((UInt16)(v1.Value - v2.Value)); } /// /// Multiplies two protected UInt16 values. /// /// The first protected UInt16. /// The second protected UInt16. /// The result of the multiplication. public static ProtectedUInt16 operator *(ProtectedUInt16 v1, ProtectedUInt16 v2) { return new ProtectedUInt16((UInt16)(v1.Value * v2.Value)); } /// /// Divides the first protected UInt16 by the second. /// /// The first protected UInt16. /// The second protected UInt16. /// The result of the division. public static ProtectedUInt16 operator /(ProtectedUInt16 v1, ProtectedUInt16 v2) { return new ProtectedUInt16((UInt16)(v1.Value / v2.Value)); } #endregion #region Equality operator /// /// Checks if two protected UInt16 values are equal based on their true values. /// /// The first protected UInt16. /// The second protected UInt16. /// True if the true values are equal; otherwise, false. public static bool operator ==(ProtectedUInt16 v1, ProtectedUInt16 v2) { return v1.Value == v2.Value; } /// /// Checks if two protected UInt16 values are not equal based on their true values. /// /// The first protected UInt16. /// The second protected UInt16. /// True if the true values are not equal; otherwise, false. public static bool operator !=(ProtectedUInt16 v1, ProtectedUInt16 v2) { return v1.Value != v2.Value; } /// /// Checks if the protected UInt16 is equal to another object based on their true values. /// /// The object to compare with the protected UInt16. /// True if the true values are equal; otherwise, false. public override bool Equals(object obj) { if (obj is ProtectedUInt16) { return this.Value == ((ProtectedUInt16)obj).Value; } return this.Value.Equals(obj); } /// /// Compares two protected UInt16 values. /// /// The first protected UInt16. /// The second protected UInt16. /// True if the first value is less than the second; otherwise, false. public static bool operator <(ProtectedUInt16 v1, ProtectedUInt16 v2) { return v1.Value < v2.Value; } /// /// Checks if the first protected UInt16 is less than or equal to the second. /// /// The first protected UInt16. /// The second protected UInt16. /// True if the first value is less than or equal to the second; otherwise, false. public static bool operator <=(ProtectedUInt16 v1, ProtectedUInt16 v2) { return v1.Value <= v2.Value; } /// /// Checks if the first protected UInt16 is greater than the second. /// /// The first protected UInt16. /// The second protected UInt16. /// True if the first value is greater than the second; otherwise, false. public static bool operator >(ProtectedUInt16 v1, ProtectedUInt16 v2) { return v1.Value > v2.Value; } /// /// Checks if the first protected UInt16 is greater than or equal to the second. /// /// The first protected UInt16. /// The second protected UInt16. /// True if the first value is greater than or equal to the second; otherwise, false. public static bool operator >=(ProtectedUInt16 v1, ProtectedUInt16 v2) { return v1.Value >= v2.Value; } #endregion } }