Files
Fishing2/Assets/Scripts/Commands/Base/CommandArgs.cs
2025-05-10 12:49:47 +08:00

89 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace NBF
{
public struct CommandArgsStruct
{
public override bool Equals(object obj)
{
return obj is CommandArgsStruct other && Equals(other);
}
public override int GetHashCode()
{
return (Value != null ? Value.GetHashCode() : 0);
}
private string Value { get; set; }
public CommandArgsStruct(string val = "")
{
Value = val;
}
public bool Equals(CommandArgsStruct other)
{
return Value == other.Value;
}
public static bool operator ==(CommandArgsStruct a, CommandArgsStruct b) => a.Value == b.Value;
public static bool operator !=(CommandArgsStruct a, CommandArgsStruct b) => a.Value != b.Value;
public override string ToString() => Value;
public int ToInt()
{
return int.TryParse(Value, out var i) ? i : 0;
}
public float ToFloat()
{
return float.TryParse(Value, out var i) ? i : 0;
}
public T[] ToArr<T>(string split = ",") where T : IConvertible
{
var arr = Value.Split(split);
return arr.Select(a => (T)Convert.ChangeType(a, typeof(T))).ToArray();
}
}
public class CommandArgs
{
public string Command { get; private set; }
private readonly List<CommandArgsStruct> _args = new List<CommandArgsStruct>();
public CommandArgs(string str)
{
_args.Clear();
var args = str.Split(' ');
for (var i = 0; i < args.Length; i++)
{
if (i == 0)
{
Command = args[i];
}
else
{
_args.Add(new CommandArgsStruct(args[i]));
}
}
}
public CommandArgsStruct this[int index] => Get(index);
public CommandArgsStruct Get(int index)
{
if (index < _args.Count && index >= 0)
{
return _args[index];
}
return new CommandArgsStruct();
}
}
}