Skip to content

Commit

Permalink
Merge pull request #25 from /issues/20
Browse files Browse the repository at this point in the history
Issues/20 Create data encoder/decoder
  • Loading branch information
mattak authored May 7, 2017
2 parents b6d4b00 + ccc16c0 commit d5b8096
Show file tree
Hide file tree
Showing 17 changed files with 263 additions and 8 deletions.
9 changes: 9 additions & 0 deletions Assets/Editor/Test/Plugin.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions Assets/Editor/Test/Plugin/XorEncoderDecoderTest.cs
Original file line number Diff line number Diff line change
@@ -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"));
}
}
}
12 changes: 12 additions & 0 deletions Assets/Editor/Test/Plugin/XorEncoderDecoderTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions Assets/Plugins/Unicache/Scripts/FileCache.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Unicache.Plugin;
using UniRx;

namespace Unicache
Expand All @@ -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<byte[]> Fetch(string key)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions Assets/Plugins/Unicache/Scripts/ICacheDecoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Unicache
{
public interface ICacheDecoder
{
byte[] Decode(byte[] data);
}
}
12 changes: 12 additions & 0 deletions Assets/Plugins/Unicache/Scripts/ICacheDecoder.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Assets/Plugins/Unicache/Scripts/ICacheEncoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Unicache
{
public interface ICacheEncoder
{
byte[] Encode(byte[] data);
}
}
12 changes: 12 additions & 0 deletions Assets/Plugins/Unicache/Scripts/ICacheEncoder.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Assets/Plugins/Unicache/Scripts/ICacheEncoderDecoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Unicache
{
public interface ICacheEncoderDecoder : ICacheEncoder, ICacheDecoder
{
}
}
12 changes: 12 additions & 0 deletions Assets/Plugins/Unicache/Scripts/ICacheEncoderDecoder.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Assets/Plugins/Unicache/Scripts/IUnicache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public interface IUnicache
ICacheHandler Handler { set; }
IUrlLocator UrlLocator { set; }
ICacheLocator CacheLocator { set; }
ICacheEncoder Encoder { set; }
ICacheDecoder Decoder { set; }

IObservable<byte[]> Fetch(string key);
void Clear();
Expand Down
17 changes: 14 additions & 3 deletions Assets/Plugins/Unicache/Scripts/MemoryCache.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Unicache.Plugin;
using UniRx;

namespace Unicache
Expand All @@ -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<string, byte[]> MemoryMap = new Dictionary<string, byte[]>();

public MemoryCache()
public MemoryCache() : this(new VoidEncoderDecoder())
{
}

public MemoryCache(ICacheEncoderDecoder encoderDecoder)
{
this.Encoder = encoderDecoder;
this.Decoder = encoderDecoder;
}

public IObservable<byte[]> Fetch(string key)
{
var url = this.UrlLocator.CreateUrl(key);
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions Assets/Plugins/Unicache/Scripts/Plugin/VoidEncoderDecoder.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
12 changes: 12 additions & 0 deletions Assets/Plugins/Unicache/Scripts/Plugin/VoidEncoderDecoder.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions Assets/Plugins/Unicache/Scripts/Plugin/XorEncoderDecoder.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
12 changes: 12 additions & 0 deletions Assets/Plugins/Unicache/Scripts/Plugin/XorEncoderDecoder.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Assets/Plugins/Unicache/Scripts/UnicacheConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEngine;
using Unicache.Plugin;
using UnityEngine;

namespace Unicache
{
Expand Down

0 comments on commit d5b8096

Please sign in to comment.