浏览代码

refactored serialization code

/main
Steven Leal 4 年前
当前提交
b35bc31e
共有 4 个文件被更改,包括 172 次插入111 次删除
  1. 9
      com.unity.perception/Editor/Randomization/Editors/ScenarioBaseEditor.cs
  2. 243
      com.unity.perception/Runtime/Randomization/Scenarios/Scenario.cs
  3. 22
      com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs
  4. 9
      com.unity.perception/Runtime/Randomization/Scenarios/UnitySimulationScenario.cs

9
com.unity.perception/Editor/Randomization/Editors/ScenarioBaseEditor.cs


$"{StaticData.uxmlDir}/ScenarioBaseElement.uxml").CloneTree();
var serializeConstantsButton = m_Root.Q<Button>("serialize-constants");
serializeConstantsButton.clicked += () => m_Scenario.Serialize();
serializeConstantsButton.clicked += () =>
{
m_Scenario.Serialize();
var newConfigFileAsset = AssetDatabase.LoadAssetAtPath<Object>(m_Scenario.defaultConfigFileAssetPath);
EditorGUIUtility.PingObject(newConfigFileAsset);
};
m_Scenario.Deserialize();
m_Scenario.Deserialize(m_Scenario.defaultConfigFilePath);
};
m_RandomizerListPlaceholder = m_Root.Q<VisualElement>("randomizer-list-placeholder");

243
com.unity.perception/Runtime/Randomization/Scenarios/Scenario.cs


using System;
using System.CodeDom;
using UnityEditor;
using UnityEngine.Experimental.Perception.Randomization.Parameters;
using UnityEngine.Experimental.Perception.Randomization.Randomizers;
using UnityEngine.Experimental.Perception.Randomization.Samplers;

