Skip to content

Commit

Permalink
fix(example): wait for the assets to be loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Oct 11, 2020
1 parent 67f04a7 commit 4cd0d2e
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 89 deletions.
23 changes: 23 additions & 0 deletions Assets/MediaPipe/Examples/Scripts/AsyncOperationAwaiter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Runtime.CompilerServices;
using UnityEngine;

public class AsyncOperationAwaiter<T> : INotifyCompletion where T : AsyncOperation {
T _asyncOperation;

public AsyncOperationAwaiter(T asyncOperation) {
_asyncOperation = asyncOperation;
}

public bool IsCompleted {
get { return _asyncOperation.isDone; }
}

public T GetResult() {
return _asyncOperation;
}

public void OnCompleted(Action continuation) {
_asyncOperation.completed += _ => { continuation(); };
}
}

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

7 changes: 7 additions & 0 deletions Assets/MediaPipe/Examples/Scripts/AsyncOperationExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using UnityEngine;

public static class AsyncOperationExtension {
public static AsyncOperationAwaiter<T> GetAwaiter<T>(this T operation) where T : AsyncOperation {
return new AsyncOperationAwaiter<T>(operation);
}
}

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

77 changes: 0 additions & 77 deletions Assets/MediaPipe/Examples/Scripts/DemoAssetManager.cs

This file was deleted.

8 changes: 8 additions & 0 deletions Assets/MediaPipe/Examples/Scripts/ResourceManager.meta

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
@@ -0,0 +1,84 @@
using Mediapipe;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;

public sealed class AssetBundleManager : ResourceManager {
private static readonly Lazy<AssetBundleManager> lazy = new Lazy<AssetBundleManager>(() => new AssetBundleManager());
public static AssetBundleManager Instance { get { return lazy.Value; } }
private readonly static string CacheRootPath = Path.Combine(Application.persistentDataPath, "Cache");
private readonly static string ModelCacheRootPath = Path.Combine(CacheRootPath, "MediaPipe", "Models");

private AssetBundleManager() : base() {
if (!Directory.Exists(ModelCacheRootPath)) {
Directory.CreateDirectory(ModelCacheRootPath);
}
}

public async Task LoadAllAssetsAsync() {
Debug.Log("Starting to load assets");

var bundleLoadReq = await AssetBundle.LoadFromFileAsync(Path.Combine(Application.streamingAssetsPath, "AssetBundles", "mediapipe", "models"));

if (bundleLoadReq.assetBundle == null) {
Debug.LogError("Failed to load the AssetBundle");
return;
}

var assetLoadReq = await bundleLoadReq.assetBundle.LoadAllAssetsAsync<TextAsset>();
if (assetLoadReq.allAssets == null) {
Debug.LogError("Failed to load assets");
return;
}

var loadTasks = assetLoadReq.allAssets.Select((asset) => WriteCacheFileAsync((TextAsset)asset));
await Task.WhenAll(loadTasks);
Debug.Log("Loaded all assets");
}

protected override string CacheFileFromAsset(string assetPath) {
var assetName = Path.GetFileNameWithoutExtension(assetPath);
var cachePath = CachePathFor(assetName);

if (File.Exists(cachePath)) {
return cachePath;
}

return null;
}

protected override bool ReadFile(string path, IntPtr dst) {
var cachePath = CacheFileFromAsset(path);

if (!File.Exists(cachePath)) {
return false;
}

var asset = File.ReadAllBytes(cachePath);
ResourceUtil.CopyBytes(dst, asset);
return true;
}

private string CachePathFor(string assetName) {
return Path.Combine(ModelCacheRootPath, assetName);
}

private async Task WriteCacheFileAsync(TextAsset asset) {
var path = CachePathFor(asset.name);
var bytes = asset.bytes;
Debug.Log($"Saving {asset.name} to {path} (length={bytes.Length})");

FileStream sourceStream = null;

try {
sourceStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true);
await sourceStream.WriteAsync(bytes, 0, bytes.Length);
} finally {
if (sourceStream != null) {
sourceStream.Close();
}
}
}
}

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

28 changes: 25 additions & 3 deletions Assets/MediaPipe/Examples/Scripts/SceneDirector.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.IO;
using System.Collections;

