public static Tuple <List <ClassNode>, List <BaseNode> > ReadNodes(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 = new List <ClassNode>();

                var nodes = new List <BaseNode>();

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

                    nodes.AddRange(serialisationClassNode.Nodes);

                    project.Remove(serialisationClassNode);
                }

                return(Tuple.Create(classes, nodes));
            }
        }
Exemple #2
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()));
            }
        }
        /// <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.SerializeNodesToStream(ms, nodes, logger);

            Clipboard.SetData(ClipboardFormat, ms.ToArray());
        }
        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())
            {
                void RecursiveAddReferences(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)
                {
                    if (node is ClassNode classNode)
                    {
                        project.AddClass(classNode);

                        continue;
                    }

                    if (node is BaseReferenceNode referenceNode)
                    {
                        RecursiveAddReferences(referenceNode);
                    }

                    serialisationClass.AddNode(node);
                }

                var file = new ReClassNetFile(project);
                file.Save(output, logger);
            }
        }
        /// <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 <Tuple <List <ClassNode>, List <BaseNode> > >() != null);

            if (ContainsNodes && Clipboard.GetData(ClipboardFormat) is byte[] data)
            {
                using var ms = new MemoryStream(data);

                return(ReClassNetFile.DeserializeNodesFromStream(ms, templateProject, logger));
            }

            return(Tuple.Create(new List <ClassNode>(), new List <BaseNode>()));
        }
Exemple #6
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 <Tuple <List <ClassNode>, List <BaseNode> > >() != null);

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

            if (ContainsNodes)
            {
                if (Clipboard.GetData(ClipboardFormat) is byte[] data)
                {
                    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));
        }
Exemple #7
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);
        }