-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlug.cs
More file actions
94 lines (76 loc) · 4.07 KB
/
Plug.cs
File metadata and controls
94 lines (76 loc) · 4.07 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
using AOT;
using UnityEngine;
using UnityEngine.Events;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
public static class Plug
{
[DllImport("__Internal")]
private static extern void PlugRequestConnect(int callbackID, string whitelistJson, string host, RequestCallback callbackPtr);
[DllImport("__Internal")]
private static extern void PlugRequestConnectWithAgent(int callbackID, string whitelistJson, string host, RequestCallback callbackPtr);
[DllImport("__Internal")]
private static extern void PlugCreateAgent(int callbackID, RequestCallback callbackPtr);
[DllImport("__Internal")]
private static extern void PlugIsConnected(int callbackID, RequestCallback callbackPtr);
[DllImport("__Internal")]
private static extern void PlugGetPrincipal(int callbackID, RequestCallback callbackPtr);
[DllImport("__Internal")]
private static extern void PlugRequestBalance(int callbackID, RequestCallback callbackPtr);
// This is the callback, whose pointer we'll send to javascript and is called by emscripten's Runtime.dynCall.
public delegate void RequestCallback(int callbackID, string response, string error);
/// Keeps track of pending callbacks by their id, once callback is received it is executed and removed from the book.
static Dictionary<int, Action<string, string>> callbacksBook = new Dictionary<int, Action<string, string>>();
static int callbackIDIncrementer = 0;
/// Called from the javascript side, this is the function whose pointer we passed to lookup
/// This must match the return type and arguments of RequestCallback
[MonoPInvokeCallback(typeof(Action))]
private static void GlobalCallback(int callbackID, string response, string error)
{
if (callbacksBook.TryGetValue(callbackID, out Action<string, string> callback))
{
callback?.Invoke(response, error);
}
// Remove this request from the tracker as it is done.
callbacksBook.Remove(callbackID);
}
/// Setup the callback. Returns the callbackID
private static int SetupCallback(Action<string, string> callback)
{
int callbackID = callbackIDIncrementer;
callbackIDIncrementer++;
callbacksBook.Add(callbackID, callback);
return callbackID;
}
public static void RequestConnect(string[] whitelist, string host, Action<string, string> callback)
{
// Now call the javascript function and when it is done it'll callback the C# GlobalCallback function.
PlugRequestConnect(SetupCallback(callback), JsonUtility.ToJson(whitelist), host, GlobalCallback);
}
public static void RequestConnectWithAgent(string[] whitelist, string host, Action<string, string> callback)
{
// Now call the javascript function and when it is done it'll callback the C# GlobalCallback function.
PlugRequestConnectWithAgent(SetupCallback(callback), JsonUtility.ToJson(whitelist), host, GlobalCallback);
}
public static void CreateAgent(Action<string, string> callback)
{
// Now call the javascript function and when it is done it'll callback the C# GlobalCallback function.
PlugCreateAgent(SetupCallback(callback), GlobalCallback);
}
public static void IsConnected(Action<string, string> callback)
{
// Now call the javascript function and when it is done it'll callback the C# GlobalCallback function.
PlugIsConnected(SetupCallback(callback), GlobalCallback);
}
public static void GetPrincipal(Action<string, string> callback)
{
// Now call the javascript function and when it is done it'll callback the C# GlobalCallback function.
PlugGetPrincipal(SetupCallback(callback), GlobalCallback);
}
public static void RequestBalance(Action<string, string> callback)
{
// Now call the javascript function and when it is done it'll callback the C# GlobalCallback function.
PlugRequestBalance(SetupCallback(callback), GlobalCallback);
}
}