Exemplo n.º 1
0
        public void Add(OnlineMapsCache cache, OnlineMapsTile tile, byte[] bytes)
        {
#if ALLOW_FILECACHE
            StringBuilder filename      = cache.GetShortTilePath(tile);
            string        shortFilename = filename.ToString();
            if (Contains(shortFilename))
            {
                return;
            }

            string fullFilename = cache.GetFullTilePath(shortFilename);

            OnlineMapsThreadManager.AddThreadAction(() =>
            {
                FileInfo fileInfo = new FileInfo(fullFilename);
                if (!Directory.Exists(fileInfo.DirectoryName))
                {
                    Directory.CreateDirectory(fileInfo.DirectoryName);
                }
                File.WriteAllBytes(fullFilename, bytes);
            });

            AddItem(shortFilename, bytes.Length);
            _size += bytes.Length;
#endif
        }
Exemplo n.º 2
0
    protected bool TryLoadFromCache(Tile tile)
    {
        if (!cacheElevations)
        {
            return(false);
        }

        byte[] data = OnlineMapsCache.Get(tile.GetCacheKey(cachePrefix));
        if (data == null || data.Length != tileWidth * tileHeight * 2)
        {
            return(false);
        }

        short[,] elevations = new short[tileWidth, tileHeight];
        int dataIndex = 0;

        for (int y = 0; y < tileHeight; y++)
        {
            for (int x = 0; x < tileWidth; x++)
            {
                elevations[x, y] = (short)((data[dataIndex + 1] << 8) + data[dataIndex]);
                dataIndex       += 2;
            }
        }

        SetElevationData(tile, elevations);
        return(true);
    }
        public void Save(OnlineMapsCache cache)
        {
#if ALLOW_FILECACHE
            StringBuilder builder = cache.GetFileCacheFolder();
            builder.Append("/").Append(atlasFilename);
            string filename = builder.ToString();

            FileInfo fileInfo = new FileInfo(filename);
            if (!Directory.Exists(fileInfo.DirectoryName))
            {
                Directory.CreateDirectory(fileInfo.DirectoryName);
            }

            FileStream   stream = new FileStream(filename, FileMode.Create);
            BinaryWriter writer = new BinaryWriter(stream);

            writer.Write((byte)'T');
            writer.Write((byte)'C');
            writer.Write(ATLAS_VERSION);

            writer.Write(_size);

            for (int i = 0; i < count; i++)
            {
                FileCacheItem item = items[i];
                writer.Write(item.filename);
                writer.Write(item.size);
                writer.Write(item.time);
            }

            (writer as IDisposable).Dispose();
#endif
        }
Exemplo n.º 4
0
        public byte[] GetBytes(OnlineMapsCache cache)
        {
#if ALLOW_FILECACHE
            string path = GetFullPath(cache);
            if (!File.Exists(path)) return null;
            return File.ReadAllBytes(path);
#else
            return null;
#endif
        }
Exemplo n.º 5
0
        public string GetFullPath(OnlineMapsCache cache, string shortFilename)
        {
#if ALLOW_FILECACHE
            StringBuilder stringBuilder = cache.GetFileCacheFolder();
            stringBuilder.Append("/").Append(shortFilename).Append(".png");
            return stringBuilder.ToString();
#else
            return null;
#endif
        }
Exemplo n.º 6
0
        public string GetFullPath(OnlineMapsCache cache)
        {
#if ALLOW_FILECACHE
            StringBuilder stringBuilder = cache.GetFileCacheFolder();
            stringBuilder.Append("/CustomCache/").Append(hash).Append(".dat");
            return stringBuilder.ToString();
#else
            return null;
#endif
        }
