forked from EpicGames/PixelStreamingInfrastructure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.ts
970 lines (891 loc) · 33.8 KB
/
Config.ts
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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
// Copyright Epic Games, Inc. All Rights Reserved.
import { Logger } from '../Logger/Logger';
import { SettingFlag } from './SettingFlag';
import { SettingNumber } from './SettingNumber';
import { SettingText } from './SettingText';
import { SettingOption } from './SettingOption';
import { EventEmitter, SettingsChangedEvent } from '../Util/EventEmitter';
import { SettingBase } from './SettingBase';
/**
* A collection of flags that can be toggled and are core to all Pixel Streaming experiences.
* These are used in the `Config.Flags` map.
*/
export class Flags {
static AutoConnect = 'AutoConnect' as const;
static AutoPlayVideo = 'AutoPlayVideo' as const;
static AFKDetection = 'TimeoutIfIdle' as const;
static BrowserSendOffer = 'OfferToReceive' as const;
static HoveringMouseMode = 'HoveringMouse' as const;
static ForceMonoAudio = 'ForceMonoAudio' as const;
static ForceTURN = 'ForceTURN' as const;
static FakeMouseWithTouches = 'FakeMouseWithTouches' as const;
static IsQualityController = 'ControlsQuality' as const;
static MatchViewportResolution = 'MatchViewportRes' as const;
static StartVideoMuted = 'StartVideoMuted' as const;
static SuppressBrowserKeys = 'SuppressBrowserKeys' as const;
static UseMic = 'UseMic' as const;
static KeyboardInput = 'KeyboardInput' as const;
static MouseInput = 'MouseInput' as const;
static TouchInput = 'TouchInput' as const;
static GamepadInput = 'GamepadInput' as const;
static XRControllerInput = 'XRControllerInput' as const;
static WaitForStreamer = "WaitForStreamer" as const;
}
export type FlagsKeys = Exclude<keyof typeof Flags, 'prototype'>;
export type FlagsIds = typeof Flags[FlagsKeys];
const isFlagId = (id: string): id is FlagsIds =>
Object.getOwnPropertyNames(Flags).some(
(name: FlagsKeys) => Flags[name] === id
);
/**
* A collection of numeric parameters that are core to all Pixel Streaming experiences.
*
*/
export class NumericParameters {
static AFKTimeoutSecs = 'AFKTimeout' as const;
static MinQP = 'MinQP' as const;
static MaxQP = 'MaxQP' as const;
static WebRTCFPS = 'WebRTCFPS' as const;
static WebRTCMinBitrate = 'WebRTCMinBitrate' as const;
static WebRTCMaxBitrate = 'WebRTCMaxBitrate' as const;
static MaxReconnectAttempts = 'MaxReconnectAttempts' as const;
static StreamerAutoJoinInterval = 'StreamerAutoJoinInterval' as const;
}
export type NumericParametersKeys = Exclude<
keyof typeof NumericParameters,
'prototype'
>;
export type NumericParametersIds =
typeof NumericParameters[NumericParametersKeys];
const isNumericId = (id: string): id is NumericParametersIds =>
Object.getOwnPropertyNames(NumericParameters).some(
(name: NumericParametersKeys) => NumericParameters[name] === id
);
/**
* A collection of textual parameters that are core to all Pixel Streaming experiences.
*
*/
export class TextParameters {
static SignallingServerUrl = 'ss' as const;
}
export type TextParametersKeys = Exclude<
keyof typeof TextParameters,
'prototype'
>;
export type TextParametersIds = typeof TextParameters[TextParametersKeys];
const isTextId = (id: string): id is TextParametersIds =>
Object.getOwnPropertyNames(TextParameters).some(
(name: TextParametersKeys) => TextParameters[name] === id
);
/**
* A collection of enum based parameters that are core to all Pixel Streaming experiences.
*
*/
export class OptionParameters {
static PreferredCodec = 'PreferredCodec' as const;
static StreamerId = 'StreamerId' as const;
}
export type OptionParametersKeys = Exclude<
keyof typeof OptionParameters,
'prototype'
>;
export type OptionParametersIds = typeof OptionParameters[OptionParametersKeys];
const isOptionId = (id: string): id is OptionParametersIds =>
Object.getOwnPropertyNames(OptionParameters).some(
(name: OptionParametersKeys) => OptionParameters[name] === id
);
/**
* Utility types for inferring data type based on setting ID
*/
export type OptionIds =
| FlagsIds
| NumericParametersIds
| TextParametersIds
| OptionParametersIds;
export type OptionKeys<T> = T extends FlagsIds
? boolean
: T extends NumericParametersIds
? number
: T extends TextParametersIds
? string
: T extends OptionParametersIds
? string
: never;
export type AllSettings = {
[K in OptionIds]: OptionKeys<K>;
};
export interface ConfigParams {
/** Initial Pixel Streaming settings */
initialSettings?: Partial<AllSettings>;
/** If useUrlParams is set true, will read initial values from URL parameters and persist changed settings into URL */
useUrlParams?: boolean;
}
export class Config {
/* A map of flags that can be toggled - options that can be set in the application - e.g. Use Mic? */
private flags = new Map<FlagsIds, SettingFlag>();
/* A map of numerical settings - options that can be in the application - e.g. MinBitrate */
private numericParameters = new Map<NumericParametersIds, SettingNumber>();
/* A map of text settings - e.g. signalling server url */
private textParameters = new Map<TextParametersIds, SettingText>();
/* A map of enum based settings - e.g. preferred codec */
private optionParameters = new Map<OptionParametersIds, SettingOption>();
private _useUrlParams: boolean;
// ------------ Settings -----------------
constructor(config: ConfigParams = {}) {
const { initialSettings, useUrlParams } = config;
this._useUrlParams = !!useUrlParams;
this.populateDefaultSettings(this._useUrlParams, initialSettings);
}
/**
* True if reading configuration initial values from URL parameters, and
* persisting changes in URL when changed.
*/
public get useUrlParams() {
return this._useUrlParams;
}
/**
* Populate the default settings for a Pixel Streaming application
*/
private populateDefaultSettings(useUrlParams: boolean, settings: Partial<AllSettings>): void {
/**
* Text Parameters
*/
this.textParameters.set(
TextParameters.SignallingServerUrl,
new SettingText(
TextParameters.SignallingServerUrl,
'Signalling url',
'Url of the signalling server',
settings && settings.hasOwnProperty(TextParameters.SignallingServerUrl) ?
settings[TextParameters.SignallingServerUrl] :
(location.protocol === 'https:' ? 'wss://' : 'ws://') +
window.location.hostname +
// for readability, we omit the port if it's 80
(window.location.port === '80' ||
window.location.port === ''
? ''
: `:${window.location.port}`),
useUrlParams
)
);
this.optionParameters.set(
OptionParameters.StreamerId,
new SettingOption(
OptionParameters.StreamerId,
'Streamer ID',
'The ID of the streamer to stream.',
settings && settings.hasOwnProperty(OptionParameters.StreamerId) ?
settings[OptionParameters.StreamerId] :
'',
[],
useUrlParams
)
);
/**
* Enum Parameters
*/
this.optionParameters.set(
OptionParameters.PreferredCodec,
new SettingOption(
OptionParameters.PreferredCodec,
'Preferred Codec',
'The preferred codec to be used during codec negotiation',
'H264 level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f',
settings && settings.hasOwnProperty(OptionParameters.PreferredCodec) ?
[settings[OptionParameters.PreferredCodec]] :
(function (): Array<string> {
const browserSupportedCodecs: Array<string> = [];
// Try get the info needed from the RTCRtpReceiver. This is only available on chrome
if (!RTCRtpReceiver.getCapabilities) {
browserSupportedCodecs.push('Only available on Chrome');
return browserSupportedCodecs;
}
const matcher = /(VP\d|H26\d|AV1).*/;
const codecs =
RTCRtpReceiver.getCapabilities('video').codecs;
codecs.forEach((codec) => {
const str =
codec.mimeType.split('/')[1] +
' ' +
(codec.sdpFmtpLine || '');
const match = matcher.exec(str);
if (match !== null) {
browserSupportedCodecs.push(str);
}
});
return browserSupportedCodecs;
})(),
useUrlParams
)
);
/**
* Boolean parameters
*/
this.flags.set(
Flags.AutoConnect,
new SettingFlag(
Flags.AutoConnect,
'Auto connect to stream',
'Whether we should attempt to auto connect to the signalling server or show a click to start prompt.',
settings && settings.hasOwnProperty(Flags.AutoConnect) ?
settings[Flags.AutoConnect] :
false,
useUrlParams
)
);
this.flags.set(
Flags.AutoPlayVideo,
new SettingFlag(
Flags.AutoPlayVideo,
'Auto play video',
'When video is ready automatically start playing it as opposed to showing a play button.',
settings && settings.hasOwnProperty(Flags.AutoPlayVideo) ?
settings[Flags.AutoPlayVideo] :
true,
useUrlParams
)
);
this.flags.set(
Flags.BrowserSendOffer,
new SettingFlag(
Flags.BrowserSendOffer,
'Browser send offer',
'Browser will initiate the WebRTC handshake by sending the offer to the streamer',
settings && settings.hasOwnProperty(Flags.BrowserSendOffer) ?
settings[Flags.BrowserSendOffer] :
false,
useUrlParams
)
);
this.flags.set(
Flags.UseMic,
new SettingFlag(
Flags.UseMic,
'Use microphone',
'Make browser request microphone access and open an input audio track.',
settings && settings.hasOwnProperty(Flags.UseMic) ?
settings[Flags.UseMic] :
false,
useUrlParams
)
);
this.flags.set(
Flags.StartVideoMuted,
new SettingFlag(
Flags.StartVideoMuted,
'Start video muted',
'Video will start muted if true.',
settings && settings.hasOwnProperty(Flags.StartVideoMuted) ?
settings[Flags.StartVideoMuted] :
false,
useUrlParams
)
);
this.flags.set(
Flags.SuppressBrowserKeys,
new SettingFlag(
Flags.SuppressBrowserKeys,
'Suppress browser keys',
'Suppress certain browser keys that we use in UE, for example F5 to show shader complexity instead of refresh the page.',
settings && settings.hasOwnProperty(Flags.SuppressBrowserKeys) ?
settings[Flags.SuppressBrowserKeys] :
true,
useUrlParams
)
);
this.flags.set(
Flags.IsQualityController,
new SettingFlag(
Flags.IsQualityController,
'Is quality controller?',
'True if this peer controls stream quality',
settings && settings.hasOwnProperty(Flags.IsQualityController) ?
settings[Flags.IsQualityController] :
true,
useUrlParams
)
);
this.flags.set(
Flags.ForceMonoAudio,
new SettingFlag(
Flags.ForceMonoAudio,
'Force mono audio',
'Force browser to request mono audio in the SDP',
settings && settings.hasOwnProperty(Flags.ForceMonoAudio) ?
settings[Flags.ForceMonoAudio] :
false,
useUrlParams
)
);
this.flags.set(
Flags.ForceTURN,
new SettingFlag(
Flags.ForceTURN,
'Force TURN',
'Only generate TURN/Relayed ICE candidates.',
settings && settings.hasOwnProperty(Flags.ForceTURN) ?
settings[Flags.ForceTURN] :
false,
useUrlParams
)
);
this.flags.set(
Flags.AFKDetection,
new SettingFlag(
Flags.AFKDetection,
'AFK if idle',
'Timeout the experience if user is AFK for a period.',
settings && settings.hasOwnProperty(Flags.AFKDetection) ?
settings[Flags.AFKDetection] :
false,
useUrlParams
)
);
this.flags.set(
Flags.MatchViewportResolution,
new SettingFlag(
Flags.MatchViewportResolution,
'Match viewport resolution',
'Pixel Streaming will be instructed to dynamically resize the video stream to match the size of the video element.',
settings && settings.hasOwnProperty(Flags.MatchViewportResolution) ?
settings[Flags.MatchViewportResolution] :
false,
useUrlParams
)
);
this.flags.set(
Flags.HoveringMouseMode,
new SettingFlag(
Flags.HoveringMouseMode,
'Control Scheme: Locked Mouse',
'Either locked mouse, where the pointer is consumed by the video and locked to it, or hovering mouse, where the mouse is not consumed.',
settings && settings.hasOwnProperty(Flags.HoveringMouseMode) ?
settings[Flags.HoveringMouseMode] :
false,
useUrlParams,
(isHoveringMouse: boolean, setting: SettingBase) => {
setting.label = `Control Scheme: ${isHoveringMouse ? 'Hovering' : 'Locked'} Mouse`;
}
)
);
this.flags.set(
Flags.FakeMouseWithTouches,
new SettingFlag(
Flags.FakeMouseWithTouches,
'Fake mouse with touches',
'A single finger touch is converted into a mouse event. This allows a non-touch application to be controlled partially via a touch device.',
settings && settings.hasOwnProperty(Flags.FakeMouseWithTouches) ?
settings[Flags.FakeMouseWithTouches] :
true,
useUrlParams
)
);
this.flags.set(
Flags.KeyboardInput,
new SettingFlag(
Flags.KeyboardInput,
'Keyboard input',
'If enabled, send keyboard events to streamer',
settings && settings.hasOwnProperty(Flags.KeyboardInput) ?
settings[Flags.KeyboardInput] :
true,
useUrlParams
)
);
this.flags.set(
Flags.MouseInput,
new SettingFlag(
Flags.MouseInput,
'Mouse input',
'If enabled, send mouse events to streamer',
settings && settings.hasOwnProperty(Flags.MouseInput) ?
settings[Flags.MouseInput] :
true,
useUrlParams
)
);
this.flags.set(
Flags.TouchInput,
new SettingFlag(
Flags.TouchInput,
'Touch input',
'If enabled, send touch events to streamer',
settings && settings.hasOwnProperty(Flags.TouchInput) ?
settings[Flags.TouchInput] :
true,
useUrlParams
)
);
this.flags.set(
Flags.GamepadInput,
new SettingFlag(
Flags.GamepadInput,
'Gamepad input',
'If enabled, send gamepad events to streamer',
settings && settings.hasOwnProperty(Flags.GamepadInput) ?
settings[Flags.GamepadInput] :
true,
useUrlParams
)
);
this.flags.set(
Flags.XRControllerInput,
new SettingFlag(
Flags.XRControllerInput,
'XR controller input',
'If enabled, send XR controller events to streamer',
settings && settings.hasOwnProperty(Flags.XRControllerInput) ?
settings[Flags.XRControllerInput] :
true,
useUrlParams
)
);
this.flags.set(
Flags.WaitForStreamer,
new SettingFlag(
Flags.WaitForStreamer,
'Wait for streamer',
'Will continue trying to connect to the first streamer available.',
settings && settings.hasOwnProperty(Flags.WaitForStreamer) ?
settings[Flags.WaitForStreamer] :
true,
useUrlParams
)
);
/**
* Numeric parameters
*/
this.numericParameters.set(
NumericParameters.AFKTimeoutSecs,
new SettingNumber(
NumericParameters.AFKTimeoutSecs,
'AFK timeout',
'The time (in seconds) it takes for the application to time out if AFK timeout is enabled.',
0 /*min*/,
600 /*max*/,
settings && settings.hasOwnProperty(NumericParameters.AFKTimeoutSecs) ?
settings[NumericParameters.AFKTimeoutSecs] :
120, /*value*/
useUrlParams
)
);
this.numericParameters.set(
NumericParameters.MaxReconnectAttempts,
new SettingNumber(
NumericParameters.MaxReconnectAttempts,
'Max Reconnects',
'Maximum number of reconnects the application will attempt when a streamer disconnects.',
0 /*min*/,
999 /*max*/,
settings && settings.hasOwnProperty(NumericParameters.MaxReconnectAttempts) ?
settings[NumericParameters.MaxReconnectAttempts] :
3, /*value*/
useUrlParams
)
);
this.numericParameters.set(
NumericParameters.MinQP,
new SettingNumber(
NumericParameters.MinQP,
'Min QP',
'The lower bound for the quantization parameter (QP) of the encoder. 0 = Best quality, 51 = worst quality.',
0 /*min*/,
51 /*max*/,
settings && settings.hasOwnProperty(NumericParameters.MinQP) ?
settings[NumericParameters.MinQP] :
0, /*value*/
useUrlParams
)
);
this.numericParameters.set(
NumericParameters.MaxQP,
new SettingNumber(
NumericParameters.MaxQP,
'Max QP',
'The upper bound for the quantization parameter (QP) of the encoder. 0 = Best quality, 51 = worst quality.',
0 /*min*/,
51 /*max*/,
settings && settings.hasOwnProperty(NumericParameters.MaxQP) ?
settings[NumericParameters.MaxQP] :
51, /*value*/
useUrlParams
)
);
this.numericParameters.set(
NumericParameters.WebRTCFPS,
new SettingNumber(
NumericParameters.WebRTCFPS,
'Max FPS',
'The maximum FPS that WebRTC will try to transmit frames at.',
1 /*min*/,
999 /*max*/,
settings && settings.hasOwnProperty(NumericParameters.WebRTCFPS) ?
settings[NumericParameters.WebRTCFPS] :
60, /*value*/
useUrlParams
)
);
this.numericParameters.set(
NumericParameters.WebRTCMinBitrate,
new SettingNumber(
NumericParameters.WebRTCMinBitrate,
'Min Bitrate (kbps)',
'The minimum bitrate that WebRTC should use.',
0 /*min*/,
500000 /*max*/,
settings && settings.hasOwnProperty(NumericParameters.WebRTCMinBitrate) ?
settings[NumericParameters.WebRTCMinBitrate] :
0, /*value*/
useUrlParams
)
);
this.numericParameters.set(
NumericParameters.WebRTCMaxBitrate,
new SettingNumber(
NumericParameters.WebRTCMaxBitrate,
'Max Bitrate (kbps)',
'The maximum bitrate that WebRTC should use.',
0 /*min*/,
500000 /*max*/,
settings && settings.hasOwnProperty(NumericParameters.WebRTCMaxBitrate) ?
settings[NumericParameters.WebRTCMaxBitrate] :
0, /*value*/
useUrlParams
)
);
this.numericParameters.set(
NumericParameters.StreamerAutoJoinInterval,
new SettingNumber(
NumericParameters.StreamerAutoJoinInterval,
'Streamer Auto Join Interval (ms)',
'Delay between retries when waiting for an available streamer.',
500 /*min*/,
900000 /*max*/,
settings && settings.hasOwnProperty(NumericParameters.StreamerAutoJoinInterval) ?
settings[NumericParameters.StreamerAutoJoinInterval] :
3000, /*value*/
useUrlParams
)
);
}
/**
* Add a callback to fire when the numeric setting is toggled.
* @param id The id of the flag.
* @param onChangedListener The callback to fire when the numeric value changes.
*/
_addOnNumericSettingChangedListener(
id: NumericParametersIds,
onChangedListener: (newValue: number) => void
): void {
if (this.numericParameters.has(id)) {
this.numericParameters
.get(id)
.addOnChangedListener(onChangedListener);
}
}
_addOnOptionSettingChangedListener(
id: OptionParametersIds,
onChangedListener: (newValue: string) => void
): void {
if (this.optionParameters.has(id)) {
this.optionParameters
.get(id)
.addOnChangedListener(onChangedListener);
}
}
/**
* @param id The id of the numeric setting we are interested in getting a value for.
* @returns The numeric value stored in the parameter with the passed id.
*/
getNumericSettingValue(id: NumericParametersIds): number {
if (this.numericParameters.has(id)) {
return this.numericParameters.get(id).number;
} else {
throw new Error(`There is no numeric setting with the id of ${id}`);
}
}
/**
* @param id The id of the text setting we are interested in getting a value for.
* @returns The text value stored in the parameter with the passed id.
*/
getTextSettingValue(id: TextParametersIds): string {
if (this.textParameters.has(id)) {
return this.textParameters.get(id).value as string;
} else {
throw new Error(`There is no numeric setting with the id of ${id}`);
}
}
/**
* Set number in the setting.
* @param id The id of the numeric setting we are interested in.
* @param value The numeric value to set.
*/
setNumericSetting(id: NumericParametersIds, value: number): void {
if (this.numericParameters.has(id)) {
this.numericParameters.get(id).number = value;
} else {
throw new Error(`There is no numeric setting with the id of ${id}`);
}
}
/**
* Add a callback to fire when the flag is toggled.
* @param id The id of the flag.
* @param onChangeListener The callback to fire when the value changes.
*/
_addOnSettingChangedListener(
id: FlagsIds,
onChangeListener: (newFlagValue: boolean) => void
): void {
if (this.flags.has(id)) {
this.flags.get(id).onChange = onChangeListener;
}
}
/**
* Add a callback to fire when the text is changed.
* @param id The id of the flag.
* @param onChangeListener The callback to fire when the value changes.
*/
_addOnTextSettingChangedListener(
id: TextParametersIds,
onChangeListener: (newTextValue: string) => void
): void {
if (this.textParameters.has(id)) {
this.textParameters.get(id).onChange = onChangeListener;
}
}
/**
* Get the option which has the given id.
* @param id The id of the option.
* @returns The SettingOption object matching id
*/
getSettingOption(id: OptionParametersIds): SettingOption {
return this.optionParameters.get(id);
}
/**
* Get the value of the configuration flag which has the given id.
* @param id The unique id for the flag.
* @returns True if the flag is enabled.
*/
isFlagEnabled(id: FlagsIds): boolean {
return this.flags.get(id).flag as boolean;
}
/**
* Set flag to be enabled/disabled.
* @param id The id of the flag to toggle.
* @param flagEnabled True if the flag should be enabled.
*/
setFlagEnabled(id: FlagsIds, flagEnabled: boolean) {
if (!this.flags.has(id)) {
Logger.Warning(
Logger.GetStackTrace(),
`Cannot toggle flag called ${id} - it does not exist in the Config.flags map.`
);
} else {
this.flags.get(id).flag = flagEnabled;
}
}
/**
* Set the text setting.
* @param id The id of the setting
* @param settingValue The value to set in the setting.
*/
setTextSetting(id: TextParametersIds, settingValue: string) {
if (!this.textParameters.has(id)) {
Logger.Warning(
Logger.GetStackTrace(),
`Cannot set text setting called ${id} - it does not exist in the Config.textParameters map.`
);
} else {
this.textParameters.get(id).text = settingValue;
}
}
/**
* Set the option setting list of options.
* @param id The id of the setting
* @param settingOptions The values the setting could take
*/
setOptionSettingOptions(
id: OptionParametersIds,
settingOptions: Array<string>
) {
if (!this.optionParameters.has(id)) {
Logger.Warning(
Logger.GetStackTrace(),
`Cannot set text setting called ${id} - it does not exist in the Config.optionParameters map.`
);
} else {
this.optionParameters.get(id).options = settingOptions;
}
}
/**
* Set option enum settings selected option.
* @param id The id of the setting
* @param settingOptions The value to select out of all the options
*/
setOptionSettingValue(id: OptionParametersIds, settingValue: string) {
if (!this.optionParameters.has(id)) {
Logger.Warning(
Logger.GetStackTrace(),
`Cannot set text setting called ${id} - it does not exist in the Config.enumParameters map.`
);
} else {
const optionSetting = this.optionParameters.get(id);
const existingOptions = optionSetting.options;
if (!existingOptions.includes(settingValue)) {
existingOptions.push(settingValue);
optionSetting.options = existingOptions;
}
optionSetting.selected = settingValue;
}
}
/**
* Set the label for the flag.
* @param id The id of the flag.
* @param label The new label to use for the flag.
*/
setFlagLabel(id: FlagsIds, label: string) {
if (!this.flags.has(id)) {
Logger.Warning(
Logger.GetStackTrace(),
`Cannot set label for flag called ${id} - it does not exist in the Config.flags map.`
);
} else {
this.flags.get(id).label = label;
}
}
/**
* Set a subset of all settings in one function call.
*
* @param settings A (partial) list of settings to set
*/
setSettings(settings: Partial<AllSettings>) {
for (const key of Object.keys(settings)) {
if (isFlagId(key)) {
this.setFlagEnabled(key, settings[key]);
} else if (isNumericId(key)) {
this.setNumericSetting(key, settings[key]);
} else if (isTextId(key)) {
this.setTextSetting(key, settings[key]);
} else if (isOptionId(key)) {
this.setOptionSettingValue(key, settings[key]);
}
}
}
/**
* Get all settings
* @returns All setting values as an object with setting ids as keys
*/
getSettings(): Partial<AllSettings> {
const settings: Partial<AllSettings> = {};
for (const [key, value] of this.flags.entries()) {
settings[key] = value.flag;
}
for (const [key, value] of this.numericParameters.entries()) {
settings[key] = value.number;
}
for (const [key, value] of this.textParameters.entries()) {
settings[key] = value.text;
}
for (const [key, value] of this.optionParameters.entries()) {
settings[key] = value.selected;
}
return settings;
}
/**
* Get all Flag settings as an array.
* @returns All SettingFlag objects
*/
getFlags(): Array<SettingFlag> {
return Array.from(this.flags.values());
}
/**
* Get all Text settings as an array.
* @returns All SettingText objects
*/
getTextSettings(): Array<SettingText> {
return Array.from(this.textParameters.values());
}
/**
* Get all Number settings as an array.
* @returns All SettingNumber objects
*/
getNumericSettings(): Array<SettingNumber> {
return Array.from(this.numericParameters.values());
}
/**
* Get all Option settings as an array.
* @returns All SettingOption objects
*/
getOptionSettings(): Array<SettingOption> {
return Array.from(this.optionParameters.values());
}
/**
* Emit events when settings change.
* @param eventEmitter
*/
_registerOnChangeEvents(eventEmitter: EventEmitter) {
for (const key of this.flags.keys()) {
const flag = this.flags.get(key);
if (flag) {
flag.onChangeEmit = (newValue: boolean) =>
eventEmitter.dispatchEvent(
new SettingsChangedEvent({
id: flag.id,
type: 'flag',
value: newValue,
target: flag
})
);
}
}
for (const key of this.numericParameters.keys()) {
const number = this.numericParameters.get(key);
if (number) {
number.onChangeEmit = (newValue: number) =>
eventEmitter.dispatchEvent(
new SettingsChangedEvent({
id: number.id,
type: 'number',
value: newValue,
target: number
})
);
}
}
for (const key of this.textParameters.keys()) {
const text = this.textParameters.get(key);
if (text) {
text.onChangeEmit = (newValue: string) =>
eventEmitter.dispatchEvent(
new SettingsChangedEvent({
id: text.id,
type: 'text',
value: newValue,
target: text
})
);
}
}
for (const key of this.optionParameters.keys()) {
const option = this.optionParameters.get(key);
if (option) {
option.onChangeEmit = (newValue: string) =>
eventEmitter.dispatchEvent(
new SettingsChangedEvent({
id: option.id,
type: 'option',
value: newValue,
target: option
})
);
}
}
}
}
/**
* The enum associated with the mouse being locked or hovering
*/
export enum ControlSchemeType {
LockedMouse = 0,
HoveringMouse = 1
}