Skip to content

Commit

Permalink
added addons files tests, fixed archive unpacking, updated to 0.27.1
Browse files Browse the repository at this point in the history
  • Loading branch information
fgsfds committed Nov 21, 2024
1 parent b135e4e commit 12c2961
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>0.27.0</Version>
<Version>0.27.1</Version>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
Expand Down
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Debug" Version="9.0.0" />
<PackageVersion Include="Moq" Version="4.20.72" />
<PackageVersion Include="Projektanker.Icons.Avalonia.MaterialDesign" Version="9.4.3" />
<PackageVersion Include="Roslynator.Analyzers" Version="4.12.9" />
<PackageVersion Include="SharpCompress" Version="0.38.0" />
Expand Down
91 changes: 56 additions & 35 deletions src/Addons/Providers/InstalledAddonsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -887,30 +887,30 @@ or GameEnum.NAM
/// <param name="addonDtos">AddonDto</param>
private string? UnpackIfNeededAndGetAddonDto(string pathToFile, IArchive archive, out List<AddonDto>? addonDtos)
{
string? unpackedTo = null;

if (archive.Entries.Any(static x => x.Key!.Equals("addons.grpinfo", StringComparison.OrdinalIgnoreCase)))
try
{
//need to unpack archive with grpinfo
unpackedTo = Unpack(pathToFile, archive);
addonDtos = null;
return unpackedTo;
}
string? unpackedTo = null;

var addonJsons = archive.Entries.Where(static x => x.Key!.StartsWith("addon") && x.Key!.EndsWith(".json"));
if (archive.Entries.Any(static x => x.Key!.Equals("addons.grpinfo", StringComparison.OrdinalIgnoreCase)))
{
//need to unpack archive with grpinfo
unpackedTo = Unpack(pathToFile, archive);
addonDtos = null;
return unpackedTo;
}

if (addonJsons?.Any() is not true)
{
addonDtos = null;
return null;
}
var addonJsonsInsideArchive = archive.Entries.Where(static x => x.Key!.StartsWith("addon") && x.Key!.EndsWith(".json"));

try
{
using var stream = addonJsons.First().OpenEntryStream();
if (addonJsonsInsideArchive?.Any() is not true)
{
addonDtos = null;
return null;
}

using var addonJsonStream = addonJsonsInsideArchive.First().OpenEntryStream();

var addonDto = JsonSerializer.Deserialize(
stream,
addonJsonStream,
AddonManifestContext.Default.AddonDto
)!;

Expand All @@ -925,29 +925,50 @@ or GameEnum.NAM
//need to unpack addons with custom executables
unpackedTo = Unpack(pathToFile, archive);
}
}
catch
{
addonDtos = null;
return null;
}

List<AddonDto> result = [];
List<AddonDto> result = [];

foreach (var addonJson in addonJsons)
{
using var stream = addonJson.OpenEntryStream();
if (unpackedTo is not null)
{
var unpackedAddonJsons = Directory.GetFiles(unpackedTo, "addon*.json");

var addonDto = JsonSerializer.Deserialize(
stream,
AddonManifestContext.Default.AddonDto
)!;
foreach (var addonJson in unpackedAddonJsons)
{
var text = File.ReadAllText(addonJson);

var addonDto2 = JsonSerializer.Deserialize(
text,
AddonManifestContext.Default.AddonDto
)!;

result.Add(addonDto2);
}
}
else
{
foreach (var addonJson in addonJsonsInsideArchive)
{
using var addonJsonStream2 = addonJson.OpenEntryStream();

var addonDto2 = JsonSerializer.Deserialize(
addonJsonStream2,
AddonManifestContext.Default.AddonDto
)!;

result.Add(addonDto2);
}
}

result.Add(addonDto);
addonDtos = result.Count > 0 ? result : null;
return unpackedTo;
}
catch (Exception ex)
{
_logger.LogCritical(ex, "=== Error while unpacking archive ===");

addonDtos = result.Count > 0 ? result : null;
return unpackedTo;
addonDtos = null;
return null;
}
}

/// <summary>
Expand Down
122 changes: 122 additions & 0 deletions src/Tests/AddonFilesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using Addons.Providers;
using Common;
using Common.Client.Interfaces;
using Common.Interfaces;
using Microsoft.Extensions.Logging;
using Moq;
using System.Reflection;

namespace Tests;

