Skip to content

Commit 42a35f0

Browse files
author
Simeon
authored
Fix for #406 (#433)
When using the "-AdditionalInterface" parameter in LottieGen (which causes the generated code to declare that it implements interfaces that it otherwise doesn't know about), the interface needs to be known to the generated IDL file. There are 2 ways that the IDL may know about the interface: 1. The interface may exist in a .winmd file that is referenced by the project. 2. The interface may be defined in the project using an IDL file. LottieGen doesn't know which case the user requires. Previously we were assuming case 2 which causes problems if the interface is not in an IDL file. Rather than requiring the user to tell LottieGen which case is being used, we know wrap the import in a condition that depends on the existence of the file. This should handle both cases seamlessly.
1 parent e7befb4 commit 42a35f0

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

source/LottieReader/Serialization/Animatables.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
6+
57
using System;
68
using System.Collections.Generic;
79
using System.Linq;
@@ -588,7 +590,7 @@ IEnumerable<KeyFrame<T>> ReadKeyFrames(
588590
// An initial keyframe is created to describe the initial value. It has no easing function.
589591
//
590592
// -
591-
T endValue = default(T);
593+
T? endValue = default(T);
592594

593595
// The initial keyframe has the same value as the initial value. Easing therefore doesn't
594596
// matter, but might as well use hold as it's the simplest (it does not interpolate).

source/UIDataCodeGen/CodeGen/Cppwinrt/CppwinrtInstantiatorGenerator.cs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,19 @@ string GenerateIdlText()
113113
{
114114
var builder = new CodeBuilder();
115115
builder.WriteLine(string.Join("\r\n", AutoGeneratedHeaderText));
116-
var idlImports = new List<string>();
117116

118-
idlImports.AddRange(SourceInfo.AdditionalInterfaces.Select(n => n.NormalizedQualifiedName));
119-
120-
if (idlImports.Any())
117+
if (SourceInfo.AdditionalInterfaces.Any())
121118
{
122-
foreach (var import in idlImports)
119+
foreach (var import in SourceInfo.AdditionalInterfaces.Select(n => n.NormalizedQualifiedName))
123120
{
124-
builder.WriteLine($"import \"{import}.idl\";");
121+
// Import an IDL file for the definition of each additional interface.
122+
// We don't know whether the user defined the interface in IDL or
123+
// whether they are reusing an interface that is already defined in
124+
// a .winmd, so be resilient to both cases by only importing the IDL
125+
// file if it exists.
126+
WriteImportFileIfExists(builder, $"{import}.idl");
127+
builder.WriteLine();
125128
}
126-
127-
builder.WriteLine();
128129
}
129130

130131
builder.WriteLine($"namespace {SourceInfo.Namespace}");
@@ -558,9 +559,7 @@ protected override void WriteImplementationFileStart(CodeBuilder builder)
558559
// {_cppwinrtGeneratedFileNameBase}.cpp is needed for UWP, but is not usually generated by
559560
// "cppwinrt for WIN32 apps. The file is related to how cppwinrt makes types activatable.
560561
// If the file exists, we'll include it.
561-
builder.WriteLine($"#if __has_include(\"{_cppwinrtGeneratedFileNameBase}.cpp\")");
562-
builder.WriteLine($"#include \"{_cppwinrtGeneratedFileNameBase}.cpp\"");
563-
builder.WriteLine("#endif");
562+
WriteIncludeFileIfExists(builder, $"{_cppwinrtGeneratedFileNameBase}.cpp");
564563
builder.WriteLine("#include <winrt/Windows.Foundation.Metadata.h>");
565564
builder.WriteLine("#include <winrt/Windows.Foundation.Collections.h>");
566565

@@ -1429,6 +1428,28 @@ protected override void WriteCanvasGeometryGroupFactory(CodeBuilder builder, Can
14291428
builder.WriteLine($"auto result = {FieldAssignment(fieldName)}winrt::make_self<CanvasGeometry>(group);");
14301429
}
14311430

1431+
// Writes code that is only included if the given file exists.
1432+
static void WriteIncludeFileIfExists(CodeBuilder builder, string fileName)
1433+
=> WriteIfFileExists(
1434+
builder,
1435+
fileName,
1436+
() => builder.WriteLine($"#include \"{fileName}\""));
1437+
1438+
// Writes code that is only imported if the given file exists.
1439+
static void WriteImportFileIfExists(CodeBuilder builder, string fileName)
1440+
=> WriteIfFileExists(
1441+
builder,
1442+
fileName,
1443+
() => builder.WriteLine($"import \"{fileName}\";"));
1444+
1445+
// Writes code that is only compiled if the given file exists.
1446+
static void WriteIfFileExists(CodeBuilder builder, string fileName, Action action)
1447+
{
1448+
builder.WriteLine($"#if __has_include (\"{fileName}\")");
1449+
action();
1450+
builder.WriteLine("#endif");
1451+
}
1452+
14321453
string QualifiedTypeName(PropertySetValueType propertySetValueType)
14331454
=> propertySetValueType switch
14341455
{

0 commit comments

Comments
 (0)