Exemplo n.º 7
0
        public byte[] GetItem(OnlineMapsCache cache, string key)
        {
            int hash = key.GetHashCode();

            for (int i = 0; i < count; i++)
            {
                CustomCacheItem item = items[i];
                if (item.hash == hash && item.key == key) return item.GetBytes(cache);
            }

            return null;
        }
        public void Load(OnlineMapsCache cache)
        {
#if ALLOW_FILECACHE
            StringBuilder builder = cache.GetFileCacheFolder();
            builder.Append("/").Append(atlasFilename);
            string filename = builder.ToString();

            if (!File.Exists(filename))
            {
                return;
            }

            FileStream   stream = new FileStream(filename, FileMode.Open);
            BinaryReader reader = new BinaryReader(stream);

            byte c1 = reader.ReadByte();
            byte c2 = reader.ReadByte();

            int cacheVersion = 0;

            if (c1 == 'T' && c2 == 'C')
            {
                cacheVersion = reader.ReadInt16();
                if (cacheVersion > 0)
                {
                }
            }
            else
            {
                stream.Position = 0;
            }

            _size = reader.ReadInt32();

            long l = stream.Length;
            while (stream.Position < l)
            {
                filename = reader.ReadString();
                int           s    = reader.ReadInt32();
                long          time = reader.ReadInt64();
                FileCacheItem item = new FileCacheItem(filename, s, time);
                if (capacity <= count)
                {
                    capacity += 100;
                    Array.Resize(ref items, capacity);
                }
                items[count++] = item;
            }

            (reader as IDisposable).Dispose();
#endif
        }
Exemplo n.º 9
0
    protected override void CacheSerializedFields()
    {
        cache = target as OnlineMapsCache;
        map   = cache.GetComponent <OnlineMaps>();

        pUseMemoryCache        = serializedObject.FindProperty("useMemoryCache");
        pUseFileCache          = serializedObject.FindProperty("useFileCache");
        pFileCacheLocation     = serializedObject.FindProperty("fileCacheLocation");
        pFileCacheCustomPath   = serializedObject.FindProperty("fileCacheCustomPath");
        pFileCacheTilePath     = serializedObject.FindProperty("fileCacheTilePath");
        pMaxFileCacheSize      = serializedObject.FindProperty("maxFileCacheSize");
        pMaxMemoryCacheSize    = serializedObject.FindProperty("maxMemoryCacheSize");
        pMemoryCacheUnloadRate = serializedObject.FindProperty("memoryCacheUnloadRate");
        pFileCacheUnloadRate   = serializedObject.FindProperty("fileCacheUnloadRate");
    }
Exemplo n.º 10
0
    private void OnTileDownloaded(Tile tile, OnlineMapsWWW www)
    {
        if (www.hasError)
        {
            Debug.Log("Download error");
            return;
        }

        if (OnDownloadSuccess != null)
        {
            OnDownloadSuccess(tile, www);
        }
        string response = www.text;

        OnlineMapsCache.Add(tile.GetCacheKey(cachePrefix), Encoding.UTF8.GetBytes(response));
        ParseResponse(tile, response);
    }
Exemplo n.º 11
0
        public void Add(OnlineMapsCache cache, string key, byte[] bytes)
        {
#if ALLOW_FILECACHE
            int hash = key.GetHashCode();

            int index = -1;
            string path = "";

            for (int i = 0; i < count; i++)
            {
                CustomCacheItem item = items[i];
                if (item.hash == hash && item.key == key)
                {
                    size -= item.size;
                    item.size = bytes.Length;
                    size += bytes.Length;

                    for (int j = i + 1; j < count; j++) items[j - 1] = items[j];
                    items[count - 1] = item;
                    path = item.GetFullPath(cache);
                    index = i;
                    break;
                }
            }

            if (index == -1)
            {
                CustomCacheItem item = AddItem(key, bytes.Length);
                path = item.GetFullPath(cache);
                size += bytes.Length;
            }

#if !UNITY_WEBGL
            OnlineMapsThreadManager.AddThreadAction(() =>
            {
#endif
                FileInfo fileInfo = new FileInfo(path);
                if (!Directory.Exists(fileInfo.DirectoryName)) Directory.CreateDirectory(fileInfo.DirectoryName);
                File.WriteAllBytes(path, bytes);
#if !UNITY_WEBGL
            });
#endif
#endif
        }
