Beispiel #1
0
        public static int Bootstrap(IntPtr arg, int argLength)
        {
            if (arg == (IntPtr)0)
            {
                Console.WriteLine("Received NULL bootstrap structure");
                return(1);
            }

            var expectedLength = Marshal.SizeOf(typeof(BootstrapArgs));

            if (argLength < expectedLength)
            {
                Console.WriteLine(
                    $"Received bootstrap structure too small - actual={argLength}, expected={expectedLength}");
                return(1);
            }

            if (argLength > expectedLength)
            {
                Console.WriteLine(
                    $"WARNING: Received bootstrap structure bigger than expected - actual={argLength}, expected={expectedLength}");
                Console.WriteLine("         This usually means that native code version is ahead of the managed code");
            }

            NativeFunctions = Marshal.PtrToStructure <BootstrapArgs>(arg);

            AllHandlers handlers;

            handlers.MainLoop  = OnMainLoop;
            handlers.RunScript = OnRunScript;
            handlers.Closure   = OnClosure;
            RegisterHandlers(handlers);

            try
            {
                NWNEventHandler.OnStart();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            return(0);
        }
Beispiel #2
0
        public static bool Init(IntPtr arg, int argLength)
        {
            if (arg == (IntPtr)0)
            {
                Console.WriteLine("Received NULL bootstrap structure");
                return(false);
            }
            int expectedLength = Marshal.SizeOf(typeof(Internal.BootstrapArgs));

            if (argLength < expectedLength)
            {
                Console.WriteLine($"Received bootstrap structure too small - actual={argLength}, expected={expectedLength}");
                return(false);
            }
            if (argLength > expectedLength)
            {
                Console.WriteLine($"WARNING: Received bootstrap structure bigger than expected - actual={argLength}, expected={expectedLength}");
                Console.WriteLine($"         This usually means that native code version is ahead of the managed code");
            }

            NativeFunctions = Marshal.PtrToStructure <Internal.BootstrapArgs>(arg);

            AllHandlers handlers;

            handlers.MainLoop  = MainLoopHandler;
            handlers.RunScript = RunScriptHandler;
            handlers.Closure   = ClosureHandler;

            var    size = Marshal.SizeOf(typeof(AllHandlers));
            IntPtr ptr  = Marshal.AllocHGlobal(size);

            Marshal.StructureToPtr(handlers, ptr, false);
            NativeFunctions.RegisterHandlers(ptr, (uint)size);
            Marshal.FreeHGlobal(ptr);

            return(true);
        }