Skip to content

Commit

Permalink
start parse sfxevents
Browse files Browse the repository at this point in the history
  • Loading branch information
AnakinRaW committed Jul 2, 2024
1 parent b772002 commit e2c4b23
Show file tree
Hide file tree
Showing 34 changed files with 638 additions and 129 deletions.
21 changes: 14 additions & 7 deletions src/ModVerify.CliApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using AET.ModVerify;
Expand All @@ -30,12 +29,16 @@
using PG.StarWarsGame.Infrastructure.Mods;
using PG.StarWarsGame.Infrastructure.Services.Dependencies;
using Serilog;
using Serilog.Events;
using Serilog.Filters;

namespace ModVerify.CliApp;

internal class Program
{
private const string EngineParserNamespace = "PG.StarWarsGame.Engine.Xml.Parsers";
private const string ParserNamespace = "PG.StarWarsGame.Engine.Xml.Parsers";

private static IServiceProvider _services = null!;
private static CliOptions _options;

Expand Down Expand Up @@ -123,7 +126,7 @@ private static VerifyGamePipeline BuildPipeline(IPlayableObject playableObject,
fallbackGame.Directory.FullName);


return new ModVerifyPipeline(GameEngineType.Foc, gameLocations, ModVerifySettings.Default, _services);
return new ModVerifyPipeline(GameEngineType.Foc, gameLocations, GameVerifySettings.Default, _services);
}

