Exemple #1
0
    static void DetectOS()
    {
        if (!IsUnix)
        {
            Console.WriteLine("OS is: Windows");
            style = "windows";
            return;
        }

        IntPtr buf = UnixMarshal.AllocHeap(8192);

        if (uname(buf) != 0)
        {
            Console.WriteLine("Warning: Unable to detect OS");
            UnixMarshal.FreeHeap(buf);
            return;
        }
        string os = Marshal.PtrToStringAnsi(buf);

        Console.WriteLine("OS is: " + os);
        if (os == "Darwin")
        {
            style = "osx";
        }

        UnixMarshal.FreeHeap(buf);
    }
Exemple #2
0
        public static IntPtr Allocate(Operations operations)
        {
            int    operationsSize = SizeOf(operations);
            IntPtr opPtr          = UnixMarshal.AllocHeap(operationsSize);

            Marshal.StructureToPtr(operations, opPtr, false);
            return(opPtr);
        }
Exemple #3
0
 /// <summary>
 /// Constructor for creating a CVar in C#
 /// </summary>
 /// <param name="name">
 /// The name of the cvar <see cref="System.String"/>
 /// </param>
 /// <param name="val">
 /// An initial string value <see cref="System.String"/>
 /// </param>
 public CVar(string name, string val)
 {
     ptr        = UnixMarshal.AllocHeap(sizeof(CVarInfo));
     cvar->name = (sbyte *)UnixMarshal.StringToHeap(name).ToPointer();
     cvar->str  = (sbyte *)UnixMarshal.StringToHeap(val).ToPointer();
     cvar->next = (CVarInfo *)IntPtr.Zero.ToPointer();
     MetaMod.MetaModEngine.engineFunctions.CVarRegister(ptr);
 }
Exemple #4
0
        public void TestPtrToString()
        {
            IntPtr p = UnixMarshal.AllocHeap(1);

            Marshal.WriteByte(p, 0);
            string s = UnixMarshal.PtrToString(p);

            UnixMarshal.FreeHeap(p);
        }
Exemple #5
0
        // возвращает указатель на структуру в куче
        // память по указателю должна быть очищена вручную
        public unsafe IntPtr CVarRegister(string name, string val)
        {
            cvar_t *cvar = (cvar_t *)UnixMarshal.AllocHeap(sizeof(cvar_t)).ToPointer();

            cvar->name = (sbyte *)UnixMarshal.StringToHeap(name).ToPointer();
            cvar->str  = (sbyte *)UnixMarshal.StringToHeap(val).ToPointer();
            cvar->next = (cvar_t *)IntPtr.Zero.ToPointer();

            ef.CVarRegister(cvar);

            return(new IntPtr(cvar));
        }
Exemple #6
0
        public static IntPtr Allocate(string[] args)
        {
            IntPtr argv =
                UnixMarshal.AllocHeap((args.Length + 1) * IntPtr.Size);

            for (int i = 0; i < args.Length; ++i)
            {
                Marshal.WriteIntPtr(
                    argv,
                    i * IntPtr.Size,
                    UnixMarshal.StringToHeap(args[i]));
            }

            Marshal.WriteIntPtr(argv, args.Length * IntPtr.Size, IntPtr.Zero);
            return(argv);
        }
Exemple #7
0
        public static IntPtr AllocPluginInfo(string ifvers, string name, string version, string date, string author, string url, string logtag, PluginLoadTime load, PluginLoadTime unload)
        {
            PluginInfo *pl = (PluginInfo *)UnixMarshal.AllocHeap(sizeof(PluginInfo)).ToPointer();

            pl->ifvers     = (sbyte *)UnixMarshal.StringToHeap(ifvers).ToPointer();
            pl->name       = (sbyte *)UnixMarshal.StringToHeap(name).ToPointer();
            pl->version    = (sbyte *)UnixMarshal.StringToHeap(version).ToPointer();
            pl->date       = (sbyte *)UnixMarshal.StringToHeap(date).ToPointer();
            pl->author     = (sbyte *)UnixMarshal.StringToHeap(author).ToPointer();
            pl->url        = (sbyte *)UnixMarshal.StringToHeap(url).ToPointer();
            pl->logtag     = (sbyte *)UnixMarshal.StringToHeap(logtag).ToPointer();
            pl->loadable   = load;
            pl->unloadable = unload;

            return(new IntPtr((void *)pl));
        }
Exemple #8
0
        public void TestUtf32PtrToString()
        {
            var utf32NativeEndianNoBom = new UTF32Encoding(
                bigEndian: !BitConverter.IsLittleEndian,
                byteOrderMark: false,
                throwOnInvalidCharacters: true
                );

            // assemble a buffer that contains:
            // 1. eight garbage bytes
            // 2. the native-endian UTF-32 string "Hello, World" without BOM
            // 3. four 0 bytes (as a C wide string terminator)
            // 4. the native-endian UTF-32 string "broken" without BOM
            // 5. eight 0 bytes
            // 6. four garbage bytes
            var buf = new List <byte>();

            for (int i = 0; i < 2; ++i)
            {
                buf.Add((byte)0x12);
                buf.Add((byte)0x34);
                buf.Add((byte)0x56);
                buf.Add((byte)0x78);
            }

            buf.AddRange(utf32NativeEndianNoBom.GetBytes("Hello, World"));

            for (int i = 0; i < 4; ++i)
            {
                buf.Add((byte)0x00);
            }

            buf.AddRange(utf32NativeEndianNoBom.GetBytes("broken"));

            for (int i = 0; i < 8; ++i)
            {
                buf.Add((byte)0x00);
            }

            buf.Add((byte)0x12);
            buf.Add((byte)0x34);
            buf.Add((byte)0x56);
            buf.Add((byte)0x78);

            // get the array version of this
            var bufArr = buf.ToArray();

            // allocate a buffer that will contain this string
            IntPtr bufPtr = UnixMarshal.AllocHeap(bufArr.Length);
            string returned;

            try
            {
                // copy it in
                Marshal.Copy(bufArr, 0, bufPtr, bufArr.Length);

                // try getting it back
                returned = UnixMarshal.PtrToString(bufPtr + 8, utf32NativeEndianNoBom);
            }
            finally
            {
                UnixMarshal.FreeHeap(bufPtr);
            }

            Assert.AreEqual("Hello, World", returned);
        }
Exemple #9
0
 public IntPtr Allocate(IntPtr size)
 {
     return(UnixMarshal.AllocHeap(size.ToInt64()));
 }