add tile cache compressor factory shared

This commit is contained in:
ikpil 2023-08-19 15:31:51 +09:00
parent 2050c476a2
commit 4be459b296
32 changed files with 158 additions and 188 deletions

View File

@ -127,7 +127,7 @@ namespace DotRecast.Detour.TileCache
header.hmin = layer.hmin;
header.hmax = layer.hmax;
var comp = _compFactory.Get(storageParams.Compatibility);
var comp = _compFactory.Create(storageParams.Compatibility ? 0 : 1);
var bytes = builder.CompressTileCacheLayer(header, layer.heights, layer.areas, layer.cons, storageParams.Order, storageParams.Compatibility, comp);
result.Add(bytes);
}

View File

@ -0,0 +1,37 @@
using System.Collections.Generic;
using DotRecast.Core;
namespace DotRecast.Detour.TileCache.Io.Compress
{
public class DtTileCacheCompressorFactory : IDtTileCacheCompressorFactory
{
public static readonly DtTileCacheCompressorFactory Shared = new DtTileCacheCompressorFactory();
private readonly Dictionary<int, IRcCompressor> _compressors = new Dictionary<int, IRcCompressor>();
public bool TryAdd(int type, IRcCompressor compressor)
{
if (0 == type)
return false;
_compressors[type] = compressor;
return true;
}
public IRcCompressor Create(int compatibility)
{
// default
if (0 == compatibility)
{
return DtTileCacheFastLzCompressor.Shared;
}
if (!_compressors.TryGetValue(compatibility, out var comp))
{
return null;
}
return comp;
}
}
}

View File

@ -24,6 +24,6 @@ namespace DotRecast.Detour.TileCache.Io.Compress
{
public interface IDtTileCacheCompressorFactory
{
IRcCompressor Get(bool cCompatibility);
IRcCompressor Create(int compatibility);
}
}

View File

@ -70,7 +70,7 @@ namespace DotRecast.Detour.TileCache.Io
header.meshParams = paramReader.Read(bb);
header.cacheParams = ReadCacheParams(bb, cCompatibility);
DtNavMesh mesh = new DtNavMesh(header.meshParams, maxVertPerPoly);
IRcCompressor comp = _compFactory.Get(cCompatibility);
IRcCompressor comp = _compFactory.Create(cCompatibility ? 0 : 1);
DtTileCacheStorageParams storageParams = new DtTileCacheStorageParams(bb.Order(), cCompatibility);
DtTileCache tc = new DtTileCache(header.cacheParams, storageParams, mesh, comp, meshProcessor);
// Read tiles.

View File

@ -63,7 +63,7 @@ namespace DotRecast.Detour.TileCache.Io
Write(stream, (int)cache.GetTileRef(tile), order);
byte[] data = tile.data;
DtTileCacheLayer layer = cache.DecompressTile(tile);
var comp = _compFactory.Get(cCompatibility);
var comp = _compFactory.Create(cCompatibility ? 0 : 1);
data = builder.CompressTileCacheLayer(comp, layer, order, cCompatibility);
Write(stream, data.Length, order);
stream.Write(data);

View File

