wasStitchNET – Blame information for rev 9
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
7 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 // |
||
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
||
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
||
5 | /////////////////////////////////////////////////////////////////////////// |
||
6 | |||
7 | using System.Collections.Generic; |
||
8 | using System.Linq; |
||
9 | using System.Xml.Linq; |
||
10 | using System.Xml.XPath; |
||
11 | |||
12 | namespace wasStitchNET.Patchers |
||
13 | { |
||
14 | public static class XML |
||
15 | { |
||
9 | office | 16 | public static XDocument PatchXDocument(XDocument cfg, XDocument nfg, HashSet<string> forceXPaths, |
17 | HashSet<string> excludeXPaths) |
||
7 | office | 18 | { |
9 | office | 19 | // Select all distinct paths in the new configuration without paths to exclude and with paths to force. |
20 | foreach (var e in nfg |
||
21 | .Descendants() |
||
22 | .Where(e => !excludeXPaths.Any(o => e.GetAbsoluteXPath().Contains(o))) |
||
23 | .Concat( |
||
24 | nfg |
||
25 | .Descendants() |
||
26 | .Where(e => forceXPaths.Any(o => e.GetAbsoluteXPath().Contains(o)))) |
||
27 | .Distinct()) |
||
8 | office | 28 | { |
9 | office | 29 | // Get the XPath to this element. |
30 | var xpath = e.GetAbsoluteXPath(); |
||
7 | office | 31 | // Select the element in the current configuration that is found in the default configuration. |
9 | office | 32 | var cfgElement = cfg.XPathSelectElement(xpath); |
7 | office | 33 | switch (cfgElement != null) |
34 | { |
||
35 | // Element found in current configuration. |
||
36 | case true: |
||
9 | office | 37 | // Only set the element value if it has no descendants and it contained in the force paths. |
38 | if (!cfgElement.Descendants().Any() && forceXPaths.Any(o => xpath.Contains(o))) |
||
7 | office | 39 | cfgElement.Value = e.Value; |
40 | break; |
||
41 | // Element not found in the current configuration. |
||
42 | default: |
||
43 | // Find the first existing parent of the default configuration in the current configuration. |
||
8 | office | 44 | var parent = e; |
7 | office | 45 | do |
46 | { |
||
8 | office | 47 | var cfgParentElement = cfg.XPathSelectElement(parent.GetAbsoluteXPath()); |
48 | if (cfgParentElement != null) |
||
49 | { |
||
50 | // Add the default configuration parent to the current configuration. |
||
9 | office | 51 | cfgParentElement.Add( |
8 | office | 52 | nfg.XPathSelectElement(cfgParentElement.GetAbsoluteXPath())); |
53 | break; |
||
54 | } |
||
55 | parent = parent.Parent; |
||
56 | } while (parent != null); |
||
7 | office | 57 | break; |
58 | } |
||
59 | } |
||
60 | return cfg; |
||
61 | } |
||
62 | } |
||
8 | office | 63 | } |