| using System; |
| using System.Reflection; |
|
|
| namespace Unity.MLAgents.Sensors.Reflection |
| { |
| |
| |
| |
| 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; |
| } |
| } |
|
|
| |
| |
| |
| internal abstract class ReflectionSensorBase : ISensor, IBuiltInSensor |
| { |
| protected object m_Object; |
|
|
| |
| protected FieldInfo m_FieldInfo; |
| protected PropertyInfo m_PropertyInfo; |
|
|
| |
| protected ObservableAttribute m_ObservableAttribute; |
|
|
| |
| string m_SensorName; |
| ObservationSpec m_ObservationSpec; |
| int m_NumFloats; |
|
|
| 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_ObservationSpec = ObservationSpec.Vector(size); |
| m_NumFloats = size; |
| } |
|
|
| |
| public ObservationSpec GetObservationSpec() |
| { |
| return m_ObservationSpec; |
| } |
|
|
| |
| public int Write(ObservationWriter writer) |
| { |
| WriteReflectedField(writer); |
| return m_NumFloats; |
| } |
|
|
| internal abstract void WriteReflectedField(ObservationWriter writer); |
|
|
| |
| |
| |
| |
| |
| 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 CompressionSpec GetCompressionSpec() |
| { |
| return CompressionSpec.Default(); |
| } |
|
|
| |
| public string GetName() |
| { |
| return m_SensorName; |
| } |
|
|
| |
| public BuiltInSensorType GetBuiltInSensorType() |
| { |
| return BuiltInSensorType.ReflectionSensor; |
| } |
| } |
| } |
|
|