C# Load Resource from DLL (ResourceManager) - c#

I need to load some .Xnb files from a DLL in a Xna Game. For this, there is a "ResourceContentManager" which takes a "ResourceManager" in the Constructor. So how to add the files as Embedded Resources to the DLL and initialize a ResourceManager? The Following Code didnt worked (namespace is "Mox")
ResourceManager resourceManager = new ResourceManager("Mox", Assembly.GetExecutingAssembly());
Stream s = resourceManager.GetStream("Shader");
if(s == 0)
throw new Exception();
I added the Resource "Shader.fx" with "Add->Existing" and then set the Build to Embedded Resource and "copy always" ... this throws an Exception so I know it didnt loaded correctly... any suggestions?

I asked a similar question over at gamedev.stackexchange.com. The answer may help you as well, see below:
I have another solution in addition to Russell's which allows you to use the content manager and allows you to embed all of the types of content XNA supports.
XNA supports the ContentManager though a resource instead of a content project. To use it do the below. Of course you will need to pass a reference of your game's services at some point.
ResourceContentManager Content = new ResourceContentManager(game.Services, Resource1.ResourceManager);
Use this to compile the shader or anything to a xnb.
Add any and all of the XNB to your resources. Them simply load your content as usual.
Content.Load<Texture2D>(".\\assetName")

Related

Winform embeded XNA contentmanager only recognize XNB

I'm writing a MapEditor in winform with embeded XNA.
And my biggest problem is whenever I try to load any file with the ContentManager, it only reads .XNB files.
I wanted to read an effect file like this:
effect = contentManager.Load<Effect>("Effect2");
But then I get the error that "Effect2.xnb doesn't exists".
And if I add "Effect2.fx" it still give me error with the message "Effect2.fx.xnb doesn't exists" :\
I have no idea what to do.
I saw a solution by set the content properties. But I cannot set those properties in this case,because it's a winform application.
Does anyone have a great idea or anything?
Edit!
Solution found!
I implemented the ContentBuilder from this example with writing a few helper methods for dynamic loading: http://create.msdn.com/en-US/education/catalog/sample/winforms_series_2
You'll have to add an XNA Content project, and include your content in that project. All of those files will be compiled/processed into individual XNB files, which you'll be able to load. Just make sure to reference that project in your WinForms project.
The same rules apply for a Game project: It still needs an accompanying Content project.
EDIT:
Ahh, right... you want to load them dynamically. For that, you'll need to ensure that any computer that tries to use your map editor will have the XNA development framework installed. Then you'll need to process the files manually before loading them with the ContentManager... not entirely sure how to do this.
EDIT:
Take a look at this post for more information about loading unprocessed content at runtime: How do I load a texture in XNA at runtime?

C# load external dll with embedded images

I've a tiny application with several icons. I decided to put them in a separated DLL, so within my solution, I created a project and I've embedded those icons as resources.
Then I compiled this library (myImages.dll)
After, I added that dll as reference in the main program and I used the ResourceManager to get the reference of my custom library. Then I pointed to use GetObject method to retrieve the name of each file.
The following code works, but an error occurs when I try to retrieve an icon:
ResourceManager rm = new ResourceManager("myAssembly.MyNamespace", Assembly.LoadFile("Images.dll"));
Image myImage = ((System.Drawing.Image)(rm.GetObject("myIcon")));
Seems that the reference to my Images.dll doesn't work properly.
Any hints?
Usually when you get resources by name, you need to provide a fully qualified name. If you call GetManifestResourceNames, you can get a list of the names in your loaded resource manager to easily find out exactly what the names area that you must use.

How do I replace embedded resources in a .NET assembly programmatically?

I am trying to replace a Resource of an exe (.NET, C#) file using C# code.
I have found this article and made this code (using Mono.Cecil 0.6):
AssemblyDefinition asdDefinition = AssemblyFactory.GetAssembly("C:\\File.exe");
EmbeddedResource erTemp = new EmbeddedResource("encFile", ManifestResourceAttributes.Public);
erTemp.Data = myNewFileBytes;
asdDefinition.MainModule.Resources.RemoveAt(0);
asdDefinition.MainModule.Resources.Add(erTemp);
AssemblyFactory.SaveAssembly(asdDefinition, "C:\\newFile.exe");
The code is actually removing the resource and then adding a new one with the same name.
The resource name is encFile and stored as encFile.exe (tried both).
I tested the code and the remove is working (i can tell by the size of the file) and the adding too, but the new file crash just like the file i created with the remove only (for the testing) - it acts like he can't see the replaced resource.
What can i do to fix it? Maybe some changes in the edited EXE file?
The EXE file reads its resource this way:
byte[] buffer = ProjectName.Properties.Resources.encFile;
Trying to do this seems overly complex. If you need dynamic update of resources, ship your resources as a folder for your application (set items in the folder as content and copy if newer in project properties).
If you need dynamic update at runtime, then it's as simple as either:
1] Allow user to replace items in place or
2] Even better, treat it like word-press themes and allow an override folder for each resource.
If you need to tag each resource with metadata you could use a sqlite database or even easier, allow a matching .meta file for each resource to describe it in more detail.
Finally, if you are allowing digital download of your software, then you might consider code-signing your executable - in which case modifying the executable in any way will not be an option.

