Skip to content

Commit

Permalink
Version 0.6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
rds1983 committed Aug 25, 2023
1 parent 5e548c8 commit beb2c4d
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 126 deletions.
2 changes: 1 addition & 1 deletion src/AssetManagementBase.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/rds1983/AssetManagementBase</PackageProjectUrl>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.6.0</Version>
<Version>0.6.1</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand Down
131 changes: 40 additions & 91 deletions src/AssetManager.cs
Original file line number Diff line number Diff line change
@@ -1,87 +1,54 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace AssetManagementBase
{
public struct AssetLoaderContext
public delegate Stream AssetOpener(string assetName);
public delegate T AssetLoader<T>(AssetManager manager, string assetName, object settings);

public class AssetManager
{
public AssetManager Manager;
public string Name;
public Func<Stream> DataStreamOpener;
public object Settings;
internal const char SeparatorSymbol = '/';
internal const string SeparatorString = "/";

/// <summary>
/// Reads asset stream as string
/// </summary>
/// <returns></returns>
public string ReadDataAsString()
{
string result;
private readonly AssetManagerCore _core;
private readonly string _currentFolder = SeparatorString;

using (var stream = DataStreamOpener())
using (var textReader = new StreamReader(stream))
{
result = textReader.ReadToEnd();
}
public Dictionary<string, object> Cache => _core.Cache;

return result;
public AssetManager(AssetOpener assetOpener)
{
_core = new AssetManagerCore(assetOpener);
}

/// <summary>
/// Reads asset stream as byte array
/// </summary>
/// <returns></returns>
public byte[] ReadAssetAsByteArray()
internal AssetManager(AssetManagerCore core, string currentFolder)
{
using (var stream = DataStreamOpener())
using (var ms = new MemoryStream())
{
stream.CopyTo(ms);
return ms.ToArray();
}
_core = core;
_currentFolder = currentFolder;
}
}

public delegate Stream AssetOpener(string assetName);
public delegate T AssetLoader<T>(AssetLoaderContext context);
public Stream OpenAssetStream(string assetName) => _core.OpenAssetStream(BuildFullPath(assetName));

public class AssetManager
{
public const char SeparatorSymbol = '/';
public const string SeparatorString = "/";
private readonly AssetOpener _assetOpener;
private string _currentFolder = SeparatorString;
/// <summary>
/// Reads asset stream as string
/// </summary>
/// <returns></returns>
public string ReadAssetAsString(string assetName) => _core.ReadAssetAsString(BuildFullPath(assetName));

public Dictionary<string, object> Cache { get; } = new Dictionary<string, object>();
/// <summary>
/// Reads asset stream as byte array
/// </summary>
/// <returns></returns>
public byte[] ReadAssetAsByteArray(string assetName) => _core.ReadAssetAsByteArray(BuildFullPath(assetName));

public AssetManager(AssetOpener assetOpener)
{
_assetOpener = assetOpener ?? throw new ArgumentNullException(nameof(assetOpener));
}

public bool HasAsset(string assetName, object settings = null)
{
assetName = BuildFullPath(assetName);
var cacheKey = BuildCacheKey(assetName, settings);

return Cache.ContainsKey(cacheKey);
}

private Func<Stream> CreateStreamOpener(string assetName)
{
return () =>
{
var stream = _assetOpener(assetName);
if (stream == null)
{
throw new Exception(string.Format("Can't open asset {0}", assetName));
}
return stream;
};
return _core.Cache.ContainsKey(cacheKey);
}

public T UseLoader<T>(AssetLoader<T> loader, string assetName, object settings = null, bool storeInCache = true)
Expand All @@ -90,47 +57,29 @@ public T UseLoader<T>(AssetLoader<T> loader, string assetName, object settings =
var cacheKey = BuildCacheKey(assetName, settings);

object cached;
if (Cache.TryGetValue(cacheKey, out cached))
if (_core.Cache.TryGetValue(cacheKey, out cached))
{
// Found in cache
return (T)cached;
}

var oldFolder = _currentFolder;

T result;

try
var assetManager = this;
var separatorIndex = assetName.LastIndexOf(SeparatorSymbol);
if (separatorIndex != -1)
{
var separatorIndex = assetName.LastIndexOf(SeparatorSymbol);
if (separatorIndex != -1)
var assetFolder = assetName.Substring(0, separatorIndex);
if (!string.IsNullOrEmpty(assetFolder))
{
_currentFolder = assetName.Substring(0, separatorIndex);
if (string.IsNullOrEmpty(_currentFolder))
{
_currentFolder = SeparatorString;
}
assetManager = new AssetManager(_core, assetFolder);
}

var context = new AssetLoaderContext
{
Manager = this,
Name = assetName,
DataStreamOpener = CreateStreamOpener(assetName),
Settings = settings,
};

result = loader(context);
}
finally
{
_currentFolder = oldFolder;
}

var result = loader(assetManager, assetName, settings);

if (storeInCache)
{
// Store in cache
Cache[cacheKey] = result;
_core.Cache[cacheKey] = result;
}

return result;
Expand Down Expand Up @@ -174,12 +123,12 @@ private static string CombinePath(string _base, string url)
return url;
}

if (_base[_base.Length - 1] == AssetManager.SeparatorSymbol)
if (_base[_base.Length - 1] == SeparatorSymbol)
{
return _base + url;
}

return _base + AssetManager.SeparatorSymbol + url;
return _base + SeparatorSymbol + url;
}

public static AssetManager CreateFileAssetManager(string baseFolder) => new AssetManager(DefaultOpeners.CreateFileOpener(baseFolder));
Expand Down
51 changes: 51 additions & 0 deletions src/AssetManagerCore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Collections.Generic;
using System.IO;
using System;

namespace AssetManagementBase
{
internal class AssetManagerCore
{
private readonly AssetOpener _assetOpener;

public Dictionary<string, object> Cache { get; } = new Dictionary<string, object>();

public AssetManagerCore(AssetOpener assetOpener)
{
_assetOpener = assetOpener ?? throw new ArgumentNullException(nameof(assetOpener));
}

public Stream OpenAssetStream(string assetName) => _assetOpener(assetName);

/// <summary>
/// Reads asset stream as string
/// </summary>
/// <returns></returns>
public string ReadAssetAsString(string assetName)
{
string result;

using (var stream = OpenAssetStream(assetName))
using (var textReader = new StreamReader(stream))
{
result = textReader.ReadToEnd();
}

return result;
}

/// <summary>
/// Reads asset stream as byte array
/// </summary>
/// <returns></returns>
public byte[] ReadAssetAsByteArray(string assetName)
{
using (var stream = OpenAssetStream(assetName))
using (var ms = new MemoryStream())
{
stream.CopyTo(ms);
return ms.ToArray();
}
}
}
}
4 changes: 2 additions & 2 deletions src/DefaultLoaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
{
public static class DefaultLoaders
{
private static AssetLoader<string> StringLoader = context => context.ReadDataAsString();
private static AssetLoader<byte[]> ByteArrayLoader = context => context.ReadAssetAsByteArray();
private static AssetLoader<string> StringLoader = (manager, assetName, settings) => manager.ReadAssetAsString(assetName);
private static AssetLoader<byte[]> ByteArrayLoader = (manager, assetName, settings) => manager.ReadAssetAsByteArray(assetName);

public static string LoadString(this AssetManager assetManager, string assetName, bool storeInCache = true) => assetManager.UseLoader(StringLoader, assetName, storeInCache: storeInCache);
public static byte[] LoadByteArray(this AssetManager assetManager, string assetName, bool storeInCache = true) => assetManager.UseLoader(ByteArrayLoader, assetName, storeInCache: storeInCache);
Expand Down
30 changes: 0 additions & 30 deletions src/Utility/PathUtils.cs

This file was deleted.

4 changes: 4 additions & 0 deletions tests/AssetManagementBase.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

<ItemGroup>
<EmbeddedResource Include="Resources\**\*.*" />
</ItemGroup>

<ItemGroup>
<None Remove="Resources\sub\test.txt" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions tests/AssetManagerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace AssetManagementBase.Tests
{
public static class AssetManagerExtensions
{
private static AssetLoader<UserProfile> _userProfileLoader = (context) =>
private static AssetLoader<UserProfile> _userProfileLoader = (manager, assetName, settings) =>
{
var data = context.ReadDataAsString();
var data = manager.ReadAssetAsString(assetName);
var xDoc = XDocument.Parse(data);
Expand Down
6 changes: 6 additions & 0 deletions tests/ResourceAssetResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public void TestWithEndDot()
TestResourceAccess("Resources.", true, "test.txt");
}

[Test]
public void TestSubFolder()
{
TestResourceAccess("Resources.", true, "sub/test.txt");
}

[Test]
public void TestWithoutPrependAssemblyName()
{
Expand Down
1 change: 1 addition & 0 deletions tests/Resources/sub/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test

0 comments on commit beb2c4d

Please sign in to comment.