Proposed Solution for #674 Permissions Changes (#743)

* Initial commit of changes. Changed permissions from bitwise index to use bitwise flags instead. Modified relevant methods involved

* Revised enum value naming

* Added FlagsAttribute to ChannelPermission, GuildPermission

* Added comments per Joe4evr suggestion

* Added underlines to hex value digits for readability per Joe4evr suggestion

* updated names to better reflect actual permission names as per SubZero0 suggestion

* fix for 236775c2d8aca9481d6c51c674f5727f97adec04

* Replaced Math.Pow with left shift operator

* Cleaned up the formatting of ChannelPermission and GuildPermission enums to make it easier to read
This commit is contained in:
Chris Johnston
2017-10-25 17:39:26 -07:00
committed by Christopher F
parent 759db34146
commit f9963380a7
6 changed files with 153 additions and 145 deletions

View File

@@ -7,84 +7,84 @@ namespace Discord
public const int MaxBits = 53;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static PermValue GetValue(ulong allow, ulong deny, ChannelPermission bit)
=> GetValue(allow, deny, (byte)bit);
public static PermValue GetValue(ulong allow, ulong deny, ChannelPermission flag)
=> GetValue(allow, deny, (ulong)flag);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static PermValue GetValue(ulong allow, ulong deny, GuildPermission bit)
=> GetValue(allow, deny, (byte)bit);
public static PermValue GetValue(ulong allow, ulong deny, GuildPermission flag)
=> GetValue(allow, deny, (ulong)flag);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static PermValue GetValue(ulong allow, ulong deny, byte bit)
public static PermValue GetValue(ulong allow, ulong deny, ulong flag)
{
if (HasBit(allow, bit))
if (HasFlag(allow, flag))
return PermValue.Allow;
else if (HasBit(deny, bit))
else if (HasFlag(deny, flag))
return PermValue.Deny;
else
return PermValue.Inherit;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool GetValue(ulong value, ChannelPermission bit)
=> GetValue(value, (byte)bit);
public static bool GetValue(ulong value, ChannelPermission flag)
=> GetValue(value, (ulong)flag);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool GetValue(ulong value, GuildPermission bit)
=> GetValue(value, (byte)bit);
public static bool GetValue(ulong value, GuildPermission flag)
=> GetValue(value, (ulong)flag);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool GetValue(ulong value, byte bit) => HasBit(value, bit);
public static bool GetValue(ulong value, ulong flag) => HasFlag(value, flag);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetValue(ref ulong rawValue, bool? value, ChannelPermission bit)
=> SetValue(ref rawValue, value, (byte)bit);
public static void SetValue(ref ulong rawValue, bool? value, ChannelPermission flag)
=> SetValue(ref rawValue, value, (ulong)flag);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetValue(ref ulong rawValue, bool? value, GuildPermission bit)
=> SetValue(ref rawValue, value, (byte)bit);
public static void SetValue(ref ulong rawValue, bool? value, GuildPermission flag)
=> SetValue(ref rawValue, value, (ulong)flag);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetValue(ref ulong rawValue, bool? value, byte bit)
public static void SetValue(ref ulong rawValue, bool? value, ulong flag)
{
if (value.HasValue)
{
if (value == true)
SetBit(ref rawValue, bit);
SetFlag(ref rawValue, flag);
else
UnsetBit(ref rawValue, bit);
UnsetFlag(ref rawValue, flag);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetValue(ref ulong allow, ref ulong deny, PermValue? value, ChannelPermission bit)
=> SetValue(ref allow, ref deny, value, (byte)bit);
public static void SetValue(ref ulong allow, ref ulong deny, PermValue? value, ChannelPermission flag)
=> SetValue(ref allow, ref deny, value, (ulong)flag);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetValue(ref ulong allow, ref ulong deny, PermValue? value, GuildPermission bit)
=> SetValue(ref allow, ref deny, value, (byte)bit);
public static void SetValue(ref ulong allow, ref ulong deny, PermValue? value, GuildPermission flag)
=> SetValue(ref allow, ref deny, value, (ulong)flag);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetValue(ref ulong allow, ref ulong deny, PermValue? value, byte bit)
public static void SetValue(ref ulong allow, ref ulong deny, PermValue? value, ulong flag)
{
if (value.HasValue)
{
switch (value)
{
case PermValue.Allow:
SetBit(ref allow, bit);
UnsetBit(ref deny, bit);
SetFlag(ref allow, flag);
UnsetFlag(ref deny, flag);
break;
case PermValue.Deny:
UnsetBit(ref allow, bit);
SetBit(ref deny, bit);
UnsetFlag(ref allow, flag);
SetFlag(ref deny, flag);
break;
default:
UnsetBit(ref allow, bit);
UnsetBit(ref deny, bit);
UnsetFlag(ref allow, flag);
UnsetFlag(ref deny, flag);
break;
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool HasBit(ulong value, byte bit) => (value & (1U << bit)) != 0;
private static bool HasFlag(ulong value, ulong flag) => (value & flag) != 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetBit(ref ulong value, byte bit) => value |= (1U << bit);
public static void SetFlag(ref ulong value, ulong flag) => value |= flag;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void UnsetBit(ref ulong value, byte bit) => value &= ~(1U << bit);
public static void UnsetFlag(ref ulong value, ulong flag) => value &= ~flag;
public static ChannelPermissions ToChannelPerms(IGuildChannel channel, ulong guildPermissions)
=> new ChannelPermissions(guildPermissions & ChannelPermissions.All(channel).RawValue);