Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some new features that I created at apis-rework #168

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions EXILED/EXILED.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Exiled.CreditTags", "Exiled
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Exiled.CustomRoles", "Exiled.CustomRoles\Exiled.CustomRoles.csproj", "{417C3309-8B93-4218-A1D1-D4BB7B09BE0F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Exiled.CustomUnits", "Exiled.CustomUnits\Exiled.CustomUnits.csproj", "{9C4931B5-FB58-4069-815D-02C9FD8BEC05}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -74,6 +76,12 @@ Global
{417C3309-8B93-4218-A1D1-D4BB7B09BE0F}.Installer|Any CPU.ActiveCfg = Installer|Any CPU
{417C3309-8B93-4218-A1D1-D4BB7B09BE0F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{417C3309-8B93-4218-A1D1-D4BB7B09BE0F}.Release|Any CPU.Build.0 = Release|Any CPU
{9C4931B5-FB58-4069-815D-02C9FD8BEC05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9C4931B5-FB58-4069-815D-02C9FD8BEC05}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9C4931B5-FB58-4069-815D-02C9FD8BEC05}.Installer|Any CPU.ActiveCfg = Debug|Any CPU
{9C4931B5-FB58-4069-815D-02C9FD8BEC05}.Installer|Any CPU.Build.0 = Debug|Any CPU
{9C4931B5-FB58-4069-815D-02C9FD8BEC05}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9C4931B5-FB58-4069-815D-02C9FD8BEC05}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
16 changes: 16 additions & 0 deletions EXILED/Exiled.API/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Exiled.API.Extensions
using System.Text;
using System.Text.RegularExpressions;

using Exiled.API.Features;
using Exiled.API.Features.Pools;

/// <summary>
Expand Down Expand Up @@ -74,6 +75,21 @@ public static (string commandName, string[] arguments) ExtractCommand(this strin
return (extractedArguments[0].ToLower(), extractedArguments.Skip(1).ToArray());
}

/// <summary>
/// Parse players from a <see cref="string"/>.
/// </summary>
/// <param name="query">Query to be parsed.</param>
/// <param name="separator">Separator to be used in the query.</param>
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="Player"/> if any found, otherwise an empty <see cref="IEnumerable{T}"/>.</returns>
public static IEnumerable<Player> ParsePlayers(this string query, char separator = '.')
{
foreach (string str in query.Split(separator))
{
if (Player.TryGet(str, out Player player))
yield return player;
}
}

/// <summary>
/// Converts a <see cref="string"/> to snake_case convention.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// -----------------------------------------------------------------------
// <copyright file="CustomValidatorAttribute.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.API.Features.Attributes.Config
{
using System;

/// <summary>
/// A custom validator for config values and base class for all config validators.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class CustomValidatorAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="CustomValidatorAttribute"/> class.
/// </summary>
/// <param name="validator"><inheritdoc cref="Validator"/></param>
public CustomValidatorAttribute(Func<object, bool> validator)
{
Validator = validator;
}

/// <summary>
/// Gets a function that validates an object.
/// </summary>
public Func<object, bool> Validator { get; }

/// <summary>
/// Validates an object.
/// </summary>
/// <param name="obj">Object to validate.</param>
/// <returns><c>true</c> if validation was successful, <c>false</c> otherwise.</returns>
public bool Validate(object obj) => Validator(obj);
}
}
28 changes: 28 additions & 0 deletions EXILED/Exiled.API/Features/Attributes/Config/MaxLengthAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// -----------------------------------------------------------------------
// <copyright file="MaxLengthAttribute.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.API.Features.Attributes.Config
{
using System.Collections;
using System.Linq;

/// <summary>
/// An attribute that checks if length of a sequence is less than a specified value.
/// </summary>
public class MaxLengthAttribute : CustomValidatorAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="MaxLengthAttribute"/> class.
/// </summary>
/// <param name="length">Maximum length of sequence.</param>
/// <param name="inclusive">Whether check is inclusive or not.</param>
public MaxLengthAttribute(int length, bool inclusive = false)
: base(x => x is IEnumerable enumerable && enumerable.Cast<object>().Count() < (inclusive ? length + 1 : length))
{
}
}
}
27 changes: 27 additions & 0 deletions EXILED/Exiled.API/Features/Attributes/Config/MaxValueAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// -----------------------------------------------------------------------
// <copyright file="MaxValueAttribute.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.API.Features.Attributes.Config
{
using System.Collections;

/// <summary>
/// An attribute to check if a value is less than a specified value.
/// </summary>
public class MaxValueAttribute : CustomValidatorAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="MaxValueAttribute"/> class.
/// </summary>
/// <param name="maxValue">Maximum value.</param>
/// <param name="inclusive">Whether check should be inclusive or not.</param>
public MaxValueAttribute(object maxValue, bool inclusive = true)
: base(x => Comparer.Default.Compare(maxValue, x) > (inclusive ? -1 : 0))
{
}
}
}
28 changes: 28 additions & 0 deletions EXILED/Exiled.API/Features/Attributes/Config/MinLengthAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// -----------------------------------------------------------------------
// <copyright file="MinLengthAttribute.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.API.Features.Attributes.Config
{
using System.Collections;
using System.Linq;

/// <summary>
/// An attribute that checks if length of a sequence is greater than a specified value.
/// </summary>
public class MinLengthAttribute : CustomValidatorAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="MinLengthAttribute"/> class.
/// </summary>
/// <param name="length">Maximum length of sequence.</param>
/// <param name="inclusive">Whether check is inclusive or not.</param>
public MinLengthAttribute(int length, bool inclusive = false)
: base(x => x is IEnumerable enumerable && enumerable.Cast<object>().Count() > (inclusive ? length - 1 : length))
{
}
}
}
27 changes: 27 additions & 0 deletions EXILED/Exiled.API/Features/Attributes/Config/MinValueAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// -----------------------------------------------------------------------
// <copyright file="MinValueAttribute.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.API.Features.Attributes.Config
{
using System.Collections;

/// <summary>
/// An attribute to check if value is greater than or equal to min value.
/// </summary>
public class MinValueAttribute : CustomValidatorAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="MinValueAttribute"/> class.
/// </summary>
/// <param name="minValue">Minimum value.</param>
/// <param name="inclusive">Whether check should be inclusive or not.</param>
public MinValueAttribute(object minValue, bool inclusive = true)
: base(x => Comparer.Default.Compare(x, minValue) > (inclusive ? -1 : 0))
{
}
}
}
26 changes: 26 additions & 0 deletions EXILED/Exiled.API/Features/Attributes/Config/OneOfAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// -----------------------------------------------------------------------
// <copyright file="OneOfAttribute.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.API.Features.Attributes.Config
{
using System;

/// <summary>
/// An attribute to check if a value is one of a possible values.
/// </summary>
public class OneOfAttribute : CustomValidatorAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="OneOfAttribute"/> class.
/// </summary>
/// <param name="values">Values.</param>
public OneOfAttribute(params object[] values)
: base(x => Array.IndexOf(values, x) != -1)
{
}
}
}
19 changes: 19 additions & 0 deletions EXILED/Exiled.API/Features/Attributes/CustomTeamAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// -----------------------------------------------------------------------
// <copyright file="CustomTeamAttribute.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.API.Features.Attributes
{
using System;

/// <summary>
/// An attribute to easily manage custom teams initialization.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public sealed class CustomTeamAttribute : Attribute
{
}
}
Loading