wasStitchNET – Diff between revs 8 and 9

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 8 Rev 9
1 /////////////////////////////////////////////////////////////////////////// 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 // 2 // Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // 3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. // 4 // rights of fair usage, the disclaimer and warranty conditions. //
5 /////////////////////////////////////////////////////////////////////////// 5 ///////////////////////////////////////////////////////////////////////////
6   6  
7 using System.Collections.Generic; 7 using System.Collections.Generic;
8 using System.Linq; 8 using System.Linq;
9 using System.Xml.Linq; 9 using System.Xml.Linq;
10 using System.Xml.XPath; 10 using System.Xml.XPath;
11   11  
12 namespace wasStitchNET.Patchers 12 namespace wasStitchNET.Patchers
13 { 13 {
14 public static class XML 14 public static class XML
15 { 15 {
-   16 public static XDocument PatchXDocument(XDocument cfg, XDocument nfg, HashSet<string> forceXPaths,
16 public static HashSet<string> GetFileDelta(string cfg, string nfg) 17 HashSet<string> excludeXPaths)
17 { 18 {
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()); 19 // Select all distinct paths in the new configuration without paths to exclude and with paths to force.
24 if (cfgElement == null) 20 foreach (var e in nfg
25 { 21 .Descendants()
26 configuredTags.Add(e.Name.LocalName); 22 .Where(e => !excludeXPaths.Any(o => e.GetAbsoluteXPath().Contains(o)))
27 continue; 23 .Concat(
28 } 24 nfg
29   -  
30 if (e.Descendants().Any()) 25 .Descendants()
31 continue; -  
32   -  
33 if (!cfgElement.Value.Equals(e.Value)) 26 .Where(e => forceXPaths.Any(o => e.GetAbsoluteXPath().Contains(o))))
34 continue; 27 .Distinct())
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 { -  
44 foreach (var e in nfg.Descendants() -  
45 .Where(e => configuredTags.Contains(e.Name.LocalName))) -  
46 { 28 {
-   29 // Get the XPath to this element.
-   30 var xpath = e.GetAbsoluteXPath();
47 // Select the element in the current configuration that is found in the default configuration. 31 // Select the element in the current configuration that is found in the default configuration.
48 var cfgElement = cfg.XPathSelectElement(e.GetAbsoluteXPath()); 32 var cfgElement = cfg.XPathSelectElement(xpath);
49 switch (cfgElement != null) 33 switch (cfgElement != null)
50 { 34 {
51 // Element found in current configuration. 35 // Element found in current configuration.
52 case true: 36 case true:
53 // Only set the element value if it has no descendants. 37 // Only set the element value if it has no descendants and it contained in the force paths.
54 if (!cfgElement.Descendants().Any()) 38 if (!cfgElement.Descendants().Any() && forceXPaths.Any(o => xpath.Contains(o)))
55 cfgElement.Value = e.Value; 39 cfgElement.Value = e.Value;
56 break; 40 break;
57 // Element not found in the current configuration. 41 // Element not found in the current configuration.
58 default: 42 default:
59 // Find the first existing parent of the default configuration in the current configuration. 43 // Find the first existing parent of the default configuration in the current configuration.
60 var parent = e; 44 var parent = e;
61 do 45 do
62 { 46 {
63 var cfgParentElement = cfg.XPathSelectElement(parent.GetAbsoluteXPath()); 47 var cfgParentElement = cfg.XPathSelectElement(parent.GetAbsoluteXPath());
64 if (cfgParentElement != null) 48 if (cfgParentElement != null)
65 { 49 {
66 // Add the default configuration parent to the current configuration. 50 // Add the default configuration parent to the current configuration.
67 cfgParentElement.ReplaceWith( 51 cfgParentElement.Add(
68 nfg.XPathSelectElement(cfgParentElement.GetAbsoluteXPath())); 52 nfg.XPathSelectElement(cfgParentElement.GetAbsoluteXPath()));
69 break; 53 break;
70 } 54 }
71 parent = parent.Parent; 55 parent = parent.Parent;
72 } while (parent != null); 56 } while (parent != null);
73 break; 57 break;
74 } 58 }
75 } 59 }
76 return cfg; 60 return cfg;
77 } 61 }
78 } 62 }
79 } 63 }
80   64  
81
Generated by GNU Enscript 1.6.5.90.
65
Generated by GNU Enscript 1.6.5.90.
82   66  
83   67  
84   68