diff --git a/Assets/Plugins/Unicache/Scripts/FileCache.cs b/Assets/Plugins/Unicache/Scripts/FileCache.cs index 4055cef..7114065 100644 --- a/Assets/Plugins/Unicache/Scripts/FileCache.cs +++ b/Assets/Plugins/Unicache/Scripts/FileCache.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using Unicache.Plugin; using UniRx; namespace Unicache @@ -10,15 +11,19 @@ public class FileCache : IUnicache public ICacheHandler Handler { get; set; } public IUrlLocator UrlLocator { get; set; } public ICacheLocator CacheLocator { get; set; } + public ICacheEncoder Encoder { get; set; } + public ICacheDecoder Decoder { get; set; } private string RootDirectory; - public FileCache() : this(UnicacheConfig.Directory) + public FileCache() : this(UnicacheConfig.Directory, new VoidEncoderDecoder()) { } - public FileCache(string rootDirectory) + public FileCache(string rootDirectory, ICacheEncoderDecoder encoderDecoder) { this.RootDirectory = rootDirectory; + this.Encoder = encoderDecoder; + this.Decoder = encoderDecoder; } public IObservable Fetch(string key) @@ -88,7 +93,8 @@ public byte[] GetCache(string key) protected byte[] GetCacheByPath(string path) { - return IO.Read(this.RootDirectory + path); + var data = IO.Read(this.RootDirectory + path); + return this.Decoder.Decode(data); } public void SetCache(string key, byte[] data) @@ -98,8 +104,9 @@ public void SetCache(string key, byte[] data) protected void SetCacheByPath(string path, byte[] data) { + byte[] writeData = this.Encoder.Encode(data); IO.MakeParentDirectory(this.RootDirectory + path); - IO.Write(this.RootDirectory + path, data); + IO.Write(this.RootDirectory + path, writeData); } public bool HasCache(string key) diff --git a/Assets/Plugins/Unicache/Scripts/IUnicache.cs b/Assets/Plugins/Unicache/Scripts/IUnicache.cs index afe0602..03f79df 100644 --- a/Assets/Plugins/Unicache/Scripts/IUnicache.cs +++ b/Assets/Plugins/Unicache/Scripts/IUnicache.cs @@ -8,6 +8,8 @@ public interface IUnicache ICacheHandler Handler { set; } IUrlLocator UrlLocator { set; } ICacheLocator CacheLocator { set; } + ICacheEncoder Encoder { set; } + ICacheDecoder Decoder { set; } IObservable Fetch(string key); void Clear(); diff --git a/Assets/Plugins/Unicache/Scripts/MemoryCache.cs b/Assets/Plugins/Unicache/Scripts/MemoryCache.cs index 5e08395..f8c7782 100644 --- a/Assets/Plugins/Unicache/Scripts/MemoryCache.cs +++ b/Assets/Plugins/Unicache/Scripts/MemoryCache.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Unicache.Plugin; using UniRx; namespace Unicache @@ -8,13 +9,21 @@ public class MemoryCache : IUnicache public ICacheHandler Handler { get; set; } public IUrlLocator UrlLocator { get; set; } public ICacheLocator CacheLocator { get; set; } + public ICacheEncoder Encoder { get; set; } + public ICacheDecoder Decoder { get; set; } private IDictionary MemoryMap = new Dictionary(); - public MemoryCache() + public MemoryCache() : this(new VoidEncoderDecoder()) { } + public MemoryCache(ICacheEncoderDecoder encoderDecoder) + { + this.Encoder = encoderDecoder; + this.Decoder = encoderDecoder; + } + public IObservable Fetch(string key) { var url = this.UrlLocator.CreateUrl(key); @@ -70,7 +79,8 @@ public byte[] GetCache(string key) private byte[] GetCacheByPath(string path) { - return this.MemoryMap[path]; + var data = this.MemoryMap[path]; + return this.Decoder.Decode(data); } public void SetCache(string key, byte[] data) @@ -80,7 +90,8 @@ public void SetCache(string key, byte[] data) private void SetCacheByPath(string path, byte[] data) { - this.MemoryMap[path] = data; + var writeData = this.Encoder.Encode(data); + this.MemoryMap[path] = writeData; } public bool HasCache(string key) diff --git a/Assets/Plugins/Unicache/Scripts/UnicacheConfig.cs b/Assets/Plugins/Unicache/Scripts/UnicacheConfig.cs index ba3982e..f29e050 100644 --- a/Assets/Plugins/Unicache/Scripts/UnicacheConfig.cs +++ b/Assets/Plugins/Unicache/Scripts/UnicacheConfig.cs @@ -1,4 +1,5 @@ -using UnityEngine; +using Unicache.Plugin; +using UnityEngine; namespace Unicache {