DtTileCacheWriter.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 DtTileCacheWriter : DtWriter
  26. {
  27. private readonly DtNavMeshParamWriter paramWriter = new DtNavMeshParamWriter();
  28. private readonly DtTileCacheBuilder builder = new DtTileCacheBuilder();
  29. private readonly IDtTileCacheCompressorFactory _compFactory;
  30. public DtTileCacheWriter(IDtTileCacheCompressorFactory compFactory)
  31. {
  32. _compFactory = compFactory;
  33. }
  34. public void Write(BinaryWriter stream, DtTileCache cache, RcByteOrder order, bool cCompatibility)
  35. {
  36. Write(stream, DtTileCacheSetHeader.TILECACHESET_MAGIC, order);
  37. Write(stream, cCompatibility
  38. ? DtTileCacheSetHeader.TILECACHESET_VERSION
  39. : DtTileCacheSetHeader.TILECACHESET_VERSION_RECAST4J, order);
  40. int numTiles = 0;
  41. for (int i = 0; i < cache.GetTileCount(); ++i)
  42. {
  43. DtCompressedTile tile = cache.GetTile(i);
  44. if (tile == null || tile.data == null)
  45. continue;
  46. numTiles++;
  47. }
  48. Write(stream, numTiles, order);
  49. paramWriter.Write(stream, cache.GetNavMesh().GetParams(), order);
  50. WriteCacheParams(stream, cache.GetParams(), order);
  51. for (int i = 0; i < cache.GetTileCount(); i++)
  52. {
  53. DtCompressedTile tile = cache.GetTile(i);
  54. if (tile == null || tile.data == null)
  55. continue;
  56. Write(stream, (int)cache.GetTileRef(tile), order);
  57. byte[] data = tile.data;
  58. DtTileCacheLayer layer = cache.DecompressTile(tile);
  59. var comp = _compFactory.Create(cCompatibility ? 0 : 1);
  60. data = builder.CompressTileCacheLayer(comp, layer, order, cCompatibility);
  61. Write(stream, data.Length, order);
  62. stream.Write(data);
  63. }
  64. }
  65. private void WriteCacheParams(BinaryWriter stream, DtTileCacheParams option, RcByteOrder order)
  66. {
  67. Write(stream, option.orig.x, order);
  68. Write(stream, option.orig.y, order);
  69. Write(stream, option.orig.z, order);
  70. Write(stream, option.cs, order);
  71. Write(stream, option.ch, order);
  72. Write(stream, option.width, order);
  73. Write(stream, option.height, order);
  74. Write(stream, option.walkableHeight, order);
  75. Write(stream, option.walkableRadius, order);
  76. Write(stream, option.walkableClimb, order);
  77. Write(stream, option.maxSimplificationError, order);
  78. Write(stream, option.maxTiles, order);
  79. Write(stream, option.maxObstacles, order);
  80. }
  81. }
  82. }