From 373b2c3d9ca571f09979c88b9f1e23be6445c3ed Mon Sep 17 00:00:00 2001 From: Rutherther Date: Fri, 20 Jan 2023 13:16:21 +0100 Subject: [PATCH] fix(game): make skills api safer by treating dash correctly and allowing only player skills --- .../Apis/Safe/NostaleSkillsApi.cs | 29 +++++++++++++++++++ .../Errors/WrongSkillPositionError.cs | 13 +++++++++ .../Errors/WrongSkillTypeError.cs | 13 +++++++++ 3 files changed, 55 insertions(+) create mode 100644 Core/NosSmooth.Game/Errors/WrongSkillPositionError.cs create mode 100644 Core/NosSmooth.Game/Errors/WrongSkillTypeError.cs diff --git a/Core/NosSmooth.Game/Apis/Safe/NostaleSkillsApi.cs b/Core/NosSmooth.Game/Apis/Safe/NostaleSkillsApi.cs index 2ad173e..6f24b19 100644 --- a/Core/NosSmooth.Game/Apis/Safe/NostaleSkillsApi.cs +++ b/Core/NosSmooth.Game/Apis/Safe/NostaleSkillsApi.cs @@ -80,6 +80,16 @@ public class NostaleSkillsApi return Task.FromResult(new WrongSkillTargetError(skill, character)); } + if (skill.Info.SkillType != SkillType.Player) + { + return Task.FromResult(new WrongSkillTypeError(SkillType.Player, skill.Info.SkillType)); + } + + if (skill.Info.AttackType == AttackType.Dash) + { + return Task.FromResult(new WrongSkillPositionError(skill.Info.AttackType)); + } + return _client.SendPacketAsync ( new UseSkillPacket @@ -143,6 +153,20 @@ public class NostaleSkillsApi return Task.FromResult(new WrongSkillTargetError(skill, entity)); } + if (skill.Info.SkillType != SkillType.Player) + { + return Task.FromResult(new WrongSkillTypeError(SkillType.Player, skill.Info.SkillType)); + } + + if (skill.Info.AttackType == AttackType.Dash && (mapX is null || mapY is null)) + { + return Task.FromResult(new WrongSkillPositionError(skill.Info.AttackType)); + } + else if (skill.Info.AttackType != AttackType.Dash && (mapX is not null || mapY is not null)) + { + return Task.FromResult(new WrongSkillPositionError(skill.Info.AttackType)); + } + var entityPosition = entity.Position; if (entityPosition is null) { @@ -285,6 +309,11 @@ public class NostaleSkillsApi return Task.FromResult(new WrongSkillTargetError(skill, null)); } + if (skill.Info.SkillType != SkillType.Player) + { + return Task.FromResult(new WrongSkillTypeError(SkillType.Player, skill.Info.SkillType)); + } + var characterPosition = _game.Character?.Position; if (characterPosition is null) { diff --git a/Core/NosSmooth.Game/Errors/WrongSkillPositionError.cs b/Core/NosSmooth.Game/Errors/WrongSkillPositionError.cs new file mode 100644 index 0000000..bbf4250 --- /dev/null +++ b/Core/NosSmooth.Game/Errors/WrongSkillPositionError.cs @@ -0,0 +1,13 @@ +// +// WrongSkillPositionError.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using NosSmooth.Data.Abstractions.Enums; +using Remora.Results; + +namespace NosSmooth.Game.Errors; + +public record WrongSkillPositionError(AttackType AttackType) + : ResultError($"The skill with an attack type {AttackType} has to have a map position specified."); \ No newline at end of file diff --git a/Core/NosSmooth.Game/Errors/WrongSkillTypeError.cs b/Core/NosSmooth.Game/Errors/WrongSkillTypeError.cs new file mode 100644 index 0000000..2b2635c --- /dev/null +++ b/Core/NosSmooth.Game/Errors/WrongSkillTypeError.cs @@ -0,0 +1,13 @@ +// +// WrongSkillTypeError.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using NosSmooth.Data.Abstractions.Enums; +using Remora.Results; + +namespace NosSmooth.Game.Errors; + +public record WrongSkillTypeError(SkillType ExpectedType, SkillType ActualType) + : ResultError($"Cannot use a skill of type {ActualType}, only type {ExpectedType} is allowed."); \ No newline at end of file -- 2.49.0