浏览代码

tested reflection.emit

/dr-test2
Steven Leal 4 年前
当前提交
c2b11b74
共有 7 个文件被更改,包括 99 次插入60 次删除
  1. 36
      com.unity.perception/Editor/Randomization/ParameterConfigurationEditor.cs
  2. 107
      com.unity.perception/Runtime/Randomization/Parameters/Abstractions/Parameter.cs
  3. 1
      com.unity.perception/Runtime/Randomization/Parameters/MonoBehaviours/ParameterConfiguration.cs
  4. 2
      com.unity.perception/Runtime/Randomization/Samplers/Abstractions/SamplerBase.cs
  5. 10
      com.unity.perception/Runtime/Randomization/Scenarios/Abstractions/CsvWriter.cs
  6. 1
      com.unity.perception/Runtime/Randomization/Scenarios/Abstractions/Scenario.cs
  7. 2
      com.unity.perception/Runtime/Randomization/Utilities/RandomUtility.cs

36
com.unity.perception/Editor/Randomization/ParameterConfigurationEditor.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine.Perception.Randomization.Parameters.Abstractions;
using UnityEngine.Perception.Randomization.Samplers.Abstractions;

var targetField = templateClone.Query<PropertyField>("target-field").First();
var propertyMenu = templateClone.Query<ToolbarMenu>("property-select-menu").First();
targetField.RegisterCallback<ChangeEvent<UnityEngine.Object>>(
targetField.RegisterCallback<ChangeEvent<Object>>(
evt =>
{
propertyMenu.text = "";

return options;
}
void AppendActionsToPropertySelectMenu(ParameterBase parameterComponent, ToolbarMenu propertyMenu)
static void AppendActionsToPropertySelectMenu(ParameterBase parameterComponent, ToolbarMenu propertyMenu)
{
propertyMenu.menu.MenuItems().Clear();
if (parameterComponent.target == null) return;

var newSampler = (SamplerBase)m_Config.gameObject.AddComponent(samplerType);
newSampler.parameter = parameterComponent;
newSampler.hideFlags = HideFlags.HideInInspector;
parameterComponent.sampler = (SamplerBase)newSampler;
parameterComponent.sampler = newSampler;
}
void UpdateStatsContainer()

totalFrameCountLabel.text = $"{m_Config.TotalFrameCount}";
}
void CreateSamplerPropertyFields(VisualElement samplerFieldsContainer, SamplerBase sampler)
static void CreateSamplerPropertyFields(VisualElement samplerFieldsContainer, SamplerBase sampler)
{
samplerFieldsContainer.Clear();
var serializedSampler = new SerializedObject(sampler);

static Type[] GetDerivedTypeOptions(Type derivedType)
{
var samplerTypes = (
from domainAssembly in AppDomain.CurrentDomain.GetAssemblies()
from assemblyType in domainAssembly.GetTypes()
where (derivedType.IsAssignableFrom(assemblyType) &&
(assemblyType.Attributes & TypeAttributes.Abstract) == 0)
select assemblyType).ToArray();
return samplerTypes;
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var samplerTypes = new List<Type>();
foreach (var assembly in assemblies)
{
try
{
foreach (var assemblyType in assembly.GetTypes())
{
var isNotAbstract = (assemblyType.Attributes & TypeAttributes.Abstract) == 0;
if (derivedType.IsAssignableFrom(assemblyType) && isNotAbstract)
samplerTypes.Add(assemblyType);
}
}
catch (ReflectionTypeLoadException)
{
// We can safely ignore this exception
}
}
return samplerTypes.ToArray();
}
}
}

107
com.unity.perception/Runtime/Randomization/Parameters/Abstractions/Parameter.cs


using System;
using System.Reflection;
using System.Reflection.Emit;
// using System.Reflection;
// using System.Reflection.Emit;
using UnityEngine.Perception.Randomization.Samplers.Abstractions;
using UnityEngine;

