DtMeshSetWriter.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Recast4J Copyright (c) 2015-2018 Piotr Piastucki piotr@jtilia.org
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not
  10. claim that you wrote the original software. If you use this software
  11. in a product, an acknowledgment in the product documentation would be
  12. appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source distribution.
  16. */
  17. using System.IO;
  18. using DotRecast.Core;
  19. namespace DotRecast.Detour.Io
  20. {
  21. public class DtMeshSetWriter : DtWriter
  22. {
  23. private readonly DtMeshDataWriter writer = new DtMeshDataWriter();
  24. private readonly DtNavMeshParamWriter paramWriter = new DtNavMeshParamWriter();
  25. public void Write(BinaryWriter stream, DtNavMesh mesh, RcByteOrder order, bool cCompatibility)
  26. {
  27. WriteHeader(stream, mesh, order, cCompatibility);
  28. WriteTiles(stream, mesh, order, cCompatibility);
  29. }
  30. private void WriteHeader(BinaryWriter stream, DtNavMesh mesh, RcByteOrder order, bool cCompatibility)
  31. {
  32. Write(stream, NavMeshSetHeader.NAVMESHSET_MAGIC, order);
  33. Write(stream, cCompatibility ? NavMeshSetHeader.NAVMESHSET_VERSION : NavMeshSetHeader.NAVMESHSET_VERSION_RECAST4J, order);
  34. int numTiles = 0;
  35. for (int i = 0; i < mesh.GetMaxTiles(); ++i)
  36. {
  37. DtMeshTile tile = mesh.GetTile(i);
  38. if (tile == null || tile.data == null || tile.data.header == null)
  39. {
  40. continue;
  41. }
  42. numTiles++;
  43. }
  44. Write(stream, numTiles, order);
  45. paramWriter.Write(stream, mesh.GetParams(), order);
  46. if (!cCompatibility)
  47. {
  48. Write(stream, mesh.GetMaxVertsPerPoly(), order);
  49. }
  50. }
  51. private void WriteTiles(BinaryWriter stream, DtNavMesh mesh, RcByteOrder order, bool cCompatibility)
  52. {
  53. for (int i = 0; i < mesh.GetMaxTiles(); ++i)
  54. {
  55. DtMeshTile tile = mesh.GetTile(i);
  56. if (tile == null || tile.data == null || tile.data.header == null)
  57. {
  58. continue;
  59. }
  60. NavMeshTileHeader tileHeader = new NavMeshTileHeader();
  61. tileHeader.tileRef = mesh.GetTileRef(tile);
  62. using MemoryStream msw = new MemoryStream();
  63. using BinaryWriter bw = new BinaryWriter(msw);
  64. writer.Write(bw, tile.data, order, cCompatibility);
  65. bw.Flush();
  66. bw.Close();
  67. byte[] ba = msw.ToArray();
  68. tileHeader.dataSize = ba.Length;
  69. Write(stream, tileHeader.tileRef, order);
  70. Write(stream, tileHeader.dataSize, order);
  71. if (cCompatibility)
  72. {
  73. Write(stream, 0, order); // C struct padding
  74. }
  75. stream.Write(ba);
  76. }
  77. }
  78. }
  79. }