Skip to content

Commit

Permalink
some refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
AnakinRaW committed Jun 3, 2024
1 parent d861003 commit 003874e
Show file tree
Hide file tree
Showing 34 changed files with 804 additions and 413 deletions.
24 changes: 19 additions & 5 deletions src/ModVerify.CliApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using AET.ModVerify;
using AET.ModVerify.Steps;
using AET.SteamAbstraction;
using AnakinRaW.CommonUtilities.Hashing;
using AnakinRaW.CommonUtilities.Registry;
Expand All @@ -14,7 +15,7 @@
using Microsoft.Extensions.Logging;
using PG.Commons.Extensibility;
using PG.StarWarsGame.Engine;
using PG.StarWarsGame.Engine.FileSystem;
using PG.StarWarsGame.Engine.Database;
using PG.StarWarsGame.Files.ALO;
using PG.StarWarsGame.Files.DAT.Services.Builder;
using PG.StarWarsGame.Files.MEG.Data.Archives;
Expand Down Expand Up @@ -97,9 +98,8 @@ private static VerifyGamePipeline BuildPipeline(IPlayableObject playableObject,
playableObject.Game.Directory.FullName,
fallbackGame.Directory.FullName);

var repo = _services.GetRequiredService<IGameRepositoryFactory>().Create(GameEngineType.Foc, gameLocations);

return new VerifyGamePipeline(repo, VerificationSettings.Default, _services);
return new ModVerifyPipeline(GameEngineType.Foc, gameLocations, VerificationSettings.Default, _services);
}

private static IServiceProvider CreateAppServices()
Expand All @@ -121,8 +121,9 @@ private static IServiceProvider CreateAppServices()
RuntimeHelpers.RunClassConstructor(typeof(IMegArchive).TypeHandle);
AloServiceContribution.ContributeServices(serviceCollection);
serviceCollection.CollectPgServiceContributions();

PetroglyphEngineServiceContribution.ContributeServices(serviceCollection);
ModVerifyServiceContribution.ContributeServices(serviceCollection);

return serviceCollection.BuildServiceProvider();
}
Expand All @@ -139,5 +140,18 @@ private static void ConfigureLogging(ILoggingBuilder loggingBuilder)
#endif
loggingBuilder.AddConsole();
}

}

internal class ModVerifyPipeline(
GameEngineType targetType,
GameLocations gameLocations,
VerificationSettings settings,
IServiceProvider serviceProvider)
: VerifyGamePipeline(targetType, gameLocations, settings, serviceProvider)
{
protected override IEnumerable<GameVerificationStep> CreateVerificationSteps(IGameDatabase database)
{
var verifyProvider = ServiceProvider.GetRequiredService<IVerificationProvider>();
return verifyProvider.GetAllDefaultVerifiers(database, Settings);
}
}
10 changes: 10 additions & 0 deletions src/ModVerify/IVerificationProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using AET.ModVerify.Steps;
using PG.StarWarsGame.Engine.Database;

namespace AET.ModVerify;

public interface IVerificationProvider
{
IEnumerable<GameVerificationStep> GetAllDefaultVerifiers(IGameDatabase database, VerificationSettings settings);
}
11 changes: 11 additions & 0 deletions src/ModVerify/ModVerifyServiceContribution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.Extensions.DependencyInjection;

namespace AET.ModVerify;

public static class ModVerifyServiceContribution
{
public static void ContributeServices(IServiceCollection serviceCollection)
{
serviceCollection.AddSingleton<IVerificationProvider>(sp => new VerificationProvider(sp));
}
}
8 changes: 4 additions & 4 deletions src/ModVerify/Steps/GameVerificationStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
using AnakinRaW.CommonUtilities.SimplePipeline.Steps;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PG.StarWarsGame.Engine;
using PG.StarWarsGame.Engine.FileSystem;
using PG.StarWarsGame.Engine.Database;
using PG.StarWarsGame.Engine.Repositories;

namespace AET.ModVerify.Steps;

