Skip to content

Commit

Permalink
Merge with Game solution - Checkpoint #5 - FOV part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
pgeerkens committed Apr 8, 2019
1 parent 68b14d1 commit acec616
Show file tree
Hide file tree
Showing 26 changed files with 381 additions and 936 deletions.
71 changes: 20 additions & 51 deletions HexUtilities/Common/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,83 +1,52 @@
#region The MIT License - Copyright (C) 2012-2019 Pieter Geerkens
/////////////////////////////////////////////////////////////////////////////////////////
// PG Software Solutions - Hex-Grid Utilities
/////////////////////////////////////////////////////////////////////////////////////////
// The MIT License:
// ----------------
//
// Copyright (c) 2012-2019 Pieter Geerkens (email: pgeerkens@users.noreply.github.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify,
// merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
/////////////////////////////////////////////////////////////////////////////////////////
#region Copyright (c) 2012-2019 Pieter Geerkens (email: pgeerkens@users.noreply.github.com)
///////////////////////////////////////////////////////////////////////////////////////////
// THis software may be used under the terms of attached file License.md (The MIT License).
///////////////////////////////////////////////////////////////////////////////////////////
#endregion
using System;
using System.Collections.Generic;

using System.Diagnostics.CodeAnalysis;

namespace PGNapoleonics.HexUtilities.Common {
/// <summary>Type-safe extension methods for parsing Enums.</summary>
public static partial class EnumExtensions{
#region Enum Parsing utilities
/// <summary>Typesafe wrapper for <c>Enum.GetValues(typeof(TEnum).</c></summary>
public static IList<TEnum> EnumGetValues<TEnum>() {
return new List<TEnum>((TEnum[])(Enum.GetValues(typeof(TEnum)))).AsReadOnly();
}
public static IList<TEnum> EnumGetValues<TEnum>()
=> new List<TEnum>((TEnum[])(Enum.GetValues(typeof(TEnum)))).AsReadOnly();

/// <summary>TODO</summary>
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
[SuppressMessage("Microsoft.Design","CA1004:GenericMethodsShouldProvideTypeParameter")]
[Obsolete("Less useful or convenient than originally thought - just use Enum.GetNames({TEnum}).")]
public static IList<string> EnumGetNames<TEnum>() where TEnum : struct {
return new List<string>(Enum.GetNames(typeof(TEnum))).AsReadOnly();
}
public static IList<string> EnumGetNames<TEnum>() where TEnum : struct
=> new List<string>(Enum.GetNames(typeof(TEnum))).AsReadOnly();

/// <summary>Typesafe wrapper for <c>Enum.ParseEnum()</c> that automatically checks membership.</summary>
public static TEnum ParseEnum<TEnum>(string value) where TEnum : struct {
return ParseEnum<TEnum>(value,true,false);
}
public static TEnum ParseEnum<TEnum>(string value) where TEnum : struct
=> ParseEnum<TEnum>(value,true,false);

/// <summary>Typesafe wrapper for <c>Enum.ParseEnum()</c> that automatically checks membership.</summary>
public static TEnum ParseEnum<TEnum>(string value, bool checkConstants, bool ignoreCase) where TEnum : struct {
if(!TryParseEnum<TEnum>(value, ignoreCase, out var enumValue) && checkConstants)
throw new ArgumentOutOfRangeException("value", value, "Enum type: " + typeof(TEnum).Name);
public static TEnum ParseEnum<TEnum>(string value, bool checkConstants, bool ignoreCase)
where TEnum : struct {
if (!TryParseEnum<TEnum>(value,ignoreCase,out var enumValue) && checkConstants)
throw new ArgumentOutOfRangeException("value",value,"Enum type: " + typeof(TEnum).Name);

return enumValue;
}

/// <summary>Typesafe wrapper for <c>Enum.TryParseEnum()</c> that automatically checks membership.</summary>
public static bool TryParseEnum<TEnum>(string value, out TEnum enumValue) where TEnum : struct {
return TryParseEnum(value, false, out enumValue);
}
/// <summary>Typesafe wrapper for <c>Enum.TryParseEnum()</c> that automatically checks membership.</summary>
public static bool TryParseEnum<TEnum>(string value, bool ignoreCase, out TEnum enumValue) where TEnum : struct {
return Enum.TryParse(value, ignoreCase, out enumValue)
&& Enum.IsDefined(typeof(TEnum),enumValue);
}
public static bool TryParseEnum<TEnum>(string value,bool ignoreCase,out TEnum enumValue)
where TEnum : struct
=> Enum.TryParse(value,ignoreCase,out enumValue)
& Enum.IsDefined(typeof(TEnum),enumValue);

/// <summary>Typesafe wrapper for <c>Enum.ToObject()</c>.</summary>
/// <typeparam name="TEnum"></typeparam>
public static TEnum EnumParse<TEnum>(char c, string lookup) {
if (lookup==null) throw new ArgumentNullException("lookup");
var index = lookup.IndexOf(c);
if (index == -1) throw new ArgumentOutOfRangeException("c",c,"Enum Type: " + typeof(TEnum).Name);

return (TEnum) Enum.ToObject(typeof(TEnum), index);
}
#endregion
}
}
124 changes: 50 additions & 74 deletions HexUtilities/Common/EnumHelper.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,7 @@
#region The MIT License - Copyright (C) 2012-2019 Pieter Geerkens
/////////////////////////////////////////////////////////////////////////////////////////
// PG Software Solutions - Hex-Grid Utilities
/////////////////////////////////////////////////////////////////////////////////////////
// The MIT License:
// ----------------
//
// Copyright (c) 2012-2019 Pieter Geerkens (email: pgeerkens@users.noreply.github.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify,
// merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
/////////////////////////////////////////////////////////////////////////////////////////
#region Copyright (c) 2012-2019 Pieter Geerkens (email: pgeerkens@users.noreply.github.com)
///////////////////////////////////////////////////////////////////////////////////////////
// THis software may be used under the terms of attached file License.md (The MIT License).
///////////////////////////////////////////////////////////////////////////////////////////
#endregion
using System;
using System.Collections.Generic;
Expand All @@ -33,59 +10,58 @@
using System.Reflection;

namespace PGNapoleonics.HexUtilities.Common {
/// <summary>Extension methods for enums that avoid boxing.</summary>
public static class EnumHelper {
/// <summary>Determines whether one or more bit fields are set in the current instance; without boxing.</summary>
/// <remarks>Use <see cref="System.Enum.HasFlag"/> where CLS-Compliance is required.</remarks>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Hasflag")]
[CLSCompliant(false)]
public static bool TestBits<TEnum>(this TEnum bitField, TEnum bitsToTest) where TEnum : struct, IConvertible {
return EnumHelper<TEnum>.HasflagDelegate(bitField, bitsToTest);
/// <summary>Extension methods for enums that avoid boxing.</summary>
public static class EnumHelper {
/// <summary>Determines whether one or more bit fields are set in the current instance; without boxing.</summary>
/// <remarks>Use <see cref="Enum.HasFlag"/> where CLS-Compliance is required.</remarks>
[SuppressMessage("Microsoft.Naming","CA1704:IdentifiersShouldBeSpelledCorrectly",MessageId = "Hasflag")]
[CLSCompliant(false)]
public static bool TestBits<TEnum>(this TEnum bitField,TEnum bitsToTest)
where TEnum : struct, IConvertible
=> EnumHelper<TEnum>.HasflagDelegate(bitField,bitsToTest);

#region Utility methods for (all possible) base-types of an enumeration;
internal static bool HasFlag( sbyte item, sbyte bitFields) => (item & bitFields) != 0;
internal static bool HasFlag( byte item, byte bitFields) => (item & bitFields) != 0;
internal static bool HasFlag( short item, short bitFields) => (item & bitFields) != 0;
internal static bool HasFlag(ushort item, ushort bitFields) => (item & bitFields) != 0;
internal static bool HasFlag( int item, int bitFields) => (item & bitFields) != 0;
internal static bool HasFlag( uint item, uint bitFields) => (item & bitFields) != 0;
internal static bool HasFlag( long item, long bitFields) => (item & bitFields) != 0;
internal static bool HasFlag( ulong item, ulong bitFields) => (item & bitFields) != 0;
#endregion
}

#region Utility methods for (all possible) base-types of an enumeration;
internal static bool HasFlag( SByte item, SByte bitFields) { return (item & bitFields) != 0; }
internal static bool HasFlag( Byte item, Byte bitFields) { return (item & bitFields) != 0; }
internal static bool HasFlag( Int16 item, Int16 bitFields) { return (item & bitFields) != 0; }
internal static bool HasFlag(UInt16 item, UInt16 bitFields) { return (item & bitFields) != 0; }
internal static bool HasFlag( Int32 item, Int32 bitFields) { return (item & bitFields) != 0; }
internal static bool HasFlag(UInt32 item, UInt32 bitFields) { return (item & bitFields) != 0; }
internal static bool HasFlag( Int64 item, Int64 bitFields) { return (item & bitFields) != 0; }
internal static bool HasFlag(UInt64 item, UInt64 bitFields) { return (item & bitFields) != 0; }
#endregion
}
/// <summary>Support class for <see cref="EnumHelper"/>.</summary>
/// <typeparam name="TEnum"></typeparam>
internal static class EnumHelper<TEnum> where TEnum : struct, IConvertible {
/// <summary>Type-safe (and boxing-free) delegate supporting <see cref="EnumHelper.TestBits"/>.</summary>
public static readonly Func<TEnum,TEnum,bool> HasflagDelegate = GetHasflagDelegate();

/// <summary>Support class for <see cref="EnumHelper"/>.</summary>
/// <typeparam name="TEnum"></typeparam>
internal static class EnumHelper<TEnum> where TEnum : struct, IConvertible {
static readonly string HasFlag = "HasFlag";
private static string HasFlag => "HasFlag";

/// <summary>Type-safe (and boxing-free) delegate supporting <see cref="EnumHelper.TestBits"/>.</summary>
public static readonly Func<TEnum,TEnum,bool> HasflagDelegate = GetHasflagDelegate();
/// <summary>Creates and returns a type-safe Hasflag delegate.</summary>
private static Func<TEnum,TEnum,bool> GetHasflagDelegate() {
const BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Static;

/// <summary>Creates and returns a type-safe Hasflag delegate.</summary>
private static Func<TEnum,TEnum,bool> GetHasflagDelegate() {
const BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Static;
var baseType = EnumBaseType(typeof(TEnum));
var baseTypes = new Type[] { baseType, baseType };
var method = typeof(EnumHelper).GetMethod(HasFlag, bindingFlags, null, baseTypes, null);
if (method == null) throw new MissingMethodException(typeof(TEnum).Name,HasFlag);

var baseType = EnumBaseType(typeof(TEnum));
var baseTypes = new Type[] { baseType, baseType };
var method = typeof(EnumHelper).GetMethod(HasFlag, bindingFlags, null, baseTypes, null);
if (method == null) throw new MissingMethodException(typeof(TEnum).Name,HasFlag);
return (Func<TEnum,TEnum,bool>)method.CreateDelegate(typeof(Func<TEnum, TEnum, bool>));
}
private static Type EnumBaseType(Type enumType) {
if ( ! enumType.IsEnum ) throw new MissingMethodException(typeof(TEnum).Name,HasFlag);
var attributes = enumType.GetCustomAttributesData() ?? new List<CustomAttributeData>();
if ( ! IsFlagsEnum(attributes)) throw new MissingMethodException(typeof(TEnum).Name,HasFlag);
return Enum.GetUnderlyingType(enumType);
}

return (Func<TEnum,TEnum,bool>)method.CreateDelegate(typeof(Func<TEnum, TEnum, bool>));
}
private static Type EnumBaseType(Type enumType) {
if ( ! enumType.IsEnum ) throw new MissingMethodException(typeof(TEnum).Name,HasFlag);
var attributes = enumType.GetCustomAttributesData() ?? new List<CustomAttributeData>();
if ( ! IsFlagsEnum(attributes)) throw new MissingMethodException(typeof(TEnum).Name,HasFlag);
return Enum.GetUnderlyingType(enumType);
}
private static bool IsFlagsEnum(IList<CustomAttributeData> attributes) {
return (from attribute in attributes select IsFlagsAttribute(attribute)
).FirstOrDefault(); //b => b);
}
private static bool IsFlagsAttribute(CustomAttributeData attribute) {
return attribute.AttributeType.FullName == "System.FlagsAttribute";
private static bool IsFlagsEnum(IList<CustomAttributeData> attributes)
=> attributes.Select(a => IsFlagsAttribute(a)).FirstOrDefault(b => b);

private static bool IsFlagsAttribute(CustomAttributeData attribute)
=> attribute.AttributeType.FullName == "System.FlagsAttribute";
}
}
}
31 changes: 4 additions & 27 deletions HexUtilities/Common/EventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,7 @@
#region The MIT License - Copyright (C) 2012-2019 Pieter Geerkens
/////////////////////////////////////////////////////////////////////////////////////////
// PG Software Solutions - Hex-Grid Utilities
/////////////////////////////////////////////////////////////////////////////////////////
// The MIT License:
// ----------------
//
// Copyright (c) 2012-2019 Pieter Geerkens (email: pgeerkens@users.noreply.github.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify,
// merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
/////////////////////////////////////////////////////////////////////////////////////////
#region Copyright (c) 2012-2019 Pieter Geerkens (email: pgeerkens@users.noreply.github.com)
///////////////////////////////////////////////////////////////////////////////////////////
// THis software may be used under the terms of attached file License.md (The MIT License).
///////////////////////////////////////////////////////////////////////////////////////////
#endregion
using System;

Expand Down
31 changes: 4 additions & 27 deletions HexUtilities/Common/IShadingMask.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,7 @@
#region The MIT License - Copyright (C) 2012-2019 Pieter Geerkens
/////////////////////////////////////////////////////////////////////////////////////////
// PG Software Solutions - Hex-Grid Utilities
/////////////////////////////////////////////////////////////////////////////////////////
// The MIT License:
// ----------------
//
// Copyright (c) 2012-2019 Pieter Geerkens (email: pgeerkens@users.noreply.github.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify,
// merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
/////////////////////////////////////////////////////////////////////////////////////////
#region Copyright (c) 2012-2019 Pieter Geerkens (email: pgeerkens@users.noreply.github.com)
///////////////////////////////////////////////////////////////////////////////////////////
// THis software may be used under the terms of attached file License.md (The MIT License).
///////////////////////////////////////////////////////////////////////////////////////////
#endregion
using System.Diagnostics.CodeAnalysis;

Expand Down
Loading

0 comments on commit acec616

Please sign in to comment.