2020-04-06 20:48:54 +00:00
|
|
|
|
using System.IO;
|
2020-05-05 20:59:47 +00:00
|
|
|
|
using System.Linq;
|
2019-11-06 23:52:48 +00:00
|
|
|
|
using System.Reactive.Subjects;
|
2020-12-31 06:44:42 +00:00
|
|
|
|
using System.Threading;
|
2019-12-06 05:29:17 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2020-04-06 20:48:54 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2019-10-12 22:15:32 +00:00
|
|
|
|
using Wabbajack.Common;
|
2020-01-07 13:50:11 +00:00
|
|
|
|
using Wabbajack.Common.IO;
|
2020-04-06 20:48:54 +00:00
|
|
|
|
using Wabbajack.Common.Serialization.Json;
|
2019-10-16 03:10:34 +00:00
|
|
|
|
using Wabbajack.Lib.Validation;
|
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
|
|
|
|
|
{
|
2020-04-09 20:20:34 +00:00
|
|
|
|
public string FullPath { get; set; } = string.Empty;
|
|
|
|
|
public string Name { get; set; } = string.Empty;
|
2019-11-06 23:52:48 +00:00
|
|
|
|
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,
|
2021-01-01 00:06:56 +00:00
|
|
|
|
Name = e.Name!,
|
2019-11-06 23:52:48 +00:00
|
|
|
|
FullPath = e.FullPath
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (IOException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 20:20:34 +00:00
|
|
|
|
public async Task<AbstractDownloadState?> GetDownloaderState(dynamic archiveINI, bool quickMode)
|
2019-10-12 22:15:32 +00:00
|
|
|
|
{
|
2019-11-21 15:51:57 +00:00
|
|
|
|
var url = archiveINI?.General?.manualURL;
|
2020-04-09 20:20:34 +00:00
|
|
|
|
return url != null ? new State(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
|
|
|
|
{
|
|
|
|
|
}
|
2020-03-22 21:55:31 +00:00
|
|
|
|
|
2020-04-06 20:48:54 +00:00
|
|
|
|
[JsonName("ManualDownloader")]
|
2019-10-12 22:15:32 +00:00
|
|
|
|
public class State : AbstractDownloadState
|
|
|
|
|
{
|
2020-04-09 20:20:34 +00:00
|
|
|
|
public string Url { get; }
|
2020-03-22 21:55:31 +00:00
|
|
|
|
|
2020-04-06 20:48:54 +00:00
|
|
|
|
[JsonIgnore]
|
2020-04-09 20:20:34 +00:00
|
|
|
|
public override object[] PrimaryKey => new object[] { Url };
|
|
|
|
|
|
|
|
|
|
public State(string url)
|
|
|
|
|
{
|
|
|
|
|
Url = url;
|
|
|
|
|
}
|
2020-01-01 16:19:06 +00:00
|
|
|
|
|
2019-10-12 22:15:32 +00:00
|
|
|
|
public override bool IsWhitelisted(ServerWhitelist whitelist)
|
|
|
|
|
{
|
2020-05-05 21:42:01 +00:00
|
|
|
|
return Url == "<TESTING>" || whitelist.AllowedPrefixes.Any(p => Url.StartsWith(p));
|
2019-10-12 22:15:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-25 22:30:43 +00:00
|
|
|
|
public override async Task<bool> Download(Archive a, AbsolutePath destination)
|
2019-10-12 22:15:32 +00:00
|
|
|
|
{
|
2020-02-11 00:30:38 +00:00
|
|
|
|
var (uri, client) = await Utils.Log(await ManuallyDownloadFile.Create(this)).Task;
|
2020-04-09 20:20:34 +00:00
|
|
|
|
var state = new HTTPDownloader.State(uri.ToString()) { Client = client };
|
2020-02-11 00:30:38 +00:00
|
|
|
|
return await state.Download(a, destination);
|
2019-10-12 22:15:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 06:44:42 +00:00
|
|
|
|
public override async Task<bool> Verify(Archive a, CancellationToken? token)
|
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:23:07 +00:00
|
|
|
|
public override string GetManifestURL(Archive a)
|
2020-02-02 12:15:29 +00:00
|
|
|
|
{
|
2020-02-02 12:23:07 +00:00
|
|
|
|
return Url;
|
2020-02-02 12:15:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-11 04:15:53 +00:00
|
|
|
|
public override string[] GetMetaIni()
|
|
|
|
|
{
|
2020-04-09 20:20:34 +00:00
|
|
|
|
return new []
|
|
|
|
|
{
|
2020-01-11 04:15:53 +00:00
|
|
|
|
"[General]",
|
2020-04-09 20:20:34 +00:00
|
|
|
|
$"manualURL={Url}",
|
2020-01-11 04:15:53 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
2019-10-12 22:15:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|