TriggerEvent() public static method

public static TriggerEvent ( GraphModifier type ) : void
type GraphModifier
return void
Ejemplo n.º 1
0
        /** Load from data from #file_cachedStartup */
        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();
        }
Ejemplo n.º 2
0
        /** Schedules graph updates internally */
        void QueueGraphUpdatesInternal()
        {
            bool anyRequiresFloodFill = false;

            while (graphUpdateQueue.Count > 0)
            {
                GraphUpdateObject ob = graphUpdateQueue.Dequeue();

                if (ob.requiresFloodFill)
                {
                    anyRequiresFloodFill = true;
                }

                foreach (IUpdatableGraph g in astar.data.GetUpdateableGraphs())
                {
                    NavGraph gr = g as NavGraph;
                    if (ob.nnConstraint == null || ob.nnConstraint.SuitableGraph(astar.data.GetGraphIndex(gr), gr))
                    {
                        var guo = new GUOSingle
                        {
                            order = GraphUpdateOrder.GraphUpdate,
                            obj   = ob,
                            graph = g
                        };
                        graphUpdateQueueRegular.Enqueue(guo);
                    }
                }
            }

            if (anyRequiresFloodFill)
            {
                var guo = new GUOSingle
                {
                    order = GraphUpdateOrder.FloodFill
                };
                graphUpdateQueueRegular.Enqueue(guo);
            }

            GraphModifier.TriggerEvent(GraphModifier.EventType.PreUpdate);
            anyGraphUpdateInProgress = true;
        }
        /// <summary>
        /// Updates graphs.
        /// Will do some graph updates, possibly signal another thread to do them.
        /// Will only process graph updates added by QueueGraphUpdatesInternal
        ///
        /// Returns: True if all graph updates have been done and pathfinding (or other tasks) may resume.
        /// False if there are still graph updates being processed or waiting in the queue.
        /// </summary>
        /// <param name="force">If true, all graph updates will be processed before this function returns. The return value
        /// will be True.</param>
        private bool ProcessGraphUpdates(bool force)
        {
            Assert.IsTrue(anyGraphUpdateInProgress);

            if (force)
            {
                asyncGraphUpdatesComplete.WaitOne();
            }
            else
            {
#if !UNITY_WEBGL
                if (!asyncGraphUpdatesComplete.WaitOne(0))
                {
                    return(false);
                }
#endif
            }

            Assert.AreEqual(graphUpdateQueueAsync.Count, 0, "Queue should be empty at this stage");

            ProcessPostUpdates();
            if (!ProcessRegularUpdates(force))
            {
                return(false);
            }

            GraphModifier.TriggerEvent(GraphModifier.EventType.PostUpdate);
            if (OnGraphsUpdated != null)
            {
                OnGraphsUpdated();
            }

            Assert.AreEqual(graphUpdateQueueRegular.Count, 0, "QueueRegular should be empty at this stage");
            Assert.AreEqual(graphUpdateQueueAsync.Count, 0, "QueueAsync should be empty at this stage");
            Assert.AreEqual(graphUpdateQueuePost.Count, 0, "QueuePost should be empty at this stage");

            anyGraphUpdateInProgress = false;
            return(true);
        }
 // Token: 0x060022E8 RID: 8936 RVA: 0x0019650C File Offset: 0x0019470C
 private bool ProcessGraphUpdates(bool force)
 {
     if (force)
     {
         this.asyncGraphUpdatesComplete.WaitOne();
     }
     else if (!this.asyncGraphUpdatesComplete.WaitOne(0))
     {
         return(false);
     }
     this.ProcessPostUpdates();
     if (!this.ProcessRegularUpdates(force))
     {
         return(false);
     }
     GraphModifier.TriggerEvent(GraphModifier.EventType.PostUpdate);
     if (this.OnGraphsUpdated != null)
     {
         this.OnGraphsUpdated();
     }
     this.anyGraphUpdateInProgress = false;
     return(true);
 }
        /// <summary>Schedules graph updates internally</summary>
        private void QueueGraphUpdatesInternal()
        {
            while (graphUpdateQueue.Count > 0)
            {
                GraphUpdateObject ob = graphUpdateQueue.Dequeue();

                foreach (IUpdatableGraph g in astar.data.GetUpdateableGraphs())
                {
                    NavGraph gr = g as NavGraph;
                    if (ob.nnConstraint == null || ob.nnConstraint.SuitableGraph(astar.data.GetGraphIndex(gr), gr))
                    {
                        var guo = new GUOSingle();
                        guo.order = GraphUpdateOrder.GraphUpdate;
                        guo.obj   = ob;
                        guo.graph = g;
                        graphUpdateQueueRegular.Enqueue(guo);
                    }
                }
            }

            GraphModifier.TriggerEvent(GraphModifier.EventType.PreUpdate);
            anyGraphUpdateInProgress = true;
        }
