DtTileCacheReader.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  3. recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
  4. DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
  5. This software is provided 'as-is', without any express or implied
  6. warranty. In no event will the authors be held liable for any damages
  7. arising from the use of this software.
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it
  10. freely, subject to the following restrictions:
  11. 1. The origin of this software must not be misrepresented; you must not
  12. claim that you wrote the original software. If you use this software
  13. in a product, an acknowledgment in the product documentation would be
  14. appreciated but is not required.
  15. 2. Altered source versions must be plainly marked as such, and must not be
  16. misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. using System.IO;
  20. using DotRecast.Core;
  21. using DotRecast.Detour.Io;
  22. using DotRecast.Detour.TileCache.Io.Compress;
  23. namespace DotRecast.Detour.TileCache.Io
  24. {
  25. public class DtTileCacheReader
  26. {
  27. private readonly DtNavMeshParamsReader paramReader = new DtNavMeshParamsReader();
  28. private readonly IDtTileCacheCompressorFactory _compFactory;
  29. public DtTileCacheReader(IDtTileCacheCompressorFactory compFactory)
  30. {
  31. _compFactory = compFactory;
  32. }
  33. public DtTileCache Read(BinaryReader @is, int maxVertPerPoly, IDtTileCacheMeshProcess meshProcessor)
  34. {
  35. RcByteBuffer bb = IOUtils.ToByteBuffer(@is);
  36. return Read(bb, maxVertPerPoly, meshProcessor);
  37. }
  38. public DtTileCache Read(RcByteBuffer bb, int maxVertPerPoly, IDtTileCacheMeshProcess meshProcessor)
  39. {
  40. DtTileCacheSetHeader header = new DtTileCacheSetHeader();
  41. header.magic = bb.GetInt();
  42. if (header.magic != DtTileCacheSetHeader.TILECACHESET_MAGIC)
  43. {
  44. header.magic = IOUtils.SwapEndianness(header.magic);
  45. if (header.magic != DtTileCacheSetHeader.TILECACHESET_MAGIC)
  46. {
  47. throw new IOException("Invalid magic");
  48. }
  49. bb.Order(bb.Order() == RcByteOrder.BIG_ENDIAN ? RcByteOrder.LITTLE_ENDIAN : RcByteOrder.BIG_ENDIAN);
  50. }
  51. header.version = bb.GetInt();
  52. if (header.version != DtTileCacheSetHeader.TILECACHESET_VERSION)
  53. {
  54. if (header.version != DtTileCacheSetHeader.TILECACHESET_VERSION_RECAST4J)
  55. {
  56. throw new IOException("Invalid version");
  57. }
  58. }
  59. bool cCompatibility = header.version == DtTileCacheSetHeader.TILECACHESET_VERSION;
  60. header.numTiles = bb.GetInt();
  61. header.meshParams = paramReader.Read(bb);
  62. header.cacheParams = ReadCacheParams(bb, cCompatibility);
  63. DtNavMesh mesh = new DtNavMesh(header.meshParams, maxVertPerPoly);
  64. IRcCompressor comp = _compFactory.Create(cCompatibility ? 0 : 1);
  65. DtTileCacheStorageParams storageParams = new DtTileCacheStorageParams(bb.Order(), cCompatibility);
  66. DtTileCache tc = new DtTileCache(header.cacheParams, storageParams, mesh, comp, meshProcessor);
  67. // Read tiles.
  68. for (int i = 0; i < header.numTiles; ++i)
  69. {
  70. long tileRef = bb.GetInt();
  71. int dataSize = bb.GetInt();
  72. if (tileRef == 0 || dataSize == 0)
  73. {
  74. break;
  75. }
  76. byte[] data = bb.ReadBytes(dataSize).ToArray();
  77. long tile = tc.AddTile(data, 0);
  78. if (tile != 0)
  79. {
  80. tc.BuildNavMeshTile(tile);
  81. }
  82. }
  83. return tc;
  84. }
  85. private DtTileCacheParams ReadCacheParams(RcByteBuffer bb, bool cCompatibility)
  86. {
  87. DtTileCacheParams option = new DtTileCacheParams();
  88. option.orig.x = bb.GetFloat();
  89. option.orig.y = bb.GetFloat();
  90. option.orig.z = bb.GetFloat();
  91. option.cs = bb.GetFloat();
  92. option.ch = bb.GetFloat();
  93. option.width = bb.GetInt();
  94. option.height = bb.GetInt();
  95. option.walkableHeight = bb.GetFloat();
  96. option.walkableRadius = bb.GetFloat();
  97. option.walkableClimb = bb.GetFloat();
  98. option.maxSimplificationError = bb.GetFloat();
  99. option.maxTiles = bb.GetInt();
  100. option.maxObstacles = bb.GetInt();
  101. return option;
  102. }
  103. }
  104. }