UniVRM/UniGLTF/Core/Scripts/IO/IStorage.cs
ousttrue 1d108052de Merge commit '2a19c831f8440eed1279b9930ef33115c61d7d82' as 'UniGLTF'
Co-authored-by: Akihiko Odaki <nekomanma@pixiv.co.jp>
Co-authored-by: Emiliana <vtemiliana@gmail.com>
Co-authored-by: junichi_hirose <junichi_hirose@dwango.co.jp>
Co-authored-by: Masataka SUMI <santarh@gmail.com>
Co-authored-by: ousttrue <oustrrue@gmail.com>
Co-authored-by: ousttrue <ousttrue@gmail.com>
Co-authored-by: TORISOUP <tori.birdstrike@gmail.com>
Co-authored-by: Yuki Shimada <emadurandal@gmail.com>
Co-authored-by: yutopp <yutopp@gmail.com>
2018-12-28 20:16:54 +09:00

75 lines
1.6 KiB
C#

using System;
using System.IO;
namespace UniGLTF
{
public interface IStorage
{
ArraySegment<Byte> Get(string url);
/// <summary>
/// Get original filepath if exists
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
string GetPath(string url);
}
public class SimpleStorage : IStorage
{
ArraySegment<Byte> m_bytes;
public SimpleStorage():this(new ArraySegment<byte>())
{
}
public SimpleStorage(ArraySegment<Byte> bytes)
{
m_bytes = bytes;
}
public ArraySegment<byte> Get(string url)
{
return m_bytes;
}
public string GetPath(string url)
{
return null;
}
}
public class FileSystemStorage : IStorage
{
string m_root;
public FileSystemStorage(string root)
{
m_root = Path.GetFullPath(root);
}
public ArraySegment<byte> Get(string url)
{
var bytes =
(url.StartsWith("data:"))
? UriByteBuffer.ReadEmbeded(url)
: File.ReadAllBytes(Path.Combine(m_root, url))
;
return new ArraySegment<byte>(bytes);
}
public string GetPath(string url)
{
if (url.StartsWith("data:"))
{
return null;
}
else
{
return Path.Combine(m_root, url).Replace("\\", "/");
}
}
}
}