From 4bdad5c501276b17a4fe6cb4a58caa7701f54f56 Mon Sep 17 00:00:00 2001 From: mattak Date: Sun, 7 May 2017 19:45:55 +0900 Subject: [PATCH 1/3] issues/20 feat(ICacheEncoder/Decoder): create interface --- Assets/Plugins/Unicache/Scripts/ICacheDecoder.cs | 7 +++++++ .../Plugins/Unicache/Scripts/ICacheDecoder.cs.meta | 12 ++++++++++++ Assets/Plugins/Unicache/Scripts/ICacheEncoder.cs | 7 +++++++ .../Plugins/Unicache/Scripts/ICacheEncoder.cs.meta | 12 ++++++++++++ .../Plugins/Unicache/Scripts/ICacheEncoderDecoder.cs | 6 ++++++ .../Unicache/Scripts/ICacheEncoderDecoder.cs.meta | 12 ++++++++++++ 6 files changed, 56 insertions(+) create mode 100644 Assets/Plugins/Unicache/Scripts/ICacheDecoder.cs create mode 100644 Assets/Plugins/Unicache/Scripts/ICacheDecoder.cs.meta create mode 100644 Assets/Plugins/Unicache/Scripts/ICacheEncoder.cs create mode 100644 Assets/Plugins/Unicache/Scripts/ICacheEncoder.cs.meta create mode 100644 Assets/Plugins/Unicache/Scripts/ICacheEncoderDecoder.cs create mode 100644 Assets/Plugins/Unicache/Scripts/ICacheEncoderDecoder.cs.meta diff --git a/Assets/Plugins/Unicache/Scripts/ICacheDecoder.cs b/Assets/Plugins/Unicache/Scripts/ICacheDecoder.cs new file mode 100644 index 0000000..2d5d301 --- /dev/null +++ b/Assets/Plugins/Unicache/Scripts/ICacheDecoder.cs @@ -0,0 +1,7 @@ +namespace Unicache +{ + public interface ICacheDecoder + { + byte[] Decode(byte[] data); + } +} \ No newline at end of file diff --git a/Assets/Plugins/Unicache/Scripts/ICacheDecoder.cs.meta b/Assets/Plugins/Unicache/Scripts/ICacheDecoder.cs.meta new file mode 100644 index 0000000..00c6bd3 --- /dev/null +++ b/Assets/Plugins/Unicache/Scripts/ICacheDecoder.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 3e62951f5eb454d539b924a1a0a4045a +timeCreated: 1494153675 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/Unicache/Scripts/ICacheEncoder.cs b/Assets/Plugins/Unicache/Scripts/ICacheEncoder.cs new file mode 100644 index 0000000..3948bfc --- /dev/null +++ b/Assets/Plugins/Unicache/Scripts/ICacheEncoder.cs @@ -0,0 +1,7 @@ +namespace Unicache +{ + public interface ICacheEncoder + { + byte[] Encode(byte[] data); + } +} \ No newline at end of file diff --git a/Assets/Plugins/Unicache/Scripts/ICacheEncoder.cs.meta b/Assets/Plugins/Unicache/Scripts/ICacheEncoder.cs.meta new file mode 100644 index 0000000..01a8fd5 --- /dev/null +++ b/Assets/Plugins/Unicache/Scripts/ICacheEncoder.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 8b429faa5f0294340adaf1865812b25c +timeCreated: 1494150666 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/Unicache/Scripts/ICacheEncoderDecoder.cs b/Assets/Plugins/Unicache/Scripts/ICacheEncoderDecoder.cs new file mode 100644 index 0000000..d909586 --- /dev/null +++ b/Assets/Plugins/Unicache/Scripts/ICacheEncoderDecoder.cs @@ -0,0 +1,6 @@ +namespace Unicache +{ + public interface ICacheEncoderDecoder : ICacheEncoder, ICacheDecoder + { + } +} \ No newline at end of file diff --git a/Assets/Plugins/Unicache/Scripts/ICacheEncoderDecoder.cs.meta b/Assets/Plugins/Unicache/Scripts/ICacheEncoderDecoder.cs.meta new file mode 100644 index 0000000..d8c57cd --- /dev/null +++ b/Assets/Plugins/Unicache/Scripts/ICacheEncoderDecoder.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 768c003c516e24d6fb028753d9e6e0eb +timeCreated: 1494153675 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From 6f7772f94407f145ce73a910adf9c86b6fd4da40 Mon Sep 17 00:00:00 2001 From: mattak Date: Sun, 7 May 2017 19:47:10 +0900 Subject: [PATCH 2/3] issues/20 feat(Encoder/Decoder): define {Void,Xor}EncoderDecoder --- Assets/Editor/Test/Plugin.meta | 9 +++ .../Test/Plugin/XorEncoderDecoderTest.cs | 64 +++++++++++++++++++ .../Test/Plugin/XorEncoderDecoderTest.cs.meta | 12 ++++ .../Scripts/Plugin/VoidEncoderDecoder.cs | 15 +++++ .../Scripts/Plugin/VoidEncoderDecoder.cs.meta | 12 ++++ .../Scripts/Plugin/XorEncoderDecoder.cs | 54 ++++++++++++++++ .../Scripts/Plugin/XorEncoderDecoder.cs.meta | 12 ++++ 7 files changed, 178 insertions(+) create mode 100644 Assets/Editor/Test/Plugin.meta create mode 100644 Assets/Editor/Test/Plugin/XorEncoderDecoderTest.cs create mode 100644 Assets/Editor/Test/Plugin/XorEncoderDecoderTest.cs.meta create mode 100644 Assets/Plugins/Unicache/Scripts/Plugin/VoidEncoderDecoder.cs create mode 100644 Assets/Plugins/Unicache/Scripts/Plugin/VoidEncoderDecoder.cs.meta create mode 100644 Assets/Plugins/Unicache/Scripts/Plugin/XorEncoderDecoder.cs create mode 100644 Assets/Plugins/Unicache/Scripts/Plugin/XorEncoderDecoder.cs.meta diff --git a/Assets/Editor/Test/Plugin.meta b/Assets/Editor/Test/Plugin.meta new file mode 100644 index 0000000..702a27d --- /dev/null +++ b/Assets/Editor/Test/Plugin.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 60cb8373b252c4e0d8a25dac1a9b5a00 +folderAsset: yes +timeCreated: 1494152374 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Editor/Test/Plugin/XorEncoderDecoderTest.cs b/Assets/Editor/Test/Plugin/XorEncoderDecoderTest.cs new file mode 100644 index 0000000..aed557d --- /dev/null +++ b/Assets/Editor/Test/Plugin/XorEncoderDecoderTest.cs @@ -0,0 +1,64 @@ +using NUnit.Framework; +using Unicache.Plugin; + +namespace Unicache.Test.Plugin +{ + public class XorEncoderDecoderTest + { + private IUnicache cache; + + [SetUp] + public void Setup() + { + this.cache = new MemoryCache(); + this.cache.CacheLocator = new SimpleCacheLocator(); + this.cache.Clear(); + } + + [TearDown] + public void TearDown() + { + this.cache.Clear(); + } + + [Test] + public void EncodeTest() + { + var encoderDecoder = new XorEncoderDecoder(new byte[] {0x01}); + Assert.AreEqual(new byte[] { }, encoderDecoder.Encode(new byte[] { })); + Assert.AreEqual(new byte[] {0x01}, encoderDecoder.Encode(new byte[] {0x00})); + Assert.AreEqual(new byte[] {0x00}, encoderDecoder.Encode(new byte[] {0x01})); + Assert.AreEqual(new byte[] {0xFE, 0x00}, encoderDecoder.Encode(new byte[] {0xFF, 0x01})); + } + + [Test] + public void DecodeTest() + { + var encoderDecoder = new XorEncoderDecoder(new byte[] {0x01}); + Assert.AreEqual(new byte[] { }, encoderDecoder.Decode(new byte[] { })); + Assert.AreEqual(new byte[] {0x01}, encoderDecoder.Decode(new byte[] {0x00})); + Assert.AreEqual(new byte[] {0x00}, encoderDecoder.Decode(new byte[] {0x01})); + Assert.AreEqual(new byte[] {0xFE, 0x00}, encoderDecoder.Decode(new byte[] {0xFF, 0x01})); + } + + [Test] + public void EncodeDecodeTest() + { + var encoderDecoder = new XorEncoderDecoder(new byte[] {0x01}); + Assert.AreEqual(new byte[] { }, encoderDecoder.Decode(encoderDecoder.Encode(new byte[] { }))); + Assert.AreEqual(new byte[] {0x00}, encoderDecoder.Decode(encoderDecoder.Encode(new byte[] {0x00}))); + Assert.AreEqual(new byte[] {0xFF, 0x01}, + encoderDecoder.Decode(encoderDecoder.Encode(new byte[] {0xFF, 0x01}))); + } + + [Test] + public void CacheEncodeTest() + { + var encoderDecoder = new XorEncoderDecoder("abc"); + this.cache.Encoder = encoderDecoder; + this.cache.Decoder = encoderDecoder; + this.cache.SetCache("foo", System.Text.UTF8Encoding.UTF8.GetBytes("foobar")); + Assert.AreEqual(System.Text.UTF8Encoding.UTF8.GetBytes("foobar"), this.cache.GetCache("foo")); + } + } +} \ No newline at end of file diff --git a/Assets/Editor/Test/Plugin/XorEncoderDecoderTest.cs.meta b/Assets/Editor/Test/Plugin/XorEncoderDecoderTest.cs.meta new file mode 100644 index 0000000..0de5e50 --- /dev/null +++ b/Assets/Editor/Test/Plugin/XorEncoderDecoderTest.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: c38bf60c442a54b67bc8e3de9de8c7da +timeCreated: 1494152374 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/Unicache/Scripts/Plugin/VoidEncoderDecoder.cs b/Assets/Plugins/Unicache/Scripts/Plugin/VoidEncoderDecoder.cs new file mode 100644 index 0000000..b6c8c5a --- /dev/null +++ b/Assets/Plugins/Unicache/Scripts/Plugin/VoidEncoderDecoder.cs @@ -0,0 +1,15 @@ +namespace Unicache.Plugin +{ + public class VoidEncoderDecoder : ICacheEncoderDecoder + { + public byte[] Decode(byte[] data) + { + return data; + } + + public byte[] Encode(byte[] data) + { + return data; + } + } +} \ No newline at end of file diff --git a/Assets/Plugins/Unicache/Scripts/Plugin/VoidEncoderDecoder.cs.meta b/Assets/Plugins/Unicache/Scripts/Plugin/VoidEncoderDecoder.cs.meta new file mode 100644 index 0000000..7de3555 --- /dev/null +++ b/Assets/Plugins/Unicache/Scripts/Plugin/VoidEncoderDecoder.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 573021361094f42ea89b102e59e99474 +timeCreated: 1494150666 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/Unicache/Scripts/Plugin/XorEncoderDecoder.cs b/Assets/Plugins/Unicache/Scripts/Plugin/XorEncoderDecoder.cs new file mode 100644 index 0000000..ef7e2b5 --- /dev/null +++ b/Assets/Plugins/Unicache/Scripts/Plugin/XorEncoderDecoder.cs @@ -0,0 +1,54 @@ +using System; + +namespace Unicache.Plugin +{ + public class XorEncoderDecoder : ICacheEncoderDecoder + { + private byte[] Key; + + public XorEncoderDecoder(string key) : this(System.Text.UTF8Encoding.UTF8.GetBytes(key)) + { + } + + public XorEncoderDecoder(byte[] key) + { + if (key == null || key.Length < 1) + { + throw new ArgumentException("key length must be greater than 0"); + } + + this.Key = key; + } + + private byte[] CreateKey(byte[] data) + { + byte[] dataKey = new byte[data.Length]; + for (int i = 0; i < data.Length; i++) + { + dataKey[i] = this.Key[i % this.Key.Length]; + } + return dataKey; + } + + private byte[] Invert(byte[] data) + { + byte[] dataKey = this.CreateKey(data); + byte[] result = new byte[data.Length]; + for (int i = 0; i < data.Length; i++) + { + result[i] = (byte) (data[i] ^ dataKey[i]); + } + return result; + } + + public byte[] Encode(byte[] data) + { + return Invert(data); + } + + public byte[] Decode(byte[] data) + { + return Invert(data); + } + } +} \ No newline at end of file diff --git a/Assets/Plugins/Unicache/Scripts/Plugin/XorEncoderDecoder.cs.meta b/Assets/Plugins/Unicache/Scripts/Plugin/XorEncoderDecoder.cs.meta new file mode 100644 index 0000000..0be26ca --- /dev/null +++ b/Assets/Plugins/Unicache/Scripts/Plugin/XorEncoderDecoder.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: a6f7580ee8d6c469e8fc98509a2a186a +timeCreated: 1494152374 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From ccc16c081e18a6b6f755ffc81fcc8db31fde68e9 Mon Sep 17 00:00:00 2001 From: mattak Date: Sun, 7 May 2017 19:48:14 +0900 Subject: [PATCH 3/3] issues/20 chore(IUnicache): handle cache encoder, decoder --- Assets/Plugins/Unicache/Scripts/FileCache.cs | 15 +++++++++++---- Assets/Plugins/Unicache/Scripts/IUnicache.cs | 2 ++ Assets/Plugins/Unicache/Scripts/MemoryCache.cs | 17 ++++++++++++++--- .../Plugins/Unicache/Scripts/UnicacheConfig.cs | 3 ++- 4 files changed, 29 insertions(+), 8 deletions(-) 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 {