refactor: added RcDirectory, RcResources

This commit is contained in:
ikpil 2023-09-20 23:41:18 +09:00
parent 502ce2efa8
commit c1cc0fb286
26 changed files with 111 additions and 71 deletions

View File

@ -1,33 +0,0 @@
using System.IO;
namespace DotRecast.Core
{
public static class Loader
{
public static byte[] ToBytes(string filename)
{
var filepath = FindParentPath(filename);
using var fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
return buffer;
}
public static string FindParentPath(string filename)
{
string filePath = Path.Combine("resources", filename);
for (int i = 0; i < 10; ++i)
{
if (File.Exists(filePath))
{
return Path.GetFullPath(filePath);
}
filePath = Path.Combine("..", filePath);
}
return Path.GetFullPath(filename);
}
}
}

View File

@ -0,0 +1,57 @@
using System.IO;
using System.Linq;
namespace DotRecast.Core
{
public static class RcDirectory
{
public static string SearchPath(string searchPath, int depth, out bool isDir)
{
isDir = false;
for (int i = 0; i < depth; ++i)
{
var relativePath = string.Join("", Enumerable.Range(0, i).Select(x => "../"));
var searchingPath = Path.Combine(relativePath, searchPath);
var fullSearchingPath = Path.GetFullPath(searchingPath);
if (File.Exists(fullSearchingPath))
{
return fullSearchingPath;
}
if (Directory.Exists(fullSearchingPath))
{
isDir = true;
return fullSearchingPath;
}
}
return string.Empty;
}
// only directory
public static string SearchDirectory(string dirname, int depth = 10)
{
var searchingPath = SearchPath(dirname, depth, out var isDir);
if (!isDir)
{
var path = Path.GetDirectoryName(searchingPath) ?? string.Empty;
return path;
}
return string.Empty;
}
public static string SearchFile(string filename, int depth = 10)
{
var searchingPath = SearchPath(filename, depth, out var isDir);
if (!isDir)
{
return searchingPath;
}
return string.Empty;
}
}
}

View File

@ -0,0 +1,17 @@
using System.IO;
namespace DotRecast.Core
{
public static class RcResources
{
public static byte[] Load(string filename)
{
var filepath = RcDirectory.SearchFile($"resources/{filename}");
using var fs = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
return buffer;
}
}
}

View File