public override void Serialize()
{
Directory.CreateDirectory(Application.dataPath + "/StreamingAssets/");
using (var writer = new StreamWriter(serializedConstantsFilePath, false))
writer.Write(SerializeRandomizers());
using (var writer = new StreamWriter(defaultConfigFilePath, false))
writer.Write(SerializeConfig());
static bool IsSubclassOfRawGeneric(Type generic, Type toCheck) {
while (toCheck != null && toCheck != typeof(object)) {
var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
if (generic == cur) {
return true;
}
toCheck = toCheck.BaseType;
}
return false;
string SerializeConfig()
{
var configObj = new JObject
{
["constants"] = SerializeConstants(),
["randomizers"] = SerializeRandomizers()
};
return JsonConvert.SerializeObject(configObj, Formatting.Indented);
string SerializeRandomizers()
JObject SerializeConstants()
var configObj = new JObject();
configObj["constants"] = constantsObj;
return constantsObj;
}
JObject SerializeRandomizers()
{
configObj["randomizers"] = randomizersObj;
var randomizerObj = new JObject();
var parameterFields = randomizer.GetType().GetFields();
foreach (var parameterField in parameterFields)
var randomizerObj = SerializeRandomizer(randomizer);
if (randomizerObj.Count > 0)
randomizersObj.Add(new JProperty(randomizer.GetType().Name, randomizerObj));
}
return randomizersObj;
}
static JObject SerializeRandomizer(Randomizer randomizer)
{
var randomizerObj = new JObject();
var parameterFields = randomizer.GetType().GetFields();
foreach (var parameterField in parameterFields)
{
if (!IsSubclassOfRawGeneric(typeof(NumericParameter<>), parameterField.FieldType))
continue;
var parameter = (Parameter)parameterField.GetValue(randomizer);
var parameterObj = SerializeParameter(parameter);
if (parameterObj.Count > 0)
randomizerObj.Add(new JProperty(parameterField.Name, parameterObj));
}
return randomizerObj;
}
static JObject SerializeParameter(Parameter parameter)
{
var parameterObj = new JObject();
var samplerFields = parameter.GetType().GetFields();
foreach (var samplerField in samplerFields)
{
if (samplerField.FieldType != typeof(ISampler))
continue;
var sampler = (ISampler)samplerField.GetValue(parameter);
var samplerObj = SerializeSampler(sampler);
parameterObj.Add(new JProperty(samplerField.Name, samplerObj));
}
return parameterObj;
}
static JObject SerializeSampler(ISampler sampler)
{
var samplerObj = new JObject();
var fields = sampler.GetType().GetFields();
foreach (var field in fields)
samplerObj.Add(new JProperty(field.Name, field.GetValue(sampler)));
if (sampler.GetType() != typeof(ConstantSampler))
{
var rangeProperty = sampler.GetType().GetProperty("range");
if (rangeProperty != null)
if (!IsSubclassOfRawGeneric(typeof(NumericParameter<>), parameterField.FieldType))
continue;
var parameter = parameterField.GetValue(randomizer);
var parameterObj = new JObject();
var samplerFields = parameter.GetType().GetFields();
foreach (var samplerField in samplerFields)
var range = (FloatRange)rangeProperty.GetValue(sampler);
var rangeObj = new JObject
if (samplerField.FieldType != typeof(ISampler))
continue;
var sampler = (ISampler)samplerField.GetValue(parameter);
var samplerObj = new JObject();
var fields = sampler.GetType().GetFields();
foreach (var field in fields)
samplerObj.Add(new JProperty(field.Name, field.GetValue(sampler)));
if (sampler.GetType() != typeof(ConstantSampler))
{
var rangeProperty = sampler.GetType().GetProperty("range");
if (rangeProperty != null)
{
var range = (FloatRange)rangeProperty.GetValue(sampler);
var rangeObj = new JObject
{
new JProperty("minimum", range.minimum),
new JProperty("maximum", range.maximum)
};
samplerObj.Add(new JProperty("range", rangeObj));
}
}
parameterObj.Add(new JProperty(samplerField.Name, samplerObj));
}
if (parameterObj.Count > 0)
randomizerObj.Add(new JProperty(parameterField.Name, parameterObj));
new JProperty("minimum", range.minimum),
new JProperty("maximum", range.maximum)
};
samplerObj.Add(new JProperty("range", rangeObj));
if (randomizerObj.Count > 0)
randomizersObj.Add(new JProperty(randomizer.GetType().Name, randomizerObj));
return JsonConvert.SerializeObject(configObj, Formatting.Indented);
return samplerObj;
/// <exception cref="ScenarioException"></exception>
public override void Deserialize()
public override void Deserialize(string configFilePath)
if (string.IsNullOrEmpty(serializedConstantsFilePath))
if (string.IsNullOrEmpty(configFilePath))
Debug.Log("No configuration file specified. Running scenario with preset editor authored configuration.");
else if (File.Exists(configFilePath))
Debug.Log("No constants file specified. Running scenario with built in constants.");
}
else if (File.Exists(serializedConstantsFilePath))
{
var jsonText = File.ReadAllText(serializedConstantsFilePath);
// constants = JsonUtility.FromJson<T>(jsonText);
DeserializeRandomizers(jsonText);
Debug.Log($"Deserialized scenario configuration from <a href=\"file:///${configFilePath}\">{configFilePath}</a>");
var jsonText = File.ReadAllText(configFilePath);
DeserializeConfig(jsonText);
{
Debug.LogWarning($"JSON scenario constants file does not exist at path {serializedConstantsFilePath}");
}
Debug.LogError($"A scenario configuration file does not exist at path {defaultConfigFilePath}");
void DeserializeRandomizers(string json)
void DeserializeConfig(string json)
DeserializeConstants(constantsObj);
var randomizersObj = (JObject)jsonObj["randomizers"];
DeserializeRandomizers(randomizersObj);
}
void DeserializeConstants(JObject constantsObj)
{
}
var randomizersObj = (JObject)jsonObj["randomizers"];
void DeserializeRandomizers(JObject randomizersObj)
{
var randomizerTypeMap = new Dictionary<string, Randomizer>();
foreach (var randomizer in randomizers)
randomizerTypeMap.Add(randomizer.GetType().Name, randomizer);

continue;
var randomizer = randomizerTypeMap[randomizerPair.Key];
var randomizerObj = (JObject)randomizerPair.Value;
foreach (var parameterPair in randomizerObj)
DeserializeRandomizer(randomizer, randomizerObj);
}
}
static void DeserializeRandomizer(Randomizer randomizer, JObject randomizerObj)
{
foreach (var parameterPair in randomizerObj)
{
var parameterField = randomizer.GetType().GetField(parameterPair.Key);
if (parameterField == null)
continue;
var parameter = (Parameter)parameterField.GetValue(randomizer);
var parameterObj = (JObject)parameterPair.Value;
DeserializeParameter(parameter, parameterObj);
}
}
static void DeserializeParameter(Parameter parameter, JObject parameterObj)
{
foreach (var samplerPair in parameterObj)
{
var samplerField = parameter.GetType().GetField(samplerPair.Key);
if (samplerField == null)
continue;
var sampler = (ISampler)samplerField.GetValue(parameter);
var samplerObj = (JObject)samplerPair.Value;
DeserializeSampler(sampler, samplerObj);
}
}
static void DeserializeSampler(ISampler sampler, JObject samplerObj)
{
foreach (var samplerFieldPair in samplerObj)
{
if (samplerFieldPair.Key == "range")
var parameterField = randomizer.GetType().GetField(parameterPair.Key);
if (parameterField == null)
continue;
var parameter = (Parameter)parameterField.GetValue(randomizer);
var parameterObj = (JObject)parameterPair.Value;
foreach (var samplerPair in parameterObj)
{
var samplerField = parameter.GetType().GetField(samplerPair.Key);
if (samplerField == null)
continue;
var sampler = (ISampler)samplerField.GetValue(parameter);
var samplerObj = (JObject)samplerPair.Value;
foreach (var samplerFieldPair in samplerObj)
{
if (samplerFieldPair.Key == "range")
{
var rangeObj = (JObject)samplerFieldPair.Value;
sampler.range = new FloatRange(
rangeObj["minimum"].ToObject<float>(), rangeObj["maximum"].ToObject<float>());
}
else
{
var field = sampler.GetType().GetField(samplerFieldPair.Key);
if (field != null)
field.SetValue(sampler, ((JValue)samplerFieldPair.Value).Value);
}
}
}
var rangeObj = (JObject)samplerFieldPair.Value;
sampler.range = new FloatRange(
rangeObj["minimum"].ToObject<float>(), rangeObj["maximum"].ToObject<float>());
}
else
{
var field = sampler.GetType().GetField(samplerFieldPair.Key);
if (field != null)
field.SetValue(sampler, ((JValue)samplerFieldPair.Value).Value);
}
static bool IsSubclassOfRawGeneric(Type generic, Type toCheck) {
while (toCheck != null && toCheck != typeof(object)) {
var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
if (generic == cur) {
return true;
}
toCheck = toCheck.BaseType;
}
return false;
}
}
}

