diff --git a/src/MonoTorrent.Tests/Client/ClientEngineTests.cs b/src/MonoTorrent.Tests/Client/ClientEngineTests.cs index f361e8558..49529c148 100644 --- a/src/MonoTorrent.Tests/Client/ClientEngineTests.cs +++ b/src/MonoTorrent.Tests/Client/ClientEngineTests.cs @@ -185,6 +185,34 @@ public async Task SaveRestoreState_NoTorrents () Assert.AreEqual (engine.Settings, restoredEngine.Settings); } + [Test] + public async Task SaveRestoreState_OneInMemoryTorrent () + { + var pieceLength = Piece.BlockSize * 4; + using var tmpDir = TempDir.Create (); + + var torrent = TestRig.CreateMultiFileTorrent (TorrentFile.Create (pieceLength, Piece.BlockSize, Piece.BlockSize * 2, Piece.BlockSize * 3), pieceLength, out BEncoding.BEncodedDictionary metadata); + + var engine = new ClientEngine (EngineSettingsBuilder.CreateForTests (cacheDirectory: tmpDir.Path)); + var torrentManager = await engine.AddAsync (torrent, "mySaveDirectory", new TorrentSettingsBuilder { CreateContainingDirectory = true }.ToSettings ()); + await torrentManager.SetFilePriorityAsync (torrentManager.Files[0], Priority.High); + await torrentManager.MoveFileAsync (torrentManager.Files[1], Path.GetFullPath ("some_fake_path.txt")); + + var restoredEngine = await ClientEngine.RestoreStateAsync (await engine.SaveStateAsync ()); + Assert.AreEqual (engine.Settings, restoredEngine.Settings); + Assert.AreEqual (engine.Torrents[0].Torrent.Name, restoredEngine.Torrents[0].Torrent.Name); + Assert.AreEqual (engine.Torrents[0].SavePath, restoredEngine.Torrents[0].SavePath); + Assert.AreEqual (engine.Torrents[0].Settings, restoredEngine.Torrents[0].Settings); + Assert.AreEqual (engine.Torrents[0].InfoHash, restoredEngine.Torrents[0].InfoHash); + Assert.AreEqual (engine.Torrents[0].MagnetLink.ToV1String (), restoredEngine.Torrents[0].MagnetLink.ToV1String ()); + + Assert.AreEqual (engine.Torrents[0].Files.Count, restoredEngine.Torrents[0].Files.Count); + for (int i = 0; i < engine.Torrents.Count; i++) { + Assert.AreEqual (engine.Torrents[0].Files[i].FullPath, restoredEngine.Torrents[0].Files[i].FullPath); + Assert.AreEqual (engine.Torrents[0].Files[i].Priority, restoredEngine.Torrents[0].Files[i].Priority); + } + } + [Test] public async Task SaveRestoreState_OneMagnetLink () { @@ -217,6 +245,7 @@ public async Task SaveRestoreState_OneTorrentFile_ContainingDirectory () var restoredEngine = await ClientEngine.RestoreStateAsync (await engine.SaveStateAsync ()); Assert.AreEqual (engine.Settings, restoredEngine.Settings); + Assert.AreEqual (engine.Torrents[0].Torrent.Name, restoredEngine.Torrents[0].Torrent.Name); Assert.AreEqual (engine.Torrents[0].SavePath, restoredEngine.Torrents[0].SavePath); Assert.AreEqual (engine.Torrents[0].Settings, restoredEngine.Torrents[0].Settings); Assert.AreEqual (engine.Torrents[0].InfoHash, restoredEngine.Torrents[0].InfoHash); @@ -244,6 +273,7 @@ public async Task SaveRestoreState_OneTorrentFile_NoContainingDirectory () var restoredEngine = await ClientEngine.RestoreStateAsync (await engine.SaveStateAsync ()); Assert.AreEqual (engine.Settings, restoredEngine.Settings); + Assert.AreEqual (engine.Torrents[0].Torrent.Name, restoredEngine.Torrents[0].Torrent.Name); Assert.AreEqual (engine.Torrents[0].SavePath, restoredEngine.Torrents[0].SavePath); Assert.AreEqual (engine.Torrents[0].Settings, restoredEngine.Torrents[0].Settings); Assert.AreEqual (engine.Torrents[0].InfoHash, restoredEngine.Torrents[0].InfoHash); diff --git a/src/MonoTorrent/MonoTorrent.Client/ClientEngine.cs b/src/MonoTorrent/MonoTorrent.Client/ClientEngine.cs index 7a7df12ab..cc5dfb7a8 100644 --- a/src/MonoTorrent/MonoTorrent.Client/ClientEngine.cs +++ b/src/MonoTorrent/MonoTorrent.Client/ClientEngine.cs @@ -339,13 +339,28 @@ public Task AddAsync (Torrent torrent, string saveDirectory) public async Task AddAsync (Torrent torrent, string saveDirectory, TorrentSettings settings) { await MainLoop.SwitchThread (); - var metadata = new BEncodedDictionary { + + var editor = new TorrentEditor (new BEncodedDictionary { { "info", BEncodedValue.Decode (torrent.InfoMetadata) } - }; + }); + editor.SetCustom ("name", (BEncodedString) torrent.Name); + + if (torrent.AnnounceUrls.Count > 0) { + if (torrent.AnnounceUrls.Count == 1 && torrent.AnnounceUrls [0].Count == 1) { + editor.Announce = torrent.AnnounceUrls.Single ().Single (); + } else { + foreach (var tier in torrent.AnnounceUrls) { + var list = new List (); + foreach (var tracker in tier) + list.Add (tracker); + editor.Announces.Add (list); + } + } + } var metadataCachePath = Settings.GetMetadataPath (torrent.InfoHash); Directory.CreateDirectory (Path.GetDirectoryName (metadataCachePath)); - File.WriteAllBytes (metadataCachePath, metadata.Encode ()); + File.WriteAllBytes (metadataCachePath, editor.ToDictionary ().Encode ()); return await AddAsync (null, torrent, saveDirectory, settings); }