Skip to content

Commit 5ece8c0

Browse files
authored
Improve console commands (#1354)
* Refactor Command * Shorten some commands * Refactor commands * Fix little bug * Make comments * Replace structs with records * Delete unecessary scripts * Move IsExternalInit * do stuff * Change usage of hurt command --------- Co-authored-by: Mechar418 <[email protected]>
1 parent 63eeeab commit 5ece8c0

25 files changed

+347
-707
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace System.Runtime.CompilerServices
2+
{
3+
/// <summary>
4+
/// Necessary for Unity to use init and record, since it's only available in .NET 5
5+
/// </summary>
6+
public class IsExternalInit {}
7+
}
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/SS3D/Systems/IngameConsoleSystem/Commands/AddHandCommand.cs

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using SS3D.Systems.Inventory.Containers;
99
using FishNet.Connection;
1010
using SS3D.Permissions;
11+
using System;
1112

1213
namespace SS3D.Systems.IngameConsoleSystem.Commands
1314
{
@@ -18,73 +19,60 @@ namespace SS3D.Systems.IngameConsoleSystem.Commands
1819
/// </summary>
1920
public class AddHandCommand : Command
2021
{
21-
public override string LongDescription => "add (ckey) [(position) (rotation)]\n Position and rotation are float arrays and written as x y z";
22-
public override string ShortDescription => "add hand to user";
22+
public override string ShortDescription => "Add hand to user";
23+
public override string Usage => "(ckey) [(position) (rotation)] \nPosition and rotation are float arrays and written as x y z";
2324
public override ServerRoleTypes AccessLevel => ServerRoleTypes.Administrator;
24-
2525
public override CommandType Type => CommandType.Server;
26+
27+
private record CalculatedValues(Player Player, Entity Entity, Vector3 Position, Vector3 Rotation) : ICalculatedValues;
28+
2629
public override string Perform(string[] args, NetworkConnection conn)
2730
{
28-
CheckArgsResponse checkArgsResponse = CheckArgs(args);
29-
if (checkArgsResponse.IsValid == false)
30-
return checkArgsResponse.InvalidArgs;
31-
32-
string ckey = args[0];
33-
34-
// default transform for hand.
35-
Vector3 position = new Vector3(0.5f, 0.7f, 0);
36-
Vector3 rotation = new Vector3(-50, -270, 90);
37-
38-
if (args.Length > 1)
39-
{
40-
position = new Vector3(float.Parse(args[1]), float.Parse(args[2]), float.Parse(args[3]));
41-
rotation = new Vector3(float.Parse(args[4]), float.Parse(args[5]), float.Parse(args[6]));
42-
}
43-
44-
Player Player = Subsystems.Get<PlayerSystem>().GetPlayer(ckey);
45-
Entity entity = Subsystems.Get<EntitySystem>().GetSpawnedEntity(Player);
46-
31+
if (!ReceiveCheckResponse(args, out CheckArgsResponse response, out CalculatedValues values)) return response.InvalidArgs;
32+
4733
GameObject leftHandPrefab = Assets.Get<GameObject>((int)AssetDatabases.BodyParts, (int)BodyPartsIds.HumanHandLeft);
48-
GameObject leftHandObject = GameObject.Instantiate(leftHandPrefab, entity.transform);
49-
leftHandObject.transform.localPosition = position;
50-
leftHandObject.transform.localEulerAngles = rotation;
51-
52-
Hand leftHand = leftHandObject.GetComponent<Hand>();
53-
InstanceFinder.ServerManager.Spawn(leftHandObject, Player.Owner);
54-
55-
Hands hands = entity.GetComponent<Hands>();
56-
HumanInventory inventory = entity.GetComponent<HumanInventory>();
57-
inventory.TryAddContainer(leftHandObject.GetComponent<AttachedContainer>());
58-
hands.AddHand(leftHand);
34+
GameObject leftHandObject = GameObject.Instantiate(leftHandPrefab, values.Entity.transform);
35+
leftHandObject.transform.localPosition = values.Position;
36+
leftHandObject.transform.localEulerAngles = values.Rotation;
37+
InstanceFinder.ServerManager.Spawn(leftHandObject, values.Player.Owner);
38+
values.Entity.GetComponent<HumanInventory>().TryAddContainer(leftHandObject.GetComponent<AttachedContainer>());
39+
values.Entity.GetComponent<Hands>().AddHand(leftHandObject.GetComponent<Hand>());
5940

6041
return "hand added";
6142
}
6243
protected override CheckArgsResponse CheckArgs(string[] args)
6344
{
64-
CheckArgsResponse response = new CheckArgsResponse();
65-
if (args.Length != 1 && args.Length != 7)
66-
{
67-
response.IsValid = false;
68-
response.InvalidArgs = "Invalid number of arguments";
69-
return response;
70-
}
45+
CheckArgsResponse response = new();
46+
if (args.Length != 1 && args.Length != 7) return response.MakeInvalid("Invalid number of arguments");
47+
7148
string ckey = args[0];
7249
Player player = Subsystems.Get<PlayerSystem>().GetPlayer(ckey);
73-
if (player == null)
50+
if (player == null) return response.MakeInvalid("This player doesn't exist");
51+
52+
Entity entity = Subsystems.Get<EntitySystem>().GetSpawnedEntity(player);
53+
if (entity == null) return response.MakeInvalid("This entity doesn't exist");
54+
55+
Vector3 position;
56+
Vector3 rotation;
57+
if (args.Length > 1)
7458
{
75-
response.IsValid = false;
76-
response.InvalidArgs = "This player doesn't exist";
77-
return response;
59+
try
60+
{
61+
position = new(float.Parse(args[1]), float.Parse(args[2]), float.Parse(args[3]));
62+
rotation = new(float.Parse(args[4]), float.Parse(args[5]), float.Parse(args[6]));
63+
}
64+
catch (FormatException)
65+
{
66+
return response.MakeInvalid("Incorrect position/rotation format");
67+
}
7868
}
79-
Entity entityToKill = Subsystems.Get<EntitySystem>().GetSpawnedEntity(player);
80-
if (entityToKill == null)
69+
else
8170
{
82-
response.IsValid = false;
83-
response.InvalidArgs = "This entity doesn't exist";
84-
return response;
71+
position = new(0.5f, 0.7f, 0);
72+
rotation = new(-50, -270, 90);
8573
}
86-
response.IsValid = true;
87-
return response;
74+
75+
return response.MakeValid(new CalculatedValues(player, entity, position, rotation));
8876
}
8977
}
9078
}

Assets/Scripts/SS3D/Systems/IngameConsoleSystem/Commands/ChangePermsCommand.cs

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,30 @@ namespace SS3D.Systems.IngameConsoleSystem.Commands
88
{
99
public class ChangePermsCommand : Command
1010
{
11-
public override string LongDescription => "changeperms (user ckey) (required role)";
1211
public override string ShortDescription => "Change user permission";
12+
public override string Usage => "(user ckey) (required role)";
1313
public override ServerRoleTypes AccessLevel => ServerRoleTypes.Administrator;
14-
1514
public override CommandType Type => CommandType.Server;
15+
16+
private record CalculatedValues(string Ckey, ServerRoleTypes Role) : ICalculatedValues;
1617

1718
public override string Perform(string[] args, NetworkConnection conn = null)
1819
{
19-
CheckArgsResponse checkArgsResponse = CheckArgs(args);
20-
if (checkArgsResponse.IsValid == false)
21-
return checkArgsResponse.InvalidArgs;
22-
string ckey = args[0];
23-
string role = args[1];
24-
ServerRoleTypes foundRole = FindRole(role);
25-
Subsystems.Get<PermissionSystem>().ChangeUserPermission(ckey, foundRole);
26-
return "Done";
20+
if (!ReceiveCheckResponse(args, out CheckArgsResponse response, out CalculatedValues values)) return response.InvalidArgs;
21+
22+
Subsystems.Get<PermissionSystem>().ChangeUserPermission(values.Ckey, values.Role);
23+
return "Permission changed to " + args[1];
2724
}
25+
2826
protected override CheckArgsResponse CheckArgs(string[] args)
2927
{
30-
CheckArgsResponse response = new CheckArgsResponse();
31-
if (args.Length != 2)
32-
{
33-
response.IsValid = false;
34-
response.InvalidArgs = "Invalid number of arguments";
35-
return response;
36-
}
28+
CheckArgsResponse response = new();
29+
if (args.Length != 2) return response.MakeInvalid("Invalid number of arguments");
3730

38-
string ckey = args[0];
39-
bool isFound = Subsystems.Get<PermissionSystem>().TryGetUserRole(ckey, out _);
40-
if (!isFound)
41-
{
42-
response.IsValid = false;
43-
response.InvalidArgs = "Ckey doesn't have any permissions";
44-
return response;
45-
}
46-
47-
string role = args[1];
48-
if (FindRole(role) == ServerRoleTypes.None)
49-
{
50-
response.IsValid = false;
51-
response.InvalidArgs = "Role doesn't exist";
52-
return response;
53-
}
54-
response.IsValid = true;
55-
return response;
31+
ServerRoleTypes role = FindRole(args[1]);
32+
if (role == ServerRoleTypes.None) return response.MakeInvalid("Role doesn't exist");
33+
34+
return response.MakeValid(new CalculatedValues(args[0], role));
5635
}
5736

5837
private ServerRoleTypes FindRole(string name)

Assets/Scripts/SS3D/Systems/IngameConsoleSystem/Commands/Command.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,45 @@ protected struct CheckArgsResponse
1010
{
1111
public bool IsValid;
1212
public string InvalidArgs;
13+
public ICalculatedValues CalculatedValues;
14+
public CheckArgsResponse MakeInvalid(string invalidArgs)
15+
{
16+
InvalidArgs = invalidArgs;
17+
IsValid = false;
18+
return this;
19+
}
20+
public CheckArgsResponse MakeValid(ICalculatedValues calculatedValues)
21+
{
22+
CalculatedValues = calculatedValues;
23+
IsValid = true;
24+
return this;
25+
}
1326
}
27+
/// <summary>
28+
/// Struct to transfer calculated values between CheckArgs and Perform methods
29+
/// </summary>
30+
protected interface ICalculatedValues { }
1431

1532
/// <summary>
1633
/// Is the command going to be executed server or client side ?
1734
/// </summary>
1835
public abstract CommandType Type { get; }
1936

37+
/// <summary>
38+
/// Desription, that will be shown after "help" command
39+
/// </summary>
2040
public abstract string ShortDescription { get; }
2141

22-
public abstract string LongDescription { get; }
42+
/// <summary>
43+
/// Detailed description of the command. Will be shown after "help (command name)" command
44+
/// </summary>
45+
public virtual string LongDescription => ShortDescription;
46+
47+
/// <summary>
48+
/// how to use the command.
49+
/// Syntax for writing usage: arguments in () - necessary; [] - optional; {} - list of arguments with undefined size
50+
/// </summary>
51+
public virtual string Usage => "";
2352

2453
/// <summary>
2554
/// The requested role to be able to perform this command.
@@ -39,5 +68,16 @@ protected struct CheckArgsResponse
3968
/// </summary>
4069
protected abstract CheckArgsResponse CheckArgs(string[] args);
4170
protected const string WrongArgsText = "Wrong args. Type \"(command) help\"";
71+
72+
/// <summary>
73+
/// Store CheckArgs response and get if response is valid.
74+
/// </summary>
75+
/// <returns>If respons is valid</returns>
76+
protected bool ReceiveCheckResponse<T>(string[] args, out CheckArgsResponse response, out T calculatedValues) where T: ICalculatedValues
77+
{
78+
response = CheckArgs(args);
79+
calculatedValues = (T)response.CalculatedValues;
80+
return response.IsValid;
81+
}
4282
}
4383
}

Assets/Scripts/SS3D/Systems/IngameConsoleSystem/Commands/DestroyBodyPartCommand.cs

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,71 +9,40 @@ namespace SS3D.Systems.IngameConsoleSystem.Commands
99
{
1010
public class DestroyBodyPartCommand : Command
1111
{
12-
public override string LongDescription => "Destroy a given body part, unattached from a player. \n " +
13-
"Usage : destroybodypart [game object name]";
12+
public override string LongDescription => "Destroy a given body part, unattached from a player";
1413
public override string ShortDescription => "Hit me daddy";
14+
public override string Usage => "(game object name)";
1515
public override ServerRoleTypes AccessLevel => ServerRoleTypes.Administrator;
1616
public override CommandType Type => CommandType.Server;
1717

18+
private record CalculatedValues(IEnumerable<BodyPart> BodyParts) : ICalculatedValues;
1819

1920
[Server]
2021
public override string Perform(string[] args, NetworkConnection conn = null)
2122
{
22-
CheckArgsResponse checkArgsResponse = CheckArgs(args);
23-
if (checkArgsResponse.IsValid == false)
24-
return checkArgsResponse.InvalidArgs;
23+
if (!ReceiveCheckResponse(args, out CheckArgsResponse response, out CalculatedValues values)) return response.InvalidArgs;
2524

26-
string gameObjectName = args[0];
27-
28-
GameObject go = GameObject.Find(gameObjectName);
29-
IEnumerable<BodyPart> bodyParts = go.GetComponentsInChildren<BodyPart>().Where(x => x.gameObject.name == gameObjectName);
30-
BodyPart bodyPart = bodyParts.First();
31-
32-
bodyPart.InflictDamageToAllLayer(new Health.DamageTypeQuantity(Health.DamageType.Heat, 10000000000));
25+
values.BodyParts.First().InflictDamageToAllLayer(new (Health.DamageType.Heat, 10000000000));
3326
return "BodyPart hurt";
3427
}
3528

3629
[Server]
3730
protected override CheckArgsResponse CheckArgs(string[] args)
3831
{
32+
CheckArgsResponse response = new();
3933

40-
CheckArgsResponse response = new CheckArgsResponse();
41-
42-
if (args.Length != 1)
43-
{
44-
response.IsValid = false;
45-
response.InvalidArgs = "Invalid number of arguments";
46-
return response;
47-
}
48-
34+
if (args.Length != 1) return response.MakeInvalid("Invalid number of arguments");
35+
4936
string gameObjectName = args[0];
50-
5137
GameObject go = GameObject.Find(gameObjectName);
52-
if (go == null)
53-
{
54-
response.IsValid = false;
55-
response.InvalidArgs = "No bodypart with this name";
56-
return response;
57-
}
58-
59-
IEnumerable<BodyPart> bodyParts = go.GetComponentsInChildren<BodyPart>().Where(x => x.gameObject.name == gameObjectName);
38+
if (go == null) return response.MakeInvalid("No bodypart with this name");
6039

61-
if (bodyParts.Count() == 0)
62-
{
63-
response.IsValid = false;
64-
response.InvalidArgs = "No bodypart with this name";
65-
return response;
66-
}
40+
BodyPart[] bodyParts = go.GetComponentsInChildren<BodyPart>().Where(x => x.gameObject.name == gameObjectName).ToArray();
41+
if (!bodyParts.Any()) return response.MakeInvalid("No bodypart with this name");
6742

68-
if (bodyParts.Count() != 1)
69-
{
70-
response.IsValid = false;
71-
response.InvalidArgs = "Multiple body parts with the same name, ambiguous command";
72-
return response;
73-
}
43+
if (bodyParts.Length != 1) return response.MakeInvalid("Multiple body parts with the same name, ambiguous command");
7444

75-
response.IsValid = true;
76-
return response;
45+
return response.MakeValid(new CalculatedValues(bodyParts));
7746
}
7847
}
7948
}

0 commit comments

Comments
 (0)