diff --git a/Robust.Client/ClientIoC.cs b/Robust.Client/ClientIoC.cs index 93c541258..310b03fd9 100644 --- a/Robust.Client/ClientIoC.cs +++ b/Robust.Client/ClientIoC.cs @@ -1,4 +1,4 @@ -using System; +using System; using Robust.Client.Console; using Robust.Client.Debugging; using Robust.Client.GameObjects; @@ -127,6 +127,7 @@ namespace Robust.Client #endif IoCManager.Register(); + IoCManager.Register(); } } } diff --git a/Robust.Client/Console/ClientConGroupController.cs b/Robust.Client/Console/ClientConGroupController.cs new file mode 100644 index 000000000..a61f746f8 --- /dev/null +++ b/Robust.Client/Console/ClientConGroupController.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using Robust.Server.Console; +using Robust.Shared.Console; +using Robust.Shared.Interfaces.Network; +using Robust.Shared.IoC; + +namespace Robust.Client.Console +{ + /// + /// Tracks the console group of the client and which commands they can use. + /// Receives up to date permissions from the server whenever they change. + /// + public class ClientConGroupController : IClientConGroupController + { +#pragma warning disable 649 + [Dependency] private readonly IClientNetManager _netManager; +#pragma warning restore 649 + + /// + /// The console group this client is in. Determines which commands the client can use and if they can use vv. + /// + private ConGroup _clientConGroup; + + public void Initialize() + { + _netManager.RegisterNetMessage(MsgConGroupUpdate.Name, _onConGroupUpdate); + } + + public bool CanCommand(string cmdName) + { + if (_clientConGroup == null) + return false; + return _clientConGroup.Commands.Contains(cmdName); + } + + public bool CanViewVar() + { + if (_clientConGroup == null) + return false; + return _clientConGroup.CanViewVar; + } + + /// + /// Update client console group data with message from the server. + /// + /// Server message listing what commands this client can use. + private void _onConGroupUpdate(MsgConGroupUpdate msg) + { + _clientConGroup = msg.ClientConGroup; + } + } +} diff --git a/Robust.Client/Console/IClientConGroupController.cs b/Robust.Client/Console/IClientConGroupController.cs new file mode 100644 index 000000000..b729f85c4 --- /dev/null +++ b/Robust.Client/Console/IClientConGroupController.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Robust.Client.Console +{ + public interface IClientConGroupController + { + void Initialize(); + + bool CanCommand(string cmdName); + bool CanViewVar(); + } +} diff --git a/Robust.Client/GameController.cs b/Robust.Client/GameController.cs index 0c48a8a03..45fd47185 100644 --- a/Robust.Client/GameController.cs +++ b/Robust.Client/GameController.cs @@ -68,6 +68,7 @@ namespace Robust.Client [Dependency] private readonly ILocalizationManager _localizationManager; [Dependency] private readonly IModLoader _modLoader; [Dependency] private readonly ISignalHandler _signalHandler; + [Dependency] private readonly IClientConGroupController _conGroupController; #pragma warning restore 649 private CommandLineArgs _commandLineArgs; @@ -158,6 +159,7 @@ namespace Robust.Client _gameStateManager.Initialize(); _placementManager.Initialize(); _viewVariablesManager.Initialize(); + _conGroupController.Initialize(); _client.Initialize(); _discord.Initialize(); diff --git a/Robust.Server/Console/ConGroupContainer.cs b/Robust.Server/Console/ConGroupContainer.cs index 0899dc544..b71800109 100644 --- a/Robust.Server/Console/ConGroupContainer.cs +++ b/Robust.Server/Console/ConGroupContainer.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using Robust.Shared.Console; using Robust.Shared.Interfaces.Log; using Robust.Shared.Interfaces.Resources; using Robust.Shared.Utility; diff --git a/Robust.Server/Console/ConGroupController.cs b/Robust.Server/Console/ConGroupController.cs index 5dbe6514f..8d2474d86 100644 --- a/Robust.Server/Console/ConGroupController.cs +++ b/Robust.Server/Console/ConGroupController.cs @@ -3,9 +3,11 @@ using System.Net; using Robust.Server.Interfaces.Player; using Robust.Server.Player; using Robust.Shared.Configuration; +using Robust.Shared.Console; using Robust.Shared.Enums; using Robust.Shared.Interfaces.Configuration; using Robust.Shared.Interfaces.Log; +using Robust.Shared.Interfaces.Network; using Robust.Shared.Interfaces.Resources; using Robust.Shared.IoC; @@ -21,6 +23,7 @@ namespace Robust.Server.Console [Dependency] private readonly IConfigurationManager _configurationManager; [Dependency] private readonly ILogManager _logManager; [Dependency] private readonly IPlayerManager _playerManager; + [Dependency] private readonly INetManager _netManager; #pragma warning restore 649 private ConGroupContainer _groups; @@ -32,6 +35,8 @@ namespace Robust.Server.Console _configurationManager.RegisterCVar("console.loginlocal", true, CVar.ARCHIVE); + _netManager.RegisterNetMessage(MsgConGroupUpdate.Name); + _playerManager.PlayerStatusChanged += _onClientStatusChanged; // load the permission groups in the console @@ -40,6 +45,8 @@ namespace Robust.Server.Console // set up the session group container _sessions = new SessionGroupContainer(_configurationManager, logger); + + UpdateAllClientData(); } private void _onClientStatusChanged(object sender, SessionStatusEventArgs e) @@ -53,6 +60,7 @@ namespace Robust.Server.Console if (Equals(address, IPAddress.Loopback) || Equals(address, IPAddress.IPv6Loopback)) { SetGroup(session, new ConGroupIndex(_configurationManager.GetCVar("console.adminGroup"))); + UpdateClientData(session); } } } @@ -88,6 +96,7 @@ namespace Robust.Server.Console { _groups.Clear(); _groups.LoadGroups(); + UpdateAllClientData(); } public void SetGroup(IPlayerSession session, ConGroupIndex newGroup) @@ -99,6 +108,32 @@ namespace Robust.Server.Console return; _sessions.SetSessionGroup(session, newGroup); + UpdateClientData(session); + } + + /// + /// Update a single clients group data. + /// + /// The client session to update. + private void UpdateClientData(IPlayerSession session) + { + var group = _sessions.GetSessionGroup(session); + var groupData = _groups.Groups[group]; + + var msg = _netManager.CreateNetMessage(); + msg.ClientConGroup = groupData; + _netManager.ServerSendMessage(msg, session.ConnectedClient); + } + + /// + /// Update group data for all clients. + /// + private void UpdateAllClientData() + { + foreach (var session in _playerManager.GetAllPlayers()) + { + UpdateClientData(session); + } } } } diff --git a/Robust.Server/Console/ConGroup.cs b/Robust.Shared/Console/ConGroup.cs similarity index 89% rename from Robust.Server/Console/ConGroup.cs rename to Robust.Shared/Console/ConGroup.cs index 75420558d..d351c9482 100644 --- a/Robust.Server/Console/ConGroup.cs +++ b/Robust.Shared/Console/ConGroup.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace Robust.Server.Console +namespace Robust.Shared.Console { internal class ConGroup { diff --git a/Robust.Server/Console/ConGroupIndex.cs b/Robust.Shared/Console/ConGroupIndex.cs similarity index 100% rename from Robust.Server/Console/ConGroupIndex.cs rename to Robust.Shared/Console/ConGroupIndex.cs diff --git a/Robust.Shared/Console/MsgConGroupUpdate.cs b/Robust.Shared/Console/MsgConGroupUpdate.cs new file mode 100644 index 000000000..83565bdc6 --- /dev/null +++ b/Robust.Shared/Console/MsgConGroupUpdate.cs @@ -0,0 +1,52 @@ +using System.Collections.Generic; +using Lidgren.Network; +using Robust.Shared.Interfaces.Network; +using Robust.Shared.Network; + +namespace Robust.Shared.Console +{ + /// + /// Sent from server to client. Contains the console group of the client, + /// which includes a list of commands they can use. + /// + class MsgConGroupUpdate : NetMessage + { + public const MsgGroups Group = MsgGroups.Command; + public const string Name = nameof(MsgConGroupUpdate); + + public MsgConGroupUpdate(INetChannel channel) : base(Name, Group) + { + + } + + //Client console group data + public ConGroup ClientConGroup = new ConGroup(); + + public override void ReadFromBuffer(NetIncomingMessage buffer) + { + ClientConGroup.Index = buffer.ReadInt32(); + ClientConGroup.Name = buffer.ReadString(); + ClientConGroup.CanViewVar = buffer.ReadBoolean(); + + int numCommands = buffer.ReadInt32(); + ClientConGroup.Commands = new List(numCommands); + for (int i = 0; i < numCommands; i++) + { + ClientConGroup.Commands.Add(buffer.ReadString()); + } + } + + public override void WriteToBuffer(NetOutgoingMessage buffer) + { + buffer.Write(ClientConGroup.Index); + buffer.Write(ClientConGroup.Name); + buffer.Write(ClientConGroup.CanViewVar); + + buffer.Write(ClientConGroup.Commands.Count); + foreach (var command in ClientConGroup.Commands) + { + buffer.Write(command); + } + } + } +}