Wes xx mediapipe integration

This commit is contained in:
Jelle De Geest
2023-03-12 20:34:16 +00:00
parent 8349b5f149
commit b11eeb465c
975 changed files with 192230 additions and 0 deletions

View File

@@ -0,0 +1,117 @@
// 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;
using System.Collections;
using System.IO;
using UnityEngine;
namespace Mediapipe.Unity
{
public class AssetBundleResourceManager : ResourceManager
{
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)
{
// It's safe to update static members because at most one RsourceManager can be initialized.
_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;
public void ClearAllCacheFiles()
{
if (Directory.Exists(_CachePathRoot))
{
Directory.Delete(_CachePathRoot, true);
}
}
public IEnumerator LoadAssetBundleAsync()
{
if (assetBundle != null)
{
Logger.LogWarning(_TAG, "AssetBundle is already loaded");
yield break;
}
// No need to lock because this code can be run in main thread only.
_assetBundleReq = AssetBundle.LoadFromFileAsync(_AssetBundlePath);
yield return _assetBundleReq;
if (_assetBundleReq.assetBundle == null)
{
throw new IOException($"Failed to load {_AssetBundlePath}");
}
}
public override IEnumerator PrepareAssetAsync(string name, string uniqueKey, bool overwrite = true)
{
var destFilePath = GetCachePathFor(uniqueKey);
if (File.Exists(destFilePath) && !overwrite)
{
Logger.LogInfo(_TAG, $"{name} will not be copied to {destFilePath} because it already exists");
yield break;
}
if (assetBundle == null)
{
yield return LoadAssetBundleAsync();
}
var assetLoadReq = assetBundle.LoadAssetAsync<TextAsset>(name);
yield return assetLoadReq;
if (assetLoadReq.asset == null)
{
throw new IOException($"Failed to load {name} from {assetBundle.name}");
}
Logger.LogVerbose(_TAG, $"Writing {name} data to {destFilePath}...");
if (!Directory.Exists(_CachePathRoot))
{
var _ = Directory.CreateDirectory(_CachePathRoot);
}
var bytes = (assetLoadReq.asset as TextAsset).bytes;
File.WriteAllBytes(destFilePath, bytes);
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);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 39737784a5d929cc4ab94aa5f36635c2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,90 @@
// 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.
#if UNITY_EDITOR
using System.Collections;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace Mediapipe.Unity
{
public class LocalResourceManager : ResourceManager
{
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)
{
// It's safe to update static members because at most one RsourceManager can be initialized.
_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)
{
var destFilePath = GetCachePathFor(uniqueKey);
if (File.Exists(destFilePath) && !overwrite)
{
Logger.LogInfo(_TAG, $"{name} will not be copied to {destFilePath} because it already exists");
yield break;
}
var assetPath = GetAssetPathFor(name);
var asset = AssetDatabase.LoadAssetAtPath<TextAsset>(assetPath);
if (asset == null)
{
throw new FileNotFoundException($"{assetPath} is not found. Check if {name} is included in the package");
}
Logger.LogVerbose(_TAG, $"Writing {name} data to {destFilePath}...");
if (!Directory.Exists(_CachePathRoot))
{
var _ = Directory.CreateDirectory(_CachePathRoot);
}
File.WriteAllBytes(destFilePath, asset.bytes);
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);
}
private static string GetCachePathFor(string assetName)
{
return Path.Combine(_CachePathRoot, assetName);
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 987096dd33929458589fc4bfd0a199c4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,127 @@
// 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;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
namespace Mediapipe.Unity
{
public class StreamingAssetsResourceManager : ResourceManager
{
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)
{
// It's safe to update static members because at most one RsourceManager can be initialized.
_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)
{
var destFilePath = GetCachePathFor(uniqueKey);
if (File.Exists(destFilePath) && !overwrite)
{
Logger.LogInfo(_TAG, $"{name} will not be copied to {destFilePath} because it already exists");
yield break;
}
var sourceFilePath = GetCachePathFor(name);
if (!File.Exists(sourceFilePath))
{
yield return CreateCacheFile(name);
}
if (sourceFilePath == destFilePath)
{
yield break;
}
Logger.LogVerbose(_TAG, $"Copying {sourceFilePath} to {destFilePath}...");
File.Copy(sourceFilePath, destFilePath, overwrite);
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);
if (File.Exists(cacheFilePath))
{
yield break;
}
#if !UNITY_ANDROID && !UNITY_WEBGL
throw new FileNotFoundException($"{cacheFilePath} is not found");
#else
var assetPath = GetAssetPathFor(assetName);
using (var webRequest = UnityWebRequest.Get(assetPath))
{
yield return webRequest.SendWebRequest();
if (webRequest.result == UnityWebRequest.Result.Success)
{
if (!Directory.Exists(_CachePathRoot))
{
var _ = Directory.CreateDirectory(_CachePathRoot);
}
Logger.LogVerbose(_TAG, $"Writing {assetName} data to {cacheFilePath}...");
var bytes = webRequest.downloadHandler.data;
File.WriteAllBytes(cacheFilePath, bytes);
Logger.LogVerbose(_TAG, $"{assetName} is saved to {cacheFilePath} (length={bytes.Length})");
}
else
{
throw new InternalException($"Failed to load {assetName}: {webRequest.error}");
}
}
#endif
}
private static string GetAssetPathFor(string assetName)
{
return Path.Combine(_AssetPathRoot, assetName);
}
private static string GetCachePathFor(string assetName)
{
var assetPath = GetAssetPathFor(assetName);
return File.Exists(assetPath) ? assetPath : Path.Combine(_CachePathRoot, assetName);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8590959d1f3a5951792fb898918d64d0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: