using System;
using System.Reflection;
namespace Unity.MLAgents.Sensors.Reflection
{
///
/// Construction info for a ReflectionSensorBase.
///
internal struct ReflectionSensorInfo
{
public object Object;
public FieldInfo FieldInfo;
public PropertyInfo PropertyInfo;
public ObservableAttribute ObservableAttribute;
public string SensorName;
public Type GetMemberType()
{
return FieldInfo != null ? FieldInfo.FieldType : PropertyInfo.PropertyType;
}
}
///
/// Abstract base class for reflection-based sensors.
///
internal abstract class ReflectionSensorBase : ISensor
{
protected object m_Object;
// Exactly one of m_FieldInfo and m_PropertyInfo should be non-null.
protected FieldInfo m_FieldInfo;
protected PropertyInfo m_PropertyInfo;
// Not currently used, but might want later.
protected ObservableAttribute m_ObservableAttribute;
// Cached sensor names and shapes.
string m_SensorName;
int[] m_Shape;
public ReflectionSensorBase(ReflectionSensorInfo reflectionSensorInfo, int size)
{
m_Object = reflectionSensorInfo.Object;
m_FieldInfo = reflectionSensorInfo.FieldInfo;
m_PropertyInfo = reflectionSensorInfo.PropertyInfo;
m_ObservableAttribute = reflectionSensorInfo.ObservableAttribute;
m_SensorName = reflectionSensorInfo.SensorName;
m_Shape = new[] {size};
}
///
public int[] GetObservationShape()
{
return m_Shape;
}
///
public int Write(ObservationWriter writer)
{
WriteReflectedField(writer);
return m_Shape[0];
}
internal abstract void WriteReflectedField(ObservationWriter writer);
///
/// Get either the reflected field, or return the reflected property.
/// This should be used by implementations in their WriteReflectedField() method.
///
///
protected object GetReflectedValue()
{
return m_FieldInfo != null ?
m_FieldInfo.GetValue(m_Object) :
m_PropertyInfo.GetMethod.Invoke(m_Object, null);
}
///
public byte[] GetCompressedObservation()
{
return null;
}
///
public void Update() {}
///
public void Reset() {}
///
public SensorCompressionType GetCompressionType()
{
return SensorCompressionType.None;
}
///
public string GetName()
{
return m_SensorName;
}
}
}