Ejemplo n.º 1
0
        private static void OverrideCombine(List <string> originalFiles, List <string> files, string outputPath, bool binaryCombine)
        {
            bool allSameSize = true;

            var originalHashes     = originalFiles.Select(file => Util.ComputeSHA256(file)).ToList();
            var modifiedFileHashes = new Dictionary <string, List <int> >();
            var size = new FileInfo(files[0]).Length;

            for (var i = 0; i < files.Count; i++)
            {
                var file = files[i];

                var hash = Util.ComputeSHA256(file);
                if (!originalHashes.Contains(hash))
                {
                    if (modifiedFileHashes.ContainsKey(hash))
                    {
                        modifiedFileHashes[hash].Add(i);
                    }
                    else
                    {
                        modifiedFileHashes.Add(hash, new List <int>()
                        {
                            i
                        });
                    }
                    if (new FileInfo(file).Length != size)
                    {
                        allSameSize = false;
                    }
                }
            }

            if (modifiedFileHashes.Count == 0)
            {
                return;
            }

            if (modifiedFileHashes.Count == 1)
            {
                if (File.Exists(outputPath))
                {
                    File.Delete(outputPath);
                }
                File.Copy(files[modifiedFileHashes.First().Value[0]], outputPath);
                return;
            }

            if (binaryCombine && allSameSize)
            {
                BinaryCombiner.Combine(originalFiles, files, outputPath, notifyCollissions);
            }
            else
            {
                var items = new List <SelectionItem>();
                items.Add(new SelectionItem()
                {
                    Name = "Original", Value = originalFiles[originalFiles.Count - 1] + "\\" + Path.GetFileName(originalFiles[0])
                });
                foreach (KeyValuePair <string, List <int> > pair in modifiedFileHashes)
                {
                    var item = new SelectionItem()
                    {
                        Name        = pair.Value.Count == 1 ? rootFiles[pair.Value[0]] + "\\" + Path.GetFileName(originalFiles[0]) : "",
                        Description = pair.Value.Count == 1 ? null : rootFiles[pair.Value[0]] + "\\" + Path.GetFileName(originalFiles[0]),
                        Value       = files[pair.Value[0]]
                    };
                    items.Add(item);
                }

                string replacingFile = (string)items[items.Count - 1].Value;
                object result        = null;
                if (notifyCollissions && SelectionDialog.Show("Select overriding file", items, out result, out notifyCollissions))
                {
                    replacingFile = (string)result;
                }
                if (outputPath == replacingFile)
                {
                    return;
                }
                if (File.Exists(outputPath))
                {
                    File.Delete(outputPath);
                }
                File.Copy(replacingFile, outputPath);
            }
        }