private static IServiceProvider CreateAppServices()
Expand Down Expand Up @@ -177,30 +180,34 @@ private static void ConfigureLogging(ILoggingBuilder loggingBuilder, IFileSystem
private static void SetupXmlParseLogging(ILoggingBuilder loggingBuilder, IFileSystem fileSystem)
{
const string xmlParseLogFileName = "XmlParseLog.txt";
const string parserNamespace = "PG.StarWarsGame.Engine.Xml.Parsers";

fileSystem.File.TryDeleteWithRetry(xmlParseLogFileName);

var logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Warning()
.Filter.ByIncludingOnly(Matching.FromSource(parserNamespace))
.Filter.ByIncludingOnly(IsXmlParserLogging)
.WriteTo.File(xmlParseLogFileName, outputTemplate: "[{Level:u3}] [{SourceContext}] {Message}{NewLine}{Exception}")
.CreateLogger();

loggingBuilder.AddSerilog(logger);

loggingBuilder.AddFilter<ConsoleLoggerProvider>((category, level) =>
loggingBuilder.AddFilter<ConsoleLoggerProvider>((category, _) =>
{
if (string.IsNullOrEmpty(category))
return false;
if (category.StartsWith(parserNamespace))
if (category.StartsWith(EngineParserNamespace) || category.StartsWith(ParserNamespace))
return false;

return true;
});
}

private static bool IsXmlParserLogging(LogEvent logEvent)
{
return Matching.FromSource(ParserNamespace)(logEvent) || Matching.FromSource(EngineParserNamespace)(logEvent);
}


internal class CliOptions
{
Expand All @@ -215,7 +222,7 @@ internal class CliOptions
internal class ModVerifyPipeline(
GameEngineType targetType,
GameLocations gameLocations,
ModVerifySettings settings,
GameVerifySettings settings,
IServiceProvider serviceProvider)
: VerifyGamePipeline(targetType, gameLocations, settings, serviceProvider)
{
Expand Down
23 changes: 23 additions & 0 deletions src/ModVerify/GameVerifySettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace AET.ModVerify;

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

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

public VerifyThrowBehavior ThrowBehavior { get; init; }

public VerifyLocalizationOption VerifyLocalization { get; init; }
}

public enum VerifyLocalizationOption
{
English,
CurrentSystem,
AllInstalled,
All
}
2 changes: 1 addition & 1 deletion src/ModVerify/IVerificationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ namespace AET.ModVerify;

public interface IVerificationProvider
{
IEnumerable<GameVerificationStep> GetAllDefaultVerifiers(IGameDatabase database, ModVerifySettings settings);
IEnumerable<GameVerificationStep> GetAllDefaultVerifiers(IGameDatabase database, GameVerifySettings settings);
}
13 changes: 0 additions & 13 deletions src/ModVerify/ModVerifySettings.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/ModVerify/Steps/DuplicateFinderStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace AET.ModVerify.Steps;

public sealed class DuplicateFinderStep(
IGameDatabase gameDatabase,
ModVerifySettings settings,
GameVerifySettings settings,
IServiceProvider serviceProvider)
: GameVerificationStep(gameDatabase, settings, serviceProvider)
{
Expand Down
4 changes: 2 additions & 2 deletions src/ModVerify/Steps/GameVerificationStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace AET.ModVerify.Steps;

public abstract class GameVerificationStep(
IGameDatabase gameDatabase,
ModVerifySettings settings,
GameVerifySettings settings,
IServiceProvider serviceProvider)
: PipelineStep(serviceProvider)
{
Expand All @@ -24,7 +24,7 @@ public abstract class GameVerificationStep(

public IReadOnlyCollection<VerificationError> VerifyErrors => _verifyErrors;

protected ModVerifySettings Settings { get; } = settings;
protected GameVerifySettings Settings { get; } = settings;

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

Expand Down
2 changes: 1 addition & 1 deletion src/ModVerify/Steps/VerifyReferencedModelsStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace AET.ModVerify.Steps;

public sealed class VerifyReferencedModelsStep(
IGameDatabase database,
ModVerifySettings settings,
GameVerifySettings settings,
IServiceProvider serviceProvider)
: GameVerificationStep(database, settings, serviceProvider)
{
Expand Down
2 changes: 1 addition & 1 deletion src/ModVerify/VerificationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace AET.ModVerify;

internal class VerificationProvider(IServiceProvider serviceProvider) : IVerificationProvider
{
public IEnumerable<GameVerificationStep> GetAllDefaultVerifiers(IGameDatabase database, ModVerifySettings settings)
public IEnumerable<GameVerificationStep> GetAllDefaultVerifiers(IGameDatabase database, GameVerifySettings settings)
{
yield return new VerifyReferencedModelsStep(database, settings, serviceProvider);
yield return new DuplicateFinderStep(database, settings, serviceProvider);
Expand Down
4 changes: 2 additions & 2 deletions src/ModVerify/VerifyGamePipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public abstract class VerifyGamePipeline : Pipeline
private readonly GameLocations _gameLocations;
private readonly ParallelRunner _verifyRunner;

protected ModVerifySettings Settings { get; }
protected GameVerifySettings Settings { get; }

protected VerifyGamePipeline(GameEngineType targetType, GameLocations gameLocations, ModVerifySettings settings, IServiceProvider serviceProvider)
protected VerifyGamePipeline(GameEngineType targetType, GameLocations gameLocations, GameVerifySettings settings, IServiceProvider serviceProvider)
: base(serviceProvider)
{
_targetType = targetType;
Expand Down
16 changes: 3 additions & 13 deletions src/PetroglyphTools/PG.StarWarsGame.Engine/DataTypes/GameObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@ namespace PG.StarWarsGame.Engine.DataTypes;

public sealed class GameObject : XmlObject
{
private readonly IReadOnlyValueListDictionary<string, object> _properties;

internal GameObject(
string type,
string name,
Crc32 nameCrc,
GameObjectType estimatedType,
IReadOnlyValueListDictionary<string, object> properties,
IReadOnlyValueListDictionary<string, object?> properties,
XmlLocationInfo location)
: base(name, nameCrc, location)
: base(name, nameCrc, properties, location)
{
_properties = properties;
Type = type ?? throw new ArgumentNullException(nameof(type));
EstimatedType = estimatedType;
}
Expand All @@ -36,7 +33,7 @@ public ISet<string> Models
{
get
{
var models = _properties.AggregateValues<string, object, string>
var models = XmlProperties.AggregateValues<string, object?, string>
(new HashSet<string>
{
"Galactic_Model_Name",
Expand Down Expand Up @@ -65,11 +62,4 @@ public ISet<string> Models

public IList<(string Terrain, string Model)>? LandTerrainModelMapping =>
GetLastPropertyOrDefault<IList<(string Terrain, string Model)>>("Land_Terrain_Model_Mapping");

private T? GetLastPropertyOrDefault<T>(string tagName, T? defaultValue = default)
{
if (!_properties.TryGetLastValue(tagName, out var value))
return defaultValue;
return (T)value;
}
}
92 changes: 86 additions & 6 deletions src/PetroglyphTools/PG.StarWarsGame.Engine/DataTypes/SfxEvent.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,100 @@
using PG.Commons.Hashing;
using System;
using System.Collections.Generic;
using System.Linq;
using PG.Commons.Hashing;
using PG.StarWarsGame.Engine.Xml.Parsers.Data;
using PG.StarWarsGame.Files.XML;

namespace PG.StarWarsGame.Engine.DataTypes;

public sealed class SfxEvent : XmlObject
{
private int _volumeValue;
private IReadOnlyList<string>? _preSamples;
private IReadOnlyList<string>? _samples;
private IReadOnlyList<string>? _postSamples;
private bool? _isPreset;

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

View workflow job for this annotation

GitHub Actions / Build & Test (windows-latest)

The field 'SfxEvent._isPreset' is never used
private bool? _is2D;
private bool? _is3D;
private bool? _isGui;
private bool? _isHudVo;
private bool? _isUnitResponseVo;
private bool? _isAmbientVo;
private bool? _isLocalized;

public bool IsPreset { get; }

public SfxEvent? Preset { get; }
public bool Is3D => LazyInitValue(ref _is3D, SfxEventXmlTags.Is3D, false)!.Value;

public int Volume => Preset?.Volume ?? _volumeValue;
public bool Is2D => LazyInitValue(ref _is2D, SfxEventXmlTags.Is2D, false)!.Value;

public bool IsGui => LazyInitValue(ref _isGui, SfxEventXmlTags.IsGui, false)!.Value;

internal SfxEvent(string name, Crc32 nameCrc, XmlLocationInfo location)
: base(name, nameCrc, location)
public bool IsHudVo => LazyInitValue(ref _isHudVo, SfxEventXmlTags.IsHudVo, false)!.Value;

public bool IsUnitResponseVo => LazyInitValue(ref _isUnitResponseVo, SfxEventXmlTags.IsUnitResponseVo, false)!.Value;

public bool IsAmbientVo => LazyInitValue(ref _isAmbientVo, SfxEventXmlTags.IsAmbientVo, false)!.Value;

public bool IsLocalized => LazyInitValue(ref _isLocalized, SfxEventXmlTags.Localize, false)!.Value;

public string? UsePresetName { get; }

public bool PlaySequentially => LazyInitValue(ref _is3D, SfxEventXmlTags.Is3D, false)!.Value;

public IEnumerable<string> AllSamples => PreSamples.Concat(Samples).Concat(PostSamples);

public IReadOnlyList<string> PreSamples => LazyInitValue(ref _preSamples, SfxEventXmlTags.PreSamples, Array.Empty<string>());

public IReadOnlyList<string> Samples => LazyInitValue(ref _samples, SfxEventXmlTags.Samples, Array.Empty<string>());

public IReadOnlyList<string> PostSamples => LazyInitValue(ref _postSamples, SfxEventXmlTags.PostSamples, Array.Empty<string>());

public IReadOnlyList<string> LocalizedTextIDs { get; }

public byte Priority { get; }

public byte Probability { get; }

public sbyte PlayCount { get; }

public float LoopFadeInSeconds { get; }

public float LoopFadeOutInSeconds { get; }

public sbyte MaxInstances { get; }

public byte MinVolume { get; }

public byte MaxVolume { get; }

public byte MinPitch { get; }

public byte MaxPitch { get; }

public byte MinPan2D { get; }

public byte MaxPan2D { get; }

public uint MinPredelay { get; }

public uint MaxPredelay { get; }

public uint MinPostdelay { get; }

public uint MaxPostdelay { get; }

public float VolumeSaturationDistance { get; }

public bool KillsPreviousObjectsSfx { get; }

public string? OverlapTestName { get; }

public string? ChainedSfxEventName { get; }

internal SfxEvent(string name, Crc32 nameCrc, IReadOnlyValueListDictionary<string, object?> properties,

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

View workflow job for this annotation

GitHub Actions / Build & Test (windows-latest)

Non-nullable property 'LocalizedTextIDs' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
XmlLocationInfo location)
: base(name, nameCrc, properties, location)
{

}
}
28 changes: 28 additions & 0 deletions src/PetroglyphTools/PG.StarWarsGame.Engine/DataTypes/XmlObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,40 @@ namespace PG.StarWarsGame.Engine.DataTypes;
public abstract class XmlObject(
string name,
Crc32 nameCrc,
IReadOnlyValueListDictionary<string, object?> properties,
XmlLocationInfo location)
: IHasCrc32
{
public XmlLocationInfo Location { get; } = location;

public Crc32 Crc32 { get; } = nameCrc;

public IReadOnlyValueListDictionary<string, object?> XmlProperties { get; } = properties ?? throw new ArgumentNullException(nameof(properties));

public string Name { get; } = name ?? throw new ArgumentNullException(nameof(name));

public T? GetLastPropertyOrDefault<T>(string tagName, T? defaultValue = default)
{
if (!XmlProperties.TryGetLastValue(tagName, out var value))
return defaultValue;
return (T)value;
}

protected T LazyInitValue<T>(ref T? field, string tag, T defaultValue, Func<T, T>? coerceFunc = null)
{
if (field is null)
{
if (XmlProperties.TryGetLastValue(tag, out var value))
{
var tValue = (T)value;
if (coerceFunc is not null)
tValue = coerceFunc(tValue);
field = tValue;
}
else
field = defaultValue;
}

return field;
}
}
Loading

0 comments on commit e2c4b23

Please sign in to comment.