- Home /
Fixing AOT errors in iOS with BinaryWriter
I have a mobile Android game on the market, and I'm trying to port it to iOS. I have the game running on my test device, but I'm getting the following AOT exception when I try to save game progress:
EXCEPTION: SaveState - Attempting to JIT compile method 'System.Collections.Generic.Dictionary`2<string, object>:Do_CopyTo<System.Collections.Generic.KeyValuePair`2<string, object>, System.Collections.Generic.KeyValuePair`2<string, object>> (System.Collections.Generic.KeyValuePair`2<string, object>[],int,System.Collections.Generic.Dictionary`2/Transform`1<string, object, System.Collections.Generic.KeyValuePair`2<string, object>>)' while running with --aot-only.
at System.Collections.Generic.Dictionary`2[System.String,System.Object].CopyTo (System.Collections.Generic.KeyValuePair`2[] array, Int32 index) [0x00000] in <filename unknown>:0
at System.Collections.Generic.Dictionary`2[System.String,System.Object].GetObjectData (System.Runtime.Serialization.SerializationInfo info, StreamingContext context) [0x00000] in <filename unknown>:0
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.GetObjectData (System.Object obj, System.Runtime.Serialization.Formatters.Binary.TypeMetadata& metadata, System.Object& data) [0x00000] in <filename unknown>:0
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObject (System.IO.BinaryWriter writer, Int64 id, System.Object obj) [0x00000] in <filename unknown>:0
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectInstance (System.IO.BinaryWriter writer, System.Object obj, Boolean isValueObject) [0x00000] in <filename unknown>:0
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteQueuedObjects (System.IO.BinaryWriter writer) [0x00000] in <filename unknown>:0
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectGraph (System.IO.BinaryWriter writer, System.Object obj, System.Runtime.Remoting.Messaging.Header[] headers) [0x00000] in <filename unknown>:0
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers) [0x00000] in <filename unknown>:0
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph) [0x00000] in <filename unknown>:0
at EncryptionManager.EncryptFile (System.String _path, System.Object _data, System.Byte[] _key) [0x00000] in <filename unknown>:0
at StateManager.SaveState () [0x00000] in <filename unknown>:0
I understand the issue, and I've tried creating AOT hints for the compiler with no luck. Can anyone suggest a fix? I don't want to incorporate another library or package because I want to keep the Android and iOS code base the same, and I already have Android users with save data. Here is my current setup:
public static void EncryptFile(string _path, object _data, byte[] _key = null)
{
// Check parameters
if (_data == null) return;
if (_path == null || _path.Length <= 0) return;
// Create AES service provider
using (RijndaelManaged aes = new RijndaelManaged())
{
// Set aes paramaters
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.ISO10126;
// Set key
aes.Key = _key;
// Set iv
aes.GenerateIV();
// Create file stream
using (FileStream file = new FileStream(_path, FileMode.Create))
{
// Write iv to file
file.Write(aes.IV, 0, aes.IV.Length);
// Create crypto stream
using (CryptoStream crypto = new CryptoStream(file, aes.CreateEncryptor(aes.Key, aes.IV), CryptoStreamMode.Write))
{
// Serialize data
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(crypto, _data);
}
}
}
}
I'm passing in a Dictionary< string, object > for _data.
Answer by WakeZero · Jan 02, 2015 at 12:23 AM
I ended up having to do several things to fix my issue. First up, I had to add the following code to the top of EncryptFile and DecryptFile:
Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
Second, for each Dictionary I was trying to store with a string as a key, I had to switch to use a Hashtable. This seems to only be an issue with reference objects as keys, because it works fine for integer keys.
Third, I had to make sure that each of my custom classes stored within the Hashtable/Dictionary implemented ISerializable. I had previously only included the [Serializable] tag, which is enough for JIT, but not AOT.
Hopefully this helps anyone else with similar issues.
Answer by RudyTheDev · Dec 24, 2014 at 11:22 PM
iOS AOT compiler does not fully support generics. It cannot resolve ahead of time what object
is going to be. So it simply fails because it would have to "include" every possible type. As far as I know, you cannot use JIT on iOS and so there's nothing you can really do about this. There are various workarounds and compatible libraries, but no real quick fix (that I know of, and I'd like one myself).