add RcImmutableArray for unity3d

This commit is contained in:
ikpil 2023-08-05 10:27:10 +09:00
parent b384133553
commit 1104e2276e
17 changed files with 246 additions and 28 deletions

View File

@ -5,7 +5,6 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="System.Collections.Immutable" Version="7.0.0" />
<PackageReference Include="System.Text.Json" Version="7.0.3" /> <PackageReference Include="System.Text.Json" Version="7.0.3" />
</ItemGroup> </ItemGroup>

View File

@ -0,0 +1,83 @@
using System;
using System.Collections;
using System.Collections.Generic;
namespace DotRecast.Core
{
public readonly partial struct RcImmutableArray<T>
{
public IEnumerator<T> GetEnumerator()
{
return EnumeratorObject.Create(_array);
}
IEnumerator IEnumerable.GetEnumerator()
{
return EnumeratorObject.Create(_array);
}
private sealed class EnumeratorObject : IEnumerator<T>
{
private static readonly IEnumerator<T> EmptyEnumerator = Create(Empty._array!);
private readonly T[] _array;
private int _index;
internal static IEnumerator<T> Create(T[] array)
{
if (array.Length != 0)
{
return new EnumeratorObject(array);
}
else
{
return EmptyEnumerator;
}
}
private EnumeratorObject(T[] array)
{
_index = -1;
_array = array;
}
public T Current
{
get
{
if (unchecked((uint)_index) < (uint)_array.Length)
{
return _array[_index];
}
throw new InvalidOperationException();
}
}
object IEnumerator.Current => this.Current;
public void Dispose()
{
}
public bool MoveNext()
{
int newIndex = _index + 1;
int length = _array.Length;
if ((uint)newIndex <= (uint)length)
{
_index = newIndex;
return (uint)newIndex < (uint)length;
}
return false;
}
void IEnumerator.Reset()
{
_index = -1;
}
}
}
}

View File

@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
namespace DotRecast.Core
{
public readonly partial struct RcImmutableArray<T> : IList<T>
{
public int Count => Length;
public bool IsReadOnly => true;
T IList<T>.this[int index]
{
get
{
var self = this;
return self[index];
}
set => throw new NotSupportedException();
}
public int IndexOf(T item)
{
for (int i = 0; i < Count; ++i)
{
if (_array![i].Equals(item))
return i;
}
return -1;
}
public bool Contains(T item)
{
return IndexOf(item) >= 0;
}
public void CopyTo(T[] array, int arrayIndex)
{
var self = this;
Array.Copy(self._array!, 0, array, arrayIndex, self.Length);
}
public void Add(T item)
{
throw new NotSupportedException();
}
public void Clear()
{
throw new NotSupportedException();
}
public bool Remove(T item)
{
throw new NotSupportedException();
}
public void Insert(int index, T item)
{
throw new NotSupportedException();
}
public void RemoveAt(int index)
{
throw new NotSupportedException();
}
}
}

View File

@ -0,0 +1,19 @@
namespace DotRecast.Core
{
public readonly partial struct RcImmutableArray<T>
{
#pragma warning disable CA1825
public static readonly RcImmutableArray<T> Empty = new RcImmutableArray<T>(new T[0]);
#pragma warning restore CA1825
private readonly T[] _array;
internal RcImmutableArray(T[] items)
{
_array = items;
}
public T this[int index] => _array![index];
public int Length => _array!.Length;
}
}

View File

@ -0,0 +1,48 @@
using System;
namespace DotRecast.Core
{
public static class RcImmutableArray
{
public static RcImmutableArray<T> Create<T>()
{
return RcImmutableArray<T>.Empty;
}
public static RcImmutableArray<T> Create<T>(T item1)
{
T[] array = new[] { item1 };
return new RcImmutableArray<T>(array);
}
public static RcImmutableArray<T> Create<T>(T item1, T item2)
{
T[] array = new[] { item1, item2 };
return new RcImmutableArray<T>(array);
}
public static RcImmutableArray<T> Create<T>(T item1, T item2, T item3)
{
T[] array = new[] { item1, item2, item3 };
return new RcImmutableArray<T>(array);
}
public static RcImmutableArray<T> Create<T>(T item1, T item2, T item3, T item4)
{
T[] array = new[] { item1, item2, item3, item4 };
return new RcImmutableArray<T>(array);
}
public static RcImmutableArray<T> Create<T>(params T[] items)
{
if (items == null || items.Length == 0)
{
return RcImmutableArray<T>.Empty;
}
var tmp = new T[items.Length];
Array.Copy(items, tmp, items.Length);
return new RcImmutableArray<T>(tmp);
}
}
}

