MLRat.Plugin.MLPlugin.Load C# (CSharp) Method

Load() public method

public Load ( ) : bool
return bool
        public bool Load()
        {
            try
            {
                int _ServerPluginLength = BitConverter.ToInt32(CombinedBytes, 0);
                int _ClientPluginLength = BitConverter.ToInt32(CombinedBytes, 4);

                byte[] _ServerPluginBytes = new byte[_ServerPluginLength];
                ClientPluginBytes = new byte[_ClientPluginLength];

                Array.Copy(CombinedBytes, 8, _ServerPluginBytes, 0, _ServerPluginLength);
                Array.Copy(CombinedBytes, 8 + _ServerPluginLength, ClientPluginBytes, 0, _ClientPluginLength);

                

                Assembly _serverPlugin = Assembly.Load(_ServerPluginBytes);
                foreach (Type t in _serverPlugin.GetTypes())
                {
                    if (typeof (IServerPlugin).IsAssignableFrom(t))
                    {
                        ServerPlugin = (IServerPlugin)Activator.CreateInstance(t);
                        ServerPluginID = t.GUID;
                        break;
                    }
                }
                if (ServerPlugin == null)
                    throw new Exception("Failed to load Server plugin");

                ClientPluginChecksum = Hash.Md5(ClientPluginBytes);
                Assembly _clientPlugin = Assembly.Load(ClientPluginBytes);
                foreach (Type t in _clientPlugin.GetTypes())
                {
                    if (typeof (IClientPlugin).IsAssignableFrom(t))
                    {
                        ClientPluginID = t.GUID;
                        break;
                    }
                }
                if (ClientPluginID == null)
                    throw new Exception("Failed to load client plugin");

                PluginInfomation = ServerPlugin.PluginInfomation;

                return true;
            }
            catch
            {
                return false;
            }
        }
    }

Usage Example

Ejemplo n.º 1
0
 void LoadPlugin(string path)
 {
     MLPlugin _plugin = null;
     try
     {
         byte[] PluginBytes = File.ReadAllBytes(path);
         _plugin = new MLPlugin(PluginBytes);
         if (!_plugin.Load())
             throw new Exception("Failed to load plugin");
         if (_plugin.ClientPluginID == Guid.Empty)
             throw new Exception("Invalid plugin ID");
         if (LoadedPlugins.ContainsKey(_plugin.ClientPluginID))
             throw new Exception("Client plugin ID match");
         pluginDisplay _display = new pluginDisplay(_plugin);
         _display.Parent = pluginPanel;
         _display.Width = pluginPanel.Width;
         _display.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
         _display.Location = new Point(0, LoadedPlugins.Count*_display.Height);
         pluginPanel.Controls.Add(_display);
         LoadedPlugins.Add(_plugin.ClientPluginID, _plugin);
         Console.WriteLine("Loaded plugin: {0}", _plugin.ClientPluginID.ToString("n"));
         _plugin.ServerPlugin.OnPluginLoad(new MLUiHost(_plugin, OncontextAdd, OnColumnAdd, getImage, LogText));
     }
     catch(Exception ex)
     {
         DisplayException(_plugin, ex);
     }
 }