wasStitchNET – Blame information for rev 8
?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 | { |
||
8 | office | 16 | public static HashSet<string> GetFileDelta(string cfg, string nfg) |
7 | office | 17 | { |
8 | office | 18 | var configuredTags = new HashSet<string>(); |
19 | |||
20 | var configuration = XDocument.Load(cfg); |
||
21 | foreach (var e in XDocument.Load(nfg).Descendants()) |
||
22 | { |
||
23 | var cfgElement = configuration.XPathSelectElement(e.GetAbsoluteXPath()); |
||
24 | if (cfgElement == null) |
||
25 | { |
||
26 | configuredTags.Add(e.Name.LocalName); |
||
27 | continue; |
||
28 | } |
||
29 | |||
30 | if (e.Descendants().Any()) |
||
31 | continue; |
||
32 | |||
33 | if (!cfgElement.Value.Equals(e.Value)) |
||
34 | continue; |
||
35 | |||
36 | configuredTags.Add(e.Name.LocalName); |
||
37 | } |
||
38 | |||
39 | return configuredTags; |
||
40 | } |
||
41 | |||
42 | public static XDocument PatchXDocument(XDocument cfg, XDocument nfg, HashSet<string> configuredTags) |
||
43 | { |
||
7 | office | 44 | foreach (var e in nfg.Descendants() |
45 | .Where(e => configuredTags.Contains(e.Name.LocalName))) |
||
46 | { |
||
47 | // Select the element in the current configuration that is found in the default configuration. |
||
48 | var cfgElement = cfg.XPathSelectElement(e.GetAbsoluteXPath()); |
||
49 | switch (cfgElement != null) |
||
50 | { |
||
51 | // Element found in current configuration. |
||
52 | case true: |
||
53 | // Only set the element value if it has no descendants. |
||
54 | if (!cfgElement.Descendants().Any()) |
||
55 | cfgElement.Value = e.Value; |
||
56 | break; |
||
57 | // Element not found in the current configuration. |
||
58 | default: |
||
59 | // Find the first existing parent of the default configuration in the current configuration. |
||
8 | office | 60 | var parent = e; |
7 | office | 61 | do |
62 | { |
||
8 | office | 63 | var cfgParentElement = cfg.XPathSelectElement(parent.GetAbsoluteXPath()); |
64 | if (cfgParentElement != null) |
||
65 | { |
||
66 | // Add the default configuration parent to the current configuration. |
||
67 | cfgParentElement.ReplaceWith( |
||
68 | nfg.XPathSelectElement(cfgParentElement.GetAbsoluteXPath())); |
||
69 | break; |
||
70 | } |
||
71 | parent = parent.Parent; |
||
72 | } while (parent != null); |
||
7 | office | 73 | break; |
74 | } |
||
75 | } |
||
76 | return cfg; |
||
77 | } |
||
78 | } |
||
8 | office | 79 | } |