Ejemplo n.º 2
0
        private static void RecursiveCombine(XmlNode result, List <XmlNode> originalNodes, List <XmlNode> nodes, List <string> breadcrumbs)
        {
            if (result.NodeType == XmlNodeType.Text)
            {
                foreach (var node in originalNodes)
                {
                    if (node.NodeType != XmlNodeType.Text)
                    {
                        throw new Exception("All original nodes are not text nodes. Post a comment at jc3mods telling what mods you were trying to combine.");
                    }
                }
                foreach (var node in nodes)
                {
                    if (node.NodeType != XmlNodeType.Text)
                    {
                        throw new Exception("All nodes are not text nodes. Post a comment at jc3mods telling what mods you were trying to combine.");
                    }
                }

                var selectionItems = GetSelectionItems(originalNodes, nodes, breadcrumbs);

                if (selectionItems.Count == 0)
                {
                    return;
                }
                if (selectionItems.Count == 1)
                {
                    if (selectionItems[0].Name != "Original")
                    {
                        ChangeNode(result, (XmlNode)selectionItems[0].Value);
                    }
                    return;
                }
                if (selectionItems[0].Name == "Original" && selectionItems.Count == 2)
                {
                    ChangeNode(result, (XmlNode)selectionItems[1].Value);
                }
                var    replacingNode   = (XmlNode)selectionItems[selectionItems.Count - 1].Value;
                object selectionResult = null;
                if (notifyCollissions && SelectionDialog.Show("Collission at " + XmlTools.GetPath(result), selectionItems, out selectionResult, out notifyCollissions))
                {
                    replacingNode = (XmlNode)selectionResult;
                }
                ChangeNode(result, replacingNode);
            }
            else if (result.NodeType == XmlNodeType.Element)
            {
                var resultNodeDict = new NodeDictionary(result);
                var nodeDict       = new NodeDictionary();
                foreach (var node in nodes)
                {
                    foreach (XmlNode childNode in node)
                    {
                        var id = XmlTools.GetID(childNode);
                        if (id != null)
                        {
                            nodeDict[id] = childNode;
                        }
                    }
                }

                foreach (var node in originalNodes)
                {
                    if (node.NodeType != XmlNodeType.Element)
                    {
                        throw new Exception("All original nodes are not Elements. Post a comment at jc3mods telling what mods you were trying to combine.");
                    }
                }
                foreach (var node in nodes)
                {
                    if (node.NodeType != XmlNodeType.Element)
                    {
                        throw new Exception("All nodes are not Elements. Post a comment at jc3mods telling what mods you were trying to combine.");
                    }
                }

                var nodesToRemove = new List <XmlNode>();
                foreach (XmlNode resultChildNode in result)
                {
                    var correspondingOriginalNodes = GetCorrespondingNodes(resultChildNode, originalNodes);
                    var correspondingNodes         = GetCorrespondingNodes(resultChildNode, nodes);

                    var changeIds = GetChangedIds(correspondingOriginalNodes, correspondingNodes);
                    if (changeIds.Count == 0)
                    {
                        continue;
                    }
                    if (changeIds.Count == 1)
                    {
                        if (correspondingNodes[changeIds[0][0]] == null)
                        {
                            nodesToRemove.Add(resultChildNode);
                        }
                        else
                        {
                            ChangeNode(resultChildNode, correspondingNodes[changeIds[0][0]]);
                        }
                        continue;
                    }

                    var nullFound          = false;
                    var nonNullChangeFound = false;

                    foreach (List <int> l in changeIds)
                    {
                        foreach (int i in l)
                        {
                            var node = nodes[i];
                            if (node == null)
                            {
                                nullFound = true;
                            }
                            else
                            {
                                nonNullChangeFound = true;
                            }
                        }
                    }

                    if (nullFound && !nonNullChangeFound)
                    {
                        nodesToRemove.Add(resultChildNode);
                    }
                    else
                    {
                        var nullIds     = new List <int>();
                        var continueIds = new List <int>();

                        foreach (List <int> l in changeIds)
                        {
                            foreach (int i in l)
                            {
                                if (correspondingNodes[i] == null)
                                {
                                    nullIds.Add(i);
                                }
                                else
                                {
                                    continueIds.Add(i);
                                }
                            }
                        }

                        var continueCombining = correspondingNodes[changeIds[changeIds.Count - 1][0]] != null;
                        if (notifyCollissions && nullIds.Count > 0)
                        {
                            var selectionItems = new List <SelectionItem>();
                            selectionItems.Add(new SelectionItem()
                            {
                                Name = "Null", Description = "", Value = false
                            });
                            selectionItems.Add(new SelectionItem()
                            {
                                Name = "Continue combining", Description = "", Value = true
                            });
                            foreach (int index in nullIds)
                            {
                                selectionItems[0].Description += (selectionItems[0].Description.Length == 0 ? "" : "\n") + breadcrumbs[index];
                            }
                            foreach (int index in continueIds)
                            {
                                selectionItems[1].Description += (selectionItems[1].Description.Length == 0 ? "" : "\n") + breadcrumbs[index];
                            }
                            object selectionResult = null;
                            if (SelectionDialog.Show("Collission at " + XmlTools.GetPath(resultChildNode), selectionItems, out selectionResult, out notifyCollissions))
                            {
                                continueCombining = (bool)selectionResult;
                            }
                        }
                        if (!continueCombining)
                        {
                            nodesToRemove.Add(resultChildNode);
                        }
                        else
                        {
                            var originalChildNodes2 = correspondingOriginalNodes;
                            originalChildNodes2.RemoveAll(item => item == null);
                            var childNodes2  = new List <XmlNode>(continueIds.Count);
                            var breadcrumbs2 = new List <string>(continueIds.Count);
                            foreach (int index in continueIds)
                            {
                                childNodes2.Add(correspondingNodes[index]);
                                breadcrumbs2.Add(breadcrumbs[index]);
                            }
                            RecursiveCombine(resultChildNode, originalChildNodes2, childNodes2, breadcrumbs2);
                        }
                    }
                }
                foreach (XmlNode node in nodesToRemove)
                {
                    result.RemoveChild(node);
                }
                foreach (KeyValuePair <string, XmlNode> entry in nodeDict)
                {
                    if (!resultNodeDict.ContainsKey(entry.Key))
                    {
                        result.AppendChild(result.OwnerDocument.ImportNode(entry.Value, true));
                    }
                }
            }
            else
            {
                throw new Exception("Cant combine nodes of type " + result.NodeType);
            }
        }