コード例 #1
0
        private void SaveCache(string name, IVersionedDataCache cache)
        {
            //var begin = System.DateTime.Now;
            //double cost = 0.0;

            string path      = System.IO.Path.Combine(UnityEngine.Application.persistentDataPath, System.IO.Path.Combine(m_cacheDirectory, name));
            string directory = System.IO.Path.GetDirectoryName(path);

            if (!System.IO.Directory.Exists(directory))
            {
                if (System.IO.Directory.CreateDirectory(directory) == null)
                {
                    EB.Debug.LogError("DataCacheManager.SaveCache: create directory {0} failed when save {1}", directory, path);
                    return;
                }

                //EB.Debug.Log("DataCacheManager.SaveCache: create directory {0}", directory);
            }

            using (System.IO.FileStream fs = System.IO.File.Create(path))
            {
                IVersionedDataCacheSerializer serializer = DataCacheUtil.GetSerializer(cache.GetType());
                UnityEngine.Debug.Assert(serializer != null, "DataCacheManager.SaveCache: GetSerializer failed");
                serializer.Serialize(fs, cache);

                fs.Close();
                fs.Dispose();

                //cost = (System.DateTime.Now - begin).TotalSeconds;
                //EB.Debug.LogIf(cost > 0.1, "DataCacheManager.SaveCache: {0} saved, cost = {1}", path, cost);
#if UNITY_IPHONE
                UnityEngine.iOS.Device.SetNoBackupFlag(path);
#endif
            }
        }
コード例 #2
0
        private IVersionedDataCache LoadCache(string name, System.Type type)
        {
            EB.Debug.Log("Start LoadCache: name ={0} time = {1}", name, UnityEngine.Time.time);

            string path = System.IO.Path.Combine(UnityEngine.Application.persistentDataPath, System.IO.Path.Combine(m_cacheDirectory, name));

            if (!System.IO.File.Exists(path))
            {
                EB.Debug.Log("DataCacheManager.LoadCache: cache file does't exists {0}", path);
                return(null);
            }

            IVersionedDataCache cache = null;

            using (System.IO.FileStream fs = System.IO.File.OpenRead(path))
            {
                IVersionedDataCacheSerializer serializer = DataCacheUtil.GetSerializer(type);
                UnityEngine.Debug.Assert(serializer != null, "DataCacheManager.LoadCache: GetSerializer failed");
                cache = serializer.Deserialize(fs);
                if (cache == null)
                {
                    EB.Debug.LogWarning("DataCacheManager.LoadCache: Deserialize {0} failed, will remove it", path);
                    System.IO.File.Delete(path);
                }
            }

            EB.Debug.Log("End LoadCache: name ={0} time = {1}", name, UnityEngine.Time.time);

            return(cache);
        }