⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 coreinstancehelper.cs

📁 Gibphone is CSharp Program, it can tell you how to design p2p chat.
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;
using GPCore.Plugins;
using GPCore.Protocols;
using GPCore.Phones;
using GPCore.NetworkProtocols;
using GPCore.Vocoders;

namespace GPCore
{
    public static partial class Core
    {
        internal static object CreateInstance(string typename)
        {
            
            try
            {
               object o = plugman.CreateInstance(typename, System.Reflection.BindingFlags.CreateInstance, new object[] { });
               ListenToEvents(o);

               if (o is CoreEventListener)
               {
                   Listeners.Add(o as CoreEventListener);

                   if (onPluginAdded != null)
                       onPluginAdded(new PluginEventArgs(o.GetType()));
               }
               else if (o is Phone)
               {
                   Phones.Add(o as Phone);
               }
               return o ;
            }
            catch (Exception e)
            {
                Logger.Log(e);
            }

            return null;
        }

        private static void ListenToEvents(object o)
        {
            foreach (EventInfo ev in o.GetType().GetEvents())
                if (ev.EventHandlerType == typeof(GPCore.NotifyCore))
                    ev.AddEventHandler(o, Delegate.CreateDelegate(ev.EventHandlerType, typeof(Core), "Event"));
        }

        /// <summary>
        /// See <see cref="CreateInstance(String,String,String,bool,object[])"/>
        /// </summary>
        public static Protocol CreateInstance(String typename, String username)
        {
            return CreateInstance(typename, username, "", false, null);
        }
        /// <summary>
        /// See <see cref="CreateInstance(String,String,String,bool,object[])"/>
        /// </summary>
        public static Protocol CreateInstance(String typename, String username, String password)
        {
            return CreateInstance(typename, username, password, false, null);
        }
        /// <summary>
        /// See <see cref="CreateInstance(String,String,String,bool,object[])"/>
        /// </summary>
        public static Protocol CreateInstance(String typename, String username, String password, bool autoload)
        {
            return CreateInstance(typename, username, password, autoload, null);
        }
        /// <summary>
        /// Creates an instance of the given <see cref="IProtocol"/> and adds it to the Protocols list.
        /// </summary>
        /// <example>
        /// Creating an <see cref="IProtocol"/>
        /// <code>
        /// void MyMethod()
        /// {
        ///     OscarProtocol prot = Core.CreateInstance("OscarPlugin.OscarProtocol","Username","Password") as OscarProtocol;
        /// }
        /// </code>
        /// </example>
        /// <param name="typename">The FullName of the <see cref="Type"/> to create an instance of.</param>
        /// <param name="username">The Username for this <see cref="IProtocol"/>.</param>
        /// <param name="password">The password for this <see cref="IProtocol"/>, default is "".</param>
        /// <param name="autoload">Whether this <see cref="IProtocol"/> will automatically load on startup. Defaults to false.</param>
        /// <param name="args">Any extra creation paramaters for <see cref="IProtocol"/> creation.</param>
        /// <returns>A reference to the instance.</returns>
        public static Protocol CreateInstance(String typename, String username, String password, bool autoload, object[] args)
        {
            try
            {
                object o = plugman.CreateInstance(typename, System.Reflection.BindingFlags.CreateInstance, new object[] { });

                foreach (EventInfo ev in o.GetType().GetEvents())
                    ev.AddEventHandler(o, Delegate.CreateDelegate(ev.EventHandlerType, typeof(Core), "Event"));

                if (o is Protocol)
                {
                    Protocol p = (o as Protocol);
                    p.Username = username;
                    if ( !string.IsNullOrEmpty(password)  && p is IAuthenticate)
                        (p as IAuthenticate).Password = password;
                    p.Args = args;
                    Protocols.Add(p);
                }

                
                if (onProtocolAdded != null)
                    onProtocolAdded(autoload,password, new ProtocolEventArgs(username, o.GetType()));

                return o as Protocol;
            }
            catch (Exception e)
            {
                Logger.Log(e);
            }
            return null;
        }
        /// <summary>
        /// Signs off and calls the destructor of the given <see cref="IProtocol"/> and removes it from the protocols list.
        /// </summary>
        /// <param name="protocol">The <see cref="IProtocol"/> you want to destroy.</param>
        public static void DestroyInstance(Protocol protocol)
        {
            if (onProtocolDeleted != null)
                onProtocolDeleted(new ProtocolEventArgs(protocol.Username, protocol.GetType()));
            try
            {
                if ( protocol is IAuthenticate)
                    (protocol as IAuthenticate) .SignOff();
                if (protocol is IDisposable)
                    (protocol as IDisposable).Dispose();
            }
            catch { }
            Sql.ExecuteNonQuery("delete from protocols where Username = ? and ProtocolType = ?", protocol.Username, protocol.GetType().FullName);
            Protocols.Remove(protocol);
            protocol = null;
            
            
        }
        /// <summary>
        /// Calls the destructor for the given plugin and removes it from the Listeners list.
        /// </summary>
        /// <param name="listener">The FullName of the <see cref="Type"/> of listener you wish to destroy.</param>
        /// <example>
        /// Closing the Buddylist Plugin:
        /// <code>
        /// void CloseBuddyList()
        /// {
        ///     Core.DestroyInstance(typeof(GPBuddyList).FullName);
        /// }
        /// </code>
        /// </example>
        public static void DestroyInstance(string listener)
        {
            for (int i = 0; i < Listeners.Count; i++)
            {
                if (Listeners[i].GetType().FullName == listener)
                {
                    if (onPluginDeleted != null)
                        onPluginDeleted(new PluginEventArgs(Listeners[i].GetType()));

                    Listeners[i].Dispose();
                    Listeners[i] = null;
                    Listeners.RemoveAt(i);
                }
            }
        }
        /// <summary>
        /// Calls the destructor for the given <see cref="CoreEventListener"/> plugin and removes it from the Listeners list.
        /// </summary>
        /// <param name="listener">The <see cref="Type"/> of <see cref="CoreEventListener"/> you wish to destroy.</param>
        /// <example>
        /// <code>
        /// void Close()
        /// {
        ///     Core.DestroyInstance(typeof(GPBuddyList));
        /// }
        /// </code>
        /// </example>
        public static void DestroyInstance(Type listener) { DestroyInstance(listener.FullName); }
        /// <summary>
        /// Calls the destructor for the given <see cref="CoreEventListener"/> and removes it from the Listeners list.
        /// </summary>
        public static void DestroyInstance(CoreEventListener listener) { DestroyInstance(listener.GetType().FullName); }

        internal class CoreCoreEventListener : CoreEventListener
        {

            public override void Dispose()
            {
                
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -