예제 #1
0
        /// <summary>
        /// Initializes ENet with given startup options.
        /// </summary>
        /// <param name="startupOptions">The startup options.</param>
        public static void Startup(ENetStartupOptions startupOptions)
        {
            ThrowHelper.ThrowIfArgumentNull(startupOptions, nameof(startupOptions));
            startupOptions.CheckValues();

            var allocator = startupOptions.Allocator;

            if (Started)
            {
                return;
            }
            Started = true;

            if (startupOptions.ModulePath != null)
            {
                LibENet.Load(startupOptions.ModulePath);
            }
            else if (startupOptions.ModuleHandle != IntPtr.Zero)
            {
                LibENet.Load(startupOptions.ModuleHandle);
            }
            else
            {
                // load from native dependencies.
                LibENet.Load();
            }

            var linkedVer = LibENet.LinkedVersion();

            s_LinkedVersion = new Version((int)(((linkedVer) >> 16) & 0xFF),
                                          (int)(((linkedVer) >> 8) & 0xFF),
                                          (int)((linkedVer) & 0xFF));

            if (allocator == null)
            {
                if (LibENet.Initialize() != 0)
                {
                    ThrowHelper.ThrowENetInitializationFailed();
                }
            }
            else
            {
                s_Allocator = allocator;

                NativeENetCallbacks callbacks = new NativeENetCallbacks
                {
                    Malloc   = Marshal.GetFunctionPointerForDelegate(s_MemAllocDelegate),
                    Free     = Marshal.GetFunctionPointerForDelegate(s_MemFreeDelegate),
                    NoMemory = Marshal.GetFunctionPointerForDelegate(s_NoMemoryDelegate)
                };

                if (LibENet.InitializeWithCallbacks(linkedVer, &callbacks) != 0)
                {
                    ThrowHelper.ThrowENetInitializationFailed();
                }
            }
        }