Пример #1
0
        public static string CallFunctionDelegate(IntPtr nameSpace, IntPtr name, IntPtr argv, int argc,
                                                  out bool result)
        {
            string _nameSpace = Marshal.PtrToStringAnsi(nameSpace);
            string _name      = Marshal.PtrToStringAnsi(name);

            string[] strings = null;
            if (argv != IntPtr.Zero)
            {
                strings = StringMarshal.IntPtrToAnsiStringArray(argv, argc);
            }
            return(EngineCallbacks.CallScriptFunction(_nameSpace, _name, strings, out result));
        }
Пример #2
0
        public static string CallMethodDelegate(IntPtr className, IntPtr classNamespace, uint obj, IntPtr name,
                                                IntPtr argv, int argc,
                                                out bool result)
        {
            string _className      = Marshal.PtrToStringAnsi(className);
            string _classNamespace = Marshal.PtrToStringAnsi(classNamespace);
            string _name           = Marshal.PtrToStringAnsi(name);

            SimObject objectBaseWrapper = Sim.FindObjectById <SimObject>(obj);

            string[] strings = { };
            if (argv != IntPtr.Zero)
            {
                strings = StringMarshal.IntPtrToAnsiStringArray(argv, argc);
            }
            string strRes = EngineCallbacks.CallScriptMethod(_className, _classNamespace, objectBaseWrapper, _name, strings,
                                                             out result);

            return(strRes);
        }
Пример #3
0
        public static IntPtr ToPtr <T>(T[] array)
        {
            if (array is string[] strings)
            {
                return(StringMarshal.StringArrayToIntPtr(strings));
            }


            int size = Marshal.SizeOf <T>();

            IntPtr unmanagedPointer = Marshal.AllocHGlobal(size * array.Length);

            for (int index = 0; index < array.Length; ++index)
            {
                IntPtr elementPointer = new IntPtr((long)unmanagedPointer + size * index);
                Marshal.StructureToPtr(array[index], elementPointer, false);
            }

            return(unmanagedPointer);
        }
Пример #4
0
        public static T[] FromPtr <T>(IntPtr ptr, int size, bool free)
        {
            bool   found = false;
            object ret   = null;

            if (typeof(T) == typeof(byte))
            {
                byte[] managedArray = new byte[size];
                Marshal.Copy(ptr, managedArray, 0, size);
                ret   = managedArray;
                found = true;
            }
            else if (typeof(T) == typeof(char))
            {
                char[] managedArray = new char[size];
                Marshal.Copy(ptr, managedArray, 0, size);
                ret   = managedArray;
                found = true;
            }
            else if (typeof(T) == typeof(short))
            {
                short[] managedArray = new short[size];
                Marshal.Copy(ptr, managedArray, 0, size);
                ret   = managedArray;
                found = true;
            }
            else if (typeof(T) == typeof(int))
            {
                int[] managedArray = new int[size];
                Marshal.Copy(ptr, managedArray, 0, size);
                ret   = managedArray;
                found = true;
            }
            else if (typeof(T) == typeof(uint))
            {
                int[] iArr = FromPtr <int>(ptr, size, free);
                ret = iArr;
            }
            else if (typeof(T) == typeof(long))
            {
                long[] managedArray = new long[size];
                Marshal.Copy(ptr, managedArray, 0, size);
                ret   = managedArray;
                found = true;
            }
            else if (typeof(T) == typeof(float))
            {
                float[] managedArray = new float[size];
                Marshal.Copy(ptr, managedArray, 0, size);
                ret   = managedArray;
                found = true;
            }
            else if (typeof(T) == typeof(double))
            {
                double[] managedArray = new double[size];
                Marshal.Copy(ptr, managedArray, 0, size);
                ret   = managedArray;
                found = true;
            }
            else if (typeof(T) == typeof(string))
            {
                ret   = StringMarshal.IntPtrToStringArray(ptr, size);
                found = true;
            }
            else if (typeof(IEngineStruct).IsAssignableFrom(typeof(T)))
            {
                T[] result     = new T[size];
                int structSize = Marshal.SizeOf <T>();
                for (int i = 0; i < size; ++i)
                {
                    result[i] = Marshal.PtrToStructure <T>(new IntPtr(ptr.ToInt32() + i * structSize));
                }

                ret = result;
            }

            if (found)
            {
                if (free)
                {
                    Marshal.FreeHGlobal(ptr);
                }
                return((T[])ret);
            }

            throw new NotImplementedException();
        }