@ -37,11 +37,10 @@ public static class Program
private static void InitializeWorkingDirectory()
{
var path = Loader.FindParentPath("dungeon.obj");
path = Path.GetDirectoryName(path);
var path = RcDirectory.SearchDirectory("resources/dungeon.obj");
if (!string.IsNullOrEmpty(path))
{
var workingDirectory = Path.Combine(path, "..");
var workingDirectory = Path.GetDirectoryName(path) ?? string.Empty;
workingDirectory = Path.GetFullPath(workingDirectory);
Directory.SetCurrentDirectory(workingDirectory);
}

View File

@ -298,7 +298,7 @@ public class RecastDemo : IRecastDemoChannel
private DemoInputGeomProvider LoadInputMesh(string filename)
{
var bytes = Loader.ToBytes(filename);
var bytes = RcResources.Load(filename);
DemoInputGeomProvider geom = DemoObjImporter.Load(bytes);
_lastGeomFileName = filename;
@ -754,7 +754,7 @@ public class RecastDemo : IRecastDemoChannel
try
{
using FileStream fs = new FileStream(args.FilePath, FileMode.Open, FileAccess.Read);
using FileStream fs = new FileStream(args.FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
LoadNavMesh(fs, args.FilePath);
}
catch (Exception e)

View File

@ -88,9 +88,9 @@ public class DynamicUpdateSampleTool : ISampleTool
public DynamicUpdateSampleTool()
{
var bridgeGeom = DemoObjImporter.Load(Loader.ToBytes("bridge.obj"));
var houseGeom = DemoObjImporter.Load(Loader.ToBytes("house.obj"));
var convexGeom = DemoObjImporter.Load(Loader.ToBytes("convex.obj"));
var bridgeGeom = DemoObjImporter.Load(RcResources.Load("bridge.obj"));
var houseGeom = DemoObjImporter.Load(RcResources.Load("house.obj"));
var convexGeom = DemoObjImporter.Load(RcResources.Load("convex.obj"));
_tool = new(Random.Shared, bridgeGeom, houseGeom, convexGeom);
executor = Task.Factory;
}

View File

@ -142,7 +142,7 @@ namespace DotRecast.Recast.Toolset.Tools
public DtDynamicNavMesh Load(string filename, IRcCompressor compressor)
{
using var fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
using var fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
using var br = new BinaryReader(fs);
DtVoxelFileReader reader = new DtVoxelFileReader(compressor);
DtVoxelFile voxelFile = reader.Read(br);

View File

@ -40,7 +40,7 @@ public class RecastTestMeshBuilder
public const float m_detailSampleMaxError = 1.0f;
public RecastTestMeshBuilder()
: this(ObjImporter.Load(Loader.ToBytes("dungeon.obj")),
: this(ObjImporter.Load(RcResources.Load("dungeon.obj")),
RcPartition.WATERSHED,
m_cellSize, m_cellHeight,
m_agentMaxSlope, m_agentHeight, m_agentRadius, m_agentMaxClimb,

View File

@ -21,7 +21,7 @@ public class DynamicNavMeshTest
[Test]
public void E2eTest()
{
byte[] bytes = Loader.ToBytes("test_tiles.voxels");
byte[] bytes = RcResources.Load("test_tiles.voxels");
using var ms = new MemoryStream(bytes);
using var br = new BinaryReader(ms);

View File

@ -30,7 +30,7 @@ public class VoxelFileReaderTest
[Test]
public void ShouldReadSingleTileFile()
{
byte[] bytes = Loader.ToBytes("test.voxels");
byte[] bytes = RcResources.Load("test.voxels");
using var ms = new MemoryStream(bytes);
using var br = new BinaryReader(ms);
@ -56,7 +56,7 @@ public class VoxelFileReaderTest
[Test]
public void ShouldReadMultiTileFile()
{
byte[] bytes = Loader.ToBytes("test_tiles.voxels");
byte[] bytes = RcResources.Load("test_tiles.voxels");
using var ms = new MemoryStream(bytes);
using var br = new BinaryReader(ms);

View File

@ -31,7 +31,7 @@ public class VoxelFileReaderWriterTest
[TestCase(true)]
public void ShouldReadSingleTileFile(bool compression)
{
byte[] bytes = Loader.ToBytes("test.voxels");
byte[] bytes = RcResources.Load("test.voxels");
using var ms = new MemoryStream(bytes);
using var br = new BinaryReader(ms);
@ -59,7 +59,7 @@ public class VoxelFileReaderWriterTest
[TestCase(true)]
public void ShouldReadMultiTileFile(bool compression)
{
byte[] bytes = Loader.ToBytes("test_tiles.voxels");
byte[] bytes = RcResources.Load("test_tiles.voxels");
using var ms = new MemoryStream(bytes);
using var br = new BinaryReader(ms);

View File

@ -93,7 +93,7 @@ public class VoxelQueryTest
private DtDynamicNavMesh CreateDynaMesh()
{
var bytes = Loader.ToBytes("test_tiles.voxels");
var bytes = RcResources.Load("test_tiles.voxels");
using var ms = new MemoryStream(bytes);
using var br = new BinaryReader(ms);

View File

@ -81,8 +81,8 @@ public class UnityAStarPathfindingImporterTest
private DtNavMesh LoadNavMesh(string filename)
{
var filepath = Loader.FindParentPath(filename);
using var fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
var filepath = RcDirectory.SearchFile($"resources/{filename}");
using var fs = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
// Import the graphs
UnityAStarPathfindingImporter importer = new UnityAStarPathfindingImporter();

View File

@ -32,7 +32,7 @@ public class MeshSetReaderTest
[Test]
public void TestNavmesh()
{
byte[] @is = Loader.ToBytes("all_tiles_navmesh.bin");
byte[] @is = RcResources.Load("all_tiles_navmesh.bin");
using var ms = new MemoryStream(@is);
using var br = new BinaryReader(ms);
DtNavMesh mesh = reader.Read(br, 6);
@ -60,7 +60,7 @@ public class MeshSetReaderTest
[Test]
public void TestDungeon()
{
byte[] @is = Loader.ToBytes("dungeon_all_tiles_navmesh.bin");
byte[] @is = RcResources.Load("dungeon_all_tiles_navmesh.bin");
using var ms = new MemoryStream(@is);
using var br = new BinaryReader(ms);
@ -89,7 +89,7 @@ public class MeshSetReaderTest
[Test]
public void TestDungeon32Bit()
{
byte[] @is = Loader.ToBytes("dungeon_all_tiles_navmesh_32bit.bin");
byte[] @is = RcResources.Load("dungeon_all_tiles_navmesh_32bit.bin");
using var ms = new MemoryStream(@is);
using var br = new BinaryReader(ms);

View File

@ -54,7 +54,7 @@ public class MeshSetReaderWriterTest
[Test]
public void Test()
{
IInputGeomProvider geom = ObjImporter.Load(Loader.ToBytes("dungeon.obj"));
IInputGeomProvider geom = ObjImporter.Load(RcResources.Load("dungeon.obj"));
NavMeshSetHeader header = new NavMeshSetHeader();
header.magic = NavMeshSetHeader.NAVMESHSET_MAGIC;

View File

@ -40,7 +40,7 @@ public class RecastTestMeshBuilder
private const float m_detailSampleMaxError = 1.0f;
public RecastTestMeshBuilder()
: this(ObjImporter.Load(Loader.ToBytes("dungeon.obj")),
: this(ObjImporter.Load(RcResources.Load("dungeon.obj")),
RcPartition.WATERSHED,
m_cellSize, m_cellHeight,
m_agentMaxSlope, m_agentHeight, m_agentRadius, m_agentMaxClimb,

View File

@ -45,7 +45,7 @@ public class TestTiledNavMeshBuilder
private const int m_tileSize = 32;
public TestTiledNavMeshBuilder() :
this(ObjImporter.Load(Loader.ToBytes("dungeon.obj")),
this(ObjImporter.Load(RcResources.Load("dungeon.obj")),
RcPartition.WATERSHED, m_cellSize, m_cellHeight, m_agentHeight, m_agentRadius, m_agentMaxClimb, m_agentMaxSlope,
m_regionMinSize, m_regionMergeSize, m_edgeMaxLen, m_edgeMaxError, m_vertsPerPoly, m_detailSampleDist,
m_detailSampleMaxError, m_tileSize)

View File

@ -35,7 +35,7 @@ public class TileCacheReaderTest
[Test]
public void TestNavmesh()
{
using var ms = new MemoryStream(Loader.ToBytes("all_tiles_tilecache.bin"));
using var ms = new MemoryStream(RcResources.Load("all_tiles_tilecache.bin"));
using var br = new BinaryReader(ms);
DtTileCache tc = reader.Read(br, 6, null);
Assert.That(tc.GetNavMesh().GetMaxTiles(), Is.EqualTo(256));
@ -133,7 +133,7 @@ public class TileCacheReaderTest
[Test]
public void TestDungeon()
{
using var ms = new MemoryStream(Loader.ToBytes("dungeon_all_tiles_tilecache.bin"));
using var ms = new MemoryStream(RcResources.Load("dungeon_all_tiles_tilecache.bin"));
using var br = new BinaryReader(ms);
DtTileCache tc = reader.Read(br, 6, null);
Assert.That(tc.GetNavMesh().GetMaxTiles(), Is.EqualTo(256));

View File

@ -51,7 +51,7 @@ public class TileCacheReaderWriterTest : AbstractTileCacheTest
private void TestDungeon(bool cCompatibility)
{
IInputGeomProvider geom = ObjImporter.Load(Loader.ToBytes("dungeon.obj"));
IInputGeomProvider geom = ObjImporter.Load(RcResources.Load("dungeon.obj"));
TestTileLayerBuilder layerBuilder = new TestTileLayerBuilder(geom);
List<byte[]> layers = layerBuilder.Build(RcByteOrder.LITTLE_ENDIAN, cCompatibility, 1);
DtTileCache tc = GetTileCache(geom, RcByteOrder.LITTLE_ENDIAN, cCompatibility);

View File

@ -34,7 +34,7 @@ public class TempObstaclesTest : AbstractTileCacheTest
public void TestDungeon()
{
bool cCompatibility = true;
IInputGeomProvider geom = ObjImporter.Load(Loader.ToBytes("dungeon.obj"));
IInputGeomProvider geom = ObjImporter.Load(RcResources.Load("dungeon.obj"));
TestTileLayerBuilder layerBuilder = new TestTileLayerBuilder(geom);
List<byte[]> layers = layerBuilder.Build(RcByteOrder.LITTLE_ENDIAN, cCompatibility, 1);
DtTileCache tc = GetTileCache(geom, RcByteOrder.LITTLE_ENDIAN, cCompatibility);
@ -68,7 +68,7 @@ public class TempObstaclesTest : AbstractTileCacheTest
public void TestDungeonBox()
{
bool cCompatibility = true;
IInputGeomProvider geom = ObjImporter.Load(Loader.ToBytes("dungeon.obj"));
IInputGeomProvider geom = ObjImporter.Load(RcResources.Load("dungeon.obj"));
TestTileLayerBuilder layerBuilder = new TestTileLayerBuilder(geom);
List<byte[]> layers = layerBuilder.Build(RcByteOrder.LITTLE_ENDIAN, cCompatibility, 1);
DtTileCache tc = GetTileCache(geom, RcByteOrder.LITTLE_ENDIAN, cCompatibility);

View File

@ -38,7 +38,7 @@ public class TileCacheFindPathTest : AbstractTileCacheTest
public TileCacheFindPathTest()
{
using var msr = new MemoryStream(Loader.ToBytes("dungeon_all_tiles_tilecache.bin"));
using var msr = new MemoryStream(RcResources.Load("dungeon_all_tiles_tilecache.bin"));
using var br = new BinaryReader(msr);
DtTileCache tcC = new DtTileCacheReader(DtTileCacheCompressorFactory.Shared).Read(br, 6, new TestTileCacheMeshProcess());
navmesh = tcC.GetNavMesh();

View File

@ -55,7 +55,7 @@ public class TileCacheNavigationTest : AbstractTileCacheTest
public void SetUp()
{
bool cCompatibility = true;
IInputGeomProvider geom = ObjImporter.Load(Loader.ToBytes("dungeon.obj"));
IInputGeomProvider geom = ObjImporter.Load(RcResources.Load("dungeon.obj"));
TestTileLayerBuilder layerBuilder = new TestTileLayerBuilder(geom);
List<byte[]> layers = layerBuilder.Build(RcByteOrder.LITTLE_ENDIAN, cCompatibility, 1);
DtTileCache tc = GetTileCache(geom, RcByteOrder.LITTLE_ENDIAN, cCompatibility);

View File

@ -59,7 +59,7 @@ public class TileCacheTest : AbstractTileCacheTest
private void TestDungeon(RcByteOrder order, bool cCompatibility)
{
IInputGeomProvider geom = ObjImporter.Load(Loader.ToBytes("dungeon.obj"));
IInputGeomProvider geom = ObjImporter.Load(RcResources.Load("dungeon.obj"));
DtTileCache tc = GetTileCache(geom, order, cCompatibility);
TestTileLayerBuilder layerBuilder = new TestTileLayerBuilder(geom);
List<byte[]> layers = layerBuilder.Build(order, cCompatibility, 1);
@ -155,7 +155,7 @@ public class TileCacheTest : AbstractTileCacheTest
private void Test(RcByteOrder order, bool cCompatibility)
{
IInputGeomProvider geom = ObjImporter.Load(Loader.ToBytes("nav_test.obj"));
IInputGeomProvider geom = ObjImporter.Load(RcResources.Load("nav_test.obj"));
DtTileCache tc = GetTileCache(geom, order, cCompatibility);
TestTileLayerBuilder layerBuilder = new TestTileLayerBuilder(geom);
List<byte[]> layers = layerBuilder.Build(order, cCompatibility, 1);
@ -182,7 +182,7 @@ public class TileCacheTest : AbstractTileCacheTest
RcByteOrder order = RcByteOrder.LITTLE_ENDIAN;
bool cCompatibility = false;
var objBytes = Loader.ToBytes("dungeon.obj");
var objBytes = RcResources.Load("dungeon.obj");
IInputGeomProvider geom = ObjImporter.Load(objBytes);
TestTileLayerBuilder layerBuilder = new TestTileLayerBuilder(geom);
for (int i = 0; i < 4; i++)

View File

@ -145,7 +145,7 @@ public class RecastLayersTest
private RcHeightfieldLayerSet Build(string filename, int x, int y)
{
IInputGeomProvider geom = ObjImporter.Load(Loader.ToBytes(filename));
IInputGeomProvider geom = ObjImporter.Load(RcResources.Load(filename));
RecastBuilder builder = new RecastBuilder();
RcConfig cfg = new RcConfig(true, m_tileSize, m_tileSize,
RcConfig.CalcBorder(m_agentRadius, m_cellSize),

View File

@ -96,7 +96,7 @@ public class RecastSoloMeshTest
int expContours, int expVerts, int expPolys, int expDetMeshes, int expDetVerts, int expDetTris)
{
m_partitionType = partitionType;
IInputGeomProvider geomProvider = ObjImporter.Load(Loader.ToBytes(filename));
IInputGeomProvider geomProvider = ObjImporter.Load(RcResources.Load(filename));
long time = RcFrequency.Ticks;
RcVec3f bmin = geomProvider.GetMeshBoundsMin();
RcVec3f bmax = geomProvider.GetMeshBoundsMax();

View File

@ -58,7 +58,7 @@ public class RecastTileMeshTest
public void TestBuild(string filename)
{
IInputGeomProvider geom = ObjImporter.Load(Loader.ToBytes(filename));
IInputGeomProvider geom = ObjImporter.Load(RcResources.Load(filename));
RecastBuilder builder = new RecastBuilder();
RcConfig cfg = new RcConfig(
true, m_tileSize, m_tileSize, RcConfig.CalcBorder(m_agentRadius, m_cellSize),
@ -100,7 +100,7 @@ public class RecastTileMeshTest
[Test]
public void TestPerformance()
{
IInputGeomProvider geom = ObjImporter.Load(Loader.ToBytes("dungeon.obj"));
IInputGeomProvider geom = ObjImporter.Load(RcResources.Load("dungeon.obj"));
RecastBuilder builder = new RecastBuilder();
RcConfig cfg = new RcConfig(
true, m_tileSize, m_tileSize,