2019-10-12 22:15:32 +00:00
|
|
|
|
using System;
|
2019-11-06 23:52:48 +00:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
2019-10-12 22:15:32 +00:00
|
|
|
|
using System.Linq;
|
2019-11-06 23:52:48 +00:00
|
|
|
|
using System.Reactive.Linq;
|
|
|
|
|
using System.Reactive.Subjects;
|
2019-12-06 05:29:17 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2019-10-12 22:15:32 +00:00
|
|
|
|
using Wabbajack.Common;
|
2020-01-07 13:50:11 +00:00
|
|
|
|
using Wabbajack.Common.IO;
|
2019-10-16 03:10:34 +00:00
|
|
|
|
using Wabbajack.Lib.Validation;
|
2019-11-06 23:52:48 +00:00
|
|
|
|
using File = System.IO.File;
|
2019-10-12 22:15:32 +00:00
|
|
|
|
|
2019-10-16 03:10:34 +00:00
|
|
|
|
namespace Wabbajack.Lib.Downloaders
|
2019-10-12 22:15:32 +00:00
|
|
|
|
{
|
2019-11-07 00:29:53 +00:00
|
|
|
|
public class ManualDownloader : IDownloader
|
2019-10-12 22:15:32 +00:00
|
|
|
|
{
|
2019-11-06 23:52:48 +00:00
|
|
|
|
private FileSystemWatcher _watcher;
|
|
|
|
|
private Subject<FileEvent> _fileEvents = new Subject<FileEvent>();
|
|
|
|
|
private KnownFolder _downloadfolder;
|
2019-12-22 00:26:51 +00:00
|
|
|
|
public readonly AsyncLock Lock = new AsyncLock();
|
2019-11-06 23:52:48 +00:00
|
|
|
|
|
|
|
|
|
class FileEvent
|
|
|
|
|
{
|
|
|
|
|
public string FullPath { get; set; }
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
public long Size { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ManualDownloader()
|
|
|
|
|
{
|
2019-11-18 17:53:11 +00:00
|
|
|
|
_downloadfolder = new KnownFolder(KnownFolderType.Downloads);
|
2019-11-06 23:52:48 +00:00
|
|
|
|
_watcher = new FileSystemWatcher(_downloadfolder.Path);
|
|
|
|
|
_watcher.Created += _watcher_Created;
|
|
|
|
|
_watcher.Changed += _watcher_Changed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void _watcher_Changed(object sender, FileSystemEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
PublishEvent(e);
|
|
|
|
|
}
|
|
|
|
|
private void _watcher_Created(object sender, FileSystemEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
PublishEvent(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PublishEvent(FileSystemEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_fileEvents.OnNext(new FileEvent
|
|
|
|
|
{
|
|
|
|
|
Size = new FileInfo(e.FullPath).Length,
|
|
|
|
|
Name = e.Name,
|
|
|
|
|
FullPath = e.FullPath
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (IOException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-07 03:50:50 +00:00
|
|
|
|
public async Task<AbstractDownloadState> GetDownloaderState(dynamic archiveINI)
|
2019-10-12 22:15:32 +00:00
|
|
|
|
{
|
2019-11-21 15:51:57 +00:00
|
|
|
|
var url = archiveINI?.General?.manualURL;
|
2020-02-02 12:15:29 +00:00
|
|
|
|
return url != null ? new State { Url = url} : null;
|
2019-10-12 22:15:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-07 02:45:13 +00:00
|
|
|
|
public async Task Prepare()
|
2019-10-12 22:15:32 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class State : AbstractDownloadState
|
|
|
|
|
{
|
2020-02-02 12:15:29 +00:00
|
|
|
|
public string Url { get; set; }
|
|
|
|
|
public override object[] PrimaryKey { get => new object[] {Url}; }
|
2020-01-01 16:19:06 +00:00
|
|
|
|
|
2019-10-12 22:15:32 +00:00
|
|
|
|
public override bool IsWhitelisted(ServerWhitelist whitelist)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-18 19:57:26 +00:00
|
|
|
|
public override async Task<bool> Download(Archive a, string destination)
|
2019-10-12 22:15:32 +00:00
|
|
|
|
{
|
2019-11-06 23:52:48 +00:00
|
|
|
|
var downloader = (ManualDownloader)GetDownloader();
|
2019-11-21 15:51:57 +00:00
|
|
|
|
var absPath = Path.Combine(downloader._downloadfolder.Path, a.Name);
|
2019-12-22 00:26:51 +00:00
|
|
|
|
using (await downloader.Lock.Wait())
|
2019-11-06 23:52:48 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-02-02 12:15:29 +00:00
|
|
|
|
Utils.Log($"You must manually visit {Url} and download {a.Name} file by hand");
|
2019-11-06 23:52:48 +00:00
|
|
|
|
Utils.Log($"Waiting for {a.Name}");
|
|
|
|
|
downloader._watcher.EnableRaisingEvents = true;
|
|
|
|
|
var watcher = downloader._fileEvents
|
|
|
|
|
.Where(f => f.Size == a.Size)
|
|
|
|
|
.Where(f => f.FullPath.FileHash(true) == a.Hash)
|
|
|
|
|
.Buffer(new TimeSpan(0, 5, 0), 1)
|
|
|
|
|
.Select(x => x.FirstOrDefault())
|
|
|
|
|
.FirstOrDefaultAsync();
|
2020-02-02 12:15:29 +00:00
|
|
|
|
Process.Start(Url);
|
2019-12-22 00:26:51 +00:00
|
|
|
|
|
|
|
|
|
absPath = (await watcher)?.FullPath;
|
2019-11-21 15:51:57 +00:00
|
|
|
|
if (!File.Exists(absPath))
|
2019-11-06 23:52:48 +00:00
|
|
|
|
throw new InvalidDataException($"File not found after manual download operation");
|
2019-11-21 15:51:57 +00:00
|
|
|
|
File.Move(absPath, destination);
|
2019-11-06 23:52:48 +00:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
downloader._watcher.EnableRaisingEvents = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-18 19:57:26 +00:00
|
|
|
|
return true;
|
2019-10-12 22:15:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-13 22:55:55 +00:00
|
|
|
|
public override async Task<bool> Verify(Archive a)
|
2019-10-12 22:15:32 +00:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override IDownloader GetDownloader()
|
|
|
|
|
{
|
|
|
|
|
return DownloadDispatcher.GetInstance<ManualDownloader>();
|
|
|
|
|
}
|
2019-10-12 22:54:25 +00:00
|
|
|
|
|
2020-02-02 12:15:29 +00:00
|
|
|
|
public override string GetReportEntry(Archive a)
|
|
|
|
|
{
|
|
|
|
|
return $"* Manual Download - [{a.Name} - {Url}]({Url})";
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-11 04:15:53 +00:00
|
|
|
|
public override string[] GetMetaIni()
|
|
|
|
|
{
|
|
|
|
|
return new [] {
|
|
|
|
|
"[General]",
|
2020-02-02 12:15:29 +00:00
|
|
|
|
$"manualURL={Url}"
|
2020-01-11 04:15:53 +00:00
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
}
|
2019-10-12 22:15:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|