#2194 Set Archives to Hidden in MO2

If meta exists, check if file is valid or not and depending on that make a new one or append to the existing one.
If no file exists create one.
The tag added is `removed=true`
This commit is contained in:
EzioTheDeadPoet 2022-11-09 19:56:32 +01:00
parent a76c80e42b
commit 1c813650e9
2 changed files with 55 additions and 0 deletions

View File

@ -148,6 +148,8 @@ public class StandardInstaller : AInstaller<StandardInstaller>
SetScreenSizeInPrefs(); SetScreenSizeInPrefs();
SetDownloadMetasToHideInMO2(token);
await ExtractedModlistFolder!.DisposeAsync(); await ExtractedModlistFolder!.DisposeAsync();
await _wjClient.SendMetric(MetricNames.FinishInstall, ModList.Name); await _wjClient.SendMetric(MetricNames.FinishInstall, ModList.Name);
@ -270,6 +272,50 @@ public class StandardInstaller : AInstaller<StandardInstaller>
} }
} }
private async Task SetDownloadMetasToHideInMO2(CancellationToken token)
{
_logger.LogInformation("Updating Metas To Hide");
await _configuration.Downloads.EnumerateFiles()
.PDoAll(async download =>
{
if (download == default) return;
if (download.Extension == Ext.Meta) return;
var linesToWriteNewFile = new HashSet<string>( );
linesToWriteNewFile.Add("[General]");
linesToWriteNewFile.Add("removed=true");
var lineToWriteAppendFile = new HashSet<string> {"removed=true"};
var metaFile = download.WithExtension(Ext.Meta);
if (metaFile.FileExists())
{
try
{
var parsed = metaFile.LoadIniFile();
if (parsed["General"] != null)
{
_logger.LogInformation("Updating {FileName}", metaFile.FileName);
await metaFile.WriteAllLinesAsync(lineToWriteAppendFile,FileMode.Append,token);
}
}
catch (Exception e)
{
_logger.LogInformation("Re-Generating corrupted {FileName}", metaFile.FileName);
await metaFile.WriteAllLinesAsync(linesToWriteNewFile,FileMode.Create,token);
}
}
else //Generate new metas to hide files without metas.
{
_logger.LogInformation("Generating {FileName}", metaFile.FileName);
await metaFile.WriteAllLinesAsync(linesToWriteNewFile,FileMode.Create,token);
}
});
}
private async Task BuildBSAs(CancellationToken token) private async Task BuildBSAs(CancellationToken token)
{ {
var bsas = ModList.Directives.OfType<CreateBSA>().ToList(); var bsas = ModList.Directives.OfType<CreateBSA>().ToList();

View File

@ -166,6 +166,15 @@ public static class AbsolutePathExtensions
await sw.DisposeAsync(); await sw.DisposeAsync();
} }
public static async Task WriteAllLinesAsync(this AbsolutePath file, IEnumerable<string> src,
FileMode fileMode, CancellationToken token)
{
await using var dest = file.Open(fileMode, FileAccess.Write, FileShare.None);
await using var sw = new StreamWriter(dest, Encoding.UTF8);
foreach (var line in src) await sw.WriteLineAsync(line);
await sw.DisposeAsync();
}
public static async ValueTask WriteAllBytesAsync(this AbsolutePath file, Memory<byte> data, public static async ValueTask WriteAllBytesAsync(this AbsolutePath file, Memory<byte> data,
CancellationToken token = default) CancellationToken token = default)
{ {