|
|
|
|
|
|
foreach (var randomizer in randomizers) |
|
|
|
{ |
|
|
|
var randomizerData = SerializeRandomizer(randomizer); |
|
|
|
if (randomizerData.items.Count == 0) |
|
|
|
if (randomizerData.items.Count == 0 && !randomizerData.state.canBeSwitchedByUser) |
|
|
|
continue; |
|
|
|
serializedRandomizers.Add(randomizer.GetType().Name, randomizerData); |
|
|
|
} |
|
|
|
|
|
|
static Group SerializeRandomizer(Randomizer randomizer) |
|
|
|
{ |
|
|
|
var randomizerData = new Group(); |
|
|
|
randomizerData.state.enabled = randomizer.enabled; |
|
|
|
randomizerData.state.canBeSwitchedByUser = randomizer.enabledStateCanBeSwitchedByUser; |
|
|
|
|
|
|
|
var fields = randomizer.GetType().GetFields(); |
|
|
|
foreach (var field in fields) |
|
|
|
{ |
|
|
|
|
|
|
randomizerData.items.Add(field.Name, new Scalar { value = scalarValue }); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
var properties = randomizer.GetType().GetProperties(); |
|
|
|
foreach (var property in properties) |
|
|
|
{ |
|
|
|
if (property.PropertyType.IsSubclassOf(typeof(Randomization.Parameters.Parameter))) |
|
|
|
{ |
|
|
|
if (!IsSubclassOfRawGeneric(typeof(NumericParameter<>), property.PropertyType)) |
|
|
|
continue; |
|
|
|
var parameter = (Randomization.Parameters.Parameter)property.GetValue(randomizer); |
|
|
|
var parameterData = SerializeParameter(parameter); |
|
|
|
if (parameterData.items.Count == 0) |
|
|
|
continue; |
|
|
|
randomizerData.items.Add(property.Name, parameterData); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
var scalarValue = ScalarFromProperty(property, randomizer); |
|
|
|
if (scalarValue != null) |
|
|
|
randomizerData.items.Add(property.Name, new Scalar { value = scalarValue }); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return randomizerData; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
static IScalarValue ScalarFromProperty(PropertyInfo property, object obj) |
|
|
|
{ |
|
|
|
if (property.PropertyType == typeof(string)) |
|
|
|
return new StringScalarValue { str = (string)property.GetValue(obj) }; |
|
|
|
if (property.PropertyType == typeof(bool)) |
|
|
|
return new BooleanScalarValue { boolean = (bool)property.GetValue(obj) }; |
|
|
|
if (property.PropertyType == typeof(float) || property.PropertyType == typeof(double) || property.PropertyType == typeof(int)) |
|
|
|
return new DoubleScalarValue { num = Convert.ToDouble(property.GetValue(obj)) }; |
|
|
|
|
|
|
|
//Properties cannot have a Range attribute like fields so no need to check for it here (see function similar to this for fields)
|
|
|
|
|
|
|
|
return null; |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Deserialization
|
|
|
|
|
|
|
|
|
|
|
static void DeserializeRandomizer(Randomizer randomizer, Group randomizerData) |
|
|
|
{ |
|
|
|
randomizer.enabled = randomizerData.state.enabled; |
|
|
|
|
|
|
|
if (field != null) |
|
|
|
{ |
|
|
|
if (pair.Value is Parameter parameterData) |
|
|
|
DeserializeParameter((Randomization.Parameters.Parameter)field.GetValue(randomizer), parameterData); |
|
|
|
else |
|
|
|
DeserializeScalarValue(randomizer, field, (Scalar)pair.Value); |
|
|
|
if (field == null) |
|
|
|
} |
|
|
|
|
|
|
|
var property = randomizer.GetType().GetProperty(pair.Key); |
|
|
|
if (property != null) |
|
|
|
{ |
|
|
|
if (pair.Value is Parameter parameterData) |
|
|
|
DeserializeParameter((Randomization.Parameters.Parameter)property.GetValue(randomizer), parameterData); |
|
|
|
else |
|
|
|
DeserializeScalarValue(randomizer, property, (Scalar)pair.Value); |
|
|
|
} |
|
|
|
if (pair.Value is Parameter parameterData) |
|
|
|
DeserializeParameter((Randomization.Parameters.Parameter)field.GetValue(randomizer), parameterData); |
|
|
|
else |
|
|
|
DeserializeScalarValue(randomizer, field, (Scalar)pair.Value); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|