How to go about creating a nuget package which is dependent on a content asset?

Edit: I’m probably going to take a similar approach as MonoGame itself does for loading its resources for BasicEffect for example, which is embedding the compiled shader bytecode directly into the assembly.

Let’s say I’ve created a MonoGame assembly which I wish to share through nuget. However, this assembly also uses custom shaders. How should I go about embedding the compiled shaders file with the nuget package to ensure that the package consumer would not run into any issues when the shader is being loaded?

Edit: I’m probably going to take a similar approach as MonoGame itself does for loading its resources for BasicEffect for example, which is embedding the compiled shader bytecode directly into the assembly.

This sounds like a reasonable approach.

If for some reason it doesn’t work out you might also want to look into creating a nuspec file to build your NuGet package. In particular I think the content section might do the trick.

I got it working nicely by embedding the compiled effect directly into the assembly. What I did was compile the .fx using 2MGFX, include the compiled file to project as Embedded Content and load it at runtime using Assembly.GetManifestResourceStream.

If for some reason it doesn’t work out you might also want to look into creating a nuspec file to build your NuGet package. In particular I think the content section1 might do the trick.

That was also my initial plan, but when I started thinking more about it, I couldn’t figure out couple of things. For example, when the compiled effect file is brought in through Nuget, how to actually copy it to the project’s build output folder … what happens if content root directory is changed … what about possible naming conflicts with other asset files. This seemed like too much hassle.

I’d be still interested to find out if maybe there’s a better solution or how others have approached the problem :smile:

I was just wondering something similar, since I’ve had vague ideas of making a basic GUI library using my own font, which I would hope to include with the DLL as Texture2Ds, since I don’t know how to use Monogame’s spritefont API yet. How exactly did you embed the effect into the assembly?

I think embedding it in the assembly is the right solution. The only downside I can think of is that your users can’t edit the effect, if that’s something you wanted.

What does your NuGet package do anyway? You should post a link to it here when it’s ready.

How exactly did you embed the effect into the assembly?

In my case, it was really simple since the Effect class has a constructor which takes a byte[] array which is the exact data that is generated by the 2MGFX.exe tool found next to the MonoGame Pipeline GUI tool. Once I had generated the compiled effect, I included it in my project and set the file’s Content Type property to Embedded Resource. Now during compilation, the file’s content is embedded into the resultant assembly file. I load the embedded data and create an Effect instance by using the exact same approach that MonoGame itself is using to load its built-in effect files. Note that a good way to test and see all the embedded content in an assembly is to use the Assembly.GetManifestResourceNames method.

I’m not aware of the processing that’s going on when loading a Texture2D through the ContentManager (like at which point the data is compressed/decompressed etc), but one option definitely would be to get the raw texture data using Texture2D.GetData, embed that and load the texture by using Texture2D.SetData.

Anyway, good luck with your GUI library! :wink:

What does your NuGet package do anyway? You should post a link to it here when it’s ready.

I’ll share it some time in the upcoming days :smile:

Thanks a lot for the info! I’ll see what I can manage. The GUI certainly won’t be anything impressive, but maybe it can help people who are just starting out and don’t want to bother with making their own, or maybe someone can use it as the basis to make something greater.