Skip to content

Commit

Permalink
make command server or client
Browse files Browse the repository at this point in the history
  • Loading branch information
stilnat committed Aug 5, 2023
1 parent f714cc7 commit 3b60837
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 43 deletions.
16 changes: 16 additions & 0 deletions Assets/Content/Data/DefaultPrefabObjects.asset
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ MonoBehaviour:
m_Name: DefaultPrefabObjects
m_EditorClassIdentifier:
_prefabs:
- {fileID: 6020083413388562179, guid: dfcd83503e242c843a6bc43cc5040fc9, type: 3}
- {fileID: 5177941338532333075, guid: 6deac385bf663034fa7d8dece88c07bb, type: 3}
- {fileID: 7875182825432581152, guid: ad573dedc3cb2d74fa42bedc463ffd75, type: 3}
- {fileID: 1962741444460096580, guid: 14b8b7b37cbe54a44a994f9d927bf213, type: 3}
- {fileID: 562230263738288595, guid: 06a27ffa07c1c754c9590607a5a80030, type: 3}
- {fileID: 562230263738288595, guid: 59f05e8a3a1d4b94eb5222aed605e260, type: 3}
- {fileID: 8203177977882359099, guid: 6ab4ce7059d374a4498481e1385c222e, type: 3}
- {fileID: 6636680625000353110, guid: cc5383d385556f443b2a7d3202f2b2c1, type: 3}
- {fileID: 3227132888119106738, guid: 7bbb74a7ffc8cae44baa886de43c586b, type: 3}
- {fileID: 1871071288925904272, guid: add4d1bc2d874d94782d6b8c912af5e2, type: 3}
- {fileID: 6115335805774105745, guid: bf177f8bab26bc84b87fd9a971004418, type: 3}
Expand Down Expand Up @@ -62,6 +66,7 @@ MonoBehaviour:
- {fileID: -7727686409064562107, guid: 05582ec649a27b64c85729877d9e344d, type: 3}
- {fileID: -7727686409064562107, guid: d211f90919bc28a4e8def76cb07197ba, type: 3}
- {fileID: 3206407796921847736, guid: 3335d001fdfff824aa859b98f2fe564e, type: 3}
- {fileID: 4302234173120300195, guid: 313c7a6f7eaaf4f4cb80cbe0fe77005d, type: 3}
- {fileID: 9154675693299657064, guid: af039f84b4a245049b5b62799af01384, type: 3}
- {fileID: 2930813178971533500, guid: 1c3eed1ea37ecc1448e41270cdc63486, type: 3}
- {fileID: 1019513474262773944, guid: 06c946c7c639138488b814e4448b82e4, type: 3}
Expand All @@ -75,6 +80,11 @@ MonoBehaviour:
- {fileID: 3258417366846669140, guid: 330d85a67a5891145bd8037dde90f37b, type: 3}
- {fileID: 3258417366846669140, guid: dd3ce5340ed4aba4babae7020f737084, type: 3}
- {fileID: 4819234911439508660, guid: 11f824c472e04854e95065dda095e8f4, type: 3}
- {fileID: 6348062478285535548, guid: 1d2cf546d53e3fe4c9103007427aaec7, type: 3}
- {fileID: 8767567404870203102, guid: 404443f7c306fc8469698745624fa3bf, type: 3}
- {fileID: 1491149956516498634, guid: b044d7bdb55405046a9055726a779eb1, type: 3}
- {fileID: 4037338737721612837, guid: f88a398ad3d01454f955de9c337d9b99, type: 3}
- {fileID: 1773710978803455870, guid: 4cb99cf2fee96f944abbacc51b0b701c, type: 3}
- {fileID: -1308347259443233487, guid: 821d0946be816bb42a0eed24631d57ee, type: 3}
- {fileID: 3815592008771065855, guid: 8686fbdb413e33b4395a775c4f2ba990, type: 3}
- {fileID: -7727686409064562107, guid: de6f06ad18d50134295959f438ba9c5d, type: 3}
Expand Down Expand Up @@ -111,6 +121,7 @@ MonoBehaviour:
- {fileID: 8635336608555116780, guid: 6caa99f8281726e40a793826d5c047db, type: 3}
- {fileID: 9015187684533357890, guid: 5e8a02c5ce0073644ac01bdb55d72a70, type: 3}
- {fileID: -2900402353996906960, guid: b5be0a52925e2674193d081d2deb68cb, type: 3}
- {fileID: 885347571389710399, guid: 162e188d33a82b440a239f20901c716a, type: 3}
- {fileID: 881130758709618923, guid: 2332a158259900e4f8a237fa8daff266, type: 3}
- {fileID: 7958861831329766982, guid: d8b0c8d1ddcdeb24d9763054c103c9fe, type: 3}
- {fileID: 7958861831329766982, guid: 79260483ae5d3e04fa272e092b49abac, type: 3}
Expand All @@ -119,6 +130,11 @@ MonoBehaviour:
- {fileID: 2090837846029440516, guid: 3520c433beff78a478401074d2ca95a3, type: 3}
- {fileID: 7958861831329766982, guid: d9aabfd5d726a7f499b8e360d919eba4, type: 3}
- {fileID: 4819234911439508660, guid: 1ae18785d74e9fa4699b6f6de889c243, type: 3}
- {fileID: 6294647206769462932, guid: 7becfab153276864f9fc7636a839d837, type: 3}
- {fileID: 1602098408367427586, guid: 9c8363ee7f934bd4b8ee5a2e30da1269, type: 3}
- {fileID: 4052990814619214022, guid: 772da386eb7b01745913050fee544c32, type: 3}
- {fileID: 7970185317444620997, guid: 781dd9024aaa53444bc3deeaa9708fbb, type: 3}
- {fileID: 261983550380735922, guid: 411b513efc8f06a40aecfc47d0c5fc5a, type: 3}
- {fileID: 6999522401175031449, guid: 9b8a1328991fc2846bc55a3d9a82e5c0, type: 3}
- {fileID: 1330758276423851772, guid: 0ef9cea3bb74e6c4788f77e711f92d4d, type: 3}
- {fileID: -7180308641962958576, guid: 501391c27a7f94c4d8c937f00a9ceb9f, type: 3}
Expand Down
54 changes: 52 additions & 2 deletions Assets/Content/Systems/UI/InGameConsole/Console.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ GameObject:
- component: {fileID: 4468402020721993883}
- component: {fileID: 4468402020721993882}
- component: {fileID: 4468402020721993861}
- component: {fileID: 2462624425783740797}
- component: {fileID: 6636680625000353110}
m_Layer: 5
m_Name: Console
m_TagString: Untagged
Expand Down Expand Up @@ -203,12 +205,60 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 42e2610303da350419124ae8ed8ce88e, type: 3}
m_Name:
m_EditorClassIdentifier:
_componentIndexCache: 0
_addedNetworkObject: {fileID: 6636680625000353110}
_networkObjectCache: {fileID: 6636680625000353110}
_consolePanel: {fileID: 4468402021449001342}
_contentContainer: {fileID: 4468402021075484997}
_movingSpeed: 2500
_inputField: {fileID: 4468402021573311568}
_commandsController: {fileID: 2462624425783740797}
_allPrevCommands:
-
--- !u!114 &2462624425783740797
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4468402020721993860}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9076991674ff4984a7bd0fed6bf0b169, type: 3}
m_Name:
m_EditorClassIdentifier:
_componentIndexCache: 1
_addedNetworkObject: {fileID: 6636680625000353110}
_networkObjectCache: {fileID: 6636680625000353110}
console: {fileID: 4468402020721993861}
--- !u!114 &6636680625000353110
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4468402020721993860}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 26b716c41e9b56b4baafaf13a523ba2e, type: 3}
m_Name:
m_EditorClassIdentifier:
<IsNested>k__BackingField: 0
<ComponentIndex>k__BackingField: 0
_networkBehaviours:
- {fileID: 4468402020721993861}
- {fileID: 2462624425783740797}
<ParentNetworkObject>k__BackingField: {fileID: 0}
<ChildNetworkObjects>k__BackingField: []
_isNetworked: 1
_isGlobal: 0
_defaultDespawnType: 0
NetworkObserver: {fileID: 0}
<PrefabId>k__BackingField: -1
_scenePathHash: 0
<SceneId>k__BackingField: 0
<AssetPathHash>k__BackingField: 17889965511288280426
_sceneNetworkObjects: []
--- !u!1 &4468402021075484997
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -245,7 +295,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}
m_AnchoredPosition: {x: 2, y: -68.55036}
m_SizeDelta: {x: -2, y: 0}
m_Pivot: {x: 0, y: 0}
--- !u!222 &4468402021075485017
Expand Down Expand Up @@ -1328,7 +1378,7 @@ MonoBehaviour:
m_HandleRect: {fileID: 4468402020486027511}
m_Direction: 2
m_Value: 0
m_Size: 1
m_Size: 0
m_NumberOfSteps: 0
m_OnValueChanged:
m_PersistentCalls:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public class ChangePermsCommand : Command
public override string LongDescription => "changeperms (user ckey) (required role)";
public override string ShortDescription => "Change user permission";
public override ServerRoleTypes AccessLevel => ServerRoleTypes.User;
public override string Perform(string[] args)
public override bool ServerCommand => true;

