Skip to content

Commit edf9677

Browse files
committed
Handle runtime dlls
1 parent a83aaba commit edf9677

6 files changed

Lines changed: 645 additions & 160 deletions

File tree

src/UnityNuGet.Tests/UnityMetaTests.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ public class UnityMetaTests
88
[Test]
99
public void GetMetaForDll_FormatsDefineConstraintsProperly_WithoutConstraints()
1010
{
11-
var output = UnityMeta.GetMetaForDll(Guid.NewGuid(), true, Array.Empty<string>(), Array.Empty<string>());
11+
var platformDefs = PlatformDefinition.CreateAllPlatforms();
12+
var anyOs = platformDefs.Find(UnityOs.AnyOs, UnityCpu.AnyCpu);
13+
var output = UnityMeta.GetMetaForDll(Guid.NewGuid(), anyOs, Array.Empty<string>(), Array.Empty<string>());
1214
StringAssert.DoesNotContain("defineConstraints", output);
1315

1416
// This is on the same line in the template, so ensure it's intact
@@ -18,7 +20,9 @@ public void GetMetaForDll_FormatsDefineConstraintsProperly_WithoutConstraints()
1820
[Test]
1921
public void GetMetaForDll_FormatsLabelsProperly_WithoutLabels()
2022
{
21-
var output = UnityMeta.GetMetaForDll(Guid.NewGuid(), true, Array.Empty<string>(), Array.Empty<string>());
23+
var platformDefs = PlatformDefinition.CreateAllPlatforms();
24+
var anyOs = platformDefs.Find(UnityOs.AnyOs, UnityCpu.AnyCpu);
25+
var output = UnityMeta.GetMetaForDll(Guid.NewGuid(), anyOs, Array.Empty<string>(), Array.Empty<string>());
2226
StringAssert.DoesNotContain("labels", output);
2327

2428
// This is on the same line in the template, so ensure it's intact
@@ -30,7 +34,9 @@ public void GetMetaForDll_FormatsLabelsProperly_WithoutLabels()
3034
public void GetMetaForDll_FormatsDefineConstraintsProperly_WithConstraints(
3135
string[] constraints, string expected)
3236
{
33-
var output = UnityMeta.GetMetaForDll(Guid.NewGuid(), true, Array.Empty<string>(), constraints);
37+
var platformDefs = PlatformDefinition.CreateAllPlatforms();
38+
var anyOs = platformDefs.Find(UnityOs.AnyOs, UnityCpu.AnyCpu);
39+
var output = UnityMeta.GetMetaForDll(Guid.NewGuid(), anyOs, Array.Empty<string>(), constraints);
3440

3541
StringAssert.Contains(expected, output);
3642

@@ -43,7 +49,9 @@ public void GetMetaForDll_FormatsDefineConstraintsProperly_WithConstraints(
4349
public void GetMetaForDll_FormatsLabelsProperly_WithLabels(
4450
string[] labels, string expected)
4551
{
46-
var output = UnityMeta.GetMetaForDll(Guid.NewGuid(), true, labels, Array.Empty<string>());
52+
var platformDefs = PlatformDefinition.CreateAllPlatforms();
53+
var anyOs = platformDefs.Find(UnityOs.AnyOs, UnityCpu.AnyCpu);
54+
var output = UnityMeta.GetMetaForDll(Guid.NewGuid(), anyOs, labels, Array.Empty<string>());
4755

4856
StringAssert.Contains(expected, output);
4957

@@ -55,15 +63,29 @@ public void GetMetaForDll_FormatsLabelsProperly_WithLabels(
5563
[TestCase(false, "0")]
5664
public void GetMetaForDll_FormatsAnyPlatformEnabledProperly(bool value, string expected)
5765
{
58-
var output = UnityMeta.GetMetaForDll(Guid.NewGuid(), value, Array.Empty<string>(), Array.Empty<string>());
66+
PlatformDefinition platformDef;
67+
68+
if (value)
69+
{
70+
var platformDefs = PlatformDefinition.CreateAllPlatforms();
71+
platformDef = platformDefs.Find(UnityOs.AnyOs, UnityCpu.AnyCpu);
72+
}
73+
else
74+
{
75+
platformDef = new PlatformDefinition(UnityOs.AnyOs, UnityCpu.None, isEditorConfig: false);
76+
}
77+
78+
var output = UnityMeta.GetMetaForDll(Guid.NewGuid(), platformDef, Array.Empty<string>(), Array.Empty<string>());
5979

6080
StringAssert.Contains($"\n platformData:\n - first:\n Any:\n second:\n enabled: {expected}\n", output);
6181
}
6282

6383
[Test]
6484
public void GetMetaForDll_ContainsNoWindowsNewlines()
6585
{
66-
var output = UnityMeta.GetMetaForDll(Guid.NewGuid(), true, Array.Empty<string>(), new[] { "TEST" });
86+
var platformDefs = PlatformDefinition.CreateAllPlatforms();
87+
var anyOs = platformDefs.Find(UnityOs.AnyOs, UnityCpu.AnyCpu);
88+
var output = UnityMeta.GetMetaForDll(Guid.NewGuid(), anyOs, Array.Empty<string>(), new[] { "TEST" });
6789
StringAssert.DoesNotContain("\r", output);
6890
}
6991
}

src/UnityNuGet/NativeLibraries.cs

Lines changed: 16 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
using System.Threading;
66
using NuGet.Common;
77
using NuGet.Packaging;
8-
using Scriban;
98

109
namespace UnityNuGet
1110
{
1211
static class NativeLibraries
1312
{
14-
public static async IAsyncEnumerable<(string file, string[] folders, string platform, string architecture)> GetSupportedNativeLibsAsync(PackageReaderBase packageReader, ILogger logger)
13+
public static async IAsyncEnumerable<(string file, string[] folders, UnityOs os, UnityCpu cpu)> GetSupportedNativeLibsAsync(
14+
PackageReaderBase packageReader,
15+
ILogger logger)
1516
{
1617
var versions = await packageReader.GetItemsAsync(PackagingConstants.Folders.Runtimes, CancellationToken.None);
1718
var files = versions.SelectMany(v => v.Items);
@@ -35,144 +36,36 @@ static class NativeLibraries
3536
continue;
3637
}
3738

38-
var platform = system[0][..3] switch
39+
UnityOs? os = system[0][..3] switch
3940
{
40-
"lin" => "Linux",
41-
"osx" => "OSX",
42-
"win" => "Windows",
41+
"lin" => UnityOs.Linux,
42+
"osx" => UnityOs.OSX,
43+
"win" => UnityOs.Windows,
4344
_ => null
4445
};
4546

46-
if (platform is null)
47+
if (os is null)
4748
{
48-
logger.LogInformation($"Skipping file for unsupported platform: {file} ...");
49+
logger.LogInformation($"Skipping file for unsupported OS: {file} ...");
4950
continue;
5051
}
5152

52-
var architecture = system[1] switch
53+
UnityCpu? cpu = system[1] switch
5354
{
54-
"x86" => "x86",
55-
"x64" => "x86_64",
56-
"arm64" => "ARM64",
55+
"x86" => UnityCpu.X86,
56+
"x64" => UnityCpu.X64,
57+
"arm64" => UnityCpu.ARM64,
5758
_ => null
5859
};
5960

60-
if (architecture is null)
61+
if (cpu is null)
6162
{
62-
logger.LogInformation($"Skipping file for unsupported architecture: {file} ...");
63+
logger.LogInformation($"Skipping file for unsupported CPU: {file} ...");
6364
continue;
6465
}
6566

66-
yield return (file, folders, platform, architecture);
67+
yield return (file, folders, os.Value, cpu.Value);
6768
}
6869
}
69-
70-
private readonly struct PlatformEnables
71-
{
72-
public readonly int Editor, Linux64, OSXUniversal, Win, Win64;
73-
74-
public PlatformEnables(int editor, int linux64, int oSXUniversal, int win, int win64)
75-
{
76-
Editor = editor;
77-
Linux64 = linux64;
78-
OSXUniversal = oSXUniversal;
79-
Win = win;
80-
Win64 = win64;
81-
}
82-
}
83-
84-
public static string? GetMetaForNative(Guid guid, string platform, string architecture, string[] labels)
85-
{
86-
// TODO: Support other platforms
87-
PlatformEnables? enables = (platform, architecture) switch
88-
{
89-
("Linux", _) => new(1, 1, 0, 0, 0),
90-
("OSX", _) => new(1, 0, 1, 0, 0),
91-
("Windows", "x86") => new(0, 0, 0, 1, 0),
92-
("Windows", "x86_64") => new(1, 0, 0, 0, 1),
93-
_ => null,
94-
};
95-
96-
if (enables is null) return null;
97-
98-
const string text = @"{{ cpu(x) = x == 1 ? architecture : ""None"" }}fileFormatVersion: 2
99-
guid: {{ guid }}
100-
{{ labels }}PluginImporter:
101-
externalObjects: {}
102-
serializedVersion: 2
103-
iconMap: {}
104-
executionOrder: {}
105-
defineConstraints: []
106-
isPreloaded: 0
107-
isOverridable: 0
108-
isExplicitlyReferenced: 0
109-
validateReferences: 1
110-
platformData:
111-
- first:
112-
: Any
113-
second:
114-
enabled: 0
115-
settings:
116-
Exclude Editor: {{ 1 - enables.Editor }}
117-
Exclude Linux64: {{ 1 - enables.Linux64 }}
118-
Exclude OSXUniversal: {{ 1 - enables.OSXUniversal }}
119-
Exclude Win: {{ 1 - enables.Win }}
120-
Exclude Win64: {{ 1 - enables.Win64 }}
121-
- first:
122-
Any:
123-
second:
124-
enabled: 1
125-
settings: {}
126-
- first:
127-
Editor: Editor
128-
second:
129-
enabled: {{ enables.Editor }}
130-
settings:
131-
CPU: {{ cpu enables.Editor }}
132-
DefaultValueInitialized: true
133-
OS: {{ enables.Editor == 1 ? platform : ""None"" }}
134-
- first:
135-
Standalone: Linux64
136-
second:
137-
enabled: {{ enables.Linux64 }}
138-
settings:
139-
CPU: {{ cpu enables.Linux64 }}
140-
- first:
141-
Standalone: OSXUniversal
142-
second:
143-
enabled: {{ enables.OSXUniversal }}
144-
settings:
145-
CPU: {{ cpu enables.OSXUniversal }}
146-
- first:
147-
Standalone: Win
148-
second:
149-
enabled: {{ enables.Win }}
150-
settings:
151-
CPU: {{ cpu enables.Win }}
152-
- first:
153-
Standalone: Win64
154-
second:
155-
enabled: {{ enables.Win64 }}
156-
settings:
157-
CPU: {{ cpu enables.Win64 }}
158-
userData:
159-
assetBundleName:
160-
assetBundleVariant:
161-
";
162-
163-
return Template
164-
.Parse(text)
165-
.Render(new
166-
{
167-
guid = guid.ToString("N"),
168-
enables,
169-
platform,
170-
architecture,
171-
labels = labels.Length == 0
172-
? string.Empty
173-
: $"labels:\n{string.Concat(labels.Select(l => $" - {l}\n"))}",
174-
})
175-
.Replace("\r\n", "\n");
176-
}
17770
}
17871
}

0 commit comments

Comments
 (0)