Skip to content

Commit

Permalink
some abstractions
Browse files Browse the repository at this point in the history
  • Loading branch information
AnakinRaW committed May 31, 2024
1 parent d434ab6 commit 62dcd3b
Show file tree
Hide file tree
Showing 18 changed files with 110 additions and 26 deletions.
3 changes: 3 additions & 0 deletions src/ModVerify.CliApp/FodyWeavers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<Costura />
</Weavers>
4 changes: 4 additions & 0 deletions src/ModVerify.CliApp/ModVerify.CliApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<ItemGroup>
<PackageReference Include="AlamoEngineTools.PG.StarWarsGame.Infrastructure.Clients" Version="3.1.5" />
<PackageReference Include="AlamoEngineTools.SteamAbstraction" Version="3.1.5" />
<PackageReference Include="Costura.Fody" Version="5.7.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
Expand Down
6 changes: 4 additions & 2 deletions src/ModVerify.CliApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static async Task Main(string[] args)
}
}

private static VerifyFocPipeline BuildPipeline(IPlayableObject playableObject, IGame fallbackGame)
private static VerifyGamePipeline BuildPipeline(IPlayableObject playableObject, IGame fallbackGame)
{
IList<string> mods = Array.Empty<string>();
if (playableObject is IMod mod)
Expand All @@ -97,7 +97,9 @@ private static VerifyFocPipeline BuildPipeline(IPlayableObject playableObject, I
playableObject.Game.Directory.FullName,
fallbackGame.Directory.FullName);

return new VerifyFocPipeline(gameLocations, VerificationSettings.Default, _services);
var repo = _services.GetRequiredService<IGameRepositoryFactory>().Create(GameEngineType.Foc, gameLocations);

return new VerifyGamePipeline(repo, VerificationSettings.Default, _services);
}

