123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using Pathfinding.WindowsStore;
- using Pathfinding.Serialization;
- #if UNITY_WINRT && !UNITY_EDITOR
- #endif
- namespace Pathfinding {
- [System.Serializable]
-
-
-
-
-
-
- public class AstarData {
-
- public static AstarPath active {
- get {
- return AstarPath.active;
- }
- }
- #region Fields
-
-
-
-
- public NavMeshGraph navmesh { get; private set; }
- #if !ASTAR_NO_GRID_GRAPH
-
-
-
-
- public GridGraph gridGraph { get; private set; }
-
-
-
-
- public LayerGridGraph layerGridGraph { get; private set; }
- #endif
- #if !ASTAR_NO_POINT_GRAPH
-
-
-
-
- public PointGraph pointGraph { get; private set; }
- #endif
-
-
-
-
- public RecastGraph recastGraph { get; private set; }
-
-
-
-
- public System.Type[] graphTypes { get; private set; }
- #if ASTAR_FAST_NO_EXCEPTIONS || UNITY_WINRT || UNITY_WEBGL
-
-
-
-
- public static readonly System.Type[] DefaultGraphTypes = new System.Type[] {
- #if !ASTAR_NO_GRID_GRAPH
- typeof(GridGraph),
- typeof(LayerGridGraph),
- #endif
- #if !ASTAR_NO_POINT_GRAPH
- typeof(PointGraph),
- #endif
- typeof(NavMeshGraph),
- typeof(RecastGraph),
- };
- #endif
-
-
-
-
-
- [System.NonSerialized]
- public NavGraph[] graphs = new NavGraph[0];
-
-
-
-
-
-
-
-
-
- [SerializeField]
- string dataString;
-
-
-
-
-
- [SerializeField]
- [UnityEngine.Serialization.FormerlySerializedAs("data")]
- private byte[] upgradeData;
-
- private byte[] data {
- get {
-
- if (upgradeData != null && upgradeData.Length > 0) {
- data = upgradeData;
- upgradeData = null;
- }
- return dataString != null? System.Convert.FromBase64String(dataString) : null;
- }
- set {
- dataString = value != null? System.Convert.ToBase64String(value) : null;
- }
- }
-
-
-
-
- public TextAsset file_cachedStartup;
-
-
-
-
-
- public byte[] data_cachedStartup;
-
-
-
-
-
-
-
- [SerializeField]
- public bool cacheStartup;
-
- List<bool> graphStructureLocked = new List<bool>();
- #endregion
- public byte[] GetData () {
- return data;
- }
- public void SetData (byte[] data) {
- this.data = data;
- }
-
- public void Awake () {
- graphs = new NavGraph[0];
- if (cacheStartup && file_cachedStartup != null) {
- LoadFromCache();
- } else {
- DeserializeGraphs();
- }
- }
-
-
-
-
-
-
-
-
- internal void LockGraphStructure (bool allowAddingGraphs = false) {
- graphStructureLocked.Add(allowAddingGraphs);
- }
-
-
-
-
- internal void UnlockGraphStructure () {
- if (graphStructureLocked.Count == 0) throw new System.InvalidOperationException();
- graphStructureLocked.RemoveAt(graphStructureLocked.Count - 1);
- }
- PathProcessor.GraphUpdateLock AssertSafe (bool onlyAddingGraph = false) {
- if (graphStructureLocked.Count > 0) {
- bool allowAdding = true;
- for (int i = 0; i < graphStructureLocked.Count; i++) allowAdding &= graphStructureLocked[i];
- if (!(onlyAddingGraph && allowAdding)) throw new System.InvalidOperationException("Graphs cannot be added, removed or serialized while the graph structure is locked. This is the case when a graph is currently being scanned and when executing graph updates and work items.\nHowever as a special case, graphs can be added inside work items.");
- }
-
- var graphLock = active.PausePathfinding();
- if (!active.IsInsideWorkItem) {
-
-
-
- active.FlushWorkItems();
-
-
-
- active.pathReturnQueue.ReturnPaths(false);
- }
- return graphLock;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void GetNodes (System.Action<GraphNode> callback) {
- for (int i = 0; i < graphs.Length; i++) {
- if (graphs[i] != null) graphs[i].GetNodes(callback);
- }
- }
-
-
-
-
-
- public void UpdateShortcuts () {
- navmesh = (NavMeshGraph)FindGraphOfType(typeof(NavMeshGraph));
- #if !ASTAR_NO_GRID_GRAPH
- gridGraph = (GridGraph)FindGraphOfType(typeof(GridGraph));
- layerGridGraph = (LayerGridGraph)FindGraphOfType(typeof(LayerGridGraph));
- #endif
- #if !ASTAR_NO_POINT_GRAPH
- pointGraph = (PointGraph)FindGraphOfType(typeof(PointGraph));
- #endif
- recastGraph = (RecastGraph)FindGraphOfType(typeof(RecastGraph));
- }
-
- public void LoadFromCache () {
- var graphLock = AssertSafe();
- if (file_cachedStartup != null) {
- var bytes = file_cachedStartup.bytes;
- DeserializeGraphs(bytes);
- GraphModifier.TriggerEvent(GraphModifier.EventType.PostCacheLoad);
- } else {
- Debug.LogError("Can't load from cache since the cache is empty");
- }
- graphLock.Release();
- }
- #region Serialization
-
-
-
-
- public byte[] SerializeGraphs () {
- return SerializeGraphs(SerializeSettings.Settings);
- }
-
-
-
-
-
- public byte[] SerializeGraphs (SerializeSettings settings) {
- uint checksum;
- return SerializeGraphs(settings, out checksum);
- }
-
-
-
-
-
- public byte[] SerializeGraphs (SerializeSettings settings, out uint checksum) {
- var graphLock = AssertSafe();
- var sr = new AstarSerializer(this, settings, active.gameObject);
- sr.OpenSerialize();
- sr.SerializeGraphs(graphs);
- sr.SerializeExtraInfo();
- byte[] bytes = sr.CloseSerialize();
- checksum = sr.GetChecksum();
- #if ASTARDEBUG
- Debug.Log("Got a whole bunch of data, "+bytes.Length+" bytes");
- #endif
- graphLock.Release();
- return bytes;
- }
-
- public void DeserializeGraphs () {
- if (data != null) {
- DeserializeGraphs(data);
- }
- }
-
- void ClearGraphs () {
- if (graphs == null) return;
- for (int i = 0; i < graphs.Length; i++) {
- if (graphs[i] != null) {
- ((IGraphInternals)graphs[i]).OnDestroy();
- graphs[i].active = null;
- }
- }
- graphs = new NavGraph[0];
- UpdateShortcuts();
- }
- public void OnDestroy () {
- ClearGraphs();
- }
-
-
-
-
- public void DeserializeGraphs (byte[] bytes) {
- var graphLock = AssertSafe();
- ClearGraphs();
- DeserializeGraphsAdditive(bytes);
- graphLock.Release();
- }
-
-
-
-
-
- public void DeserializeGraphsAdditive (byte[] bytes) {
- var graphLock = AssertSafe();
- try {
- if (bytes != null) {
- var sr = new AstarSerializer(this, active.gameObject);
- if (sr.OpenDeserialize(bytes)) {
- DeserializeGraphsPartAdditive(sr);
- sr.CloseDeserialize();
- } else {
- Debug.Log("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");
- }
- } else {
- throw new System.ArgumentNullException("bytes");
- }
- active.VerifyIntegrity();
- } catch (System.Exception e) {
- Debug.LogError("Caught exception while deserializing data.\n"+e);
- graphs = new NavGraph[0];
- }
- UpdateShortcuts();
- graphLock.Release();
- }
-
- void DeserializeGraphsPartAdditive (AstarSerializer sr) {
- if (graphs == null) graphs = new NavGraph[0];
- var gr = new List<NavGraph>(graphs);
-
-
- sr.SetGraphIndexOffset(gr.Count);
- if (graphTypes == null) FindGraphTypes();
- gr.AddRange(sr.DeserializeGraphs(graphTypes));
- graphs = gr.ToArray();
- sr.DeserializeEditorSettingsCompatibility();
- sr.DeserializeExtraInfo();
-
- for (int i = 0; i < graphs.Length; i++) {
- if (graphs[i] == null) continue;
- graphs[i].GetNodes(node => node.GraphIndex = (uint)i);
- }
- for (int i = 0; i < graphs.Length; i++) {
- for (int j = i+1; j < graphs.Length; j++) {
- if (graphs[i] != null && graphs[j] != null && graphs[i].guid == graphs[j].guid) {
- Debug.LogWarning("Guid Conflict when importing graphs additively. Imported graph will get a new Guid.\nThis message is (relatively) harmless.");
- graphs[i].guid = Pathfinding.Util.Guid.NewGuid();
- break;
- }
- }
- }
- sr.PostDeserialization();
- active.hierarchicalGraph.RecalculateIfNecessary();
- }
- #endregion
-
-
-
-
- public void FindGraphTypes () {
- #if !ASTAR_FAST_NO_EXCEPTIONS && !UNITY_WINRT && !UNITY_WEBGL
- var graphList = new List<System.Type>();
- foreach (var assembly in System.AppDomain.CurrentDomain.GetAssemblies()) {
- System.Type[] types = null;
- try {
- types = assembly.GetTypes();
- } catch {
-
-
- continue;
- }
- foreach (var type in types) {
- #if NETFX_CORE && !UNITY_EDITOR
- System.Type baseType = type.GetTypeInfo().BaseType;
- #else
- var baseType = type.BaseType;
- #endif
- while (baseType != null) {
- if (System.Type.Equals(baseType, typeof(NavGraph))) {
- graphList.Add(type);
- break;
- }
- #if NETFX_CORE && !UNITY_EDITOR
- baseType = baseType.GetTypeInfo().BaseType;
- #else
- baseType = baseType.BaseType;
- #endif
- }
- }
- }
- graphTypes = graphList.ToArray();
- #if ASTARDEBUG
- Debug.Log("Found "+graphTypes.Length+" graph types");
- #endif
- #else
- graphTypes = DefaultGraphTypes;
- #endif
- }
- #region GraphCreation
-
-
-
-
-
- [System.Obsolete("If really necessary. Use System.Type.GetType instead.")]
- public System.Type GetGraphType (string type) {
- for (int i = 0; i < graphTypes.Length; i++) {
- if (graphTypes[i].Name == type) {
- return graphTypes[i];
- }
- }
- return null;
- }
-
-
-
-
-
-
-
- [System.Obsolete("Use CreateGraph(System.Type) instead")]
- public NavGraph CreateGraph (string type) {
- Debug.Log("Creating Graph of type '"+type+"'");
- for (int i = 0; i < graphTypes.Length; i++) {
- if (graphTypes[i].Name == type) {
- return CreateGraph(graphTypes[i]);
- }
- }
- Debug.LogError("Graph type ("+type+") wasn't found");
- return null;
- }
-
-
-
-
- internal NavGraph CreateGraph (System.Type type) {
- var graph = System.Activator.CreateInstance(type) as NavGraph;
- graph.active = active;
- return graph;
- }
-
-
-
-
-
- [System.Obsolete("Use AddGraph(System.Type) instead")]
- public NavGraph AddGraph (string type) {
- NavGraph graph = null;
- for (int i = 0; i < graphTypes.Length; i++) {
- if (graphTypes[i].Name == type) {
- graph = CreateGraph(graphTypes[i]);
- }
- }
- if (graph == null) {
- Debug.LogError("No NavGraph of type '"+type+"' could be found");
- return null;
- }
- AddGraph(graph);
- return graph;
- }
-
-
-
-
- public NavGraph AddGraph (System.Type type) {
- NavGraph graph = null;
- for (int i = 0; i < graphTypes.Length; i++) {
- if (System.Type.Equals(graphTypes[i], type)) {
- graph = CreateGraph(graphTypes[i]);
- }
- }
- if (graph == null) {
- Debug.LogError("No NavGraph of type '"+type+"' could be found, "+graphTypes.Length+" graph types are avaliable");
- return null;
- }
- AddGraph(graph);
- return graph;
- }
-
- void AddGraph (NavGraph graph) {
-
- var graphLock = AssertSafe(true);
-
- bool foundEmpty = false;
- for (int i = 0; i < graphs.Length; i++) {
- if (graphs[i] == null) {
- graphs[i] = graph;
- graph.graphIndex = (uint)i;
- foundEmpty = true;
- break;
- }
- }
- if (!foundEmpty) {
- if (graphs != null && graphs.Length >= GraphNode.MaxGraphIndex) {
- throw new System.Exception("Graph Count Limit Reached. You cannot have more than " + GraphNode.MaxGraphIndex + " graphs.");
- }
-
- var graphList = new List<NavGraph>(graphs ?? new NavGraph[0]);
- graphList.Add(graph);
- graphs = graphList.ToArray();
- graph.graphIndex = (uint)(graphs.Length-1);
- }
- UpdateShortcuts();
- graph.active = active;
- graphLock.Release();
- }
-
-
-
-
-
-
-
-
-
-
-
- public bool RemoveGraph (NavGraph graph) {
-
-
-
- var graphLock = AssertSafe();
- ((IGraphInternals)graph).OnDestroy();
- graph.active = null;
- int i = System.Array.IndexOf(graphs, graph);
- if (i != -1) graphs[i] = null;
- UpdateShortcuts();
- graphLock.Release();
- return i != -1;
- }
- #endregion
- #region GraphUtility
-
-
-
-
-
-
- public static NavGraph GetGraph (GraphNode node) {
- if (node == null) return null;
- AstarPath script = AstarPath.active;
- if (script == null) return null;
- AstarData data = script.data;
- if (data == null || data.graphs == null) return null;
- uint graphIndex = node.GraphIndex;
- if (graphIndex >= data.graphs.Length) {
- return null;
- }
- return data.graphs[(int)graphIndex];
- }
-
- public NavGraph FindGraph (System.Func<NavGraph, bool> predicate) {
- if (graphs != null) {
- for (int i = 0; i < graphs.Length; i++) {
- if (graphs[i] != null && predicate(graphs[i])) {
- return graphs[i];
- }
- }
- }
- return null;
- }
-
- public NavGraph FindGraphOfType (System.Type type) {
- return FindGraph(graph => System.Type.Equals(graph.GetType(), type));
- }
-
- public NavGraph FindGraphWhichInheritsFrom (System.Type type) {
- return FindGraph(graph => WindowsStoreCompatibility.GetTypeInfo(type).IsAssignableFrom(WindowsStoreCompatibility.GetTypeInfo(graph.GetType())));
- }
-
-
-
-
-
-
-
-
-
- public IEnumerable FindGraphsOfType (System.Type type) {
- if (graphs == null) yield break;
- for (int i = 0; i < graphs.Length; i++) {
- if (graphs[i] != null && System.Type.Equals(graphs[i].GetType(), type)) {
- yield return graphs[i];
- }
- }
- }
-
-
-
-
-
-
-
-
- public IEnumerable GetUpdateableGraphs () {
- if (graphs == null) yield break;
- for (int i = 0; i < graphs.Length; i++) {
- if (graphs[i] is IUpdatableGraph) {
- yield return graphs[i];
- }
- }
- }
-
-
-
-
-
-
-
-
- [System.Obsolete("Obsolete because it is not used by the package internally and the use cases are few. Iterate through the graphs array instead.")]
- public IEnumerable GetRaycastableGraphs () {
- if (graphs == null) yield break;
- for (int i = 0; i < graphs.Length; i++) {
- if (graphs[i] is IRaycastableGraph) {
- yield return graphs[i];
- }
- }
- }
-
- public int GetGraphIndex (NavGraph graph) {
- if (graph == null) throw new System.ArgumentNullException("graph");
- var index = -1;
- if (graphs != null) {
- index = System.Array.IndexOf(graphs, graph);
- if (index == -1) Debug.LogError("Graph doesn't exist");
- }
- return index;
- }
- #endregion
- }
- }
|