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

Source Generators, lock issue and general Cleanup #34

Merged
merged 25 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f70b789
feat(version/13_1_0): Add gamedefinitions and example replay
BloodyTakao Feb 8, 2024
2b0f520
feat(version/13_2_0]: Add gamedefinitions and example replay
BloodyTakao Mar 14, 2024
b7816f2
feat(version/13_3_0): Add gamedifinitions and example replay
BloodyTakao Apr 11, 2024
b007f61
drafts for source gen logic
stewieoO Apr 12, 2024
1d03bec
Method Subscriptions kinda done probably
stewieoO Apr 15, 2024
e13eeb6
some general cleanup
stewieoO Apr 15, 2024
7d7137f
more cleanup and replace semaphore with lock for file loader
stewieoO Apr 15, 2024
fc1972c
implement property changed subscription
stewieoO Apr 15, 2024
be8dea0
Serializable Entities
stewieoO Apr 16, 2024
186bcd2
fix some entity serializer things
stewieoO Apr 16, 2024
602224b
fix entity serializer
stewieoO Apr 16, 2024
be3eaa0
fix benchmarks
stewieoO Apr 16, 2024
fc28221
emit generated files
stewieoO Apr 16, 2024
7c29dad
nuget setup for generator
stewieoO Apr 16, 2024
8ea1b7a
Fix tests
stewieoO Apr 16, 2024
5601332
cleanup console project
stewieoO Apr 16, 2024
c48d0f6
update readme and add some more utility extensions
stewieoO Apr 16, 2024
11a8adf
add entity serializer to the readme
stewieoO Apr 16, 2024
a6ac3f1
fix github workflows
stewieoO Apr 16, 2024
a8a7cd8
add check for duplicate controller/replay registration
stewieoO Apr 16, 2024
270ba78
remove unused class
stewieoO Apr 16, 2024
1dc5d4f
fix broken readme
stewieoO Apr 16, 2024
4290488
update replay types in sample
stewieoO Apr 16, 2024
4c5a2f4
Fix incorrect readme
stewieoO Apr 17, 2024
94e1eee
Update README.md
stewieoO Apr 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
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
Loading