-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathDreamInterfaceManager.cs
More file actions
1107 lines (932 loc) · 46.2 KB
/
DreamInterfaceManager.cs
File metadata and controls
1107 lines (932 loc) · 46.2 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
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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using OpenDreamClient.Interface.Controls;
using OpenDreamClient.Interface.Prompts;
using OpenDreamClient.Resources;
using OpenDreamClient.Resources.ResourceTypes;
using OpenDreamShared.Dream;
using OpenDreamShared.Interface.Descriptors;
using OpenDreamShared.Interface.DMF;
using OpenDreamShared.Network.Messages;
using Robust.Client;
using Robust.Client.Graphics;
using Robust.Client.Input;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.ContentPack;
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Random;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
using SixLabors.ImageSharp;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
namespace OpenDreamClient.Interface;
internal sealed class DreamInterfaceManager : IDreamInterfaceManager {
private static readonly ResPath DefaultInterfaceFile = new("/OpenDream/DefaultInterface.dmf");
[Dependency] private readonly IClyde _clyde = default!;
[Dependency] private readonly IBaseClient _client = default!;
[Dependency] private readonly IEyeManager _eyeManager = default!;
[Dependency] private readonly IClientNetManager _netManager = default!;
[Dependency] private readonly IDreamResourceManager _dreamResource = default!;
[Dependency] private readonly IResourceManager _resourceManager = default!;
[Dependency] private readonly IFileDialogManager _fileDialogManager = default!;
[Dependency] private readonly ISerializationManager _serializationManager = default!;
[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
[Dependency] private readonly IInputManager _inputManager = default!;
[Dependency] private readonly IUserInterfaceManager _uiManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly ITimerManager _timerManager = default!;
[Dependency] private readonly IUriOpener _uriOpener = default!;
[Dependency] private readonly IGameController _gameController = default!;
private readonly ISawmill _sawmill = Logger.GetSawmill("opendream.interface");
public InterfaceDescriptor InterfaceDescriptor { get; private set; }
public ControlWindow? DefaultWindow { get; private set; }
public ControlOutput? DefaultOutput { get; private set; }
public ControlInfo? DefaultInfo { get; private set; }
public ControlMap? DefaultMap { get; private set; }
public Dictionary<string, ControlWindow> Windows { get; } = new();
public Dictionary<string, InterfaceMenu> Menus { get; } = new();
public Dictionary<string, InterfaceMacroSet> MacroSets { get; } = new();
private Dictionary<WindowId, ControlWindow> ClydeWindowIdToControl { get; } = new();
public CursorHolder Cursors { get; private set; } = default!;
public ViewRange View {
get => _view;
private set {
// Cap to a view range of 45x45
// Any larger causes crashes with RobustToolbox's GetPixel()
if (value.Width > 45 || value.Height > 45)
value = new(Math.Min(value.Width, 45), Math.Min(value.Height, 45));
_view = value;
DefaultMap?.UpdateViewRange(_view);
}
}
public bool ShowPopupMenus { get; private set; } = true;
public int IconSize { get; private set; }
private ViewRange _view = new(5);
public void LoadInterfaceFromSource(string source) {
Reset();
DMFLexer dmfLexer = new DMFLexer(source);
DMFParser dmfParser = new DMFParser(dmfLexer, _serializationManager);
InterfaceDescriptor interfaceDescriptor = dmfParser.Interface();
if (dmfParser.Errors.Count > 0) {
foreach (string error in dmfParser.Errors) {
_sawmill.Error(error);
}
// Open an error message that disconnects from the server once closed
OpenAlert(
"Error",
"Encountered error(s) while parsing interface source.\nCheck the console for details.",
"Ok", null, null,
(_, _) => _client.DisconnectFromServer("Errors while parsing interface"));
return;
}
LoadInterface(interfaceDescriptor);
}
private void SetupBaseDreamBinds() {
// Set up the middle-mouse button keybind
_inputManager.Contexts.GetContext("common").AddFunction(OpenDreamKeyFunctions.MouseMiddle);
_inputManager.RegisterBinding(new KeyBindingRegistration() {
Function = OpenDreamKeyFunctions.MouseMiddle,
BaseKey = Keyboard.Key.MouseMiddle
});
}
public void Initialize() {
SetupBaseDreamBinds();
Cursors = new(_clyde);
_netManager.RegisterNetMessage<MsgUpdateStatPanels>(RxUpdateStatPanels);
_netManager.RegisterNetMessage<MsgSelectStatPanel>(RxSelectStatPanel);
_netManager.RegisterNetMessage<MsgOutput>(RxOutput);
_netManager.RegisterNetMessage<MsgAlert>(RxAlert);
_netManager.RegisterNetMessage<MsgPrompt>(RxPrompt);
_netManager.RegisterNetMessage<MsgPromptList>(RxPromptList);
_netManager.RegisterNetMessage<MsgPromptResponse>();
_netManager.RegisterNetMessage<MsgBrowse>(RxBrowse);
_netManager.RegisterNetMessage<MsgTopic>();
_netManager.RegisterNetMessage<MsgWinSet>(RxWinSet);
_netManager.RegisterNetMessage<MsgWinClone>(RxWinClone);
_netManager.RegisterNetMessage<MsgWinExists>(RxWinExists);
_netManager.RegisterNetMessage<MsgWinGet>(RxWinGet);
_netManager.RegisterNetMessage<MsgLink>(RxLink);
_netManager.RegisterNetMessage<MsgFtp>(RxFtp);
_netManager.RegisterNetMessage<MsgLoadInterface>(RxLoadInterface);
_netManager.RegisterNetMessage<MsgAckLoadInterface>();
_netManager.RegisterNetMessage<MsgUpdateClientInfo>(RxUpdateClientInfo);
_netManager.RegisterNetMessage<MsgNotifyMobEyeUpdate>(RxNotifyMobEyeUpdate);
_clyde.OnWindowFocused += OnWindowFocused;
}
private void RxNotifyMobEyeUpdate(MsgNotifyMobEyeUpdate message) {
_entitySystemManager.GetEntitySystem<DreamClientSystem>().RxNotifyMobEyeUpdate(message);
}
private void RxUpdateStatPanels(MsgUpdateStatPanels message) {
DefaultInfo?.UpdateStatPanels(message);
}
private void RxSelectStatPanel(MsgSelectStatPanel message) {
DefaultInfo?.SelectStatPanel(message.StatPanel);
}
private void RxOutput(MsgOutput pOutput) {
Output(pOutput.Control, pOutput.Value);
}
private void RxAlert(MsgAlert message) {
OpenAlert(
message.Title,
message.Message,
message.Button1, message.Button2, message.Button3,
(responseType, response) => OnPromptFinished(message.PromptId, responseType, response));
}
public void OpenAlert(string title, string message, string button1, string? button2, string? button3, Action<DreamValueType, object?>? onClose) {
var alert = new AlertWindow(
title,
message,
button1, button2, button3,
onClose);
alert.Owner = _clyde.MainWindow;
alert.Show();
}
private void RxPrompt(MsgPrompt pPrompt) {
void OnPromptClose(DreamValueType responseType, object? response) {
OnPromptFinished(pPrompt.PromptId, responseType, response);
}
Prompt(pPrompt.Types, pPrompt.Title, pPrompt.Message, pPrompt.DefaultValue, OnPromptClose);
}
private void RxPromptList(MsgPromptList pPromptList) {
var prompt = new ListPrompt(
pPromptList.Title,
pPromptList.Message,
pPromptList.DefaultValue,
pPromptList.CanCancel,
pPromptList.Values,
(responseType, response) => OnPromptFinished(pPromptList.PromptId, responseType, response)
);
ShowPrompt(prompt);
}
private void RxBrowse(MsgBrowse pBrowse) {
var referencedElement = (pBrowse.Window != null) ? FindElementWithId(pBrowse.Window) : DefaultWindow;
if (pBrowse.HtmlSource == null && referencedElement != null) {
// Closing the referenced window or browser
if (referencedElement is ControlWindow window) {
window.CloseChildWindow();
} else if (referencedElement is ControlBrowser browser) {
// TODO: What does "closing" the browser mean? Redirect to a blank page or remove the control entirely?
browser.SetFileSource(null);
}
} else if (pBrowse.HtmlSource != null) {
var htmlFileName = $"browse{_random.Next()}"; // TODO: Possible collisions and explicit file names
ControlBrowser? outputBrowser = referencedElement as ControlBrowser;
if (outputBrowser == null) {
if (referencedElement is ControlWindow window) {
outputBrowser = null;
// Find a browser within this window
foreach (var childControl in window.ChildControls) {
if (childControl is not ControlBrowser browser)
continue;
outputBrowser = browser;
break;
}
} else if (pBrowse.Window != null) {
// Creating a new popup
var popup = new BrowsePopup(pBrowse.Window, pBrowse.Size, _clyde.MainWindow);
popup.Closed += () => { Windows.Remove(pBrowse.Window); };
outputBrowser = popup.Browser;
Windows.Add(pBrowse.Window, popup.WindowElement);
popup.Open();
}
}
if (outputBrowser == null) {
_sawmill.Error($"Failed to find a browser element in window \"{pBrowse.Window}\" to browse()");
return;
}
var cacheFile = _dreamResource.CreateCacheFile(htmlFileName + ".html", pBrowse.HtmlSource);
outputBrowser.SetFileSource(cacheFile);
}
}
private void RxWinSet(MsgWinSet message) {
WinSet(message.ControlId, message.Params);
}
private void RxWinClone(MsgWinClone message) {
WinClone(message.ControlId, message.CloneId);
}
private void RxWinExists(MsgWinExists message) {
InterfaceElement? element = FindElementWithId(message.ControlId);
MsgPromptResponse response = new() {
PromptId = message.PromptId,
Type = DreamValueType.Text,
Value = element?.Type.Value ?? string.Empty
};
_netManager.ClientSendMessage(response);
}
private void RxWinGet(MsgWinGet message) {
// Run this later to ensure any pending UI measurements have occured
_timerManager.AddTimer(new Timer(100, false, () => {
MsgPromptResponse response = new() {
PromptId = message.PromptId,
Type = DreamValueType.Text,
Value = WinGet(message.ControlId, message.QueryValue, forceSnowflake:true)
};
_netManager.ClientSendMessage(response);
}));
}
private void RxLink(MsgLink message) {
Uri uri;
try {
uri = new Uri(message.Url);
} catch (Exception e) {
_sawmill.Error($"Received link \"{message.Url}\" which failed to parse as a valid URI: {e.Message}");
return;
}
// TODO: This can be a topic call
if (uri.Scheme is "http" or "https") {
_uriOpener.OpenUri(message.Url);
} else if (uri.Scheme is "ss14" or "ss14s") {
if (_gameController.LaunchState.FromLauncher)
_gameController.Redial(message.Url, "link() used to connect to another server.");
else
_sawmill.Warning("link() only supports connecting to other servers when utilizing the launcher. Ignoring.");
} else {
_sawmill.Warning($"Received link \"{message.Url}\" which is not supported. Ignoring.");
}
}
private void RxFtp(MsgFtp message) {
_dreamResource.LoadResourceAsync<DreamResource>(message.ResourceId, async resource => {
// TODO: Default the filename to message.SuggestedName
// RT doesn't seem to support this currently
var tuple = await _fileDialogManager.SaveFile();
if (tuple == null) // User cancelled
return;
await using var file = tuple.Value.fileStream;
resource.WriteTo(file);
});
}
private void RxLoadInterface(MsgLoadInterface message) {
string? interfaceText = message.InterfaceText;
if (interfaceText == null) {
if (!_resourceManager.TryContentFileRead(DefaultInterfaceFile.CanonPath, out var defaultInterface)) {
// Open an error message that disconnects from the server once closed
OpenAlert(
"Error",
"The server did not provide an interface and there is no default interface in the resources folder.",
"Ok", null, null,
(_, _) => _client.DisconnectFromServer("No interface to use"));
return;
}
using var defaultInterfaceReader = new StreamReader(defaultInterface);
interfaceText = defaultInterfaceReader.ReadToEnd();
}
LoadInterfaceFromSource(interfaceText);
_netManager.ClientSendMessage(new MsgAckLoadInterface());
}
private void RxUpdateClientInfo(MsgUpdateClientInfo msg) {
IconSize = msg.IconSize;
View = msg.View;
ShowPopupMenus = msg.ShowPopupMenus;
if (msg.CursorResource != 0)
_dreamResource.LoadResourceAsync<DMIResource>(msg.CursorResource, resource => {
//TODO should trigger a cursor update immediately
Cursors = new(_clyde, resource);
});
else {
Cursors = new(_clyde); //reset to default
}
}
private void ShowPrompt(PromptWindow prompt) {
prompt.Owner = _clyde.MainWindow;
prompt.Show();
}
public void FrameUpdate(FrameEventArgs frameEventArgs) {
if (DefaultMap != null)
DefaultMap.Viewport.Eye = _eyeManager.CurrentEye;
}
public InterfaceElement? FindElementWithId(string id) {
string[] split = id.Split(".");
if (split.Length == 2) {
string windowId = split[0];
string elementId = split[1];
ControlWindow? window = null;
if (Windows.ContainsKey(windowId)) {
window = Windows[windowId];
} else if (Menus.TryGetValue(windowId, out var menu)) {
if (menu.MenuElementsById.TryGetValue(elementId, out var menuElement))
return menuElement;
} else if(MacroSets.TryGetValue(windowId, out var macroSet)) {
if (macroSet.Macros.TryGetValue(elementId, out var macroElement))
return macroElement;
}
if (window != null) {
foreach (InterfaceControl element in window.ChildControls) {
if (element.Id.Value == elementId) return element;
}
}
} else {
string elementId = split[0];
// ":[element]" returns the default element of that type
switch (elementId) {
case ":map":
return DefaultMap;
case ":info":
return DefaultInfo;
case ":window":
return DefaultWindow;
case ":output":
return DefaultOutput;
}
foreach (ControlWindow window in Windows.Values) {
if (window.Id.Value == elementId)
return window;
foreach (InterfaceControl element in window.ChildControls) {
if (element.Id.Value == elementId) return element;
}
}
foreach (InterfaceMenu menu in Menus.Values) {
if (menu.Id.Value == elementId)
return menu;
if (menu.MenuElementsById.TryGetValue(elementId, out var menuElement))
return menuElement;
}
foreach (var macroSet in MacroSets.Values) {
if (macroSet.Id.Value == elementId)
return macroSet;
if (macroSet.Macros.TryGetValue(elementId, out var macroElement))
return macroElement;
}
}
return null;
}
public void SaveScreenshot(bool openDialog) {
// ReSharper disable once AsyncVoidLambda
DefaultMap?.Viewport.Screenshot(async img => {
//TODO: Support automatically choosing a location if openDialog == false
var filters = new FileDialogFilters(new FileDialogFilters.Group("png"));
var tuple = await _fileDialogManager.SaveFile(filters);
if (tuple == null)
return;
await using var file = tuple.Value.fileStream;
await img.SaveAsPngAsync(file);
});
}
public void Prompt(DreamValueType types, string title, string message, string defaultValue, Action<DreamValueType, object?>? onClose) {
PromptWindow? prompt = null;
bool canCancel = (types & DreamValueType.Null) == DreamValueType.Null;
if ((types & DreamValueType.Text) == DreamValueType.Text) {
prompt = new TextPrompt(title, message, defaultValue, canCancel, onClose);
} else if ((types & DreamValueType.Num) == DreamValueType.Num) {
prompt = new NumberPrompt(title, message, defaultValue, canCancel, onClose);
} else if ((types & DreamValueType.Message) == DreamValueType.Message) {
prompt = new MessagePrompt(title, message, defaultValue, canCancel, onClose);
} else if ((types & DreamValueType.Color) == DreamValueType.Color) {
prompt = new ColorPrompt(title, message, defaultValue, canCancel, onClose);
}
if (prompt != null) {
ShowPrompt(prompt);
}
}
public void RunCommand(string fullCommand, bool repeating = false) {
switch (fullCommand) {
case not null when fullCommand.StartsWith(".quit"):
_gameController.Shutdown(".quit used");
break;
case not null when fullCommand.StartsWith(".screenshot"):
string[] split = fullCommand.Split(" ");
SaveScreenshot(split.Length == 1 || split[1] != "auto");
break;
case not null when fullCommand.StartsWith(".configure"):
_sawmill.Warning(".configure command is not implemented");
break;
case not null when fullCommand.StartsWith(".winset"):
// Everything after .winset, excluding the space and quotes
string winsetParams = fullCommand.Substring(7); //clip .winset
winsetParams = winsetParams.Trim(); //clip space
if (winsetParams.StartsWith('"') && winsetParams.EndsWith('"'))
winsetParams = winsetParams.Substring(1, winsetParams.Length - 2); //clip quotes
WinSet(null, winsetParams);
break;
case not null when fullCommand.StartsWith(".output"): {
string[] args = fullCommand.Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (args.Length != 3) {
_sawmill.Error($".output command was executed with {args.Length - 1} args instead of 2");
break;
}
Output(args[1], args[2]);
break;
}
default: {
string[] argsRaw = fullCommand!.Split(' ', 2, StringSplitOptions.TrimEntries);
string command = argsRaw[0].ToLowerInvariant(); // Case-insensitive
if (!_entitySystemManager.TryGetEntitySystem(out ClientVerbSystem? verbSystem))
return;
var ret = verbSystem.FindVerbWithCommandName(command);
if (ret is not var (verbId, verbSrc, verbInfo))
return;
if (argsRaw.Length == 1) { // No args given; Let the verb system handle the possible prompting
if (repeating) {
verbSystem.StartRepeatingVerb(verbSrc, verbId);
} else {
verbSystem.ExecuteVerb(verbSrc, verbId);
}
} else { // Attempt to parse the given arguments
List<string> args = new List<string>();
StringBuilder currentArg = new();
bool stringCapture = false;
for (int i = 0; i < argsRaw[1].Length; i++) {
if (argsRaw[1][i] == '"') {
currentArg.Append('"');
if (stringCapture) {
var result = HandleEmbeddedWinget(null, currentArg.ToString(), out var hadWinget);
// 64x64 or 64,64 gets split into two "64 64" args
if (hadWinget && result.Split(['x', ',']) is {Length: 2} wingetSplit &&
float.TryParse(wingetSplit[0], out _) && float.TryParse(wingetSplit[1], out _)) {
args.Add(wingetSplit[0]);
args.Add(wingetSplit[1]);
} else {
args.Add(result);
}
currentArg.Clear();
}
stringCapture = !stringCapture;
continue;
}
if (argsRaw[1][i] == ' ' && !stringCapture) {
var result = HandleEmbeddedWinget(null, currentArg.ToString(), out var hadWinget);
// 64x64 or 64,64 gets split into two "64 64" args
if (hadWinget && result.Split(['x', ',']) is {Length: 2} wingetSplit &&
float.TryParse(wingetSplit[0], out _) && float.TryParse(wingetSplit[1], out _)) {
args.Add(wingetSplit[0]);
args.Add(wingetSplit[1]);
} else {
args.Add(result);
}
currentArg.Clear();
continue;
}
currentArg.Append(argsRaw[1][i]);
}
if (currentArg.ToString() is { } arg && !string.IsNullOrEmpty(arg)) {
var result = HandleEmbeddedWinget(null, arg, out var hadWinget);
// 64x64 or 64,64 gets split into two "64 64" args
if (hadWinget && result.Split(['x', ',']) is {Length: 2} wingetSplit &&
float.TryParse(wingetSplit[0], out _) && float.TryParse(wingetSplit[1], out _)) {
args.Add(wingetSplit[0]);
args.Add(wingetSplit[1]);
} else {
args.Add(result);
}
}
if (args.Count != verbInfo.Arguments.Length) {
_sawmill.Error(
$"Attempted to call a verb with {verbInfo.Arguments.Length} argument(s) with only {args.Count}: {fullCommand}");
return;
}
var arguments = new object?[verbInfo.Arguments.Length];
for (int i = 0; i < verbInfo.Arguments.Length; i++) {
DreamValueType argumentType = verbInfo.Arguments[i].Types;
if (argumentType is DreamValueType.Text or DreamValueType.Message or DreamValueType.CommandText) {
arguments[i] = args[i];
} else if (argumentType == DreamValueType.Num) {
if (!float.TryParse(args[i], out var numArg)) {
_sawmill.Error(
$"Invalid number argument \"{args[i]}\"; ignoring command ({fullCommand})");
return;
}
arguments[i] = numArg;
} else {
_sawmill.Error($"Parsing verb args of type {argumentType} is unimplemented; ignoring command ({fullCommand})");
return;
}
}
verbSystem.ExecuteVerb(verbSrc, verbId, arguments);
}
break;
}
}
}
public void StopRepeatingCommand(string command) {
string[] argsRaw = command.Split(' ', 2, StringSplitOptions.TrimEntries);
string parsedCommand = argsRaw[0].ToLowerInvariant(); // Case-insensitive
if (!_entitySystemManager.TryGetEntitySystem(out ClientVerbSystem? verbSystem))
return;
var ret = verbSystem.FindVerbWithCommandName(parsedCommand);
if (ret is not var (verbId, verbSrc, _))
return;
verbSystem.StopRepeatingVerb(verbSrc, verbId);
}
public string HandleEmbeddedWinget(string? controlId, string value, out bool hadWinget) {
hadWinget = false;
string result = value;
int startPos = result.IndexOf("[[", StringComparison.Ordinal);
while(startPos > -1){
int endPos = result.IndexOf("]]", startPos, StringComparison.Ordinal);
if(endPos == -1)
break;
string inner = result.Substring(startPos+2, endPos-startPos-2);
string[] elementSplit = inner.Split('.');
string innerControlId = controlId ?? "";
if(elementSplit.Length > 1){
innerControlId = (string.IsNullOrEmpty(innerControlId) ? "" : innerControlId+".")+string.Join(".", elementSplit[..^1]);
inner = elementSplit[^1];
}
string innerResult = WinGet(innerControlId, inner);
hadWinget = true;
result = result.Substring(0, startPos) + innerResult + result.Substring(endPos+2);
startPos = result.IndexOf("[[", StringComparison.Ordinal);
}
return result;
}
public void WinSet(string? controlId, string winsetParams) {
DMFParser parser;
try{
var lexer = new DMFLexer(winsetParams);
parser = new DMFParser(lexer, _serializationManager);
} catch (Exception e) {
_sawmill.Error($"Error parsing winset: {e}");
return;
}
bool CheckParserErrors() {
if (parser.Errors.Count <= 0)
return false;
foreach (string error in parser.Errors) {
_sawmill.Error(error);
}
return true;
}
if (string.IsNullOrEmpty(controlId)) {
List<DMFWinSet> winSets = parser.GlobalWinSet();
if (CheckParserErrors())
return;
// id=abc overrides the elements of other winsets without an element
string? elementOverride = winSets.FirstOrDefault(winSet => winSet.Element == null && winSet.Attribute == "id")?.Value;
foreach (DMFWinSet winSet in winSets) {
if (winSet.Attribute == "id") // This is used to set the target, not an actual winset
continue;
string? elementId = winSet.Element ?? elementOverride;
if (elementId == null) {
if (winSet.Attribute == "command") {
RunCommand(HandleEmbeddedWinget(controlId, winSet.Value, out _));
} else {
_sawmill.Error($"Invalid global winset \"{winsetParams}\"");
}
} else {
if(winSet.TrueStatements is not null) {
InterfaceElement? conditionalElement = FindElementWithId(elementId);
if(conditionalElement is null)
_sawmill.Error($"Invalid element on ternary condition \"{elementId}\"");
else
if(conditionalElement.TryGetProperty(winSet.Attribute, out var conditionalCheckValue) && conditionalCheckValue.Equals(winSet.Value)) {
foreach(DMFWinSet statement in winSet.TrueStatements) {
string statementElementId = statement.Element ?? elementId;
InterfaceElement? statementElement = FindElementWithId(statementElementId);
if(statementElement is not null) {
statementElement.SetProperty(statement.Attribute, HandleEmbeddedWinget(statementElementId, statement.Value, out _), manualWinset: true);
} else {
_sawmill.Error($"Invalid element on ternary \"{statementElementId}\"");
}
}
} else if (winSet.FalseStatements is not null){
foreach(DMFWinSet statement in winSet.FalseStatements) {
string statementElementId = statement.Element ?? elementId;
InterfaceElement? statementElement = FindElementWithId(statementElementId);
if(statementElement is not null) {
statementElement.SetProperty(statement.Attribute, HandleEmbeddedWinget(statementElementId, statement.Value, out _), manualWinset: true);
} else {
_sawmill.Error($"Invalid element on ternary \"{statementElementId}\"");
}
}
}
} else {
InterfaceElement? element = FindElementWithId(elementId);
if (element != null) {
element.SetProperty(winSet.Attribute, HandleEmbeddedWinget(elementId, winSet.Value, out _), manualWinset: true);
} else {
_sawmill.Error($"Invalid element \"{elementId}\"");
}
}
}
}
} else {
InterfaceElement? element = FindElementWithId(controlId);
var attributes = parser.AttributesValues();
if (CheckParserErrors())
return;
if (element == null && attributes.TryGetValue("parent", out var parentId)) {
var parent = FindElementWithId(parentId);
if (parent == null) {
_sawmill.Error($"Attempted to create an element with nonexistent parent \"{parentId}\" ({winsetParams})");
return;
}
attributes["id"] = controlId;
var childDescriptor = parent.ElementDescriptor.CreateChildDescriptor(_serializationManager, attributes);
if (childDescriptor == null)
return;
parent.AddChild(childDescriptor);
} else if (element != null) {
foreach (var attribute in attributes) {
element.SetProperty(attribute.Key, attribute.Value, manualWinset: true);
}
} else {
_sawmill.Error($"Invalid element \"{controlId}\"");
}
}
}
public string WinGet(string controlId, string queryValue, bool forceJson = false, bool forceSnowflake = false) {
bool ParseAndTryGet(InterfaceElement element, string query, out string result) {
//parse "as blah" from query if it's there
string[] querySplit = query.Split(" as ");
IDMFProperty propResult;
if(querySplit.Length != 2) //must be "thing as blah" or "thing". Anything else is invalid.
if(element.TryGetProperty(query, out propResult!)){
result = forceJson ? propResult.AsJson() : forceSnowflake ? propResult.AsSnowflake() : propResult.AsRaw();
return true;
} else {
result = "";
return false;
}
else{
if(!element.TryGetProperty(querySplit[0], out propResult!)) {
result = "";
return false;
}
if (forceJson) {
result = propResult.AsJson();
return true;
} else if (forceSnowflake) {
result = propResult.AsSnowflake();
return true;
}
switch(querySplit[1]){
case "arg":
result = propResult.AsArg();
break;
case "escaped":
result = propResult.AsEscaped();
break;
case "string":
result = propResult.AsString();
break;
case "params":
result = propResult.AsParams();
break;
case "json":
result = propResult.AsJson();
break;
case "json-dm":
result = propResult.AsJsonDM();
break;
case "raw":
result = propResult.AsRaw();
break;
default:
_sawmill.Error($"Invalid winget query function \"{querySplit[1]}\" in \"{query}\"");
result = "";
return false;
}
return true;
}
}
string GetProperty(string elementId) {
var element = FindElementWithId(elementId);
if (element == null) {
_sawmill.Error($"Could not winget element {elementId} because it does not exist");
return string.Empty;
}
var multiQuery = queryValue.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
if(multiQuery.Length > 1) {
var result = "";
foreach(var query in multiQuery) {
if (!ParseAndTryGet(element, query, out var queryResult))
_sawmill.Error($"Could not winget property {query} on {element.Id}");
result += query+"="+queryResult + ";";
}
return result.TrimEnd(';');
} else if (ParseAndTryGet(element, queryValue, out var value))
return value;
_sawmill.Error($"Could not winget property {queryValue} on {element.Id}");
return string.Empty;
}
var elementIds = controlId.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
if (elementIds.Length == 0) {
switch (queryValue) {
// The server will actually never query this because it can predict the answer to be "true"
// But also have it here in case a local winget ever wants it
case "hwmode":
return "true";
case "windows":
return string.Join(';',
Windows.Where(pair => !((WindowDescriptor)pair.Value.ElementDescriptor).IsPane.Value).Select(pair => pair.Key));
case "panes":
return string.Join(';',
Windows.Where(pair => ((WindowDescriptor)pair.Value.ElementDescriptor).IsPane.Value).Select(pair => pair.Key));
case "menus":
return string.Join(';', Menus.Keys);
case "macros":
return string.Join(';', MacroSets.Keys);
case "url":
return _netManager.ServerChannel?.RemoteEndPoint.ToString() ?? string.Empty; // TODO: Port should be 0 "if connected to a local .dmb file"
case "dpi":
return (_clyde.DefaultWindowScale.X.ToString(CultureInfo.InvariantCulture));
default:
_sawmill.Error($"Special winget \"{queryValue}\" is not implemented");
return string.Empty;
}
} else if (elementIds.Length == 1) {
return GetProperty(elementIds[0]);
}
var result = new StringBuilder(elementIds.Length * 6 - 1);
for (int i = 0; i < elementIds.Length; i++) {
var elementId = elementIds[i];
result.Append(elementId);
result.Append('.');
result.Append(queryValue);
result.Append('=');
result.Append(GetProperty(elementId));
if (i != elementIds.Length - 1)
result.Append(';');
}
return result.ToString();
}
public void Output(string? control, string value) {
InterfaceControl? interfaceElement;
string? data = null;
if (control != null) {
string[] split = control.Split(":");
interfaceElement = (InterfaceControl?)FindElementWithId(split[0]);
if (split.Length > 1) data = split[1];
} else {
interfaceElement = DefaultOutput;
}
interfaceElement?.Output(value, data);
}
public void WinClone(string controlId, string cloneId) {
ElementDescriptor? elementDescriptor = InterfaceDescriptor.GetElementDescriptor(controlId);
elementDescriptor = elementDescriptor?.CreateCopy(_serializationManager, cloneId);
// If window_name is "window", "pane", "menu", or "macro", and the skin file does not have a control of
// that name already, we will create a new control of that type from scratch.
if (elementDescriptor == null) {
switch (controlId) {
case "window":
elementDescriptor = new WindowDescriptor(cloneId);
break;
case "menu":
elementDescriptor = new MenuDescriptor(cloneId);
break;
case "macro":
elementDescriptor = new MacroSetDescriptor(cloneId);
break;
default:
_sawmill.Error($"Invalid element to winclone \"{controlId}\"");
return;
}
}
if (elementDescriptor is WindowDescriptor windowDescriptor) {
// Cloned windows start off non-visible
elementDescriptor = windowDescriptor.WithVisible(_serializationManager, false);
}
LoadDescriptor(elementDescriptor);
if (elementDescriptor is WindowDescriptor && Windows.TryGetValue(cloneId, out var window)) {
window.CreateChildControls();
}
}
private void Reset() {
_uiManager.MainViewport.Visible = false;
//close windows if they're open, and clear all child ui elements
foreach (var window in Windows.Values){
window.CloseChildWindow();
window.UIElement.RemoveAllChildren();
}
Windows.Clear();
Menus.Clear();
MacroSets.Clear();
_inputManager.ResetAllBindings();
SetupBaseDreamBinds();
}
private void LoadInterface(InterfaceDescriptor descriptor) {
InterfaceDescriptor = descriptor;
foreach (MacroSetDescriptor macroSet in descriptor.MacroSetDescriptors) {
LoadDescriptor(macroSet);
}
foreach (MenuDescriptor menuDescriptor in InterfaceDescriptor.MenuDescriptors) {
LoadDescriptor(menuDescriptor);
}
foreach (WindowDescriptor windowDescriptor in InterfaceDescriptor.WindowDescriptors) {
LoadDescriptor(windowDescriptor);
}
foreach (ControlWindow window in Windows.Values) {
window.CreateChildControls();
foreach (InterfaceControl control in window.ChildControls) {
if (control.IsDefault) {
switch (control) {
case ControlOutput controlOutput: DefaultOutput = controlOutput; break;
case ControlInfo controlInfo: DefaultInfo = controlInfo; break;
case ControlMap controlMap: DefaultMap = controlMap; break;
}
}
}
}
if (DefaultWindow == null)
throw new Exception("Given DMF did not have a default window");
DefaultWindow.RegisterOnClydeWindow(_clyde.MainWindow);
DefaultWindow.UIElement.Name = "MainWindow";
LayoutContainer.SetAnchorRight(DefaultWindow.UIElement, 1);
LayoutContainer.SetAnchorBottom(DefaultWindow.UIElement, 1);
_uiManager.StateRoot.AddChild(DefaultWindow.UIElement);
if (DefaultWindow.GetClydeWindow() is { } clydeWindow) {
ClydeWindowIdToControl.Add(clydeWindow.Id, DefaultWindow);
}
}
private void OnWindowFocused(WindowFocusedEventArgs args) {
if (ClydeWindowIdToControl.TryGetValue(args.Window.Id, out var controlWindow)) {
_sawmill.Verbose($"window id {controlWindow.Id} was {(args.Focused ? "focused" : "defocused")}");
WindowDescriptor descriptor = (WindowDescriptor)controlWindow.ElementDescriptor;
descriptor.Focus = new DMFPropertyBool(args.Focused);
if (args.Focused && MacroSets.TryGetValue(descriptor.Macro.AsRaw(), out var windowMacroSet)) {