Exemplo n.º 12
0
        public override void DeleteOldItems(OnlineMapsCache cache)
        {
#if ALLOW_FILECACHE
            int unloadSize = Mathf.RoundToInt(cache.maxCustomCacheSize * cache.customCacheUnloadRate);

            int s = 0;
            int countUnload = 0;

            while (countUnload < items.Length && s < unloadSize)
            {
                s += items[countUnload].size;
                countUnload++;
            }

            string[] unloadFiles = new string[countUnload];

            for (int i = 0; i < countUnload; i++)
            {
                CustomCacheItem item = items[i];
                size -= item.size;
                string fullFilename = item.GetFullPath(cache);
                unloadFiles[i] = fullFilename;
                items[i] = null;
            }

            for (int i = countUnload; i < count; i++) items[i - countUnload] = items[i];

            count -= countUnload;

#if !UNITY_WEBGL
            OnlineMapsThreadManager.AddThreadAction(() =>
            {
#endif
                for (int i = 0; i < countUnload; i++)
                {
                    string fn = unloadFiles[i];
                    if (File.Exists(fn)) File.Delete(fn);
                }
#if !UNITY_WEBGL
        });
#endif
#endif
        }
Exemplo n.º 13
0
        public void Save(OnlineMapsCache cache)
        {
#if ALLOW_FILECACHE
            StringBuilder builder = cache.GetFileCacheFolder();
            builder.Append("/").Append(atlasName);
            string filename = builder.ToString();

            FileInfo fileInfo = new FileInfo(filename);
            if (!Directory.Exists(fileInfo.DirectoryName)) Directory.CreateDirectory(fileInfo.DirectoryName);

            T[] itemsCopy = new T[items.Length];
            items.CopyTo(itemsCopy, 0);

#if !UNITY_WEBGL
            OnlineMapsThreadManager.AddThreadAction(() =>
            {
#endif
                FileStream stream = new FileStream(filename, FileMode.Create);
                BinaryWriter writer = new BinaryWriter(stream);

                writer.Write((byte)'T');
                writer.Write((byte)'C');
                writer.Write(ATLAS_VERSION);

                writer.Write(size);

                for (int i = 0; i < count; i++)
                {
                    T item = itemsCopy[i];
                    writer.Write(item.key);
                    writer.Write(item.size);
                    writer.Write(item.time);
                }

                writer.Close();
#if !UNITY_WEBGL
        });
#endif
#endif
        }
Exemplo n.º 14
0
    protected void SetElevationToCache(Tile tile, short[,] elevations)
    {
        if (!cacheElevations)
        {
            return;
        }

        byte[] cache      = new byte[tileWidth * tileHeight * 2];
        int    cacheIndex = 0;

        for (int y = 0; y < tileHeight; y++)
        {
            for (int x = 0; x < tileWidth; x++)
            {
                short s = elevations[x, y];
                cache[cacheIndex++] = (byte)(s & 255);
                cache[cacheIndex++] = (byte)(s >> 8);
            }
        }

        OnlineMapsCache.Add(tile.GetCacheKey(cachePrefix), cache);
    }
Exemplo n.º 15
0
 private void OnEnable()
 {
     cache = target as OnlineMapsCache;
     map   = cache.GetComponent <OnlineMaps>();
     CacheSerializedFields();
 }
