Wanzyee Studio
CryptoValue< T, U >

Wrapper to encrypt and store a value to avoid leave plain data in the memory. More...

Inherits ISerializationCallbackReceiver.

Inherited by CryptoCache< T, U >, and CryptoObject< T, U >.

Public Member Functions

 CryptoValue (T value=default)
 Initialize with the specified value. More...
 
override string ToString ()
 Return the plain text. More...
 
virtual void OnBeforeSerialize ()
 OnBeforeSerialize, check if saving to obfuscate the plain data, otherwise prepare the editee value. More...
 
virtual void OnAfterDeserialize ()
 OnAfterDeserialize, check if to store and clean the editee value. More...
 

Protected Member Functions

ReadValue ()
 Read the stored real value. More...
 
void WriteValue (T value)
 Write the real value to store. More...
 
string ReadText ()
 Read the decrypted plain text. More...
 
void WriteText (string text, bool force=false)
 Encrypt the plain text and write with a new key. More...
 
virtual T ToValue (string text)
 Convert the decrypted text to the real value. More...
 
virtual string ToText (T value)
 Convert the real value to the text to encrypt. More...
 
virtual U ReadEditee ()
 Read the stored value to the editee value. More...
 
virtual void WriteEditee (U editee)
 Write the editee value to the stored value. More...
 
virtual string ValidateEditee ()
 Validate the editee value, return null if correct, otherwise the warning message. More...
 

Properties

virtual T value [get, set]
 The stored value. More...
 

Detailed Description

Wrapper to encrypt and store a value to avoid leave plain data in the memory.

Please note:

  1. Reference this wrapper in need, instead of keeping the real value.
  2. Like deserializing, the decrypted value won't be the same instance before encrypting.

Commonly, a derived class may:

  1. Declare a non-generic type for Unity serialization.
  2. Override ToValue() or ToText() for custom conversion between plain text.
  3. Or override value property to wrap custom behavior, e.g., obfuscated cache.
Template Parameters
TThe value type.
UThe editee type, commonly use T or string.

Constructor & Destructor Documentation

CryptoValue ( value = default)

Initialize with the specified value.

Parameters
valueValue.

Member Function Documentation

override string ToString ( )

Return the plain text.

Returns
A string represents this.
T ReadValue ( )
protected

Read the stored real value.

Returns
Value.
void WriteValue ( value)
protected

Write the real value to store.

Parameters
valueValue.
string ReadText ( )
protected

Read the decrypted plain text.

Returns
Plain text.
void WriteText ( string  text,
bool  force = false 
)
protected

Encrypt the plain text and write with a new key.

Parameters
textPlain text.
forceIf to force update, otherwise check changed in editor.
virtual T ToValue ( string  text)
protectedvirtual

Convert the decrypted text to the real value.

Parameters
textText.
Returns
Value.

Reimplemented in CryptoObject< T, U >.

virtual string ToText ( value)
protectedvirtual

Convert the real value to the text to encrypt.

Parameters
valueValue.
Returns
Text.

Reimplemented in CryptoObject< T, U >.

virtual void OnBeforeSerialize ( )
virtual

OnBeforeSerialize, check if saving to obfuscate the plain data, otherwise prepare the editee value.

Reimplemented in CryptoCache< T, U >.

virtual void OnAfterDeserialize ( )
virtual

OnAfterDeserialize, check if to store and clean the editee value.

Reimplemented in CryptoCache< T, U >.

virtual U ReadEditee ( )
protectedvirtual

Read the stored value to the editee value.

Returns
Editee.
virtual void WriteEditee ( editee)
protectedvirtual

Write the editee value to the stored value.

Parameters
editeeEditee.
virtual string ValidateEditee ( )
protectedvirtual

Validate the editee value, return null if correct, otherwise the warning message.

Returns
Message.

Property Documentation

virtual T value
getset

The stored value.