Expand All @@ -20,6 +21,9 @@ public class SceneDirector : MonoBehaviour {

const int MAX_WAIT_FRAME = 50;

bool IsAssetLoaded = false;
bool IsAssetLoadFailed = false;

void OnEnable() {
var nameForGlog = Path.Combine(Application.dataPath, "MediaPipePlugin");
var logDir = Path.Combine(Application.persistentDataPath, "Logs", "MediaPipe");
Expand All @@ -31,7 +35,7 @@ public class SceneDirector : MonoBehaviour {
UnsafeNativeMethods.InitGoogleLogging(nameForGlog, logDir);
}

void Start() {
async void Start() {
webCamScreen = GameObject.Find("WebCamScreen");

if (useGPU) {
Expand All @@ -41,8 +45,15 @@ public class SceneDirector : MonoBehaviour {
gpuHelper.InitializeForTest(gpuResources);
}

DemoAssetManager.Instance.LoadAllAssetsAsync();
ResourceUtil.InitializeResourceManager(DemoAssetManager.Instance);
ResourceUtil.InitializeResourceManager(AssetBundleManager.Instance);

try {
await AssetBundleManager.Instance.LoadAllAssetsAsync();
IsAssetLoaded = true;
} catch (Exception e) {
Debug.LogError(e);
IsAssetLoadFailed = true;
}
}

void OnDisable() {
Expand Down Expand Up @@ -111,6 +122,17 @@ public class SceneDirector : MonoBehaviour {
yield break;
}

if (!IsAssetLoaded && !IsAssetLoadFailed) {
Debug.Log("Waiting for assets to be loaded...");
}

yield return new WaitUntil(() => IsAssetLoaded || IsAssetLoadFailed);

if (IsAssetLoadFailed) {
Debug.LogError("Failed to load assets. Stopping...");
yield break;
}

graphContainer = Instantiate(graphPrefab);
var graph = graphContainer.GetComponent<IDemoGraph<PixelData>>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Runtime.InteropServices;

namespace Mediapipe {
public abstract class AssetManager {
public abstract class ResourceManager {
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate string CacheFilePathResolver(string path);
private readonly CacheFilePathResolver cacheFilePathResolver;
Expand All @@ -11,7 +11,7 @@ public abstract class AssetManager {
private delegate bool ReadFileHandler(string path, IntPtr dst);
private readonly ReadFileHandler readFileHandler;

protected AssetManager() {
protected ResourceManager() {
cacheFilePathResolver = CacheFileFromAsset;
readFileHandler = ReadFile;
}
Expand All @@ -24,8 +24,8 @@ public abstract class AssetManager {
return Marshal.GetFunctionPointerForDelegate(readFileHandler);
}

public abstract string CacheFileFromAsset(string assetPath);
protected abstract string CacheFileFromAsset(string assetPath);

public abstract bool ReadFile(string path, IntPtr dst);
protected abstract bool ReadFile(string path, IntPtr dst);
}
}
11 changes: 11 additions & 0 deletions Assets/MediaPipe/SDK/Scripts/Util/ResourceManager.cs.meta

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

4 changes: 2 additions & 2 deletions Assets/MediaPipe/SDK/Scripts/Util/ResourceUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Mediapipe {
public class ResourceUtil {
public static void InitializeResourceManager(AssetManager assetManager) {
UnsafeNativeMethods.MpResourceManagerInitialize(assetManager.GetCacheFilePathResolverPtr(), assetManager.GetReadFileHandlerPtr());
public static void InitializeResourceManager(ResourceManager resourceManager) {
UnsafeNativeMethods.MpResourceManagerInitialize(resourceManager.GetCacheFilePathResolverPtr(), resourceManager.GetReadFileHandlerPtr());
}

public static void CopyBytes(IntPtr dst, byte[] src) {
Expand Down
1 change: 0 additions & 1 deletion ProjectSettings/GraphicsSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ GraphicsSettings:
- {fileID: 16000, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 16001, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 17000, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 16003, guid: 0000000000000000f000000000000000, type: 0}
m_PreloadedShaders: []
m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000,
type: 0}
Expand Down

0 comments on commit 4cd0d2e

Please sign in to comment.