-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPBXSections.cs
116 lines (98 loc) · 3.23 KB
/
PBXSections.cs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
using System.IO;
// Base classes for section handling
namespace UnityEditor.iOS.Xcode
{
// common base
abstract class SectionBase
{
public abstract void ReadSection(string curLine, TextReader sr);
public abstract void WriteSection(TextWriter sw, GUIDToCommentMap comments);
}
// known section: contains objects that we care about
class KnownSectionBase<T> : SectionBase where T : PBXObjectBase, new()
{
public SortedDictionary<string, T> entry = new SortedDictionary<string, T>();
readonly string m_Name;
public KnownSectionBase(string sectionName)
{
m_Name = sectionName;
}
public override void ReadSection(string curLine, TextReader sr)
{
if (!PBXRegex.BeginSection.IsMatch(curLine))
throw new Exception("Can't read section");
if (PBXRegex.BeginSection.Match(curLine).Groups[1].Value != m_Name)
throw new Exception("Wrong section");
curLine = PBXStream.ReadSkippingEmptyLines(sr);
while (!PBXRegex.EndSection.IsMatch(curLine))
{
var obj = new T();
obj.ReadFromSection(curLine, sr);
entry[obj.guid] = obj;
curLine = sr.ReadLine();
}
}
public override void WriteSection(TextWriter sw, GUIDToCommentMap comments)
{
if (entry.Count == 0)
return; // do not write empty sections
sw.WriteLine();
sw.WriteLine("/* Begin {0} section */", m_Name);
foreach (T obj in entry.Values)
obj.WriteToSection(sw, comments);
sw.WriteLine("/* End {0} section */", m_Name);
}
public T this[string guid]
{
get
{
if (entry.ContainsKey(guid))
return entry[guid];
return null;
}
}
public void AddEntry(T obj)
{
entry[obj.guid] = obj;
}
public void RemoveEntry(string guid)
{
if (entry.ContainsKey(guid))
entry.Remove(guid);
}
}
// just stores text line by line
class TextSection : SectionBase
{
public List<string> text = new List<string>();
public override void ReadSection(string curLine, TextReader sr)
{
text.Add(curLine);
PBXStream.ReadLinesWithConditionForLastLine(sr, text, s => PBXRegex.EndSection.IsMatch(s));
}
public override void WriteSection(TextWriter sw, GUIDToCommentMap comments)
{
sw.WriteLine();
foreach (string s in text)
sw.WriteLine(s);
}
}
// we assume there is only one PBXProject entry
class PBXProjectSection : KnownSectionBase<PBXProjectObject>
{
public PBXProjectSection() : base("PBXProject")
{
}
public PBXProjectObject project
{
get
{
foreach (var kv in entry)
return kv.Value;
return null;
}
}
}
} // UnityEditor.iOS.Xcode