Example #1
0
        public void MemoryCopyTest()
        {
            Random random = new Random();

            int[] source      = new int[random.Next(100000, 200000)];
            int[] destination = new int[source.Length];

            for (int i = 0; i < source.Length; i++)
            {
                source[i] = random.Next();
            }

            ILUtils.MemoryCopy(destination, source, sizeof(int) * source.Length);

            CollectionAssert.AreEqual(source, destination, "Something went wrong while copying the memory from source to destination.");
        }
Example #2
0
        public unsafe void MemoryCopyPointerTest()
        {
            Random random = new Random();

            int length = random.Next(100000, 200000);

            int *source      = stackalloc int[length];
            int *destination = stackalloc int[length];

            for (int i = 0; i < length; i++)
            {
                source[i] = random.Next();
            }

            ILUtils.MemoryCopy(destination, source, sizeof(int) * length);

            for (int i = 0; i < length; i++)
            {
                Assert.AreEqual(source[i], destination[i], "Something went wrong while copying the memory from source to destination.");
            }
        }
Example #3
0
        public static unsafe IntPtr AllocMemoryAndBuildCurve(IEnumerable <CurvePoint> points)
        {
            if (points == null)
            {
                return(IntPtr.Zero);
            }

            var p          = points.ToArray();
            int pointCount = p.Length;

            if (pointCount <= 0)
            {
                return(IntPtr.Zero);
            }

            CurveNative *nativeCurvePtr = (CurveNative *)((void *)Marshal.AllocHGlobal(GetSizeOfCurve(pointCount)));

            nativeCurvePtr->CurvePointsPtr = new IntPtr((CurvePoint *)&nativeCurvePtr[1]);
            nativeCurvePtr->PointCount     = pointCount;
            ILUtils.WriteToMemory(nativeCurvePtr->CurvePointsPtr, p, 0, nativeCurvePtr->PointCount);

            return((IntPtr)nativeCurvePtr);
        }
Example #4
0
 private static int GetSizeOfCurve(int pointCount)
 {
     return(ILUtils.SizeOf <CurvePoint>() + ILUtils.SizeOf <CurvePoint>() * pointCount);
 }