22
com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs


/// <summary>
/// The name of the Json file this scenario's constants are serialized to/from.
/// </summary>
public virtual string serializedConstantsFileName => "constants";
public virtual string configFileName => "constants";
/// <summary>
/// Returns the active parameter scenario in the scene

}
/// <summary>
/// Returns the file location of the JSON serialized constants
/// Returns the asset location of the JSON serialized constants.
/// This API is used for finding the config file using the AssetDatabase API.
public string serializedConstantsFilePath =>
Application.dataPath + "/StreamingAssets/" + serializedConstantsFileName + ".json";
public string defaultConfigFileAssetPath =>
"Assets/StreamingAssets/" + configFileName + ".json";
/// <summary>
/// Returns the absolute file path of the JSON serialized constants
/// </summary>
public string defaultConfigFilePath =>
Application.dataPath + "/StreamingAssets/" + configFileName + ".json";
/// <summary>
/// Returns this scenario's non-typed serialized constants

/// <summary>
/// Deserializes constants saved in a JSON file located at serializedConstantsFilePath
/// </summary>
public abstract void Deserialize();
public abstract void Deserialize(string configFilePath);
/// <summary>
/// This method executed directly after this scenario has been registered and initialized

activeScenario = this;
}
void Start()
{
var randomSeedMetricDefinition = DatasetCapture.RegisterMetricDefinition(

DatasetCapture.ReportMetric(randomSeedMetricDefinition, new[] { genericConstants.randomSeed });
Deserialize();
#if !UNITY_EDITOR
Deserialize(defaultConfigFilePath);
#endif
}
void Update()

9
com.unity.perception/Runtime/Randomization/Scenarios/UnitySimulationScenario.cs


/// <summary>
/// Deserializes this scenario's constants from the Unity Simulation AppParams Json file
/// </summary>
public sealed override void Deserialize()
public sealed override void Deserialize(string configFilePath)
if (Configuration.Instance.IsSimulationRunningInCloud())
constants = Configuration.Instance.GetAppParams<T>();
else
base.Deserialize();
base.Deserialize(Configuration.Instance.IsSimulationRunningInCloud()
? Configuration.Instance.SimulationConfig.app_param_uri
: configFilePath);
currentIteration = constants.instanceIndex;
}
}
正在加载...
取消
保存