Page 1 of 1

Support for Interfaces

Posted: Tue Jun 26, 2018 6:53 am
by Joel
Status

Requested

Complexity

5/10

Description

Ability to store interfaces rather than concrete classes.

Re: Support for Interfaces

Posted: Sat May 30, 2020 11:29 am
by MilkyBrain
It would be great!!!!

Re: Support for Interfaces

Posted: Sat Jul 17, 2021 12:53 am
by TauCetiLabs
I have figured out a work around using references and handling the data in a custom way. One caveat is the component must already exists in the reference manager upon loading:


Both the "m_PickupLocation" and "m_DropoffLocation" refer to a private interface, namely 'ISubstanceAccessible'.

Code: Select all

namespace ES3Types
{
	[UnityEngine.Scripting.Preserve]
	[ES3PropertiesAttribute("m_CurrentState", "m_PickupLocation", "m_DropoffLocation", "m_TransferVolume", "enabled", "name")]
	public class ES3UserType_SubstanceHandler : ES3ComponentType
	{
		public static ES3Type Instance = null;

		public ES3UserType_SubstanceHandler() : base(typeof(SubstanceHandler)){ Instance = this; priority = 1;}


		protected override void WriteComponent(object obj, ES3Writer writer)
		{
			var instance = (SubstanceHandler)obj;
			
			writer.WritePrivateField("m_CurrentState", instance);
			writer.WriteProperty<long>("m_PickupLocation", ES3Internal.ES3ReferenceMgrBase.Current.Get((UnityEngine.Object)instance.PickupLocation));
			writer.WriteProperty<long>("m_DropoffLocation", ES3Internal.ES3ReferenceMgrBase.Current.Get((UnityEngine.Object)instance.DropoffLocation));
			writer.WritePrivateField("m_TransferVolume", instance);
			writer.WriteProperty("enabled", instance.enabled, ES3Type_bool.Instance);
		}

		protected override void ReadComponent<T>(ES3Reader reader, object obj)
		{
			var instance = (SubstanceHandler)obj;
			foreach(string propertyName in reader.Properties)
			{
				switch(propertyName)
				{
					
					case "m_CurrentState":
						reader.SetPrivateField("m_CurrentState", reader.Read<SubstanceHandler.State>(), instance);
						break;
					case "m_PickupLocation":
						reader.SetPrivateField("m_PickupLocation", ES3Internal.ES3ReferenceMgrBase.Current.Get(reader.Read<long>()), instance);
						break;
					case "m_DropoffLocation":
						reader.SetPrivateField("m_DropoffLocation", ES3Internal.ES3ReferenceMgrBase.Current.Get(reader.Read<long>()), instance);
						break;
					case "m_TransferVolume":
						reader.SetPrivateField("m_TransferVolume", reader.Read<System.Single>(), instance);
						break;
					case "enabled":
						instance.enabled = reader.Read<System.Boolean>(ES3Type_bool.Instance);
						break;
					default:
						reader.Skip();
						break;
				}
			}
		}
	}


	public class ES3UserType_SubstanceHandlerArray : ES3ArrayType
	{
		public static ES3Type Instance;

		public ES3UserType_SubstanceHandlerArray() : base(typeof(SubstanceHandler[]), ES3UserType_SubstanceHandler.Instance)
		{
			Instance = this;
		}
	}
}