Changed `DtWriter` to a static class and renamed it to `RcIO`

This commit is contained in:
ikpil 2024-05-23 22:39:03 +09:00
parent 99224251dc
commit 886be3712f
16 changed files with 303 additions and 329 deletions

View File

@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Changed `Dictionary<int, List<DtMeshTile>>` to `DtMeshTile[]` to optimize memory usage - Changed `Dictionary<int, List<DtMeshTile>>` to `DtMeshTile[]` to optimize memory usage
- Changed `MAX_STEER_POINTS` from class constant to local. - Changed `MAX_STEER_POINTS` from class constant to local.
- Changed `List<DtStraightPath>` to `Span<DtStraightPath>` for enhanced memory efficiency - Changed `List<DtStraightPath>` to `Span<DtStraightPath>` for enhanced memory efficiency
- Changed `DtWriter` to a static class and renamed it to `RcIO`
### Removed ### Removed
- Nothing - Nothing

135
src/DotRecast.Core/RcIO.cs Normal file
View File

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

View File

@ -35,12 +35,12 @@ namespace DotRecast.Detour.Dynamic.Io
public DtVoxelFile Read(BinaryReader stream) public DtVoxelFile Read(BinaryReader stream)
{ {
RcByteBuffer buf = IOUtils.ToByteBuffer(stream); RcByteBuffer buf = RcIO.ToByteBuffer(stream);
DtVoxelFile file = new DtVoxelFile(); DtVoxelFile file = new DtVoxelFile();
int magic = buf.GetInt(); int magic = buf.GetInt();
if (magic != DtVoxelFile.MAGIC) if (magic != DtVoxelFile.MAGIC)
{ {
magic = IOUtils.SwapEndianness(magic); magic = RcIO.SwapEndianness(magic);
if (magic != DtVoxelFile.MAGIC) if (magic != DtVoxelFile.MAGIC)
{ {
throw new IOException("Invalid magic"); throw new IOException("Invalid magic");

View File

@ -19,12 +19,11 @@ freely, subject to the following restrictions:
using System.IO; using System.IO;
using DotRecast.Core; using DotRecast.Core;
using DotRecast.Core.Numerics;
using DotRecast.Detour.Io; using DotRecast.Detour.Io;
namespace DotRecast.Detour.Dynamic.Io namespace DotRecast.Detour.Dynamic.Io
{ {
public class DtVoxelFileWriter : DtWriter public class DtVoxelFileWriter
{ {
private readonly IRcCompressor _compressor; private readonly IRcCompressor _compressor;
@ -40,34 +39,34 @@ namespace DotRecast.Detour.Dynamic.Io
public void Write(BinaryWriter stream, DtVoxelFile f, RcByteOrder byteOrder, bool compression) public void Write(BinaryWriter stream, DtVoxelFile f, RcByteOrder byteOrder, bool compression)
{ {
Write(stream, DtVoxelFile.MAGIC, byteOrder); RcIO.Write(stream, DtVoxelFile.MAGIC, byteOrder);
Write(stream, DtVoxelFile.VERSION_EXPORTER_RECAST4J | (compression ? DtVoxelFile.VERSION_COMPRESSION_LZ4 : 0), byteOrder); RcIO.Write(stream, DtVoxelFile.VERSION_EXPORTER_RECAST4J | (compression ? DtVoxelFile.VERSION_COMPRESSION_LZ4 : 0), byteOrder);
Write(stream, f.walkableRadius, byteOrder); RcIO.Write(stream, f.walkableRadius, byteOrder);
Write(stream, f.walkableHeight, byteOrder); RcIO.Write(stream, f.walkableHeight, byteOrder);
Write(stream, f.walkableClimb, byteOrder); RcIO.Write(stream, f.walkableClimb, byteOrder);
Write(stream, f.walkableSlopeAngle, byteOrder); RcIO.Write(stream, f.walkableSlopeAngle, byteOrder);
Write(stream, f.cellSize, byteOrder); RcIO.Write(stream, f.cellSize, byteOrder);
Write(stream, f.maxSimplificationError, byteOrder); RcIO.Write(stream, f.maxSimplificationError, byteOrder);
Write(stream, f.maxEdgeLen, byteOrder); RcIO.Write(stream, f.maxEdgeLen, byteOrder);
Write(stream, f.minRegionArea, byteOrder); RcIO.Write(stream, f.minRegionArea, byteOrder);
Write(stream, f.regionMergeArea, byteOrder); RcIO.Write(stream, f.regionMergeArea, byteOrder);
Write(stream, f.vertsPerPoly, byteOrder); RcIO.Write(stream, f.vertsPerPoly, byteOrder);
Write(stream, f.buildMeshDetail); RcIO.Write(stream, f.buildMeshDetail);
Write(stream, f.detailSampleDistance, byteOrder); RcIO.Write(stream, f.detailSampleDistance, byteOrder);
Write(stream, f.detailSampleMaxError, byteOrder); RcIO.Write(stream, f.detailSampleMaxError, byteOrder);
Write(stream, f.useTiles); RcIO.Write(stream, f.useTiles);
Write(stream, f.tileSizeX, byteOrder); RcIO.Write(stream, f.tileSizeX, byteOrder);
Write(stream, f.tileSizeZ, byteOrder); RcIO.Write(stream, f.tileSizeZ, byteOrder);
Write(stream, f.rotation.X, byteOrder); RcIO.Write(stream, f.rotation.X, byteOrder);
Write(stream, f.rotation.Y, byteOrder); RcIO.Write(stream, f.rotation.Y, byteOrder);
Write(stream, f.rotation.Z, byteOrder); RcIO.Write(stream, f.rotation.Z, byteOrder);
Write(stream, f.bounds[0], byteOrder); RcIO.Write(stream, f.bounds[0], byteOrder);
Write(stream, f.bounds[1], byteOrder); RcIO.Write(stream, f.bounds[1], byteOrder);
Write(stream, f.bounds[2], byteOrder); RcIO.Write(stream, f.bounds[2], byteOrder);
Write(stream, f.bounds[3], byteOrder); RcIO.Write(stream, f.bounds[3], byteOrder);
Write(stream, f.bounds[4], byteOrder); RcIO.Write(stream, f.bounds[4], byteOrder);
Write(stream, f.bounds[5], byteOrder); RcIO.Write(stream, f.bounds[5], byteOrder);
Write(stream, f.tiles.Count, byteOrder); RcIO.Write(stream, f.tiles.Count, byteOrder);
foreach (DtVoxelTile t in f.tiles) foreach (DtVoxelTile t in f.tiles)
{ {
WriteTile(stream, t, byteOrder, compression); 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) public void WriteTile(BinaryWriter stream, DtVoxelTile tile, RcByteOrder byteOrder, bool compression)
{ {
Write(stream, tile.tileX, byteOrder); RcIO.Write(stream, tile.tileX, byteOrder);
Write(stream, tile.tileZ, byteOrder); RcIO.Write(stream, tile.tileZ, byteOrder);
Write(stream, tile.width, byteOrder); RcIO.Write(stream, tile.width, byteOrder);
Write(stream, tile.depth, byteOrder); RcIO.Write(stream, tile.depth, byteOrder);
Write(stream, tile.borderSize, byteOrder); RcIO.Write(stream, tile.borderSize, byteOrder);
Write(stream, tile.boundsMin.X, byteOrder); RcIO.Write(stream, tile.boundsMin.X, byteOrder);
Write(stream, tile.boundsMin.Y, byteOrder); RcIO.Write(stream, tile.boundsMin.Y, byteOrder);
Write(stream, tile.boundsMin.Z, byteOrder); RcIO.Write(stream, tile.boundsMin.Z, byteOrder);
Write(stream, tile.boundsMax.X, byteOrder); RcIO.Write(stream, tile.boundsMax.X, byteOrder);
Write(stream, tile.boundsMax.Y, byteOrder); RcIO.Write(stream, tile.boundsMax.Y, byteOrder);
Write(stream, tile.boundsMax.Z, byteOrder); RcIO.Write(stream, tile.boundsMax.Z, byteOrder);
Write(stream, tile.cellSize, byteOrder); RcIO.Write(stream, tile.cellSize, byteOrder);
Write(stream, tile.cellHeight, byteOrder); RcIO.Write(stream, tile.cellHeight, byteOrder);
byte[] bytes = tile.spanData; byte[] bytes = tile.spanData;
if (compression) if (compression)
{ {
bytes = _compressor.Compress(bytes); bytes = _compressor.Compress(bytes);
} }
Write(stream, bytes.Length, byteOrder); RcIO.Write(stream, bytes.Length, byteOrder);
stream.Write(bytes); stream.Write(bytes);
} }
} }

View File

@ -31,7 +31,7 @@ namespace DotRecast.Detour.Extras.Unity.Astar
ZipArchiveEntry graphReferences = file.GetEntry(filename); ZipArchiveEntry graphReferences = file.GetEntry(filename);
using var entryStream = graphReferences.Open(); using var entryStream = graphReferences.Open();
using var br = new BinaryReader(entryStream); using var br = new BinaryReader(entryStream);
RcByteBuffer buffer = IOUtils.ToByteBuffer(br); RcByteBuffer buffer = RcIO.ToByteBuffer(br);
buffer.Order(RcByteOrder.LITTLE_ENDIAN); buffer.Order(RcByteOrder.LITTLE_ENDIAN);
return buffer; return buffer;
} }

View File

@ -24,34 +24,34 @@ using DotRecast.Detour.Io;
namespace DotRecast.Detour.TileCache.Io namespace DotRecast.Detour.TileCache.Io
{ {
public class DtTileCacheLayerHeaderWriter : DtWriter public class DtTileCacheLayerHeaderWriter
{ {
public void Write(BinaryWriter stream, DtTileCacheLayerHeader header, RcByteOrder order, bool cCompatibility) public void Write(BinaryWriter stream, DtTileCacheLayerHeader header, RcByteOrder order, bool cCompatibility)
{ {
Write(stream, header.magic, order); RcIO.Write(stream, header.magic, order);
Write(stream, header.version, order); RcIO.Write(stream, header.version, order);
Write(stream, header.tx, order); RcIO.Write(stream, header.tx, order);
Write(stream, header.ty, order); RcIO.Write(stream, header.ty, order);
Write(stream, header.tlayer, order); RcIO.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);
Write(stream, (short)header.hmin, order); RcIO.Write(stream, header.bmin.X, order);
Write(stream, (short)header.hmax, order); RcIO.Write(stream, header.bmin.Y, order);
Write(stream, (byte)header.width); RcIO.Write(stream, header.bmin.Z, order);
Write(stream, (byte)header.height); RcIO.Write(stream, header.bmax.X, order);
Write(stream, (byte)header.minx); RcIO.Write(stream, header.bmax.Y, order);
Write(stream, (byte)header.maxx); RcIO.Write(stream, header.bmax.Z, order);
Write(stream, (byte)header.miny);
Write(stream, (byte)header.maxy); 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) if (cCompatibility)
{ {
Write(stream, (short)0, order); // C struct padding RcIO.Write(stream, (short)0, order); // C struct padding
} }
} }
} }

View File

@ -37,7 +37,7 @@ namespace DotRecast.Detour.TileCache.Io
public DtTileCache Read(BinaryReader @is, int maxVertPerPoly, IDtTileCacheMeshProcess meshProcessor) public DtTileCache Read(BinaryReader @is, int maxVertPerPoly, IDtTileCacheMeshProcess meshProcessor)
{ {
RcByteBuffer bb = IOUtils.ToByteBuffer(@is); RcByteBuffer bb = RcIO.ToByteBuffer(@is);
return Read(bb, maxVertPerPoly, meshProcessor); return Read(bb, maxVertPerPoly, meshProcessor);
} }
@ -47,7 +47,7 @@ namespace DotRecast.Detour.TileCache.Io
header.magic = bb.GetInt(); header.magic = bb.GetInt();
if (header.magic != DtTileCacheSetHeader.TILECACHESET_MAGIC) if (header.magic != DtTileCacheSetHeader.TILECACHESET_MAGIC)
{ {
header.magic = IOUtils.SwapEndianness(header.magic); header.magic = RcIO.SwapEndianness(header.magic);
if (header.magic != DtTileCacheSetHeader.TILECACHESET_MAGIC) if (header.magic != DtTileCacheSetHeader.TILECACHESET_MAGIC)
{ {
throw new IOException("Invalid magic"); throw new IOException("Invalid magic");

View File

@ -25,7 +25,7 @@ using DotRecast.Detour.TileCache.Io.Compress;
namespace DotRecast.Detour.TileCache.Io namespace DotRecast.Detour.TileCache.Io
{ {
public class DtTileCacheWriter : DtWriter public class DtTileCacheWriter
{ {
private readonly DtNavMeshParamWriter paramWriter = new DtNavMeshParamWriter(); private readonly DtNavMeshParamWriter paramWriter = new DtNavMeshParamWriter();
private readonly IDtTileCacheCompressorFactory _compFactory; private readonly IDtTileCacheCompressorFactory _compFactory;
@ -38,8 +38,8 @@ namespace DotRecast.Detour.TileCache.Io
public void Write(BinaryWriter stream, DtTileCache cache, RcByteOrder order, bool cCompatibility) public void Write(BinaryWriter stream, DtTileCache cache, RcByteOrder order, bool cCompatibility)
{ {
Write(stream, DtTileCacheSetHeader.TILECACHESET_MAGIC, order); RcIO.Write(stream, DtTileCacheSetHeader.TILECACHESET_MAGIC, order);
Write(stream, cCompatibility RcIO.Write(stream, cCompatibility
? DtTileCacheSetHeader.TILECACHESET_VERSION ? DtTileCacheSetHeader.TILECACHESET_VERSION
: DtTileCacheSetHeader.TILECACHESET_VERSION_RECAST4J, order); : DtTileCacheSetHeader.TILECACHESET_VERSION_RECAST4J, order);
int numTiles = 0; int numTiles = 0;
@ -51,7 +51,7 @@ namespace DotRecast.Detour.TileCache.Io
numTiles++; numTiles++;
} }
Write(stream, numTiles, order); RcIO.Write(stream, numTiles, order);
paramWriter.Write(stream, cache.GetNavMesh().GetParams(), order); paramWriter.Write(stream, cache.GetNavMesh().GetParams(), order);
WriteCacheParams(stream, cache.GetParams(), order); WriteCacheParams(stream, cache.GetParams(), order);
for (int i = 0; i < cache.GetTileCount(); i++) for (int i = 0; i < cache.GetTileCount(); i++)
@ -59,32 +59,32 @@ namespace DotRecast.Detour.TileCache.Io
DtCompressedTile tile = cache.GetTile(i); DtCompressedTile tile = cache.GetTile(i);
if (tile == null || tile.data == null) if (tile == null || tile.data == null)
continue; continue;
Write(stream, (int)cache.GetTileRef(tile), order); RcIO.Write(stream, (int)cache.GetTileRef(tile), order);
byte[] data = tile.data; byte[] data = tile.data;
DtTileCacheLayer layer = cache.DecompressTile(tile); DtTileCacheLayer layer = cache.DecompressTile(tile);
var comp = _compFactory.Create(cCompatibility ? 0 : 1); var comp = _compFactory.Create(cCompatibility ? 0 : 1);
data = DtTileCacheBuilder.CompressTileCacheLayer(comp, layer, order, cCompatibility); data = DtTileCacheBuilder.CompressTileCacheLayer(comp, layer, order, cCompatibility);
Write(stream, data.Length, order); RcIO.Write(stream, data.Length, order);
stream.Write(data); stream.Write(data);
} }
} }
private void WriteCacheParams(BinaryWriter stream, DtTileCacheParams option, RcByteOrder order) private void WriteCacheParams(BinaryWriter stream, DtTileCacheParams option, RcByteOrder order)
{ {
Write(stream, option.orig.X, order); RcIO.Write(stream, option.orig.X, order);
Write(stream, option.orig.Y, order); RcIO.Write(stream, option.orig.Y, order);
Write(stream, option.orig.Z, order); RcIO.Write(stream, option.orig.Z, order);
Write(stream, option.cs, order); RcIO.Write(stream, option.cs, order);
Write(stream, option.ch, order); RcIO.Write(stream, option.ch, order);
Write(stream, option.width, order); RcIO.Write(stream, option.width, order);
Write(stream, option.height, order); RcIO.Write(stream, option.height, order);
Write(stream, option.walkableHeight, order); RcIO.Write(stream, option.walkableHeight, order);
Write(stream, option.walkableRadius, order); RcIO.Write(stream, option.walkableRadius, order);
Write(stream, option.walkableClimb, order); RcIO.Write(stream, option.walkableClimb, order);
Write(stream, option.maxSimplificationError, order); RcIO.Write(stream, option.maxSimplificationError, order);
Write(stream, option.maxTiles, order); RcIO.Write(stream, option.maxTiles, order);
Write(stream, option.maxObstacles, order); RcIO.Write(stream, option.maxObstacles, order);
} }
} }
} }

View File

@ -26,10 +26,12 @@ namespace DotRecast.Detour.Io
public class DtMeshDataReader public class DtMeshDataReader
{ {
public const int DT_POLY_DETAIL_SIZE = 10; 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) public DtMeshData Read(BinaryReader stream, int maxVertPerPoly)
{ {
RcByteBuffer buf = IOUtils.ToByteBuffer(stream); RcByteBuffer buf = RcIO.ToByteBuffer(stream);
return Read(buf, maxVertPerPoly, false); return Read(buf, maxVertPerPoly, false);
} }
@ -40,7 +42,7 @@ namespace DotRecast.Detour.Io
public DtMeshData Read32Bit(BinaryReader stream, int maxVertPerPoly) public DtMeshData Read32Bit(BinaryReader stream, int maxVertPerPoly)
{ {
RcByteBuffer buf = IOUtils.ToByteBuffer(stream); RcByteBuffer buf = RcIO.ToByteBuffer(stream);
return Read(buf, maxVertPerPoly, true); return Read(buf, maxVertPerPoly, true);
} }
@ -57,7 +59,7 @@ namespace DotRecast.Detour.Io
header.magic = buf.GetInt(); header.magic = buf.GetInt();
if (header.magic != DT_NAVMESH_MAGIC) if (header.magic != DT_NAVMESH_MAGIC)
{ {
header.magic = IOUtils.SwapEndianness(header.magic); header.magic = RcIO.SwapEndianness(header.magic);
if (header.magic != DT_NAVMESH_MAGIC) if (header.magic != DT_NAVMESH_MAGIC)
{ {
throw new IOException("Invalid magic"); throw new IOException("Invalid magic");
@ -118,8 +120,6 @@ namespace DotRecast.Detour.Io
return data; return data;
} }
public const int LINK_SIZEOF = 16;
public const int LINK_SIZEOF32BIT = 12;
public static int GetSizeofLink(bool is32Bit) public static int GetSizeofLink(bool is32Bit)
{ {

View File

@ -23,36 +23,36 @@ namespace DotRecast.Detour.Io
{ {
using static DtDetour; using static DtDetour;
public class DtMeshDataWriter : DtWriter public class DtMeshDataWriter
{ {
public void Write(BinaryWriter stream, DtMeshData data, RcByteOrder order, bool cCompatibility) public void Write(BinaryWriter stream, DtMeshData data, RcByteOrder order, bool cCompatibility)
{ {
DtMeshHeader header = data.header; DtMeshHeader header = data.header;
Write(stream, header.magic, order); RcIO.Write(stream, header.magic, order);
Write(stream, cCompatibility ? DT_NAVMESH_VERSION : DT_NAVMESH_VERSION_RECAST4J_LAST, order); RcIO.Write(stream, cCompatibility ? DT_NAVMESH_VERSION : DT_NAVMESH_VERSION_RECAST4J_LAST, order);
Write(stream, header.x, order); RcIO.Write(stream, header.x, order);
Write(stream, header.y, order); RcIO.Write(stream, header.y, order);
Write(stream, header.layer, order); RcIO.Write(stream, header.layer, order);
Write(stream, header.userId, order); RcIO.Write(stream, header.userId, order);
Write(stream, header.polyCount, order); RcIO.Write(stream, header.polyCount, order);
Write(stream, header.vertCount, order); RcIO.Write(stream, header.vertCount, order);
Write(stream, header.maxLinkCount, order); RcIO.Write(stream, header.maxLinkCount, order);
Write(stream, header.detailMeshCount, order); RcIO.Write(stream, header.detailMeshCount, order);
Write(stream, header.detailVertCount, order); RcIO.Write(stream, header.detailVertCount, order);
Write(stream, header.detailTriCount, order); RcIO.Write(stream, header.detailTriCount, order);
Write(stream, header.bvNodeCount, order); RcIO.Write(stream, header.bvNodeCount, order);
Write(stream, header.offMeshConCount, order); RcIO.Write(stream, header.offMeshConCount, order);
Write(stream, header.offMeshBase, order); RcIO.Write(stream, header.offMeshBase, order);
Write(stream, header.walkableHeight, order); RcIO.Write(stream, header.walkableHeight, order);
Write(stream, header.walkableRadius, order); RcIO.Write(stream, header.walkableRadius, order);
Write(stream, header.walkableClimb, order); RcIO.Write(stream, header.walkableClimb, order);
Write(stream, header.bmin.X, order); RcIO.Write(stream, header.bmin.X, order);
Write(stream, header.bmin.Y, order); RcIO.Write(stream, header.bmin.Y, order);
Write(stream, header.bmin.Z, order); RcIO.Write(stream, header.bmin.Z, order);
Write(stream, header.bmax.X, order); RcIO.Write(stream, header.bmax.X, order);
Write(stream, header.bmax.Y, order); RcIO.Write(stream, header.bmax.Y, order);
Write(stream, header.bmax.Z, order); RcIO.Write(stream, header.bmax.Z, order);
Write(stream, header.bvQuantFactor, order); RcIO.Write(stream, header.bvQuantFactor, order);
WriteVerts(stream, data.verts, header.vertCount, order); WriteVerts(stream, data.verts, header.vertCount, order);
WritePolys(stream, data, order, cCompatibility); WritePolys(stream, data, order, cCompatibility);
if (cCompatibility) if (cCompatibility)
@ -72,7 +72,7 @@ namespace DotRecast.Detour.Io
{ {
for (int i = 0; i < count * 3; i++) 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) if (cCompatibility)
{ {
Write(stream, 0xFFFF, order); RcIO.Write(stream, 0xFFFF, order);
} }
for (int j = 0; j < data.polys[i].verts.Length; j++) 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++) 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); RcIO.Write(stream, (short)data.polys[i].flags, order);
Write(stream, (byte)data.polys[i].vertCount); RcIO.Write(stream, (byte)data.polys[i].vertCount);
Write(stream, (byte)data.polys[i].areaAndtype); 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++) for (int i = 0; i < data.header.detailMeshCount; i++)
{ {
Write(stream, data.detailMeshes[i].vertBase, order); RcIO.Write(stream, data.detailMeshes[i].vertBase, order);
Write(stream, data.detailMeshes[i].triBase, order); RcIO.Write(stream, data.detailMeshes[i].triBase, order);
Write(stream, (byte)data.detailMeshes[i].vertCount); RcIO.Write(stream, (byte)data.detailMeshes[i].vertCount);
Write(stream, (byte)data.detailMeshes[i].triCount); RcIO.Write(stream, (byte)data.detailMeshes[i].triCount);
if (cCompatibility) 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++) 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++) 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++) 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 else
{ {
for (int j = 0; j < 3; j++) 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++) 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++) for (int j = 0; j < 2; j++)
{ {
Write(stream, data.offMeshCons[i].pos[j].X, order); RcIO.Write(stream, data.offMeshCons[i].pos[j].X, order);
Write(stream, data.offMeshCons[i].pos[j].Y, order); RcIO.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].Z, order);
} }
Write(stream, data.offMeshCons[i].rad, order); RcIO.Write(stream, data.offMeshCons[i].rad, order);
Write(stream, (short)data.offMeshCons[i].poly, order); RcIO.Write(stream, (short)data.offMeshCons[i].poly, order);
Write(stream, (byte)data.offMeshCons[i].flags); RcIO.Write(stream, (byte)data.offMeshCons[i].flags);
Write(stream, (byte)data.offMeshCons[i].side); RcIO.Write(stream, (byte)data.offMeshCons[i].side);
Write(stream, data.offMeshCons[i].userId, order); RcIO.Write(stream, data.offMeshCons[i].userId, order);
} }
} }
} }

View File

@ -31,7 +31,7 @@ namespace DotRecast.Detour.Io
public DtNavMesh Read(BinaryReader @is, int maxVertPerPoly) 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) public DtNavMesh Read(RcByteBuffer bb, int maxVertPerPoly)
@ -41,7 +41,7 @@ namespace DotRecast.Detour.Io
public DtNavMesh Read32Bit(BinaryReader @is, int maxVertPerPoly) 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) public DtNavMesh Read32Bit(RcByteBuffer bb, int maxVertPerPoly)
@ -51,7 +51,7 @@ namespace DotRecast.Detour.Io
public DtNavMesh Read(BinaryReader @is) public DtNavMesh Read(BinaryReader @is)
{ {
return Read(IOUtils.ToByteBuffer(@is)); return Read(RcIO.ToByteBuffer(@is));
} }
public DtNavMesh Read(RcByteBuffer bb) public DtNavMesh Read(RcByteBuffer bb)
@ -80,7 +80,7 @@ namespace DotRecast.Detour.Io
header.magic = bb.GetInt(); header.magic = bb.GetInt();
if (header.magic != NavMeshSetHeader.NAVMESHSET_MAGIC) if (header.magic != NavMeshSetHeader.NAVMESHSET_MAGIC)
{ {
header.magic = IOUtils.SwapEndianness(header.magic); header.magic = RcIO.SwapEndianness(header.magic);
if (header.magic != NavMeshSetHeader.NAVMESHSET_MAGIC) if (header.magic != NavMeshSetHeader.NAVMESHSET_MAGIC)
{ {
throw new IOException("Invalid magic " + header.magic); throw new IOException("Invalid magic " + header.magic);

View File

@ -22,7 +22,7 @@ using DotRecast.Core.Numerics;
namespace DotRecast.Detour.Io namespace DotRecast.Detour.Io
{ {
public class DtMeshSetWriter : DtWriter public class DtMeshSetWriter
{ {
private readonly DtMeshDataWriter writer = new DtMeshDataWriter(); private readonly DtMeshDataWriter writer = new DtMeshDataWriter();
private readonly DtNavMeshParamWriter paramWriter = new DtNavMeshParamWriter(); 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) private void WriteHeader(BinaryWriter stream, DtNavMesh mesh, RcByteOrder order, bool cCompatibility)
{ {
Write(stream, NavMeshSetHeader.NAVMESHSET_MAGIC, order); RcIO.Write(stream, NavMeshSetHeader.NAVMESHSET_MAGIC, order);
Write(stream, cCompatibility ? NavMeshSetHeader.NAVMESHSET_VERSION : NavMeshSetHeader.NAVMESHSET_VERSION_RECAST4J, order); RcIO.Write(stream, cCompatibility ? NavMeshSetHeader.NAVMESHSET_VERSION : NavMeshSetHeader.NAVMESHSET_VERSION_RECAST4J, order);
int numTiles = 0; int numTiles = 0;
for (int i = 0; i < mesh.GetMaxTiles(); ++i) for (int i = 0; i < mesh.GetMaxTiles(); ++i)
{ {
@ -49,11 +49,11 @@ namespace DotRecast.Detour.Io
numTiles++; numTiles++;
} }
Write(stream, numTiles, order); RcIO.Write(stream, numTiles, order);
paramWriter.Write(stream, mesh.GetParams(), order); paramWriter.Write(stream, mesh.GetParams(), order);
if (!cCompatibility) 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(); byte[] ba = msw.ToArray();
tileHeader.dataSize = ba.Length; tileHeader.dataSize = ba.Length;
Write(stream, tileHeader.tileRef, order); RcIO.Write(stream, tileHeader.tileRef, order);
Write(stream, tileHeader.dataSize, order); RcIO.Write(stream, tileHeader.dataSize, order);
if (cCompatibility) if (cCompatibility)
{ {
Write(stream, 0, order); // C struct padding RcIO.Write(stream, 0, order); // C struct padding
} }
stream.Write(ba); stream.Write(ba);

View File

@ -4,17 +4,17 @@ using DotRecast.Core.Numerics;
namespace DotRecast.Detour.Io namespace DotRecast.Detour.Io
{ {
public class DtNavMeshParamWriter : DtWriter public class DtNavMeshParamWriter
{ {
public void Write(BinaryWriter stream, DtNavMeshParams option, RcByteOrder order) public void Write(BinaryWriter stream, DtNavMeshParams option, RcByteOrder order)
{ {
Write(stream, option.orig.X, order); RcIO.Write(stream, option.orig.X, order);
Write(stream, option.orig.Y, order); RcIO.Write(stream, option.orig.Y, order);
Write(stream, option.orig.Z, order); RcIO.Write(stream, option.orig.Z, order);
Write(stream, option.tileWidth, order); RcIO.Write(stream, option.tileWidth, order);
Write(stream, option.tileHeight, order); RcIO.Write(stream, option.tileHeight, order);
Write(stream, option.maxTiles, order); RcIO.Write(stream, option.maxTiles, order);
Write(stream, option.maxPolys, order); RcIO.Write(stream, option.maxPolys, order);
} }
} }
} }

View File

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

View File

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

View File

@ -1,5 +1,6 @@
/* /*
recast4j Copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.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 This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages