// 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 boolean, designed to enhance integrity and security by obfuscating its value and incorporating anti-cheat measures. /// In most cases, this protected boolean can be used as a drop-in replacement for the default boolean type. /// [Serializable] public struct ProtectedBool : 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 obfuscated value of the protected. /// private byte obfuscatedValue; /// /// A secret key used to obfuscate the true value. /// private Int32 secret; /// /// A honeypot pretending to be the original value. If a user attempts to change this value via a cheat/hack engine, you will be notified. /// The protected value will retain its true value. /// [SerializeField] private bool fakeValue; /// /// Unity serialization hook. Ensures the correct values are serialized. /// public void OnBeforeSerialize() { this.fakeValue = Value; } /// /// Unity deserialization hook. Ensures the correct values are deserialized. /// public void OnAfterDeserialize() { this = this.fakeValue; } /// /// Creates a new protected boolean with the specified initial value. /// /// The initial value of the protected boolean. public ProtectedBool(bool _Value = false) { // Initialization this.isInitialized = true; this.obfuscatedValue = 0; this.secret = GlobalSettings.RandomProvider.RandomInt32(1, Int32.MaxValue); this.fakeValue = _Value; this.hasIntegrity = true; // Obfuscate the value. this.Obfuscate(_Value); } /// /// Gets and sets the true unencrypted field value. /// public bool Value { get { if(!this.isInitialized) { return false; } 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(bool _Value) { // Obfuscate the value. byte var_BoolAsByte = (_Value) ? (byte)1 : (byte)0; this.obfuscatedValue = (byte)(var_BoolAsByte ^ this.secret); // Assign the fake value. this.fakeValue = _Value; } /// /// Unobfuscates the secured value and returns the true unencrypted value. /// /// The true unencrypted value. private bool UnObfuscate() { // Get the unobfuscated value. byte var_BoolAsByte = (byte)(this.obfuscatedValue ^ this.secret); return (var_BoolAsByte == 1); } /// /// Obfuscates the current value, generating a new random secret key. /// public void Obfuscate() { // Unobfuscate the secured value. bool var_UnobfuscatedValue = this.UnObfuscate(); // Create a new random secret. this.secret = GlobalSettings.RandomProvider.RandomInt32(1, Int32.MaxValue); // Obfuscate the value. this.Obfuscate(var_UnobfuscatedValue); } /// /// Checks the integrity of the protected boolean, detecting if an attacher changed the honeypot fake value. /// /// True if the protected boolean has integrity; otherwise, false. public bool CheckIntegrity() { // Unobfuscate the secured value. bool 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 secure and secret values. /// public void Dispose() { this.obfuscatedValue = 0; this.secret = 0; } /// /// Gets the hash code of the protected boolean's true value. /// /// The hash code of the true value. public override int GetHashCode() { return this.Value.GetHashCode(); } /// /// Converts the protected boolean to its string representation. /// /// The string representation of the true value. public override string ToString() { return this.Value.ToString(); } #region Serialization /// /// Used to serialize the protected to the player prefs. /// /// The obfuscated value of the protected. /// The secret key used to obfuscate the true value. internal void Serialize(out byte _ObfuscatedValue, out int _Secret) { _ObfuscatedValue = this.obfuscatedValue; _Secret = this.secret; } /// /// Used to deserialize the protected from the player prefs. /// /// The obfuscated value of the protected. /// The secret key used to obfuscate the true value. internal void Deserialize(byte _ObfuscatedValue, int _Secret) { this.obfuscatedValue = _ObfuscatedValue; this.secret = _Secret; this.fakeValue = this.UnObfuscate(); } #endregion #region Implicit operator /// /// Implicitly converts a boolean value to a protected boolean. /// /// The boolean value to convert. /// The corresponding protected boolean. public static implicit operator ProtectedBool(bool _Value) { return new ProtectedBool(_Value); } /// /// Implicitly converts a protected boolean to its boolean value. /// /// The protected boolean to convert. /// The boolean value of the protected boolean. public static implicit operator bool(ProtectedBool _Value) { return _Value.Value; } #endregion #region Equality operator /// /// Checks if two protected booleans are equal based on their true values. /// /// The first protected boolean. /// The second protected boolean. /// True if the true values are equal; otherwise, false. public static bool operator ==(ProtectedBool v1, ProtectedBool v2) { return v1.Value == v2.Value; } /// /// Checks if two protected booleans are not equal based on their true values. /// /// The first protected boolean. /// The second protected boolean. /// True if the true values are not equal; otherwise, false. public static bool operator !=(ProtectedBool v1, ProtectedBool v2) { return v1.Value != v2.Value; } /// /// Checks if the protected boolean is equal to another object based on their true values. /// /// The object to compare with the protected boolean. /// True if the true values are equal; otherwise, false. public override bool Equals(object obj) { if (obj is ProtectedBool) { return this.Value == ((ProtectedBool)obj).Value; } return this.Value.Equals(obj); } #endregion } }