From 7664ae9f3d0b8a55571d04ff623e4e9ece504708 Mon Sep 17 00:00:00 2001 From: ikpil Date: Sat, 25 May 2024 09:36:00 +0900 Subject: [PATCH] code cleanup in RcResources --- src/DotRecast.Core/RcIO.cs | 30 +++++++++++++++++++ src/DotRecast.Core/RcResources.cs | 22 -------------- .../Geom/DemoInputGeomProvider.cs | 2 +- .../Geom/SimpleInputGeomProvider.cs | 2 +- .../RcFilledVolumeRasterization.cs | 4 +-- .../DynamicNavMeshTest.cs | 2 +- .../Io/VoxelFileReaderTest.cs | 4 +-- .../Io/VoxelFileReaderWriterTest.cs | 4 +-- .../VoxelQueryTest.cs | 2 +- .../Io/MeshSetReaderTest.cs | 6 ++-- .../Io/TileCacheReaderTest.cs | 4 +-- .../TileCacheFindPathTest.cs | 2 +- 12 files changed, 46 insertions(+), 38 deletions(-) delete mode 100644 src/DotRecast.Core/RcResources.cs diff --git a/src/DotRecast.Core/RcIO.cs b/src/DotRecast.Core/RcIO.cs index ea1f268..80018bb 100644 --- a/src/DotRecast.Core/RcIO.cs +++ b/src/DotRecast.Core/RcIO.cs @@ -61,6 +61,36 @@ namespace DotRecast.Core return (int)s; } + public static byte[] ReadFileIfFound(string filename) + { + if (string.IsNullOrEmpty(filename)) + return null; + + string filePath = filename; + + if (!File.Exists(filePath)) + { + var searchFilePath = RcDirectory.SearchFile($"{filename}"); + if (!File.Exists(searchFilePath)) + { + searchFilePath = RcDirectory.SearchFile($"resources/{filename}"); + } + + if (File.Exists(searchFilePath)) + { + filePath = searchFilePath; + } + } + + using var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); + byte[] buffer = new byte[fs.Length]; + var read = fs.Read(buffer, 0, buffer.Length); + if (read != buffer.Length) + return null; + + return buffer; + } + public static void Write(BinaryWriter ws, float value, RcByteOrder order) { byte[] bytes = BitConverter.GetBytes(value); diff --git a/src/DotRecast.Core/RcResources.cs b/src/DotRecast.Core/RcResources.cs deleted file mode 100644 index 2b33f7c..0000000 --- a/src/DotRecast.Core/RcResources.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.IO; - -namespace DotRecast.Core -{ - public static class RcResources - { - public static byte[] Load(string filename) - { - var filepath = filename; - if (!File.Exists(filepath)) - { - 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; - } - } -} \ No newline at end of file diff --git a/src/DotRecast.Recast.Toolset/Geom/DemoInputGeomProvider.cs b/src/DotRecast.Recast.Toolset/Geom/DemoInputGeomProvider.cs index 50b3390..2161945 100644 --- a/src/DotRecast.Recast.Toolset/Geom/DemoInputGeomProvider.cs +++ b/src/DotRecast.Recast.Toolset/Geom/DemoInputGeomProvider.cs @@ -41,7 +41,7 @@ namespace DotRecast.Recast.Toolset.Geom public static DemoInputGeomProvider LoadFile(string objFilePath) { - byte[] chunk = RcResources.Load(objFilePath); + byte[] chunk = RcIO.ReadFileIfFound(objFilePath); var context = RcObjImporter.LoadContext(chunk); return new DemoInputGeomProvider(context.vertexPositions, context.meshFaces); } diff --git a/src/DotRecast.Recast/Geom/SimpleInputGeomProvider.cs b/src/DotRecast.Recast/Geom/SimpleInputGeomProvider.cs index ec08dd2..ed4477c 100644 --- a/src/DotRecast.Recast/Geom/SimpleInputGeomProvider.cs +++ b/src/DotRecast.Recast/Geom/SimpleInputGeomProvider.cs @@ -39,7 +39,7 @@ namespace DotRecast.Recast.Geom public static SimpleInputGeomProvider LoadFile(string objFilePath) { - byte[] chunk = RcResources.Load(objFilePath); + byte[] chunk = RcIO.ReadFileIfFound(objFilePath); var context = RcObjImporter.LoadContext(chunk); return new SimpleInputGeomProvider(context.vertexPositions, context.meshFaces); } diff --git a/src/DotRecast.Recast/RcFilledVolumeRasterization.cs b/src/DotRecast.Recast/RcFilledVolumeRasterization.cs index 7aa5c86..8c7a5c4 100644 --- a/src/DotRecast.Recast/RcFilledVolumeRasterization.cs +++ b/src/DotRecast.Recast/RcFilledVolumeRasterization.cs @@ -507,8 +507,8 @@ namespace DotRecast.Recast for (int i = 0; i < 8; i++) { int vi = i * 3; - if (vertices[vi] >= rectangle[0] && vertices[vi] < rectangle[2] && vertices[vi + 2] >= rectangle[1] - && vertices[vi + 2] < rectangle[3]) + if (vertices[vi] >= rectangle[0] && vertices[vi] < rectangle[2] && + vertices[vi + 2] >= rectangle[1] && vertices[vi + 2] < rectangle[3]) { yMin = Math.Min(yMin, vertices[vi + 1]); yMax = Math.Max(yMax, vertices[vi + 1]); diff --git a/test/DotRecast.Detour.Dynamic.Test/DynamicNavMeshTest.cs b/test/DotRecast.Detour.Dynamic.Test/DynamicNavMeshTest.cs index cbcaa04..2f11aee 100644 --- a/test/DotRecast.Detour.Dynamic.Test/DynamicNavMeshTest.cs +++ b/test/DotRecast.Detour.Dynamic.Test/DynamicNavMeshTest.cs @@ -21,7 +21,7 @@ public class DynamicNavMeshTest [Test] public void E2eTest() { - byte[] bytes = RcResources.Load("test_tiles.voxels"); + byte[] bytes = RcIO.ReadFileIfFound("test_tiles.voxels"); using var ms = new MemoryStream(bytes); using var br = new BinaryReader(ms); diff --git a/test/DotRecast.Detour.Dynamic.Test/Io/VoxelFileReaderTest.cs b/test/DotRecast.Detour.Dynamic.Test/Io/VoxelFileReaderTest.cs index 275330f..07e8b31 100644 --- a/test/DotRecast.Detour.Dynamic.Test/Io/VoxelFileReaderTest.cs +++ b/test/DotRecast.Detour.Dynamic.Test/Io/VoxelFileReaderTest.cs @@ -31,7 +31,7 @@ public class VoxelFileReaderTest [Test] public void ShouldReadSingleTileFile() { - byte[] bytes = RcResources.Load("test.voxels"); + byte[] bytes = RcIO.ReadFileIfFound("test.voxels"); using var ms = new MemoryStream(bytes); using var br = new BinaryReader(ms); @@ -57,7 +57,7 @@ public class VoxelFileReaderTest [Test] public void ShouldReadMultiTileFile() { - byte[] bytes = RcResources.Load("test_tiles.voxels"); + byte[] bytes = RcIO.ReadFileIfFound("test_tiles.voxels"); using var ms = new MemoryStream(bytes); using var br = new BinaryReader(ms); diff --git a/test/DotRecast.Detour.Dynamic.Test/Io/VoxelFileReaderWriterTest.cs b/test/DotRecast.Detour.Dynamic.Test/Io/VoxelFileReaderWriterTest.cs index 7ebf01a..ab3e79d 100644 --- a/test/DotRecast.Detour.Dynamic.Test/Io/VoxelFileReaderWriterTest.cs +++ b/test/DotRecast.Detour.Dynamic.Test/Io/VoxelFileReaderWriterTest.cs @@ -32,7 +32,7 @@ public class VoxelFileReaderWriterTest [TestCase(true)] public void ShouldReadSingleTileFile(bool compression) { - byte[] bytes = RcResources.Load("test.voxels"); + byte[] bytes = RcIO.ReadFileIfFound("test.voxels"); using var ms = new MemoryStream(bytes); using var br = new BinaryReader(ms); @@ -60,7 +60,7 @@ public class VoxelFileReaderWriterTest [TestCase(true)] public void ShouldReadMultiTileFile(bool compression) { - byte[] bytes = RcResources.Load("test_tiles.voxels"); + byte[] bytes = RcIO.ReadFileIfFound("test_tiles.voxels"); using var ms = new MemoryStream(bytes); using var br = new BinaryReader(ms); diff --git a/test/DotRecast.Detour.Dynamic.Test/VoxelQueryTest.cs b/test/DotRecast.Detour.Dynamic.Test/VoxelQueryTest.cs index 451de65..63b7a76 100644 --- a/test/DotRecast.Detour.Dynamic.Test/VoxelQueryTest.cs +++ b/test/DotRecast.Detour.Dynamic.Test/VoxelQueryTest.cs @@ -93,7 +93,7 @@ public class VoxelQueryTest private DtDynamicNavMesh CreateDynaMesh() { - var bytes = RcResources.Load("test_tiles.voxels"); + var bytes = RcIO.ReadFileIfFound("test_tiles.voxels"); using var ms = new MemoryStream(bytes); using var br = new BinaryReader(ms); diff --git a/test/DotRecast.Detour.Test/Io/MeshSetReaderTest.cs b/test/DotRecast.Detour.Test/Io/MeshSetReaderTest.cs index d93ffb9..a723b61 100644 --- a/test/DotRecast.Detour.Test/Io/MeshSetReaderTest.cs +++ b/test/DotRecast.Detour.Test/Io/MeshSetReaderTest.cs @@ -31,7 +31,7 @@ public class MeshSetReaderTest [Test] public void TestNavmesh() { - byte[] @is = RcResources.Load("all_tiles_navmesh.bin"); + byte[] @is = RcIO.ReadFileIfFound("all_tiles_navmesh.bin"); using var ms = new MemoryStream(@is); using var br = new BinaryReader(ms); DtNavMesh mesh = reader.Read(br, 6); @@ -67,7 +67,7 @@ public class MeshSetReaderTest [Test] public void TestDungeon() { - byte[] @is = RcResources.Load("dungeon_all_tiles_navmesh.bin"); + byte[] @is = RcIO.ReadFileIfFound("dungeon_all_tiles_navmesh.bin"); using var ms = new MemoryStream(@is); using var br = new BinaryReader(ms); @@ -104,7 +104,7 @@ public class MeshSetReaderTest [Test] public void TestDungeon32Bit() { - byte[] @is = RcResources.Load("dungeon_all_tiles_navmesh_32bit.bin"); + byte[] @is = RcIO.ReadFileIfFound("dungeon_all_tiles_navmesh_32bit.bin"); using var ms = new MemoryStream(@is); using var br = new BinaryReader(ms); diff --git a/test/DotRecast.Detour.TileCache.Test/Io/TileCacheReaderTest.cs b/test/DotRecast.Detour.TileCache.Test/Io/TileCacheReaderTest.cs index ba67a7f..96205a0 100644 --- a/test/DotRecast.Detour.TileCache.Test/Io/TileCacheReaderTest.cs +++ b/test/DotRecast.Detour.TileCache.Test/Io/TileCacheReaderTest.cs @@ -35,7 +35,7 @@ public class TileCacheReaderTest [Test] public void TestNavmesh() { - using var ms = new MemoryStream(RcResources.Load("all_tiles_tilecache.bin")); + using var ms = new MemoryStream(RcIO.ReadFileIfFound("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(RcResources.Load("dungeon_all_tiles_tilecache.bin")); + using var ms = new MemoryStream(RcIO.ReadFileIfFound("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)); diff --git a/test/DotRecast.Detour.TileCache.Test/TileCacheFindPathTest.cs b/test/DotRecast.Detour.TileCache.Test/TileCacheFindPathTest.cs index eab34e0..0bf3f7a 100644 --- a/test/DotRecast.Detour.TileCache.Test/TileCacheFindPathTest.cs +++ b/test/DotRecast.Detour.TileCache.Test/TileCacheFindPathTest.cs @@ -39,7 +39,7 @@ public class TileCacheFindPathTest : AbstractTileCacheTest public TileCacheFindPathTest() { - using var msr = new MemoryStream(RcResources.Load("dungeon_all_tiles_tilecache.bin")); + using var msr = new MemoryStream(RcIO.ReadFileIfFound("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();