Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 7 additions & 3 deletions EIV_Pack.Example/EIV_Pack.Example.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<TargetFrameworks>net10.0;net8.0;net472</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand All @@ -13,10 +14,13 @@
</ProjectReference>
<ProjectReference Include="..\EIV_Pack\EIV_Pack.csproj" />
</ItemGroup>
<!-- Check Analyzer/Generator-->
<!-- Check Analyzer/Generator
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

<ItemGroup>
<None Include="Generated/**" />
</ItemGroup>
-->
</Project>
4 changes: 2 additions & 2 deletions EIV_Pack.Generator/EIV_Pack.Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
<!-- NuGet -->
<IsPackable>true</IsPackable>
<Description>Code generator for EIVPack.</Description>
<Version>2.1.2</Version>
<FileVersion>2.1.2</FileVersion>
<Version>2.2</Version>
<FileVersion>2.2</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
22 changes: 20 additions & 2 deletions EIV_Pack.Generator/MainGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,27 @@ or StructDeclarationSyntax
})
.WithTrackingName("EIV_Pack.EIV_Packable.1_ForAttributeEIV_PackableAttribute");

context.RegisterSourceOutput(typeDeclarations.Combine(context.CompilationProvider), static (context, source) =>
var options = context.ParseOptionsProvider.Select((option, token) =>
{
PackGenerator.Generate(source.Left, source.Right, context);
return option.PreprocessorSymbolNames.Contains("NET8_0_OR_GREATER");
});

context.RegisterSourceOutput(typeDeclarations.Combine(context.CompilationProvider).Combine(options), static (context, source) =>
{
GeneratorClass generatorClass = new()
{
Syntax = source.Left.Left,
Compilation = source.Left.Right,
IsNet8OrGreater = source.Right,
};
PackGenerator.Generate(generatorClass, context);
});
}
}

public class GeneratorClass
{
public TypeDeclarationSyntax Syntax { get; set; } = default!;
public Compilation Compilation { get; set; } = default!;
public bool IsNet8OrGreater { get; set; } = default!;
}
66 changes: 48 additions & 18 deletions EIV_Pack.Generator/PackGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ namespace EIV_Pack.Generator;

