// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
using System;
using FlaxEditor.Scripting;
using FlaxEngine;
using FlaxEngine.Json;
namespace FlaxEditor.Utilities
{
///
/// This structure represents the comparison of one member of an object to the corresponding member of another object.
///
[Serializable, HideInEditor]
public struct MemberComparison
{
///
/// Members path this Comparison compares.
///
public MemberInfoPath MemberPath;
///
/// The value of first object respective member
///
public readonly object Value1;
///
/// The value of second object respective member
///
public readonly object Value2;
///
/// Initializes a new instance of the struct.
///
/// The member.
/// The first value.
/// The second value.
public MemberComparison(ScriptMemberInfo member, object value1, object value2)
{
MemberPath = new MemberInfoPath(member);
Value1 = value1;
Value2 = value2;
}
///
/// Initializes a new instance of the struct.
///
/// The member path.
/// The first value.
/// The second value.
public MemberComparison(MemberInfoPath memberPath, object value1, object value2)
{
MemberPath = memberPath;
Value1 = value1;
Value2 = value2;
}
///
/// Sets the member value. Handles field or property type.
///
/// The instance.
/// The value.
public void SetMemberValue(object instance, object value)
{
var finalMember = MemberPath.GetLastMember(ref instance);
var type = finalMember.Type;
if (value != null && type != ScriptType.Null)
{
if (type.IsEnum)
{
value = Convert.ChangeType(value, Enum.GetUnderlyingType(type.Type));
}
else if (value is long && type.Type == typeof(int))
{
value = (int)(long)value;
}
else if (value is double && type.Type == typeof(float))
{
value = (float)(double)value;
}
}
finalMember.SetValue(instance, value);
}
///
public override string ToString()
{
return MemberPath.Path + ": " + (Value1 ?? "") + (JsonSerializer.ValueEquals(Value1, Value2) ? " == " : " != ") + (Value2 ?? "");
}
}
}