Ejemplo n.º 6
0
        /** Process graph updating work items.
         * Process all queued work items, e.g graph updates and the likes.
         *
         * \returns
         * - false if there are still items to be processed.
         * - true if the last work items was processed and pathfinding threads are ready to be resumed.
         *
         * \see AddWorkItem
         * \see threadSafeUpdateState
         * \see Update
         */
        public bool ProcessWorkItems(bool force)
        {
            if (workItemsInProgressRightNow)
            {
                throw new System.Exception("Processing work items recursively. Please do not wait for other work items to be completed inside work items. " +
                                           "If you think this is not caused by any of your scripts, this might be a bug.");
            }

            workItemsInProgressRightNow = true;
            astar.data.LockGraphStructure(true);
            while (workItems.Count > 0)
            {
                // Working on a new batch
                if (!workItemsInProgress)
                {
                    workItemsInProgress     = true;
                    queuedWorkItemFloodFill = false;
                }

                // Peek at first item in the queue
                AstarWorkItem itm = workItems[0];
                bool          status;

                try {
                    // Call init the first time the item is seen
                    if (itm.init != null)
                    {
                        itm.init();
                        itm.init = null;
                    }

                    if (itm.initWithContext != null)
                    {
                        itm.initWithContext(this);
                        itm.initWithContext = null;
                    }

                    // Make sure the item in the queue is up to date
                    workItems[0] = itm;

                    if (itm.update != null)
                    {
                        status = itm.update(force);
                    }
                    else if (itm.updateWithContext != null)
                    {
                        status = itm.updateWithContext(this, force);
                    }
                    else
                    {
                        status = true;
                    }
                } catch {
                    workItems.Dequeue();
                    workItemsInProgressRightNow = false;
                    astar.data.UnlockGraphStructure();
                    throw;
                }

                if (!status)
                {
                    if (force)
                    {
                        Debug.LogError("Misbehaving WorkItem. 'force'=true but the work item did not complete.\nIf force=true is passed to a WorkItem it should always return true.");
                    }

                    // Still work items to process
                    workItemsInProgressRightNow = false;
                    astar.data.UnlockGraphStructure();
                    return(false);
                }
                else
                {
                    workItems.Dequeue();
                }
            }

            EnsureValidFloodFill();
            Profiler.BeginSample("PostUpdate");
            if (anyGraphsDirty)
            {
                GraphModifier.TriggerEvent(GraphModifier.EventType.PostUpdate);
            }
            Profiler.EndSample();

            anyGraphsDirty = false;
            workItemsInProgressRightNow = false;
            workItemsInProgress         = false;
            astar.data.UnlockGraphStructure();
            return(true);
        }
