Skip to content

Commit 84ec162

Browse files
committed
.NETStandard 2.0 support
1 parent 531ca00 commit 84ec162

File tree

10 files changed

+60
-7
lines changed

10 files changed

+60
-7
lines changed

src/ECS.Boost/Friflo.Engine.ECS.Boost.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>library</OutputType>
5-
<TargetFrameworks>net8.0;net7.0;net6.0;netstandard2.1</TargetFrameworks>
5+
<TargetFrameworks>net8.0;net7.0;net6.0;netstandard2.1;netstandard2.0</TargetFrameworks>
66
<ImplicitUsings>disable</ImplicitUsings>
77
<Nullable>disable</Nullable>
88
<RootNamespace>Friflo.Engine.ECS</RootNamespace>

src/ECS/Archetype/EntityStore.Archetype.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Hard rule: this file MUST NOT use type: Entity
55

66
using System;
7+
using Friflo.Engine.ECS.Compat;
78

89
// ReSharper disable InlineTemporaryVariable
910
// ReSharper disable ArrangeTrailingCommaInMultilineLists

src/ECS/Base/SchemaUtils.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ private static bool RegisterComponentTypesByReflection() {
1818
if (Platform.IsUnityRuntime) {
1919
return true;
2020
}
21+
#if NETSTANDARD && !NETSTANDARD2_1_OR_GREATER
22+
return true;
23+
#else
2124
return System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeCompiled;
25+
#endif
2226
}
2327

2428
[ExcludeFromCodeCoverage]

src/ECS/Compat/HashSet.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Collections.Generic;
2+
3+
namespace Friflo.Engine.ECS.Compat;
4+
5+
#if NETSTANDARD && !NETSTANDARD2_1_OR_GREATER
6+
public static class HashSet
7+
{
8+
public static bool TryGetValue<T>(this HashSet<T> set, T value, out T actualValue)
9+
{
10+
if (set.Contains(value)) {
11+
actualValue = value;
12+
return true;
13+
}
14+
actualValue = default;
15+
return false;
16+
}
17+
}
18+
#endif

src/ECS/Entity/MemberPath.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ private MemberPath(MemberPathKey key, ComponentType componentType, int structInd
104104

105105
private const BindingFlags Flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.GetProperty;
106106

107+
#if NETSTANDARD && !NETSTANDARD2_1_OR_GREATER
108+
private static readonly char[] SplitDotArray = ['.'];
109+
#endif
110+
107111
/// <summary>
108112
/// Returns a <see cref="MemberPath"/> identifying a specific field / property by its <paramref name="path"/>
109113
/// within the passed <paramref name="type"/>.
@@ -122,7 +126,11 @@ public static MemberPath Get(Type type, string path)
122126
if (Map.TryGetValue(key, out var componentFieldInfo)) {
123127
return componentFieldInfo;
124128
}
129+
#if NETSTANDARD && !NETSTANDARD2_1_OR_GREATER
130+
var pathItems = path.Split(SplitDotArray, StringSplitOptions.RemoveEmptyEntries);
131+
#else
125132
var pathItems = path.Split('.', StringSplitOptions.RemoveEmptyEntries);
133+
#endif
126134
var memberInfos = new MemberInfo[pathItems.Length];
127135
var memberType = type;
128136
bool canWrite = true;
@@ -188,10 +196,24 @@ public static MemberPath Get(Type type, string path)
188196

189197
// See ThrowIfTypeNeverValidGenericArgument() at:
190198
// https://github.com/dotnet/runtime/blob/4f5c6938d09e935830492c006aa8381611b65ad8/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs#L736
191-
private static bool IsInvalidType(Type type) {
192-
return type.IsPointer || type.IsByRef || type == typeof(void) ||
193-
type.IsByRefLike; // type is a ref struct. E.g. Span<>
199+
private static bool IsInvalidType(Type type)
200+
{
201+
if (type.IsPointer || type.IsByRef || type == typeof(void))
202+
return true;
203+
#if NETSTANDARD && !NETSTANDARD2_1_OR_GREATER
204+
if (type.IsByRef)
205+
return true;
206+
if (type.IsGenericType)
207+
{
208+
var genericTypeDef = type.GetGenericTypeDefinition();
209+
if (genericTypeDef == typeof(Span<>) || genericTypeDef == typeof(ReadOnlySpan<>))
210+
return true;
194211
}
212+
return false;
213+
#else
214+
return type.IsByRefLike; // type is a ref struct. E.g. Span<>
215+
#endif
216+
}
195217

196218
// ReSharper disable UnusedMember.Local
197219
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = "Not called for NativeAOT")]

src/ECS/Friflo.Engine.ECS.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>library</OutputType>
5-
<TargetFrameworks>net8.0;net7.0;net6.0;netstandard2.1</TargetFrameworks>
5+
<TargetFrameworks>net8.0;net7.0;net6.0;netstandard2.1;netstandard2.0</TargetFrameworks>
66
<ImplicitUsings>disable</ImplicitUsings>
77
<Nullable>disable</Nullable>
88
<RootNamespace>Friflo.Engine.ECS</RootNamespace>

src/ECS/Systems/Extensions/SystemPerf.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ public PerfResource () {
9292

9393
[ExcludeFromCodeCoverage]
9494
internal static long GetAllocatedBytes() {
95+
#if NETSTANDARD && !NETSTANDARD2_1_OR_GREATER
96+
return 0;
97+
#else
9598
return Platform.IsUnityRuntime ? 0 : GC.GetAllocatedBytesForCurrentThread();
99+
#endif
96100
}
97101
}
98102

src/ECS/Utils/TreeUtils.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@ private static void DuplicateChildren(Entity entity, Entity clone, EntityStore s
8080
public static AddDataEntitiesResult AddDataEntitiesToEntity(Entity targetEntity, IReadOnlyList<DataEntity> dataEntities)
8181
{
8282
var entityCount = dataEntities.Count;
83+
#if NETSTANDARD && !NETSTANDARD2_1_OR_GREATER
84+
var childEntities = new HashSet<long> ();
85+
childEntities.EnsureCapacity(entityCount);
86+
#else
8387
var childEntities = new HashSet<long> (entityCount);
88+
#endif
8489
var oldToNewPid = new Dictionary<long, long>(entityCount);
8590
var newToOldPid = new Dictionary<long, long>(entityCount);
8691
var store = targetEntity.Store;

src/Hub/Engine.Hub.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>library</OutputType>
5-
<TargetFrameworks>net8.0;netstandard2.1</TargetFrameworks>
5+
<TargetFrameworks>net8.0;netstandard2.1;netstandard2.0</TargetFrameworks>
66
<ImplicitUsings>disable</ImplicitUsings>
77
<Nullable>disable</Nullable>
88
<RootNamespace>Friflo.Engine.Hub</RootNamespace>

src/Tests-NativeAOT/Tests-NativeAOT.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<PublishAot>true</PublishAot>
65
<!-- <DefineConstants Condition="'$(PublishAot)' == 'true'">
76
$(DefineConstants);IS_NATIVE_AOT
87
</DefineConstants> -->

0 commit comments

Comments
 (0)