Merge pull request #513 from wabbajack-tools/issue-475-512

Rename old log files. Don't overwrite downloads with the same name
This commit is contained in:
Timothy Baldridge 2020-02-12 06:43:51 -07:00 committed by GitHub
commit d9c879ca51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 108 additions and 10 deletions

View File

@ -54,11 +54,15 @@ namespace Wabbajack.Common
Directory.CreateDirectory(Consts.LocalAppDataPath); Directory.CreateDirectory(Consts.LocalAppDataPath);
var programName = Assembly.GetEntryAssembly()?.Location ?? "Wabbajack"; var programName = Assembly.GetEntryAssembly()?.Location ?? "Wabbajack";
LogFile = Path.GetFileNameWithoutExtension(programName) + DateTime.Now.ToString(" yyyy-MM-dd HH_mm_ss") + ".log"; LogFile = Path.GetFileNameWithoutExtension(programName) + ".current.log";
_startTime = DateTime.Now; _startTime = DateTime.Now;
if (LogFile.FileExists()) if (LogFile.FileExists())
File.Delete(LogFile); {
var new_path = Path.GetFileNameWithoutExtension(programName) + (new FileInfo(LogFile)).LastWriteTime.ToString(" yyyy-MM-dd HH_mm_ss") + ".log";
File.Move(LogFile, new_path, MoveOptions.ReplaceExisting);
}
var watcher = new FileSystemWatcher(Consts.LocalAppDataPath); var watcher = new FileSystemWatcher(Consts.LocalAppDataPath);
AppLocalEvents = Observable.Merge(Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>(h => watcher.Changed += h, h => watcher.Changed -= h).Select(e => (FileEventType.Changed, e.EventArgs)), AppLocalEvents = Observable.Merge(Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>(h => watcher.Changed += h, h => watcher.Changed -= h).Select(e => (FileEventType.Changed, e.EventArgs)),
@ -183,6 +187,18 @@ namespace Wabbajack.Common
return sha.Hash.ToBase64(); return sha.Hash.ToBase64();
} }
public static string StringSHA256Hex(this string s)
{
var sha = new SHA256Managed();
using (var o = new CryptoStream(Stream.Null, sha, CryptoStreamMode.Write))
{
using var i = new MemoryStream(Encoding.UTF8.GetBytes(s));
i.CopyTo(o);
}
return sha.Hash.ToHex();
}
public static string FileHash(this string file, bool nullOnIOError = false) public static string FileHash(this string file, bool nullOnIOError = false)
{ {
try try

View File

@ -240,7 +240,7 @@ namespace Wabbajack.Lib
await DownloadMissingArchives(missing); await DownloadMissingArchives(missing);
} }
private async Task DownloadMissingArchives(List<Archive> missing, bool download = true) public async Task DownloadMissingArchives(List<Archive> missing, bool download = true)
{ {
if (download) if (download)
{ {
@ -258,20 +258,30 @@ namespace Wabbajack.Lib
var outputPath = Path.Combine(DownloadFolder, archive.Name); var outputPath = Path.Combine(DownloadFolder, archive.Name);
if (download) if (download)
{
if (outputPath.FileExists())
{
var orig_name = Path.GetFileNameWithoutExtension(archive.Name);
var ext = Path.GetExtension(archive.Name);
var unique_key = archive.State.PrimaryKeyString.StringSHA256Hex();
outputPath = Path.Combine(DownloadFolder, orig_name + "_" + unique_key + "_" + ext);
if (outputPath.FileExists()) if (outputPath.FileExists())
File.Delete(outputPath); File.Delete(outputPath);
}
}
return await DownloadArchive(archive, download); return await DownloadArchive(archive, download, outputPath);
}); });
} }
public async Task<bool> DownloadArchive(Archive archive, bool download) public async Task<bool> DownloadArchive(Archive archive, bool download, string destination = null)
{ {
try try
{ {
var path = Path.Combine(DownloadFolder, archive.Name); if (destination == null)
await archive.State.Download(archive, path); destination = Path.Combine(DownloadFolder, archive.Name);
path.FileHashCached(); await archive.State.Download(archive, destination);
destination.FileHashCached();
} }
catch (Exception ex) catch (Exception ex)

View File

@ -4,6 +4,8 @@ using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading;
using Alphaleonis.Win32.Filesystem; using Alphaleonis.Win32.Filesystem;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using Wabbajack.Common; using Wabbajack.Common;
@ -13,6 +15,7 @@ using Wabbajack.Lib.Downloaders;
using Wabbajack.Lib.LibCefHelpers; using Wabbajack.Lib.LibCefHelpers;
using Wabbajack.Lib.NexusApi; using Wabbajack.Lib.NexusApi;
using Wabbajack.Lib.Validation; using Wabbajack.Lib.Validation;
using Directory = System.IO.Directory;
using File = Alphaleonis.Win32.Filesystem.File; using File = Alphaleonis.Win32.Filesystem.File;
using Game = Wabbajack.Common.Game; using Game = Wabbajack.Common.Game;
@ -465,6 +468,76 @@ namespace Wabbajack.Test
} }
/// <summary>
/// Tests that files from different sources don't overwrite eachother when downloaded by AInstaller
/// </summary>
/// <returns></returns>
[TestMethod]
public async Task DownloadRenamingTests()
{
// Test mode off for this test
Consts.TestMode = false;
var inia = $@"[General]
gameName={Game.SkyrimSpecialEdition.MetaData().MO2ArchiveName}
gameFile=Data/Update.esm";
var statea = (GameFileSourceDownloader.State)await DownloadDispatcher.ResolveArchive(inia.LoadIniString());
var inib = $@"[General]
gameName={Game.SkyrimSpecialEdition.MetaData().MO2ArchiveName}
gameFile=Data/Skyrim.esm";
var stateb = (GameFileSourceDownloader.State)await DownloadDispatcher.ResolveArchive(inib.LoadIniString());
var archivesa = new List<Archive>()
{
new Archive {Hash = statea.Hash, Name = "Download.esm", State = statea}
};
var archivesb = new List<Archive>()
{
new Archive {Hash = stateb.Hash, Name = "Download.esm", State = stateb}
};
if (Directory.Exists("DownloadTests"))
Utils.DeleteDirectory("DownloadTests");
Directory.CreateDirectory("DownloadTests");
var inst = new TestInstaller(null, null, null, "DownloadTests", null);
await inst.DownloadMissingArchives(archivesa, true);
await inst.DownloadMissingArchives(archivesb, true);
CollectionAssert.AreEqual(Directory.EnumerateFiles("DownloadTests").Select(Path.GetFileName).OrderBy(a => a).ToArray(),
new string[]
{
@"Download.esm",
@"Download.esm.xxHash",
@"Download_f80ee6d109516018308f62e2c862b7f061987ac4a8c2327a101ac6b8f80ec4ae_.esm",
@"Download_f80ee6d109516018308f62e2c862b7f061987ac4a8c2327a101ac6b8f80ec4ae_.esm.xxHash"
}.OrderBy(a => a).ToArray());
Consts.TestMode = true;
}
class TestInstaller : AInstaller
{
public TestInstaller(string archive, ModList modList, string outputFolder, string downloadFolder, SystemParameters parameters) : base(archive, modList, outputFolder, downloadFolder, parameters)
{
ConfigureProcessor(1, new Subject<int>().StartWith(1));
}
protected override Task<bool> _Begin(CancellationToken cancel)
{
throw new NotImplementedException();
}
public override ModManager ModManager { get => ModManager.MO2; }
}
} }

View File

@ -80,7 +80,6 @@ namespace Wabbajack.Test
var modlist = await CompileAndInstall(profile); var modlist = await CompileAndInstall(profile);
utils.VerifyAllFiles(); utils.VerifyAllFiles();
var loot_folder = Path.Combine(utils.InstallFolder, "LOOT Config Files"); var loot_folder = Path.Combine(utils.InstallFolder, "LOOT Config Files");