// Copyright (c) 2012-2023 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 && type != value.GetType()) { // Convert value to ensure it matches the member type (eg. undo that uses json serializer might return different value type for some cases) if (type.IsEnum) value = Convert.ChangeType(value, Enum.GetUnderlyingType(type.Type)); else if (type.Type == typeof(byte)) value = Convert.ToByte(value); else if (type.Type == typeof(sbyte)) value = Convert.ToSByte(value); else if (type.Type == typeof(short)) value = Convert.ToInt16(value); else if (type.Type == typeof(int)) value = Convert.ToInt32(value); else if (type.Type == typeof(long)) value = Convert.ToInt64(value); else if (type.Type == typeof(int)) value = Convert.ToUInt16(value); else if (type.Type == typeof(uint)) value = Convert.ToUInt32(value); else if (type.Type == typeof(ulong)) value = Convert.ToUInt64(value); else if (type.Type == typeof(float)) value = Convert.ToSingle(value); else if (type.Type == typeof(double)) value = Convert.ToDouble(value); } finalMember.SetValue(instance, value); } /// public override string ToString() { return MemberPath.Path + ": " + (Value1 ?? "") + (JsonSerializer.ValueEquals(Value1, Value2) ? " == " : " != ") + (Value2 ?? ""); } } }