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
36 changes: 36 additions & 0 deletions source/LottieGen/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

Expand All @@ -14,9 +15,13 @@ sealed class CommandLineOptions
readonly List<string> _additionalInterfaces = new List<string>();
readonly List<string> _languageStrings = new List<string>();
Version? _winUIVersion;
Version? _cSharpVersion;

internal IReadOnlyList<string> AdditionalInterfaces => _additionalInterfaces;

// The version of the C# language that generated C# code may use. Defaults to 7.3.
internal Version CSharpVersion => _cSharpVersion ?? new Version(7, 3);

internal bool DisableCodeGenOptimizer { get; private set; }

internal bool DisableLottieMergeOptimizer { get; private set; }
Expand Down Expand Up @@ -84,6 +89,11 @@ internal string ToConfigurationCommandLine(Language languageSwitch)
}
}

if (_cSharpVersion is not null)
{
sb.Append($" -{nameof(CSharpVersion)} {(CSharpVersion.Minor == 0 ? CSharpVersion.Major.ToString() : CSharpVersion.ToString(2))}");
}

if (DisableCodeGenOptimizer)
{
sb.Append($" -{nameof(DisableCodeGenOptimizer)}");
Expand Down Expand Up @@ -180,6 +190,7 @@ enum Keyword

// Normal keywords.
AdditionalInterface,
CSharpVersion,
DisableCodeGenOptimizer,
DisableLottieMergeOptimizer,
DisableTranslationOptimizer,
Expand Down Expand Up @@ -251,6 +262,7 @@ void ParseCommandLineStrings(string[] args)
// Define the keywords accepted on the command line.
var tokenizer = new CommandlineTokenizer<Keyword>(ambiguousValue: Keyword.Ambiguous)
.AddPrefixedKeyword(Keyword.AdditionalInterface)
.AddPrefixedKeyword(Keyword.CSharpVersion)
.AddPrefixedKeyword(Keyword.DisableCodeGenOptimizer)
.AddPrefixedKeyword(Keyword.DisableLottieMergeOptimizer)
.AddPrefixedKeyword(Keyword.DisableTranslationOptimizer)
Expand Down Expand Up @@ -325,6 +337,7 @@ void ParseCommandLineStrings(string[] args)

// The following keywords require a parameter as the next token.
case Keyword.AdditionalInterface:
case Keyword.CSharpVersion:
case Keyword.InputFile:
case Keyword.Interface:
case Keyword.Language:
Expand All @@ -345,6 +358,29 @@ void ParseCommandLineStrings(string[] args)

case Keyword.AdditionalInterface:
_additionalInterfaces.Add(arg);
previousKeyword = Keyword.None;
break;
case Keyword.CSharpVersion:
if (_cSharpVersion is not null)
{
ErrorDescription = ArgumentSpecifiedMoreThanOnce("C# version");
return;
}

{
// Accepts a major version such as 12, or a major and minor version such as 7.3.
Version? version = int.TryParse(arg, NumberStyles.None, CultureInfo.InvariantCulture, out var major)
? new Version(major, 0)
: Version.TryParse(arg, out var parsed) && parsed.Build < 0 ? parsed : null;
if (version is null || version < new Version(7, 3))
{
ErrorDescription = "C# version must be 7.3 or later, for example 7.3 or 12.";
return;
}

_cSharpVersion = version;
}

previousKeyword = Keyword.None;
break;
case Keyword.InputFile:
Expand Down
1 change: 1 addition & 0 deletions source/LottieGen/LottieJsonFileProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ CodegenConfiguration CreateCodeGenConfiguration(
Public = _options.Public,
Width = lottieComposition.Width,
WinUIVersion = _options.WinUIVersion,
CSharpVersion = _options.CSharpVersion,
};

return result;
Expand Down
6 changes: 6 additions & 0 deletions source/LottieGenExe/Usage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ [Other options]
-AdditionalInterface
Specifies an additional interface that the generated code
will claim to implement. May be specified multiple times.
-CSharpVersion
C# only. The version of the C# language that the generated
code may use, such as 7.3 (the default) or 12. With 12 or
later, collection expressions and spans are used to avoid
allocating arrays, which assumes .NET 8 or later. The code
works with both CsWinRT 2.x and 3.0.
-DisableTranslationOptimizer
Disables optimization of the translation from Lottie to
Windows code. Mainly used to detect bugs in the optimizer.
Expand Down
26 changes: 26 additions & 0 deletions source/UIDataCodeGen/CodeGen/AnimatedVisualClassMembers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

using System.Collections.Generic;

namespace CommunityToolkit.WinUI.Lottie.UIData.CodeGen
{
/// <summary>
/// The fields and methods of an AnimatedVisual class.
/// </summary>
sealed class AnimatedVisualClassMembers
{
internal AnimatedVisualClassMembers(IReadOnlyList<AnimatedVisualField> fields, IReadOnlyList<AnimatedVisualMethod> methods)
{
Fields = fields;
Methods = methods;
}

internal IReadOnlyList<AnimatedVisualField> Fields { get; }

internal IReadOnlyList<AnimatedVisualMethod> Methods { get; }
}
}
59 changes: 59 additions & 0 deletions source/UIDataCodeGen/CodeGen/AnimatedVisualField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

namespace CommunityToolkit.WinUI.Lottie.UIData.CodeGen
{
/// <summary>
/// A field of an AnimatedVisual class.
/// </summary>
sealed class AnimatedVisualField
{
internal AnimatedVisualField(
string name,
string declaration,
string? mutableDeclaration = null,
bool isAssignedByConstructor = false,
bool isCommon = false)
{
Name = name;
Declaration = declaration;
MutableDeclaration = mutableDeclaration ?? declaration;
IsAssignedByConstructor = isAssignedByConstructor;
IsCommon = isCommon;
}

internal string Name { get; }

/// <summary>
/// Gets the declaration of the field, as written in the class.
/// </summary>
internal string Declaration { get; }

/// <summary>
/// Gets the declaration of the field without the modifier that makes it read-only. This
/// is the same as <see cref="Declaration"/> if the field is not read-only.
/// </summary>
internal string MutableDeclaration { get; }

/// <summary>
/// Gets a value indicating whether the field is read-only.
/// </summary>
internal bool IsReadonly => Declaration != MutableDeclaration;

/// <summary>
/// Gets a value indicating whether the field is read-only only because a constructor of the class
/// assigns it, so that it can be declared without the read-only modifier in a base class.
/// </summary>
internal bool IsAssignedByConstructor { get; }

/// <summary>
/// Gets a value indicating whether the field is one that every class has, and that any of its
/// methods can use without recording it, like the Compositor. Common fields move to the base
/// class together with the methods that are shared.
/// </summary>
internal bool IsCommon { get; }
}
}
45 changes: 45 additions & 0 deletions source/UIDataCodeGen/CodeGen/AnimatedVisualMemberUses.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

using System.Collections.Generic;

namespace CommunityToolkit.WinUI.Lottie.UIData.CodeGen
{
/// <summary>
/// The fields and methods of an AnimatedVisual class that some generated code uses, which the
/// generator records when it writes their names.
/// </summary>
sealed class AnimatedVisualMemberUses
{
readonly HashSet<string> _fields = new HashSet<string>();
readonly HashSet<string> _methods = new HashSet<string>();

/// <summary>
/// Gets the names of the fields that are used.
/// </summary>
internal IReadOnlyCollection<string> Fields => _fields;

/// <summary>
/// Gets the names of the methods that are used. Overloads have the same name, so a
/// use of the name is a use of all of the overloads.
/// </summary>
internal IReadOnlyCollection<string> Methods => _methods;

internal void UseField(string name) => _fields.Add(name);

internal void UseMethod(string name) => _methods.Add(name);

/// <summary>
/// Records the uses that were recorded by another instance.
/// </summary>
/// <param name="other">The uses to add.</param>
internal void Add(AnimatedVisualMemberUses other)
{
_fields.UnionWith(other._fields);
_methods.UnionWith(other._methods);
}
}
}
124 changes: 124 additions & 0 deletions source/UIDataCodeGen/CodeGen/AnimatedVisualMethod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable

using System.Collections.Generic;
using System.Linq;

namespace CommunityToolkit.WinUI.Lottie.UIData.CodeGen
{
/// <summary>
/// A method of an AnimatedVisual class: a factory that creates an object, or a helper
/// that the factories call. These are the methods that the AnimatedVisual classes of the
/// different versions of an IAnimatedVisualSource may share.
/// </summary>
sealed class AnimatedVisualMethod
{
string? _key;
string? _signature;
IReadOnlyList<string>? _headerLines;

internal AnimatedVisualMethod(string name)
{
Name = name;
}

/// <summary>
/// Gets the name of the method.
/// </summary>
internal string Name { get; }

/// <summary>
/// Gets the comments before the method. They are not part of what makes methods equal.
/// </summary>
internal CodeBuilder Comment { get; } = new CodeBuilder();

/// <summary>
/// Gets or sets the attribute of the method, or null.
/// </summary>
internal string? Attribute { get; set; }

/// <summary>
/// Gets the signature of the method, which can have several lines, for example if each
/// parameter is on its own line. The first line has the modifiers and the return type.
/// </summary>
internal CodeBuilder Header { get; } = new CodeBuilder();

/// <summary>
/// Gets the body of the method, including its braces.
/// </summary>
internal CodeBuilder Body { get; } = new CodeBuilder();

/// <summary>
/// Gets the fields and methods of the class that the method uses.
/// </summary>
internal AnimatedVisualMemberUses Uses { get; } = new AnimatedVisualMemberUses();

/// <summary>
/// Gets or sets a value indicating whether the method is static.
/// </summary>
internal bool IsStatic { get; set; }

/// <summary>
/// Gets a value indicating whether the signature has not been written. This is the case
/// for a helper method that has not been needed.
/// </summary>
internal bool IsEmpty => Header.IsEmpty;

/// <summary>
/// Gets the lines of the signature. Overloads have different signatures.
/// </summary>
internal IReadOnlyList<string> HeaderLines => _headerLines ??= Header.ToLines(0).ToArray();

/// <summary>
/// Gets the text that identifies the method among those of its class.
/// </summary>
internal string Signature => _signature ??= string.Join("\n", HeaderLines);

/// <summary>
/// Gets the text that is the same for two methods if and only if they have the same
/// attribute, signature and code, ignoring comments.
/// </summary>
internal string Key
=> _key ??= string.Join(
"\n",
new[] { Attribute ?? string.Empty }
.Concat(Header.ToLines(0, includeComments: false))
.Concat(Body.ToLines(0, includeComments: false)));

/// <summary>
/// Writes the method.
/// </summary>
/// <param name="builder">The <see cref="CodeBuilder"/> to write to.</param>
/// <param name="modifiers">Text to write before the signature, or null.</param>
internal void Write(CodeBuilder builder, string? modifiers = null)
{
builder.WriteCodeBuilder(Comment);
if (Attribute is not null)
{
builder.WriteLine(Attribute);
}

if (modifiers is null)
{
builder.WriteCodeBuilder(Header);
}
else
{
builder.WritePreindentedLines(HeaderLines.Select((line, i) => i == 0 ? modifiers + line : line));
}

builder.WriteCodeBuilder(Body);
}

/// <summary>
/// Writes the declaration of the method as abstract, which is what the shared base class
/// has for a method that the classes implement differently.
/// </summary>
/// <param name="builder">The <see cref="CodeBuilder"/> to write to.</param>
internal void WriteAbstractDeclaration(CodeBuilder builder)
=> builder.WriteLine($"internal abstract {HeaderLines.Single()};");
}
}
Loading
Loading