From 886be3712f74f47e5d1ec07f8560475eada43f65 Mon Sep 17 00:00:00 2001 From: ikpil Date: Thu, 23 May 2024 22:39:03 +0900 Subject: [PATCH] Changed `DtWriter` to a static class and renamed it to `RcIO` --- CHANGELOG.md | 1 + src/DotRecast.Core/RcIO.cs | 135 ++++++++++++++++++ .../Io/DtVoxelFileReader.cs | 4 +- .../Io/DtVoxelFileWriter.cs | 87 ++++++----- .../Unity/Astar/ZipBinaryReader.cs | 2 +- .../Io/DtTileCacheLayerHeaderWriter.cs | 44 +++--- .../Io/DtTileCacheReader.cs | 4 +- .../Io/DtTileCacheWriter.cs | 38 ++--- src/DotRecast.Detour/Io/DtMeshDataReader.cs | 10 +- src/DotRecast.Detour/Io/DtMeshDataWriter.cs | 104 +++++++------- src/DotRecast.Detour/Io/DtMeshSetReader.cs | 8 +- src/DotRecast.Detour/Io/DtMeshSetWriter.cs | 16 +-- .../Io/DtNavMeshParamWriter.cs | 16 +-- src/DotRecast.Detour/Io/DtWriter.cs | 98 ------------- src/DotRecast.Detour/Io/IOUtils.cs | 64 --------- .../AbstractDetourTest.cs | 1 + 16 files changed, 303 insertions(+), 329 deletions(-) create mode 100644 src/DotRecast.Core/RcIO.cs delete mode 100644 src/DotRecast.Detour/Io/DtWriter.cs delete mode 100644 src/DotRecast.Detour/Io/IOUtils.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f8f729..9f0176e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Changed `Dictionary>` to `DtMeshTile[]` to optimize memory usage - Changed `MAX_STEER_POINTS` from class constant to local. - Changed `List` to `Span` for enhanced memory efficiency +- Changed `DtWriter` to a static class and renamed it to `RcIO` ### Removed - Nothing diff --git a/src/DotRecast.Core/RcIO.cs b/src/DotRecast.Core/RcIO.cs new file mode 100644 index 0000000..ea1f268 --- /dev/null +++ b/src/DotRecast.Core/RcIO.cs @@ -0,0 +1,135 @@ +/* +Recast4J Copyright (c) 2015 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 System; +using System.IO; + +namespace DotRecast.Core +{ + public static class RcIO + { + public static RcByteBuffer ToByteBuffer(BinaryReader br, bool direct) + { + byte[] data = ToByteArray(br); + if (direct) + { + Array.Reverse(data); + } + + return new RcByteBuffer(data); + } + + public static byte[] ToByteArray(BinaryReader br) + { + using var ms = new MemoryStream(); + Span buffer = stackalloc byte[4096]; + int l; + while ((l = br.Read(buffer)) > 0) + { + ms.Write(buffer.Slice(0, l)); + } + + return ms.ToArray(); + } + + + public static RcByteBuffer ToByteBuffer(BinaryReader br) + { + var bytes = ToByteArray(br); + return new RcByteBuffer(bytes); + } + + public static int SwapEndianness(int i) + { + var s = (((uint)i >> 24) & 0xFF) | (((uint)i >> 8) & 0xFF00) | (((uint)i << 8) & 0xFF0000) | ((i << 24) & 0xFF000000); + return (int)s; + } + + public static void Write(BinaryWriter ws, float value, RcByteOrder order) + { + byte[] bytes = BitConverter.GetBytes(value); + int i = BitConverter.ToInt32(bytes, 0); + Write(ws, i, order); + } + + public static void Write(BinaryWriter ws, short value, RcByteOrder order) + { + if (order == RcByteOrder.BIG_ENDIAN) + { + ws.Write((byte)((value >> 8) & 0xFF)); + ws.Write((byte)(value & 0xFF)); + } + else + { + ws.Write((byte)(value & 0xFF)); + ws.Write((byte)((value >> 8) & 0xFF)); + } + } + + public static void Write(BinaryWriter ws, long value, RcByteOrder order) + { + if (order == RcByteOrder.BIG_ENDIAN) + { + Write(ws, (int)((ulong)value >> 32), order); + Write(ws, (int)(value & 0xFFFFFFFF), order); + } + else + { + Write(ws, (int)(value & 0xFFFFFFFF), order); + Write(ws, (int)((ulong)value >> 32), order); + } + } + + public static void Write(BinaryWriter ws, int value, RcByteOrder order) + { + if (order == RcByteOrder.BIG_ENDIAN) + { + ws.Write((byte)((value >> 24) & 0xFF)); + ws.Write((byte)((value >> 16) & 0xFF)); + ws.Write((byte)((value >> 8) & 0xFF)); + ws.Write((byte)(value & 0xFF)); + } + else + { + ws.Write((byte)(value & 0xFF)); + ws.Write((byte)((value >> 8) & 0xFF)); + ws.Write((byte)((value >> 16) & 0xFF)); + ws.Write((byte)((value >> 24) & 0xFF)); + } + } + + public static void Write(BinaryWriter ws, bool value) + { + Write(ws, (byte)(value ? 1 : 0)); + } + + public static void Write(BinaryWriter ws, byte value) + { + ws.Write(value); + } + + public static void Write(BinaryWriter ws, MemoryStream ms) + { + ms.Position = 0; + byte[] buffer = new byte[ms.Length]; + ms.Read(buffer, 0, buffer.Length); + ws.Write(buffer); + } + } +} \ No newline at end of file diff --git a/src/DotRecast.Detour.Dynamic/Io/DtVoxelFileReader.cs b/src/DotRecast.Detour.Dynamic/Io/DtVoxelFileReader.cs index c63f285..1f886ab 100644 --- a/src/DotRecast.Detour.Dynamic/Io/DtVoxelFileReader.cs +++ b/src/DotRecast.Detour.Dynamic/Io/DtVoxelFileReader.cs @@ -35,12 +35,12 @@ namespace DotRecast.Detour.Dynamic.Io public DtVoxelFile Read(BinaryReader stream) { - RcByteBuffer buf = IOUtils.ToByteBuffer(stream); + RcByteBuffer buf = RcIO.ToByteBuffer(stream); DtVoxelFile file = new DtVoxelFile(); int magic = buf.GetInt(); if (magic != DtVoxelFile.MAGIC) { - magic = IOUtils.SwapEndianness(magic); + magic = RcIO.SwapEndianness(magic); if (magic != DtVoxelFile.MAGIC) { throw new IOException("Invalid magic"); diff --git a/src/DotRecast.Detour.Dynamic/Io/DtVoxelFileWriter.cs b/src/DotRecast.Detour.Dynamic/Io/DtVoxelFileWriter.cs index 70ed7b2..0a5cdad 100644 --- a/src/DotRecast.Detour.Dynamic/Io/DtVoxelFileWriter.cs +++ b/src/DotRecast.Detour.Dynamic/Io/DtVoxelFileWriter.cs @@ -19,12 +19,11 @@ freely, subject to the following restrictions: using System.IO; using DotRecast.Core; -using DotRecast.Core.Numerics; using DotRecast.Detour.Io; namespace DotRecast.Detour.Dynamic.Io { - public class DtVoxelFileWriter : DtWriter + public class DtVoxelFileWriter { private readonly IRcCompressor _compressor; @@ -40,34 +39,34 @@ namespace DotRecast.Detour.Dynamic.Io public void Write(BinaryWriter stream, DtVoxelFile f, RcByteOrder byteOrder, bool compression) { - Write(stream, DtVoxelFile.MAGIC, byteOrder); - Write(stream, DtVoxelFile.VERSION_EXPORTER_RECAST4J | (compression ? DtVoxelFile.VERSION_COMPRESSION_LZ4 : 0), byteOrder); - Write(stream, f.walkableRadius, byteOrder); - Write(stream, f.walkableHeight, byteOrder); - Write(stream, f.walkableClimb, byteOrder); - Write(stream, f.walkableSlopeAngle, byteOrder); - Write(stream, f.cellSize, byteOrder); - Write(stream, f.maxSimplificationError, byteOrder); - Write(stream, f.maxEdgeLen, byteOrder); - Write(stream, f.minRegionArea, byteOrder); - Write(stream, f.regionMergeArea, byteOrder); - Write(stream, f.vertsPerPoly, byteOrder); - Write(stream, f.buildMeshDetail); - Write(stream, f.detailSampleDistance, byteOrder); - Write(stream, f.detailSampleMaxError, byteOrder); - Write(stream, f.useTiles); - Write(stream, f.tileSizeX, byteOrder); - Write(stream, f.tileSizeZ, byteOrder); - Write(stream, f.rotation.X, byteOrder); - Write(stream, f.rotation.Y, byteOrder); - Write(stream, f.rotation.Z, byteOrder); - Write(stream, f.bounds[0], byteOrder); - Write(stream, f.bounds[1], byteOrder); - Write(stream, f.bounds[2], byteOrder); - Write(stream, f.bounds[3], byteOrder); - Write(stream, f.bounds[4], byteOrder); - Write(stream, f.bounds[5], byteOrder); - Write(stream, f.tiles.Count, byteOrder); + RcIO.Write(stream, DtVoxelFile.MAGIC, byteOrder); + RcIO.Write(stream, DtVoxelFile.VERSION_EXPORTER_RECAST4J | (compression ? DtVoxelFile.VERSION_COMPRESSION_LZ4 : 0), byteOrder); + RcIO.Write(stream, f.walkableRadius, byteOrder); + RcIO.Write(stream, f.walkableHeight, byteOrder); + RcIO.Write(stream, f.walkableClimb, byteOrder); + RcIO.Write(stream, f.walkableSlopeAngle, byteOrder); + RcIO.Write(stream, f.cellSize, byteOrder); + RcIO.Write(stream, f.maxSimplificationError, byteOrder); + RcIO.Write(stream, f.maxEdgeLen, byteOrder); + RcIO.Write(stream, f.minRegionArea, byteOrder); + RcIO.Write(stream, f.regionMergeArea, byteOrder); + RcIO.Write(stream, f.vertsPerPoly, byteOrder); + RcIO.Write(stream, f.buildMeshDetail); + RcIO.Write(stream, f.detailSampleDistance, byteOrder); + RcIO.Write(stream, f.detailSampleMaxError, byteOrder); + RcIO.Write(stream, f.useTiles); + RcIO.Write(stream, f.tileSizeX, byteOrder); + RcIO.Write(stream, f.tileSizeZ, byteOrder); + RcIO.Write(stream, f.rotation.X, byteOrder); + RcIO.Write(stream, f.rotation.Y, byteOrder); + RcIO.Write(stream, f.rotation.Z, byteOrder); + RcIO.Write(stream, f.bounds[0], byteOrder); + RcIO.Write(stream, f.bounds[1], byteOrder); + RcIO.Write(stream, f.bounds[2], byteOrder); + RcIO.Write(stream, f.bounds[3], byteOrder); + RcIO.Write(stream, f.bounds[4], byteOrder); + RcIO.Write(stream, f.bounds[5], byteOrder); + RcIO.Write(stream, f.tiles.Count, byteOrder); foreach (DtVoxelTile t in f.tiles) { WriteTile(stream, t, byteOrder, compression); @@ -76,26 +75,26 @@ namespace DotRecast.Detour.Dynamic.Io public void WriteTile(BinaryWriter stream, DtVoxelTile tile, RcByteOrder byteOrder, bool compression) { - Write(stream, tile.tileX, byteOrder); - Write(stream, tile.tileZ, byteOrder); - Write(stream, tile.width, byteOrder); - Write(stream, tile.depth, byteOrder); - Write(stream, tile.borderSize, byteOrder); - Write(stream, tile.boundsMin.X, byteOrder); - Write(stream, tile.boundsMin.Y, byteOrder); - Write(stream, tile.boundsMin.Z, byteOrder); - Write(stream, tile.boundsMax.X, byteOrder); - Write(stream, tile.boundsMax.Y, byteOrder); - Write(stream, tile.boundsMax.Z, byteOrder); - Write(stream, tile.cellSize, byteOrder); - Write(stream, tile.cellHeight, byteOrder); + RcIO.Write(stream, tile.tileX, byteOrder); + RcIO.Write(stream, tile.tileZ, byteOrder); + RcIO.Write(stream, tile.width, byteOrder); + RcIO.Write(stream, tile.depth, byteOrder); + RcIO.Write(stream, tile.borderSize, byteOrder); + RcIO.Write(stream, tile.boundsMin.X, byteOrder); + RcIO.Write(stream, tile.boundsMin.Y, byteOrder); + RcIO.Write(stream, tile.boundsMin.Z, byteOrder); + RcIO.Write(stream, tile.boundsMax.X, byteOrder); + RcIO.Write(stream, tile.boundsMax.Y, byteOrder); + RcIO.Write(stream, tile.boundsMax.Z, byteOrder); + RcIO.Write(stream, tile.cellSize, byteOrder); + RcIO.Write(stream, tile.cellHeight, byteOrder); byte[] bytes = tile.spanData; if (compression) { bytes = _compressor.Compress(bytes); } - Write(stream, bytes.Length, byteOrder); + RcIO.Write(stream, bytes.Length, byteOrder); stream.Write(bytes); } } diff --git a/src/DotRecast.Detour.Extras/Unity/Astar/ZipBinaryReader.cs b/src/DotRecast.Detour.Extras/Unity/Astar/ZipBinaryReader.cs index b7a2f9f..6abc219 100644 --- a/src/DotRecast.Detour.Extras/Unity/Astar/ZipBinaryReader.cs +++ b/src/DotRecast.Detour.Extras/Unity/Astar/ZipBinaryReader.cs @@ -31,7 +31,7 @@ namespace DotRecast.Detour.Extras.Unity.Astar ZipArchiveEntry graphReferences = file.GetEntry(filename); using var entryStream = graphReferences.Open(); using var br = new BinaryReader(entryStream); - RcByteBuffer buffer = IOUtils.ToByteBuffer(br); + RcByteBuffer buffer = RcIO.ToByteBuffer(br); buffer.Order(RcByteOrder.LITTLE_ENDIAN); return buffer; } diff --git a/src/DotRecast.Detour.TileCache/Io/DtTileCacheLayerHeaderWriter.cs b/src/DotRecast.Detour.TileCache/Io/DtTileCacheLayerHeaderWriter.cs index 444888d..386bba6 100644 --- a/src/DotRecast.Detour.TileCache/Io/DtTileCacheLayerHeaderWriter.cs +++ b/src/DotRecast.Detour.TileCache/Io/DtTileCacheLayerHeaderWriter.cs @@ -24,34 +24,34 @@ using DotRecast.Detour.Io; namespace DotRecast.Detour.TileCache.Io { - public class DtTileCacheLayerHeaderWriter : DtWriter + public class DtTileCacheLayerHeaderWriter { public void Write(BinaryWriter stream, DtTileCacheLayerHeader header, RcByteOrder order, bool cCompatibility) { - Write(stream, header.magic, order); - Write(stream, header.version, order); - Write(stream, header.tx, order); - Write(stream, header.ty, order); - Write(stream, header.tlayer, order); - - Write(stream, header.bmin.X, order); - Write(stream, header.bmin.Y, order); - Write(stream, header.bmin.Z, order); - Write(stream, header.bmax.X, order); - Write(stream, header.bmax.Y, order); - Write(stream, header.bmax.Z, order); + RcIO.Write(stream, header.magic, order); + RcIO.Write(stream, header.version, order); + RcIO.Write(stream, header.tx, order); + RcIO.Write(stream, header.ty, order); + RcIO.Write(stream, header.tlayer, order); - Write(stream, (short)header.hmin, order); - Write(stream, (short)header.hmax, order); - Write(stream, (byte)header.width); - Write(stream, (byte)header.height); - Write(stream, (byte)header.minx); - Write(stream, (byte)header.maxx); - Write(stream, (byte)header.miny); - Write(stream, (byte)header.maxy); + RcIO.Write(stream, header.bmin.X, order); + RcIO.Write(stream, header.bmin.Y, order); + RcIO.Write(stream, header.bmin.Z, order); + RcIO.Write(stream, header.bmax.X, order); + RcIO.Write(stream, header.bmax.Y, order); + RcIO.Write(stream, header.bmax.Z, order); + + RcIO.Write(stream, (short)header.hmin, order); + RcIO.Write(stream, (short)header.hmax, order); + RcIO.Write(stream, (byte)header.width); + RcIO.Write(stream, (byte)header.height); + RcIO.Write(stream, (byte)header.minx); + RcIO.Write(stream, (byte)header.maxx); + RcIO.Write(stream, (byte)header.miny); + RcIO.Write(stream, (byte)header.maxy); if (cCompatibility) { - Write(stream, (short)0, order); // C struct padding + RcIO.Write(stream, (short)0, order); // C struct padding } } } diff --git a/src/DotRecast.Detour.TileCache/Io/DtTileCacheReader.cs b/src/DotRecast.Detour.TileCache/Io/DtTileCacheReader.cs index 43f3d1a..09aa6a2 100644 --- a/src/DotRecast.Detour.TileCache/Io/DtTileCacheReader.cs +++ b/src/DotRecast.Detour.TileCache/Io/DtTileCacheReader.cs @@ -37,7 +37,7 @@ namespace DotRecast.Detour.TileCache.Io public DtTileCache Read(BinaryReader @is, int maxVertPerPoly, IDtTileCacheMeshProcess meshProcessor) { - RcByteBuffer bb = IOUtils.ToByteBuffer(@is); + RcByteBuffer bb = RcIO.ToByteBuffer(@is); return Read(bb, maxVertPerPoly, meshProcessor); } @@ -47,7 +47,7 @@ namespace DotRecast.Detour.TileCache.Io header.magic = bb.GetInt(); if (header.magic != DtTileCacheSetHeader.TILECACHESET_MAGIC) { - header.magic = IOUtils.SwapEndianness(header.magic); + header.magic = RcIO.SwapEndianness(header.magic); if (header.magic != DtTileCacheSetHeader.TILECACHESET_MAGIC) { throw new IOException("Invalid magic"); diff --git a/src/DotRecast.Detour.TileCache/Io/DtTileCacheWriter.cs b/src/DotRecast.Detour.TileCache/Io/DtTileCacheWriter.cs index 14438d5..74d4582 100644 --- a/src/DotRecast.Detour.TileCache/Io/DtTileCacheWriter.cs +++ b/src/DotRecast.Detour.TileCache/Io/DtTileCacheWriter.cs @@ -25,7 +25,7 @@ using DotRecast.Detour.TileCache.Io.Compress; namespace DotRecast.Detour.TileCache.Io { - public class DtTileCacheWriter : DtWriter + public class DtTileCacheWriter { private readonly DtNavMeshParamWriter paramWriter = new DtNavMeshParamWriter(); private readonly IDtTileCacheCompressorFactory _compFactory; @@ -38,8 +38,8 @@ namespace DotRecast.Detour.TileCache.Io public void Write(BinaryWriter stream, DtTileCache cache, RcByteOrder order, bool cCompatibility) { - Write(stream, DtTileCacheSetHeader.TILECACHESET_MAGIC, order); - Write(stream, cCompatibility + RcIO.Write(stream, DtTileCacheSetHeader.TILECACHESET_MAGIC, order); + RcIO.Write(stream, cCompatibility ? DtTileCacheSetHeader.TILECACHESET_VERSION : DtTileCacheSetHeader.TILECACHESET_VERSION_RECAST4J, order); int numTiles = 0; @@ -51,7 +51,7 @@ namespace DotRecast.Detour.TileCache.Io numTiles++; } - Write(stream, numTiles, order); + RcIO.Write(stream, numTiles, order); paramWriter.Write(stream, cache.GetNavMesh().GetParams(), order); WriteCacheParams(stream, cache.GetParams(), order); for (int i = 0; i < cache.GetTileCount(); i++) @@ -59,32 +59,32 @@ namespace DotRecast.Detour.TileCache.Io DtCompressedTile tile = cache.GetTile(i); if (tile == null || tile.data == null) continue; - Write(stream, (int)cache.GetTileRef(tile), order); + RcIO.Write(stream, (int)cache.GetTileRef(tile), order); byte[] data = tile.data; DtTileCacheLayer layer = cache.DecompressTile(tile); var comp = _compFactory.Create(cCompatibility ? 0 : 1); data = DtTileCacheBuilder.CompressTileCacheLayer(comp, layer, order, cCompatibility); - Write(stream, data.Length, order); + RcIO.Write(stream, data.Length, order); stream.Write(data); } } private void WriteCacheParams(BinaryWriter stream, DtTileCacheParams option, RcByteOrder order) { - Write(stream, option.orig.X, order); - Write(stream, option.orig.Y, order); - Write(stream, option.orig.Z, order); + RcIO.Write(stream, option.orig.X, order); + RcIO.Write(stream, option.orig.Y, order); + RcIO.Write(stream, option.orig.Z, order); - Write(stream, option.cs, order); - Write(stream, option.ch, order); - Write(stream, option.width, order); - Write(stream, option.height, order); - Write(stream, option.walkableHeight, order); - Write(stream, option.walkableRadius, order); - Write(stream, option.walkableClimb, order); - Write(stream, option.maxSimplificationError, order); - Write(stream, option.maxTiles, order); - Write(stream, option.maxObstacles, order); + RcIO.Write(stream, option.cs, order); + RcIO.Write(stream, option.ch, order); + RcIO.Write(stream, option.width, order); + RcIO.Write(stream, option.height, order); + RcIO.Write(stream, option.walkableHeight, order); + RcIO.Write(stream, option.walkableRadius, order); + RcIO.Write(stream, option.walkableClimb, order); + RcIO.Write(stream, option.maxSimplificationError, order); + RcIO.Write(stream, option.maxTiles, order); + RcIO.Write(stream, option.maxObstacles, order); } } } \ No newline at end of file diff --git a/src/DotRecast.Detour/Io/DtMeshDataReader.cs b/src/DotRecast.Detour/Io/DtMeshDataReader.cs index 0ae0c2d..a258c5d 100644 --- a/src/DotRecast.Detour/Io/DtMeshDataReader.cs +++ b/src/DotRecast.Detour/Io/DtMeshDataReader.cs @@ -26,10 +26,12 @@ namespace DotRecast.Detour.Io public class DtMeshDataReader { public const int DT_POLY_DETAIL_SIZE = 10; + public const int LINK_SIZEOF = 16; + public const int LINK_SIZEOF32BIT = 12; public DtMeshData Read(BinaryReader stream, int maxVertPerPoly) { - RcByteBuffer buf = IOUtils.ToByteBuffer(stream); + RcByteBuffer buf = RcIO.ToByteBuffer(stream); return Read(buf, maxVertPerPoly, false); } @@ -40,7 +42,7 @@ namespace DotRecast.Detour.Io public DtMeshData Read32Bit(BinaryReader stream, int maxVertPerPoly) { - RcByteBuffer buf = IOUtils.ToByteBuffer(stream); + RcByteBuffer buf = RcIO.ToByteBuffer(stream); return Read(buf, maxVertPerPoly, true); } @@ -57,7 +59,7 @@ namespace DotRecast.Detour.Io header.magic = buf.GetInt(); if (header.magic != DT_NAVMESH_MAGIC) { - header.magic = IOUtils.SwapEndianness(header.magic); + header.magic = RcIO.SwapEndianness(header.magic); if (header.magic != DT_NAVMESH_MAGIC) { throw new IOException("Invalid magic"); @@ -118,8 +120,6 @@ namespace DotRecast.Detour.Io return data; } - public const int LINK_SIZEOF = 16; - public const int LINK_SIZEOF32BIT = 12; public static int GetSizeofLink(bool is32Bit) { diff --git a/src/DotRecast.Detour/Io/DtMeshDataWriter.cs b/src/DotRecast.Detour/Io/DtMeshDataWriter.cs index 4189676..fa70e7b 100644 --- a/src/DotRecast.Detour/Io/DtMeshDataWriter.cs +++ b/src/DotRecast.Detour/Io/DtMeshDataWriter.cs @@ -23,36 +23,36 @@ namespace DotRecast.Detour.Io { using static DtDetour; - public class DtMeshDataWriter : DtWriter + public class DtMeshDataWriter { public void Write(BinaryWriter stream, DtMeshData data, RcByteOrder order, bool cCompatibility) { DtMeshHeader header = data.header; - Write(stream, header.magic, order); - Write(stream, cCompatibility ? DT_NAVMESH_VERSION : DT_NAVMESH_VERSION_RECAST4J_LAST, order); - Write(stream, header.x, order); - Write(stream, header.y, order); - Write(stream, header.layer, order); - Write(stream, header.userId, order); - Write(stream, header.polyCount, order); - Write(stream, header.vertCount, order); - Write(stream, header.maxLinkCount, order); - Write(stream, header.detailMeshCount, order); - Write(stream, header.detailVertCount, order); - Write(stream, header.detailTriCount, order); - Write(stream, header.bvNodeCount, order); - Write(stream, header.offMeshConCount, order); - Write(stream, header.offMeshBase, order); - Write(stream, header.walkableHeight, order); - Write(stream, header.walkableRadius, order); - Write(stream, header.walkableClimb, order); - Write(stream, header.bmin.X, order); - Write(stream, header.bmin.Y, order); - Write(stream, header.bmin.Z, order); - Write(stream, header.bmax.X, order); - Write(stream, header.bmax.Y, order); - Write(stream, header.bmax.Z, order); - Write(stream, header.bvQuantFactor, order); + RcIO.Write(stream, header.magic, order); + RcIO.Write(stream, cCompatibility ? DT_NAVMESH_VERSION : DT_NAVMESH_VERSION_RECAST4J_LAST, order); + RcIO.Write(stream, header.x, order); + RcIO.Write(stream, header.y, order); + RcIO.Write(stream, header.layer, order); + RcIO.Write(stream, header.userId, order); + RcIO.Write(stream, header.polyCount, order); + RcIO.Write(stream, header.vertCount, order); + RcIO.Write(stream, header.maxLinkCount, order); + RcIO.Write(stream, header.detailMeshCount, order); + RcIO.Write(stream, header.detailVertCount, order); + RcIO.Write(stream, header.detailTriCount, order); + RcIO.Write(stream, header.bvNodeCount, order); + RcIO.Write(stream, header.offMeshConCount, order); + RcIO.Write(stream, header.offMeshBase, order); + RcIO.Write(stream, header.walkableHeight, order); + RcIO.Write(stream, header.walkableRadius, order); + RcIO.Write(stream, header.walkableClimb, order); + RcIO.Write(stream, header.bmin.X, order); + RcIO.Write(stream, header.bmin.Y, order); + RcIO.Write(stream, header.bmin.Z, order); + RcIO.Write(stream, header.bmax.X, order); + RcIO.Write(stream, header.bmax.Y, order); + RcIO.Write(stream, header.bmax.Z, order); + RcIO.Write(stream, header.bvQuantFactor, order); WriteVerts(stream, data.verts, header.vertCount, order); WritePolys(stream, data, order, cCompatibility); if (cCompatibility) @@ -72,7 +72,7 @@ namespace DotRecast.Detour.Io { for (int i = 0; i < count * 3; i++) { - Write(stream, verts[i], order); + RcIO.Write(stream, verts[i], order); } } @@ -82,22 +82,22 @@ namespace DotRecast.Detour.Io { if (cCompatibility) { - Write(stream, 0xFFFF, order); + RcIO.Write(stream, 0xFFFF, order); } for (int j = 0; j < data.polys[i].verts.Length; j++) { - Write(stream, (short)data.polys[i].verts[j], order); + RcIO.Write(stream, (short)data.polys[i].verts[j], order); } for (int j = 0; j < data.polys[i].neis.Length; j++) { - Write(stream, (short)data.polys[i].neis[j], order); + RcIO.Write(stream, (short)data.polys[i].neis[j], order); } - Write(stream, (short)data.polys[i].flags, order); - Write(stream, (byte)data.polys[i].vertCount); - Write(stream, (byte)data.polys[i].areaAndtype); + RcIO.Write(stream, (short)data.polys[i].flags, order); + RcIO.Write(stream, (byte)data.polys[i].vertCount); + RcIO.Write(stream, (byte)data.polys[i].areaAndtype); } } @@ -105,13 +105,13 @@ namespace DotRecast.Detour.Io { for (int i = 0; i < data.header.detailMeshCount; i++) { - Write(stream, data.detailMeshes[i].vertBase, order); - Write(stream, data.detailMeshes[i].triBase, order); - Write(stream, (byte)data.detailMeshes[i].vertCount); - Write(stream, (byte)data.detailMeshes[i].triCount); + RcIO.Write(stream, data.detailMeshes[i].vertBase, order); + RcIO.Write(stream, data.detailMeshes[i].triBase, order); + RcIO.Write(stream, (byte)data.detailMeshes[i].vertCount); + RcIO.Write(stream, (byte)data.detailMeshes[i].triCount); if (cCompatibility) { - Write(stream, (short)0, order); + RcIO.Write(stream, (short)0, order); } } } @@ -120,7 +120,7 @@ namespace DotRecast.Detour.Io { for (int i = 0; i < data.header.detailTriCount * 4; i++) { - Write(stream, (byte)data.detailTris[i]); + RcIO.Write(stream, (byte)data.detailTris[i]); } } @@ -132,28 +132,28 @@ namespace DotRecast.Detour.Io { for (int j = 0; j < 3; j++) { - Write(stream, (short)data.bvTree[i].bmin[j], order); + RcIO.Write(stream, (short)data.bvTree[i].bmin[j], order); } for (int j = 0; j < 3; j++) { - Write(stream, (short)data.bvTree[i].bmax[j], order); + RcIO.Write(stream, (short)data.bvTree[i].bmax[j], order); } } else { for (int j = 0; j < 3; j++) { - Write(stream, data.bvTree[i].bmin[j], order); + RcIO.Write(stream, data.bvTree[i].bmin[j], order); } for (int j = 0; j < 3; j++) { - Write(stream, data.bvTree[i].bmax[j], order); + RcIO.Write(stream, data.bvTree[i].bmax[j], order); } } - Write(stream, data.bvTree[i].i, order); + RcIO.Write(stream, data.bvTree[i].i, order); } } @@ -163,16 +163,16 @@ namespace DotRecast.Detour.Io { for (int j = 0; j < 2; j++) { - Write(stream, data.offMeshCons[i].pos[j].X, order); - Write(stream, data.offMeshCons[i].pos[j].Y, order); - Write(stream, data.offMeshCons[i].pos[j].Z, order); + RcIO.Write(stream, data.offMeshCons[i].pos[j].X, order); + RcIO.Write(stream, data.offMeshCons[i].pos[j].Y, order); + RcIO.Write(stream, data.offMeshCons[i].pos[j].Z, order); } - Write(stream, data.offMeshCons[i].rad, order); - Write(stream, (short)data.offMeshCons[i].poly, order); - Write(stream, (byte)data.offMeshCons[i].flags); - Write(stream, (byte)data.offMeshCons[i].side); - Write(stream, data.offMeshCons[i].userId, order); + RcIO.Write(stream, data.offMeshCons[i].rad, order); + RcIO.Write(stream, (short)data.offMeshCons[i].poly, order); + RcIO.Write(stream, (byte)data.offMeshCons[i].flags); + RcIO.Write(stream, (byte)data.offMeshCons[i].side); + RcIO.Write(stream, data.offMeshCons[i].userId, order); } } } diff --git a/src/DotRecast.Detour/Io/DtMeshSetReader.cs b/src/DotRecast.Detour/Io/DtMeshSetReader.cs index a9964d0..8e0714d 100644 --- a/src/DotRecast.Detour/Io/DtMeshSetReader.cs +++ b/src/DotRecast.Detour/Io/DtMeshSetReader.cs @@ -31,7 +31,7 @@ namespace DotRecast.Detour.Io public DtNavMesh Read(BinaryReader @is, int maxVertPerPoly) { - return Read(IOUtils.ToByteBuffer(@is), maxVertPerPoly, false); + return Read(RcIO.ToByteBuffer(@is), maxVertPerPoly, false); } public DtNavMesh Read(RcByteBuffer bb, int maxVertPerPoly) @@ -41,7 +41,7 @@ namespace DotRecast.Detour.Io public DtNavMesh Read32Bit(BinaryReader @is, int maxVertPerPoly) { - return Read(IOUtils.ToByteBuffer(@is), maxVertPerPoly, true); + return Read(RcIO.ToByteBuffer(@is), maxVertPerPoly, true); } public DtNavMesh Read32Bit(RcByteBuffer bb, int maxVertPerPoly) @@ -51,7 +51,7 @@ namespace DotRecast.Detour.Io public DtNavMesh Read(BinaryReader @is) { - return Read(IOUtils.ToByteBuffer(@is)); + return Read(RcIO.ToByteBuffer(@is)); } public DtNavMesh Read(RcByteBuffer bb) @@ -80,7 +80,7 @@ namespace DotRecast.Detour.Io header.magic = bb.GetInt(); if (header.magic != NavMeshSetHeader.NAVMESHSET_MAGIC) { - header.magic = IOUtils.SwapEndianness(header.magic); + header.magic = RcIO.SwapEndianness(header.magic); if (header.magic != NavMeshSetHeader.NAVMESHSET_MAGIC) { throw new IOException("Invalid magic " + header.magic); diff --git a/src/DotRecast.Detour/Io/DtMeshSetWriter.cs b/src/DotRecast.Detour/Io/DtMeshSetWriter.cs index 4568813..0d0ec88 100644 --- a/src/DotRecast.Detour/Io/DtMeshSetWriter.cs +++ b/src/DotRecast.Detour/Io/DtMeshSetWriter.cs @@ -22,7 +22,7 @@ using DotRecast.Core.Numerics; namespace DotRecast.Detour.Io { - public class DtMeshSetWriter : DtWriter + public class DtMeshSetWriter { private readonly DtMeshDataWriter writer = new DtMeshDataWriter(); private readonly DtNavMeshParamWriter paramWriter = new DtNavMeshParamWriter(); @@ -35,8 +35,8 @@ namespace DotRecast.Detour.Io private void WriteHeader(BinaryWriter stream, DtNavMesh mesh, RcByteOrder order, bool cCompatibility) { - Write(stream, NavMeshSetHeader.NAVMESHSET_MAGIC, order); - Write(stream, cCompatibility ? NavMeshSetHeader.NAVMESHSET_VERSION : NavMeshSetHeader.NAVMESHSET_VERSION_RECAST4J, order); + RcIO.Write(stream, NavMeshSetHeader.NAVMESHSET_MAGIC, order); + RcIO.Write(stream, cCompatibility ? NavMeshSetHeader.NAVMESHSET_VERSION : NavMeshSetHeader.NAVMESHSET_VERSION_RECAST4J, order); int numTiles = 0; for (int i = 0; i < mesh.GetMaxTiles(); ++i) { @@ -49,11 +49,11 @@ namespace DotRecast.Detour.Io numTiles++; } - Write(stream, numTiles, order); + RcIO.Write(stream, numTiles, order); paramWriter.Write(stream, mesh.GetParams(), order); if (!cCompatibility) { - Write(stream, mesh.GetMaxVertsPerPoly(), order); + RcIO.Write(stream, mesh.GetMaxVertsPerPoly(), order); } } @@ -77,11 +77,11 @@ namespace DotRecast.Detour.Io byte[] ba = msw.ToArray(); tileHeader.dataSize = ba.Length; - Write(stream, tileHeader.tileRef, order); - Write(stream, tileHeader.dataSize, order); + RcIO.Write(stream, tileHeader.tileRef, order); + RcIO.Write(stream, tileHeader.dataSize, order); if (cCompatibility) { - Write(stream, 0, order); // C struct padding + RcIO.Write(stream, 0, order); // C struct padding } stream.Write(ba); diff --git a/src/DotRecast.Detour/Io/DtNavMeshParamWriter.cs b/src/DotRecast.Detour/Io/DtNavMeshParamWriter.cs index 9aab820..75bdff0 100644 --- a/src/DotRecast.Detour/Io/DtNavMeshParamWriter.cs +++ b/src/DotRecast.Detour/Io/DtNavMeshParamWriter.cs @@ -4,17 +4,17 @@ using DotRecast.Core.Numerics; namespace DotRecast.Detour.Io { - public class DtNavMeshParamWriter : DtWriter + public class DtNavMeshParamWriter { public void Write(BinaryWriter stream, DtNavMeshParams option, RcByteOrder order) { - Write(stream, option.orig.X, order); - Write(stream, option.orig.Y, order); - Write(stream, option.orig.Z, order); - Write(stream, option.tileWidth, order); - Write(stream, option.tileHeight, order); - Write(stream, option.maxTiles, order); - Write(stream, option.maxPolys, order); + RcIO.Write(stream, option.orig.X, order); + RcIO.Write(stream, option.orig.Y, order); + RcIO.Write(stream, option.orig.Z, order); + RcIO.Write(stream, option.tileWidth, order); + RcIO.Write(stream, option.tileHeight, order); + RcIO.Write(stream, option.maxTiles, order); + RcIO.Write(stream, option.maxPolys, order); } } } \ No newline at end of file diff --git a/src/DotRecast.Detour/Io/DtWriter.cs b/src/DotRecast.Detour/Io/DtWriter.cs deleted file mode 100644 index 30eb56a..0000000 --- a/src/DotRecast.Detour/Io/DtWriter.cs +++ /dev/null @@ -1,98 +0,0 @@ -/* -Recast4J Copyright (c) 2015 Piotr Piastucki piotr@jtilia.org - -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 System; -using System.IO; -using DotRecast.Core; - -namespace DotRecast.Detour.Io -{ - public abstract class DtWriter - { - protected void Write(BinaryWriter stream, float value, RcByteOrder order) - { - byte[] bytes = BitConverter.GetBytes(value); - int i = BitConverter.ToInt32(bytes, 0); - Write(stream, i, order); - } - - protected void Write(BinaryWriter stream, short value, RcByteOrder order) - { - if (order == RcByteOrder.BIG_ENDIAN) - { - stream.Write((byte)((value >> 8) & 0xFF)); - stream.Write((byte)(value & 0xFF)); - } - else - { - stream.Write((byte)(value & 0xFF)); - stream.Write((byte)((value >> 8) & 0xFF)); - } - } - - protected void Write(BinaryWriter stream, long value, RcByteOrder order) - { - if (order == RcByteOrder.BIG_ENDIAN) - { - Write(stream, (int)((ulong)value >> 32), order); - Write(stream, (int)(value & 0xFFFFFFFF), order); - } - else - { - Write(stream, (int)(value & 0xFFFFFFFF), order); - Write(stream, (int)((ulong)value >> 32), order); - } - } - - protected void Write(BinaryWriter stream, int value, RcByteOrder order) - { - if (order == RcByteOrder.BIG_ENDIAN) - { - stream.Write((byte)((value >> 24) & 0xFF)); - stream.Write((byte)((value >> 16) & 0xFF)); - stream.Write((byte)((value >> 8) & 0xFF)); - stream.Write((byte)(value & 0xFF)); - } - else - { - stream.Write((byte)(value & 0xFF)); - stream.Write((byte)((value >> 8) & 0xFF)); - stream.Write((byte)((value >> 16) & 0xFF)); - stream.Write((byte)((value >> 24) & 0xFF)); - } - } - - protected void Write(BinaryWriter stream, bool @bool) - { - Write(stream, (byte)(@bool ? 1 : 0)); - } - - protected void Write(BinaryWriter stream, byte value) - { - stream.Write(value); - } - - protected void Write(BinaryWriter stream, MemoryStream data) - { - data.Position = 0; - byte[] buffer = new byte[data.Length]; - data.Read(buffer, 0, buffer.Length); - stream.Write(buffer); - } - } -} \ No newline at end of file diff --git a/src/DotRecast.Detour/Io/IOUtils.cs b/src/DotRecast.Detour/Io/IOUtils.cs deleted file mode 100644 index 2c5f7d2..0000000 --- a/src/DotRecast.Detour/Io/IOUtils.cs +++ /dev/null @@ -1,64 +0,0 @@ -/* -Recast4J Copyright (c) 2015 Piotr Piastucki piotr@jtilia.org - -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 System; -using System.IO; -using DotRecast.Core; - -namespace DotRecast.Detour.Io -{ - public static class IOUtils - { - public static RcByteBuffer ToByteBuffer(BinaryReader @is, bool direct) - { - byte[] data = ToByteArray(@is); - if (direct) - { - Array.Reverse(data); - } - - return new RcByteBuffer(data); - } - - public static byte[] ToByteArray(BinaryReader inputStream) - { - using var msw = new MemoryStream(); - byte[] buffer = new byte[4096]; - int l; - while ((l = inputStream.Read(buffer)) > 0) - { - msw.Write(buffer, 0, l); - } - - return msw.ToArray(); - } - - - public static RcByteBuffer ToByteBuffer(BinaryReader inputStream) - { - var bytes = ToByteArray(inputStream); - return new RcByteBuffer(bytes); - } - - public static int SwapEndianness(int i) - { - var s = (((uint)i >> 24) & 0xFF) | (((uint)i >> 8) & 0xFF00) | (((uint)i << 8) & 0xFF0000) | ((i << 24) & 0xFF000000); - return (int)s; - } - } -} \ No newline at end of file diff --git a/test/DotRecast.Detour.Test/AbstractDetourTest.cs b/test/DotRecast.Detour.Test/AbstractDetourTest.cs index 552cb31..55652f5 100644 --- a/test/DotRecast.Detour.Test/AbstractDetourTest.cs +++ b/test/DotRecast.Detour.Test/AbstractDetourTest.cs @@ -1,5 +1,6 @@ /* 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