-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
300 split freeecore into a hierarchy of projects take2 (#314)
* Split out some utility code into a FrEee.Core.Utility project * Rename FrEee project to FrEee.Core (finally!) * Clarify purpose of FrEee.Core.Utility project * Delete an extraneous quote * Move combat from Objects to Processes * Move Parser and some extension methods to FrEee.Core.Utility * Delete some unused extension methods * Move AI from Objects to Processes * Move some galaxy/mod reference type stuff to appropriate places * Delete duplicate class * Move abilities from Objects to Modding * Move AbilityExtensions to Modding.Abilities * Move game setup to Processes * Move MiningModle to FrEee.Core, it doesn't reference game objects but it kind of is one..
- Loading branch information
Showing
545 changed files
with
23,133 additions
and
22,834 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Dynamic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.Serialization; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using FrEee.Extensions; | ||
using FrEee.Extensions; | ||
using FrEee.Utility; | ||
|
||
namespace FrEee.Extensions; | ||
|
||
public static class TypeExtensions | ||
{ | ||
private static SafeDictionary<MemberInfo, IEnumerable<Attribute>> attributeCache = new SafeDictionary<MemberInfo, IEnumerable<Attribute>>(); | ||
|
||
private static SafeDictionary<Type, IEnumerable<Type>> interfaceCache = new SafeDictionary<Type, IEnumerable<Type>>(); | ||
|
||
private static SafeDictionary<Type, IEnumerable<MemberInfo>> memberCache = new SafeDictionary<Type, IEnumerable<MemberInfo>>(); | ||
|
||
public static object Instantiate(this Type type, params object[] args) | ||
{ | ||
if (type.GetConstructors().Where(c => c.GetParameters().Length == (args == null ? 0 : args.Length)).Any()) | ||
return Activator.CreateInstance(type, args) ?? throw new NullReferenceException($"Couldn't create instance of type {type}."); | ||
else | ||
return FormatterServices.GetSafeUninitializedObject(type); | ||
} | ||
|
||
public static T Instantiate<T>(params object[] args) | ||
{ | ||
return (T)typeof(T).Instantiate(args); | ||
} | ||
|
||
/// <summary> | ||
/// Equals method that doesn't throw an exception when objects are null. | ||
/// Null is not equal to anything else, except other nulls. | ||
/// </summary> | ||
/// <param name="o1"></param> | ||
/// <param name="o2"></param> | ||
/// <returns></returns> | ||
public static bool SafeEquals(this object o1, object o2) | ||
{ | ||
if (o1 == null && o2 == null) | ||
return true; | ||
if (o1 == null || o2 == null) | ||
return false; | ||
return o1.Equals(o2); | ||
} | ||
|
||
public static bool SafeSequenceEqual<T>(this IEnumerable<T> e1, IEnumerable<T> e2) | ||
{ | ||
if (e1.SafeEquals(null) && e2.SafeEquals(null)) | ||
return true; | ||
if (e1.SafeEquals(null) || e2.SafeEquals(null)) | ||
return false; | ||
return e1.SequenceEqual(e2); | ||
} | ||
|
||
/// <summary> | ||
/// Checks for attributes in a class or its interfaces. | ||
/// </summary> | ||
/// <param name="mi"></param> | ||
/// <param name="attributeType"></param> | ||
/// <returns></returns> | ||
public static bool HasAttribute<T>(this MemberInfo mi) | ||
{ | ||
return mi.HasAttribute(typeof(T)); | ||
} | ||
|
||
/// <summary> | ||
/// Checks for attributes in a class or its interfaces. | ||
/// </summary> | ||
/// <param name="mi"></param> | ||
/// <param name="attributeType"></param> | ||
/// <returns></returns> | ||
public static bool HasAttribute(this MemberInfo mi, Type attributeType, bool checkInterfaces = true) | ||
{ | ||
if (attributeCache[mi] == null) | ||
attributeCache[mi] = Attribute.GetCustomAttributes(mi).ToArray(); | ||
if (attributeCache[mi].Where(a => attributeType.IsAssignableFrom(a.GetType())).Any()) | ||
return true; | ||
var dt = mi is Type ? mi as Type : mi.DeclaringType; | ||
if (checkInterfaces) | ||
{ | ||
if (interfaceCache[dt] == null) | ||
interfaceCache[dt] = dt.GetInterfaces(); | ||
foreach (var i in interfaceCache[dt]) | ||
{ | ||
if (memberCache[i] == null) | ||
memberCache[i] = i.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).ToArray(); // TODO - refactor into method | ||
if (memberCache[i].Any(m => m.Name == mi.Name && m.MemberType == mi.MemberType && m.HasAttribute(attributeType, false))) // no need to check interfaces of interfaces, they're already listed by GetInterfaces | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public static bool HasProperty(this ExpandoObject obj, string propertyName) | ||
{ | ||
return obj.GetType().GetProperty(propertyName) != null; | ||
} | ||
|
||
/// <summary> | ||
/// Checks for attributes in a class or its interfaces. | ||
/// </summary> | ||
/// <param name="mi"></param> | ||
/// <param name="attributeType"></param> | ||
/// <returns></returns> | ||
public static IEnumerable<T> GetAttributes<T>(this MemberInfo mi) where T : Attribute | ||
{ | ||
if (attributeCache[mi] == null) | ||
attributeCache[mi] = Attribute.GetCustomAttributes(mi).ToArray(); | ||
var atts = attributeCache[mi].OfType<T>(); | ||
foreach (var att in atts) | ||
yield return att; | ||
if (interfaceCache[mi.DeclaringType] == null) | ||
interfaceCache[mi.DeclaringType] = mi.DeclaringType.GetInterfaces(); | ||
foreach (var i in interfaceCache[mi.DeclaringType]) | ||
{ | ||
if (memberCache[i] == null) | ||
memberCache[i] = i.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).ToArray(); // TODO - refactor into method | ||
var mi2 = memberCache[i].SingleOrDefault(x => x.MemberType == mi.MemberType && x.Name == mi.Name); | ||
if (mi2 != null) | ||
{ | ||
foreach (var att2 in mi2.GetAttributes<T>()) | ||
yield return att2; | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets all names for a property, class, etc. including custom names and the actual item name. | ||
/// </summary> | ||
/// <param name="m"></param> | ||
/// <returns></returns> | ||
public static IEnumerable<string> GetNames(this MemberInfo m) | ||
{ | ||
return m.GetAttributes<NameAttribute>().Select(a => a.Name).UnionSingle(m.Name); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the canonical name for a property, class, etc. | ||
/// This is taken from the [CanonicalName] attribute if present, otherwise the name of the item itself. | ||
/// </summary> | ||
/// <param name="m"></param> | ||
/// <returns></returns> | ||
public static string GetCanonicalName(this MemberInfo m) | ||
{ | ||
// TODO - use most derived class's attribute? | ||
var name = m.GetAttributes<CanonicalNameAttribute>().Select(a => a.Name).SingleOrDefault(); | ||
if (name == null) | ||
return m.Name; | ||
return name; | ||
} | ||
|
||
/// <summary> | ||
/// Gets a property value from an object using reflection. | ||
/// If the property does not exist, returns null. | ||
/// </summary> | ||
/// <param name="o"></param> | ||
/// <param name="propertyName"></param> | ||
/// <returns></returns> | ||
public static object GetPropertyValue(this object o, string propertyName) | ||
{ | ||
if (o == null) | ||
return null; | ||
var prop = o.GetType().GetProperty(propertyName); | ||
if (prop == null) | ||
return null; | ||
return prop.GetValue(o, new object[0]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<OutputType>Library</OutputType> | ||
<AssemblyName>FrEee.Core.Utility</AssemblyName> | ||
<Description>Utility code for FrEee. This is low level utility code that doesn't need to know about any game objects.</Description> | ||
</PropertyGroup> | ||
|
||
<Import Project="../FrEee.Assets/CommonProjectProperties.xml" /> | ||
|
||
<PropertyGroup> | ||
<EnableSourceControlManagerQueries>false</EnableSourceControlManagerQueries> | ||
<RootNamespace>FrEee</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> | ||
<PackageReference Include="System.ComponentModel.Composition" Version="8.0.0" /> | ||
</ItemGroup> | ||
</Project> |
22 changes: 22 additions & 0 deletions
22
FrEee.Core.Utility/Serialization/DoNotSerializeAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using FrEee.Utility; | ||
using FrEee.Serialization; | ||
|
||
namespace FrEee.Serialization | ||
{ | ||
/// <summary> | ||
/// Prevents a property from being serialized, or copied when the containing object is copied. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)] | ||
public sealed class DoNotSerializeAttribute : DoNotCopyAttribute | ||
{ | ||
public DoNotSerializeAttribute(bool allowSafeCopy = true) | ||
: base(allowSafeCopy) | ||
{ | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
FrEee/Serialization/IData.cs → FrEee.Core.Utility/Serialization/IData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using Newtonsoft.Json; | ||
using FrEee.Serialization; | ||
|
||
namespace FrEee.Serialization; | ||
|
||
|
3 changes: 3 additions & 0 deletions
3
FrEee/Serialization/IDataObject.cs → ...Core.Utility/Serialization/IDataObject.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
FrEee/Serialization/IReferenceEnumerable.cs → ...ity/Serialization/IReferenceEnumerable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
namespace FrEee.Serialization; | ||
using FrEee.Serialization; | ||
namespace FrEee.Serialization; | ||
|
||
/// <summary> | ||
/// Flag interface for enumerables that should not be serialized as enumerables because they contain references. | ||
/// </summary> | ||
public interface IReferenceEnumerable | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...erialization/Stringifiers/IStringifier.cs → ...erialization/Stringifiers/IStringifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...lization/Stringifiers/PointStringifier.cs → ...lization/Stringifiers/PointStringifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...alization/Stringifiers/SizeStringifier.cs → ...alization/Stringifiers/SizeStringifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...Serialization/Stringifiers/Stringifier.cs → ...Serialization/Stringifiers/Stringifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...zation/Stringifiers/StringifierLibrary.cs → ...zation/Stringifiers/StringifierLibrary.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.