Exemplo n.º 1
0
        /// <summary>
        /// Creates a new <see cref="Library"/>-Instance
        /// </summary>
        /// <param name="parameters">Information used to create the instance</param>
        /// <exception cref="InvalidOperationException">a <see cref="Library"/> is already created</exception>
        /// <exception cref="NullReferenceException"><paramref name="parameters"/> is null</exception>
        public static void Initialize(LibraryParameters parameters)
        {
            Require.NotNull(nameof(parameters), parameters);

            InitializedLock.AcquireWriterLock(Timeout.Infinite);
            try
            {
                if (Handle != null)
                {
                    throw new InvalidOperationException("Library is already initialized");
                }
                if (ProcessExitRegistered == false)
                {
                    AppDomain.CurrentDomain.ProcessExit += ProcessExit;
                    ProcessExitRegistered = true;
                }
                Platform              = parameters.Platform;
                Handle                = SdkHandle.Load(Platform, parameters.PossibleNativeBinaryLocations);
                NativeBinary          = Handle.Location;
                FunctionRarePointers  = parameters.FunctionRarePointers;
                UsedLogTypes          = parameters.UsedLogTypes;
                LogFileFolder         = parameters.LogFileFolder;
                ResourcesFolder       = parameters.ResourcesFolder ?? System.IO.Path.GetDirectoryName(NativeBinary);
                ClientPasswordEncrypt = parameters.ClientPasswordEncrypt;
                CustomPacketEncrypt   = parameters.CustomPacketEncrypt;
                CustomPacketDecrypt   = parameters.CustomPacketDecrypt;
                Events                = new NativeEvents(
                    useClientPasswordEncrypt: ClientPasswordEncrypt != null,
                    useCustomPacketEncrypt: CustomPacketEncrypt != null,
                    useCustomPacketDecrypt: CustomPacketDecrypt != null);
                ConnectionsCache   = new ConcurrentDictionary <ulong, Connection>();
                CustomDevicesCache = new List <CustomDevice>();

                if (ResourcesFolder == null)
                {
                    ResourcesFolder = string.Empty;
                }
                else if (ResourcesFolder.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString(), StringComparison.InvariantCulture) == false)
                {
                    ResourcesFolder += System.IO.Path.DirectorySeparatorChar;
                }

                NativeMethods nativeMethods = new NativeMethods(Handle);
                nativeMethods.InitClientLib(Events.ClientUIFunctions, FunctionRarePointers, UsedLogTypes, LogFileFolder, ResourcesFolder);
                NativeMethods = nativeMethods;
            }
            catch
            {
                Handle?.Dispose();
                Handle = null;
                throw;
            }
            finally
            {
                InitializedLock.ReleaseWriterLock();
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Releases the unmanaged resources used by the <see cref="Library"/>
 /// </summary>
 public static void Destroy()
 {
     InitializedLock.AcquireWriterLock(Timeout.Infinite);
     try
     {
         Handle?.Dispose();
         Handle = null;
     }
     finally
     {
         InitializedLock.ReleaseWriterLock();
     }
 }