public override string Perform(string[] args)
{
CheckArgsResponse checkArgsResponse = CheckArgs(args);
if (checkArgsResponse.IsValid == false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ protected struct CheckArgsResponse
public bool IsValid;
public string InvalidArgs;
}

public abstract bool ServerCommand { get; }
public abstract string ShortDescription { get; }
public abstract string LongDescription { get; }
public abstract ServerRoleTypes AccessLevel { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public class EchoCommand : Command
public override string LongDescription => "echo (number) (your string)";
public override string ShortDescription => "Repeat your string";
public override ServerRoleTypes AccessLevel => ServerRoleTypes.User;
public override string Perform(string[] args)
public override bool ServerCommand => true;
public override string Perform(string[] args)
{
CheckArgsResponse checkArgsResponse = CheckArgs(args);
if (checkArgsResponse.IsValid == false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ public class AddTraitCommand : Command
public override string LongDescription => "Adds a trait to the item in hand";
public override string ShortDescription => "item.addtrait";
public override ServerRoleTypes AccessLevel => ServerRoleTypes.User;
public override bool ServerCommand => true;

public override string Perform(string[] args)
public override string Perform(string[] args)
{
CheckArgsResponse checkArgsResponse = CheckArgs(args);
if (checkArgsResponse.IsValid == false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using SS3D.Systems.Inventory.Items;
using SS3D.Systems.Inventory.Items;
using SS3D.Systems.Permissions;

namespace SS3D.Systems.IngameConsoleSystem.Commands
Expand All @@ -11,8 +11,9 @@ public class DescribeItemCommand : Command
public override string LongDescription => "Describes the item in hand";
public override string ShortDescription => "item.describe";
public override ServerRoleTypes AccessLevel => ServerRoleTypes.User;
public override bool ServerCommand => false;

public override string Perform(string[] args)
public override string Perform(string[] args)
{
CheckArgsResponse checkArgsResponse = CheckArgs(args);
if (checkArgsResponse.IsValid == false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using SS3D.Systems.Inventory.Items;
using SS3D.Systems.Inventory.Items;
using SS3D.Systems.Permissions;

namespace SS3D.Systems.IngameConsoleSystem.Commands
Expand All @@ -11,8 +11,9 @@ public class GetTraitCommand : Command
public override string LongDescription => "Get all traits from item in hand";
public override string ShortDescription => "item.traits";
public override ServerRoleTypes AccessLevel => ServerRoleTypes.User;
public override bool ServerCommand => false;

public override string Perform(string[] args)
public override string Perform(string[] args)
{
CheckArgsResponse checkArgsResponse = CheckArgs(args);
if (checkArgsResponse.IsValid == false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public class KillCommand : Command
public override string LongDescription => "kill (user ckey)";
public override string ShortDescription => "Kill player";
public override ServerRoleTypes AccessLevel => ServerRoleTypes.User;
public override string Perform(string[] args)
public override bool ServerCommand => true;

public override string Perform(string[] args)
{
CheckArgsResponse checkArgsResponse = CheckArgs(args);
if (checkArgsResponse.IsValid == false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public class PlayerListCommand : Command
public override string ShortDescription => "Show all players online";
public override ServerRoleTypes AccessLevel => ServerRoleTypes.User;

public override string Perform(string[] args)
public override bool ServerCommand => false;

public override string Perform(string[] args)
{
CheckArgsResponse checkArgsResponse = CheckArgs(args);
if (checkArgsResponse.IsValid == false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ public class QuitCommand : Command
public override string LongDescription => "Close app";
public override string ShortDescription => "Close app";
public override ServerRoleTypes AccessLevel => ServerRoleTypes.User;
public override string Perform(string[] args)

public override bool ServerCommand => true;
public override string Perform(string[] args)
{
CheckArgsResponse checkArgsResponse = CheckArgs(args);
if (checkArgsResponse.IsValid == false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ public class ReconnectCommand: Command
public override string LongDescription => "Restart app";
public override string ShortDescription => "Restart app";
public override ServerRoleTypes AccessLevel => ServerRoleTypes.User;
public override string Perform(string[] args)

public override bool ServerCommand => true;

public override string Perform(string[] args)
{
CheckArgsResponse checkArgsResponse = CheckArgs(args);
if (checkArgsResponse.IsValid == false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Codice.Client.Common;
using FishNet.Connection;
using FishNet.Object;
using SS3D.Core;
using SS3D.Core.Behaviours;
using SS3D.Systems.IngameConsoleSystem.Commands;
using SS3D.Systems.Permissions;
using SS3D.Systems.PlayerControl;
using UnityEngine;
using UnityEngine.InputSystem;

namespace SS3D.Systems.IngameConsoleSystem
{
public class CommandsController
public class CommandsController : NetworkActor
{
/// <summary>
/// Contains all command infos and their names in lowercase
Expand All @@ -16,7 +25,10 @@ public class CommandsController
/// Length of space between columns in help command
/// </summary>
private const int TabLength = 20;
public CommandsController()

[SerializeField] private ConsolePanelView console;

protected override void OnStart()
{
IEnumerable<Type> commands = Assembly.GetAssembly(typeof(Command)).GetTypes().Where(iType =>
iType.IsClass && iType.IsSubclassOf(typeof(Command)) && !iType.IsAbstract);
Expand All @@ -29,35 +41,82 @@ public CommandsController()
_allCommands.Add(name, instance);
}
}
/// <summary>
/// Find and call command
/// </summary>
/// <param name="command">Command and it's args separated by spaces</param>
/// <returns>Command response</returns>
public string ProcessCommand(string command)

[Client]
public void ClientProcessCommand(string command)
{
ProcessCommand(command);
}

/// <summary>
/// Find and call command
/// </summary>
/// <param name="command">Command and it's args separated by spaces</param>
/// <returns>Command response</returns>
[ServerRpc(RequireOwnership = false)]
public void ProcessCommand(string command, NetworkConnection conn = null)
{
string[] splitCommand = command.Split(' ');
string ckey = Subsystems.Get<PlayerSystem>().GetCkey(conn);
if (!Subsystems.Get<PermissionSystem>().TryGetUserRole(ckey, out ServerRoleTypes userPermission))
{
CommandAnswer(conn, string.Format("No role found for user {0}, can't process command", ckey));
}

string[] splitCommand = command.Split(' ');
string commandName = splitCommand[0];
if (commandName == "help")
{
return HelpCommand();
CommandAnswer(conn, HelpCommand());
}
if (splitCommand.Length > 1)
{
if (splitCommand[1] == "help")
{
return LongHelpCommand(command);
CommandAnswer(conn, LongHelpCommand(command));
}
}
// Find proper command body and prepare correct args
if (_allCommands.ContainsKey(commandName))

if (!_allCommands.ContainsKey(commandName))
{
return _allCommands[commandName].Perform(command.Split().Skip(1).ToArray());
}
return "No such command exists";
}
/// <returns>All available commands with short descriptions</returns>
private string HelpCommand()
CommandAnswer(conn, "No such command exists");
}

if (_allCommands[commandName].AccessLevel > userPermission)
{
CommandAnswer(conn, "Access level too low, can't perform command");
}

if (_allCommands[commandName].ServerCommand)
{
CommandAnswer(conn, _allCommands[commandName].Perform(command.Split().Skip(1).ToArray()));
}
else
{
PerformOnClient(conn, command);
}

// Find proper command body and prepare correct args
CommandAnswer(conn, _allCommands[commandName].Perform(command.Split().Skip(1).ToArray()));

}

[TargetRpc]
public void PerformOnClient(NetworkConnection conn, string command)
{
string[] splitCommand = command.Split(' ');
string commandName = splitCommand[0];
string answer = _allCommands[commandName].Perform(command.Split().Skip(1).ToArray());
console.AddText(answer);
}

[TargetRpc]
public void CommandAnswer(NetworkConnection conn, string answer)
{
console.AddText(answer);
}

/// <returns>All available commands with short descriptions</returns>
private string HelpCommand()
{
string ret = "";
foreach (KeyValuePair<string, Command> i in _allCommands)
Expand Down
Loading

0 comments on commit 3b60837

Please sign in to comment.