コード例 #1
0
        internal static void Sort <T>(List <T> items, GetSystemType <T> getType, Type parentType)
        {
#if !NET_DOTS
            lookupDictionary = null;
#endif
            // Populate dictionary mapping systemType to system-and-before/after-types.
            // This is clunky - it is easier to understand, and cleaner code, to
            // just use a Dictionary<Type, SysAndDep>. However, Tiny doesn't currently have
            // the ability to use Type as a key to a NativeHash, so we're stuck until that gets addressed.
            //
            // Likewise, this is important shared code. It can be done cleaner with 2 versions, but then...
            // 2 sets of bugs and slightly different behavior will creep in.
            //
            var sysAndDep = new SysAndDep <T> [items.Count];

            for (int i = 0; i < items.Count; ++i)
            {
                var sys = items[i];

                sysAndDep[i] = new SysAndDep <T>
                {
                    item         = sys,
                    type         = getType(sys),
                    updateBefore = new List <Type>(),
                    nAfter       = 0,
                };
            }

            for (int i = 0; i < sysAndDep.Length; ++i)
            {
                var systemType = sysAndDep[i].type;

                var before = TypeManager.GetSystemAttributes(systemType, typeof(UpdateBeforeAttribute));
                var after  = TypeManager.GetSystemAttributes(systemType, typeof(UpdateAfterAttribute));
                foreach (var attr in before)
                {
                    var dep = attr as UpdateBeforeAttribute;
                    if (!typeof(ComponentSystemBase).IsAssignableFrom(dep.SystemType))
                    {
#if !NET_DOTS
                        Debug.LogWarning(
                            $"Ignoring invalid [UpdateBefore] attribute on {systemType} because {dep.SystemType} is not a subclass of {nameof(ComponentSystemBase)}.\n"
                            + $"Set the target parameter of [UpdateBefore] to a system class in the same {nameof(ComponentSystemGroup)} as {systemType}.");
#else
                        Debug.LogWarning($"WARNING: invalid [UpdateBefore] attribute:");
                        Debug.LogWarning(TypeManager.GetSystemName(dep.SystemType));
                        Debug.LogWarning(" is not derived from ComponentSystemBase. Set the target parameter of [UpdateBefore] to a system class in the same ComponentSystemGroup.");
#endif
                        continue;
                    }

                    if (dep.SystemType == systemType)
                    {
#if !NET_DOTS
                        Debug.LogWarning(
                            $"Ignoring invalid [UpdateBefore] attribute on {systemType} because a system cannot be updated before itself.\n"
                            + $"Set the target parameter of [UpdateBefore] to a different system class in the same {nameof(ComponentSystemGroup)} as {systemType}.");
#else
                        Debug.LogWarning($"WARNING: invalid [UpdateBefore] attribute:");
                        Debug.LogWarning(TypeManager.GetSystemName(systemType));
                        Debug.LogWarning("  depends on itself. Set the target parameter of [UpdateBefore] to a system class in the same ComponentSystemGroup.");
#endif
                        continue;
                    }

                    if (parentType == typeof(InitializationSystemGroup))
                    {
                        if (dep.SystemType == typeof(BeginInitializationEntityCommandBufferSystem))
                        {
#if !NET_DOTS
                            throw new ArgumentException(
                                      $"Invalid [UpdateBefore] {dep.SystemType} attribute on {systemType}, because that system is already restricted to be first.");
#else
                            throw new ArgumentException($"Invalid [UpdateBefore] BeginInitializationEntityCommandBufferSystem, because that system is already restricted to be first.");
#endif
                        }

                        if (dep.SystemType == typeof(EndInitializationEntityCommandBufferSystem))
                        {
                            WarningForBeforeCheck(systemType, dep.SystemType);
                            continue;
                        }
                    }

                    if (parentType == typeof(SimulationSystemGroup))
                    {
                        if (dep.SystemType == typeof(BeginSimulationEntityCommandBufferSystem))
                        {
#if !NET_DOTS
                            throw new ArgumentException(
                                      $"Invalid [UpdateBefore] {dep.SystemType} attribute on {systemType}, because that system is already restricted to be first.");
#else
                            throw new ArgumentException($"Invalid [UpdateBefore] BeginSimulationEntityCommandBufferSystem, because that system is already restricted to be first.");
#endif
                        }

                        if (dep.SystemType == typeof(LateSimulationSystemGroup))
                        {
                            WarningForBeforeCheck(systemType, dep.SystemType);
                            continue;
                        }

                        if (dep.SystemType == typeof(EndSimulationEntityCommandBufferSystem))
                        {
                            WarningForBeforeCheck(systemType, dep.SystemType);
                            continue;
                        }
                    }

                    if (parentType == typeof(PresentationSystemGroup))
                    {
                        if (dep.SystemType == typeof(BeginPresentationEntityCommandBufferSystem))
                        {
#if !NET_DOTS
                            throw new ArgumentException(
                                      $"Invalid [UpdateBefore] {dep.SystemType} attribute on {systemType}, because that system is already restricted to be first.");
#else
                            throw new ArgumentException($"Invalid [UpdateBefore] BeginPreesntationEntityCommandBufferSystem, because that system is already restricted to be first.");
#endif
                        }
                    }

                    int depIndex = LookupSysAndDep(dep.SystemType, sysAndDep);
                    if (depIndex < 0)
                    {
#if !NET_DOTS
                        Debug.LogWarning(
                            $"Ignoring invalid [UpdateBefore] attribute on {systemType} because {dep.SystemType} belongs to a different {nameof(ComponentSystemGroup)}.\n"
                            + $"This attribute can only order systems that are children of the same {nameof(ComponentSystemGroup)}.\n"
                            + $"Make sure that both systems are in the same parent group with [UpdateInGroup(typeof({parentType})].\n"
                            + $"You can also change the relative order of groups when appropriate, by using [UpdateBefore] and [UpdateAfter] attributes at the group level.");
#else
                        Debug.LogWarning("WARNING: invalid [UpdateBefore] dependency:");
                        Debug.LogWarning(TypeManager.GetSystemName(systemType));
                        Debug.LogWarning("  depends on a non-sibling system: ");
                        Debug.LogWarning(TypeManager.GetSystemName(dep.SystemType));
#endif
                        continue;
                    }

                    sysAndDep[i].updateBefore.Add(dep.SystemType);
                    sysAndDep[depIndex].nAfter++;
                }

                foreach (var attr in after)
                {
                    var dep = attr as UpdateAfterAttribute;
                    if (!typeof(ComponentSystemBase).IsAssignableFrom(dep.SystemType))
                    {
#if !NET_DOTS
                        Debug.LogWarning(
                            $"Ignoring invalid [UpdateAfter] attribute on {systemType} because {dep.SystemType} is not a subclass of {nameof(ComponentSystemBase)}.\n"
                            + $"Set the target parameter of [UpdateAfter] to a system class in the same {nameof(ComponentSystemGroup)} as {systemType}.");
#else
                        Debug.LogWarning($"WARNING: invalid [UpdateAfter] attribute:");
                        Debug.LogWarning(TypeManager.GetSystemName(dep.SystemType));
                        Debug.LogWarning(" is not derived from ComponentSystemBase. Set the target parameter of [UpdateAfter] to a system class in the same ComponentSystemGroup.");
#endif
                        continue;
                    }

                    if (dep.SystemType == systemType)
                    {
#if !NET_DOTS
                        Debug.LogWarning(
                            $"Ignoring invalid [UpdateAfter] attribute on {systemType} because a system cannot be updated after itself.\n"
                            + $"Set the target parameter of [UpdateAfter] to a different system class in the same {nameof(ComponentSystemGroup)} as {systemType}.");
#else
                        Debug.LogWarning($"WARNING: invalid [UpdateAfter] attribute:");
                        Debug.LogWarning(TypeManager.GetSystemName(systemType));
                        Debug.LogWarning("  depends on itself. Set the target parameter of [UpdateAfter] to a system class in the same ComponentSystemGroup.");
#endif
                        continue;
                    }

                    if (parentType == typeof(InitializationSystemGroup))
                    {
                        if (dep.SystemType == typeof(BeginInitializationEntityCommandBufferSystem))
                        {
                            WarningForAfterCheck(systemType, dep.SystemType);
                            continue;
                        }

                        if (dep.SystemType == typeof(EndInitializationEntityCommandBufferSystem))
                        {
#if !NET_DOTS
                            throw new ArgumentException(
                                      $"Invalid [UpdateAfter] {dep.SystemType} attribute on {systemType}, because that system is already restricted to be last.");
#else
                            throw new ArgumentException($"Invalid [UpdateAfter] EndInitializationEntityCommandBufferSystem, because that system is already restricted to be last.");
#endif
                        }
                    }

                    if (parentType == typeof(SimulationSystemGroup))
                    {
                        if (dep.SystemType == typeof(BeginSimulationEntityCommandBufferSystem))
                        {
                            WarningForAfterCheck(systemType, dep.SystemType);
                            continue;
                        }

                        if (dep.SystemType == typeof(LateSimulationSystemGroup))
                        {
#if !NET_DOTS
                            throw new ArgumentException(
                                      $"Invalid [UpdateAfter] {dep.SystemType} attribute on {systemType}, because that system is already restricted to be last.");
#else
                            throw new ArgumentException($"Invalid [UpdateAfter] EndLateSimulationEntityCommandBufferSystem, because that system is already restricted to be last.");
#endif
                        }

                        if (dep.SystemType == typeof(EndSimulationEntityCommandBufferSystem))
                        {
#if !NET_DOTS
                            throw new ArgumentException(
                                      $"Invalid [UpdateAfter] {dep.SystemType} attribute on {systemType}, because that system is already restricted to be last.");
#else
                            throw new ArgumentException($"Invalid [UpdateAfter] EndSimulationEntityCommandBufferSystem, because that system is already restricted to be last.");
#endif
                        }
                    }

                    if (parentType == typeof(PresentationSystemGroup))
                    {
                        if (dep.SystemType == typeof(BeginPresentationEntityCommandBufferSystem))
                        {
                            WarningForAfterCheck(systemType, dep.SystemType);
                            continue;
                        }
                    }

                    int depIndex = LookupSysAndDep(dep.SystemType, sysAndDep);
                    if (depIndex < 0)
                    {
#if !NET_DOTS
                        Debug.LogWarning(
                            $"Ignoring invalid [UpdateAfter] attribute on {systemType} because {dep.SystemType} belongs to a different {nameof(ComponentSystemGroup)}.\n"
                            + $"This attribute can only order systems that are children of the same {nameof(ComponentSystemGroup)}.\n"
                            + $"Make sure that both systems are in the same parent group with [UpdateInGroup(typeof({parentType})].\n"
                            + $"You can also change the relative order of groups when appropriate, by using [UpdateBefore] and [UpdateAfter] attributes at the group level.");
#else
                        Debug.LogWarning("WARNING: invalid [UpdateAfter] dependency:");
                        Debug.LogWarning(TypeManager.GetSystemName(systemType));
                        Debug.LogWarning("  depends on a non-sibling system: ");
                        Debug.LogWarning(TypeManager.GetSystemName(dep.SystemType));
#endif
                        continue;
                    }

                    sysAndDep[depIndex].updateBefore.Add(systemType);
                    sysAndDep[i].nAfter++;
                }
            }

            // Clear the systems list and rebuild it in sorted order from the lookup table
            var readySystems = new Heap <TypeHeapElement>(items.Count);
            items.Clear();
            for (int i = 0; i < sysAndDep.Length; ++i)
            {
                if (sysAndDep[i].nAfter == 0)
                {
                    readySystems.Insert(new TypeHeapElement(i, sysAndDep[i].type));
                }
            }

            while (!readySystems.Empty)
            {
                var sysIndex = readySystems.Extract().unsortedIndex;
                var sd       = sysAndDep[sysIndex];

                sysAndDep[sysIndex] = new SysAndDep <T>(); // "Remove()"
                items.Add(sd.item);
                foreach (var beforeType in sd.updateBefore)
                {
                    int beforeIndex = LookupSysAndDep(beforeType, sysAndDep);
                    if (beforeIndex < 0)
                    {
                        throw new Exception("Bug in SortSystemUpdateList(), beforeIndex < 0");
                    }
                    if (sysAndDep[beforeIndex].nAfter <= 0)
                    {
                        throw new Exception("Bug in SortSystemUpdateList(), nAfter <= 0");
                    }

                    sysAndDep[beforeIndex].nAfter--;
                    if (sysAndDep[beforeIndex].nAfter == 0)
                    {
                        readySystems.Insert(new TypeHeapElement(beforeIndex, sysAndDep[beforeIndex].type));
                    }
                }
            }

            for (int i = 0; i < sysAndDep.Length; ++i)
            {
                if (sysAndDep[i].item != null)
                {
                    // Since no System in the circular dependency would have ever been added
                    // to the heap, we should have values for everything in sysAndDep. Check,
                    // just in case.
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                    var visitedSystems = new List <Type>();
                    var startIndex     = i;
                    var currentIndex   = i;
                    while (true)
                    {
                        if (sysAndDep[currentIndex].item != null)
                        {
                            visitedSystems.Add(sysAndDep[currentIndex].type);
                        }

                        currentIndex = LookupSysAndDep(sysAndDep[currentIndex].updateBefore[0], sysAndDep);
                        if (currentIndex < 0 || currentIndex == startIndex || sysAndDep[currentIndex].item == null)
                        {
                            throw new CircularSystemDependencyException(visitedSystems);
                        }
                    }
#else
                    sysAndDep[i] = new SysAndDep <T>();
#endif
                }
            }
        }