private static IServiceProvider CreateAppServices()
Expand Down
4 changes: 4 additions & 0 deletions src/ModVerify/GameVerificationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public sealed class GameVerificationException(IEnumerable<GameVerificationStep>
private readonly string? _error = null;
private readonly IEnumerable<IStep> _failedSteps = failedSteps ?? throw new ArgumentNullException(nameof(failedSteps));

public GameVerificationException(GameVerificationStep step) : this([step])
{
}

/// <inheritdoc/>
public override string Message => Error;

Expand Down
4 changes: 4 additions & 0 deletions src/ModVerify/ModVerify.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Required" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
16 changes: 13 additions & 3 deletions src/ModVerify/Steps/GameVerificationStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

namespace AET.ModVerify.Steps;

public abstract class GameVerificationStep(CreateGameDatabaseStep createDatabaseStep, IGameRepository repository, IServiceProvider serviceProvider)
public abstract class GameVerificationStep(
CreateGameDatabaseStep createDatabaseStep,
IGameRepository repository,
VerificationSettings settings,
IServiceProvider serviceProvider)
: PipelineStep(serviceProvider)
{
protected readonly IFileSystem FileSystem = serviceProvider.GetRequiredService<IFileSystem>();
Expand All @@ -22,6 +26,8 @@ public abstract class GameVerificationStep(CreateGameDatabaseStep createDatabase

public IReadOnlyCollection<VerificationError> VerifyErrors => _verifyErrors;

protected VerificationSettings Settings { get; } = settings;

protected GameDatabase Database { get; private set; } = null!;

protected IGameRepository Repository => repository;
Expand All @@ -34,14 +40,16 @@ protected sealed override void RunCore(CancellationToken token)
{
createDatabaseStep.Wait();
Database = createDatabaseStep.GameDatabase;


Logger?.LogInformation($"Running verifier '{Name}'...");
try
{
_errorLog = CreateVerificationLogFile();
RunVerification(token);
}
finally
{
Logger?.LogInformation($"Finished verifier '{Name}'");
_errorLog.Dispose();
_errorLog = null!;
}
Expand All @@ -57,9 +65,11 @@ protected void AddError(VerificationError error)
Logger?.LogTrace($"Error suppressed: '{error}'");
return;
}

_verifyErrors.Add(error);
_errorLog.WriteLine(error.Message);

if (Settings.ThrowBehavior == VerifyThrowBehavior.FailFast)
throw new GameVerificationException(this);
}

protected virtual bool OnError(VerificationError error)
Expand Down
8 changes: 6 additions & 2 deletions src/ModVerify/Steps/VerifyReferencedModelsStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@

namespace AET.ModVerify.Steps;

internal sealed class VerifyReferencedModelsStep(CreateGameDatabaseStep createDatabaseStep, IGameRepository repository, IServiceProvider serviceProvider)
: GameVerificationStep(createDatabaseStep, repository, serviceProvider)
internal sealed class VerifyReferencedModelsStep(
CreateGameDatabaseStep createDatabaseStep,
IGameRepository repository,
VerificationSettings settings,
IServiceProvider serviceProvider)
: GameVerificationStep(createDatabaseStep, repository, settings, serviceProvider)
{
public const string ModelNotFound = "ALO00";
public const string ModelBroken = "ALO01";
Expand Down
11 changes: 11 additions & 0 deletions src/ModVerify/VerificationSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace AET.ModVerify;

public record VerificationSettings
{
public static readonly VerificationSettings Default = new()
{
ThrowBehavior = VerifyThrowBehavior.None
};

public VerifyThrowBehavior ThrowBehavior { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,26 @@

namespace AET.ModVerify;


public record VerificationSettings
{
public static readonly VerificationSettings Default = new();

public bool ThrowOnError { get; init; }
}


public class VerifyFocPipeline : ParallelPipeline
public class VerifyGamePipeline : ParallelPipeline
{
private IList<GameVerificationStep> _verificationSteps = null!;
private readonly GameLocations _gameLocations;
private readonly IGameRepository _repository;
private readonly VerificationSettings _settings;

public VerifyFocPipeline(GameLocations gameLocations, VerificationSettings settings, IServiceProvider serviceProvider)
public VerifyGamePipeline(IGameRepository gameRepository, VerificationSettings settings, IServiceProvider serviceProvider)
: base(serviceProvider, 4, false)
{
_gameLocations = gameLocations;
_repository = gameRepository;
_settings = settings;
}

protected override Task<IList<IStep>> BuildSteps()
{
var repository = new FocGameRepository(_gameLocations, ServiceProvider);
var buildIndexStep = new CreateGameDatabaseStep(_repository, ServiceProvider);

var buildIndexStep = new CreateGameDatabaseStep(repository, ServiceProvider);
_verificationSteps = new List<GameVerificationStep>
{
new VerifyReferencedModelsStep(buildIndexStep, repository, ServiceProvider),
new VerifyReferencedModelsStep(buildIndexStep, _repository, _settings, ServiceProvider),
};

var allSteps = new List<IStep>
Expand Down Expand Up @@ -71,7 +61,7 @@ public override async Task RunAsync(CancellationToken token = default)
}
}

if (_settings.ThrowOnError && failedSteps.Count > 0)
if (_settings.ThrowBehavior == VerifyThrowBehavior.FinalThrow && failedSteps.Count > 0)
throw new GameVerificationException(stepsWithVerificationErrors);
}
finally
Expand Down
8 changes: 8 additions & 0 deletions src/ModVerify/VerifyThrowBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AET.ModVerify;

public enum VerifyThrowBehavior
{
None,
FinalThrow,
FailFast
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class FocGameRepository : IGameRepository

private readonly IVirtualMegArchive? _masterMegArchive;

public GameEngineType EngineType => GameEngineType.Foc;

public IRepository EffectsRepository { get; }

public FocGameRepository(GameLocations gameLocations, IServiceProvider serviceProvider)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace PG.StarWarsGame.Engine.FileSystem;

internal sealed class GameRepositoryFactory(IServiceProvider serviceProvider) : IGameRepositoryFactory
{
public IGameRepository Create(GameEngineType engineType, GameLocations gameLocations)
{
if (engineType == GameEngineType.Eaw)
throw new NotImplementedException("Empire at War is currently not supported.");
return new FocGameRepository(gameLocations, serviceProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

public interface IGameRepository : IRepository
{
IRepository EffectsRepository { get; }
public GameEngineType EngineType { get; }

bool FileExists(string filePath, string[] extensions, bool megFileOnly = false);
IRepository EffectsRepository { get; }

bool FileExists(string filePath, string[] extensions, bool megFileOnly = false);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace PG.StarWarsGame.Engine.FileSystem;

public interface IGameRepositoryFactory
{
IGameRepository Create(GameEngineType engineType, GameLocations gameLocations);
}
2 changes: 2 additions & 0 deletions src/PetroglyphTools/PG.StarWarsGame.Engine/GameDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace PG.StarWarsGame.Engine;

public class GameDatabase
{
public required GameEngineType EngineType { get; init; }

public required GameConstants GameConstants { get; init; }

public required IList<GameObject> GameObjects { get; init; }
Expand Down
16 changes: 16 additions & 0 deletions src/PetroglyphTools/PG.StarWarsGame.Engine/GameEngineType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace PG.StarWarsGame.Engine;

/// <summary>
/// The type of engine can be either "Empire at War" of "Forces of Corruption"
/// </summary>
public enum GameEngineType
{
/// <summary>
/// Empire at War
/// </summary>
Eaw = 0,
/// <summary>
/// Forces of Corruption
/// </summary>
Foc = 1
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using PG.StarWarsGame.Engine.FileSystem;
using PG.StarWarsGame.Engine.Language;
using PG.StarWarsGame.Engine.Xml;

Expand All @@ -8,6 +9,7 @@ public static class PetroglyphEngineServiceContribution
{
public static void ContributeServices(IServiceCollection serviceCollection)
{
serviceCollection.AddSingleton<IGameRepositoryFactory>(sp => new GameRepositoryFactory(sp));
serviceCollection.AddSingleton<IGameLanguageManager>(sp => new GameLanguageManager(sp));
serviceCollection.AddSingleton<IPetroglyphXmlFileParserFactory>(sp => new PetroglyphXmlFileParserFactory(sp));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ protected override async Task RunCoreAsync(CancellationToken token)

GameDatabase = new GameDatabase
{
EngineType = repository.EngineType,
GameConstants = _parseGameConstants.Database,
GameObjects = _parseGameObjects.Database
};
Expand Down

0 comments on commit 62dcd3b

Please sign in to comment.