Skip to content

Commit

Permalink
refactor!: remove ResourceManager base class (#1210)
Browse files Browse the repository at this point in the history
* feat!: implement ResourceUtil

* add IResourceManager and remove ResourceManager base class

* test: enable ignored tests since we can init ResourceManager twice now
  • Loading branch information
homuler authored Jun 30, 2024
1 parent 994beee commit 741dc4a
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 219 deletions.
4 changes: 2 additions & 2 deletions Assets/MediaPipeUnity/Samples/Common/Scripts/AssetLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace Mediapipe.Unity.Sample
{
public static class AssetLoader
{
private static ResourceManager _ResourceManager;
private static IResourceManager _ResourceManager;

public static void Provide(ResourceManager manager) => _ResourceManager = manager;
public static void Provide(IResourceManager manager) => _ResourceManager = manager;

public static IEnumerator PrepareAssetAsync(string name, string uniqueKey, bool overwrite = false)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ internal static partial class SafeNativeMethods
{
[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern void mp__SetCustomGlobalResourceProvider__P(
[MarshalAs(UnmanagedType.FunctionPtr)] ResourceManager.NativeResourceProvider provider);
[MarshalAs(UnmanagedType.FunctionPtr)] ResourceUtil.NativeResourceProvider provider);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern void mp__SetCustomGlobalPathResolver__P(
[MarshalAs(UnmanagedType.FunctionPtr)] ResourceManager.PathResolver resolver);
[MarshalAs(UnmanagedType.FunctionPtr)] ResourceUtil.PathResolver resolver);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2021 homuler
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
using System.Collections;

namespace Mediapipe.Unity
{
public interface IResourceManager
{
/// <summary>
/// Saves <paramref name="name" /> as <paramref name="uniqueKey" /> asynchronously.
/// </summary>
/// <remarks>
/// To make it possible for MediaPipe to read the saved asset, the asset path must be registered
/// by calling <see cref="ResourceUtil.AddAssetPath" /> or <see cref="ResourceUtil.SetAssetPath"/> internally.
/// </remarks>
/// <param name="overwrite">
/// Specifies whether <paramref name="uniqueKey" /> will be overwritten if it already exists.
/// </param>
public IEnumerator PrepareAssetAsync(string name, string uniqueKey, bool overwrite = true);

public IEnumerator PrepareAssetAsync(string name, bool overwrite = true) => PrepareAssetAsync(name, name, overwrite);
}
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,26 @@
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

using System;
using System.Collections;
using System.IO;
using UnityEngine;

namespace Mediapipe.Unity
{
public class AssetBundleResourceManager : ResourceManager
public class AssetBundleResourceManager : IResourceManager
{
private static readonly string _TAG = nameof(AssetBundleResourceManager);

private static string _AssetBundlePath;
private static string _CachePathRoot;

public AssetBundleResourceManager(string assetBundleName, string cachePath = "Cache") : base(PathToResourceAsFile, GetResourceContents)
public AssetBundleResourceManager(string assetBundleName, string cachePath = "Cache")
{
// It's safe to update static members because at most one RsourceManager can be initialized.
ResourceUtil.EnableCustomResolver();
_AssetBundlePath = Path.Combine(Application.streamingAssetsPath, assetBundleName);
_CachePathRoot = Path.Combine(Application.persistentDataPath, cachePath);
}

public override bool IsPrepared(string name)
{
var path = GetCachePathFor(name);

return File.Exists(path);
}


private AssetBundleCreateRequest _assetBundleReq;
private AssetBundle assetBundle => _assetBundleReq?.assetBundle;

Expand Down Expand Up @@ -62,9 +53,17 @@ public IEnumerator LoadAssetBundleAsync()
}
}

public override IEnumerator PrepareAssetAsync(string name, string uniqueKey, bool overwrite = true)
IEnumerator IResourceManager.PrepareAssetAsync(string name, string uniqueKey, bool overwrite)
{
var destFilePath = GetCachePathFor(uniqueKey);
if (overwrite)
{
ResourceUtil.SetAssetPath(name, destFilePath);
}
else
{
ResourceUtil.AddAssetPath(name, destFilePath);
}

if (File.Exists(destFilePath) && !overwrite)
{
Expand Down Expand Up @@ -95,20 +94,6 @@ public override IEnumerator PrepareAssetAsync(string name, string uniqueKey, boo
Logger.LogVerbose(_TAG, $"{name} is saved to {destFilePath} (length={bytes.Length})");
}

protected static string PathToResourceAsFile(string assetPath)
{
var assetName = GetAssetNameFromPath(assetPath);
return GetCachePathFor(assetName);
}

protected static byte[] GetResourceContents(string path)
{
Logger.LogDebug($"{path} is requested");

var cachePath = PathToResourceAsFile(path);
return File.ReadAllBytes(cachePath);
}

private static string GetCachePathFor(string assetName)
{
return Path.Combine(_CachePathRoot, assetName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,34 @@

namespace Mediapipe.Unity
{
public class LocalResourceManager : ResourceManager
public class LocalResourceManager : IResourceManager
{
private static readonly string _TAG = nameof(LocalResourceManager);

private static string _RelativePath;
private static readonly string _AssetPathRoot = "Packages/com.github.homuler.mediapipe/PackageResources/MediaPipe";
private static string _CachePathRoot;

public LocalResourceManager(string path) : base(PathToResourceAsFile, GetResourceContents)
public LocalResourceManager(string path)
{
// It's safe to update static members because at most one RsourceManager can be initialized.
ResourceUtil.EnableCustomResolver();
_RelativePath = path;
_CachePathRoot = Path.Combine(Application.persistentDataPath, _RelativePath);
}

public LocalResourceManager() : this("") { }

public override bool IsPrepared(string assetName)
{
return File.Exists(GetCachePathFor(assetName));
}

public override IEnumerator PrepareAssetAsync(string name, string uniqueKey, bool overwrite = true)
IEnumerator IResourceManager.PrepareAssetAsync(string name, string uniqueKey, bool overwrite)
{
var destFilePath = GetCachePathFor(uniqueKey);
if (overwrite)
{
ResourceUtil.SetAssetPath(name, destFilePath);
}
else
{
ResourceUtil.AddAssetPath(name, destFilePath);
}

if (File.Exists(destFilePath) && !overwrite)
{
Expand All @@ -61,21 +64,6 @@ public override IEnumerator PrepareAssetAsync(string name, string uniqueKey, boo
Logger.LogVerbose(_TAG, $"{name} is saved to {destFilePath} (length={asset.bytes.Length})");
}

protected static string PathToResourceAsFile(string assetPath)
{
var assetName = GetAssetNameFromPath(assetPath);
return GetCachePathFor(assetName);
}

protected static byte[] GetResourceContents(string path)
{
// TODO: try AsyncReadManager
Logger.LogDebug($"{path} is requested");

var cachePath = PathToResourceAsFile(path);
return File.ReadAllBytes(cachePath);
}

private static string GetAssetPathFor(string assetName)
{
return Path.Combine(_AssetPathRoot, assetName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,35 @@

namespace Mediapipe.Unity
{
public class StreamingAssetsResourceManager : ResourceManager
public class StreamingAssetsResourceManager : IResourceManager
{
private static readonly string _TAG = nameof(StreamingAssetsResourceManager);

private static string _RelativePath;
private static string _AssetPathRoot;
private static string _CachePathRoot;

public StreamingAssetsResourceManager(string path) : base(PathToResourceAsFile, GetResourceContents)
public StreamingAssetsResourceManager(string path)
{
// It's safe to update static members because at most one RsourceManager can be initialized.
ResourceUtil.EnableCustomResolver();
_RelativePath = path;
_AssetPathRoot = Path.Combine(Application.streamingAssetsPath, _RelativePath);
_CachePathRoot = Path.Combine(Application.persistentDataPath, _RelativePath);
}

public StreamingAssetsResourceManager() : this("") { }

public override bool IsPrepared(string name)
{
var path = GetCachePathFor(name);

return File.Exists(path);
}

public override IEnumerator PrepareAssetAsync(string name, string uniqueKey, bool overwrite = true)
IEnumerator IResourceManager.PrepareAssetAsync(string name, string uniqueKey, bool overwrite)
{
var destFilePath = GetCachePathFor(uniqueKey);
if (overwrite)
{
ResourceUtil.SetAssetPath(name, destFilePath);
}
else
{
ResourceUtil.AddAssetPath(name, destFilePath);
}

if (File.Exists(destFilePath) && !overwrite)
{
Expand All @@ -62,21 +63,6 @@ public override IEnumerator PrepareAssetAsync(string name, string uniqueKey, boo
Logger.LogVerbose(_TAG, $"{sourceFilePath} is copied to {destFilePath}");
}

protected static string PathToResourceAsFile(string assetPath)
{
var assetName = GetAssetNameFromPath(assetPath);
return GetCachePathFor(assetName);
}

protected static byte[] GetResourceContents(string path)
{
// TODO: try AsyncReadManager
Logger.LogDebug($"{path} is requested");

var cachePath = PathToResourceAsFile(path);
return File.ReadAllBytes(cachePath);
}

private IEnumerator CreateCacheFile(string assetName)
{
var cacheFilePath = GetCachePathFor(assetName);
Expand Down

This file was deleted.

Loading

0 comments on commit 741dc4a

Please sign in to comment.