Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge feature/defs-filestore to develop #30

Merged
merged 22 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ead35ac
Init `Nodsoft.WowsReplaysUnpack.FileStore` library project
SakuraIsayeki Sep 9, 2022
fa9c604
Breakout `IDefinitionLoader` from `IDefinitionStore` + Implementations
SakuraIsayeki Sep 9, 2022
00cc5e2
Update DI extensions & builder to include `IDefinitionLoader`
SakuraIsayeki Sep 9, 2022
19c4d88
Fix the DefinitionLoader Extraction
stewieoO Sep 9, 2022
68a97d8
fix visibility of loader
stewieoO Sep 9, 2022
728faf5
Draft Filesystem defs loader implementation
SakuraIsayeki Sep 11, 2022
a2d0658
Add integration tests for unpacker configurations (default + fs)
SakuraIsayeki Sep 11, 2022
c4f426b
Fix Version Loading
stewieoO Sep 11, 2022
a0c6573
Add Comment For GetSupportedVersions()
stewieoO Sep 11, 2022
0862e5d
HACK: Disable tests parallelization
SakuraIsayeki Sep 12, 2022
e66d12a
Support running build manually
HenryQuan Oct 1, 2023
58b3436
Use nbgv 0.4.1
HenryQuan Oct 1, 2023
6123905
Allow testing in parallel
HenryQuan Oct 1, 2023
689a09c
Debug multiple tasks in Program
HenryQuan Oct 1, 2023
0d1f080
Fixed issue with mutlithreading
HenryQuan Oct 1, 2023
7c8bfef
Added simple benchmark in Program
HenryQuan Oct 2, 2023
7a2a542
Merge pull request #29 from wowsinfo/feature/defs-filestore
SakuraIsayeki Oct 12, 2023
7f63f27
Merge branch 'develop' into feature/defs-filestore
HenryQuan Oct 13, 2023
8b6672b
Fix missing consts
HenryQuan Oct 13, 2023
542283f
Update the samplePath
HenryQuan Oct 13, 2023
7d856bb
Update sameFolder in tests
HenryQuan Oct 13, 2023
8aacd2c
Add a timeout to prevent dead locks
HenryQuan Oct 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: .NET Build & Test

on:
- push
- pull_request
- workflow_call
push:
pull_request:
workflow_call:
workflow_dispatch:

jobs:
build:
Expand Down
7 changes: 0 additions & 7 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<AnalysisLevel>latest</AnalysisLevel>

<!-- <VersionPrefix>1.0</VersionPrefix>-->
<!-- <VersionSuffix>pre1</VersionSuffix>-->

<Company>Nodsoft Systems</Company>
<Authors>imkindaprogrammermyself, Sakura Akeno Isayeki, floribe2000, StewieoO</Authors>
<Product>WoWS Replays Unpack</Product>
Expand All @@ -33,10 +30,6 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Nerdbank.GitVersioning" Version="$(NerdbankGitVersioningVersion)" PrivateAssets="all" />
</ItemGroup>

<ImportGroup Condition=" '$(ExcludeRestorePackageImports)' == 'true' ">
<Import Project="$(UserProfile)\.nuget\packages\nerdbank.gitversioning\$(NerdbankGitVersioningVersion)\buildCrossTargeting\Nerdbank.GitVersioning.targets"
Condition="Exists('$(UserProfile)\.nuget\packages\nerdbank.gitversioning\$(NerdbankGitVersioningVersion)\buildCrossTargeting\Nerdbank.GitVersioning.targets')" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<ProjectReference Include="../Nodsoft.WowsReplaysUnpack.Core/Nodsoft.WowsReplaysUnpack.Core.csproj" />
<ProjectReference Include="../Nodsoft.WowsReplaysUnpack.ExtendedData/Nodsoft.WowsReplaysUnpack.ExtendedData.csproj" />
<ProjectReference Include="../Nodsoft.WowsReplaysUnpack/Nodsoft.WowsReplaysUnpack.csproj" />
<ProjectReference Include="..\Nodsoft.WowsReplaysUnpack.FileStore\Nodsoft.WowsReplaysUnpack.FileStore.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
45 changes: 39 additions & 6 deletions Nodsoft.WowsReplaysUnpack.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,31 @@
using Nodsoft.WowsReplaysUnpack.EntitySerializer;
using Nodsoft.WowsReplaysUnpack.ExtendedData;
using Nodsoft.WowsReplaysUnpack.ExtendedData.Models;
using Nodsoft.WowsReplaysUnpack.FileStore.Definitions;
using Nodsoft.WowsReplaysUnpack.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;

