Skip to content

Commit

Permalink
Fix console, change speed (RE-SS3D#1241)
Browse files Browse the repository at this point in the history
Co-authored-by: Mechar418 <[email protected]>
  • Loading branch information
iamteapot422 and iamteapot422 authored Aug 27, 2023
1 parent c025208 commit 77d0b1f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 113 deletions.
8 changes: 4 additions & 4 deletions Assets/Content/Systems/UI/InGameConsole/Console.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 2, y: -230.00002}
m_AnchoredPosition: {x: 2, y: -229.99997}
m_SizeDelta: {x: -2, y: 0}
m_Pivot: {x: 0, y: 0}
--- !u!222 &4468402021075485017
Expand Down Expand Up @@ -1137,7 +1137,7 @@ MonoBehaviour:
m_Elasticity: 0.1
m_Inertia: 1
m_DecelerationRate: 0.135
m_ScrollSensitivity: 1
m_ScrollSensitivity: 30
m_Viewport: {fileID: 4468402022117316798}
m_HorizontalScrollbar: {fileID: 0}
m_VerticalScrollbar: {fileID: 4468402022262526144}
Expand Down Expand Up @@ -1360,8 +1360,8 @@ MonoBehaviour:
m_TargetGraphic: {fileID: 4468402020486027508}
m_HandleRect: {fileID: 4468402020486027511}
m_Direction: 2
m_Value: 1
m_Size: 0.9991756
m_Value: 0
m_Size: 1
m_NumberOfSteps: 0
m_OnValueChanged:
m_PersistentCalls:
Expand Down
190 changes: 81 additions & 109 deletions Assets/Scripts/SS3D/Systems/IngameConsoleSystem/CommandsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using FishNet.Object;
using SS3D.Core;
using SS3D.Core.Behaviours;
using SS3D.Logging;
using SS3D.Systems.IngameConsoleSystem.Commands;
using SS3D.Systems.Permissions;
using SS3D.Systems.PlayerControl;
Expand Down Expand Up @@ -34,7 +33,7 @@ protected override void OnStart()
foreach (Type command in commands)
{
string name = command.Name.ToLower();
// Remove Command suffix
// Remove "Command" suffix
name = name.Substring(0, name.Length - 7);
Command instance = (Command)Activator.CreateInstance(command);
_allCommands.Add(name, instance);
Expand All @@ -48,165 +47,138 @@ protected override void OnStart()
[Client]
public void ClientProcessCommand(string command)
{
if(_allCommands[CommandName(command)].Type == CommandType.Offline)
{
OfflineProcessCommand(command);
if (GetCommandName(command) == "help")
{
CommandAnswer(HelpCommand());
return;
}

if (!FindCommand(GetCommandName(command), out Command commandObject))
{
CommandAnswer("No such command exists");
return;
}

string[] splitCommand = command.Split(' ');
if (splitCommand.Length > 1)
{
if (splitCommand[1] == "help")
{
CommandAnswer(LongHelpCommand(commandObject));
}
}

string[] args = GetCommandArgs(command);
if(_allCommands[GetCommandName(command)].Type == CommandType.Offline)
{
OfflineProcessCommand(commandObject, args);
return;
}
CmdProcessOnlineCommand(command);
CmdProcessOnlineCommand(GetCommandName(command), args);
}

[ServerOrClient]
private string CommandName(string command)
private string GetCommandName(string command)
{
string[] splitCommand = command.Split(' ');
return splitCommand[0];
}

[ServerOrClient]
private void OfflineProcessCommand(string command)
private string[] GetCommandArgs(string command)
{
string[] splitCommand = command.Split(' ');
string commandName = CommandName(command);

if (TreatUnknownCommand(null, commandName, out Command commandObject))
{
return;
}

if (TreatHelpCommand(null, command, commandName, splitCommand))
{
return;
}

commandObject.Perform(command.Split().Skip(1).ToArray());
return command.Split().Skip(1).ToArray();
}
/// <summary>
/// Perform command without sending a request to server.
/// </summary>
[ServerOrClient]
private void OfflineProcessCommand(Command command, string[] args)
{
CommandAnswer(command.Perform(args));
}

/// <summary>
/// Find and call command
/// Check user permission and either perform command on server or on client
/// </summary>
/// <param name="command">Command and it's args separated by spaces</param>
/// <returns>Command response</returns>
[ServerRpc(RequireOwnership = false)]
private void CmdProcessOnlineCommand(string command, NetworkConnection conn = null)
private void CmdProcessOnlineCommand(string commandName, string[] args, NetworkConnection conn = null)
{
string ckey = Subsystems.Get<PlayerSystem>().GetCkey(conn);
FindCommand(commandName, out Command command);

if (!Subsystems.Get<PermissionSystem>().TryGetUserRole(ckey, out ServerRoleTypes userPermission))
{
CommandAnswer(conn, string.Format("No role found for user {0}, can't process command", ckey));
CommandAnswer(string.Format("No role found for user {0}, can't process command", ckey), conn);
return;
}

string[] splitCommand = command.Split(' ');
string commandName = CommandName(command);

if (TreatUnknownCommand(conn, commandName, out Command commandObject))
{
return;
}

if (TreatHelpCommand(conn, command, commandName, splitCommand))
{
return;
}

if (commandObject.AccessLevel > userPermission)

if (userPermission < command.AccessLevel)
{
CommandAnswer(conn, "Access level too low, can't perform command");
CommandAnswer("Your permission level is too low, can't perform the command", conn);
return;
}

// Either execute command on server or call on client.
// Offline commands go through server if client is connected to server.
switch (commandObject.Type)
switch (command.Type)
{
case CommandType.Server:
CommandAnswer(conn, commandObject.Perform(command.Split().Skip(1).ToArray(), conn));
CommandAnswer(command.Perform(args, conn), conn);
break;

case CommandType.Client:
RpcPerformOnClient(conn, command);
break;

case CommandType.Offline:
RpcPerformOnClient(conn, command);
RpcPerformOnClient(conn, commandName, args);
break;
}
}

[ServerOrClient]
private bool TreatHelpCommand(NetworkConnection conn, string command, string commandName, string[] splitCommand)
{
if (commandName == "help")
{
CommandAnswer(conn, HelpCommand());
return true;
}
if (splitCommand.Length > 1)
{
if (splitCommand[1] == "help")
{
CommandAnswer(conn, LongHelpCommand(command));
return true;
}
}
return false;
}

/// <summary>
/// Find command by name
/// </summary>
/// <returns>Is given command found</returns>
[ServerOrClient]
private bool TreatUnknownCommand(NetworkConnection conn, string commandName, out Command command)
private bool FindCommand(string commandName, out Command command)
{
if (!_allCommands.ContainsKey(commandName))
{
CommandAnswer(conn, "No such command exists");
command = null;
return true;
command = null;
return false;
}
command = _allCommands[commandName];
return false;
return true;
}


[TargetRpc]
private void RpcPerformOnClient(NetworkConnection conn, string command)
/// <summary>
/// Perform command on client
/// </summary>
[TargetRpc]
private void RpcPerformOnClient(NetworkConnection conn, string commandName, string[] args)
{
string[] splitCommand = command.Split(' ');
string commandName = splitCommand[0];

if (TreatUnknownCommand(conn, commandName, out Command commandObject))
{
return;
}

if (TreatHelpCommand(conn, command, commandName, splitCommand))
{
return;
}

string answer = commandObject.Perform(command.Split().Skip(1).ToArray());
FindCommand(commandName, out Command command);
string answer = command.Perform(args);
_console.AddText(answer);
}
/// <summary>
/// Send or print command's answer
/// </summary>
[ServerOrClient]
private void CommandAnswer(string answer, NetworkConnection conn = null)
{
if ((IsServer) && (conn != null))
{
RpcCommandAnswer(conn, answer);
}
else
{
_console.AddText(answer);
}
}

[TargetRpc]
private void RpcCommandAnswer(NetworkConnection conn, string answer)
{
_console.AddText(answer);
}

[ServerOrClient]
private void CommandAnswer(NetworkConnection conn, string answer)
{
if (IsServer)
{
RpcCommandAnswer(conn, answer);
}
else
{
_console.AddText(answer);
}
}


/// <returns>All available commands with short descriptions</returns>
private string HelpCommand()
{
Expand All @@ -220,9 +192,9 @@ private string HelpCommand()
return ret;
}
/// <returns>Long help for given command</returns>
private string LongHelpCommand(string command)
private string LongHelpCommand(Command command)
{
return _allCommands[command.Split(' ')[0]].LongDescription;
return command.LongDescription;
}
}
}

0 comments on commit 77d0b1f

Please sign in to comment.