[Collection("Sync")]
public sealed class AddonFilesTests : IDisposable
{
private readonly InstalledAddonsProvider _installedAddonsProvider;
private readonly MethodInfo _getAddonsFromFilesAsync;

public AddonFilesTests()
{
var game = new Mock<IGame>();
var config = new Mock<IConfigProvider>();
_ = config.Setup(x => x.DisabledAutoloadMods).Returns([]);
var logger = new Mock<ILogger>();

_installedAddonsProvider = new(game.Object, config.Object, logger.Object);

Check warning on line 24 in src/Tests/AddonFilesTests.cs

View workflow job for this annotation

GitHub Actions / Build_and_Test_Linux

'InstalledAddonsProvider.InstalledAddonsProvider(IGame, IConfigProvider, ILogger)' is obsolete: 'Don't create directly. Use InstalledAddonsProvider.'

Check warning on line 24 in src/Tests/AddonFilesTests.cs

View workflow job for this annotation

GitHub Actions / Build_and_Test

'InstalledAddonsProvider.InstalledAddonsProvider(IGame, IConfigProvider, ILogger)' is obsolete: 'Don't create directly. Use InstalledAddonsProvider.'
_getAddonsFromFilesAsync = typeof(InstalledAddonsProvider).GetMethod("GetAddonsFromFilesAsync", BindingFlags.NonPublic | BindingFlags.Instance)!;
}

public void Dispose()
{
Directory.Delete("FilesTemp", true);
}


[Fact]
public async Task AddonArchiveTest()
{
_ = Directory.CreateDirectory("FilesTemp");
File.Copy(Path.Combine("Files", "ZippedAddon.zip"), Path.Combine("FilesTemp", "ZippedAddon.zip"));

var pathToFile = Path.Combine(Directory.GetCurrentDirectory(), "FilesTemp", "ZippedAddon.zip");

var result = await (Task<Dictionary<AddonVersion, IAddon>>)_getAddonsFromFilesAsync.Invoke(_installedAddonsProvider, [(object)new List<string>() { pathToFile }])!;

Assert.Equal(2, result.Count);

var a = result.First();
var b = result.Last();

Assert.Equal("blood-voxel-pack", a.Key.Id);
Assert.Equal("p292", a.Key.Version);
Assert.Equal("Voxel Pack", a.Value.Title);

Assert.Equal("blood-voxel-pack-2", b.Key.Id);
Assert.Equal("p292-2", b.Key.Version);
Assert.Equal("Voxel Pack 2", b.Value.Title);

Assert.True(File.Exists(pathToFile));
Assert.False(Directory.Exists(pathToFile.Replace(".zip", "")));
}

[Fact]
public async Task UnpackedAddonTest()
{
_ = Directory.CreateDirectory("FilesTemp");
File.Copy(Path.Combine("Files", "UnpackedAddon.zip"), Path.Combine("FilesTemp", "UnpackedAddon.zip"));

var pathToFile = Path.Combine(Directory.GetCurrentDirectory(), "FilesTemp", "UnpackedAddon.zip");

var result = await (Task<Dictionary<AddonVersion, IAddon>>)_getAddonsFromFilesAsync.Invoke(_installedAddonsProvider, [(object)new List<string>() { pathToFile }])!;

Assert.Equal(2, result.Count);

var a = result.First();
var b = result.Last();

Assert.Equal("blood-voxel-pack", a.Key.Id);
Assert.Equal("p292", a.Key.Version);
Assert.Equal("Voxel Pack", a.Value.Title);

Assert.Equal("blood-voxel-pack-2", b.Key.Id);
Assert.Equal("p292-2", b.Key.Version);
Assert.Equal("Voxel Pack 2", b.Value.Title);

Assert.False(File.Exists(pathToFile));
Assert.True(Directory.Exists(pathToFile.Replace(".zip", "")));
}

[Fact]
public async Task LooseMapTest()
{
_ = Directory.CreateDirectory("FilesTemp");
File.Copy(Path.Combine("Files", "TEST.MAP"), Path.Combine("FilesTemp", "TEST.MAP"));

var pathToFile = Path.Combine(Directory.GetCurrentDirectory(), "FilesTemp", "TEST.MAP");

var result = await (Task<Dictionary<AddonVersion, IAddon>>)_getAddonsFromFilesAsync.Invoke(_installedAddonsProvider, [(object)new List<string>() { pathToFile }])!;

var map = Assert.Single(result);

Assert.Equal("TEST.MAP", map.Key.Id);
Assert.Null(map.Key.Version);
Assert.Equal("TEST.MAP", map.Value.Title);

Assert.True(File.Exists(pathToFile));
}

[Fact]
public async Task GrpInfoTest()
{
_ = Directory.CreateDirectory("FilesTemp");
File.Copy(Path.Combine("Files", "GrpInfoAddon.zip"), Path.Combine("FilesTemp", "GrpInfoAddon.zip"));

var pathToFile = Path.Combine(Directory.GetCurrentDirectory(), "FilesTemp", "GrpInfoAddon.zip");

var result = await (Task<Dictionary<AddonVersion, IAddon>>)_getAddonsFromFilesAsync.Invoke(_installedAddonsProvider, [(object)new List<string>() { pathToFile }])!;

Assert.Empty(result);

Assert.False(File.Exists(pathToFile));
Assert.True(Directory.Exists(pathToFile.Replace(".zip", "")));
}
}
Binary file added src/Tests/Files/GrpInfoAddon.zip
Binary file not shown.
Binary file added src/Tests/Files/TEST.MAP
Binary file not shown.
Binary file added src/Tests/Files/UnpackedAddon.zip
Binary file not shown.
Binary file added src/Tests/Files/ZippedAddon.zip
Binary file not shown.
26 changes: 26 additions & 0 deletions src/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@
<AssemblyName>BuildLauncher.$(MSBuildProjectName)</AssemblyName>
</PropertyGroup>

<ItemGroup>
<None Remove="Files\GrpInfoAddon.zip" />
<None Remove="Files\TEST.MAP" />
<None Remove="Files\UnpackedAddon.zip" />
<None Remove="Files\ZippedAddon.zip" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AddSealed" PrivateAssets="all" />
<PackageReference Include="CommunityToolkit.Diagnostics" />
<PackageReference Include="coverlet.collector" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="Roslynator.Analyzers">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -27,4 +35,22 @@
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<Content Include="Files\TEST.MAP">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Files\UnpackedAddon.zip">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Files\ZippedAddon.zip">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<Content Include="Files\GrpInfoAddon.zip">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>

0 comments on commit 12c2961

Please sign in to comment.