Beispiel #1
0
        public static Tuple <List <ClassNode>, List <BaseNode> > DeserializeNodesFromStream(Stream input, ReClassNetProject templateProject, ILogger logger)
        {
            Contract.Requires(input != null);
            Contract.Requires(logger != null);
            Contract.Ensures(Contract.Result <Tuple <List <ClassNode>, List <BaseNode> > >() != null);

            using (var project = new ReClassNetProject())
            {
                templateProject?.Classes.ForEach(project.AddClass);

                var file = new ReClassNetFile(project);
                file.Load(input, logger);

                var classes = project.Classes
                              .Where(c => c.Name != SerializationClassName);
                if (templateProject != null)
                {
                    classes = classes.Where(c => !templateProject.ContainsClass(c.Uuid));
                }

                var nodes = project.Classes
                            .Where(c => c.Name == SerializationClassName)
                            .SelectMany(c => c.Nodes);

                return(Tuple.Create(classes.ToList(), nodes.ToList()));
            }
        }
        private void PasteNodeFromClipboardToSelection()
        {
            var result = ReClassClipboard.Paste(project, Program.Logger);

            foreach (var classNode in result.Item1)
            {
                if (!project.ContainsClass(classNode.Uuid))
                {
                    project.AddClass(classNode);
                }
            }

            if (selectedNodes.Count == 1)
            {
                var selectedNode = selectedNodes.First().Node;
                var parent       = selectedNode.ParentNode as ClassNode;
                if (parent != null)
                {
                    foreach (var node in result.Item2)
                    {
                        if (IsCycleFree(parent, node))
                        {
                            parent.InsertNode(selectedNode, node);
                        }
                    }
                }
            }
        }
        public static void WriteNodes(Stream output, IEnumerable <BaseNode> nodes, ILogger logger)
        {
            Contract.Requires(output != null);
            Contract.Requires(nodes != null);
            Contract.Requires(Contract.ForAll(nodes, n => n != null));
            Contract.Requires(logger != null);

            using (var project = new ReClassNetProject())
            {
                Action <BaseReferenceNode> recursiveAddReferences = null;
                recursiveAddReferences = delegate(BaseReferenceNode referenceNode)
                {
                    if (project.ContainsClass(referenceNode.InnerNode.Uuid))
                    {
                        return;
                    }

                    project.AddClass(referenceNode.InnerNode);

                    foreach (var reference in referenceNode.InnerNode.Nodes.OfType <BaseReferenceNode>())
                    {
                        recursiveAddReferences(reference);
                    }
                };

                var serialisationClass = new ClassNode(false)
                {
                    Name = SerialisationClassName
                };

                project.AddClass(serialisationClass);

                foreach (var node in nodes)
                {
                    var classNode = node as ClassNode;
                    if (classNode != null)
                    {
                        project.AddClass(classNode);

                        continue;
                    }

                    var referenceNode = node as BaseReferenceNode;
                    if (referenceNode != null)
                    {
                        recursiveAddReferences(referenceNode);
                    }

                    serialisationClass.AddNode(node);
                }

                var file = new ReClassNetFile(project);
                file.Save(output, logger);
            }
        }
Beispiel #4
0
        public static Tuple <List <ClassNode>, List <BaseNode> > ReadNodes(Stream input, ReClassNetProject templateProject, ILogger logger)
        {
            Contract.Requires(input != null);
            Contract.Requires(logger != null);

            using (var project = new ReClassNetProject())
            {
                if (templateProject != null)
                {
                    templateProject.Classes.ForEach(project.AddClass);
                }

                var file = new ReClassNetFile(project);
                file.Load(input, logger);

                var classes = new List <ClassNode>();

                var nodes = new List <BaseNode>();

                var serialisationClassNode = project.Classes.FirstOrDefault(c => c.Name == SerialisationClassName);
                if (serialisationClassNode != null)
                {
                    if (templateProject != null)
                    {
                        foreach (var classNode in project.Classes.Where(c => c != serialisationClassNode))
                        {
                            if (!templateProject.ContainsClass(classNode.Uuid))
                            {
                                classes.Add(classNode);
                            }
                        }
                    }

                    nodes.AddRange(serialisationClassNode.Nodes);

                    project.Remove(serialisationClassNode);
                }

                return(Tuple.Create(classes, nodes));
            }
        }
Beispiel #5
0
        public static void SerializeNodesToStream(Stream output, IEnumerable <BaseNode> nodes, ILogger logger)
        {
            Contract.Requires(output != null);
            Contract.Requires(nodes != null);
            Contract.Requires(Contract.ForAll(nodes, n => n != null));
            Contract.Requires(logger != null);

            using var project = new ReClassNetProject();

            void RecursiveAddClasses(BaseNode node)
            {
                ClassNode classNode = null;

                switch (node)
                {
                case ClassNode c1:
                    classNode = c1;
                    break;

                case BaseWrapperNode wrapperNode when wrapperNode.ResolveMostInnerNode() is ClassNode c2:
                    classNode = c2;

                    break;
                }

                if (classNode == null || project.ContainsClass(classNode.Uuid))
                {
                    return;
                }

                project.AddClass(classNode);

                foreach (var wrapperNodeChild in classNode.Nodes.OfType <BaseWrapperNode>())
                {
                    RecursiveAddClasses(wrapperNodeChild);
                }
            }

            var serialisationClass = new ClassNode(false)
            {
                Name = SerializationClassName
            };

            var needsSerialisationClass = true;

            foreach (var node in nodes)
            {
                RecursiveAddClasses(node);

                if (!(node is ClassNode))
                {
                    if (needsSerialisationClass)
                    {
                        needsSerialisationClass = false;

                        project.AddClass(serialisationClass);
                    }

                    serialisationClass.AddNode(node);
                }
            }

            var file = new ReClassNetFile(project);

            file.Save(output, logger);
        }