-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathProjectDefinitionProperties.cs
More file actions
102 lines (84 loc) · 2.25 KB
/
Copy pathProjectDefinitionProperties.cs
File metadata and controls
102 lines (84 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using RPGCore.Packages;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
namespace RPGCore.Projects;
public class ProjectDefinitionProperties : IDefinitionProperties
{
private readonly FileInfo file;
private readonly XmlDocument document;
public string Name
{
get
{
string name = GetNodeForProperty("Name")?.InnerText;
return name ?? file.Name.Replace(".bproj", "");
}
set => GetOrCreateNodeForProperty("Name").InnerText = value;
}
public string Version
{
get => GetNodeForProperty("Version")?.InnerText;
set => GetOrCreateNodeForProperty("Version").InnerText = value;
}
public ProjectDefinitionProperties(FileInfo file, XmlDocument document)
{
this.file = file;
this.document = document;
}
private XmlNode GetNodeForProperty(string name)
{
foreach (var propertyGroup in PropertyGroups())
{
var properties = propertyGroup.ChildNodes;
for (int j = 0; j < properties.Count; j++)
{
var property = properties.Item(j);
if (property.Name == name)
{
return property;
}
}
}
return null;
}
private XmlNode GetOrCreateNodeForProperty(string name)
{
var node = GetNodeForProperty(name);
if (node != null)
{
return node;
}
var propertyGroup = PropertyGroups().FirstOrDefault();
if (propertyGroup == null)
{
var prefix = document.CreateWhitespace("\n ");
document.DocumentElement.AppendChild(prefix);
var newPropertyGroup = document.CreateElement("PropertyGroup", null);
document.DocumentElement.AppendChild(newPropertyGroup);
propertyGroup = newPropertyGroup;
prefix = document.CreateWhitespace("\n\n");
document.DocumentElement.AppendChild(prefix);
}
var whitespace = document.CreateWhitespace("\n ");
propertyGroup.AppendChild(whitespace);
var propertyNode = document.CreateElement(name, null);
propertyGroup.AppendChild(propertyNode);
whitespace = document.CreateWhitespace("\n ");
propertyGroup.AppendChild(whitespace);
return propertyNode;
}
private IEnumerable<XmlNode> PropertyGroups()
{
var rootNodes = document.DocumentElement.ChildNodes;
for (int j = 0; j < rootNodes.Count; j++)
{
var property = rootNodes.Item(j);
if (property.Name == "PropertyGroup")
{
yield return property;
}
}
}
}