Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by WakeZero · Dec 24, 2014 at 11:14 PM · iosaotjitserializer

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

iOS JIT compile error on Generic method called with basic types (int, bool, float) 1 Answer

A node in a childnode? 1 Answer

ios: 2 function calls on the same interface, one working... the other function call doing literally nothing 0 Answers

Saving Data on IOS produces JIT Error in Xcode 2 Answers

ExecutionEngineException : JIT/AOT error when adding to delegate 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges