Skip to content

Commit

Permalink
Merge pull request #34 from Nodsoft/feature/source-gen
Browse files Browse the repository at this point in the history
Source Generators, lock issue and general Cleanup
  • Loading branch information
SakuraIsayeki authored May 11, 2024
2 parents 7a94259 + 94e1eee commit 5b7d0d7
Show file tree
Hide file tree
Showing 338 changed files with 27,152 additions and 904 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.x
dotnet-version: 8.0.x

- uses: dotnet/nbgv@master
id: nbgv
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/nuget-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ jobs:
project: [
{ name: "Main", path: "Nodsoft.WowsReplaysUnpack/Nodsoft.WowsReplaysUnpack.csproj" },
{ name: "Core", path: "Nodsoft.WowsReplaysUnpack.Core/Nodsoft.WowsReplaysUnpack.Core.csproj" },
{ name: "ExtendedData", path: "Nodsoft.WowsReplaysUnpack.ExtendedData/Nodsoft.WowsReplaysUnpack.ExtendedData.csproj" }
{ name: "ExtendedData", path: "Nodsoft.WowsReplaysUnpack.ExtendedData/Nodsoft.WowsReplaysUnpack.ExtendedData.csproj" },
{ name: "Generators", path: "Nodsoft.WowsReplaysUnpack.Generators/Nodsoft.WowsReplaysUnpack.Generators.csproj" }
]

# Sources to publish to
Expand All @@ -44,7 +45,7 @@ jobs:
- name: Setup dotnet
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.x
dotnet-version: 8.0.x

