1
\$\begingroup\$

In the game I am developing, I have created a serializable class that generates a .map file for saving and loading levels. In XNA, this works great using the following code:

FileStream fileStream = new FileStream(filePath,
                FileMode.OpenOrCreate);
BinaryFormatter formatter = new BinaryFormatter();
MapInfo data = (MapInfo)formatter.Deserialize(fileStream);

Unfortunately, in android it appears that the filePath ends up at the device's root (argument being passed in as "Content/maps"). After some research, I came across this page from xamarin that explains how to properly load assets from the asset folder. However, this action takes place in a separate namespace from my main activity, and so far have not found a way to access the Assets.Open() function.

any other ways to load that asset?

UPDATE: After playing around some more, I ended up trying to pass the Assets property of the main activity down through function calls, loading the file this way:

Stream fileStream = androidGame.Assets.Open(filePath);
BinaryFormatter formatter = new BinaryFormatter();
MapInfo data = (MapInfo)formatter.Deserialize(fileStream);

SADLY, the file still does not load. Looking at the variables in debug shows the correct address for the activity contained in the variable androidGame, but says Open is an "Unknown member"

UPDATE WITH WORKING SOLUTION:

In case anyone comes across file loading issues in Monogame Android, this is what I ended up doing:

First, make sure you copy/reference your files in the correct dir of your android build. This should appear somewhere under Assets/Content/yourfile.file or subfolder.

Then, check the properties of the file and select "AndroidAsset" for build options and "Copy if newer" for output.

Finally, open the stream using the Assets property of your main activity. This can be accessed using your Microsoft.Xna.Framework.Game class in this way:

Stream fileStream = Game.Activity.Assets.Open(path);

Turns out my issue was not the stream itself, but the deserialization which could not open a .dll in android (overlooked that one...). I ended up switching to XML serialization, since the binary serialization is build dependent

\$\endgroup\$
  • \$\begingroup\$ This should probably be on Stack Overflow (or in Xamarin's forums). There's nothing especially game-specific going on here. \$\endgroup\$ – Andrew Russell Jun 16 '14 at 3:16
  • \$\begingroup\$ Alright, I will try somewhere else. Will post a workaround if I find a solution \$\endgroup\$ – PixelCake Games Jun 16 '14 at 6:57
  • \$\begingroup\$ @AndrewRussell - it is tagged with MonoGame. Otherwise we need to migrate all Unity3d assets questions as well. \$\endgroup\$ – Den Jun 16 '14 at 9:26
  • \$\begingroup\$ @StickerAddicts - so are you using MonoGame or just plain Xamarin Android? \$\endgroup\$ – Den Jun 16 '14 at 9:26
  • 1
    \$\begingroup\$ @AndrewRussell There's nothing game specific about the code in the question, but the answer is actually very specific to MonoGame or XNA so I would say it is definitely posted in the correct place. \$\endgroup\$ – craftworkgames Jun 23 '14 at 6:31
2
\$\begingroup\$

I believe you can use TitleContainer.OpenStream in MonoGame for a platform independent way of reading and writing files.

See the XNA documentation for reference:

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.titlecontainer.openstream.aspx

The thing you still have to be careful of is case sensitivity and formatting the path correctly. Otherwise I think it's relatively straightforward.

EDIT: Actually the XNA documentation is pretty sparse so here's an example that should give you the right idea. If your file is in the Content folder and you've set it to Content / Copy if newer in properties.

var filePath= Path.Combine(Content.RootDirectory, "FileName.dat");
using(var stream = TitleContainer.OpenStream(filePath))
{
    BinaryFormatter formatter = new BinaryFormatter();
    MapInfo data = (MapInfo)formatter.Deserialize(stream);

    // do your thing here
}
\$\endgroup\$
  • \$\begingroup\$ Thanks for the help, TitleContainer turned out to be handy. Although it did not actually solve this particular problem on the android version (monogame doesn't appear to have a complete implementation?), it simplified iOS and Windows builds \$\endgroup\$ – PixelCake Games Jun 26 '14 at 22:56
  • \$\begingroup\$ What error are you getting? I've been using TitleContainer on Android for a while now. \$\endgroup\$ – craftworkgames Jun 27 '14 at 2:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.