-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJSBridge.cs
More file actions
296 lines (241 loc) · 8.73 KB
/
Copy pathJSBridge.cs
File metadata and controls
296 lines (241 loc) · 8.73 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
using System;
using System.Linq;
using JetBrains.Annotations;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
/// <summary>
/// Used for interacting with the unity renderer from JavaScript.
///
/// Usage: unityInstance.SendMessage('JSBridge', 'MethodName', 'value');
/// </summary>
public class JSBridge : MonoBehaviour
{
[System.Serializable]
public class OverridesData
{
public string mode;
public string profile;
public string emote;
public string urn;
public string background;
public string skinColor;
public string hairColor;
public string eyeColor;
public string bodyShape;
public string showAnimationReference;
public string projection;
public string base64;
public string contract;
public string item;
public string token;
public string env;
public string disableLoader;
}
[SerializeField] private Bootstrap bootstrap;
[UsedImplicitly]
public void ParseFromURL()
{
bootstrap.ParseFromURL();
bootstrap.InvokeReload();
}
[UsedImplicitly]
public void SetMode(string value)
{
bootstrap.Config.SetMode(value);
bootstrap.InvokeReload();
}
[UsedImplicitly]
public void SetProfile(string value)
{
bootstrap.Config.Profile = value;
bootstrap.InvokeReload();
}
[UsedImplicitly]
public void SetEmote(string value)
{
bootstrap.Config.Emote = value;
bootstrap.InvokeReload();
}
[UsedImplicitly]
public void SetBase64(string value)
{
bootstrap.Config.SetBase64(value);
bootstrap.InvokeReload();
}
[UsedImplicitly]
public void SetUrns(string value)
{
bootstrap.Config.Urns = value.Split(',', StringSplitOptions.RemoveEmptyEntries).ToList();
bootstrap.InvokeReload();
}
[UsedImplicitly]
public void SetBackground(string value)
{
bootstrap.Config.SetBackground(value);
bootstrap.InvokeLightReload();
}
[UsedImplicitly]
public void SetSkinColor(string value)
{
bootstrap.Config.SetSkinColor(value);
bootstrap.InvokeReload();
}
[UsedImplicitly]
public void SetHairColor(string value)
{
bootstrap.Config.SetHairColor(value);
bootstrap.InvokeReload();
}
[UsedImplicitly]
public void SetEyeColor(string value)
{
bootstrap.Config.SetEyeColor(value);
bootstrap.InvokeReload();
}
[UsedImplicitly]
public void SetBodyShape(string value)
{
bootstrap.Config.BodyShape = value;
bootstrap.InvokeReload();
}
[UsedImplicitly]
public void SetShowAnimationReference(string value)
{
bootstrap.Config.ShowAnimationReference = bool.Parse(value);
bootstrap.InvokeLightReload();
}
[UsedImplicitly]
public void SetProjection(string value)
{
bootstrap.Config.Projection = value;
bootstrap.InvokeReload();
}
[UsedImplicitly]
public void SetContract(string value)
{
bootstrap.Config.Contract = value;
bootstrap.InvokeReload();
}
[UsedImplicitly]
public void SetItemID(string value)
{
bootstrap.Config.ItemID = value;
bootstrap.InvokeReload();
}
[UsedImplicitly]
public void SetTokenID(string value)
{
bootstrap.Config.TokenID = value;
bootstrap.InvokeReload();
}
[UsedImplicitly]
public void SetDisableLoader(string value)
{
bootstrap.Config.DisableLoader = bool.Parse(value);
bootstrap.InvokeLightReload();
}
[UsedImplicitly]
public void TakeScreenshot()
{
StartCoroutine(TakeScreenshotCoroutine());
}
private static async Awaitable TakeScreenshotCoroutine()
{
await Awaitable.EndOfFrameAsync();
var width = Screen.width;
var height = Screen.height;
var rt = RenderTexture.GetTemporary(width, height, 0, GraphicsFormat.B8G8R8A8_UNorm);
ScreenCapture.CaptureScreenshotIntoRenderTexture(rt);
var gpuReadbackRequest = await AsyncGPUReadback.RequestAsync(rt);
if (gpuReadbackRequest.hasError)
{
Debug.LogError("Failed to capture screenshot");
NativeCalls.OnScreenshotTaken(null);
return;
}
var sourceData = gpuReadbackRequest.GetData<byte>();
var texture = new Texture2D(width, height, TextureFormat.BGRA32, false);
var destinationData = texture.GetRawTextureData<byte>();
// We have to flip the pixels vertically because OpenGL reasons
for (var i = 0; i < sourceData.Length; i += 4)
{
var arrayIndex = i / 4;
var x = arrayIndex % width;
var y = arrayIndex / width;
var flippedY = (height - 1 - y);
var flippedIndex = x + flippedY * width;
destinationData[i] = sourceData[flippedIndex * 4];
destinationData[i + 1] = sourceData[flippedIndex * 4 + 1];
destinationData[i + 2] = sourceData[flippedIndex * 4 + 2];
destinationData[i + 3] = sourceData[flippedIndex * 4 + 3];
}
var pngBytes = texture.EncodeToPNG();
var base64Png = Convert.ToBase64String(pngBytes);
NativeCalls.OnScreenshotTaken(base64Png);
RenderTexture.ReleaseTemporary(rt);
}
[UsedImplicitly]
public void SetOverrides(string jsonValue)
{
try
{
var overrides = JsonUtility.FromJson<OverridesData>(jsonValue);
if (!string.IsNullOrEmpty(overrides.mode))
bootstrap.Config.SetMode(overrides.mode);
if (!string.IsNullOrEmpty(overrides.profile))
bootstrap.Config.Profile = overrides.profile;
if (!string.IsNullOrEmpty(overrides.emote))
bootstrap.Config.Emote = overrides.emote;
if (!string.IsNullOrEmpty(overrides.urn))
bootstrap.Config.Urns.Add(overrides.urn);
if (!string.IsNullOrEmpty(overrides.background))
bootstrap.Config.SetBackground(overrides.background);
if (!string.IsNullOrEmpty(overrides.skinColor))
bootstrap.Config.SetSkinColor(overrides.skinColor);
if (!string.IsNullOrEmpty(overrides.hairColor))
bootstrap.Config.SetHairColor(overrides.hairColor);
if (!string.IsNullOrEmpty(overrides.eyeColor))
bootstrap.Config.SetEyeColor(overrides.eyeColor);
if (!string.IsNullOrEmpty(overrides.bodyShape))
bootstrap.Config.BodyShape = overrides.bodyShape;
if (!string.IsNullOrEmpty(overrides.showAnimationReference))
bootstrap.Config.ShowAnimationReference = bool.Parse(overrides.showAnimationReference);
if (!string.IsNullOrEmpty(overrides.projection))
bootstrap.Config.Projection = overrides.projection;
if (!string.IsNullOrEmpty(overrides.base64))
bootstrap.Config.SetBase64(overrides.base64);
if (!string.IsNullOrEmpty(overrides.contract))
bootstrap.Config.Contract = overrides.contract;
if (!string.IsNullOrEmpty(overrides.item))
bootstrap.Config.ItemID = overrides.item;
if (!string.IsNullOrEmpty(overrides.token))
bootstrap.Config.TokenID = overrides.token;
if (!string.IsNullOrEmpty(overrides.env))
{
APIService.Environment = overrides.env == "dev" ? "zone" : "org";
Debug.Log($"Using environment {APIService.Environment}");
}
if (!string.IsNullOrEmpty(overrides.disableLoader))
bootstrap.Config.DisableLoader = bool.Parse(overrides.disableLoader);
bootstrap.InvokeReload();
}
catch (Exception ex)
{
Debug.LogError($"Failed to parse overrides JSON: {ex.Message}");
}
}
public static class NativeCalls
{
#if UNITY_EDITOR
public static void OnScreenshotTaken(string base64Str) =>
Debug.Log($"NativeCall OnScreenshotTaken({base64Str.Length} bytes)");
public static void OnLoadComplete() => Debug.Log("NativeCall OnLoadComplete");
#else
[System.Runtime.InteropServices.DllImport("__Internal")]
public static extern void OnScreenshotTaken(string base64Str);
[System.Runtime.InteropServices.DllImport("__Internal")]
public static extern void OnLoadComplete();
#endif
}
}