Skip to content

Commit

Permalink
start parsing sfxevents and duplicate checker
Browse files Browse the repository at this point in the history
  • Loading branch information
AnakinRaW committed Jun 8, 2024
1 parent 003874e commit b89074f
Show file tree
Hide file tree
Showing 36 changed files with 647 additions and 184 deletions.
49 changes: 49 additions & 0 deletions src/ModVerify/Steps/DuplicateFinderStep.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Linq;
using System.Threading;
using AnakinRaW.CommonUtilities.Collections;
using PG.StarWarsGame.Engine.Database;
using PG.StarWarsGame.Engine.DataTypes;

namespace AET.ModVerify.Steps;

public sealed class DuplicateFinderStep(
IGameDatabase gameDatabase,
VerificationSettings settings,
IServiceProvider serviceProvider)
: GameVerificationStep(gameDatabase, settings, serviceProvider)
{
public const string DuplicateFound = "DUP00";

protected override string LogFileName => "Duplicates";

public override string Name => "Duplicate Definitions";

protected override void RunVerification(CancellationToken token)
{
CheckDatabaseForDuplicates(Database.GameObjects, "GameObject");
CheckDatabaseForDuplicates(Database.SfxEvents, "SFXEvent");
}

private void CheckDatabaseForDuplicates<T>(IXmlDatabase<T> database, string context) where T : XmlObject
{
foreach (var key in database.EntryKeys)
{
var entries = database.GetEntries(key);
if (entries.Count > 1)
AddError(VerificationError.Create(DuplicateFound, CreateDuplicateErrorMessage(context, entries)));
}
}

private string CreateDuplicateErrorMessage<T>(string context, ReadOnlyFrugalList<T> entries) where T : XmlObject
{
var firstEntry = entries.First();

var message = $"{context} '{firstEntry.Name}' ({firstEntry.Crc32}) has duplicate definitions: ";

foreach (var entry in entries)
message += $"['{entry.Name}' in {entry.Location.XmlFile}] ";

return message.TrimEnd();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace AET.ModVerify.Steps;

internal sealed class VerifyReferencedModelsStep(
public sealed class VerifyReferencedModelsStep(
IGameDatabase database,
VerificationSettings settings,
IServiceProvider serviceProvider)
Expand All @@ -34,13 +34,13 @@ internal sealed class VerifyReferencedModelsStep(

private readonly IAloFileService _modelFileService = serviceProvider.GetRequiredService<IAloFileService>();

protected override string LogFileName => "Model";
protected override string LogFileName => "ModelRefs";

public override string Name => "Model";
public override string Name => "Referenced Models";

protected override void RunVerification(CancellationToken token)
{
var aloQueue = new Queue<string>(Database.GameObjects
var aloQueue = new Queue<string>(Database.GameObjects.Entries
.SelectMany(x => x.Models)
.Concat(FocHardcodedConstants.HardcodedModels));

Expand Down
1 change: 1 addition & 0 deletions src/ModVerify/VerificationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ internal class VerificationProvider(IServiceProvider serviceProvider) : IVerific
public IEnumerable<GameVerificationStep> GetAllDefaultVerifiers(IGameDatabase database, VerificationSettings settings)
{
yield return new VerifyReferencedModelsStep(database, settings, serviceProvider);
yield return new DuplicateFinderStep(database, settings, serviceProvider);
}
}
1 change: 0 additions & 1 deletion src/ModVerify/VerifyGamePipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Microsoft.Extensions.Logging;
using PG.StarWarsGame.Engine;
using PG.StarWarsGame.Engine.Database;
using PG.StarWarsGame.Engine.Pipeline;

namespace AET.ModVerify;

Expand Down
61 changes: 36 additions & 25 deletions src/PetroglyphTools/PG.StarWarsGame.Engine/DataTypes/GameObject.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using PG.Commons.DataTypes;
using PG.Commons.Hashing;
using PG.StarWarsGame.Files.XML;

namespace PG.StarWarsGame.Engine.DataTypes;

public sealed class GameObject(string type, string name, Crc32 nameCrc, GameObjectType estimatedType, ValueListDictionary<string, object> properties, XmlLocationInfo location)
: IHasCrc32
public sealed class GameObject : XmlObject
{
public string Type { get; } = type ?? throw new ArgumentNullException(nameof(type));
private readonly IReadOnlyValueListDictionary<string, object> _properties;

public string Name { get; } = name ?? throw new ArgumentNullException(nameof(name));
internal GameObject(
string type,
string name,
Crc32 nameCrc,
GameObjectType estimatedType,
IReadOnlyValueListDictionary<string, object> properties,
XmlLocationInfo location)
: base(name, nameCrc, location)
{
_properties = properties;
Type = type ?? throw new ArgumentNullException(nameof(type));
EstimatedType = estimatedType;
}

public Crc32 Crc32 { get; } = nameCrc;
public string Type { get; }

public GameObjectType EstimatedType { get; } = estimatedType;

public XmlLocationInfo Location { get; } = location;
public GameObjectType EstimatedType { get; }

/// <summary>
/// Gets all model files (including particles) the game object references.
Expand All @@ -27,22 +36,24 @@ public ISet<string> Models
{
get
{
var models = properties.AggregateValues<string>(new HashSet<string>
{
"Galactic_Model_Name",
"Destroyed_Galactic_Model_Name",
"Land_Model_Name",
"Space_Model_Name",
"Model_Name",
"Tactical_Model_Name",
"Galactic_Fleet_Override_Model_Name",
"GUI_Model_Name",
"GUI_Model",
// This can either be a model or a unit reference
"Land_Model_Anim_Override_Name",
"xxxSpace_Model_Name",
"Damaged_Smoke_Asset_Name"
}, v => v.EndsWith(".alo", StringComparison.OrdinalIgnoreCase));
var models = _properties.AggregateValues<string, object, string>
(new HashSet<string>
{
"Galactic_Model_Name",
"Destroyed_Galactic_Model_Name",
"Land_Model_Name",
"Space_Model_Name",
"Model_Name",
"Tactical_Model_Name",
"Galactic_Fleet_Override_Model_Name",
"GUI_Model_Name",
"GUI_Model",
// This can either be a model or a unit reference
"Land_Model_Anim_Override_Name",
"xxxSpace_Model_Name",
"Damaged_Smoke_Asset_Name"
}, v => v.EndsWith(".alo", StringComparison.OrdinalIgnoreCase),
ValueListDictionaryExtensions.AggregateStrategy.LastValuePerKey);

var terrainMappedModels = LandTerrainModelMapping?.Select(x => x.Model);
if (terrainMappedModels is null)
Expand All @@ -57,7 +68,7 @@ public ISet<string> Models

private T? GetLastPropertyOrDefault<T>(string tagName, T? defaultValue = default)
{
if (!properties.TryGetLastValue(tagName, out var value))
if (!_properties.TryGetLastValue(tagName, out var value))
return defaultValue;
return (T)value;
}
Expand Down
20 changes: 20 additions & 0 deletions src/PetroglyphTools/PG.StarWarsGame.Engine/DataTypes/SfxEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using PG.Commons.Hashing;
using PG.StarWarsGame.Files.XML;

namespace PG.StarWarsGame.Engine.DataTypes;

public sealed class SfxEvent : XmlObject
{
private int _volumeValue;

Check warning on line 8 in src/PetroglyphTools/PG.StarWarsGame.Engine/DataTypes/SfxEvent.cs

View workflow job for this annotation

GitHub Actions / Build & Test (windows-latest)

Field 'SfxEvent._volumeValue' is never assigned to, and will always have its default value 0

public bool IsPreset { get; }

public SfxEvent? Preset { get; }

public int Volume => Preset?.Volume ?? _volumeValue;

internal SfxEvent(string name, Crc32 nameCrc, XmlLocationInfo location)
: base(name, nameCrc, location)
{
}
}
19 changes: 19 additions & 0 deletions src/PetroglyphTools/PG.StarWarsGame.Engine/DataTypes/XmlObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using PG.Commons.DataTypes;
using PG.Commons.Hashing;
using PG.StarWarsGame.Files.XML;

namespace PG.StarWarsGame.Engine.DataTypes;

public abstract class XmlObject(
string name,
Crc32 nameCrc,
XmlLocationInfo location)
: IHasCrc32
{
public XmlLocationInfo Location { get; } = location;

public Crc32 Crc32 { get; } = nameCrc;

public string Name { get; } = name ?? throw new ArgumentNullException(nameof(name));
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using PG.StarWarsGame.Engine.DataTypes;
using PG.StarWarsGame.Engine.DataTypes;
using PG.StarWarsGame.Engine.Repositories;

namespace PG.StarWarsGame.Engine.Database;
Expand All @@ -10,5 +9,7 @@ internal class GameDatabase : IGameDatabase

public required GameConstants GameConstants { get; init; }

public required IList<GameObject> GameObjects { get; init; }
public required IXmlDatabase<GameObject> GameObjects { get; init; }

public required IXmlDatabase<SfxEvent> SfxEvents { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PG.StarWarsGame.Engine.Database;
using PG.StarWarsGame.Engine.Database.Initialization;
using PG.StarWarsGame.Engine.Repositories;

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

internal class GameDatabaseService(IServiceProvider serviceProvider) : IGameDatabaseService
{
public async Task<IGameDatabase> CreateDatabaseAsync(GameEngineType targetEngineType, GameLocations locations, CancellationToken cancellationToken = default)
public async Task<IGameDatabase> CreateDatabaseAsync(
GameEngineType targetEngineType,
GameLocations locations,
CancellationToken cancellationToken = default)
{
var repoFactory = serviceProvider.GetRequiredService<IGameRepositoryFactory>();
var repository = repoFactory.Create(targetEngineType, locations);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using PG.StarWarsGame.Engine.DataTypes;
using PG.StarWarsGame.Engine.DataTypes;
using PG.StarWarsGame.Engine.Repositories;

namespace PG.StarWarsGame.Engine.Database;
Expand All @@ -10,5 +9,7 @@ public interface IGameDatabase

public GameConstants GameConstants { get; }

public IList<GameObject> GameObjects { get; }
public IXmlDatabase<GameObject> GameObjects { get; }

public IXmlDatabase<SfxEvent> SfxEvents { get; }
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System.Threading;
using System.Threading.Tasks;
using PG.StarWarsGame.Engine.Database;

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

public interface IGameDatabaseService
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;
using AnakinRaW.CommonUtilities.Collections;
using PG.Commons.Hashing;
using PG.StarWarsGame.Engine.DataTypes;

namespace PG.StarWarsGame.Engine.Database;

public interface IXmlDatabase<T> where T : XmlObject
{
ICollection<T> Entries { get; }

ICollection<Crc32> EntryKeys { get; }

ReadOnlyFrugalList<T> GetEntries(Crc32 key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using Microsoft.Extensions.Logging;
using PG.StarWarsGame.Engine.Repositories;

namespace PG.StarWarsGame.Engine.Pipeline;
namespace PG.StarWarsGame.Engine.Database.Initialization;

public abstract class CreateDatabaseStep<T>(IGameRepository repository, IServiceProvider serviceProvider)
internal abstract class CreateDatabaseStep<T>(IGameRepository repository, IServiceProvider serviceProvider)
: PipelineStep(serviceProvider) where T : class
{
public T Database { get; private set; } = null!;
Expand Down
Loading

0 comments on commit b89074f

Please sign in to comment.