views:

376

answers:

3

I am porting an iphone game to windows phone 7. iphone works fairly similarly to Winmo7 in that you add all the files you want to be able to read to the project. we didn't want this extra step in our asset creation pipeline, so we just made it so all files were put into our own basic file archive, then just added that one archive file to the project. we then have an asset build process that exports all our assets, then creates this archive from them.

in winmo7 it caught me by surprise that you couldn't just do the same thing. as far as I know, the only way you can load data is though the content pipeline. we solved this fairly easily though by simply making a contentImporter that would just convert all the files to byte[] and export them as byte arrays, then you can simply load them an just directly access all the bytes that are in the file. unfortunately, unlike c++ where you would just cast memory to structures (because the file would already be stored in the structure's format) c# seems to require a more manual approach, where you use things such as BitConverter to load all the data into structures and classes from the byte array.

the thing is, we want to use our already existing asset export processes for things like textures and meshes, were we already have stuff setup for figuring out the exact pixel format that should be used for each texture ect. so in those cases we don't want to use the default Texture and Mesh content Importers. we tried making our on Texture ContentImporter, by simply making it return a Texture2D, but in order to create a Texture2D, you need a graphics device.

the second problem was the process of having to add every asset to the project. we decided that we didn't want to just load our dataArchive like we do for the iphone, because we DO want to use the default ContentImporters for Some of the data (like sound). but we solved this problem by making it so you just add one text file, with the root data dir in it, to the project, then made a ContentImporter that iterates through that directory structure and calls 'context.BuildAsset' on all the files there.

so summing up, we have one asset and ContentImporter that automatically handles the importing of all the assets in the data directory, thus solving the problem of having to add them manually to the project. some of these assets will be directed through the default ContentImporters (like sound and music, and xmls) while others will just be imported as byte[] and loaded manually, because we already have the asset in the format we want. in the case of those assets, it would be good if we could do the 'byte[] -> loaded manually' inside custom ContentImporters - offline- but for the first one we tried - Textures - it required a Graphics device to create the native Textue2D structure, and we couldn't find one in the ContentImporter framework.

so any thoughts? pointers? or is this the bestest way to do everything? I suppose another option would be to just convert all assets to a format the default Texture and Mesh processors can take in, and parameters to go with each of them (so we have a hand crafted 565 texture, convert it back to an 888 tga, then send it through the default texture pipeline with a parameter saying "convert this to 565")

+2  A: 

Hi Matt,

It's straight forward to open a text file and read the contents in a WP7 project. Here is one way.

    Uri linesUri = new Uri("lines.txt", UriKind.Relative);
    StreamResourceInfo stream = App.GetResourceStream(linesUri);
    StreamReader streamReader = new StreamReader(stream.Stream);
    var contents = streamReader.ReadToEnd();
    streamReader.Close();

I initially dragged the lines.txt file into my project from explorer - no other handling was necessary for this code to work.

Include the references you need...

using System.IO;
using System.Windows.Resources;
Mick N
wow, ok cool, I've never seen that way of loading resources before, I assume it will work with binary files too (using a streamreader and all).
matt
ok, I needed to add System.Windows to my project's references, and now everything works except it doesn't know what 'App' is.I looked into it and it seems to be Application.GetResourceStream, where Application is like some sort of control class for Silverlight apps? I should have mentioned, I'm using XNA
matt
A: 

Hi Matt,

Rephrase the question and I would be more than happy to help you. Specifically, give examples of the issues you are trying to solve. I have done a lot work with the content pipeline in XNA and could share with you some tips.

I will update my answer once you give more details.

Dennis Roche
done, updated, thanks
matt
Thanks Matt. I will have look over your updated question and get back to you soon as possible.
Dennis Roche
A: 

TitleContainer

its this new class they added in 4.0, and is a little less explicit than the File stuff

just go TitleContainer.OpenStream(path)

matt