protected override void SetupFieldOrPropertySetters()
{
// if (!hasTarget)
// return;
// var componentType = propertyTarget.targetComponent.GetType();
// switch (propertyTarget.targetKind)
// {
// case TargetKind.Field:
// var fieldInfo = componentType.GetField(propertyTarget.propertyName);
// m_ApplyParameterDelegate = CreateFieldSetter(fieldInfo, componentType);
// break;
// case TargetKind.Property:
// var propertyInfo = componentType.GetProperty(propertyTarget.propertyName);
// m_ApplyParameterDelegate = CreatePropertySetter(propertyInfo, componentType);
// break;
// }
}
public override void Apply(IterationData data)
{
iterationData = data;
UnreflectiveApply();
// if (!hasTarget)
// return;
// var value = ((Sampler<T>)sampler).NextSample();
// m_ApplyParameterDelegate(propertyTarget.targetComponent, value);
}
void UnreflectiveApply()
{
var value = ((Sampler<T>)sampler).NextSample();
m_ApplyParameterDelegate = CreateFieldSetter(fieldInfo, componentType);
fieldInfo.SetValue(propertyTarget.targetComponent, value);
m_ApplyParameterDelegate = CreatePropertySetter(propertyInfo, componentType);
propertyInfo.SetValue(propertyTarget.targetComponent, value);
public override void Apply(IterationData data)
{
iterationData = data;
if (!hasTarget)
return;
var value = ((Sampler<T>)sampler).NextSample();
m_ApplyParameterDelegate(propertyTarget.targetComponent, value);
}
static Action<Component, T> CreateFieldSetter(FieldInfo field, Type componentType)
{
var methodName = field.ReflectedType.FullName + ".set_" + field.Name;
var setterMethod = new DynamicMethod(
methodName, null, new []{ typeof(Component), typeof(T) }, true);
var gen = setterMethod.GetILGenerator();
gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Castclass, componentType);
gen.Emit(OpCodes.Ldarg_1);
gen.Emit(OpCodes.Stfld, field);
gen.Emit(OpCodes.Ret);
var newDelegate = (Action<Component, T>)setterMethod.CreateDelegate(typeof(Action<Component, T>));
return newDelegate;
}
static Action<Component, T> CreatePropertySetter(PropertyInfo property, Type componentType)
{
var methodName = property.ReflectedType.FullName + ".set_" + property.Name;
var setMethod = property.GetSetMethod();
var setterMethod = new DynamicMethod(
methodName, null, new []{ typeof(Component), typeof(T) }, true);
var gen = setterMethod.GetILGenerator();
gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Castclass, componentType);
gen.Emit(OpCodes.Ldarg_1);
gen.Emit(OpCodes.Callvirt, setMethod);
gen.Emit(OpCodes.Ret);
var newDelegate = (Action<Component, T>)setterMethod.CreateDelegate(typeof(Action<Component, T>));
return newDelegate;
}
// static Action<Component, T> CreateFieldSetter(FieldInfo field, Type componentType)
// {
// var methodName = field.ReflectedType.FullName + ".set_" + field.Name;
// var setterMethod = new DynamicMethod(
// methodName, null, new []{ typeof(Component), typeof(T) }, true);
// var gen = setterMethod.GetILGenerator();
// gen.Emit(OpCodes.Ldarg_0);
// gen.Emit(OpCodes.Castclass, componentType);
// gen.Emit(OpCodes.Ldarg_1);
// gen.Emit(OpCodes.Stfld, field);
// gen.Emit(OpCodes.Ret);
// var newDelegate = (Action<Component, T>)setterMethod.CreateDelegate(typeof(Action<Component, T>));
// return newDelegate;
// }
//
// static Action<Component, T> CreatePropertySetter(PropertyInfo property, Type componentType)
// {
// var methodName = property.ReflectedType.FullName + ".set_" + property.Name;
// var setMethod = property.GetSetMethod();
// var setterMethod = new DynamicMethod(
// methodName, null, new []{ typeof(Component), typeof(T) }, true);
// var gen = setterMethod.GetILGenerator();
// gen.Emit(OpCodes.Ldarg_0);
// gen.Emit(OpCodes.Castclass, componentType);
// gen.Emit(OpCodes.Ldarg_1);
// gen.Emit(OpCodes.Callvirt, setMethod);
// gen.Emit(OpCodes.Ret);
// var newDelegate = (Action<Component, T>)setterMethod.CreateDelegate(typeof(Action<Component, T>));
// return newDelegate;
// }
}
}

1
com.unity.perception/Runtime/Randomization/Parameters/MonoBehaviours/ParameterConfiguration.cs


m_IterationState = new int[parameters.Count];
if (scenario == null)
scenario = gameObject.AddComponent<EmptyScenario>();
scenario.parameterConfiguration = this;
}
void Start()

2
com.unity.perception/Runtime/Randomization/Samplers/Abstractions/SamplerBase.cs


{
public abstract class SamplerBase : MonoBehaviour
{
public ParameterBase parameter;
[HideInInspector] public ParameterBase parameter;
public abstract int SampleCount { get; }
public abstract string GetSampleString();

10
com.unity.perception/Runtime/Randomization/Scenarios/Abstractions/CsvWriter.cs


using System;
using System.IO;
using UnityEngine.Perception.Randomization.Parameters.Abstractions;
using UnityEngine.Perception.Randomization.Parameters.MonoBehaviours;
namespace UnityEngine.Perception.Randomization.Scenarios.Abstractions

string m_Delimiter;
StreamWriter m_File;
ParameterConfiguration m_Config;
ParameterBase[] m_SelectedParameters;
string[] additionalHeaders,
ParameterBase[] selectedParameters = null,
string[] additionalHeaders = null,
string delimiter = "\t")
{
m_File = overwriteData

m_Config = config;
m_Delimiter = delimiter;
m_SelectedParameters = selectedParameters ?? m_Config.parameters.ToArray();
WriteHeaders(additionalHeaders);
}

void WriteHeaders(string[] additionalHeaders)
{
var output = $"Global Iteration{m_Delimiter}";
foreach (var parameter in m_Config.parameters)
foreach (var parameter in m_SelectedParameters)
{
output += $"{parameter.parameterName} ({parameter.ParameterTypeName}){m_Delimiter}";
}

public void WriteParameterIterationToFile(string[] additionalData = null)
{
var output = $"{m_Config.GlobalIterationIndex}{m_Delimiter}";
foreach (var parameter in m_Config.parameters)
foreach (var parameter in m_SelectedParameters)
{
output += $"{parameter.sampler.GetSampleString()}{m_Delimiter}";
}

1
com.unity.perception/Runtime/Randomization/Scenarios/Abstractions/Scenario.cs


{
public abstract class Scenario : MonoBehaviour
{
[HideInInspector]
public ParameterConfiguration parameterConfiguration;
public abstract int FrameCount { get; }
public virtual void Setup() { }

2
com.unity.perception/Runtime/Randomization/Utilities/RandomUtility.cs


public static uint CombineSeeds(uint seed1, uint seed2)
{
return seed1 ^ seed2;
return seed1 * seed2;
}
}
}
正在加载...
取消
保存