170 lines
7.0 KiB
C#
170 lines
7.0 KiB
C#
using System;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Runtime.InteropServices;
|
|
// ReSharper disable BuiltInTypeReferenceStyle
|
|
|
|
// The type we store a level in. As our implementation is aimed at normal Pokemon behaviour, a u8
|
|
// is probably enough, as we'd go up to 100. If you for some reason want to go higher, you can just
|
|
// change this type to hold a higher number.
|
|
using BackingLevelInt = System.Byte;
|
|
|
|
namespace PkmnLibSharp.StaticData
|
|
{
|
|
/// <summary>
|
|
/// The data structure used to store a level. This is a struct to allow for easy modification of the data type.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// If you want to change the type of the level, you can just change the type of <see cref="BackingLevelInt"/>.
|
|
/// Most of the implementation below is to allow for easy conversion between the two types, and to allow for
|
|
/// using the type as a normal integer.
|
|
/// </remarks>
|
|
[StructLayout(LayoutKind.Explicit)]
|
|
public struct LevelInt : IComparable, IComparable<BackingLevelInt>, IComparable<LevelInt>, IConvertible,
|
|
IEquatable<BackingLevelInt>, IEquatable<LevelInt>, IEquatable<int>, IFormattable
|
|
{
|
|
[FieldOffset(0)] private BackingLevelInt _value;
|
|
|
|
/// <summary>
|
|
/// Converts a <see cref="LevelInt"/> to a <see cref="BackingLevelInt"/>.
|
|
/// </summary>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static implicit operator BackingLevelInt(LevelInt l)
|
|
{
|
|
return l._value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a <see cref="BackingLevelInt"/> to a <see cref="LevelInt"/>.
|
|
/// </summary>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static implicit operator LevelInt(BackingLevelInt b)
|
|
{
|
|
return new LevelInt { _value = b };
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public int CompareTo(object obj)
|
|
{
|
|
if (obj is LevelInt l)
|
|
return _value.CompareTo(l._value);
|
|
return _value.CompareTo(obj);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public int CompareTo(BackingLevelInt other) => _value.CompareTo(other);
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public int CompareTo(LevelInt other) => _value.CompareTo(other._value);
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public TypeCode GetTypeCode() => Type.GetTypeCode(typeof(BackingLevelInt));
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public bool ToBoolean(IFormatProvider provider) => ((IConvertible)_value).ToBoolean(provider);
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public byte ToByte(IFormatProvider provider) => ((IConvertible)_value).ToByte(provider);
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public char ToChar(IFormatProvider provider) => ((IConvertible)_value).ToChar(provider);
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public DateTime ToDateTime(IFormatProvider provider) => ((IConvertible)_value).ToDateTime(provider);
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public decimal ToDecimal(IFormatProvider provider) => ((IConvertible)_value).ToDecimal(provider);
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public double ToDouble(IFormatProvider provider) => ((IConvertible)_value).ToDouble(provider);
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public short ToInt16(IFormatProvider provider) => ((IConvertible)_value).ToInt16(provider);
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public int ToInt32(IFormatProvider provider) => ((IConvertible)_value).ToInt32(provider);
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public long ToInt64(IFormatProvider provider) => ((IConvertible)_value).ToInt64(provider);
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public sbyte ToSByte(IFormatProvider provider) => ((IConvertible)_value).ToSByte(provider);
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public float ToSingle(IFormatProvider provider) => ((IConvertible)_value).ToSingle(provider);
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public string ToString(IFormatProvider provider) => _value.ToString(provider);
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public object ToType(Type conversionType, IFormatProvider provider) =>
|
|
((IConvertible)_value).ToType(conversionType, provider);
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public ushort ToUInt16(IFormatProvider provider) => ((IConvertible)_value).ToUInt16(provider);
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public uint ToUInt32(IFormatProvider provider) => ((IConvertible)_value).ToUInt32(provider);
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public ulong ToUInt64(IFormatProvider provider) => ((IConvertible)_value).ToUInt64(provider);
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public bool Equals(BackingLevelInt other) => _value == other;
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public string ToString(string format, IFormatProvider formatProvider) =>
|
|
_value.ToString(format, formatProvider);
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public bool Equals(LevelInt other) => _value == other._value;
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public bool Equals(int other) => _value == other;
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public override bool Equals(object? obj) => obj is LevelInt other && Equals(other);
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public override int GetHashCode()
|
|
{
|
|
return _value.GetHashCode();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Equality operator for <see cref="LevelInt"/>.
|
|
/// </summary>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static bool operator ==(LevelInt left, LevelInt right) => left.Equals(right);
|
|
|
|
/// <summary>
|
|
/// Inequality operator for <see cref="LevelInt"/>.
|
|
/// </summary>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static bool operator !=(LevelInt left, LevelInt right) => !left.Equals(right);
|
|
}
|
|
} |