private static void Insert(IList <EntitySystemConfiguration> sorted, EntitySystemConfiguration current, // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local int collisionDegree = 0) { if (collisionDegree > sorted.Count + 1) { throw new OverflowException("A unresolvable collision occurred in the entity system configurations."); } int indexA = sorted.Count - 1; for (; indexA >= 0; indexA--) { EntitySystemConfiguration system = sorted[indexA]; if (Contains(current.Attribute.After, system.Attribute.Name)) { indexA++; break; } if (Contains(system.Attribute.Before, current.Attribute.Name)) { indexA++; break; } } int indexB = 0; for (; indexB < sorted.Count; indexB++) { EntitySystemConfiguration system = sorted[indexB]; if (Contains(current.Attribute.Before, system.Attribute.Name)) { break; } if (Contains(system.Attribute.After, current.Attribute.Name)) { break; } } if (indexB >= indexA) //no collision { sorted.Insert(indexB, current); } else //collision { EntitySystemConfiguration collision = sorted[indexB]; sorted.RemoveAt(indexB); Insert(sorted, current, collisionDegree + 1); Insert(sorted, collision, collisionDegree + 1); } }
private static void SortEntitySystems(ref List <EntitySystemConfiguration> systems) { List <EntitySystemConfiguration> sorted = new List <EntitySystemConfiguration>(systems.Count); List <EntitySystemConfiguration> replace = new List <EntitySystemConfiguration>(systems.Count); for (int i = 0; i < systems.Count; i++) { EntitySystemConfiguration current = systems[i]; if (!string.IsNullOrEmpty(current.Attribute.Replace)) { replace.Add(current); continue; } if (sorted.Count == 0) { sorted.Add(current); continue; } Insert(sorted, current); } for (int i = 0; i < replace.Count; i++) { EntitySystemConfiguration current = replace[i]; for (int k = 0; k < sorted.Count; k++) { if (sorted[k].Attribute.Name == current.Attribute.Replace) { if (current.Attribute.After == null && current.Attribute.Before == null) { sorted[k] = current; break; } sorted.RemoveAt(k); if (sorted.Count == 0) { sorted.Add(current); continue; } Insert(sorted, current); } } } systems.Clear(); systems = sorted; }