コード例 #2
0
 public TypeHeapElement(int index, Type t)
 {
     unsortedIndex = index;
     typeName      = TypeManager.GetSystemName(t);
 }
コード例 #3
0
        internal void FlushPendingBuffers(bool playBack)
        {
            m_ProducerHandle.Complete();
            m_ProducerHandle = new JobHandle();

            int length;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            length = m_PendingBuffers.Count;
#else
            length = m_PendingBuffers.Length;
#endif

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            List <string> playbackErrorLog             = null;
            bool          completeAllJobsBeforeDispose = false;
#endif
            for (int i = 0; i < length; ++i)
            {
                var buffer = m_PendingBuffers[i];
                if (!buffer.IsCreated)
                {
                    continue;
                }
                if (playBack)
                {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                    try
                    {
                        var system     = GetSystemFromSystemID(World, buffer.SystemID);
                        var systemName = system != null?TypeManager.GetSystemName(system.GetType()) : "Unknown";

                        Profiler.BeginSample(systemName);
                        buffer.Playback(EntityManager);
                        Profiler.EndSample();
                    }
                    catch (Exception e)
                    {
                        var system     = GetSystemFromSystemID(World, buffer.SystemID);
                        var systemType = system != null?system.GetType().ToString() : "Unknown";

                        var error = $"{e.Message}\nEntityCommandBuffer was recorded in {systemType} and played back in {GetType()}.\n" + e.StackTrace;
                        if (playbackErrorLog == null)
                        {
                            playbackErrorLog = new List <string>();
                        }
                        playbackErrorLog.Add(error);
                        completeAllJobsBeforeDispose = true;
                    }
#else
                    buffer.Playback(EntityManager);
#endif
                }
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                try
                {
                    if (completeAllJobsBeforeDispose)
                    {
                        // If we get here, there was an error during playback (potentially a race condition on the
                        // buffer itself), and we should wait for all jobs writing to this command buffer to complete before attempting
                        // to dispose of the command buffer to prevent a potential race condition.
                        buffer.WaitForWriterJobs();
                        completeAllJobsBeforeDispose = false;
                    }
                    buffer.Dispose();
                }
                catch (Exception e)
                {
                    var system     = GetSystemFromSystemID(World, buffer.SystemID);
                    var systemType = system != null?system.GetType().ToString() : "Unknown";

                    var error = $"{e.Message}\nEntityCommandBuffer was recorded in {systemType} and disposed in {GetType()}.\n" + e.StackTrace;
                    if (playbackErrorLog == null)
                    {
                        playbackErrorLog = new List <string>();
                    }
                    playbackErrorLog.Add(error);
                }
#else
                buffer.Dispose();
#endif
                m_PendingBuffers[i] = buffer;
            }

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            if (playbackErrorLog != null)
            {
#if !NET_DOTS
                var exceptionMessage = new StringBuilder();
                foreach (var err in playbackErrorLog)
                {
                    exceptionMessage.AppendLine(err);
                }
#else
                var exceptionMessage = "";
                foreach (var err in playbackErrorLog)
                {
                    exceptionMessage += err;
                    exceptionMessage += '\n';
                }
#endif

                throw new System.ArgumentException(exceptionMessage.ToString());
            }
#endif
        }