@ -8,12 +8,23 @@ namespace DotRecast.Recast.Demo.Draw;
[StructLayout(LayoutKind.Explicit, Pack = 1)]
public struct OpenGLVertex
{
[FieldOffset(0)] private readonly float x;
[FieldOffset(4)] private readonly float y;
[FieldOffset(8)] private readonly float z;
[FieldOffset(12)] private readonly float u;
[FieldOffset(16)] private readonly float v;
[FieldOffset(20)] private readonly int color;
[FieldOffset(0)]
private readonly float x;
[FieldOffset(4)]
private readonly float y;
[FieldOffset(8)]
private readonly float z;
[FieldOffset(12)]
private readonly float u;
[FieldOffset(16)]
private readonly float v;
[FieldOffset(20)]
private readonly int color;
public OpenGLVertex(RcVec3f pos, RcVec2f uv, int color) :
this(pos.x, pos.y, pos.z, uv.x, uv.y, color)

View File

@ -23,7 +23,6 @@ using System.Collections.Generic;
using System.Numerics;
using DotRecast.Core;
using DotRecast.Detour;
using DotRecast.Recast.Toolset.Builder;
using Silk.NET.OpenGL;

View File

@ -1,21 +0,0 @@
using DotRecast.Core;
using DotRecast.Detour.TileCache.Io.Compress;
namespace DotRecast.Recast.Demo;
public class DtTileCacheCompressorDemoFactory : IDtTileCacheCompressorFactory
{
public static readonly DtTileCacheCompressorDemoFactory Shared = new();
private DtTileCacheCompressorDemoFactory()
{
}
public IRcCompressor Get(bool cCompatibility)
{
if (cCompatibility)
return DtTileCacheFastLzCompressor.Shared;
return DtTileCacheLZ4DemoCompressor.Shared;
}
}

View File

@ -1,49 +0,0 @@
/*
Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
using DotRecast.Core;
using K4os.Compression.LZ4;
namespace DotRecast.Recast.Demo
{
public class DtTileCacheLZ4DemoCompressor : IRcCompressor
{
public static readonly DtTileCacheLZ4DemoCompressor Shared = new();
private DtTileCacheLZ4DemoCompressor()
{
}
public byte[] Decompress(byte[] buf)
{
return LZ4Pickler.Unpickle(buf);
}
public byte[] Decompress(byte[] buf, int offset, int len, int outputlen)
{
return LZ4Pickler.Unpickle(buf, offset, len);
}
public byte[] Compress(byte[] buf)
{
return LZ4Pickler.Pickle(buf);
}
}
}

View File

@ -2,5 +2,4 @@
public class IRecastDemoMessage
{
}

View File

@ -2,5 +2,4 @@
public class NavMeshBuildBeganEvent : IRecastDemoMessage
{
}

View File

@ -2,5 +2,4 @@
public class NavMeshSaveBeganEvent : IRecastDemoMessage
{
}

View File

@ -24,7 +24,6 @@ using DotRecast.Core;
using DotRecast.Detour;
using DotRecast.Detour.Crowd;
using DotRecast.Detour.Crowd.Tracking;
using DotRecast.Recast.Toolset.Builder;
using DotRecast.Recast.Demo.Draw;
using DotRecast.Recast.Toolset;

View File

@ -14,14 +14,14 @@ public class BoxGizmo : IColliderGizmo
private static readonly RcVec3f[] VERTS =
{
RcVec3f.Of( -1f, -1f, -1f),
RcVec3f.Of( 1f, -1f, -1f),
RcVec3f.Of( 1f, -1f, 1f),
RcVec3f.Of( -1f, -1f, 1f),
RcVec3f.Of( -1f, 1f, -1f),
RcVec3f.Of( 1f, 1f, -1f),
RcVec3f.Of( 1f, 1f, 1f),
RcVec3f.Of( -1f, 1f, 1f),
RcVec3f.Of(-1f, -1f, -1f),
RcVec3f.Of(1f, -1f, -1f),
RcVec3f.Of(1f, -1f, 1f),
RcVec3f.Of(-1f, -1f, 1f),
RcVec3f.Of(-1f, 1f, -1f),
RcVec3f.Of(1f, 1f, -1f),
RcVec3f.Of(1f, 1f, 1f),
RcVec3f.Of(-1f, 1f, 1f),
};
private readonly float[] vertices = new float[8 * 3];

View File

@ -1,6 +1,5 @@
using DotRecast.Core;
using DotRecast.Recast.Demo.Draw;
using static DotRecast.Core.RcMath;
using static DotRecast.Recast.Demo.Tools.Gizmos.GizmoHelper;

View File

@ -1,6 +1,5 @@
using DotRecast.Core;
using DotRecast.Recast.Demo.Draw;
using static DotRecast.Core.RcMath;
using static DotRecast.Recast.Demo.Tools.Gizmos.GizmoHelper;

View File

@ -1,4 +1,5 @@
using DotRecast.Core;
using DotRecast.Detour.TileCache.Io.Compress;
using DotRecast.Recast.Demo.Draw;
using DotRecast.Recast.Toolset;
using DotRecast.Recast.Toolset.Tools;
@ -16,7 +17,7 @@ public class ObstacleTool : IRcTool
public ObstacleTool()
{
_impl = new(DtTileCacheCompressorDemoFactory.Shared);
_impl = new(DtTileCacheCompressorFactory.Shared);
}
public ISampleTool GetTool()

View File

@ -67,7 +67,7 @@ public class AbstractTileCacheTest
navMeshParams.maxPolys = 16384;
var navMesh = new DtNavMesh(navMeshParams, 6);
var comp = DtTileCacheCompressorForTestFactory.Shared.Get(cCompatibility);
var comp = DtTileCacheCompressorFactory.Shared.Create(cCompatibility ? 0 : 1);
var storageParams = new DtTileCacheStorageParams(order, cCompatibility);
var process = new TestTileCacheMeshProcess();
DtTileCache tc = new DtTileCache(option, storageParams, navMesh, comp, process);

View File

@ -1,21 +0,0 @@
using DotRecast.Core;
using DotRecast.Detour.TileCache.Io.Compress;
namespace DotRecast.Detour.TileCache.Test.Io;
public class DtTileCacheCompressorForTestFactory : IDtTileCacheCompressorFactory
{
public static readonly DtTileCacheCompressorForTestFactory Shared = new();
private DtTileCacheCompressorForTestFactory()
{
}
public IRcCompressor Get(bool cCompatibility)
{
if (cCompatibility)
return DtTileCacheFastLzCompressor.Shared;
return DtTileCacheLZ4ForTestCompressor.Shared;
}
}

View File

@ -22,6 +22,7 @@ using System;
using System.IO;
using DotRecast.Core;
using DotRecast.Detour.TileCache.Io;
using DotRecast.Detour.TileCache.Io.Compress;
using NUnit.Framework;
namespace DotRecast.Detour.TileCache.Test.Io;
@ -29,7 +30,7 @@ namespace DotRecast.Detour.TileCache.Test.Io;
[Parallelizable]
public class TileCacheReaderTest
{
private readonly DtTileCacheReader reader = new DtTileCacheReader(DtTileCacheCompressorForTestFactory.Shared);
private readonly DtTileCacheReader reader = new DtTileCacheReader(DtTileCacheCompressorFactory.Shared);
[Test]
public void TestNavmesh()

View File

@ -22,6 +22,7 @@ using System.Collections.Generic;
using System.IO;
using DotRecast.Core;
using DotRecast.Detour.TileCache.Io;
using DotRecast.Detour.TileCache.Io.Compress;
using DotRecast.Recast;
using DotRecast.Recast.Geom;
using NUnit.Framework;
@ -31,8 +32,8 @@ namespace DotRecast.Detour.TileCache.Test.Io;
[Parallelizable]
public class TileCacheReaderWriterTest : AbstractTileCacheTest
{
private readonly DtTileCacheReader reader = new DtTileCacheReader(DtTileCacheCompressorForTestFactory.Shared);
private readonly DtTileCacheWriter writer = new DtTileCacheWriter(DtTileCacheCompressorForTestFactory.Shared);
private readonly DtTileCacheReader reader = new DtTileCacheReader(DtTileCacheCompressorFactory.Shared);
private readonly DtTileCacheWriter writer = new DtTileCacheWriter(DtTileCacheCompressorFactory.Shared);
[Test]
public void TestFastLz()

View File

@ -56,7 +56,7 @@ public class TestTileLayerBuilder : DtTileCacheLayerBuilder
public readonly int tw;
public readonly int th;
public TestTileLayerBuilder(IInputGeomProvider geom) : base(DtTileCacheCompressorForTestFactory.Shared)
public TestTileLayerBuilder(IInputGeomProvider geom) : base(DtTileCacheCompressorFactory.Shared)
{
_geom = geom;
_cfg = new RcConfig(true, m_tileSize, m_tileSize, RcConfig.CalcBorder(AgentRadius, CellSize),

View File

@ -22,6 +22,7 @@ using System.Collections.Generic;
using System.IO;
using DotRecast.Core;
using DotRecast.Detour.TileCache.Io;
using DotRecast.Detour.TileCache.Io.Compress;
using DotRecast.Detour.TileCache.Test.Io;
using NUnit.Framework;
@ -39,7 +40,7 @@ public class TileCacheFindPathTest : AbstractTileCacheTest
{
using var msis = new MemoryStream(Loader.ToBytes("dungeon_all_tiles_tilecache.bin"));
using var @is = new BinaryReader(msis);
DtTileCache tcC = new DtTileCacheReader(DtTileCacheCompressorForTestFactory.Shared).Read(@is, 6, new TestTileCacheMeshProcess());
DtTileCache tcC = new DtTileCacheReader(DtTileCacheCompressorFactory.Shared).Read(@is, 6, new TestTileCacheMeshProcess());
navmesh = tcC.GetNavMesh();
query = new DtNavMeshQuery(navmesh);
}

View File

@ -0,0 +1,16 @@
using DotRecast.Detour.TileCache.Io.Compress;
using DotRecast.Detour.TileCache.Test.Io;
using NUnit.Framework;
namespace DotRecast.Detour.TileCache.Test;
[SetUpFixture]
public class TileCacheTestSetUpFixture
{
[OneTimeSetUp]
public void OneTimeSetUp()
{
// add lz4
DtTileCacheCompressorFactory.Shared.TryAdd(1, DtTileCacheLZ4ForTestCompressor.Shared);
}
}