Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

IsolatedStorage "No ApplicationIdentity available for AppDomain"

Discussion in 'Scripting' started by YondernautsGames, Mar 10, 2015.

  1. YondernautsGames

    YondernautsGames

    Joined:
    Nov 24, 2014
    Posts:
    328
    Hi there

    I'm in the process of porting one of my games over from Xamarin/MonoGame to Unity 5. My game has been out on the mobile app stores for a couple of years now and I want to update to be as seamless as possible. So far I only have 1 problem but it's a major one:

    On all platforms I used IsolatedStorage for game saves. With the .NET 2.0 (and subset) available for Unity the way I handle IsolatedStorage has changed slightly but still seems to compile fine. However, when I run the game in Unity I get an exception thrown:

    I've only had the chance to try this on standalone, but with my old setup it worked for me on desktop too. Is there anything I can do to get around this? It would be catastrophic if I can't load the save files from the old MonoGame version of the game into Unity. This does also mean that any answers along the lines of "save/load from this other folder instead" are missing the point. I'm already commited to IsolatedStorage (I've done a lot of searching for this problem and that's the way all the responses tend to head on this question)

    Thanks in advance
     
  2. YondernautsGames

    YondernautsGames

    Joined:
    Nov 24, 2014
    Posts:
    328
    A cut down version of the code I'm using in case it helps shed some light:

    Code (CSharp):
    1. private const string saveDirectory = "SaveData";
    2. private const string savePath = "SaveData/SaveData_001.dat";
    3.  
    4. using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
    5. {
    6.     // Check directory exists
    7.     string[] directories = storage.GetDirectoryNames(saveDirectory);
    8.     if (directories.Contains(saveDirectory) == true)
    9.     {
    10.         // Check if filename exists
    11.         string[] fileNames = storage.GetFileNames(savePath);
    12.         if (fileNames.Contains(savePath))
    13.         {
    14.             // Try to load the campaign info
    15.             using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(savePath, FileMode.Open, FileAccess.Read, storage))
    16.             {
    17.                 CampaignInfo info = new CampaignInfo();
    18.                 if (info.Load(stream)) {
    19.                     campaignState = info;
    20.                 }
    21.             }
    22.         }
    23.     }
    24. }
    I know it's a bit messy with the directory and file checks, but when I can use .NET4.x code onwards I'll be able to use IsolatedStorageFile.FileExists(path) and IsolatedStorageFile.DirectoryExists(path)
     
  3. Prashant_206

    Prashant_206

    Joined:
    Feb 26, 2014
    Posts:
    11
    Hi,
    I'm getting a similar error when I'm trying xml serialization in windows phone. Were you able to solve this ?
     
  4. YondernautsGames

    YondernautsGames

    Joined:
    Nov 24, 2014
    Posts:
    328
    Hi there.

    I'm not 100% sure on this, but after some digging I think that this specific error only shows up when running the game in the editor. As the game isn't then running as an application specifically, it has no ApplicationIdentity. Running the game outside of the editor as a standalone build or mobile seems to work but my testing on this part has been minimal so far.

    If this isn't your specific problem then I should note that I had trouble with serialization on Windows Phone too. It seems some C# libraries aren't available on Windows Phone and I have a feeling XML serialization was one. You might need to look for an alternative such as Protocol Buffers or an XML serialization plugin off the asset store.

    Best of luck
     
  5. Prashant_206

    Prashant_206

    Joined:
    Feb 26, 2014
    Posts:
    11
    Hi,
    Thanks for the reply. It actually works perfectly in the editor. I'm getting this error when I'm trying to build it in Unity. I didn't find anything reliable on asset store. I'll have a look at Protocol buffers. Here's my code

    using UnityEngine;
    using System.Collections.Generic;
    using System.Xml.Serialization;
    using System.IO;
    using UnityEngine.UI;
    using System.IO.IsolatedStorage;
    //using Windows.Storage;
    #if NETFX_CORE
    using XmlReader = WinRTLegacy.Xml.XmlReader;
    #else
    using XmlReader = System.Xml.XmlReader;
    #endif
    public class XMLSerialize : MonoBehaviour {


    TextAsset textFile;
    static AddressDirectory obj;
    public Text showtext;
    // Use this for initialization
    void Start () {

    //


    showtext = GetComponent<Text>();
    //showtext.text = "house:";
    obj = new AddressDirectory ();
    obj.addressList = new List<Address> ();
    obj.addressList.Add (new Address());
    obj.addressList [0].HouseNo = 50;
    // SaveXmlFile (obj);
    // AddressDirectory obj2 = LoadXmlFile ();
    // print (obj2.addressList [0].HouseNo);

    //
    // var storage = IsolatedStorageFile.GetUserStoreForApplication();
    IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
    // IsolatedStorageFile.GetStore( IsolatedStorageScope.User
    // | IsolatedStorageScope.Assembly, null, null );

    string[] fileNames = isoStore.GetFileNames( "temp.xml");

    print (fileNames.Length);
    foreach ( string file in fileNames )
    {
    if ( file =="temp.xml" )
    {
    Debug.Log("The file already exists!");
    }
    }


    IsolatedStorageFileStream oStream =
    new IsolatedStorageFileStream( "temp.xml",
    FileMode.Create, isoStore );

    StreamWriter writer = new StreamWriter( oStream );
    XmlSerializer serialize = new XmlSerializer (typeof(AddressDirectory));

    serialize.Serialize (writer,obj);
    writer.Close();


    AddressDirectory obj2 = LoadXmlFile ();
    print (obj2.addressList [0].HouseNo);
    // showtext.text = "house:"+obj2.addressList [0].HouseNo;
    }

    public static void SaveXmlFile(AddressDirectory d)
    {
    IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
    IsolatedStorageFileStream stream = null;

    stream = new IsolatedStorageFileStream("temp.xml",
    FileMode.OpenOrCreate,
    FileAccess.ReadWrite,
    storage);
    XmlSerializer serializer = new XmlSerializer(typeof (AddressDirectory));
    serializer.Serialize(stream, obj);


    // XmlSerializer serialize = new XmlSerializer (typeof(AddressDirectory));
    // TextWriter writer = new StringWriter() ;
    // serialize.Serialize (writer,d);
    // File.WriteAllText (Application.dataPath+"/temp.xml",writer.ToString ());
    // print (writer+ " " );


    }


    public static AddressDirectory LoadXmlFile()
    {
    //
    // IsolatedStorageFile isoStore =
    // IsolatedStorageFile.GetStore( IsolatedStorageScope.User
    // | IsolatedStorageScope.Assembly, null, null );
    IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();


    IsolatedStorageFileStream oStream =
    new IsolatedStorageFileStream( "temp.xml",
    FileMode.Open, isoStore );


    XmlSerializer serializer = new XmlSerializer(typeof (AddressDirectory));
    //serializer.Serialize(stream, obj);
    AddressDirectory gData = (AddressDirectory)serializer.Deserialize (oStream);


    // string str = File.ReadAllText (Application.dataPath+"/temp.xml");
    // XmlSerializer deSerialize = new XmlSerializer (typeof(AddressDirectory));
    // TextReader reader = new StringReader(str) ;
    // AddressDirectory gData = (AddressDirectory)deSerialize.Deserialize (reader);
    //print (str);

    return gData;
    }
    }

    public class AddressDirectory
    {
    [XmlElement("Address")]
    public List<Address> addressList = new List<Address>();

    }
    public class Address
    {
    public int HouseNo ;
    public string StreetName;
    public string City ;

    public Address()
    {
    HouseNo = 90;

    }
    }
     
  6. YondernautsGames

    YondernautsGames

    Joined:
    Nov 24, 2014
    Posts:
    328
    Yeah, see for me it's the "GetUserStoreForApplication()" function that errors when I run the game inside the editor. Why that wouldn't happen for you, I've no idea. For my code I actually use the BitConverter and write all my properties to the stream manually (my project started on Windows Phone / XNA and is now being converted to Unity, but needs existing player save files to survive the transition).

    Can I check you're definitely getting the "IsolatedStorageException: No ApplicationIdentity available for AppDomain." error? If not then I'd post a new question. If it is then you could possibly just skip the isolated storage and write files directly using Application.persistantDataPath for your root folder. In my situation I'm stuck with it, but if I didn't need my old saves to work then that's probably what I'd do.

    Oh and for future posts, please clean up your code and use the "Insert/Code" functionality to format it better. It's very hard to work through like that so you're unlikely to get a good reply.