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