string samplePath = Path.Join(Directory.GetCurrentDirectory(), "../../../..", "Replay-Samples");
string samplePath = Path.Join(Directory.GetCurrentDirectory(), "../../../../Nodsoft.WowsReplaysUnpack.Tests", "Replay-Samples");
FileStream _GetReplayFile(string name) => File.OpenRead(Path.Join(samplePath, name));

ServiceProvider? services = new ServiceCollection()
.AddWowsReplayUnpacker(builder =>
{
//builder.AddReplayController<CVECheckOnlyController>();
builder.AddExtendedData();
})
//.AddWowsReplayUnpacker(builder =>
//{
// //builder.AddReplayController<CVECheckOnlyController>();
// //builder.AddExtendedData();
//})
.AddWowsReplayUnpacker(builder => builder
.WithDefinitionLoader<FileSystemDefinitionLoader>())
.Configure<FileSystemDefinitionLoaderOptions>(options =>
{
options.RootDirectory = options.RootDirectory = Path.Join(Directory.GetCurrentDirectory(),
"..", "..", "..", "..", "Nodsoft.WowsReplaysUnpack.Core", "Definitions", "Versions");
})
.AddLogging(logging =>
{
logging.ClearProviders();
Expand All @@ -41,8 +50,32 @@
// Console.WriteLine($"[{GetGroupString(msg)}] {msg.EntityId} : {msg.MessageContent}");
//}

const int CYCLE = 20;
async Task<UnpackedReplay[]> syncTasks(bool sync)
{
List<UnpackedReplay> unpackedReplays = new List<UnpackedReplay>();
if (sync)
{
for (int i = 0; i < CYCLE; i++)
{
replayUnpacker.GetUnpacker().Unpack(_GetReplayFile("good.wowsreplay"));
}
}
else
{
Parallel.ForEach(Enumerable.Range(0, CYCLE), (i) =>
{
unpackedReplays.Add(replayUnpacker.GetUnpacker().Unpack(_GetReplayFile("good.wowsreplay")));
});
}
return unpackedReplays.ToArray();
}

DateTime start = DateTime.Now;
await syncTasks(false);
Console.WriteLine(DateTime.Now - start);

var goodReplay = replayUnpacker.GetUnpacker().Unpack(_GetReplayFile("good.wowsreplay"));
var alphaReplay = replayUnpacker.GetUnpacker().Unpack(_GetReplayFile("press_account_alpha.wowsreplay"));
var bravoReplay = replayUnpacker.GetUnpacker().Unpack(_GetReplayFile("unfinished_replay.wowsreplay"));

Expand Down Expand Up @@ -71,14 +104,14 @@

public class BattleLogic
{
public Statee State { get; set; }

Check warning on line 107 in Nodsoft.WowsReplaysUnpack.Console/Program.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'State' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 107 in Nodsoft.WowsReplaysUnpack.Console/Program.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'State' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public class Statee
{
[DataMember(Name = "missions")]
public Missions _missions { get; set; }

Check warning on line 111 in Nodsoft.WowsReplaysUnpack.Console/Program.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property '_missions' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 111 in Nodsoft.WowsReplaysUnpack.Console/Program.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property '_missions' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public class Missions
{
public List<TeamsScore> teamsScore { get; set; }

Check warning on line 114 in Nodsoft.WowsReplaysUnpack.Console/Program.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'teamsScore' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 114 in Nodsoft.WowsReplaysUnpack.Console/Program.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'teamsScore' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public class TeamsScore
{
public ushort score { get; set; }
Expand All @@ -88,9 +121,9 @@
}
public static class ext
{
public static FixedDictionary GetAsDict(this Dictionary<string, object?> dict, string key) => dict[key] as FixedDictionary;

Check warning on line 124 in Nodsoft.WowsReplaysUnpack.Console/Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 124 in Nodsoft.WowsReplaysUnpack.Console/Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
public static FixedList GetAsArr(this Dictionary<string, object?> dict, string key) => dict[key] as FixedList;

Check warning on line 125 in Nodsoft.WowsReplaysUnpack.Console/Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 125 in Nodsoft.WowsReplaysUnpack.Console/Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
public static FixedDictionary GetAsDict(this FixedList list, int index) => list[index] as FixedDictionary;

Check warning on line 126 in Nodsoft.WowsReplaysUnpack.Console/Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 126 in Nodsoft.WowsReplaysUnpack.Console/Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
public static T GetAsValue<T>(this FixedDictionary dict, string key) => (T)dict[key];
}

Expand Down
4 changes: 4 additions & 0 deletions Nodsoft.WowsReplaysUnpack.Core/Consts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
public static class Consts
{
public const int Infinity = 0xFFFF;

public const char Underscore = '_';
public const char Dot = '.';
public const char Comma = ',';
}
114 changes: 114 additions & 0 deletions Nodsoft.WowsReplaysUnpack.Core/Definitions/AssemblyDefinitionLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using Nodsoft.WowsReplaysUnpack.Core.Extensions;
using System.Reflection;
using System.Xml;

namespace Nodsoft.WowsReplaysUnpack.Core.Definitions;
public class AssemblyDefinitionLoader : IDefinitionLoader
{
private static readonly XmlReaderSettings _xmlReaderSettings = new() { IgnoreComments = true };

/// <summary>
/// Assembly of the Definition store (defaults to the implementation assembly).
/// </summary>
protected readonly Assembly Assembly;

/// <summary>
/// Version -> Definitions Directory
/// </summary>
protected readonly Dictionary<string, DefinitionDirectory> DirectoryCache = new();

public AssemblyDefinitionLoader()
{

Assembly = typeof(DefaultDefinitionStore).Assembly;
}

/// <inheritdoc />
public Version[] GetSupportedVersions()
{
string versionsDirectory = JoinPath(Assembly.FullName!.GetStringBeforeIndex(','), "Definitions", "Versions");
return Assembly.GetManifestResourceNames()
.Where(name => name.StartsWith(versionsDirectory))
.Select(name => name.GetStringAfterLength(versionsDirectory + '.').GetStringBeforeIndex('.')[1..])
.Distinct()
.Select(static version => version.Split('_').Select(int.Parse).ToArray())
.Select(static arr => new Version(arr[0], arr[1], arr[2]))
.OrderByDescending(static version => version)
.ToArray();
}

/// <inheritdoc />
public virtual XmlDocument GetFileAsXml(Version clientVersion, string name, params string[] directoryNames)
{
DefinitionDirectory directory = FindDirectory(clientVersion, directoryNames);

if (directory.Files.SingleOrDefault(f => f.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)) is not { } file)
{
throw new InvalidOperationException("File could not be found");
}

XmlReader reader = XmlReader.Create(Assembly.GetManifestResourceStream(file.Path) ?? throw new InvalidOperationException("File not found"), _xmlReaderSettings);
XmlDocument xmlDocument = new();
xmlDocument.Load(reader);

return xmlDocument;
}

/// <summary>
/// Finds a definition directory by given names.
/// </summary>
/// <param name="clientVersion">The game client version.</param>
/// <param name="directoryNames">The names of the directories.</param>
/// <returns>The definition directory.</returns>
public virtual DefinitionDirectory FindDirectory(Version clientVersion, IEnumerable<string> directoryNames)
{
DefinitionDirectory folder = GetRootDirectory(clientVersion);

foreach (string? folderName in directoryNames)
{
DefinitionDirectory? foundFolder = folder.Directories.SingleOrDefault(f => f.Name.Equals(folderName, StringComparison.InvariantCultureIgnoreCase));

if (foundFolder is null)
{
break;
}

folder = foundFolder;
}

return folder;
}

/// <summary>
/// Gets the root definition directory for a given game client version.
/// </summary>
/// <param name="clientVersion">The game client version.</param>
/// <returns>The root definition directory.</returns>
public virtual DefinitionDirectory GetRootDirectory(Version clientVersion)
{
if (DirectoryCache.TryGetValue(clientVersion.ToString(), out DefinitionDirectory? rootDirectory))
{
return rootDirectory;
}

string scriptsDirectory = JoinPath(Assembly.FullName!.GetStringBeforeIndex(Consts.Comma),
"Definitions", "Versions", $"{Consts.Underscore}{clientVersion.ToString().Replace(Consts.Dot, Consts.Underscore)}", "scripts"
);

string[] fileNames = Assembly.GetManifestResourceNames()
.Where(name => name.StartsWith(scriptsDirectory))
.ToArray();

rootDirectory = new("scripts", scriptsDirectory, fileNames);
DirectoryCache.Add(clientVersion.ToString(), rootDirectory);

return rootDirectory;
}

/// <summary>
/// Joins parts of an XML path, separated by a dot.
/// </summary>
/// <param name="parts">Parts of the path.</param>
/// <returns>The joined path.</returns>
protected static string JoinPath(params string[] parts) => string.Join('.', parts);
}
Loading
Loading