Exemplo n.º 1
0
        public IEnumerable <IEntity> Read(Stream stream, object context, Func <IPlan, object, bool> filter)
        {
            var node = _hierarchy.Lookup(context);

            var document = new XmlDocument();

            document.Load(stream);

            if (document.DocumentElement == null)
            {
                throw new InvalidOperationException("The level data doesn't contain a document element.");
            }

            // Find the <gameObjectFolder> node under the root element.  This is
            // the top of our hierarchy.
            var gameObjectFolder =
                document.DocumentElement.ChildNodes.OfType <XmlElement>()
                .FirstOrDefault(x => x.LocalName == "gameObjectFolder");

            if (gameObjectFolder == null)
            {
                throw new InvalidOperationException("No top level game folder found in ATF level.");
            }

            // Construct the plans for the level.
            var plansList = new List <IPlan>();

            foreach (var element in gameObjectFolder.ChildNodes.OfType <XmlElement>())
            {
                ProcessElementToPlan(node, null, element, plansList, 0, filter);
            }

            // Validate the level configuration.
            var plans = plansList.ToArray();
            IEnumerable <IEntity> entities = null;
            bool isOkay = false;

            try
            {
                _kernel.ValidateAll(plans);

                // Resolve all the plans.
                entities = _kernel.ResolveAll(plans).OfType <IEntity>();
                isOkay   = true;
                return(entities);
            }
            finally
            {
                if (!isOkay)
                {
                    _kernel.DiscardAll(plans);

                    foreach (var plan in plans)
                    {
                        // Also detach the child nodes that were appended to the root object.
                        _hierarchy.RemoveChildNode(node, (INode)plan);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public async Task DiscardAsync(IPlan plan)
        {
#endif
            var planAsNode = (DefaultNode)plan;
            planAsNode.Discarded = true;
            foreach (var plan1 in planAsNode.PlannedCreatedNodes)
            {
                var toCreate = (DefaultNode)plan1;
                var parent   = toCreate.ParentPlan;
                if (parent != null)
                {
                    _hierarchy.RemoveChildNode(parent, toCreate);
                }
                else
                {
                    _hierarchy.RemoveRootNode(toCreate);
                }
                toCreate.Parent    = null;
                toCreate.Discarded = true;
            }
            var nodeParent = planAsNode.ParentPlan;
            if (nodeParent != null)
            {
                _hierarchy.RemoveChildNode(nodeParent, planAsNode);
            }
            else
            {
                _hierarchy.RemoveRootNode(planAsNode);
            }

            if (plan.DiscardOnResolve.Count > 0)
            {
                foreach (var child in plan.DiscardOnResolve.ToList())
                {
                    _hierarchy.RemoveNode((INode)child);
                }
            }
        }