- uses: dotnet/nbgv@master
id: nbgv
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Nodsoft.WowsReplaysUnpack.Benchmark.Controllers
{
internal class PerformanceController : ReplayControllerBase<PerformanceController>
internal class PerformanceController : ReplayControllerBase<UnpackedReplay>
{
public PerformanceController(IDefinitionStore definitionStore, ILogger<Entity> entityLogger) : base(definitionStore, entityLogger) { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions Nodsoft.WowsReplaysUnpack.Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

BenchmarkRunner.Run<UnpackBenchmark>(DefaultConfig.Instance
.WithOptions(ConfigOptions.DisableOptimizationsValidator)
.AddJob(Job.Default.WithRuntime(CoreRuntime.Core50))
.AddJob(Job.Default.WithRuntime(CoreRuntime.Core60)));
.AddJob(Job.Default.WithRuntime(CoreRuntime.Core80)));

Console.ReadLine();
9 changes: 5 additions & 4 deletions Nodsoft.WowsReplaysUnpack.Benchmark/UnpackBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Nodsoft.WowsReplaysUnpack.Benchmark.Controllers;
using Nodsoft.WowsReplaysUnpack.Core.Models;
using Nodsoft.WowsReplaysUnpack.Services;

namespace Nodsoft.WowsReplaysUnpack.Benchmark
Expand All @@ -10,8 +11,8 @@ public class UnpackBenchmark
{
private static readonly string samplesPath = Path.Join(Environment.CurrentDirectory, "Samples");
private static readonly MemoryStream _ms;
private static readonly IReplayUnpackerService _defaultUnpacker;
private static readonly IReplayUnpackerService _performanceUnpacker;
private static readonly IReplayUnpackerService<UnpackedReplay> _defaultUnpacker;
private static readonly IReplayUnpackerService<UnpackedReplay> _performanceUnpacker;

static UnpackBenchmark()
{
Expand All @@ -25,14 +26,14 @@ static UnpackBenchmark()
ServiceProvider? services = new ServiceCollection()
.AddWowsReplayUnpacker(b =>
{
b.AddReplayController<PerformanceController>();
b.AddReplayController<PerformanceController, UnpackedReplay>();
})
.AddLogging(l => l.ClearProviders())
.BuildServiceProvider();

ReplayUnpackerFactory? factory = services.GetRequiredService<ReplayUnpackerFactory>();
_defaultUnpacker = factory.GetUnpacker();
_performanceUnpacker = factory.GetUnpacker<PerformanceController>();
_performanceUnpacker = factory.GetUnpacker<UnpackedReplay>();
}

[Benchmark]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@
<OutputType>Exe</OutputType>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<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" />
<ProjectReference Include="..\Nodsoft.WowsReplaysUnpack.Generators\Nodsoft.WowsReplaysUnpack.Generators.csproj"
OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="All"/>
</ItemGroup>

<ItemGroup>
Expand Down
137 changes: 3 additions & 134 deletions Nodsoft.WowsReplaysUnpack.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,135 +1,4 @@
// See https://aka.ms/new-console-template for more information
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Nodsoft.WowsReplaysUnpack;
using Nodsoft.WowsReplaysUnpack.Core.Models;
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;
using Nodsoft.WowsReplaysUnpack.Console.Samples.EntitySerializer;

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
.WithDefinitionLoader<FileSystemDefinitionLoader>())
.Configure<FileSystemDefinitionLoaderOptions>(options =>
{
options.RootDirectory = options.RootDirectory = Path.Join(Directory.GetCurrentDirectory(),
"..", "..", "..", "..", "Nodsoft.WowsReplaysUnpack.Core", "Definitions", "Versions");
})
.AddLogging(logging =>
{
logging.ClearProviders();
logging.AddConsole();
logging.SetMinimumLevel(LogLevel.Error);
})
.BuildServiceProvider();

ReplayUnpackerFactory? replayUnpacker = services.GetRequiredService<ReplayUnpackerFactory>();

//var unpackedReplay = replayUnpacker.GetUnpacker().Unpack(GetReplayFile("payload.wowsreplay"));
//var unpackedReplay = replayUnpacker.GetUnpacker<CVECheckOnlyController>().Unpack(GetReplayFile("payload.wowsreplay"));
//ExtendedDataReplay? unpackedReplay = (ExtendedDataReplay)replayUnpacker.GetExtendedDataUnpacker().Unpack(_GetReplayFile("good.wowsreplay"));

//foreach (ReplayMessage msg in replay.ChatMessages)
//{
// 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"));

var alphaState = alphaReplay.Entities.Single(e => e.Value.Name == "BattleLogic").Value.ClientProperties
.GetAsDict("state")
.GetAsDict("missions")
.GetAsArr("teamsScore");

var bravoState = bravoReplay.Entities.Single(e => e.Value.Name == "BattleLogic").Value.ClientProperties
.GetAsDict("state")
.GetAsDict("missions")
.GetAsArr("teamsScore");

var scoreA = alphaState.GetAsDict(0).GetAsValue<ushort>("score");
var scoreB = alphaState.GetAsDict(1).GetAsValue<ushort>("score");

var _scoreA = bravoState.GetAsDict(0).GetAsValue<ushort>("score");
var _scoreB = bravoState.GetAsDict(1).GetAsValue<ushort>("score");



var test = alphaReplay.SerializeEntity<BattleLogic>("BattleLogic");

Console.WriteLine();
Console.ReadKey();

public class BattleLogic
{
public Statee State { get; set; }
public class Statee
{
[DataMember(Name = "missions")]
public Missions _missions { get; set; }
public class Missions
{
public List<TeamsScore> teamsScore { get; set; }
public class TeamsScore
{
public ushort score { get; set; }
}
}
}
}
public static class ext
{
public static FixedDictionary GetAsDict(this Dictionary<string, object?> dict, string key) => dict[key] as FixedDictionary;
public static FixedList GetAsArr(this Dictionary<string, object?> dict, string key) => dict[key] as FixedList;
public static FixedDictionary GetAsDict(this FixedList list, int index) => list[index] as FixedDictionary;
public static T GetAsValue<T>(this FixedDictionary dict, string key) => (T)dict[key];
}

//static string GetGroupString(ReplayMessage msg) => msg.MessageGroup switch
//{
// "battle_team" => "Team",
// "battle_common" => "All",
// _ => ""
//};
// await new SyncTest().ExecuteAsync();
await new EntitySerializerSample().ExecuteAsync();
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Nodsoft.WowsReplaysUnpack.Generators;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Nodsoft.WowsReplaysUnpack.Console.Samples.EntitySerializer;

[SerializableEntity]
public partial class BattleLogic
{
[DataMember(Name = "state")]
public State State { get; set; } = null!;
}

public class State
{
[DataMember(Name = "missions")]
public Missions Missions { get; set; } = null!;
}

public class Missions
{
[DataMember(Name = "teamsScore")]
public List<TeamsScore> TeamsScore { get; set; } = null!;
}

public class TeamsScore
{
[DataMember(Name = "score")]
public ushort Score { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Microsoft.Extensions.DependencyInjection;
using Nodsoft.WowsReplaysUnpack.Core.Models;
using Nodsoft.WowsReplaysUnpack.EntitySerializer;
using Nodsoft.WowsReplaysUnpack.ExtendedData;
using Nodsoft.WowsReplaysUnpack.ExtendedData.Models;
using Nodsoft.WowsReplaysUnpack.Services;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Nodsoft.WowsReplaysUnpack.Console.Samples.EntitySerializer;

public class EntitySerializerSample : TaskBase
{
protected override void ConfigureServices(IServiceCollection serviceCollection)
{
serviceCollection.AddWowsReplayUnpacker(builder =>
{
builder.AddExtendedData();
});
}

protected override Task ExecuteAsync(IServiceProvider services)
{
using IReplayUnpackerService<ExtendedDataReplay> unpacker =
services.GetRequiredService<IReplayUnpackerFactory>().GetExtendedDataUnpacker();

ExtendedDataReplay alphaReplay = unpacker.Unpack(GetReplayFile("press_account_alpha.wowsreplay"));
ExtendedDataReplay bravoReplay = unpacker.Unpack(GetReplayFile("press_account_bravo.wowsreplay"));

ManualExtensions(alphaReplay, bravoReplay);
Serializer(alphaReplay, bravoReplay);

return Task.CompletedTask;
}

private void ManualExtensions(UnpackedReplay alphaReplay, UnpackedReplay bravoReplay)
{
FixedList? alphaState = alphaReplay.Entities.Single(e => e.Value.Name == "BattleLogic").Value.ClientProperties
.GetAsDict("state")?
.GetAsDict("missions")?
.GetAsArr("teamsScore");

FixedList? bravoState = bravoReplay.Entities.Single(e => e.Value.Name == "BattleLogic").Value.ClientProperties
.GetAsDict("state")?
.GetAsDict("missions")?
.GetAsArr("teamsScore");

ushort? alphaScoreA = alphaState?.GetAsDict(0)?.GetAsValue<ushort>("score");
ushort? alphaScoreB = alphaState?.GetAsDict(1)?.GetAsValue<ushort>("score");

ushort? bravoScoreA = bravoState?.GetAsDict(0)?.GetAsValue<ushort>("score");
ushort? bravoScoreB = bravoState?.GetAsDict(1)?.GetAsValue<ushort>("score");

System.Console.WriteLine("Manuel Extensions:");
System.Console.WriteLine($"Alpha Replay: [{alphaScoreA}:{alphaScoreB}]");
System.Console.WriteLine($"Bravo Replay: [{bravoScoreA}:{bravoScoreB}]");
}

private void Serializer(UnpackedReplay alphaReplay, UnpackedReplay bravoReplay)
{
BattleLogic alphaBattleLogic = alphaReplay.DeserializeEntity<BattleLogic>("BattleLogic");
BattleLogic bravoBattleLogic = bravoReplay.DeserializeEntity<BattleLogic>("BattleLogic");


System.Console.WriteLine("Manuel Extensions:");
System.Console.WriteLine(
$"Alpha Replay: [{alphaBattleLogic.State.Missions.TeamsScore[0].Score}:{alphaBattleLogic.State.Missions.TeamsScore[1].Score}]");
System.Console.WriteLine(
$"Bravo Replay: [{bravoBattleLogic.State.Missions.TeamsScore[0].Score}:{bravoBattleLogic.State.Missions.TeamsScore[1].Score}]");
}
}
Loading

0 comments on commit 5b7d0d7

Please sign in to comment.