Example #5
0
        /// <summary>
        ///     Calculates DSP settings with respect to 3D parameters.
        /// </summary>
        /// <param name="listener">Represents the point of reception.</param>
        /// <param name="emitter">Represents the sound source.</param>
        /// <param name="flags">Bitwise combination of <see cref="CalculateFlags" /> specifying which 3D parameters to calculate.</param>
        /// <param name="settings">
        ///     Instance of the <see cref="DspSettings" /> class that receives the calculation results.
        /// </param>
        public unsafe void X3DAudioCalculate(Listener listener, Emitter emitter, CalculateFlags flags,
                                             DspSettings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }
            if (listener == null)
            {
                throw new ArgumentNullException("listener");
            }
            if (emitter == null)
            {
                throw new ArgumentNullException("emitter");
            }
            if ((int)flags == 0)
            {
                throw new ArgumentOutOfRangeException("flags");
            }

            if (emitter.ChannelCount > 1 && emitter.ChannelAzimuths == null)
            {
                throw new ArgumentException("No ChannelAzimuths set for the specified emitter. The ChannelAzimuths property must not be null if the ChannelCount of the emitter is bigger than 1.");
            }

            DspSettings.DspSettingsNative nativeSettings = settings.NativeInstance;
            Listener.ListenerNative       nativeListener = listener.NativeInstance;
            Emitter.EmitterNative         nativeEmitter  = emitter.NativeInstance;

            try
            {
                #region setup listener

                //setup listener:
                Cone   listenerCone    = listener.Cone.HasValue ? listener.Cone.Value : default(Cone);
                IntPtr listenerConePtr = listener.Cone.HasValue ? (IntPtr)(&listenerCone) : IntPtr.Zero;
                nativeListener.ConePtr = listenerConePtr;

                #endregion

                #region setup emitter

                //setup emitter
                IntPtr channelAzimuthsPtr = IntPtr.Zero;
                if (emitter.ChannelAzimuths != null && emitter.ChannelAzimuths.Length > 0 && emitter.ChannelCount > 0)
                {
                    const int sizeOfFloat         = sizeof(float);
                    int       channelAzimuthsSize = sizeOfFloat *
                                                    Math.Min(emitter.ChannelCount, emitter.ChannelAzimuths.Length);
                    channelAzimuthsPtr = Marshal.AllocHGlobal(channelAzimuthsSize);
                    ILUtils.WriteToMemory(channelAzimuthsPtr, emitter.ChannelAzimuths, 0,
                                          channelAzimuthsSize / sizeOfFloat);
                }

                Cone   emitterCone    = emitter.Cone.HasValue ? emitter.Cone.Value : default(Cone);
                IntPtr emitterConePtr = emitter.Cone.HasValue ? (IntPtr)(&emitterCone) : IntPtr.Zero;

                nativeEmitter.ChannelAzimuthsPtr = channelAzimuthsPtr;
                nativeEmitter.ConePtr            = emitterConePtr;
                nativeEmitter.LFECurvePtr        = CurveNative.AllocMemoryAndBuildCurve(emitter.LowFrequencyEffectCurve);
                nativeEmitter.LPFDirectCurvePtr  = CurveNative.AllocMemoryAndBuildCurve(emitter.LowPassFilterDirectCurve);
                nativeEmitter.LPFReverbCurvePtr  = CurveNative.AllocMemoryAndBuildCurve(emitter.LowPassFilterReverbCurve);
                nativeEmitter.ReverbCurvePtr     = CurveNative.AllocMemoryAndBuildCurve(emitter.ReverbCurve);
                nativeEmitter.VolumeCurvePtr     = CurveNative.AllocMemoryAndBuildCurve(emitter.VolumeCurve);

                #endregion

                #region setup settings

                //setup settings
                fixed(void *pmc = settings.MatrixCoefficients, pdt = settings.DelayTimes)
                {
                    nativeSettings.MatrixCoefficientsPtr = new IntPtr(pmc);
                    nativeSettings.DelayTimesPtr         = new IntPtr(pdt);

                    #endregion

                    fixed(void *p = &_handle)
                    {
                        X3DAudioCalculate(new IntPtr(p), (IntPtr)(&nativeListener),
                                          (IntPtr)(&nativeEmitter), flags,
                                          new IntPtr(&nativeSettings));
                    }

                    settings.NativeInstance = nativeSettings;
                }
            }
            finally
            {
                nativeEmitter.FreeMemory();
            }
        }
