ai.muna.muna 0.0.45 → 0.0.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Editor/Build/AndroidBuildHandler.cs +3 -3
- package/Editor/Build/iOSBuildHandler.cs +4 -4
- package/Editor/Build/macOSBuildHandler.cs +6 -6
- package/Editor/MunaMenu.cs +9 -11
- package/Plugins/Android/Muna.aar +0 -0
- package/Plugins/macOS/Function.dylib.meta +26 -25
- package/README.md +1 -1
- package/Runtime/Beta/OpenAI/SpeechService.cs +6 -0
- package/Runtime/Beta/OpenAI/Types.cs +0 -1
- package/Runtime/Beta/Remote/RemotePredictionService.cs +43 -42
- package/Runtime/Beta/{Remote → Types}/Value.cs +2 -2
- package/{Unity → Runtime/Beta}/Types.meta +1 -1
- package/Runtime/Muna.cs +1 -1
- package/Unity/API/PredictionCacheClient.cs +44 -51
- package/Unity/Converters/Color.cs +46 -0
- package/Unity/Converters/Color.cs.meta +2 -0
- package/Unity/Converters/Rect.cs +230 -0
- package/Unity/Converters/Rect.cs.meta +2 -0
- package/Unity/Converters/Vector2.cs +44 -0
- package/Unity/Converters/Vector2.cs.meta +2 -0
- package/Unity/Converters/Vector3.cs +45 -0
- package/Unity/Converters/Vector3.cs.meta +2 -0
- package/Unity/Converters/Vector4.cs +46 -0
- package/Unity/Converters/Vector4.cs.meta +2 -0
- package/Unity/Converters.meta +8 -0
- package/Unity/Internal/MunaSettings.cs +1 -1
- package/Unity/Internal/PredictionCache.cs +160 -0
- package/Unity/Internal/PredictionCache.cs.meta +2 -0
- package/Unity/MunaUnity.cs +68 -20
- package/package.json +1 -1
- package/Unity/Types/CachedPrediction.cs +0 -36
- package/Unity/Types/CachedPrediction.cs.meta +0 -11
- /package/Runtime/Beta/{Remote → Types}/Value.cs.meta +0 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Muna
|
|
3
|
+
* Copyright © 2025 NatML Inc. All rights reserved.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
#nullable enable
|
|
7
|
+
|
|
8
|
+
namespace Muna.Converters {
|
|
9
|
+
|
|
10
|
+
using System;
|
|
11
|
+
using System.Runtime.Serialization;
|
|
12
|
+
using UnityEngine;
|
|
13
|
+
using Newtonsoft.Json;
|
|
14
|
+
using Newtonsoft.Json.Converters;
|
|
15
|
+
using Newtonsoft.Json.Linq;
|
|
16
|
+
|
|
17
|
+
/// <summary>
|
|
18
|
+
/// Box format.
|
|
19
|
+
/// </summary>
|
|
20
|
+
[JsonConverter(typeof(StringEnumConverter))]
|
|
21
|
+
public enum BoxFormat : int {
|
|
22
|
+
/// <summary>
|
|
23
|
+
/// Boxes are represented via corners: x1, y1 being top left and x2, y2 being bottom right.
|
|
24
|
+
/// </summary>
|
|
25
|
+
[EnumMember(Value = @"xyxy")]
|
|
26
|
+
XYXY = 1,
|
|
27
|
+
/// <summary>
|
|
28
|
+
/// Boxes are represented via corner, width and height: x1, y2 being top left.
|
|
29
|
+
/// </summary>
|
|
30
|
+
[EnumMember(Value = @"xywh")]
|
|
31
|
+
XYWH = 2,
|
|
32
|
+
/// <summary>
|
|
33
|
+
/// Boxes are represented via center, width, and height.
|
|
34
|
+
/// </summary>
|
|
35
|
+
[EnumMember(Value = @"cxcywh")]
|
|
36
|
+
CxCyWH = 3,
|
|
37
|
+
/// <summary>
|
|
38
|
+
/// Boxes are represented via corners:
|
|
39
|
+
/// - x1, y1 being top left
|
|
40
|
+
/// - x2, y2 top right
|
|
41
|
+
/// - x3, y3 bottom right
|
|
42
|
+
/// - x4, y4 bottom left
|
|
43
|
+
/// </summary>
|
|
44
|
+
[EnumMember(Value = @"xyxyxyxy")]
|
|
45
|
+
XYXYXYXY = 4,
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/// <summary>
|
|
49
|
+
/// Convert an object field to a `Rect`.
|
|
50
|
+
/// </summary>
|
|
51
|
+
public sealed class ObjectToRectConverter : JsonConverter<Rect> {
|
|
52
|
+
|
|
53
|
+
private readonly BoxFormat format;
|
|
54
|
+
private readonly string[] fieldNames;
|
|
55
|
+
|
|
56
|
+
/// <summary>
|
|
57
|
+
/// Create a converter with the provided box format.
|
|
58
|
+
/// </summary>
|
|
59
|
+
/// <param name="format">Box format to parse.</param>
|
|
60
|
+
public ObjectToRectConverter(BoxFormat format): this(
|
|
61
|
+
format,
|
|
62
|
+
GetReferenceFieldNames(format)
|
|
63
|
+
) { }
|
|
64
|
+
|
|
65
|
+
/// <summary>
|
|
66
|
+
/// Create a converter with the provided box format and corresponding field names.
|
|
67
|
+
/// </summary>
|
|
68
|
+
/// <param name="format">Box format to parse.</param>
|
|
69
|
+
/// <param name="fieldNames">Field names that correspond to the chosen format.</param>
|
|
70
|
+
public ObjectToRectConverter(BoxFormat format, string[] fieldNames) {
|
|
71
|
+
this.format = format;
|
|
72
|
+
this.fieldNames = fieldNames;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public override void WriteJson(
|
|
76
|
+
JsonWriter writer,
|
|
77
|
+
Rect value,
|
|
78
|
+
JsonSerializer serializer
|
|
79
|
+
) {
|
|
80
|
+
var values = GetRectValues(value, format);
|
|
81
|
+
var obj = new JObject();
|
|
82
|
+
for (var i = 0; i < fieldNames.Length; ++i)
|
|
83
|
+
obj[fieldNames[i]] = values[i];
|
|
84
|
+
obj.WriteTo(writer);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
public override Rect ReadJson(
|
|
88
|
+
JsonReader reader,
|
|
89
|
+
Type type,
|
|
90
|
+
Rect existing,
|
|
91
|
+
bool hasExisting,
|
|
92
|
+
JsonSerializer s
|
|
93
|
+
) {
|
|
94
|
+
var obj = JObject.Load(reader);
|
|
95
|
+
return format switch {
|
|
96
|
+
BoxFormat.XYXY => Rect.MinMaxRect(
|
|
97
|
+
xmin: GetFieldValue(obj, fieldNames[0]),
|
|
98
|
+
ymin: GetFieldValue(obj, fieldNames[1]),
|
|
99
|
+
xmax: GetFieldValue(obj, fieldNames[2]),
|
|
100
|
+
ymax: GetFieldValue(obj, fieldNames[3])
|
|
101
|
+
),
|
|
102
|
+
BoxFormat.XYWH => new Rect(
|
|
103
|
+
x: GetFieldValue(obj, fieldNames[0]),
|
|
104
|
+
y: GetFieldValue(obj, fieldNames[1]),
|
|
105
|
+
width: GetFieldValue(obj, fieldNames[2]),
|
|
106
|
+
height: GetFieldValue(obj, fieldNames[3])
|
|
107
|
+
),
|
|
108
|
+
BoxFormat.CxCyWH => GetCenterRect(obj),
|
|
109
|
+
BoxFormat.XYXYXYXY => Rect.MinMaxRect(
|
|
110
|
+
xmin: GetFieldValue(obj, fieldNames[0]), // left
|
|
111
|
+
ymin: GetFieldValue(obj, fieldNames[1]), // top
|
|
112
|
+
xmax: GetFieldValue(obj, fieldNames[4]), // right
|
|
113
|
+
ymax: GetFieldValue(obj, fieldNames[5]) // bottom
|
|
114
|
+
),
|
|
115
|
+
_ => throw new JsonSerializationException($"Failed to read `Rect` from JSON object because of unsupported format: {format}")
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private Rect GetCenterRect(JObject obj) {
|
|
120
|
+
var cx = GetFieldValue(obj, fieldNames[0]);
|
|
121
|
+
var cy = GetFieldValue(obj, fieldNames[1]);
|
|
122
|
+
var w = GetFieldValue(obj, fieldNames[2]);
|
|
123
|
+
var h = GetFieldValue(obj, fieldNames[3]);
|
|
124
|
+
var center = new Vector2(cx, cy);
|
|
125
|
+
var size = new Vector2(w, h);
|
|
126
|
+
return new Rect(center - 0.5f * size, size);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private float GetFieldValue(
|
|
130
|
+
JObject obj,
|
|
131
|
+
string name
|
|
132
|
+
) {
|
|
133
|
+
if (!obj.TryGetValue(name, StringComparison.InvariantCulture, out var value))
|
|
134
|
+
throw new JsonSerializationException($"Missing '{name}' field for {format} box.");
|
|
135
|
+
return (float)value;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
internal static float[] GetRectValues(
|
|
139
|
+
in Rect rect,
|
|
140
|
+
BoxFormat format
|
|
141
|
+
) => format switch {
|
|
142
|
+
BoxFormat.XYXY => new[] { rect.xMin, rect.yMin, rect.xMax, rect.yMax },
|
|
143
|
+
BoxFormat.XYWH => new[] { rect.xMin, rect.yMin, rect.width, rect.height },
|
|
144
|
+
BoxFormat.CxCyWH => new[] { rect.center.x, rect.center.y, rect.width, rect.height },
|
|
145
|
+
BoxFormat.XYXYXYXY => new[] {
|
|
146
|
+
rect.xMin, rect.yMin, // top-left
|
|
147
|
+
rect.xMax, rect.yMin, // top-right
|
|
148
|
+
rect.xMax, rect.yMax, // bottom-right
|
|
149
|
+
rect.xMin, rect.yMax // bottom-left
|
|
150
|
+
},
|
|
151
|
+
_ => throw new ArgumentOutOfRangeException(nameof(format))
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
private static string[] GetReferenceFieldNames(BoxFormat format) => format switch {
|
|
155
|
+
BoxFormat.XYXY => new[] { @"x_min", @"y_min", @"x_max", @"y_max" },
|
|
156
|
+
BoxFormat.XYWH => new[] { @"x", @"y", @"width", @"height" },
|
|
157
|
+
BoxFormat.CxCyWH => new[] { @"x_center", @"y_center", @"width", @"height" },
|
|
158
|
+
BoxFormat.XYXYXYXY => new[] { @"x1", @"y1", @"x2", @"y2", @"x3", @"y3", @"x4", @"y4" },
|
|
159
|
+
_ => throw new ArgumentOutOfRangeException(nameof(format))
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/// <summary>
|
|
164
|
+
/// Convert an array field to a `Rect`.
|
|
165
|
+
/// The array MUST contain numbers, and have the required count depending on the box format.
|
|
166
|
+
/// </summary>
|
|
167
|
+
public sealed class ArrayToRectConverter : JsonConverter<Rect> {
|
|
168
|
+
|
|
169
|
+
private readonly BoxFormat format;
|
|
170
|
+
|
|
171
|
+
/// <summary>
|
|
172
|
+
/// Create a converter for the provided box format.
|
|
173
|
+
/// </summary>
|
|
174
|
+
/// <param name="format">Box format to parse.</param>
|
|
175
|
+
public ArrayToRectConverter(BoxFormat format) => this.format = format;
|
|
176
|
+
|
|
177
|
+
public override void WriteJson(
|
|
178
|
+
JsonWriter writer,
|
|
179
|
+
Rect value,
|
|
180
|
+
JsonSerializer serializer
|
|
181
|
+
) {
|
|
182
|
+
var values = ObjectToRectConverter.GetRectValues(value, format);
|
|
183
|
+
var arr = new JArray(values);
|
|
184
|
+
arr.WriteTo(writer);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
public override Rect ReadJson(
|
|
188
|
+
JsonReader reader,
|
|
189
|
+
Type type,
|
|
190
|
+
Rect existing,
|
|
191
|
+
bool hasExisting,
|
|
192
|
+
JsonSerializer s
|
|
193
|
+
) {
|
|
194
|
+
var arr = JArray.Load(reader);
|
|
195
|
+
var expected = ExpectedCount(format);
|
|
196
|
+
if (arr.Count != expected)
|
|
197
|
+
throw new JsonSerializationException($"Expected {expected} numbers for {format} box but got {arr.Count}.");
|
|
198
|
+
var data = arr.ToObject<float[]>()!;
|
|
199
|
+
return format switch {
|
|
200
|
+
BoxFormat.XYXY => Rect.MinMaxRect(
|
|
201
|
+
xmin: data[0],
|
|
202
|
+
ymin: data[1],
|
|
203
|
+
xmax: data[2],
|
|
204
|
+
ymax: data[3]
|
|
205
|
+
),
|
|
206
|
+
BoxFormat.XYWH => new Rect(
|
|
207
|
+
x: data[0],
|
|
208
|
+
y: data[1],
|
|
209
|
+
width: data[2],
|
|
210
|
+
height: data[3]
|
|
211
|
+
),
|
|
212
|
+
BoxFormat.CxCyWH => new Rect(
|
|
213
|
+
x: data[0] - 0.5f * data[2],
|
|
214
|
+
y: data[1] * 0.5f - data[3],
|
|
215
|
+
width: data[2],
|
|
216
|
+
height: data[3]
|
|
217
|
+
),
|
|
218
|
+
_ => throw new JsonSerializationException($"Failed to read `Rect` from JSON array because of unsupported format: {format}")
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private static int ExpectedCount(BoxFormat format) => format switch {
|
|
223
|
+
BoxFormat.XYXY => 4,
|
|
224
|
+
BoxFormat.XYWH => 4,
|
|
225
|
+
BoxFormat.CxCyWH => 4,
|
|
226
|
+
BoxFormat.XYXYXYXY => 8,
|
|
227
|
+
_ => throw new ArgumentOutOfRangeException(nameof(format))
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Muna
|
|
3
|
+
* Copyright © 2025 NatML Inc. All rights reserved.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
#nullable enable
|
|
7
|
+
|
|
8
|
+
namespace Muna.Converters {
|
|
9
|
+
|
|
10
|
+
using System;
|
|
11
|
+
using UnityEngine;
|
|
12
|
+
using Newtonsoft.Json;
|
|
13
|
+
using Newtonsoft.Json.Linq;
|
|
14
|
+
|
|
15
|
+
/// <summary>
|
|
16
|
+
/// Convert an array field to a `Vector2`.
|
|
17
|
+
/// The array MUST contain exactly two numbers.
|
|
18
|
+
/// </summary>
|
|
19
|
+
public sealed class ArrayToVector2Converter : JsonConverter<Vector2> {
|
|
20
|
+
|
|
21
|
+
public override void WriteJson(
|
|
22
|
+
JsonWriter writer,
|
|
23
|
+
Vector2 value,
|
|
24
|
+
JsonSerializer serializer
|
|
25
|
+
) {
|
|
26
|
+
var obj = new JArray { value.x, value.y };
|
|
27
|
+
obj.WriteTo(writer);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public override Vector2 ReadJson(
|
|
31
|
+
JsonReader reader,
|
|
32
|
+
Type type,
|
|
33
|
+
Vector2 existing,
|
|
34
|
+
bool hasExisting,
|
|
35
|
+
JsonSerializer s
|
|
36
|
+
) {
|
|
37
|
+
var arr = JArray.Load(reader);
|
|
38
|
+
return new Vector2(
|
|
39
|
+
x: (float)arr[0],
|
|
40
|
+
y: (float)arr[1]
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Muna
|
|
3
|
+
* Copyright © 2025 NatML Inc. All rights reserved.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
#nullable enable
|
|
7
|
+
|
|
8
|
+
namespace Muna.Converters {
|
|
9
|
+
|
|
10
|
+
using System;
|
|
11
|
+
using UnityEngine;
|
|
12
|
+
using Newtonsoft.Json;
|
|
13
|
+
using Newtonsoft.Json.Linq;
|
|
14
|
+
|
|
15
|
+
/// <summary>
|
|
16
|
+
/// Convert an array field to a `Vector3`.
|
|
17
|
+
/// The array MUST contain exactly three numbers.
|
|
18
|
+
/// </summary>
|
|
19
|
+
public sealed class ArrayToVector3Converter : JsonConverter<Vector3> {
|
|
20
|
+
|
|
21
|
+
public override void WriteJson(
|
|
22
|
+
JsonWriter writer,
|
|
23
|
+
Vector3 value,
|
|
24
|
+
JsonSerializer serializer
|
|
25
|
+
) {
|
|
26
|
+
var obj = new JArray { value.x, value.y, value.z };
|
|
27
|
+
obj.WriteTo(writer);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public override Vector3 ReadJson(
|
|
31
|
+
JsonReader reader,
|
|
32
|
+
Type type,
|
|
33
|
+
Vector3 existing,
|
|
34
|
+
bool hasExisting,
|
|
35
|
+
JsonSerializer s
|
|
36
|
+
) {
|
|
37
|
+
var arr = JArray.Load(reader);
|
|
38
|
+
return new Vector3(
|
|
39
|
+
x: (float)arr[0],
|
|
40
|
+
y: (float)arr[1],
|
|
41
|
+
z: (float)arr[2]
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Muna
|
|
3
|
+
* Copyright © 2025 NatML Inc. All rights reserved.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
#nullable enable
|
|
7
|
+
|
|
8
|
+
namespace Muna.Converters {
|
|
9
|
+
|
|
10
|
+
using System;
|
|
11
|
+
using UnityEngine;
|
|
12
|
+
using Newtonsoft.Json;
|
|
13
|
+
using Newtonsoft.Json.Linq;
|
|
14
|
+
|
|
15
|
+
/// <summary>
|
|
16
|
+
/// Convert an array field to a `Vector4`.
|
|
17
|
+
/// The array MUST contain exactly four numbers.
|
|
18
|
+
/// </summary>
|
|
19
|
+
public sealed class ArrayToVector4Converter : JsonConverter<Vector4> {
|
|
20
|
+
|
|
21
|
+
public override void WriteJson(
|
|
22
|
+
JsonWriter writer,
|
|
23
|
+
Vector4 value,
|
|
24
|
+
JsonSerializer serializer
|
|
25
|
+
) {
|
|
26
|
+
var obj = new JArray { value.x, value.y, value.z, value.w };
|
|
27
|
+
obj.WriteTo(writer);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public override Vector4 ReadJson(
|
|
31
|
+
JsonReader reader,
|
|
32
|
+
Type type,
|
|
33
|
+
Vector4 existing,
|
|
34
|
+
bool hasExisting,
|
|
35
|
+
JsonSerializer s
|
|
36
|
+
) {
|
|
37
|
+
var arr = JArray.Load(reader);
|
|
38
|
+
return new Vector4(
|
|
39
|
+
x: (float)arr[0],
|
|
40
|
+
y: (float)arr[1],
|
|
41
|
+
z: (float)arr[2],
|
|
42
|
+
w: (float)arr[3]
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -27,7 +27,7 @@ namespace Muna.Internal {
|
|
|
27
27
|
/// Predictor cache.
|
|
28
28
|
/// </summary>
|
|
29
29
|
[field: SerializeField, HideInInspector]
|
|
30
|
-
public List<CachedPrediction> cache { get; internal set; } = new();
|
|
30
|
+
public List<PredictionCache.CachedPrediction> cache { get; internal set; } = new();
|
|
31
31
|
|
|
32
32
|
/// <summary>
|
|
33
33
|
/// Settings instance for this project.
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Muna
|
|
3
|
+
* Copyright © 2025 NatML Inc. All rights reserved.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
#nullable enable
|
|
7
|
+
|
|
8
|
+
namespace Muna.Internal {
|
|
9
|
+
|
|
10
|
+
using System;
|
|
11
|
+
using System.IO;
|
|
12
|
+
using System.Security.Cryptography;
|
|
13
|
+
using System.Text;
|
|
14
|
+
using UnityEngine;
|
|
15
|
+
using Newtonsoft.Json;
|
|
16
|
+
using Services;
|
|
17
|
+
|
|
18
|
+
internal static class PredictionCache {
|
|
19
|
+
|
|
20
|
+
#region --Types--
|
|
21
|
+
/// <summary>
|
|
22
|
+
/// Cached prediction.
|
|
23
|
+
/// </summary>
|
|
24
|
+
[Preserve, Serializable]
|
|
25
|
+
internal class CachedPrediction : Prediction {
|
|
26
|
+
public string? clientId;
|
|
27
|
+
public string? configurationId;
|
|
28
|
+
|
|
29
|
+
[Preserve]
|
|
30
|
+
public CachedPrediction() { }
|
|
31
|
+
}
|
|
32
|
+
#endregion
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
#region --Client API--
|
|
36
|
+
/// <summary>
|
|
37
|
+
/// Add a prediction to the cache.
|
|
38
|
+
/// </summary>
|
|
39
|
+
public static void Add(CachedPrediction prediction) {
|
|
40
|
+
var path = GetCachedPath(prediction);
|
|
41
|
+
var json = JsonConvert.SerializeObject(prediction, Formatting.Indented);
|
|
42
|
+
File.WriteAllText(path, json);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/// <summary>
|
|
46
|
+
/// Get a prediction from the cache.
|
|
47
|
+
/// </summary>
|
|
48
|
+
public static bool Get(
|
|
49
|
+
string tag,
|
|
50
|
+
string clientId,
|
|
51
|
+
string configurationId,
|
|
52
|
+
PredictionResource[]? embeddedResources,
|
|
53
|
+
out CachedPrediction? prediction
|
|
54
|
+
) {
|
|
55
|
+
prediction = null;
|
|
56
|
+
// Check filesystem
|
|
57
|
+
var path = GetCachedPath(tag, clientId, configurationId);
|
|
58
|
+
if (!File.Exists(path))
|
|
59
|
+
return false;
|
|
60
|
+
// Load prediction
|
|
61
|
+
var json = File.ReadAllText(path);
|
|
62
|
+
var result = JsonConvert.DeserializeObject<CachedPrediction>(json)!;
|
|
63
|
+
// Check that resources are valid
|
|
64
|
+
if (!ResourcesAreValid(result.resources!, embeddedResources)) {
|
|
65
|
+
File.Delete(path);
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
// Pass
|
|
69
|
+
prediction = result;
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/// <summary>
|
|
74
|
+
/// Remove a prediction from the cache.
|
|
75
|
+
/// </summary>
|
|
76
|
+
/// <param name="prediction"></param>
|
|
77
|
+
public static void Remove(CachedPrediction prediction) {
|
|
78
|
+
var path = GetCachedPath(prediction);
|
|
79
|
+
if (File.Exists(path))
|
|
80
|
+
File.Delete(path);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/// <summary>
|
|
84
|
+
/// Create a cached prediction from a prediction.
|
|
85
|
+
/// </summary>
|
|
86
|
+
public static CachedPrediction AsCached(
|
|
87
|
+
this Prediction prediction,
|
|
88
|
+
string? clientId = null,
|
|
89
|
+
string? configurationId = null
|
|
90
|
+
) => new() {
|
|
91
|
+
id = prediction.id,
|
|
92
|
+
tag = prediction.tag,
|
|
93
|
+
created = prediction.created,
|
|
94
|
+
results = prediction.results,
|
|
95
|
+
latency = prediction.latency,
|
|
96
|
+
error = prediction.error,
|
|
97
|
+
logs = prediction.logs,
|
|
98
|
+
resources = prediction.resources,
|
|
99
|
+
configuration = prediction.configuration,
|
|
100
|
+
clientId = clientId,
|
|
101
|
+
configurationId = configurationId
|
|
102
|
+
};
|
|
103
|
+
#endregion
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
#region --Operations--
|
|
107
|
+
private static string CacheRoot => Application.isEditor ?
|
|
108
|
+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), @".fxn") :
|
|
109
|
+
Path.Combine(Application.persistentDataPath, @"fxn");
|
|
110
|
+
internal static string ResourceCachePath => Path.Combine(CacheRoot, @"cache");
|
|
111
|
+
internal static string PredictorCachePath => Path.Combine(CacheRoot, @"predictors");
|
|
112
|
+
|
|
113
|
+
private static string GetCachedPath(CachedPrediction prediction) => GetCachedPath(
|
|
114
|
+
tag: prediction.tag,
|
|
115
|
+
clientId: prediction.clientId!,
|
|
116
|
+
configurationId: prediction.configurationId!
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
private static string GetCachedPath(
|
|
120
|
+
string tag,
|
|
121
|
+
string clientId,
|
|
122
|
+
string configurationId
|
|
123
|
+
) {
|
|
124
|
+
if (!Directory.Exists(PredictorCachePath))
|
|
125
|
+
Directory.CreateDirectory(PredictorCachePath);
|
|
126
|
+
using var sha256 = new SHA256Managed();
|
|
127
|
+
var identifier = $"{tag}::{clientId}::{configurationId}";
|
|
128
|
+
var identifierData = Encoding.UTF8.GetBytes(identifier);
|
|
129
|
+
var hashData = sha256.ComputeHash(identifierData);
|
|
130
|
+
var hash = BitConverter.ToString(hashData).Replace("-", "").ToLower();
|
|
131
|
+
var path = Path.Combine(PredictorCachePath, $"{hash}.json");
|
|
132
|
+
return path;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
private static bool ResourcesAreValid(
|
|
136
|
+
PredictionResource[] cachedResources,
|
|
137
|
+
PredictionResource[]? embeddedResources
|
|
138
|
+
) {
|
|
139
|
+
if (embeddedResources != null && cachedResources.Length != embeddedResources.Length)
|
|
140
|
+
return false;
|
|
141
|
+
for (var idx = 0; idx < cachedResources.Length; ++idx) {
|
|
142
|
+
var cachedResource = cachedResources[idx];
|
|
143
|
+
var cachedPath = new Uri(cachedResource.url).LocalPath;
|
|
144
|
+
var embeddedPath = embeddedResources != null ?
|
|
145
|
+
PredictionService.GetResourcePath(embeddedResources[idx], ResourceCachePath) :
|
|
146
|
+
null;
|
|
147
|
+
|
|
148
|
+
Application.SetStackTraceLogType(LogType.Log, StackTraceLogType.None);
|
|
149
|
+
Debug.Log($"Cached path: {cachedPath} Embedded path: {embeddedPath}");
|
|
150
|
+
|
|
151
|
+
if (!File.Exists(cachedPath))
|
|
152
|
+
return false;
|
|
153
|
+
if (embeddedPath != null && cachedPath != embeddedPath)
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
#endregion
|
|
159
|
+
}
|
|
160
|
+
}
|