// 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 Quaternion, enhancing security for sensitive vector data.
/// In most scenarios, it is recommended to replace the default Quaternion type with this protected variant.
///
[Serializable]
public struct ProtectedQuaternion : 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 UIntFloat obfuscatedValueX;
///
/// The encrypted true value for the y-component.
///
private UIntFloat obfuscatedValueY;
///
/// The encrypted true value for the z-component.
///
private UIntFloat obfuscatedValueZ;
///
/// The encrypted true value for the w-component.
///
private UIntFloat obfuscatedValueW;
///
/// Used for calculation of int/float values for the secured value.
///
private UIntFloat manager;
///
/// A secret key the true value gets encrypted with.
///
private UInt32 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 Quaternion 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 Quaternion with the specified value.
///
/// The initial value of the protected Quaternion.
public ProtectedQuaternion(Quaternion _Value)
{
// Initialization
this.isInitialized = true;
// Initialization - Secured values.
this.obfuscatedValueX.intValue = 0;
this.obfuscatedValueX.floatValue = 0;
this.obfuscatedValueY.intValue = 0;
this.obfuscatedValueY.floatValue = 0;
this.obfuscatedValueZ.intValue = 0;
this.obfuscatedValueZ.floatValue = 0;
this.obfuscatedValueW.intValue = 0;
this.obfuscatedValueW.floatValue = 0;
// Initialization - Manager.
this.manager.intValue = 0;
this.manager.floatValue = 0;
// Initialization - Random secret.
this.secret = 0;
// Initialization - Fake value.
this.fakeValue = Quaternion.identity;
// Initialization - Integrity.
this.hasIntegrity = true;
// Obfuscate the value.
this.Obfuscate(_Value);
}
///
/// Gets and sets the true unencrypted field value.
///
public Quaternion Value
{
get
{
if (!this.isInitialized)
{
return new Quaternion();
}
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(Quaternion _Value)
{
// Obfuscate the value.
this.manager.floatValue = _Value.x;
this.manager.intValue = this.manager.intValue ^ this.secret;
this.obfuscatedValueX.floatValue = this.manager.floatValue;
this.manager.floatValue = _Value.y;
this.manager.intValue = this.manager.intValue ^ this.secret;
this.obfuscatedValueY.floatValue = this.manager.floatValue;
this.manager.floatValue = _Value.z;
this.manager.intValue = this.manager.intValue ^ this.secret;
this.obfuscatedValueZ.floatValue = this.manager.floatValue;
this.manager.floatValue = _Value.w;
this.manager.intValue = this.manager.intValue ^ this.secret;
this.obfuscatedValueW.floatValue = this.manager.floatValue;
// Assign the fake value.
this.fakeValue = _Value;
}
///
/// Unobfuscates the secured value and returns the true unencrypted value.
///
/// The true unencrypted value.
private Quaternion UnObfuscate()
{
// Get the unobfuscated value.
Quaternion var_RealValue = new Quaternion();
this.manager.intValue = this.obfuscatedValueX.intValue ^ this.secret;
var_RealValue.x = this.manager.floatValue;
this.manager.intValue = this.obfuscatedValueY.intValue ^ this.secret;
var_RealValue.y = this.manager.floatValue;
this.manager.intValue = this.obfuscatedValueZ.intValue ^ this.secret;
var_RealValue.z = this.manager.floatValue;
this.manager.intValue = this.obfuscatedValueW.intValue ^ this.secret;
var_RealValue.w = this.manager.floatValue;
// Return the unobfuscated value.
return var_RealValue;
}
///
/// Obfuscates the current value, generating a new random secret key.
///
public void Obfuscate()
{
// Unobfuscate the secured value.
Quaternion 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 attacker changed the honeypot fake value.
///
/// True if the protected value has integrity; otherwise, false.
public bool CheckIntegrity()
{
// Unobfuscate the secured value.
Quaternion 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 Quaternion.
///
public void Dispose()
{
this.obfuscatedValueX.intValue = 0;
this.obfuscatedValueY.intValue = 0;
this.obfuscatedValueZ.intValue = 0;
this.obfuscatedValueW.intValue = 0;
this.manager.intValue = 0;
this.secret = 0;
}
///
/// Returns a string representation of the protected Quaternion.
///
/// A string representation of the protected Quaternion.
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 Quaternion to a protected Quaternion.
///
/// The regular Quaternion to be converted.
/// A new instance of the protected Quaternion.
public static implicit operator ProtectedQuaternion(Quaternion _Value)
{
return new ProtectedQuaternion(_Value);
}
///
/// Implicitly converts a protected Quaternion to a regular Quaternion.
///
/// The protected Quaternion to be converted.
/// The true unencrypted value of the protected Quaternion.
public static implicit operator Quaternion(ProtectedQuaternion _Value)
{
return _Value.Value;
}
#endregion
#region Equality operator
///
/// Checks if two protected Quaternion instances are equal.
///
/// The first protected Quaternion.
/// The second protected Quaternion.
/// True if the values of the protected Quaternion instances are equal; otherwise, false.
public static bool operator ==(ProtectedQuaternion v1, ProtectedQuaternion v2)
{
return v1.Value == v2.Value;
}
///
/// Checks if two protected Quaternion instances are not equal.
///
/// The first protected Quaternion.
/// The second protected Quaternion.
/// True if the values of the protected Quaternion instances are not equal; otherwise, false.
public static bool operator !=(ProtectedQuaternion v1, ProtectedQuaternion v2)
{
return v1.Value != v2.Value;
}
///
/// Checks if the protected Quaternion is equal to the specified object.
///
/// The object to compare with the protected Quaternion.
/// True if the values are equal; otherwise, false.
public override bool Equals(object obj)
{
if (obj is ProtectedQuaternion)
{
return this.Value == ((ProtectedQuaternion)obj).Value;
}
return this.Value.Equals(obj);
}
#endregion
}
}