Skip to content

Commit 142a6d2

Browse files
committed
find references even if they don't use the "tools" directory structure
closes #11
1 parent 7b12463 commit 142a6d2

File tree

4 files changed

+41
-20
lines changed

4 files changed

+41
-20
lines changed

Contentless/Program.cs

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,24 +90,28 @@ private static int ProcessContentFile(FileInfo contentFile, string packagesFolde
9090
// replace mismatched reference paths if requested by the user
9191
if (referencesToEdit.Remove(libraryName)) {
9292
if (installedPackages.TryGetValue(libraryName, out var version)) {
93-
var fullLibraryPath = Program.CalculateFullPathToLibrary(packagesFolder, libraryName, version);
94-
if (reference != fullLibraryPath) {
95-
Console.WriteLine($"Changing reference from {reference} to {fullLibraryPath}");
96-
reference = fullLibraryPath;
97-
content[i] = referenceHeader + fullLibraryPath;
98-
changed = true;
93+
var fullLibraryPath = Program.FindFullLibraryPath(packagesFolder, libraryName, version);
94+
if (fullLibraryPath != null) {
95+
if (reference != fullLibraryPath) {
96+
Console.WriteLine($"Changing reference from {reference} to {fullLibraryPath}");
97+
reference = fullLibraryPath;
98+
content[i] = referenceHeader + fullLibraryPath;
99+
changed = true;
100+
} else {
101+
if (config.LogSkipped)
102+
Console.WriteLine($"Skipping reference replacement for {fullLibraryPath} which already matched");
103+
}
99104
} else {
100-
if (config.LogSkipped)
101-
Console.WriteLine($"Skipping reference replacement for {fullLibraryPath} which already matched");
105+
Console.Error.WriteLine($"Unable to find library {libraryName} in packages folder");
102106
}
103107
} else {
104108
Console.Error.WriteLine($"Unable to find existing reference {libraryName} in project file");
105109
}
106110
}
107111

108112
var refPath = Path.GetFullPath(Path.Combine(contentFile.DirectoryName, reference));
109-
Program.SafeAssemblyLoad(refPath);
110-
Console.WriteLine($"Using reference {refPath}");
113+
if (Program.SafeAssemblyLoad(refPath))
114+
Console.WriteLine($"Using reference {refPath}");
111115
}
112116

113117
if (referencesToEdit.Count > 0) {
@@ -126,14 +130,14 @@ private static int ProcessContentFile(FileInfo contentFile, string packagesFolde
126130
// add references that aren't in the content file yet
127131
foreach (var reference in referencesToEdit) {
128132
if (installedPackages.TryGetValue(reference, out var version)) {
129-
try {
130-
var path = Program.CalculateFullPathToLibrary(packagesFolder, reference, version);
133+
var path = Program.FindFullLibraryPath(packagesFolder, reference, version);
134+
if (path != null) {
131135
content.Insert(lastReferenceLine++, referenceHeader + path);
132136
changed = true;
133-
Program.SafeAssemblyLoad(path);
134-
Console.WriteLine($"Adding reference {path}");
135-
} catch (Exception e) {
136-
Console.Error.WriteLine($"Error adding reference {reference}: {e}");
137+
if (Program.SafeAssemblyLoad(path))
138+
Console.WriteLine($"Adding reference {path}");
139+
} else {
140+
Console.Error.WriteLine($"Unable to find library {reference} in packages folder");
137141
}
138142
} else {
139143
Console.Error.WriteLine($"Unable to find configured reference {reference} in project file");
@@ -227,11 +231,13 @@ private static int ProcessContentFile(FileInfo contentFile, string packagesFolde
227231
return 0;
228232
}
229233

230-
private static void SafeAssemblyLoad(string refPath) {
234+
private static bool SafeAssemblyLoad(string refPath) {
231235
try {
232236
Assembly.LoadFrom(refPath);
237+
return true;
233238
} catch (Exception e) {
234239
Console.Error.WriteLine($"Error loading reference {refPath}: {e}");
240+
return false;
235241
}
236242
}
237243

@@ -247,8 +253,21 @@ private static Dictionary<string, string> ExtractPackagesFromProject(string cspr
247253
return ret;
248254
}
249255

250-
private static string CalculateFullPathToLibrary(string packageFolder, string libraryName, string referencesVersion) {
251-
return Path.Combine(packageFolder, libraryName.ToLowerInvariant(), referencesVersion, "tools", $"{libraryName}.dll").Replace('\\', '/');
256+
private static string FindFullLibraryPath(string packageFolder, string libraryName, string version) {
257+
// we try several paths, finally arriving at the least specific one (which is just any file with the dll name)
258+
foreach (var dir in new[] {"tools", "lib/net*", "*"}) {
259+
foreach (var name in new[] {libraryName.ToLowerInvariant(), libraryName}) {
260+
var basePath = Path.Combine(packageFolder, name, version);
261+
if (!Directory.Exists(basePath))
262+
continue;
263+
foreach (var subdir in Directory.GetDirectories(basePath, dir, dir == "*" ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)) {
264+
var path = Path.Combine(subdir, $"{libraryName}.dll");
265+
if (File.Exists(path))
266+
return path.Replace('\\', '/');
267+
}
268+
}
269+
}
270+
return null;
252271
}
253272

254273
private static (List<ImporterInfo>, List<string>) GetContentData() {

Test/Content/Content.mgcb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#-------------------------------- References --------------------------------#
1212

1313
/reference:C:/Users/me/.nuget/packages/monogame.extended.content.pipeline/4.1.0/tools/MonoGame.Extended.Content.Pipeline.dll
14+
/reference:C:/Users/me/.nuget/packages/monogame.aseprite.content.pipeline/6.2.0/lib/net8.0/MonoGame.Aseprite.Content.Pipeline.dll
1415

1516
#---------------------------------- Content ---------------------------------#
1617

Test/Content/Contentless.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"Tiled/*.png"
77
],
88
"logSkipped": true,
9-
"references": ["MonoGame.Extended.Content.Pipeline"],
9+
"references": ["MonoGame.Extended.Content.Pipeline", "MonoGame.Aseprite.Content.Pipeline"],
1010
"overrides": {
1111
"*/Copy.*": {
1212
"copy": true

Test/Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
<ItemGroup>
99
<PackageReference Include="MonoGame.Extended.Content.Pipeline" Version="4.1.0" />
10+
<PackageReference Include="MonoGame.Aseprite.Content.Pipeline" Version="6.2.0" />
1011
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.3" />
1112
</ItemGroup>
1213

0 commit comments

Comments
 (0)