How can I inject a file into an EXE at runtime and reference the file during program operation?

I'd like a user to download an exe from my website, where (synchronously upon download) an XML file is injected into this application. This XML file contains a public key, and a signature.
How do I inject the file prior to downloading and reference it later during execution?
Ideally I won't be using a shell to inject the file, rather a native .NET api.
You could that easily with Mono.Cecil, you'd just have to write something like:
var module = ModuleDefinition.ReadModule ("Application.exe");
module.Resources.Add (
new EmbeddedResource (
"signature.xml",
ManifestResourceAttributes.Private,
File.ReadAllBytes ("signature.xml")));
module.Write ("Application.exe",
new WriterParameters {
StrongNameKeyPair = new StrongNameKeyPair ("keypair.snk")
});
To inject the signature.xml resource from the signature.xml file, and sign back your assembly with your keypair.snk that you used to sign Application.exe.
And at runtime, you'd just have to use:
var stream = Assembly.GetExecutingAssembly ()
.GetManifestResourceStream ("signature.xml");
To retrieve the resource.
To inject the file add it to your project. Then right-click on it in the solution explorer, go to properties, and change its type to EmbeddedResource.
To load it at run-time use Assembly.GetManifestResourceStream(). Read more here: http://msdn.microsoft.com/en-us/library/xc4235zt.aspx.
From what he writes it seems he's gonna dynamically change the file prior to download.
This really depends on the server-side language you use, And how much control you have over your server/hosting provider account.
Say you have a download.aspx file which generates this exe files and sends for download.
One thing you can do is to put the assembly's source on your server then download.aspx assembles it and send it for download. (The difficult way)
Another way is to put the compiled assembly on server then use e.g cecil ( Programmically embed resources in a .NET assembly ) or whatever to change it then send it for download.
This google result seems promising.
Add the file to your project, typically something along the lines of:
+solution
+project
+Resources
+SomeDirectory
-SomeFile
Then go to your project's properties, go to the resources tab on the left site and select the files resource on the top nav bar. Select to add a resource > add existing file. Browse to the file you just put into your project and select to add it.
The file will now show up under your Resources tab of your project's properties. Change the name of your file in the Resources tab to be more meaningful.
The file is now an embedded resource of your project and you can access it by the following in code:
var MyFile = Properties.Resources.MyFile

C# - Integrating data (pictures) into .exe

this is my first question here.
I've made a small quiz project about the 199 world's states via Visual Studio C#.
therefor I collected all flags of every country and put them into a folder - you can imagine that i collected MANY.
To run my current project i need the folder with all these .gif images -
otherwise the startup will end in a fatal error. :-(
My question is if it is possible to integrate the images into my .exe file so that i can run it without that nasty folder. (Also important for future project with even more content!)
And if it is possible - how?
It would also be nice if you let me know how to use the images - what pathes they have got etc ... =)
Thanks in advance!
Robbepop
You can add a resource file to your application, by going to
Project >> Your Project Properties >>
Resources >> Create A Resource File.
You can then simply add any image to your application and reference it via your code. Select Images from the drop down on the top of the menu, and then click Add Resource >> From existing file.
After you save your resource file, you can then access your images via code, e.g.
> Image img =
> YourProject.Properties.Resources.Image1
However, with the number of images you have, and what I would believe you are using them for, I would suggest using a Image List, which you can add all of your images to, and access them via their key or index. e.g.
Image img = imageList1[0];
Or
Image img = imageList1["US"];
This can be found in your toolbox.
You can add the images as embedded resources. Then use the ManifestResourceStream from the Assembly to extract the raw byte data, and subsequently load it as an image.
Note: this is different from the answer/approach of Femaref below.
Every assembly in .net can contain so called resources. You can add them in the properties of a project in the resources tab. After that, you can access them via ProjectNameSpace.Properties.Resources.

Categories

Resources