TitleWindowManager.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using Content.Shared.CCVar;
  2. using Robust.Client;
  3. using Robust.Client.Graphics;
  4. using Robust.Shared;
  5. using Robust.Shared.Configuration;
  6. namespace Content.Client.GameTicking.Managers;
  7. public sealed class TitleWindowManager
  8. {
  9. [Dependency] private readonly IBaseClient _client = default!;
  10. [Dependency] private readonly IClyde _clyde = default!;
  11. [Dependency] private readonly IConfigurationManager _cfg = default!;
  12. [Dependency] private readonly IGameController _gameController = default!;
  13. public void Initialize()
  14. {
  15. _cfg.OnValueChanged(CVars.GameHostName, OnHostnameChange, true);
  16. _cfg.OnValueChanged(CCVars.GameHostnameInTitlebar, OnHostnameTitleChange, true);
  17. _client.RunLevelChanged += OnRunLevelChangedChange;
  18. }
  19. public void Shutdown()
  20. {
  21. _cfg.UnsubValueChanged(CVars.GameHostName, OnHostnameChange);
  22. _cfg.UnsubValueChanged(CCVars.GameHostnameInTitlebar, OnHostnameTitleChange);
  23. }
  24. private void OnHostnameChange(string hostname)
  25. {
  26. var defaultWindowTitle = _gameController.GameTitle();
  27. // Since the game assumes the server name is MyServer and that GameHostnameInTitlebar CCVar is true by default
  28. // Lets just... not show anything. This also is used to revert back to just the game title on disconnect.
  29. if (_client.RunLevel == ClientRunLevel.Initialize)
  30. {
  31. _clyde.SetWindowTitle(defaultWindowTitle);
  32. return;
  33. }
  34. if (_cfg.GetCVar(CCVars.GameHostnameInTitlebar))
  35. // If you really dislike the dash I guess change it here
  36. _clyde.SetWindowTitle(hostname + " - " + defaultWindowTitle);
  37. else
  38. _clyde.SetWindowTitle(defaultWindowTitle);
  39. }
  40. // Clients by default assume game.hostname_in_titlebar is true
  41. // but we need to clear it as soon as we join and actually receive the servers preference on this.
  42. // This will ensure we rerun OnHostnameChange and set the correct title bar name.
  43. private void OnHostnameTitleChange(bool colonthree)
  44. {
  45. OnHostnameChange(_cfg.GetCVar(CVars.GameHostName));
  46. }
  47. // This is just used we can rerun the hostname change function when we disconnect to revert back to just the games title.
  48. private void OnRunLevelChangedChange(object? sender, RunLevelChangedEventArgs runLevelChangedEventArgs)
  49. {
  50. OnHostnameChange(_cfg.GetCVar(CVars.GameHostName));
  51. }
  52. }