Beispiel #1
0
        private float CalculateDistance()
        {
            // Orbit distance
            var v1 = Utilities.Project2D(ValueOriginal.inverse.GetPosition(), Vector3.forward);

            v1.Normalize();

            var v2 = Utilities.Project2D(ValueTarget.inverse.GetPosition(), Vector3.forward);

            v2.Normalize();

            var angle = Mathf.Abs(Vector3.Angle(v1, v2));

            // Rotate distance
            v1 = Utilities.Project2D(ValueOriginal.GetPosition(), Vector3.forward);
            v1.Normalize();

            v2 = Utilities.Project2D(ValueTarget.GetPosition(), Vector3.forward);
            v2.Normalize();

            var angle2 = Mathf.Abs(Vector3.Angle(v1, v2));

            if (angle2 > angle)
            {
                angle = angle2;
            }

            var radius = (ValueOriginal.inverse.GetPosition().magnitude + ValueTarget.inverse.GetPosition().magnitude) / 2;

            var arcDist  = radius * angle;
            var lineDist = (ValueTarget.inverse.GetPosition() - ValueOriginal.inverse.GetPosition()).magnitude;

            return((lineDist > arcDist) ? lineDist : arcDist);
        }
Beispiel #2
0
        private IEnumerable <Instruction> CopySet(TypeReference type, ValueSource source, ValueTarget target)
        {
            var typeOfArgument = type.GetGenericArguments().Single();

            var list = new List <Instruction>();

            using (new IfNotNull(list, source, target.IsTargetingBase))
            {
                VariableDefinition variable = null;
                if (!target.IsTargetingBase)
                {
                    list.AddRange(NewInstance(type, typeof(ISet <>), typeof(HashSet <>), out variable));
                }

                using (var forEach = new ForEach(this, list, type, source))
                {
                    list.AddRange(Copy(typeOfArgument,
                                       ValueSource.New().Variable(forEach.Current),
                                       ValueTarget.New().Instance(variable).Callvirt(ImportMethod(type.Resolve(), nameof(ISet <object> .Add), typeOfArgument)).Add(OpCodes.Pop)));
                }

                if (!target.IsTargetingBase)
                {
                    list.AddRange(target.Build(variable));
                }
            }

            return(list);
        }
Beispiel #3
0
        private void AddDeepConstructor(TypeDefinition type)
        {
            var constructor = new MethodDefinition(ConstructorName, ConstructorAttributes, TypeSystem.VoidReference);

            constructor.Parameters.Add(new ParameterDefinition(type));

            var processor = constructor.Body.GetILProcessor();

            Func <TypeReference, IEnumerable <Instruction> > baseCopyFunc = null;

            if (type.BaseType.Resolve().MetadataToken == TypeSystem.ObjectDefinition.MetadataToken)
            {
                processor.Emit(OpCodes.Ldarg_0);
                processor.Emit(OpCodes.Call, ImportDefaultConstructor(TypeSystem.ObjectDefinition));
            }
            else if (IsCopyConstructorAvailable(type.BaseType, out var baseConstructor))
            {
                processor.Emit(OpCodes.Ldarg_0);
                processor.Emit(OpCodes.Ldarg_1);
                processor.Emit(OpCodes.Call, baseConstructor);
            }
            else if (IsType(type.BaseType.GetElementType().Resolve(), typeof(Dictionary <,>)))
            {
                processor.Emit(OpCodes.Ldarg_0);
                processor.Emit(OpCodes.Call, ImportDefaultConstructor(type.BaseType));
                baseCopyFunc = reference => CopyDictionary(reference, ValueSource.New(), ValueTarget.New());
            }
            else if (IsType(type.BaseType.GetElementType().Resolve(), typeof(List <>)))
            {
                processor.Emit(OpCodes.Ldarg_0);
                processor.Emit(OpCodes.Call, ImportDefaultConstructor(type.BaseType));
                baseCopyFunc = reference => CopyList(reference, ValueSource.New(), ValueTarget.New());
            }
            else if (IsType(type.BaseType.GetElementType().Resolve(), typeof(HashSet <>)))
            {
                processor.Emit(OpCodes.Ldarg_0);
                processor.Emit(OpCodes.Call, ImportDefaultConstructor(type.BaseType));
                baseCopyFunc = reference => CopySet(reference, ValueSource.New(), ValueTarget.New());
            }
            else
            {
                throw new NoCopyConstructorFoundException(type.BaseType);
            }

            InsertCopyInstructions(type, constructor.Body, baseCopyFunc);

            processor.Emit(OpCodes.Ret);
            type.Methods.Add(constructor);
        }
Beispiel #4
0
        private bool TryCopy(PropertyDefinition property, out IEnumerable <Instruction> instructions)
        {
            if (property.AnyAttribute(IgnoreDuringDeepCopyAttribute))
            {
                property.CustomAttributes.Remove(property.SingleAttribute(IgnoreDuringDeepCopyAttribute));
                instructions = null;
                return(false);
            }

            if (property.GetMethod == null ||
                property.SetMethod == null && property.GetBackingField() == null)
            {
                instructions = null;
                return(false);
            }

            if (property.AnyAttribute(DeepCopyByReferenceAttribute))
            {
                property.CustomAttributes.Remove(property.SingleAttribute(DeepCopyByReferenceAttribute));
                instructions = new[]
                {
                    Instruction.Create(OpCodes.Ldarg_0),
                    Instruction.Create(OpCodes.Ldarg_1),
                    Instruction.Create(OpCodes.Callvirt, property.GetMethod),
                    property.CreateSetInstruction()
                };
                return(true);
            }

            var source = ValueSource.New().Property(property);
            var target = ValueTarget.New().Property(property);

            var list = new List <Instruction>();

            instructions = list;

            if (property.PropertyType.IsArray)
            {
                using (new IfNotNull(list, source))
                    list.AddRange(CopyArray(property));
                return(true);
            }

            instructions = Copy(property.PropertyType, source, target);
            return(true);
        }
