// 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 Vector2Int, enhancing security for sensitive vector data. /// In most scenarios, it is recommended to replace the default Vector2Int type with this protected variant. /// [Serializable] public struct ProtectedVector2Int : 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 for the x-component. /// private Int32 obfuscatedValueX; /// /// The encrypted true value for the y-component. /// private Int32 obfuscatedValueY; /// /// A secret key the true value gets encrypted with. /// private Int32 secret; /// /// A honeypot pretending to be the original value. If a user attempts to change this value via a cheat/hack engine, notifications will be triggered. /// The protected value will maintain its true representation. /// [SerializeField] private Vector2Int 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 Vector2Int with the specified value. /// /// The initial value of the protected Vector2Int. public ProtectedVector2Int(Vector2Int _Value) { // Initialization this.isInitialized = true; // Initialization - Secured values. this.obfuscatedValueX = 0; this.obfuscatedValueY = 0; // Initialization - Random secret. this.secret = 0; // Initialization - Fake value. this.fakeValue = Vector2Int.zero; // Initialization - Integrity. this.hasIntegrity = true; // Obfuscate the value. this.Obfuscate(_Value); } /// /// Gets and sets the true unencrypted field value. /// public Vector2Int Value { get { if (!this.isInitialized) { return new Vector2Int(); } 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(Vector2Int _Value) { // Obfuscate the value. this.obfuscatedValueX = (int)_Value.x ^ this.secret; this.obfuscatedValueY = (int)_Value.y ^ this.secret; // Assign the fake value. this.fakeValue = _Value; } /// /// Unobfuscates the secured value and returns the true unencrypted value. /// /// The true unencrypted value. private Vector2Int UnObfuscate() { // Get the unobfuscated value. Vector2Int var_RealValue = new Vector2Int(); var_RealValue.x = this.obfuscatedValueX ^ this.secret; var_RealValue.y = this.obfuscatedValueY ^ this.secret; // Return the unobfuscated value. return var_RealValue; } /// /// Obfuscates the current value, generating a new random secret key. /// public void Obfuscate() { // Unobfuscate the secured value. Vector2Int 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 value, detecting if an attacker changed the honeypot fake value. /// /// True if the protected value has integrity; otherwise, false. public bool CheckIntegrity() { // Unobfuscate the secured value. Vector2Int var_UnobfuscatedValue = this.UnObfuscate(); // Check if an attacker changed the honeypot fake value. if (this.fakeValue != var_UnobfuscatedValue) { this.HasIntegrity = false; } // Return the integrity status. return this.HasIntegrity; } /// /// Releases the resources used by the protected Vector2Int. /// public void Dispose() { this.obfuscatedValueX = 0; this.obfuscatedValueY = 0; this.secret = 0; } /// /// Returns a string representation of the protected Vector2Int. /// /// A string representation of the protected Vector2Int. public override string ToString() { return this.Value.ToString(); } /// /// Gets the hash code for the protected value. /// /// The hash code for the protected value. public override int GetHashCode() { return this.Value.GetHashCode(); } #region Implicit operator /// /// Implicitly converts a regular Vector2Int to a protected Vector2Int. /// /// The regular Vector2Int to be converted. /// A new instance of the protected Vector2Int. public static implicit operator ProtectedVector2Int(Vector2Int _Value) { return new ProtectedVector2Int(_Value); } /// /// Implicitly converts a protected Vector2Int to a regular Vector2Int. /// /// The protected Vector2Int to be converted. /// The true unencrypted value of the protected Vector2Int. public static implicit operator Vector2Int(ProtectedVector2Int _Value) { return _Value.Value; } /// /// Implicitly converts a protected Vector2Int to a protected Quaternion. /// /// The protected Vector2Int to be converted. /// The encrypted value as protected Quaternion. public static implicit operator ProtectedQuaternion(ProtectedVector2Int _Value) { return new ProtectedQuaternion(new Quaternion(_Value.Value.x, _Value.Value.y, 0, 0)); } /// /// Implicitly converts a protected Quaternion to a protected Vector2Int. /// /// The protected Quaternion to be converted. /// The encrypted value as protected Vector2Int. public static implicit operator ProtectedVector2Int(ProtectedQuaternion _Value) { return new ProtectedVector2Int(new Vector2Int((int)_Value.Value.x, (int)_Value.Value.y)); } #endregion #region Equality operator /// /// Checks if two protected Vector2Int instances are equal. /// /// The first protected Vector2Int. /// The second protected Vector2Int. /// True if the values of the protected Vector2Int instances are equal; otherwise, false. public static bool operator ==(ProtectedVector2Int v1, ProtectedVector2Int v2) { return v1.Value == v2.Value; } /// /// Checks if two protected Vector2Int instances are not equal. /// /// The first protected Vector2Int. /// The second protected Vector2Int. /// True if the values of the protected Vector2Int instances are not equal; otherwise, false. public static bool operator !=(ProtectedVector2Int v1, ProtectedVector2Int v2) { return v1.Value != v2.Value; } /// /// Checks if the protected Vector2Int is equal to the specified object. /// /// The object to compare with the protected Vector2Int. /// True if the values are equal; otherwise, false. public override bool Equals(object obj) { if (obj is ProtectedVector2Int) { return this.Value == ((ProtectedVector2Int)obj).Value; } return this.Value.Equals(obj); } #endregion } }