DeserializeHelper.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections.Generic;
  3. using PF;
  4. using Guid = PF.Guid;
  5. namespace ETModel
  6. {
  7. public static class DeserializeHelper
  8. {
  9. public static NavGraph[] Load(string filePath)
  10. {
  11. byte[] bytes = AstarSerializer.LoadFromFile(filePath);
  12. AstarSerializer sr = new AstarSerializer();
  13. if (!sr.OpenDeserialize(bytes))
  14. {
  15. 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");
  16. }
  17. var gr = new List<NavGraph>();
  18. // Set an offset so that the deserializer will load
  19. // the graphs with the correct graph indexes
  20. sr.SetGraphIndexOffset(gr.Count);
  21. gr.AddRange(sr.DeserializeGraphs());
  22. NavGraph[] graphs = gr.ToArray();
  23. sr.DeserializeEditorSettingsCompatibility();
  24. sr.DeserializeExtraInfo();
  25. //Assign correct graph indices.
  26. for (int i = 0; i < graphs.Length; i++)
  27. {
  28. if (graphs[i] == null)
  29. {
  30. continue;
  31. }
  32. int i1 = i;
  33. graphs[i].GetNodes(node => node.GraphIndex = (uint)i1);
  34. }
  35. for (int i = 0; i < graphs.Length; i++)
  36. {
  37. for (int j = i+1; j < graphs.Length; j++)
  38. {
  39. if (graphs[i] != null && graphs[j] != null && graphs[i].guid == graphs[j].guid)
  40. {
  41. graphs[i].guid = Guid.NewGuid();
  42. break;
  43. }
  44. }
  45. }
  46. sr.PostDeserialization();
  47. sr.CloseDeserialize();
  48. return graphs;
  49. }
  50. }
  51. }