-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.cs
More file actions
163 lines (132 loc) · 5.19 KB
/
Main.cs
File metadata and controls
163 lines (132 loc) · 5.19 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
// AvatarConnect Unity SDK - By MoNA Gallery, Inc. and It's Open Source Contributors.
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace AvatarConnect
{
// Main Avatar Connect class
// Use this class to call the modules
public static class Core
{
// Global module registry.
public static AvatarConnectModule[] GetModuleRegistry()
{
return new AvatarConnectModule[]{
new ReadyPlayerMe.ReadyPlayerMeModule(),
new Meebits.MeebitsModule(),
new CryptoAvatars.CryptoAvatarsModule()
};
}
// If AvatarConnect is ready to use
public static bool ServiceInitialized = false;
public static List<AvatarConnectModule> ActiveModules;
public static List<ACAvatar> Avatars;
// Considered temporary.
public static GameObject AvatarObject;
public static AvatarConnectBridge Bridge;
public static RuntimeAnimatorController AvatarController;
public static string[] SupportedAvatarExtensions = { "glb", "vrm" };
// Required startup call, returns true if service is ok.
public static bool Initialize()
{
if (ServiceInitialized) return true;
Avatars = new List<ACAvatar>();
Bridge = new GameObject("AvatarConnectBridge").AddComponent<AvatarConnectBridge>();
// Check if the unity client has internet access
if (Application.internetReachability == NetworkReachability.NotReachable)
{
AvatarConnectError.Fail(AvatarConnectError.SERVICE_CONNECT_ERROR);
return false;
}
// Create fallback object if not AvatarObject is set.
if (AvatarObject == null)
{
AvatarConnectError.Fail(AvatarConnectError.CONSUMER_CHARACTER_NOT_SET);
AvatarObject = new GameObject("Avatar");
}
ActiveModules = new List<AvatarConnectModule>();
// Load generic animator controller
AvatarController = Resources.Load("GenericCharacter") as RuntimeAnimatorController;
if (AvatarController == null)
{
AvatarConnectError.Fail(AvatarConnectError.SERVICE_CONFIG_FAIL);
return false;
}
ServiceInitialized = true;
return true;
}
// Activate all modules
public static void ActivateAllModules()
{
if (!ServiceInitialized) return;
ActivateModules(GetModuleRegistry());
}
// Activate specific modules
public static void ActivateModules(AvatarConnectModule[] avatarConnectModules)
{
if (!ServiceInitialized) return;
foreach (AvatarConnectModule module in avatarConnectModules)
{
AvatarConnectResult result = module.ModuleActivate();
if (!result.Success)
{
ManageModuleError(module, result);
return;
}
ActiveModules.Add(module);
}
}
// Recives data from Avatarconnect
public static void ReceiveMetadata(string metadata)
{
if (!ServiceInitialized) return;
// AvatarRequest request = JsonUtility.FromJson<AvatarRequest>(metadata);
JObject avatarData = JObject.Parse(metadata);
if (avatarData == null || avatarData["avatar"] == null)
{
AvatarConnectError.Fail(AvatarConnectError.SERVICE_JSON_FAIL);
return;
}
AvatarConnectModule module = GetModule(avatarData["provider"].ToString());
if (module == null)
{
AvatarConnectError.Fail(AvatarConnectError.MODULE_NOT_FOUND);
return;
}
if (!module.ModuleInitialized)
{
AvatarConnectError.Fail(AvatarConnectError.MODULE_UNINITIALIZED, module);
return;
}
module.RequestAvatar(AvatarObject, avatarData);
}
// Endpoint for all internal & external errors
public static void ManageModuleError(AvatarConnectModule module, AvatarConnectResult error)
{
if (error.Success) return;
// Assume this is a internal error
if (module == null)
{
// TODO
Debug.LogError("AvatarConnect [Internal]: " + error.Error);
return;
}
Debug.LogError("AvatarConnect " + module.ModuleName + " : " + error.Error);
}
// Get partner module by name, returns null if not found.
public static AvatarConnectModule GetModule(string moduleName)
{
if (!ServiceInitialized || ActiveModules == null) return null;
foreach (AvatarConnectModule module in ActiveModules)
{
if (module.ModuleName == moduleName)
{
return module;
}
}
return null;
}
}
}