namespace UdonSharp.Serialization { public interface IHeapStorage { /// /// Sets an element value in the storage interface, the storage interface must be a collection interface /// /// /// void SetElementValueWeak(string elementKey, object value); /// /// Get an element value /// /// /// object GetElementValueWeak(string elementKey); T GetElementValue(string elementKey); void SetElementValue(string elementKey, T value); IValueStorage GetElementStorage(string elementKey); } public interface IValueStorage { object Value { get; set; } void Reset(); } public abstract class ValueStorage : IValueStorage { public abstract T Value { get; set; } object IValueStorage.Value { get => Value; set { try { Value = (T)value; } catch (System.InvalidCastException) { Value = default; UnityEngine.Debug.LogWarning($"Failed to assign element in storage, could not cast from '{value.GetType()}' to '{typeof(T)}'. Assigning default value."); } } } public System.Type ValueType { get { return typeof(T); } } public void Reset() { Value = default; } } /// /// This variant of ValueStorage acts like StrongBox more or less /// More complex implementations of ValueStorage can do things like reference an element in an IHeapStorageInterface /// public class SimpleValueStorage : ValueStorage { T _value; public override T Value { get => _value; set => _value = value; } public SimpleValueStorage() { _value = default; } public SimpleValueStorage(T value) { _value = value; } } public static class ValueStorageUtil { public static IValueStorage CreateStorage(System.Type storageType) { return (IValueStorage)System.Activator.CreateInstance(typeof(SimpleValueStorage<>).MakeGenericType(storageType)); } public static IValueStorage CreateStorage(System.Type storageType, object value) { return (IValueStorage)System.Activator.CreateInstance(typeof(SimpleValueStorage<>).MakeGenericType(storageType), value); } public static IValueStorage CreateStorage(T value) { ValueStorage valueStorage = (ValueStorage)System.Activator.CreateInstance(typeof(SimpleValueStorage)); valueStorage.Value = value; return valueStorage; } } }