-
-
Notifications
You must be signed in to change notification settings - Fork 459
/
ResourceManager.cs
121 lines (110 loc) · 3.46 KB
/
ResourceManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// 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;
namespace Mediapipe
{
/// <summary>
/// Class to manage assets that MediaPipe accesses.
/// </summary>
/// <remarks>
/// There must not be more than one instance at the same time.
/// </remarks>
public abstract class ResourceManager
{
public delegate string PathResolver(string path);
internal delegate bool NativeResourceProvider(string path, IntPtr dest);
public delegate byte[] ResourceProvider(string path);
private static readonly object _InitLock = new object();
private static ResourceManager _Instance;
private readonly PathResolver _pathResolver;
private readonly ResourceProvider _resourceProvider;
public ResourceManager(PathResolver pathResolver, ResourceProvider resourceProvider)
{
lock (_InitLock)
{
if (_Instance != null)
{
throw new InvalidOperationException("ResourceManager can be initialized only once");
}
_pathResolver = pathResolver;
_resourceProvider = resourceProvider;
SafeNativeMethods.mp__SetCustomGlobalPathResolver__P(PathToResourceAsFile);
SafeNativeMethods.mp__SetCustomGlobalResourceProvider__P(GetResourceContents);
_Instance = this;
}
}
/// <param name="name">Asset name</param>
/// <returns>
/// Returns true if <paramref name="name" /> is already prepared (saved locally on the device).
/// </returns>
public abstract bool IsPrepared(string name);
/// <summary>
/// Saves <paramref name="name" /> as <paramref name="uniqueKey" /> asynchronously.
/// </summary>
/// <param name="overwrite">
/// Specifies whether <paramref name="uniqueKey" /> will be overwritten if it already exists.
/// </param>
public abstract IEnumerator PrepareAssetAsync(string name, string uniqueKey, bool overwrite = true);
public IEnumerator PrepareAssetAsync(string name, bool overwrite = true)
{
return PrepareAssetAsync(name, name, overwrite);
}
[AOT.MonoPInvokeCallback(typeof(PathResolver))]
private static string PathToResourceAsFile(string assetPath)
{
try
{
return _Instance._pathResolver(assetPath);
}
catch (Exception e)
{
UnityEngine.Debug.LogException(e);
return "";
}
}
[AOT.MonoPInvokeCallback(typeof(ResourceProvider))]
private static bool GetResourceContents(string path, IntPtr dst)
{
try
{
var asset = _Instance._resourceProvider(path);
using (var srcStr = new StdString(asset))
{
srcStr.Swap(new StdString(dst, false));
}
return true;
}
catch (Exception e)
{
UnityEngine.Debug.LogException(e);
return false;
}
}
protected static string GetAssetNameFromPath(string assetPath)
{
var assetName = Path.GetFileNameWithoutExtension(assetPath);
var extension = Path.GetExtension(assetPath);
switch (extension)
{
case ".binarypb":
case ".tflite":
{
return $"{assetName}.bytes";
}
case ".pbtxt":
{
return $"{assetName}.txt";
}
default:
{
return $"{assetName}{extension}";
}
}
}
}
}