This repository was archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathUnityInterface.ts
More file actions
749 lines (604 loc) · 25.7 KB
/
Copy pathUnityInterface.ts
File metadata and controls
749 lines (604 loc) · 25.7 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
import { Vector3 } from '@dcl/ecs-math'
import { WSS_ENABLED, WORLD_EXPLORER, RESET_TUTORIAL } from 'config'
import { AirdropInfo } from 'shared/airdrops/interface'
import { HotSceneInfo, IUnityInterface, setUnityInstance, MinimapSceneInfo } from './IUnityInterface'
import {
HUDConfiguration,
InstancedSpawnPoint,
LoadableParcelScene,
Notification,
ChatMessage,
HUDElementID,
FriendsInitializationMessage,
FriendshipUpdateStatusMessage,
UpdateUserStatusMessage,
RenderProfile,
BuilderConfiguration,
RealmsInfoForRenderer,
TutorialInitializationMessage,
WorldPosition,
HeaderRequest,
AddFriendsPayload,
AddFriendRequestsPayload,
UpdateTotalUnseenMessagesPayload,
UpdateUserUnseenMessagesPayload,
AddChatMessagesPayload,
UpdateTotalUnseenMessagesByUserPayload,
AddFriendsWithDirectMessagesPayload,
FriendsInitializeChatPayload,
UpdateTotalFriendRequestsPayload,
UpdateTotalFriendsPayload,
UpdateTotalUnseenMessagesByChannelPayload,
ChannelInfoPayloads,
UpdateChannelMembersPayload,
ChannelSearchResultsPayload,
ChannelErrorPayload,
SetAudioDevicesPayload
} from 'shared/types'
import { nativeMsgBridge } from './nativeMessagesBridge'
import { createUnityLogger, ILogger } from 'shared/logger'
import { setDelightedSurveyEnabled } from './delightedSurvey'
import { QuestForRenderer } from '@dcl/ecs-quests/@dcl/types'
import { profileToRendererFormat } from 'shared/profiles/transformations/profileToRendererFormat'
import { Emote, WearableV2 } from 'shared/catalogs/types'
import { Observable } from 'mz-observable'
import type { UnityGame } from '@dcl/unity-renderer/src'
import { FeatureFlag } from 'shared/meta/types'
import { getProvider } from 'shared/session/index'
import { uuid } from 'atomicHelpers/math'
import future, { IFuture } from 'fp-future'
import { futures } from './BrowserInterface'
import { trackEvent } from 'shared/analytics'
import { Avatar, ContentMapping } from '@dcl/schemas'
import { AddUserProfilesToCatalogPayload, NewProfileForRenderer } from 'shared/profiles/transformations/types'
import { incrementCounter } from '../shared/occurences'
import { AboutResponse } from '@dcl/protocol/out-ts/decentraland/bff/http_endpoints.gen'
const MINIMAP_CHUNK_SIZE = 100
export const originalPixelRatio: number = devicePixelRatio
devicePixelRatio = 1
const unityLogger: ILogger = createUnityLogger()
export class UnityInterface implements IUnityInterface {
public logger = unityLogger
public gameInstance!: UnityGame
public Module: any
public crashPayloadResponseObservable: Observable<string> = new Observable<string>()
public SetTargetHeight(height: number): void {
// above 2000 is assumed "Match display", below that it is "Normal"
// as defined in https://rfc.decentraland.org/adr/ADR-83
if (height >= 2000) {
devicePixelRatio = originalPixelRatio
} else {
devicePixelRatio = 1
}
}
public Init(gameInstance: UnityGame): void {
if (!WSS_ENABLED) {
nativeMsgBridge.initNativeMessages(gameInstance)
}
this.gameInstance = gameInstance
this.Module = this.gameInstance.Module
}
public SendGenericMessage(object: string, method: string, payload: string) {
this.SendMessageToUnity(object, method, payload)
}
public SetDebug() {
this.SendMessageToUnity('Main', 'SetDebug')
}
public LoadProfile(profile: NewProfileForRenderer) {
this.SendMessageToUnity('Main', 'LoadProfile', JSON.stringify(profile))
}
public UpdateHomeScene(sceneCoords: string) {
this.SendMessageToUnity('Main', 'UpdateHomeScene', sceneCoords)
}
public SetRenderProfile(id: RenderProfile) {
this.SendMessageToUnity('Main', 'SetRenderProfile', JSON.stringify({ id: id }))
}
public SetAudioDevices(devices: SetAudioDevicesPayload) {
this.SendMessageToUnity('Bridges', 'SetAudioDevices', JSON.stringify(devices))
}
public CreateGlobalScene(data: {
id: string
name: string
baseUrl: string
contents: Array<ContentMapping>
icon?: string
isPortableExperience: boolean
sceneNumber: number
sdk7: boolean
}) {
/**
* UI Scenes are scenes that does not check any limit or boundary. The
* position is fixed at 0,0 and they are universe-wide. An example of this
* kind of scenes is the Avatar scene. All the avatars are just GLTFs in
* a scene.
*/
this.SendMessageToUnity('Main', 'CreateGlobalScene', JSON.stringify(data))
}
/** Sends the camera position & target to the engine */
public Teleport(
{ position: { x, y, z }, cameraTarget }: InstancedSpawnPoint,
rotateIfTargetIsNotSet: boolean = true
) {
const theY = y <= 0 ? 2 : y
this.SendMessageToUnity('CharacterController', 'Teleport', JSON.stringify({ x, y: theY, z }))
if (cameraTarget || rotateIfTargetIsNotSet) {
this.SendMessageToUnity('CameraController', 'SetRotation', JSON.stringify({ x, y: theY, z, cameraTarget }))
}
}
/** Tells the engine which scenes to load */
public LoadParcelScenes(parcelsToLoad: LoadableParcelScene[]) {
if (parcelsToLoad.length > 1) {
throw new Error('Only one scene at a time!')
}
this.SendMessageToUnity('Main', 'LoadParcelScenes', JSON.stringify(parcelsToLoad[0]))
}
public UnloadScene(sceneId: string) {
this.SendMessageToUnity('Main', 'UnloadScene', sceneId)
}
public UnloadSceneV2(sceneNumber: number) {
this.SendMessageToUnity('Main', 'UnloadSceneV2', JSON.stringify(sceneNumber))
}
public SendSceneMessage(messages: string) {
this.SendMessageToUnity(`SceneController`, `SendSceneMessage`, messages)
}
/** @deprecated send it with the kernelConfigForRenderer instead. */
public SetSceneDebugPanel() {
this.SendMessageToUnity('Main', 'SetSceneDebugPanel')
}
public ShowFPSPanel() {
this.SendMessageToUnity('Main', 'ShowFPSPanel')
}
public DetectABs(data: { isOn: boolean; forCurrentScene: boolean }) {
this.SendMessageToUnity('Bridges', 'DetectABs', JSON.stringify(data))
}
public HideFPSPanel() {
this.SendMessageToUnity('Main', 'HideFPSPanel')
}
public SetEngineDebugPanel() {
this.SendMessageToUnity('Main', 'SetEngineDebugPanel')
}
public SetDisableAssetBundles() {
this.SendMessageToUnity('Main', 'SetDisableAssetBundles')
}
public async CrashPayloadRequest(): Promise<string> {
// Over wasm this should come back on the same call stack frame because
// the response comes within the CrashPayloadRequest method body.
// For websocket this should take more frames, so we need promises.
const promise = new Promise<string>((resolve, reject) => {
const crashListener = this.crashPayloadResponseObservable.addOnce((payload) => {
resolve(payload)
})
setTimeout(() => {
this.crashPayloadResponseObservable.remove(crashListener)
reject()
}, 2000)
this.SendMessageToUnity('Main', 'CrashPayloadRequest')
})
return promise
}
public ActivateRendering() {
this.SendMessageToUnity('Main', 'ActivateRendering')
}
public SetLoadingScreen(data: { isVisible: boolean; message: string; showTips: boolean }) {
if (!this.gameInstance) {
return
}
this.SendMessageToUnity('Bridges', 'SetLoadingScreen', JSON.stringify(data))
}
public FadeInLoadingHUD(data: { xCoord: number; yCoord: number; message?: string }) {
if (!this.gameInstance) {
return
}
this.SendMessageToUnity('Bridges', 'FadeInLoadingHUD', JSON.stringify(data))
}
public SendMemoryUsageToRenderer() {
const memory = (performance as any).memory
const jsHeapSizeLimit = memory?.jsHeapSizeLimit
const totalJSHeapSize = memory?.totalJSHeapSize
const usedJSHeapSize = memory?.usedJSHeapSize
this.SendMessageToUnity(
'Main',
'SetMemoryUsage',
JSON.stringify({ jsHeapSizeLimit, totalJSHeapSize, usedJSHeapSize })
)
}
public UpdateRealmAbout(configurations: AboutResponse) {
this.SendMessageToUnity('Bridges', 'SetRealmAbout', JSON.stringify(configurations))
}
public DeactivateRendering() {
this.SendMessageToUnity('Main', 'DeactivateRendering')
}
public ReportFocusOn() {
this.SendMessageToUnity('Bridges', 'ReportFocusOn')
}
public ReportFocusOff() {
this.SendMessageToUnity('Bridges', 'ReportFocusOff')
}
public UnlockCursor() {
this.SetCursorState(false)
}
public SetCursorState(locked: boolean) {
this.SendMessageToUnity('Bridges', 'UnlockCursorBrowser', locked ? 1 : 0)
}
public SetBuilderReady() {
this.SendMessageToUnity('Main', 'BuilderReady')
}
public AddUserProfileToCatalog(peerProfile: NewProfileForRenderer) {
this.SendMessageToUnity('Main', 'AddUserProfileToCatalog', JSON.stringify(peerProfile))
}
public AddWearablesToCatalog(wearables: WearableV2[], context?: string) {
const jsonString = JSON.stringify({ wearables, context })
const result = jsonString.match(/.{1,1306299}/g) || []
for (let i = 0; i < result.length; i++) {
this.SendMessageToUnity('Main', 'AddWearablesToCatalog', result[i])
}
}
public AddEmotesToCatalog(emotes: Emote[], context?: string) {
this.SendMessageToUnity('Bridges', 'AddEmotesToCatalog', JSON.stringify({ emotes, context }))
}
public WearablesRequestFailed(error: string, context: string | undefined) {
this.SendMessageToUnity('Main', 'WearablesRequestFailed', JSON.stringify({ error, context }))
}
public RemoveWearablesFromCatalog(wearableIds: string[]) {
this.SendMessageToUnity('Main', 'RemoveWearablesFromCatalog', JSON.stringify(wearableIds))
}
public ClearWearableCatalog() {
this.SendMessageToUnity('Main', 'ClearWearableCatalog')
}
public ShowNotification(notification: Notification) {
this.SendMessageToUnity('HUDController', 'ShowNotificationFromJson', JSON.stringify(notification))
}
public ConfigureHUDElement(
hudElementId: HUDElementID,
configuration: HUDConfiguration,
extraPayload: any | null = null
) {
this.SendMessageToUnity(
'HUDController',
`ConfigureHUDElement`,
JSON.stringify({
hudElementId: hudElementId,
configuration: configuration,
extraPayload: extraPayload ? JSON.stringify(extraPayload) : null
})
)
}
public ShowWelcomeNotification() {
this.SendMessageToUnity('HUDController', 'ShowWelcomeNotification')
}
/** @deprecated */
public TriggerSelfUserExpression(expressionId: string) {
this.SendMessageToUnity('HUDController', 'TriggerSelfUserExpression', expressionId)
}
public UpdateMinimapSceneInformation(info: MinimapSceneInfo[]) {
for (let i = 0; i < info.length; i += MINIMAP_CHUNK_SIZE) {
const chunk = info.slice(i, i + MINIMAP_CHUNK_SIZE)
this.SendMessageToUnity('Main', 'UpdateMinimapSceneInformation', JSON.stringify(chunk))
}
}
public SetTutorialEnabled(tutorialConfig: TutorialInitializationMessage) {
this.SendMessageToUnity('TutorialController', 'SetTutorialEnabled', JSON.stringify(tutorialConfig))
}
public SetTutorialEnabledForUsersThatAlreadyDidTheTutorial(tutorialConfig: TutorialInitializationMessage) {
this.SendMessageToUnity(
'TutorialController',
'SetTutorialEnabledForUsersThatAlreadyDidTheTutorial',
JSON.stringify(tutorialConfig)
)
}
public TriggerAirdropDisplay(_data: AirdropInfo) {
// Disabled for security reasons
}
public AddMessageToChatWindow(message: ChatMessage) {
if (message.body.length > 1000) {
trackEvent('long_chat_message_ignored', { message: message.body, sender: message.sender })
return
}
this.SendMessageToUnity('Main', 'AddMessageToChatWindow', JSON.stringify(message))
}
public AddChatMessages(addChatMessagesPayload: AddChatMessagesPayload): void {
this.SendMessageToUnity('Main', 'AddChatMessages', JSON.stringify(addChatMessagesPayload))
}
public InitializeFriends(initializationMessage: FriendsInitializationMessage) {
this.SendMessageToUnity('Main', 'InitializeFriends', JSON.stringify(initializationMessage))
}
public InitializeChat(initializationMessage: FriendsInitializeChatPayload): void {
this.SendMessageToUnity('Main', 'InitializeChat', JSON.stringify(initializationMessage))
}
public AddUserProfilesToCatalog(payload: AddUserProfilesToCatalogPayload): void {
this.SendMessageToUnity('Main', 'AddUserProfilesToCatalog', JSON.stringify(payload))
}
public AddFriends(addFriendsPayload: AddFriendsPayload): void {
this.SendMessageToUnity('Main', 'AddFriends', JSON.stringify(addFriendsPayload))
}
public AddFriendRequests(addFriendRequestsPayload: AddFriendRequestsPayload): void {
this.SendMessageToUnity('Main', 'AddFriendRequests', JSON.stringify(addFriendRequestsPayload))
}
public UpdateTotalUnseenMessagesByUser(
updateTotalUnseenMessagesByUserPayload: UpdateTotalUnseenMessagesByUserPayload
): void {
this.SendMessageToUnity(
'Main',
'UpdateTotalUnseenMessagesByUser',
JSON.stringify(updateTotalUnseenMessagesByUserPayload)
)
}
public AddFriendsWithDirectMessages(addFriendsWithDirectMessagesPayload: AddFriendsWithDirectMessagesPayload): void {
this.SendMessageToUnity('Main', 'AddFriendsWithDirectMessages', JSON.stringify(addFriendsWithDirectMessagesPayload))
}
public UpdateTotalFriendRequests(updateTotalFriendRequestsPayload: UpdateTotalFriendRequestsPayload): void {
this.SendMessageToUnity('Main', 'UpdateTotalFriendRequests', JSON.stringify(updateTotalFriendRequestsPayload))
}
public UpdateTotalFriends(updateTotalFriendPayload: UpdateTotalFriendsPayload): void {
this.SendMessageToUnity('Main', 'UpdateTotalFriends', JSON.stringify(updateTotalFriendPayload))
}
public UpdateTotalUnseenMessages(updateTotalUnseenMessagesPayload: UpdateTotalUnseenMessagesPayload): void {
this.SendMessageToUnity('Main', 'UpdateTotalUnseenMessages', JSON.stringify(updateTotalUnseenMessagesPayload))
}
public UpdateUserUnseenMessages(updateUserUnseenMessagesPayload: UpdateUserUnseenMessagesPayload): void {
this.SendMessageToUnity('Main', 'UpdateUserUnseenMessages', JSON.stringify(updateUserUnseenMessagesPayload))
}
public UpdateFriendshipStatus(updateMessage: FriendshipUpdateStatusMessage) {
this.SendMessageToUnity('Main', 'UpdateFriendshipStatus', JSON.stringify(updateMessage))
}
public UpdateUserPresence(status: UpdateUserStatusMessage) {
this.SendMessageToUnity('Main', 'UpdateUserPresence', JSON.stringify(status))
}
public FriendNotFound(queryString: string) {
this.SendMessageToUnity('Main', 'FriendNotFound', JSON.stringify(queryString))
}
public JoinChannelConfirmation(channelInfoPayload: ChannelInfoPayloads) {
this.SendMessageToUnity('Main', 'JoinChannelConfirmation', JSON.stringify(channelInfoPayload))
}
public JoinChannelError(joinChannelErrorPayload: ChannelErrorPayload) {
this.SendMessageToUnity('Main', 'JoinChannelError', JSON.stringify(joinChannelErrorPayload))
}
public UpdateTotalUnseenMessagesByChannel(
updateTotalUnseenMessagesByChannelPayload: UpdateTotalUnseenMessagesByChannelPayload
) {
this.SendMessageToUnity(
'Main',
'UpdateTotalUnseenMessagesByChannel',
JSON.stringify(updateTotalUnseenMessagesByChannelPayload)
)
}
public UpdateChannelInfo(channelInfoPayload: ChannelInfoPayloads) {
this.SendMessageToUnity('Main', 'UpdateChannelInfo', JSON.stringify(channelInfoPayload))
}
public UpdateChannelSearchResults(channelSearchResultsPayload: ChannelSearchResultsPayload) {
this.SendMessageToUnity('Main', 'UpdateChannelSearchResults', JSON.stringify(channelSearchResultsPayload))
}
public LeaveChannelError(leaveChannelErrorPayload: ChannelErrorPayload) {
this.SendMessageToUnity('Main', 'LeaveChannelError', JSON.stringify(leaveChannelErrorPayload))
}
public MuteChannelError(muteChannelErrorPayload: ChannelErrorPayload) {
this.SendMessageToUnity('Main', 'MuteChannelError', JSON.stringify(muteChannelErrorPayload))
}
public UpdateChannelMembers(updateChannelMembersPayload: UpdateChannelMembersPayload) {
this.SendMessageToUnity('Main', 'UpdateChannelMembers', JSON.stringify(updateChannelMembersPayload))
}
// eslint-disable-next-line @typescript-eslint/ban-types
public RequestTeleport(teleportData: {}) {
this.SendMessageToUnity('HUDController', 'RequestTeleport', JSON.stringify(teleportData))
}
public UpdateHotScenesList(info: HotSceneInfo[]) {
const chunks: any[] = []
while (info.length) {
chunks.push(info.splice(0, MINIMAP_CHUNK_SIZE))
}
for (let i = 0; i < chunks.length; i++) {
const payload = { chunkIndex: i, chunksCount: chunks.length, scenesInfo: chunks[i] }
this.SendMessageToUnity('Main', 'UpdateHotScenesList', JSON.stringify(payload))
}
}
public ConnectionToRealmSuccess(successData: WorldPosition) {
this.SendMessageToUnity('Bridges', 'ConnectionToRealmSuccess', JSON.stringify(successData))
}
public ConnectionToRealmFailed(failedData: WorldPosition) {
this.SendMessageToUnity('Bridges', 'ConnectionToRealmFailed', JSON.stringify(failedData))
}
public SendGIFPointers(id: string, width: number, height: number, pointers: number[], frameDelays: number[]) {
this.SendMessageToUnity('Main', 'UpdateGIFPointers', JSON.stringify({ id, width, height, pointers, frameDelays }))
}
public SendGIFFetchFailure(id: string) {
this.SendMessageToUnity('Main', 'FailGIFFetch', id)
}
public ConfigureTutorial(tutorialStep: number, tutorialConfig: TutorialInitializationMessage) {
const tutorialCompletedFlag = 256
if (WORLD_EXPLORER) {
if (RESET_TUTORIAL || (tutorialStep & tutorialCompletedFlag) === 0) {
this.SetTutorialEnabled(tutorialConfig)
} else {
this.SetTutorialEnabledForUsersThatAlreadyDidTheTutorial(tutorialConfig)
setDelightedSurveyEnabled(true)
}
}
}
public UpdateBalanceOfMANA(balance: string) {
this.SendMessageToUnity('HUDController', 'UpdateBalanceOfMANA', balance)
}
public RequestWeb3ApiUse(requestType: string, payload: any): IFuture<boolean> {
const isWalletConnect = (getProvider() as any).wc !== undefined
const id = uuid()
futures[id] = future()
if (!isWalletConnect) {
futures[id].resolve(true)
} else {
this.SendMessageToUnity('Bridges', 'RequestWeb3ApiUse', JSON.stringify({ id, requestType, payload }))
}
return futures[id]
}
public SetPlayerTalking(talking: boolean) {
this.SendMessageToUnity('HUDController', 'SetPlayerTalking', JSON.stringify(talking))
}
public ShowAvatarEditorInSignIn() {
this.SendMessageToUnity('HUDController', 'ShowAvatarEditorInSignUp')
this.SendMessageToUnity('Main', 'ForceActivateRendering')
}
public SetUserTalking(userId: string, talking: boolean) {
this.SendMessageToUnity('HUDController', 'SetUserTalking', JSON.stringify({ userId: userId, talking: talking }))
}
public SetUsersMuted(usersId: string[], muted: boolean) {
this.SendMessageToUnity('HUDController', 'SetUsersMuted', JSON.stringify({ usersId: usersId, muted: muted }))
}
public SetVoiceChatEnabledByScene(enabled: boolean) {
this.SendMessageToUnity('HUDController', 'SetVoiceChatEnabledByScene', enabled ? 1 : 0)
}
public SetVoiceChatStatus(status: { isConnected: boolean }): void {
this.SendMessageToUnity('VoiceChatController', 'VoiceChatStatus', JSON.stringify(status))
}
public SetKernelConfiguration(config: any) {
this.SendMessageToUnity('Bridges', 'SetKernelConfiguration', JSON.stringify(config))
}
public SetFeatureFlagsConfiguration(config: FeatureFlag) {
this.SendMessageToUnity('Bridges', 'SetFeatureFlagConfiguration', JSON.stringify(config))
}
public UpdateRealmsInfo(realmsInfo: Partial<RealmsInfoForRenderer>) {
this.SendMessageToUnity('Bridges', 'UpdateRealmsInfo', JSON.stringify(realmsInfo))
}
public SendPublishSceneResult() {
this.logger.warn('SendPublishSceneResult')
}
public SendBuilderProjectInfo(projectName: string, projectDescription: string, isNewEmptyProject: boolean) {
this.SendMessageToUnity(
'Main',
'BuilderProjectInfo',
JSON.stringify({ title: projectName, description: projectDescription, isNewEmptyProject: isNewEmptyProject })
)
}
// Note: This message is deprecated and should be deleted in the future.
// We are maintaining it for backward compatibility we can safely delete if we are further than 2/03/2022
public SendBuilderCatalogHeaders(headers: Record<string, string>) {
this.SendMessageToUnity('Main', 'BuilderInWorldCatalogHeaders', JSON.stringify(headers))
}
public SendHeaders(endpoint: string, headers: Record<string, string>) {
const request: HeaderRequest = {
endpoint: endpoint,
headers: headers
}
this.SendMessageToUnity('Main', 'RequestedHeaders', JSON.stringify(request))
}
public SendSceneAssets() {
this.logger.warn('SendSceneAssets')
}
public SetENSOwnerQueryResult(searchInput: string, profiles: Avatar[] | undefined, contentServerBaseUrl: string) {
if (!profiles) {
this.SendMessageToUnity('Bridges', 'SetENSOwnerQueryResult', JSON.stringify({ searchInput, success: false }))
return
}
// TODO: why do we send the whole profile while asking for the ENS???
const profilesForRenderer: NewProfileForRenderer[] = []
for (const profile of profiles) {
profilesForRenderer.push(
profileToRendererFormat(profile, { address: profile.userId, baseUrl: contentServerBaseUrl })
)
}
this.SendMessageToUnity(
'Bridges',
'SetENSOwnerQueryResult',
JSON.stringify({ searchInput, success: true, profiles: profilesForRenderer })
)
}
public SendUnpublishSceneResult() {
this.logger.warn('SendUnpublishSceneResult')
}
// *********************************************************************************
// ************** Quests messages **************
// *********************************************************************************
InitQuestsInfo(rendererQuests: QuestForRenderer[]) {
this.SendMessageToUnity('Bridges', 'InitializeQuests', JSON.stringify(rendererQuests))
}
UpdateQuestProgress(rendererQuest: QuestForRenderer) {
this.SendMessageToUnity('Bridges', 'UpdateQuestProgress', JSON.stringify(rendererQuest))
}
// *********************************************************************************
// ************** Builder messages **************
// *********************************************************************************
// @internal
public SendBuilderMessage(method: string, payload: string = '') {
this.SendMessageToUnity(`BuilderController`, method, payload)
}
public SelectGizmoBuilder(type: string) {
this.SendBuilderMessage('SelectGizmo', type)
}
public ResetBuilderObject() {
this.SendBuilderMessage('ResetObject')
}
public SetCameraZoomDeltaBuilder(delta: number) {
this.SendBuilderMessage('ZoomDelta', delta.toString())
}
public GetCameraTargetBuilder(futureId: string) {
this.SendBuilderMessage('GetCameraTargetBuilder', futureId)
}
public SetPlayModeBuilder(on: string) {
this.SendBuilderMessage('SetPlayMode', on)
}
public PreloadFileBuilder(url: string) {
this.SendBuilderMessage('PreloadFile', url)
}
public GetMousePositionBuilder(x: string, y: string, id: string) {
this.SendBuilderMessage('GetMousePosition', `{"x":"${x}", "y": "${y}", "id": "${id}" }`)
}
public TakeScreenshotBuilder(id: string) {
this.SendBuilderMessage('TakeScreenshot', id)
}
public SetCameraPositionBuilder(position: Vector3) {
this.SendBuilderMessage('SetBuilderCameraPosition', position.x + ',' + position.y + ',' + position.z)
}
public SetCameraRotationBuilder(aplha: number, beta: number) {
this.SendBuilderMessage('SetBuilderCameraRotation', aplha + ',' + beta)
}
public ResetCameraZoomBuilder() {
this.SendBuilderMessage('ResetBuilderCameraZoom')
}
public SetBuilderGridResolution(position: number, rotation: number, scale: number) {
this.SendBuilderMessage(
'SetGridResolution',
JSON.stringify({ position: position, rotation: rotation, scale: scale })
)
}
public SetBuilderSelectedEntities(entities: string[]) {
this.SendBuilderMessage('SetSelectedEntities', JSON.stringify({ entities: entities }))
}
public ResetBuilderScene() {
this.SendBuilderMessage('ResetBuilderScene')
}
public OnBuilderKeyDown(key: string) {
this.SendBuilderMessage('OnBuilderKeyDown', key)
}
public SetBuilderConfiguration(config: BuilderConfiguration) {
this.SendBuilderMessage('SetBuilderConfiguration', JSON.stringify(config))
}
// NOTE: we override wasm's setThrew function before sending message to unity and restore it to it's
// original function after message is sent. If an exception is thrown during SendMessage we assume that it's related
// to the code executed by the SendMessage on unity's side.
public SendMessageToUnity(object: string, method: string, payload: any = undefined) {
// "this.Module" is not present when using remote websocket renderer, so we just send the message to unity without doing any override.
if (!this.Module) {
this.gameInstance.SendMessage(object, method, payload)
return
}
incrementCounter(method as any)
const originalSetThrew = this.Module['setThrew']
const unityModule = this.Module
function overrideSetThrew() {
unityModule['setThrew'] = function () {
incrementCounter(`setThrew:${method}`)
const error = `Error while sending Message to Unity. Object: ${object}. Method: ${method}.`
unityLogger.error(error)
// eslint-disable-next-line prefer-rest-params
return originalSetThrew.apply(this, arguments)
}
}
function restoreSetThrew() {
unityModule['setThrew'] = originalSetThrew
}
overrideSetThrew()
try {
this.gameInstance.SendMessage(object, method, payload)
} finally {
restoreSetThrew()
}
}
}
setUnityInstance(new UnityInterface())