Beispiel #5
0
        public override void Apply()
        {
            //Notifier.AddMessage(StepCountTotal.ToString());
            var f = Mathf.SmoothStep(0, 1, DurationCurrent / Duration);
            //var f = (float)_step / _steps;
            var l = Mathf.Lerp(beginLength, endLength, f);

            var d = Vector3.Lerp(valueBegin.GetPosition(), valueEnd.GetPosition(), f);

            d.Normalize();
            d *= l;

            ValueCurrent =
                MatrixUtilities.CreateRotationMatrix(Quaternion.Slerp(ValueOriginal.GetRotation(), ValueTarget.GetRotation(), f))
                * MatrixUtilities.CreateTranslationMatrix(d);

            transform.position = d;
            transform.rotation = Quaternion.Slerp(ValueOriginal.GetRotation(), ValueTarget.GetRotation(), f);
        }
Beispiel #6
0
        private IEnumerable <Instruction> CopyDictionary(TypeReference type, ValueSource source, ValueTarget target)
        {
            var typesOfArguments = type.GetGenericArguments();
            var typeKeyValuePair = ImportType(typeof(KeyValuePair <,>), typesOfArguments);

            var list = new List <Instruction>();

            using (new IfNotNull(list, source, target.IsTargetingBase))
            {
                VariableDefinition variable = null;
                if (!target.IsTargetingBase)
                {
                    list.AddRange(NewInstance(type, typeof(IDictionary <,>), typeof(Dictionary <,>), out variable));
                }

                using (var forEach = new ForEach(this, list, type, source))
                {
                    var sourceKey   = ValueSource.New().Variable(forEach.Current).Method(ImportMethod(typeKeyValuePair, "get_Key", typesOfArguments));
                    var sourceValue = ValueSource.New().Variable(forEach.Current).Method(ImportMethod(typeKeyValuePair, "get_Value", typesOfArguments));

                    var targetKey = NewVariable(typesOfArguments[0]);
                    list.AddRange(Copy(typesOfArguments[0], sourceKey, ValueTarget.New().Variable(targetKey)));
                    var targetValue = NewVariable(typesOfArguments[1]);
                    list.AddRange(Copy(typesOfArguments[1], sourceValue, ValueTarget.New().Variable(targetValue)));

                    list.Add(variable?.CreateLoadInstruction() ?? Instruction.Create(OpCodes.Ldarg_0));
                    list.Add(targetKey.CreateLoadInstruction());
                    list.Add(targetValue.CreateLoadInstruction());
                    list.Add(Instruction.Create(OpCodes.Callvirt, ImportMethod(type.Resolve(), "set_Item", typesOfArguments)));
                }

                if (!target.IsTargetingBase)
                {
                    list.AddRange(target.Build(ValueSource.New().Variable(variable)));
                }
            }

            return(list);
        }
Beispiel #7
0
        private IEnumerable <Instruction> Copy(TypeReference type, ValueSource source, ValueTarget target)
        {
            var list = new List <Instruction>();

            if (type.IsPrimitive || type.IsValueType)
            {
                list.AddRange(target.Build(source));
                return(list);
            }

            var typeToken = type.Resolve().MetadataToken;

            if (DeepCopyExtensions.TryGetValue(typeToken, out var extensionMethod))
            {
                using (target.Build(list))
                {
                    list.AddRange(source);
                    list.Add(Instruction.Create(OpCodes.Call, extensionMethod));
                }
            }
            else if (typeToken == TypeSystem.StringDefinition.MetadataToken)
            {
                using (target.Build(list, out var next))
                {
                    list.AddRange(source.BuildNullSafe(next));
                    list.Add(Instruction.Create(OpCodes.Call, StringCopy()));
                }
            }
            else if (IsCopyConstructorAvailable(type, out var constructor))
            {
                using (target.Build(list, out var next))
                {
                    list.AddRange(source.BuildNullSafe(next));
                    list.Add(Instruction.Create(OpCodes.Newobj, constructor));
                }
            }
            else if (type.IsImplementing(typeof(IDictionary <,>)))
            {
                list.AddRange(CopyDictionary(type, source, target));
            }
            else if (type.IsImplementing(typeof(IList <>)))
            {
                list.AddRange(CopyList(type, source, target));
            }
            else if (type.IsImplementing(typeof(ISet <>)))
            {
                list.AddRange(CopySet(type, source, target));
            }

            else if (typeToken == TypeSystem.ObjectDefinition.MetadataToken)
            {
                throw new NotSupportedException(type);
            }

            else
            {
                throw new NoCopyConstructorFoundException(type);
            }

            return(list);
        }
Beispiel #8
0
        private IEnumerable <Instruction> CopyArrayItem(PropertyDefinition property, TypeReference elementType, VariableDefinition index)
        {
            if (!elementType.IsPrimitive && !elementType.IsValueType)
            {
                return(Copy(elementType, ValueSource.New().Property(property).Index(index), ValueTarget.New().Property(property).Index(index)));
            }

            var instructions = new List <Instruction>
            {
                Instruction.Create(OpCodes.Ldarg_0),
                Instruction.Create(OpCodes.Call, property.GetMethod),
                Instruction.Create(OpCodes.Ldloc, index),
                Instruction.Create(OpCodes.Ldarg_1),
                Instruction.Create(OpCodes.Callvirt, property.GetMethod),
                Instruction.Create(OpCodes.Ldloc, index),
                Instruction.Create(OpCodes.Ldelem_I4),
                Instruction.Create(OpCodes.Stelem_I4)
            };

            return(instructions);
        }