コード例 #1
0
ファイル: EaSystem.cs プロジェクト: mytechniques/Ea
        public static T Open <T>  (string fileName, bool decryption = false) where T : IEaSerializable, new()
        {
            MonoSingleton <EaMobile> .Initialize();

            string      path       = EaDevice.filePath(typeof(T).Name.brackets() + fileName + EaDevice.fileType);
            T           @out       = new T();
            ThreadStart fileResult = new ThreadStart(() => {
                BinaryFormatter bf = new BinaryFormatter();
                if (path.exist())
                {
                    using (FileStream fs = File.Open(path, FileMode.Open)) {
                        try {
                                                        #if UNITY_EDITOR && DEBUG
                            var json = (string)bf.Deserialize(fs);
                            @out     = json.json_decode <T> ();
                                                        #elif UNITY_ANDROID || UNITY_IOS || UNITY_IOS
                            if (decryption)
                            {
                                string decryptor = (string)bf.Deserialize(fs);
                                @out             = decryptor.decrypt().json_decode <T>();
                            }
                            else
                            {
                                @out = (T)bf.Deserialize(fs);
                            }
                                                        #endif
                        } catch (Exception fileFormatException) {
                            Debug.LogError("Can't deserialize, file format not found!");
                            throw fileFormatException;
                        }
                    }
                }
                else
                {
                    using (FileStream fs = File.Create(path)) {
                        try {
                                                        #if UNITY_EDITOR && DEBUG
                            bf.Serialize(fs, @out.json_encode());
                                                        #elif UNITY_ANDROID || UNITY_IPHONE || UNITY_IOS
                            if (decryption)
                            {
                                string encryptor = @out.json_encode <T>().encrypt();
                                bf.Serialize(fs, encryptor);
                            }
                            else
                            {
                                bf.Serialize(fs, @out);
                            }
                                                        #endif
                        } catch (Exception failedSerializeException) {
                            Debug.LogError("Can't serialize object,make sure the object have attribute [System.Serializable]");
                            throw failedSerializeException;
                        }
                    }
                }
                @out.cryption = decryption;
                @out.path     = path;
                if (!EaMobile.openedFiles.Contains(path))
                {
                    EaMobile.openedFiles.Add(path);
                    EaMobile.onQuit += delegate {
                        Debug.Log(typeof(T).Name + " saving...");
                        @out.Save();
                    };

                    EaMobile.onPause += status => {
                        if (status)
                        {
                            Debug.Log(typeof(T).Name + " saving...");
                            @out.Save();
                        }
                    };
                }
            });
            Thread fileThread = new Thread(fileResult);

            fileThread.Start();
            fileThread.Join();

            return(@out);
        }