internal static class PackGenerator
{
internal static void Generate(TypeDeclarationSyntax syntax, Compilation compilation, SourceProductionContext context)
internal static void Generate(GeneratorClass generatorClass, SourceProductionContext context)
{
var semanticModel = compilation.GetSemanticModel(syntax.SyntaxTree);
TypeDeclarationSyntax syntax = generatorClass.Syntax;
var semanticModel = generatorClass.Compilation.GetSemanticModel(syntax.SyntaxTree);

var typeSymbol = semanticModel.GetDeclaredSymbol(syntax, context.CancellationToken);
if (typeSymbol == null)
Expand Down Expand Up @@ -53,7 +54,13 @@ internal static void Generate(TypeDeclarationSyntax syntax, Compilation compilat
sb.AppendLine("// Generated with EIV_Pack.Generator.");
sb.AppendLine("using EIV_Pack;");
sb.AppendLine("using EIV_Pack.Formatters;");
sb.AppendLine("#nullable enable");
sb.AppendLine();

if (generatorClass.IsNet8OrGreater)
{
sb.AppendLine("#nullable enable");
sb.AppendLine();
}

List<string> names = [];
INamespaceSymbol namespaceSymbol = typeSymbol.ContainingNamespace;
Expand All @@ -77,26 +84,39 @@ internal static void Generate(TypeDeclarationSyntax syntax, Compilation compilat
namespaceStr = namespaceStr.Substring(1);

if (!string.IsNullOrEmpty(namespaceStr))
sb.AppendLine($"namespace {namespaceStr};");

{
if (generatorClass.IsNet8OrGreater)
{
sb.AppendLine($"namespace {namespaceStr};");
}
else
{
sb.AppendLine($"namespace {namespaceStr}");
sb.AppendLine("{");
}
}

sb.AppendLine();
sb.AppendLine($"partial {classOrStructOrRecord} {typeSymbol.Name} : IPackable<{typeSymbol.Name}>, IFormatter<{typeSymbol.Name}>");
sb.AppendLine("{");

if (!typeSymbol.GetMembers().Any(x => x.IsStatic && x.Kind == SymbolKind.Method && x.Name == ".cctor"))
{
sb.AppendLine($$"""
string register = generatorClass.IsNet8OrGreater ? $"FormatterProvider.Register<{typeSymbol.Name}>" : "RegisterFormatter";

sb.AppendLine(
$$"""

static {{typeSymbol.Name}}()
{
FormatterProvider.Register<{{typeSymbol.Name}}>();
{{register}}();
}

""");
}

sb.AppendLine($$"""
sb.AppendLine(
$$"""

public static void RegisterFormatter()
{
Expand All @@ -110,33 +130,43 @@ public static void RegisterFormatter()
FormatterProvider.Register(new ArrayFormatter<{{typeSymbol.Name}}>());
}
}

""");

GeneratePackable(ref syntax, ref typeSymbol, ref sb, ref fieldOrParamList);
GeneratePackable(ref syntax, ref typeSymbol, ref sb, ref fieldOrParamList, generatorClass.IsNet8OrGreater);

sb.AppendLine($$"""

public void Deserialize(ref PackReader reader, scoped ref {{typeSymbol.Name}}{{(typeSymbol.IsValueType ? string.Empty : "?")}} value)

sb.AppendLine(
$$"""

public void Deserialize(ref PackReader reader, scoped ref {{typeSymbol.Name}}{{(typeSymbol.IsValueType ? string.Empty : generatorClass.IsNet8OrGreater ? "?" : string.Empty)}} value)
{
DeserializePackable(ref reader, ref value);
}

public void Serialize(ref PackWriter writer, scoped ref readonly {{typeSymbol.Name}}{{(typeSymbol.IsValueType ? string.Empty : "?")}} value)
public void Serialize(ref PackWriter writer, scoped ref readonly {{typeSymbol.Name}}{{(typeSymbol.IsValueType ? string.Empty : generatorClass.IsNet8OrGreater ? "?" : string.Empty)}} value)
{
SerializePackable(ref writer, in value);
}

""");

sb.AppendLine("}");

if (!string.IsNullOrEmpty(namespaceStr) && !generatorClass.IsNet8OrGreater)
{
sb.AppendLine();
sb.AppendLine("}");
}

context.AddSource($"{fullType}.g.cs", sb.ToString());
}


internal static void GeneratePackable(ref TypeDeclarationSyntax _, ref INamedTypeSymbol typeSymbol, ref StringBuilder sb, ref List<ISymbol> FieldAndParamList)
internal static void GeneratePackable(ref TypeDeclarationSyntax _, ref INamedTypeSymbol typeSymbol, ref StringBuilder sb, ref List<ISymbol> FieldAndParamList, bool isNet8OrGreater)
{
var nullable = typeSymbol.IsValueType ? "" : "?";
var nullable = typeSymbol.IsValueType ? string.Empty : isNet8OrGreater ? "?" : string.Empty;


sb.AppendLine($"\tconst int EIV_PACK_FieldAndParamCount = {FieldAndParamList.Count};");
Expand Down Expand Up @@ -165,7 +195,7 @@ internal static void GeneratePackable(ref TypeDeclarationSyntax _, ref INamedTyp
}

sb.AppendLine("\t}");

sb.AppendLine();
sb.AppendLine($"\tpublic static void SerializePackable(ref PackWriter writer, scoped ref readonly {typeSymbol.Name}{nullable} value)");
sb.AppendLine("\t{");
if (!typeSymbol.IsValueType)
Expand Down
1 change: 1 addition & 0 deletions EIV_Pack.Test/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[assembly: CaptureConsole]
11 changes: 8 additions & 3 deletions EIV_Pack.Test/EIV_Pack.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<TargetFrameworks>net10.0;net8.0;net472</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="coverlet.collector" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
2 changes: 1 addition & 1 deletion EIV_Pack.Test/OtherFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public void LazyTest()
PackWriter writer = new();
var formatter = new LazyFormatter<int>();
Assert.NotNull(formatter);
Lazy<int> val = new(6);
Lazy<int> val = new(() => 6);
Lazy<int>? vallNull = null;
writer.WriteValueWithFormatter(formatter, val);
writer.WriteValueWithFormatter(formatter, vallNull);
Expand Down
4 changes: 2 additions & 2 deletions EIV_Pack.Test/ProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void ProviderTest()
{
Assert.Throws<PackException>(() => FormatterProvider.GetFormatter<Test>());

FormatterProvider.Register<Test>(null);
FormatterProvider.Register<Test>(null!);

Assert.Throws<PackException>(() => FormatterProvider.GetFormatter<Test>());
}
Expand All @@ -26,7 +26,7 @@ public void CustomTypeTest()
Value = "sdfsf"
};

FormatterProvider.Register<CustomType>();
CustomType.RegisterFormatter();

var bytes = Serializer.Serialize(customType);
Assert.NotEmpty(bytes);
Expand Down
Loading