AstarDeserializer.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. namespace PF
  4. {
  5. public static class AstarDeserializer
  6. {
  7. /** Deserializes graphs from the specified byte array.
  8. * An error will be logged if deserialization fails.
  9. */
  10. public static NavGraph[] DeserializeGraphs (byte[] bytes)
  11. {
  12. NavGraph[] graphs = { new RecastGraph() };
  13. DeserializeGraphsAdditive(graphs, bytes);
  14. return graphs;
  15. }
  16. /** Deserializes graphs from the specified byte array additively.
  17. * An error will be logged if deserialization fails.
  18. * This function will add loaded graphs to the current ones.
  19. */
  20. public static void DeserializeGraphsAdditive (NavGraph[] graphs, byte[] bytes) {
  21. try {
  22. if (bytes != null) {
  23. var sr = new AstarSerializer();
  24. if (sr.OpenDeserialize(bytes)) {
  25. DeserializeGraphsPartAdditive(graphs, sr);
  26. sr.CloseDeserialize();
  27. } else {
  28. throw new Exception("Invalid data file (cannot read zip).\nThe data is either corrupt or it was saved using a 3.0.x or earlier version of the system");
  29. }
  30. } else {
  31. throw new System.ArgumentNullException("bytes");
  32. }
  33. //AstarPath.active.VerifyIntegrity();
  34. } catch (System.Exception e) {
  35. #if !SERVER
  36. UnityEngine.Debug.LogError("Caught exception while deserializing data.\n"+e);
  37. #endif
  38. throw;
  39. }
  40. }
  41. /** Helper function for deserializing graphs */
  42. public static void DeserializeGraphsPartAdditive (NavGraph[] graphs, AstarSerializer sr) {
  43. if (graphs == null) graphs = new NavGraph[0];
  44. var gr = new List<NavGraph>(graphs);
  45. // Set an offset so that the deserializer will load
  46. // the graphs with the correct graph indexes
  47. sr.SetGraphIndexOffset(gr.Count);
  48. gr.AddRange(sr.DeserializeGraphs());
  49. graphs = gr.ToArray();
  50. sr.DeserializeEditorSettingsCompatibility();
  51. sr.DeserializeExtraInfo();
  52. //Assign correct graph indices.
  53. for (int i = 0; i < graphs.Length; i++) {
  54. if (graphs[i] == null) continue;
  55. graphs[i].GetNodes(node => node.GraphIndex = (uint)i);
  56. }
  57. for (int i = 0; i < graphs.Length; i++) {
  58. for (int j = i+1; j < graphs.Length; j++) {
  59. if (graphs[i] != null && graphs[j] != null && graphs[i].guid == graphs[j].guid) {
  60. #if !SERVER
  61. UnityEngine.Debug.LogWarning("Guid Conflict when importing graphs additively. Imported graph will get a new Guid.\nThis message is (relatively) harmless.");
  62. #endif
  63. graphs[i].guid = Guid.NewGuid();
  64. break;
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }