示例#1
0
        /// <summary>Pastes nodes from the clipboard.</summary>
        /// <param name="templateProject">The project to resolve class references.</param>
        /// <param name="logger">The logger.</param>
        /// <returns>A list of <see cref="ClassNode"/> and <see cref="BaseNode"/>. If no data was present, both lists are empty.</returns>
        public static Tuple <List <ClassNode>, List <BaseNode> > Paste(ReClassNetProject templateProject, ILogger logger)
        {
            Contract.Requires(templateProject != null);
            Contract.Requires(logger != null);
            Contract.Ensures(Contract.Result <List <BaseNode> >() != null);

            var classes = new List <ClassNode>();
            var nodes   = new List <BaseNode>();

            if (ContainsNodes)
            {
                var data = Clipboard.GetData(ClipboardFormat) as byte[];
                if (data != null)
                {
                    using (var ms = new MemoryStream(data))
                    {
                        var result = ReClassNetFile.ReadNodes(ms, templateProject, logger);
                        classes.AddRange(result.Item1);
                        nodes.AddRange(result.Item2);
                    }
                }
            }

            return(Tuple.Create(classes, nodes));
        }
        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);
            }
        }
示例#3
0
        /// <summary>Copies the nodes to the clipboard.</summary>
        /// <param name="nodes">The nodes to copy.</param>
        /// <param name="logger">The logger.</param>
        public static void Copy(IEnumerable <BaseNode> nodes, ILogger logger)
        {
            Contract.Requires(nodes != null);
            Contract.Requires(logger != null);

            using (var ms = new MemoryStream())
            {
                ReClassNetFile.WriteNodes(ms, nodes, logger);

                Clipboard.SetData(ClipboardFormat, ms.ToArray());
            }
        }
示例#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));
            }
        }