public abstract class GameVerificationStep(
GameDatabase gameDatabase,
IGameDatabase gameDatabase,
VerificationSettings settings,
IServiceProvider serviceProvider)
: PipelineStep(serviceProvider)
Expand All @@ -26,7 +26,7 @@ public abstract class GameVerificationStep(

protected VerificationSettings Settings { get; } = settings;

protected GameDatabase Database { get; } = gameDatabase ?? throw new ArgumentNullException(nameof(gameDatabase));
protected IGameDatabase Database { get; } = gameDatabase ?? throw new ArgumentNullException(nameof(gameDatabase));

protected IGameRepository Repository => gameDatabase.GameRepository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using PG.Commons.Files;
using PG.Commons.Utilities;
using PG.StarWarsGame.Engine;
using PG.StarWarsGame.Engine.Database;
using PG.StarWarsGame.Files.ALO.Files.Models;
using PG.StarWarsGame.Files.ALO.Files.Particles;
using PG.StarWarsGame.Files.ALO.Services;
Expand All @@ -16,7 +17,7 @@
namespace AET.ModVerify.Steps;

internal sealed class VerifyReferencedModelsStep(
GameDatabase database,
IGameDatabase database,
VerificationSettings settings,
IServiceProvider serviceProvider)
: GameVerificationStep(database, settings, serviceProvider)
Expand All @@ -42,7 +43,7 @@ protected override void RunVerification(CancellationToken token)
var aloQueue = new Queue<string>(Database.GameObjects
.SelectMany(x => x.Models)
.Concat(FocHardcodedConstants.HardcodedModels));

var visitedAloFiles = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

while (aloQueue.Count != 0)
Expand Down
14 changes: 14 additions & 0 deletions src/ModVerify/VerificationProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using AET.ModVerify.Steps;
using PG.StarWarsGame.Engine.Database;

namespace AET.ModVerify;

internal class VerificationProvider(IServiceProvider serviceProvider) : IVerificationProvider
{
public IEnumerable<GameVerificationStep> GetAllDefaultVerifiers(IGameDatabase database, VerificationSettings settings)
{
yield return new VerifyReferencedModelsStep(database, settings, serviceProvider);
}
}
2 changes: 2 additions & 0 deletions src/ModVerify/VerificationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public record VerificationSettings
{
public int ParallelWorkers { get; init; } = 4;

public static readonly VerificationSettings Default = new()
{
ThrowBehavior = VerifyThrowBehavior.None
Expand Down
70 changes: 39 additions & 31 deletions src/ModVerify/VerifyGamePipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,70 @@
using System.Threading.Tasks;
using AET.ModVerify.Steps;
using AnakinRaW.CommonUtilities.SimplePipeline;
using AnakinRaW.CommonUtilities.SimplePipeline.Runners;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PG.StarWarsGame.Engine.FileSystem;
using PG.StarWarsGame.Engine;
using PG.StarWarsGame.Engine.Database;
using PG.StarWarsGame.Engine.Pipeline;

namespace AET.ModVerify;

public abstract class VerifyGamePipeline : Pipeline
{
private IList<GameVerificationStep> _verificationSteps = new List<GameVerificationStep>();
private readonly List<GameVerificationStep> _verificationSteps = new();
private readonly GameEngineType _targetType;
private readonly GameLocations _gameLocations;
private readonly VerificationSettings _settings;
private readonly ParallelRunner _verifyRunner;

protected VerifyGamePipeline(GameLocations gameLocations, VerificationSettings settings, IServiceProvider serviceProvider)
protected VerificationSettings Settings { get; }

protected VerifyGamePipeline(GameEngineType targetType, GameLocations gameLocations, VerificationSettings settings, IServiceProvider serviceProvider)
: base(serviceProvider)
{
_targetType = targetType;
_gameLocations = gameLocations ?? throw new ArgumentNullException(nameof(gameLocations));
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
Settings = settings ?? throw new ArgumentNullException(nameof(settings));

if (settings.ParallelWorkers is < 0 or > 64)
throw new ArgumentException("Settings has invalid parallel worker number.", nameof(settings));

_verifyRunner = new ParallelRunner(settings.ParallelWorkers, serviceProvider);
}


protected override Task<bool> PrepareCoreAsync()
protected sealed override Task<bool> PrepareCoreAsync()
{
throw new NotImplementedException();
_verificationSteps.Clear();
return Task.FromResult(true);
}

protected override async Task RunCoreAsync(CancellationToken token)
protected sealed override async Task RunCoreAsync(CancellationToken token)
{
Logger?.LogInformation("Verifying game...");
try
{
var databaseService = ServiceProvider.GetRequiredService<IGameDatabaseService>();

await databaseService.CreateDatabaseAsync()
var database = await databaseService.CreateDatabaseAsync(_targetType, _gameLocations, token);

foreach (var gameVerificationStep in CreateVerificationSteps(database))
{
_verifyRunner.AddStep(gameVerificationStep);
_verificationSteps.Add(gameVerificationStep);
}

try
{
Logger?.LogInformation("Verifying...");
_verifyRunner.Error += OnError;
await _verifyRunner.RunAsync(token);
}
finally
{
_verifyRunner.Error -= OnError;
Logger?.LogInformation("Finished Verifying");
}

var stepsWithVerificationErrors = _verificationSteps.Where(x => x.VerifyErrors.Any()).ToList();

Expand All @@ -54,7 +82,7 @@ await databaseService.CreateDatabaseAsync()
}
}

if (_settings.ThrowBehavior == VerifyThrowBehavior.FinalThrow && failedSteps.Count > 0)
if (Settings.ThrowBehavior == VerifyThrowBehavior.FinalThrow && failedSteps.Count > 0)
throw new GameVerificationException(stepsWithVerificationErrors);
}
finally
Expand All @@ -63,25 +91,5 @@ await databaseService.CreateDatabaseAsync()
}
}


//protected sealed override async Task<IList<IStep>> BuildSteps()
//{
// var buildIndexStep = new CreateGameDatabaseStep(_repository, ServiceProvider);

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

// var allSteps = new List<IStep>
// {
// buildIndexStep
// };
// allSteps.AddRange(CreateVeificationSteps());

// return allSteps;
//}


protected abstract IEnumerable<GameVerificationStep> CreateVerificationSteps();
protected abstract IEnumerable<GameVerificationStep> CreateVerificationSteps(IGameDatabase database);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Collections.Generic;
using PG.StarWarsGame.Engine.DataTypes;
using PG.StarWarsGame.Engine.FileSystem;
using PG.StarWarsGame.Engine.Repositories;

namespace PG.StarWarsGame.Engine;
namespace PG.StarWarsGame.Engine.Database;

public class GameDatabase
internal class GameDatabase : IGameDatabase
{
public required IGameRepository GameRepository { get; init; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;
using PG.StarWarsGame.Engine.DataTypes;
using PG.StarWarsGame.Engine.Repositories;

namespace PG.StarWarsGame.Engine.Database;

public interface IGameDatabase
{
public IGameRepository GameRepository { get; }

public GameConstants GameConstants { get; }

public IList<GameObject> GameObjects { get; }
}

This file was deleted.

Loading

0 comments on commit 003874e

Please sign in to comment.