C
C#16mo ago
Chik3r

Custom MSBuild task fails to load `System.Runtime`

Hi, I'm making a custom MSBuild task that needs to load images and I was using ImageSharp for that. Before, I was targetting net6.0 and it worked when using dotnet build or Rider, but I now had to change to netstandard2.0 as I need to support VS and forgot that VS only likes netstandard. After I changed to netstandard2.0, a method using ImageSharp started to throw the following exception while running the task, but only from VS (it still works when using dotnet build):
Could not load file or assembly 'System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
at tModLoader.BuildTools.ModFile.ContentConverters.ToRaw(Stream src, Stream dst)
at tModLoader.BuildTools.ModFile.ContentConverters.Convert(String& resourceName, FileStream src, MemoryStream dst) in C:\Users\ikerv\Documents\Programming\tModLoader\tModLoader\BuildTools\ModFile\ContentConverters.cs:line 15
at tModLoader.BuildTools.Tasks.PackageModFile.AddResource(TmodFile tmodFile, String resourcePath) in C:\Users\ikerv\Documents\Programming\tModLoader\tModLoader\BuildTools\Tasks\PackageModFile.cs:line 253
at tModLoader.BuildTools.Tasks.PackageModFile.<>c__DisplayClass42_0.<Run>b__1(String resource) in C:\Users\ikerv\Documents\Programming\tModLoader\tModLoader\BuildTools\Tasks\PackageModFile.cs:line 109
at System.Threading.Tasks.Parallel.<>c__DisplayClass17_0`1.<ForWorker>b__1()
at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
at System.Threading.Tasks.Task.<>c__DisplayClass176_0.<ExecuteSelfReplicating>b__0(Object <p0>)
Could not load file or assembly 'System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
at tModLoader.BuildTools.ModFile.ContentConverters.ToRaw(Stream src, Stream dst)
at tModLoader.BuildTools.ModFile.ContentConverters.Convert(String& resourceName, FileStream src, MemoryStream dst) in C:\Users\ikerv\Documents\Programming\tModLoader\tModLoader\BuildTools\ModFile\ContentConverters.cs:line 15
at tModLoader.BuildTools.Tasks.PackageModFile.AddResource(TmodFile tmodFile, String resourcePath) in C:\Users\ikerv\Documents\Programming\tModLoader\tModLoader\BuildTools\Tasks\PackageModFile.cs:line 253
at tModLoader.BuildTools.Tasks.PackageModFile.<>c__DisplayClass42_0.<Run>b__1(String resource) in C:\Users\ikerv\Documents\Programming\tModLoader\tModLoader\BuildTools\Tasks\PackageModFile.cs:line 109
at System.Threading.Tasks.Parallel.<>c__DisplayClass17_0`1.<ForWorker>b__1()
at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
at System.Threading.Tasks.Task.<>c__DisplayClass176_0.<ExecuteSelfReplicating>b__0(Object <p0>)
7 Replies
Chik3r
Chik3rOP16mo ago
Here's the method's code:
public static void ToRaw(Stream src, Stream dst) {
using Image<Rgba32> image = Image.Load<Rgba32>(src);

using BinaryWriter writer = new(dst);
writer.Write(Version);
writer.Write(image.Width);
writer.Write(image.Height);

for (int y = 0; y < image.Height; y++) {
for (int x = 0; x < image.Width; x++) {
Rgba32 color = image[x, y];

if (color.A == 0) color = new Rgba32(0, 0, 0, 0);
dst.WriteByte(color.R);
dst.WriteByte(color.G);
dst.WriteByte(color.B);
dst.WriteByte(color.A);
}
}
}
public static void ToRaw(Stream src, Stream dst) {
using Image<Rgba32> image = Image.Load<Rgba32>(src);

using BinaryWriter writer = new(dst);
writer.Write(Version);
writer.Write(image.Width);
writer.Write(image.Height);

for (int y = 0; y < image.Height; y++) {
for (int x = 0; x < image.Width; x++) {
Rgba32 color = image[x, y];

if (color.A == 0) color = new Rgba32(0, 0, 0, 0);
dst.WriteByte(color.R);
dst.WriteByte(color.G);
dst.WriteByte(color.B);
dst.WriteByte(color.A);
}
}
}
Chik3r
Chik3rOP16mo ago
PackageReference to ImageSharp
Chik3r
Chik3rOP16mo ago
How can I stop this exception from happening when building from Visual Studio?
phaseshift
phaseshift16mo ago
VS only likes net standard? Wut?
Chik3r
Chik3rOP16mo ago
when using custom msbuild tasks i might be wrong though but when I tried using the net6.0 version it threw an error
Chik3r
Chik3rOP16mo ago
Chik3r
Chik3rOP16mo ago
Solved my issue My build task is ProjectReferenced by a net6.0 project that copies references to a custom folder, and it then passes a .targets file to other projects for them to use the build task. Since the custom task was using <PackageReference Include="SixLabors.ImageSharp" />, the reference was passed along to the net6.0 project which instead of using the netstandard2.0 version of ImageSharp used the netcoreapp3.1 version of ImageSharp. That version was then copied next to the BuildTools.dll file so that the references to ImageSharp were resolved. As it was the netcoreapp3.1 version, it failed when building from VS (since it uses net framework), so it threw the system.runtime error. I fixed this by adding PrivateAssets="all" to the ImageSharp PackageReference (in the custom build tool project), adding a <PackageReference Include="SixLabors.ImageSharp" Version="2.1.5" GeneratePathProperty="true" PrivateAssets="all" ExcludeAssets="all" /> just to get the path to the dll of imagesharp, and then adding a copy task to copy the netstandard2.0 dll next to the BuildTools.dll (<Copy SourceFiles="$(PkgSixLabors_ImageSharp)\lib\netstandard2.0\SixLabors.ImageSharp.dll" DestinationFolder="$(_ActualOutputDirectory)/Libraries/tModLoader.BuildTools/1.0.0/" SkipUnchangedFiles="True" />)
Want results from more Discord servers?
Add your server