From b84d19e702c51ead817f4f9be6e2ec363786e98d Mon Sep 17 00:00:00 2001 From: Kevin Zheng Date: Mon, 29 Aug 2022 19:38:56 -0700 Subject: [PATCH] Send server rules over the network (#10847) Add a new InfoSystem that sends SharedInfo from the server to client when requested. Currently, only the rule header and rule text is sent. Previously, the rule header and rule text was bundled in the client, which means that the client would only display rules that it was built with, even if the server has different rules. This allows servers all running the same build to send different rules. This could be useful, for example, for servers running the official builds to send different rulesets without a client rebuild. --- Content.Client/Info/InfoSection.xaml.cs | 5 +++- Content.Client/Info/InfoSystem.cs | 25 ++++++++++++++++ Content.Client/Info/RulesAndInfoWindow.cs | 13 ++------ Content.Client/Info/RulesControl.xaml.cs | 5 ++-- Content.Client/Info/RulesManager.cs | 18 ++++++++++++ Content.Server/Info/InfoSystem.cs | 36 +++++++++++++++++++++++ Content.Shared/Info/SharedInfo.cs | 28 ++++++++++++++++++ 7 files changed, 117 insertions(+), 13 deletions(-) create mode 100644 Content.Client/Info/InfoSystem.cs create mode 100644 Content.Server/Info/InfoSystem.cs create mode 100644 Content.Shared/Info/SharedInfo.cs diff --git a/Content.Client/Info/InfoSection.xaml.cs b/Content.Client/Info/InfoSection.xaml.cs index 5671c833ba..ab9d352d32 100644 --- a/Content.Client/Info/InfoSection.xaml.cs +++ b/Content.Client/Info/InfoSection.xaml.cs @@ -11,9 +11,12 @@ public sealed partial class InfoSection : BoxContainer public InfoSection(string title, string text, bool markup = false) { RobustXamlLoader.Load(this); + SetText(title, text, markup); + } + public void SetText(string title, string text, bool markup = false) + { TitleLabel.Text = title; - if (markup) Content.SetMessage(FormattedMessage.FromMarkup(text.Trim())); else diff --git a/Content.Client/Info/InfoSystem.cs b/Content.Client/Info/InfoSystem.cs new file mode 100644 index 0000000000..82dd95ee67 --- /dev/null +++ b/Content.Client/Info/InfoSystem.cs @@ -0,0 +1,25 @@ +using Content.Shared.Info; +using Robust.Shared.Log; + +namespace Content.Client.Info; + +public sealed class InfoSystem : EntitySystem +{ + public RulesMessage Rules = new RulesMessage("Server Rules", "The server did not send any rules."); + [Dependency] private readonly RulesManager _rules = default!; + + public override void Initialize() + { + base.Initialize(); + SubscribeNetworkEvent(OnRulesReceived); + Logger.DebugS("info", "Requested server info."); + RaiseNetworkEvent(new RequestRulesMessage()); + } + + protected void OnRulesReceived(RulesMessage message, EntitySessionEventArgs eventArgs) + { + Logger.DebugS("info", "Received server rules."); + Rules = message; + _rules.UpdateRules(); + } +} diff --git a/Content.Client/Info/RulesAndInfoWindow.cs b/Content.Client/Info/RulesAndInfoWindow.cs index 0e477978b6..acb797828c 100644 --- a/Content.Client/Info/RulesAndInfoWindow.cs +++ b/Content.Client/Info/RulesAndInfoWindow.cs @@ -13,6 +13,8 @@ namespace Content.Client.Info { [Dependency] private readonly IResourceCache _resourceManager = default!; [Dependency] private readonly IConfigurationManager _cfgManager = default!; + [Dependency] private readonly IEntitySystemManager _sysMan = default!; + [Dependency] private readonly RulesManager _rules = default!; private OptionsMenu optionsMenu; @@ -35,7 +37,7 @@ namespace Content.Client.Info TabContainer.SetTabTitle(rulesList, Loc.GetString("ui-info-tab-rules")); TabContainer.SetTabTitle(tutorialList, Loc.GetString("ui-info-tab-tutorial")); - PopulateRules(rulesList); + AddSection(rulesList, _rules.RulesSection()); PopulateTutorial(tutorialList); Contents.AddChild(rootContainer); @@ -43,11 +45,6 @@ namespace Content.Client.Info SetSize = (650, 650); } - private void PopulateRules(Info rulesList) - { - AddSection(rulesList, MakeRules(_cfgManager, _resourceManager)); - } - private void PopulateTutorial(Info tutorialList) { AddSection(tutorialList, Loc.GetString("ui-info-header-intro"), "Intro.txt"); @@ -74,9 +71,5 @@ namespace Content.Client.Info return new InfoSection(title, res.ContentFileReadAllText($"/Server Info/{path}"), markup); } - public static Control MakeRules(IConfigurationManager cfg, IResourceManager res) - { - return MakeSection(Loc.GetString(cfg.GetCVar(CCVars.RulesHeader)), cfg.GetCVar(CCVars.RulesFile), true, res); - } } } diff --git a/Content.Client/Info/RulesControl.xaml.cs b/Content.Client/Info/RulesControl.xaml.cs index 4470094b92..dda96dbe24 100644 --- a/Content.Client/Info/RulesControl.xaml.cs +++ b/Content.Client/Info/RulesControl.xaml.cs @@ -13,12 +13,13 @@ public sealed partial class RulesControl : BoxContainer { [Dependency] private readonly IResourceCache _resourceManager = default!; [Dependency] private readonly IConfigurationManager _cfgManager = default!; + [Dependency] private readonly IEntitySystemManager _sysMan = default!; + [Dependency] private readonly RulesManager _rules = default!; public RulesControl() { RobustXamlLoader.Load(this); IoCManager.InjectDependencies(this); - - AddChild(RulesAndInfoWindow.MakeRules(_cfgManager, _resourceManager)); + AddChild(_rules.RulesSection()); } } diff --git a/Content.Client/Info/RulesManager.cs b/Content.Client/Info/RulesManager.cs index ccb742befa..1a098dd19c 100644 --- a/Content.Client/Info/RulesManager.cs +++ b/Content.Client/Info/RulesManager.cs @@ -17,7 +17,9 @@ public sealed class RulesManager : SharedRulesManager [Dependency] private readonly IStateManager _stateManager = default!; [Dependency] private readonly IClientConsoleHost _consoleHost = default!; [Dependency] private readonly INetManager _netManager = default!; + [Dependency] private readonly IEntitySystemManager _sysMan = default!; + private InfoSection rulesSection = new InfoSection("", "", false); private bool _shouldShowRules; public void Initialize() @@ -72,4 +74,20 @@ public sealed class RulesManager : SharedRulesManager var message = _netManager.CreateNetMessage(); _netManager.ClientSendMessage(message); } + + public void UpdateRules() + { + var rules = _sysMan.GetEntitySystem().Rules; + rulesSection.SetText(rules.Title, rules.Text, true); + } + + public Control RulesSection() + { + if (rulesSection.Disposed) + { + rulesSection = new InfoSection("", "", false); + } + UpdateRules(); + return rulesSection; + } } diff --git a/Content.Server/Info/InfoSystem.cs b/Content.Server/Info/InfoSystem.cs new file mode 100644 index 0000000000..78b1adfa4a --- /dev/null +++ b/Content.Server/Info/InfoSystem.cs @@ -0,0 +1,36 @@ +using Content.Shared.CCVar; +using Content.Shared.Info; +using Robust.Shared.Configuration; +using Robust.Shared.ContentPack; +using Robust.Shared.Log; + +namespace Content.Server.Info; + +public class InfoSystem : EntitySystem +{ + [Dependency] private readonly IResourceManager _res = default!; + [Dependency] private readonly IConfigurationManager _cfg = default!; + public override void Initialize() + { + base.Initialize(); + SubscribeNetworkEvent(OnRequestRules); + } + + protected void OnRequestRules(RequestRulesMessage message, EntitySessionEventArgs eventArgs) + { + Logger.DebugS("info", "Client requested rules."); + var title = Loc.GetString(_cfg.GetCVar(CCVars.RulesHeader)); + var path = _cfg.GetCVar(CCVars.RulesFile); + var rules = "Server could not read its rules."; + try + { + rules = _res.ContentFileReadAllText($"/Server Info/{path}"); + } + catch (System.Exception) + { + Logger.ErrorS("info", "Could not read server rules file."); + } + var response = new RulesMessage(title, rules); + RaiseNetworkEvent(response, eventArgs.SenderSession.ConnectedClient); + } +} diff --git a/Content.Shared/Info/SharedInfo.cs b/Content.Shared/Info/SharedInfo.cs new file mode 100644 index 0000000000..4a0e688cf9 --- /dev/null +++ b/Content.Shared/Info/SharedInfo.cs @@ -0,0 +1,28 @@ +using Robust.Shared.Serialization; + +namespace Content.Shared.Info +{ + /// + /// A client request for server rules. + /// + [Serializable, NetSerializable] + public sealed class RequestRulesMessage : EntityEventArgs + { + } + + /// + /// A server response with server rules. + /// + [Serializable, NetSerializable] + public sealed class RulesMessage : EntityEventArgs + { + public string Title; + public string Text; + + public RulesMessage(string title, string rules) + { + Title = title; + Text = rules; + } + } +}