using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
#if UNITY_EDITOR
using Unity.VisualScripting.YamlDotNet.Core.Tokens;
#endif
using UnityEngine;
namespace XericLibrary.Runtime.MacroLibrary
{
///
/// 枚举宏,用于完成枚举项遍历,位操作等
///
public class MacroEnum
where T: struct, Enum
{
///
/// 反射组值
///
private Array valueArray;
///
/// 反射组名称
///
private string[] nameArray;
///
/// 构建一个枚举辅助器
///
public MacroEnum()
{
valueArray = typeof(T).GetEnumValues();
nameArray = typeof(T).GetEnumNames();
}
///
/// 获取列表
///
///
public IEnumerable GetValues()
{
foreach(var type in valueArray)
yield return (T)type;
}
///
/// 获取列表
///
///
public IEnumerable GetNames()
{
foreach(var type in nameArray)
yield return type;
}
///
/// 获取枚举项目
///
///
public IEnumerable<(string, T)> GetEnums()
{
var output = nameArray
.Zip(GetValues(), (name, value) => (name, value));
return output;
}
///
/// 设置目标的枚举值
///
///
///
public static void SetEnum(ref int target, params T[] values)
{
foreach(var value in values)
{
target |= Convert.ToInt32(value);
}
}
///
/// 设置目标的枚举值
///
///
///
public static void SetEnum(ref int target, T value)
{
target |= Convert.ToInt32(value);
}
///
/// 复位目标的枚举值
///
///
///
public static void ResetEnum(ref int target, params T[] values)
{
int temp = 0;
foreach(var value in values)
{
temp |= Convert.ToInt32(value);
}
target &= ~temp;
}
///
/// 复位目标的枚举值
///
///
///
public static void ResetEnum(ref int target, T value)
{
target &= ~Convert.ToInt32(value);
}
///
/// 翻转目标的枚举值
///
///
///
public static void FlipEnum(ref int target, params T[] values)
{
int temp = 0;
foreach(var value in values)
{
temp |= Convert.ToInt32(value);
}
target ^= temp;
}
///
/// 翻转目标的枚举值
///
///
///
public static void FlipEnum(ref int target, T value)
{
target ^= Convert.ToInt32(value);
}
///
/// 检查是否包含给定,且只关注是否完全包含
///
/// 要检查的特征
/// 过滤的特征
///
public static bool CheckEnum(T examineCC, T filterCC)
{
var cc = Convert.ToInt32(filterCC);
var isValid = Convert.ToInt32(examineCC) & cc;
var isFull = isValid ^ cc;
return isValid > 0 && isFull <= 0;
}
}
public static partial class MacroMath
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int EnumToInt32(this Enum target)
{
return Convert.ToInt32(target);
}
///
/// 枚举仅此有效
///
///
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool EnumJust(this Enum target, Enum value)
{
return target.EnumToInt32() == value.EnumToInt32();
}
///
/// 枚举至少存在一个有效的枚举项目
///
/// 返回是否存在至少有一个有效的枚举项目
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool EnumGreater(this Enum target)
{
return target.EnumToInt32() != 0;
}
///
/// 枚举至少存在一个有效的枚举项目
///
/// 返回是否存在至少有一个有效的枚举项目
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool EnumGreater(this Enum target, Enum value)
{
return (target.EnumToInt32() & value.EnumToInt32()) != 0;
}
///
/// 枚举1有效
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool EnumGreater1(this Enum target)
{
return (target.EnumToInt32() & 01) != 0;
}
///
/// 枚举2有效
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool EnumGreater2(this Enum target)
{
return (target.EnumToInt32() & 02) != 0;
}
///
/// 枚举3有效
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool EnumGreater3(this Enum target)
{
return (target.EnumToInt32() & 04) != 0;
}
///
/// 枚举4有效
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool EnumGreater4(this Enum target)
{
return (target.EnumToInt32() & 010) != 0;
}
///
/// 枚举5有效
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool EnumGreater5(this Enum target)
{
return (target.EnumToInt32() & 020) != 0;
}
///
/// 枚举6有效
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool EnumGreater6(this Enum target)
{
return (target.EnumToInt32() & 030) != 0;
}
///
/// 枚举7有效
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool EnumGreater7(this Enum target)
{
return (target.EnumToInt32() & 040) != 0;
}
///
/// 枚举8有效
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool EnumGreater8(this Enum target)
{
return (target.EnumToInt32() & 0100) != 0;
}
///
/// 枚举小于0,或者说不存在有效的枚举项目
///
/// 返回是否不存在有效的枚举项目
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool EnumLess(this Enum target)
{
return target.EnumToInt32() <= 0;
}
///
/// 枚举小于0,或者说不存在有效的枚举项目
///
/// 返回是否不存在有效的枚举项目
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool EnumLess(this Enum target, Enum value)
{
return (target.EnumToInt32() & value.EnumToInt32()) <= 0;
}
}
}