Ejemplo n.º 7
0
        bool ProcessWorkItems(bool force, bool sendEvents)
        {
            if (workItemsInProgressRightNow)
            {
                throw new System.Exception("Processing work items recursively. Please do not wait for other work items to be completed inside work items. " +
                                           "If you think this is not caused by any of your scripts, this might be a bug.");
            }

            // Make sure the physics engine data is up to date.
            // Graph updates may use physics methods and it is very confusing if they
            // do not always pick up the latest changes made to the scene.
            UnityEngine.Physics.SyncTransforms();
            UnityEngine.Physics2D.SyncTransforms();

            workItemsInProgressRightNow = true;
            astar.data.LockGraphStructure(true);
            while (workItems.Count > 0)
            {
                // Working on a new batch
                if (!workItemsInProgress)
                {
                    workItemsInProgress = true;
                }

                // Peek at first item in the queue
                AstarWorkItem itm = workItems[0];
                bool          status;

                try {
                    // Call init the first time the item is seen
                    if (itm.init != null)
                    {
                        itm.init();
                        itm.init = null;
                    }

                    if (itm.initWithContext != null)
                    {
                        itm.initWithContext(this);
                        itm.initWithContext = null;
                    }

                    // Make sure the item in the queue is up to date
                    workItems[0] = itm;

                    if (itm.update != null)
                    {
                        status = itm.update(force);
                    }
                    else if (itm.updateWithContext != null)
                    {
                        status = itm.updateWithContext(this, force);
                    }
                    else
                    {
                        status = true;
                    }
                } catch {
                    workItems.Dequeue();
                    workItemsInProgressRightNow = false;
                    astar.data.UnlockGraphStructure();
                    throw;
                }

                if (!status)
                {
                    if (force)
                    {
                        Debug.LogError("Misbehaving WorkItem. 'force'=true but the work item did not complete.\nIf force=true is passed to a WorkItem it should always return true.");
                    }

                    // Still work items to process
                    workItemsInProgressRightNow = false;
                    astar.data.UnlockGraphStructure();
                    return(false);
                }
                else
                {
                    workItems.Dequeue();
                }
            }

            if (sendEvents)
            {
                Profiler.BeginSample("PostUpdate");
                if (anyGraphsDirty)
                {
                    GraphModifier.TriggerEvent(GraphModifier.EventType.PostUpdateBeforeAreaRecalculation);
                }
                Profiler.EndSample();

                EnsureValidFloodFill();

                Profiler.BeginSample("PostUpdate");
                if (anyGraphsDirty)
                {
                    GraphModifier.TriggerEvent(GraphModifier.EventType.PostUpdate);
                    if (OnGraphsUpdated != null)
                    {
                        OnGraphsUpdated();
                    }
                }
                Profiler.EndSample();
            }

            // Reset flags at the end
            anyGraphsDirty     = false;
            preUpdateEventSent = false;

            workItemsInProgressRightNow = false;
            workItemsInProgress         = false;
            astar.data.UnlockGraphStructure();
            return(true);
        }
        /** Updates graphs.
         * Will do some graph updates, possibly signal another thread to do them.
         * Will only process graph updates added by QueueGraphUpdatesInternal
         *
         * \param force If true, all graph updates will be processed before this function returns. The return value
         * will be True.
         *
         * \returns True if all graph updates have been done and pathfinding (or other tasks) may resume.
         * False if there are still graph updates being done or waiting in the queue.
         *
         *
         */
        bool ProcessGraphUpdates(bool force)
        {
            if (force)
            {
                asyncGraphUpdatesComplete.WaitOne();
            }
            else
            {
#if !UNITY_WEBGL
                if (!asyncGraphUpdatesComplete.WaitOne(0))
                {
                    return(false);
                }
#endif
            }

            if (graphUpdateQueueAsync.Count != 0)
            {
                throw new System.Exception("Queue should be empty at this stage");
            }

            while (graphUpdateQueueRegular.Count > 0)
            {
                GUOSingle s = graphUpdateQueueRegular.Peek();

                GraphUpdateThreading threading = s.order == GraphUpdateOrder.FloodFill ? GraphUpdateThreading.SeparateThread : s.graph.CanUpdateAsync(s.obj);

#if !UNITY_WEBGL
                bool forceUnityThread = force;

                // When not playing or when not using a graph update thread (or if it has crashed), everything runs in the Unity thread
                if (!Application.isPlaying || graphUpdateThread == null || !graphUpdateThread.IsAlive)
                {
                    forceUnityThread = true;
                }

                if (!forceUnityThread && (threading == GraphUpdateThreading.SeparateAndUnityInit))
                {
                    if (graphUpdateQueueAsync.Count > 0)
                    {
                        //Process async graph updates first.

                        //Next call to this function will process this object so it is not dequeued now
                        asyncGraphUpdatesComplete.Reset();
                        graphUpdateAsyncEvent.Set();

                        return(false);
                    }

                    s.graph.UpdateAreaInit(s.obj);

                    //Move GUO to async queue to be updated by another thread
                    graphUpdateQueueRegular.Dequeue();
                    graphUpdateQueueAsync.Enqueue(s);

                    //Next call to this function will process this object so it is not dequeued now
                    asyncGraphUpdatesComplete.Reset();
                    graphUpdateAsyncEvent.Set();

                    return(false);
                }
                else if (!forceUnityThread && (threading == GraphUpdateThreading.SeparateThread))
                {
                    //Move GUO to async queue to be updated by another thread
                    graphUpdateQueueRegular.Dequeue();
                    graphUpdateQueueAsync.Enqueue(s);
                }
                else
                {
#endif
                //Everything should be done in the unity thread

                if (graphUpdateQueueAsync.Count > 0)
                {
                    //Process async graph updates first.

                    if (force)
                    {
                        throw new System.Exception("This should not happen");
                    }

                    //Next call to this function will process this object so it is not dequeued now
                    asyncGraphUpdatesComplete.Reset();
                    graphUpdateAsyncEvent.Set();

                    return(false);
                }

                graphUpdateQueueRegular.Dequeue();

                if (s.order == GraphUpdateOrder.FloodFill)
                {
                    FloodFill();
                }
                else
                {
                    if (threading == GraphUpdateThreading.SeparateAndUnityInit)
                    {
                        try {
                            s.graph.UpdateAreaInit(s.obj);
                        } catch (System.Exception e) {
                            Debug.LogError("Error while initializing GraphUpdates\n" + e);
                        }
                    }
                    try {
                        s.graph.UpdateArea(s.obj);
                    } catch (System.Exception e) {
                        Debug.LogError("Error while updating graphs\n" + e);
                    }
                }
#if !UNITY_WEBGL
            }
#endif
            }

#if !UNITY_WEBGL
            if (graphUpdateQueueAsync.Count > 0)
            {
                //Next call to this function will process this object so it is not dequeued now
                asyncGraphUpdatesComplete.Reset();
                graphUpdateAsyncEvent.Set();

                return(false);
            }
#endif

            GraphModifier.TriggerEvent(GraphModifier.EventType.PostUpdate);
            if (OnGraphsUpdated != null)
            {
                OnGraphsUpdated();
            }

            return(true);
        }