Exemplo n.º 1
0
        static void LoadPlugin(string path)
        {
            MLClientPlugin _plugin = null;

            try
            {
                byte[] PluginBytes = null;

                using (FileStream fs = new FileStream(path, FileMode.Open))
                {
                    using (Rijndael rij = new RijndaelManaged())
                    {
                        string key = Path.GetExtension(path);
                        key = key.Substring(1, key.Length - 1);
                        byte[] encryptionKey = eSock.Hashing.MD5Hash(key);
                        rij.Key = encryptionKey;
                        rij.IV  = encryptionKey;
                        ICryptoTransform crypto = rij.CreateDecryptor();
                        using (MemoryStream ms = new MemoryStream())
                        {
                            byte[] buffer    = new byte[1000];
                            int    bytesread = 0;
                            using (CryptoStream cs = new CryptoStream(fs, crypto, CryptoStreamMode.Read))
                            {
                                while ((bytesread = cs.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    ms.Write(buffer, 0, bytesread);
                                }
                                cs.Close();
                            }
                            PluginBytes = ms.ToArray();
                        }
                    }
                }

                /*
                 * using (Rijndael rij = new RijndaelManaged())
                 * {
                 *  byte[] encryptionKey = eSock.Hashing.MD5Hash(Path.GetExtension(path));
                 *  rij.Key = encryptionKey;
                 *  rij.IV = encryptionKey;
                 *  ICryptoTransform crypto = rij.CreateDecryptor();
                 *  byte[] encrypted = File.ReadAllBytes(path);
                 *
                 *  using (MemoryStream ms = new MemoryStream())
                 *  {
                 *      using (CryptoStream cs = new CryptoStream(ms, crypto, CryptoStreamMode.Write))
                 *      {
                 *          cs.Write(encrypted, 0, encrypted.Length);
                 *          cs.Flush();
                 *          cs.Close();
                 *      }
                 *      PluginBytes = ms.ToArray();
                 *  }
                 *
                 * }
                 */

                _plugin = new MLClientPlugin(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");
                }
                _plugin.Path = path;
                LoadedPlugins.Add(_plugin.ClientPluginID, _plugin);
                Console.WriteLine("Loaded plugin: {0}", _plugin.ClientPluginID.ToString("n"));
                _plugin.ClientPlugin.OnPluginLoad(new MLConnection(_plugin.ClientPluginID, OnSend));
            }
            catch (Exception ex)
            {
                File.Delete(path);
                DisplayException(_plugin, ex);
            }
        }