Skip to content

Commit

Permalink
300 split freeecore into a hierarchy of projects take2 (#314)
Browse files Browse the repository at this point in the history
* 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
ekolis authored Jun 9, 2024
1 parent 6b2e70e commit d23739e
Show file tree
Hide file tree
Showing 545 changed files with 23,133 additions and 22,834 deletions.
2 changes: 1 addition & 1 deletion FrEee.Assets/CommonProjectProperties.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Product>FrEee</Product>
<Version>0.0.9</Version>
<Copyright>Copyright © 2013-2024</Copyright>
<ApplicationIcon>FrEee.ico</ApplicationIcon>
<ApplicationIcon>../FrEee.Assets/FrEee.ico</ApplicationIcon>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
6 changes: 3 additions & 3 deletions FrEee.Assets/GameSetups/Quickstart.gsu
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FrEee.Setup.GameSetup, FrEee.Core:
FrEee.Processes.Setup.GameSetup, FrEee.Core:
p31:
AllowedTrades:
:All;
Expand All @@ -7,7 +7,7 @@ p31:
EmpirePoints:
:2000;
EmpireTemplates:
System.Collections.Generic.List`1[[FrEee.Setup.EmpireTemplate, FrEee.Core]], System.Private.CoreLib:
System.Collections.Generic.List`1[[FrEee.Processes.Setup.EmpireTemplate, FrEee.Core]], System.Private.CoreLib:
c1:
:p10:
AIName:
Expand Down Expand Up @@ -181,7 +181,7 @@ p31:
;
;
WarpPointPlacementStrategy:
FrEee.Setup.WarpPointPlacementStrategies.EdgeAlignedWarpPointPlacementStrategy, FrEee.Core:
FrEee.Processes.Setup.WarpPointPlacementStrategies.EdgeAlignedWarpPointPlacementStrategy, FrEee.Core:
p2:
Description:
:"Places warp points along the edge of the system, aligned with the star systems they lead to.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FrEee.Extensions;
using FrEee.Utility;

namespace FrEee.Extensions;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
using System.Linq;
using System.Numerics;
using FrEee.Utility;
using FrEee.Extensions;
using FrEee.Extensions;
using FrEee.Utility;
using FrEee.Extensions;
namespace FrEee.Extensions;

public static class MathExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using System.Reflection;
using FrEee.Utility;
using FrEee.Extensions;
using FrEee.Extensions;
namespace FrEee.Extensions;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Linq;
using FrEee.Extensions;
using FrEee.Extensions;

namespace FrEee.Extensions;

Expand Down Expand Up @@ -90,4 +92,4 @@ public static string Possessive(this string s, bool isStart = false)
return s + "'";
return s + "'s";
}
}
}
174 changes: 174 additions & 0 deletions FrEee.Core.Utility/Extensions/TypeExtensions.cs
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]);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FrEee.Extensions;
using FrEee.Extensions;
using FrEee.Extensions;

namespace FrEee.Extensions;

Expand Down Expand Up @@ -247,4 +250,4 @@ public static string ToUnitString(this double? value, bool bForBillions = false,
return undefinedValue;
return value.Value.ToUnitString(bForBillions, sigfigs);
}
}
}
20 changes: 20 additions & 0 deletions FrEee.Core.Utility/FrEee.Core.Utility.csproj
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 FrEee.Core.Utility/Serialization/DoNotSerializeAttribute.cs
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)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using FrEee.Serialization;

namespace FrEee.Serialization;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using FrEee.Utility;
using FrEee.Serialization;
using FrEee.Serialization;
using FrEee.Utility;
namespace FrEee.Serialization;

/// <summary>
Expand Down
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
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System;
using System.Linq;
using System.Reflection;
using FrEee.Serialization;
using FrEee.Serialization;

namespace FrEee.Serialization;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using FrEee.Serialization.Stringifiers;
using FrEee.Serialization.Stringifiers;

namespace FrEee.Serialization.Stringifiers;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.ComponentModel.Composition;
using System.Drawing;
using System.Linq;
using FrEee.Serialization.Stringifiers;
using FrEee.Serialization.Stringifiers;

namespace FrEee.Serialization.Stringifiers;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.ComponentModel.Composition;
using System.Drawing;
using System.Linq;
using FrEee.Serialization.Stringifiers;
using FrEee.Serialization.Stringifiers;

namespace FrEee.Serialization.Stringifiers;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using FrEee.Serialization.Stringifiers;
using FrEee.Serialization.Stringifiers;

namespace FrEee.Serialization.Stringifiers;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using FrEee.Serialization.Stringifiers;
using FrEee.Serialization.Stringifiers;

namespace FrEee.Serialization.Stringifiers;

Expand Down
Loading

0 comments on commit d23739e

Please sign in to comment.