Beispiel #1
0
        public BaseSerializableData load(string tag, DataLocation location)
        {
            BaseSerializableData data = null;

            if (location == DataLocation.memory)
            {
                if (!readCache(tag, out data))
                {
                    ZLog.log("TODO: cache miss, read from file");
                }
            }
            else if (location == DataLocation.local)
            {
                if (!readFile(_persistPath, tag, out data))
                {
                    ZLog.warn("TODO: file miss, read from net");
                }
            }
            else if (location == DataLocation.remote)
            {
                ZLog.error("not yet");
            }

            return(data);
        }
Beispiel #2
0
 private void writeCache(string tag, BaseSerializableData data)
 {
     if (_dataCache.ContainsKey(tag))
     {
         _dataCache[tag] = data;
     }
     else
     {
         _dataCache.Add(tag, data);
     }
 }
Beispiel #3
0
        private bool readCache(string tag, out BaseSerializableData data)
        {
            data = null;

            if (_dataCache.ContainsKey(tag))
            {
                data = _dataCache[tag];
                return(true);
            }

            return(false);
        }
Beispiel #4
0
        private readonly string _streamPath = Application.streamingAssetsPath; //r

        #region public

        public void save(string tag, BaseSerializableData data, DataLocation location)
        {
            data.dataTag = tag;

            if (location == DataLocation.memory)
            {
                writeCache(tag, data);
            }
            else if (location == DataLocation.local)
            {
                writeFile(_persistPath, tag, data);
            }
            else if (location == DataLocation.remote)
            {
                ZLog.error("not yet");
            }
        }
Beispiel #5
0
        private void writeFile(string folder, string tag, BaseSerializableData data)
        {
            Directory.CreateDirectory(folder);//如果已经有了就不会创建

            string     path   = _persistPath + "/" + tag + ".bytes";
            FileStream stream = new FileStream(path, FileMode.Create);//create new or overwritten

            if (serializationMethod == SerializationMethod.bin)
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, data);
            }
            else if (serializationMethod == SerializationMethod.json)
            {
                //string content = JsonUtility.ToJson(data);
            }

            stream.Close();
        }
Beispiel #6
0
        private bool readFile(string folder, string tag, out BaseSerializableData data)
        {
            data = null;

            string path = folder + "/" + tag + ".bytes";

            if (File.Exists(path))
            {
                FileStream stream = new FileStream(path, FileMode.Open);
                if (serializationMethod == SerializationMethod.bin)
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    data = formatter.Deserialize(stream) as BaseSerializableData;
                }
                else if (serializationMethod == SerializationMethod.json)
                {
                }

                stream.Close();
                return(true);
            }

            return(false);
        }