Example #6
0
        /// <summary>
        /// Injects like <see cref="Mono.Cecil.Inject.InjectionDefinition"/>, with less safety measures
        /// and injects a value from a local compiler class' field
        /// </summary>
        /// <returns>The modified self parameter.</returns>
        /// <param name="self">Self.</param>
        /// <param name="injectionMethod">The method to inject.</param>
        /// <param name="callLoc">The instruction position to inject at.</param>
        /// <param name="localClass">The Local class value whose field is to be injecting from.</param>
        /// <param name="fieldInClass">The field reference in local class.</param>
        /// <param name="f">The injection flags.</param>
        /// <param name="localsID">The other local value ids.</param>
        /// <param name="typeFields">The type fields.</param>
        public static MethodDefinition InjectWithLocalFieldParameter(
            this MethodDefinition self,
            MethodDefinition injectionMethod,
            int callLoc,
            int localClass,
            FieldDefinition fieldInClass,
            bool fieldByRef              = false,
            InjectDirection direction    = InjectDirection.Before,
            InjectFlags f                = InjectFlags.None,
            int[] localsID               = null,
            FieldDefinition[] typeFields = null,
            int token = 0
            )
        {
            var flags = f.ToValues();

            if (callLoc == -1)
            {
                throw new ArgumentOutOfRangeException(nameof(callLoc));
            }
            if (flags.PassLocals && localsID == null)
            {
                throw new ArgumentNullException(nameof(localsID));
            }
            if (flags.PassFields && typeFields == null)
            {
                throw new ArgumentNullException(nameof(typeFields));
            }

            var body   = self.Body;
            var il     = body.GetILProcessor();
            var isVoid = self.ReturnType.FullName == "System.Void";
            var inst   = body.Instructions[callLoc];
            var inst2  = inst;

            if (direction == InjectDirection.Before && callLoc != 0)
            {
                Instruction oldIns = ILUtils.CopyInstruction(inst);
                ILUtils.ReplaceInstruction(inst, il.Create(OpCodes.Nop));
                Instruction ins = body.Instructions[callLoc];
                il.InsertAfter(ins, oldIns);
                inst2 = body.Instructions[callLoc + 1];
            }
            else if (direction == InjectDirection.After)
            {
                il.InsertAfter(inst, il.Create(OpCodes.Nop));
                inst2 = body.Instructions[callLoc + 1];
            }

            VariableDefinition returnDef = null;

            if (flags.ModifyReturn && !isVoid)
            {
                body.InitLocals = true;
                returnDef       = new VariableDefinition(self.ReturnType);
                body.Variables.Add(returnDef);
            }
            if (flags.PassTag)
            {
                il.InsertBefore(inst, il.Create(OpCodes.Ldc_I4, token));
            }
            if (flags.PassInvokingInstance)
            {
                il.InsertBefore(inst, il.Create(OpCodes.Ldarg_0));
            }
            if (flags.ModifyReturn && !isVoid)
            {
                il.InsertBefore(inst, il.Create(OpCodes.Ldloca_S, returnDef));
            }
            if (flags.PassLocals)
            {
                foreach (int i in localsID)
                {
                    il.InsertBefore(inst, il.Create(OpCodes.Ldloca_S, (byte)i));
                }
            }
            if (flags.PassFields)
            {
                var memberRefs = typeFields.Select(t => t.Module.ImportReference(t));
                foreach (FieldReference t in memberRefs)
                {
                    il.InsertBefore(inst, il.Create(OpCodes.Ldarg_0));
                    il.InsertBefore(inst, il.Create(OpCodes.Ldflda, t));
                }
            }
            if (flags.PassParameters)
            {
                int prefixCount = Convert.ToInt32(flags.PassTag) +
                                  Convert.ToInt32(flags.PassInvokingInstance) +
                                  Convert.ToInt32(flags.ModifyReturn && !isVoid);
                int localsCount    = flags.PassLocals ? localsID.Length : 0;
                int memberRefCount = flags.PassFields ? typeFields.Length : 0;
                int paramCount     = flags.PassParameters ? self.Parameters.Count : 0;
                int parameters     = injectionMethod.Parameters.Count - prefixCount - localsCount - memberRefCount - 1;
                int icr            = Convert.ToInt32(!self.IsStatic);
                for (int i = 0; i < parameters; i++)
                {
                    il.InsertBefore(
                        inst,
                        il.Create(flags.PassParametersByRef ? OpCodes.Ldarga_S : OpCodes.Ldarg_S, (byte)(i + icr))
                        );
                }
            }
            il.InsertBefore(inst, il.Create(OpCodes.Ldloc_S, (byte)localClass));
            il.InsertBefore(inst, il.Create(fieldByRef ? OpCodes.Ldflda : OpCodes.Ldfld, fieldInClass));
            il.InsertBefore(inst, il.Create(OpCodes.Call, self.Module.ImportReference(injectionMethod)));
            if (flags.ModifyReturn)
            {
                il.InsertBefore(inst, il.Create(OpCodes.Brfalse_S, inst));
                if (!isVoid)
                {
                    il.InsertBefore(inst, il.Create(OpCodes.Ldloc_S, returnDef));
                }
                il.InsertBefore(inst, il.Create(OpCodes.Ret));
            }
            // If we don't use the return value of InjectMethod, pop it from the ES
            else if (injectionMethod.ReturnType.FullName != "System.Void")
            {
                il.InsertBefore(inst, il.Create(OpCodes.Pop));
            }
            if (direction == InjectDirection.After)
            {
                il.Remove(inst2);
            }
            return(self);
        }