private void LoadState()
        {
            string settings = System.IO.Path.Combine(StoragePath, SettingsFile);

            if (!System.IO.File.Exists(settings))
            {
                return;
            }

            byte[]       buffer = System.IO.File.ReadAllBytes(settings);
            BEncodedList list   = BEncodedValue.Decode <BEncodedList> (buffer);

            List <TorrentData> data = new List <TorrentData>();

            foreach (BEncodedDictionary dict in list)
            {
                TorrentData d = new TorrentData();
                d.Deserialize(dict);
                data.Add(d);
            }

            foreach (TorrentData d in data)
            {
                Console.WriteLine("Reloading: {0}", d.TorrentPath);

                TorrentAdapter torrent = torrents[LoadTorrent(d.TorrentPath)];
                TorrentManager manager = new TorrentManager(torrent.Torrent, d.SavePath, d.Settings, d.FastResume);

                Load(torrent, d.Settings, manager);
            }
        }
        private ObjectPath LoadTorrent(string path)
        {
            Torrent torrent;

            if (System.IO.File.Exists(path))
            {
                torrent = Torrent.Load(path);
            }
            else
            {
                string[] parts    = path.Split('/');
                string   filename = parts.Length > 0 ? System.IO.Path.Combine(StoragePath, parts[parts.Length - 1]) : "";

                torrent = Torrent.Load(new Uri(path), filename);
            }

            foreach (TorrentAdapter t in torrents.Values)
            {
                if (Toolbox.ByteMatch(t.Torrent.InfoHash, torrent.InfoHash))
                {
                    Console.WriteLine("Returning an existing torrent in memory");
                    return(t.Path);
                }
            }

            Console.WriteLine("Loading a torrent from disk");
            ObjectPath     torrentPath = new ObjectPath(string.Format("{0}/torrent{1}", Path.ToString(), torrentNumber++));
            TorrentAdapter tAdapter    = new TorrentAdapter(torrent, torrentPath);

            torrents.Add(tAdapter.Path, tAdapter);
            TorrentService.Bus.Register(tAdapter.Path, tAdapter);
            return(torrentPath);
        }
        public TorrentManagerAdapter(TorrentManager manager, TorrentAdapter torrent, TorrentSettingsAdapter settings, ObjectPath path)
        {
            this.manager = manager;
            this.torrent = torrent;
            this.settingsAdapter = settings;
            this.path = path;

            manager.TorrentStateChanged += delegate (object sender, TorrentStateChangedEventArgs e) {
                if (StateChanged != null)
                    StateChanged (path, EnumAdapter.Adapt(e.OldState), EnumAdapter.Adapt (e.NewState));
            };

            manager.PieceHashed += OnPieceHashed;

            LoadTrackers (manager.TrackerManager.TrackerTiers);
        }
        private ObjectPath Load(TorrentAdapter tAdapter, TorrentSettings settings, TorrentManager manager)
        {
            ObjectPath managerPath  = new ObjectPath(string.Format(DownloaderPath, downloaderNumber++));
            ObjectPath settingsPath = new ObjectPath(string.Format("{0}/settings", managerPath.ToString()));

            TorrentSettingsAdapter sAdapter = new TorrentSettingsAdapter(settings, settingsPath);
            TorrentManagerAdapter  mAdapter = new TorrentManagerAdapter(manager, tAdapter, sAdapter, managerPath);

            TorrentService.Bus.Register(sAdapter.Path, sAdapter);
            TorrentService.Bus.Register(mAdapter.Path, mAdapter);

            engine.Register(manager);
            downloaders.Add(mAdapter.Path, mAdapter);

            return(mAdapter.Path);
        }
        public TorrentManagerAdapter(TorrentManager manager, TorrentAdapter torrent, TorrentSettingsAdapter settings, ObjectPath path)
        {
            this.manager         = manager;
            this.torrent         = torrent;
            this.settingsAdapter = settings;
            this.path            = path;

            manager.TorrentStateChanged += delegate(object sender, TorrentStateChangedEventArgs e) {
                if (StateChanged != null)
                {
                    StateChanged(path, EnumAdapter.Adapt(e.OldState), EnumAdapter.Adapt(e.NewState));
                }
            };

            manager.PieceHashed += OnPieceHashed;

            LoadTrackers(manager.TrackerManager.TrackerTiers);
        }
        private ObjectPath LoadTorrent(string path)
        {
            Torrent torrent;

            if (System.IO.File.Exists (path))
            {
                torrent = Torrent.Load (path);
            }
            else
            {
                string[] parts = path.Split ('/');
                string filename = parts.Length > 0 ? System.IO.Path.Combine (StoragePath, parts[parts.Length - 1]) : "";

                torrent = Torrent.Load (new Uri(path), filename);
            }

            foreach (TorrentAdapter t in torrents.Values)
            {
                if (Toolbox.ByteMatch(t.Torrent.InfoHash, torrent.InfoHash))
                {
                    Console.WriteLine ("Returning an existing torrent in memory");
                    return t.Path;
                }
            }

            Console.WriteLine ("Loading a torrent from disk");
            ObjectPath torrentPath = new ObjectPath (string.Format ("{0}/torrent{1}", Path.ToString (), torrentNumber++));
            TorrentAdapter tAdapter = new TorrentAdapter (torrent, torrentPath);

            torrents.Add (tAdapter.Path, tAdapter);
            TorrentService.Bus.Register (tAdapter.Path, tAdapter);
            return torrentPath;
        }
        private ObjectPath Load(TorrentAdapter tAdapter, TorrentSettings settings, TorrentManager manager)
        {
            ObjectPath managerPath = new ObjectPath (string.Format (DownloaderPath, downloaderNumber++));
            ObjectPath settingsPath = new ObjectPath (string.Format ("{0}/settings", managerPath.ToString ()));

            TorrentSettingsAdapter sAdapter = new TorrentSettingsAdapter(settings, settingsPath);
            TorrentManagerAdapter mAdapter = new TorrentManagerAdapter(manager, tAdapter, sAdapter, managerPath);

            TorrentService.Bus.Register (sAdapter.Path, sAdapter);
            TorrentService.Bus.Register (mAdapter.Path, mAdapter);

            engine.Register (manager);
            downloaders.Add (mAdapter.Path, mAdapter);

            return mAdapter.Path;
        }