code cleanup in RcResources

This commit is contained in:
ikpil 2024-05-25 09:36:00 +09:00
parent aeefed7fbb
commit 7664ae9f3d
12 changed files with 46 additions and 38 deletions

View File

@ -61,6 +61,36 @@ namespace DotRecast.Core
return (int)s; 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) public static void Write(BinaryWriter ws, float value, RcByteOrder order)
{ {
byte[] bytes = BitConverter.GetBytes(value); byte[] bytes = BitConverter.GetBytes(value);

View File

@ -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;
}
}
}

View File

@ -41,7 +41,7 @@ namespace DotRecast.Recast.Toolset.Geom
public static DemoInputGeomProvider LoadFile(string objFilePath) public static DemoInputGeomProvider LoadFile(string objFilePath)
{ {
byte[] chunk = RcResources.Load(objFilePath); byte[] chunk = RcIO.ReadFileIfFound(objFilePath);
var context = RcObjImporter.LoadContext(chunk); var context = RcObjImporter.LoadContext(chunk);
return new DemoInputGeomProvider(context.vertexPositions, context.meshFaces); return new DemoInputGeomProvider(context.vertexPositions, context.meshFaces);
} }

View File

@ -39,7 +39,7 @@ namespace DotRecast.Recast.Geom
public static SimpleInputGeomProvider LoadFile(string objFilePath) public static SimpleInputGeomProvider LoadFile(string objFilePath)
{ {
byte[] chunk = RcResources.Load(objFilePath); byte[] chunk = RcIO.ReadFileIfFound(objFilePath);
var context = RcObjImporter.LoadContext(chunk); var context = RcObjImporter.LoadContext(chunk);
return new SimpleInputGeomProvider(context.vertexPositions, context.meshFaces); return new SimpleInputGeomProvider(context.vertexPositions, context.meshFaces);
} }

View File

@ -507,8 +507,8 @@ namespace DotRecast.Recast
for (int i = 0; i < 8; i++) for (int i = 0; i < 8; i++)
{ {
int vi = i * 3; int vi = i * 3;
if (vertices[vi] >= rectangle[0] && vertices[vi] < rectangle[2] && vertices[vi + 2] >= rectangle[1] if (vertices[vi] >= rectangle[0] && vertices[vi] < rectangle[2] &&
&& vertices[vi + 2] < rectangle[3]) vertices[vi + 2] >= rectangle[1] && vertices[vi + 2] < rectangle[3])
{ {
yMin = Math.Min(yMin, vertices[vi + 1]); yMin = Math.Min(yMin, vertices[vi + 1]);
yMax = Math.Max(yMax, vertices[vi + 1]); yMax = Math.Max(yMax, vertices[vi + 1]);

View File

@ -21,7 +21,7 @@ public class DynamicNavMeshTest
[Test] [Test]
public void E2eTest() 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 ms = new MemoryStream(bytes);
using var br = new BinaryReader(ms); using var br = new BinaryReader(ms);

View File

@ -31,7 +31,7 @@ public class VoxelFileReaderTest
[Test] [Test]
public void ShouldReadSingleTileFile() public void ShouldReadSingleTileFile()
{ {
byte[] bytes = RcResources.Load("test.voxels"); byte[] bytes = RcIO.ReadFileIfFound("test.voxels");
using var ms = new MemoryStream(bytes); using var ms = new MemoryStream(bytes);
using var br = new BinaryReader(ms); using var br = new BinaryReader(ms);
@ -57,7 +57,7 @@ public class VoxelFileReaderTest
[Test] [Test]
public void ShouldReadMultiTileFile() 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 ms = new MemoryStream(bytes);
using var br = new BinaryReader(ms); using var br = new BinaryReader(ms);

View File

@ -32,7 +32,7 @@ public class VoxelFileReaderWriterTest
[TestCase(true)] [TestCase(true)]
public void ShouldReadSingleTileFile(bool compression) 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 ms = new MemoryStream(bytes);
using var br = new BinaryReader(ms); using var br = new BinaryReader(ms);
@ -60,7 +60,7 @@ public class VoxelFileReaderWriterTest
[TestCase(true)] [TestCase(true)]
public void ShouldReadMultiTileFile(bool compression) 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 ms = new MemoryStream(bytes);
using var br = new BinaryReader(ms); using var br = new BinaryReader(ms);

View File

@ -93,7 +93,7 @@ public class VoxelQueryTest
private DtDynamicNavMesh CreateDynaMesh() 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 ms = new MemoryStream(bytes);
using var br = new BinaryReader(ms); using var br = new BinaryReader(ms);

View File

@ -31,7 +31,7 @@ public class MeshSetReaderTest
[Test] [Test]
public void TestNavmesh() 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 ms = new MemoryStream(@is);
using var br = new BinaryReader(ms); using var br = new BinaryReader(ms);
DtNavMesh mesh = reader.Read(br, 6); DtNavMesh mesh = reader.Read(br, 6);
@ -67,7 +67,7 @@ public class MeshSetReaderTest
[Test] [Test]
public void TestDungeon() 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 ms = new MemoryStream(@is);
using var br = new BinaryReader(ms); using var br = new BinaryReader(ms);
@ -104,7 +104,7 @@ public class MeshSetReaderTest
[Test] [Test]
public void TestDungeon32Bit() 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 ms = new MemoryStream(@is);
using var br = new BinaryReader(ms); using var br = new BinaryReader(ms);

View File

@ -35,7 +35,7 @@ public class TileCacheReaderTest
[Test] [Test]
public void TestNavmesh() 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); using var br = new BinaryReader(ms);
DtTileCache tc = reader.Read(br, 6, null); DtTileCache tc = reader.Read(br, 6, null);
Assert.That(tc.GetNavMesh().GetMaxTiles(), Is.EqualTo(256)); Assert.That(tc.GetNavMesh().GetMaxTiles(), Is.EqualTo(256));
@ -133,7 +133,7 @@ public class TileCacheReaderTest
[Test] [Test]
public void TestDungeon() 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); using var br = new BinaryReader(ms);
DtTileCache tc = reader.Read(br, 6, null); DtTileCache tc = reader.Read(br, 6, null);
Assert.That(tc.GetNavMesh().GetMaxTiles(), Is.EqualTo(256)); Assert.That(tc.GetNavMesh().GetMaxTiles(), Is.EqualTo(256));

View File

@ -39,7 +39,7 @@ public class TileCacheFindPathTest : AbstractTileCacheTest
public TileCacheFindPathTest() 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); using var br = new BinaryReader(msr);
DtTileCache tcC = new DtTileCacheReader(DtTileCacheCompressorFactory.Shared).Read(br, 6, new TestTileCacheMeshProcess()); DtTileCache tcC = new DtTileCacheReader(DtTileCacheCompressorFactory.Shared).Read(br, 6, new TestTileCacheMeshProcess());
navmesh = tcC.GetNavMesh(); navmesh = tcC.GetNavMesh();