Exemplo n.º 16
0
        public override void DeleteOldItems(OnlineMapsCache cache)
        {
#if ALLOW_FILECACHE
            int countUnload = Mathf.RoundToInt(count * cache.fileCacheUnloadRate);
            if (countUnload <= 0) throw new Exception("Can not unload a negative number of items. Check fileCacheUnloadRate.");
            if (count < countUnload) countUnload = count;

            long[] unloadTimes = new long[countUnload];
            int[] unloadIndices = new int[countUnload];
            string[] unloadFiles = new string[countUnload];
            int c = 0;

            for (int i = 0; i < count; i++)
            {
                long t = items[i].time;
                if (c == 0)
                {
                    unloadIndices[0] = 0;
                    unloadTimes[0] = t;
                    c++;
                }
                else
                {
                    int index = c;
                    int index2 = index - 1;

                    while (index2 >= 0)
                    {
                        if (unloadTimes[index2] < t) break;

                        index2--;
                        index--;
                    }

                    if (index < countUnload)
                    {
                        for (int j = countUnload - 1; j > index; j--)
                        {
                            unloadIndices[j] = unloadIndices[j - 1];
                            unloadTimes[j] = unloadTimes[j - 1];
                        }
                        unloadIndices[index] = i;
                        unloadTimes[index] = t;
                        if (c < countUnload) c++;
                    }
                }
            }

            for (int i = 0; i < countUnload; i++)
            {
                int index = unloadIndices[i];
                size -= items[index].size;
                string fullFilename = GetFullPath(cache, items[index].key);
                unloadFiles[i] = fullFilename;
                items[index] = null;
            }

            int offset = 0;
            for (int i = 0; i < count; i++)
            {
                if (items[i] == null) offset++;
                else if (offset > 0) items[i - offset] = items[i];
            }

            count -= countUnload;

#if !UNITY_WEBGL
            OnlineMapsThreadManager.AddThreadAction(() =>
            {
#endif
                for (int i = 0; i < countUnload; i++)
                {
                    string fn = unloadFiles[i];
                    if (File.Exists(fn)) File.Delete(fn);
                }
#if !UNITY_WEBGL
        });
#endif
#endif
        }
        public void DeleteOldItems(OnlineMapsCache cache)
        {
#if ALLOW_FILECACHE
            int countUnload = Mathf.RoundToInt(count * cache.fileCacheUnloadRate);
            if (countUnload <= 0)
            {
                throw new Exception("Can not unload a negative number of items. Check fileCacheUnloadRate.");
            }
            if (count < countUnload)
            {
                countUnload = count;
            }

            long[] unloadTimes   = new long[countUnload];
            int[]  unloadIndices = new int[countUnload];
            int    c             = 0;

            for (int i = 0; i < count; i++)
            {
                long t = items[i].time;
                if (c == 0)
                {
                    unloadIndices[0] = 0;
                    unloadTimes[0]   = t;
                    c++;
                }
                else
                {
                    int index  = c;
                    int index2 = index - 1;

                    while (index2 >= 0)
                    {
                        if (unloadTimes[index2] < t)
                        {
                            break;
                        }

                        index2--;
                        index--;
                    }

                    if (index < countUnload)
                    {
                        for (int j = countUnload - 1; j > index; j--)
                        {
                            unloadIndices[j] = unloadIndices[j - 1];
                            unloadTimes[j]   = unloadTimes[j - 1];
                        }
                        unloadIndices[index] = i;
                        unloadTimes[index]   = t;
                        if (c < countUnload)
                        {
                            c++;
                        }
                    }
                }
            }

            for (int i = 0; i < countUnload; i++)
            {
                int index = unloadIndices[i];
                _size -= items[index].size;
                string fullFilename = cache.GetFullTilePath(items[index].filename);
                if (File.Exists(fullFilename))
                {
                    File.Delete(fullFilename);
                }
                items[index] = null;
            }

            int offset = 0;
            for (int i = 0; i < count; i++)
            {
                if (items[i] == null)
                {
                    offset++;
                }
                else if (offset > 0)
                {
                    items[i - offset] = items[i];
                }
            }

            count -= countUnload;
#endif
        }
Exemplo n.º 18
0
 public abstract void DeleteOldItems(OnlineMapsCache cache);
Exemplo n.º 19
0
 private void OnEnable()
 {
     _instance = this;
 }
Exemplo n.º 20
0
 private void OnEnable()
 {
     cache = target as OnlineMapsCache;
     CacheSerializedFields();
 }