View File

@ -19,7 +19,7 @@ freely, subject to the following restrictions:
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using DotRecast.Core; using DotRecast.Core;
using DotRecast.Detour.Dynamic.Colliders; using DotRecast.Detour.Dynamic.Colliders;
@ -60,8 +60,14 @@ namespace DotRecast.Detour.Dynamic
private RcHeightfield BuildHeightfield(DynamicNavMeshConfig config, RcTelemetry telemetry) private RcHeightfield BuildHeightfield(DynamicNavMeshConfig config, RcTelemetry telemetry)
{ {
ICollection<long> rasterizedColliders = checkpoint != null ? checkpoint.colliders : ImmutableHashSet<long>.Empty; ICollection<long> rasterizedColliders = checkpoint != null
RcHeightfield heightfield = checkpoint != null ? checkpoint.heightfield : voxelTile.Heightfield(); ? checkpoint.colliders as ICollection<long>
: RcImmutableArray<long>.Empty;
RcHeightfield heightfield = checkpoint != null
? checkpoint.heightfield
: voxelTile.Heightfield();
foreach (var (cid, c) in colliders) foreach (var (cid, c) in colliders)
{ {
if (!rasterizedColliders.Contains(cid)) if (!rasterizedColliders.Contains(cid))

View File

@ -20,8 +20,6 @@ freely, subject to the following restrictions:
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable;
using System.Numerics;
using DotRecast.Core; using DotRecast.Core;
namespace DotRecast.Detour namespace DotRecast.Detour
@ -725,7 +723,7 @@ namespace DotRecast.Detour
{ {
if (!RcVec3f.IsFinite(center) || !RcVec3f.IsFinite(halfExtents)) if (!RcVec3f.IsFinite(center) || !RcVec3f.IsFinite(halfExtents))
{ {
return ImmutableArray<DtMeshTile>.Empty; return RcImmutableArray<DtMeshTile>.Empty;
} }
RcVec3f bmin = center.Subtract(halfExtents); RcVec3f bmin = center.Subtract(halfExtents);

View File

@ -19,6 +19,7 @@ freely, subject to the following restrictions:
*/ */
using System.Collections.Immutable; using System.Collections.Immutable;
using DotRecast.Core;
namespace DotRecast.Recast.Demo.Draw; namespace DotRecast.Recast.Demo.Draw;
@ -43,7 +44,7 @@ public class DrawMode
public static readonly DrawMode DRAWMODE_POLYMESH = new(16, "Poly Mesh"); public static readonly DrawMode DRAWMODE_POLYMESH = new(16, "Poly Mesh");
public static readonly DrawMode DRAWMODE_POLYMESH_DETAIL = new(17, "Poly Mesh Detils"); public static readonly DrawMode DRAWMODE_POLYMESH_DETAIL = new(17, "Poly Mesh Detils");
public static readonly ImmutableArray<DrawMode> Values = ImmutableArray.Create( public static readonly RcImmutableArray<DrawMode> Values = RcImmutableArray.Create(
DRAWMODE_MESH, DRAWMODE_MESH,
DRAWMODE_NAVMESH, DRAWMODE_NAVMESH,
DRAWMODE_NAVMESH_INVIS, DRAWMODE_NAVMESH_INVIS,

View File

@ -18,8 +18,8 @@ freely, subject to the following restrictions:
3. This notice may not be removed or altered from any source distribution. 3. This notice may not be removed or altered from any source distribution.
*/ */
using System.Collections.Immutable;
using System.Linq; using System.Linq;
using DotRecast.Core;
namespace DotRecast.Recast.Toolset.Builder namespace DotRecast.Recast.Toolset.Builder
{ {
@ -49,7 +49,7 @@ namespace DotRecast.Recast.Toolset.Builder
public static readonly RcAreaModification SAMPLE_AREAMOD_DOOR = new RcAreaModification(SAMPLE_POLYAREA_TYPE_DOOR); public static readonly RcAreaModification SAMPLE_AREAMOD_DOOR = new RcAreaModification(SAMPLE_POLYAREA_TYPE_DOOR);
public static readonly RcAreaModification SAMPLE_AREAMOD_JUMP = new RcAreaModification(SAMPLE_POLYAREA_TYPE_JUMP); public static readonly RcAreaModification SAMPLE_AREAMOD_JUMP = new RcAreaModification(SAMPLE_POLYAREA_TYPE_JUMP);
public static readonly ImmutableArray<RcAreaModification> Values = ImmutableArray.Create( public static readonly RcImmutableArray<RcAreaModification> Values = RcImmutableArray.Create(
SAMPLE_AREAMOD_WALKABLE, SAMPLE_AREAMOD_WALKABLE,
SAMPLE_AREAMOD_GROUND, SAMPLE_AREAMOD_GROUND,
SAMPLE_AREAMOD_WATER, SAMPLE_AREAMOD_WATER,

View File

@ -18,7 +18,7 @@ freely, subject to the following restrictions:
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using DotRecast.Core;
using DotRecast.Detour; using DotRecast.Detour;
using DotRecast.Recast.Toolset.Geom; using DotRecast.Recast.Toolset.Geom;
@ -55,7 +55,7 @@ namespace DotRecast.Recast.Toolset.Builder
} }
var navMesh = BuildNavMesh(meshData, vertsPerPoly); var navMesh = BuildNavMesh(meshData, vertsPerPoly);
return new NavMeshBuildResult(ImmutableArray.Create(rcResult), navMesh); return new NavMeshBuildResult(RcImmutableArray.Create(rcResult), navMesh);
} }
private DtNavMesh BuildNavMesh(DtMeshData meshData, int vertsPerPoly) private DtNavMesh BuildNavMesh(DtMeshData meshData, int vertsPerPoly)

View File

@ -20,7 +20,6 @@ freely, subject to the following restrictions:
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable;
using DotRecast.Core; using DotRecast.Core;
using DotRecast.Recast.Geom; using DotRecast.Recast.Geom;
@ -107,7 +106,7 @@ namespace DotRecast.Recast.Toolset.Geom
public IEnumerable<RcTriMesh> Meshes() public IEnumerable<RcTriMesh> Meshes()
{ {
return ImmutableArray.Create(_mesh); return RcImmutableArray.Create(_mesh);
} }
public List<DemoOffMeshConnection> GetOffMeshConnections() public List<DemoOffMeshConnection> GetOffMeshConnections()

View File

@ -1,4 +1,4 @@
using System.Collections.Immutable; using DotRecast.Core;
namespace DotRecast.Recast.Toolset.Tools namespace DotRecast.Recast.Toolset.Tools
{ {
@ -10,7 +10,7 @@ namespace DotRecast.Recast.Toolset.Tools
public static readonly CrowdToolMode TOGGLE_POLYS = new CrowdToolMode(3, "Toggle Polys"); public static readonly CrowdToolMode TOGGLE_POLYS = new CrowdToolMode(3, "Toggle Polys");
public static readonly CrowdToolMode PROFILING = new CrowdToolMode(4, "Profiling"); public static readonly CrowdToolMode PROFILING = new CrowdToolMode(4, "Profiling");
public static readonly ImmutableArray<CrowdToolMode> Values = ImmutableArray.Create( public static readonly RcImmutableArray<CrowdToolMode> Values = RcImmutableArray.Create(
CREATE, CREATE,
MOVE_TARGET, MOVE_TARGET,
SELECT, SELECT,

View File

@ -1,4 +1,4 @@
using System.Collections.Immutable; using DotRecast.Core;
namespace DotRecast.Recast.Toolset.Tools namespace DotRecast.Recast.Toolset.Tools
{ {
@ -8,7 +8,7 @@ namespace DotRecast.Recast.Toolset.Tools
public static readonly DynamicUpdateToolMode COLLIDERS = new DynamicUpdateToolMode(1, "Colliders"); public static readonly DynamicUpdateToolMode COLLIDERS = new DynamicUpdateToolMode(1, "Colliders");
public static readonly DynamicUpdateToolMode RAYCAST = new DynamicUpdateToolMode(2, "Raycast"); public static readonly DynamicUpdateToolMode RAYCAST = new DynamicUpdateToolMode(2, "Raycast");
public static readonly ImmutableArray<DynamicUpdateToolMode> Values = ImmutableArray.Create( public static readonly RcImmutableArray<DynamicUpdateToolMode> Values = RcImmutableArray.Create(
BUILD, COLLIDERS, RAYCAST BUILD, COLLIDERS, RAYCAST
); );

View File

@ -1,4 +1,4 @@
using System.Collections.Immutable; using DotRecast.Core;
namespace DotRecast.Recast.Toolset.Tools namespace DotRecast.Recast.Toolset.Tools
{ {
@ -14,7 +14,7 @@ namespace DotRecast.Recast.Toolset.Tools
public static readonly TestNavmeshToolMode FIND_LOCAL_NEIGHBOURHOOD = new TestNavmeshToolMode(7, "Find Local Neighbourhood"); public static readonly TestNavmeshToolMode FIND_LOCAL_NEIGHBOURHOOD = new TestNavmeshToolMode(7, "Find Local Neighbourhood");
public static readonly TestNavmeshToolMode RANDOM_POINTS_IN_CIRCLE = new TestNavmeshToolMode(8, "Random Points in Circle"); public static readonly TestNavmeshToolMode RANDOM_POINTS_IN_CIRCLE = new TestNavmeshToolMode(8, "Random Points in Circle");
public static readonly ImmutableArray<TestNavmeshToolMode> Values = ImmutableArray.Create( public static readonly RcImmutableArray<TestNavmeshToolMode> Values = RcImmutableArray.Create(
PATHFIND_FOLLOW, PATHFIND_FOLLOW,
PATHFIND_STRAIGHT, PATHFIND_STRAIGHT,
PATHFIND_SLICED, PATHFIND_SLICED,

View File

@ -1,5 +1,4 @@
using System.Linq; using System.Linq;
using System.Collections.Immutable;
using DotRecast.Core; using DotRecast.Core;
using DotRecast.Recast.Toolset.Builder; using DotRecast.Recast.Toolset.Builder;
@ -130,8 +129,7 @@ namespace DotRecast.Recast.Toolset.Tools
var tb = new TileNavMeshBuilder(); var tb = new TileNavMeshBuilder();
var meshData = tb.BuildMeshData(geom, var meshData = tb.BuildMeshData(geom,
settings.cellSize, settings.cellHeight, settings.agentHeight, settings.agentRadius, settings.agentMaxClimb, settings.cellSize, settings.cellHeight, settings.agentHeight, settings.agentRadius, settings.agentMaxClimb, RcImmutableArray.Create(result)
ImmutableArray.Create(result)
).FirstOrDefault(); ).FirstOrDefault();
if (null == meshData) if (null == meshData)

View File

@ -20,7 +20,6 @@ freely, subject to the following restrictions:
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable;
using DotRecast.Core; using DotRecast.Core;
namespace DotRecast.Recast.Geom namespace DotRecast.Recast.Geom
@ -108,7 +107,7 @@ namespace DotRecast.Recast.Geom
public IEnumerable<RcTriMesh> Meshes() public IEnumerable<RcTriMesh> Meshes()
{ {
return ImmutableArray.Create(_mesh); return RcImmutableArray.Create(_mesh);
} }
public void CalculateNormals() public void CalculateNormals()

View File

@ -18,7 +18,6 @@ freely, subject to the following restrictions:
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable;
using DotRecast.Core; using DotRecast.Core;
namespace DotRecast.Recast.Geom namespace DotRecast.Recast.Geom
@ -56,12 +55,12 @@ namespace DotRecast.Recast.Geom
public IEnumerable<RcTriMesh> Meshes() public IEnumerable<RcTriMesh> Meshes()
{ {
return ImmutableArray.Create(_mesh); return RcImmutableArray.Create(_mesh);
} }
public IList<RcConvexVolume> ConvexVolumes() public IList<RcConvexVolume> ConvexVolumes()
{ {
return ImmutableArray<RcConvexVolume>.Empty; return RcImmutableArray<RcConvexVolume>.Empty;
} }
} }
} }