-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy pathXCPlist.cs
132 lines (118 loc) · 3.33 KB
/
XCPlist.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace UnityEditor.XCodeEditor
{
public class XCPlist
{
string plistPath;
bool plistModified;
// URLTypes constant --- plist
const string BundleUrlTypes = "CFBundleURLTypes";
const string BundleTypeRole = "CFBundleTypeRole";
const string BundleUrlName = "CFBundleURLName";
const string BundleUrlSchemes = "CFBundleURLSchemes";
// URLTypes constant --- projmods
const string PlistUrlType = "urltype";
const string PlistRole = "role";
const string PlistEditor = "Editor";
const string PlistName = "name";
const string PlistSchemes = "schemes";
public XCPlist(string plistPath)
{
this.plistPath = plistPath;
}
public void Process(Hashtable plist)
{
Dictionary<string, object> dict = (Dictionary<string, object>)PlistCS.Plist.readPlist(plistPath);
foreach( DictionaryEntry entry in plist)
{
this.AddPlistItems((string)entry.Key, entry.Value, dict);
}
if (plistModified)
{
PlistCS.Plist.writeXml(dict, plistPath);
}
}
// http://stackoverflow.com/questions/20618809/hashtable-to-dictionary
public static Dictionary<K,V> HashtableToDictionary<K,V> (Hashtable table)
{
Dictionary<K,V> dict = new Dictionary<K,V>();
foreach(DictionaryEntry kvp in table)
dict.Add((K)kvp.Key, (V)kvp.Value);
return dict;
}
public void AddPlistItems(string key, object value, Dictionary<string, object> dict)
{
Debug.Log ("AddPlistItems: key=" + key);
if (key.CompareTo(PlistUrlType) == 0)
{
processUrlTypes((ArrayList)value, dict);
}
else
{
dict[key] = HashtableToDictionary<string, object>((Hashtable)value);
plistModified = true;
}
}
private void processUrlTypes(ArrayList urltypes, Dictionary<string, object> dict)
{
List<object> bundleUrlTypes;
if (dict.ContainsKey(BundleUrlTypes))
{
bundleUrlTypes = (List<object>)dict[BundleUrlTypes];
}
else
{
bundleUrlTypes = new List<object>();
}
foreach(Hashtable table in urltypes)
{
string role = (string)table[PlistRole];
if (string.IsNullOrEmpty(role))
{
role = PlistEditor;
}
string name = (string)table[PlistName];
ArrayList shcemes = (ArrayList)table[PlistSchemes];
// new schemes
List<object> urlTypeSchemes = new List<object>();
foreach(string s in shcemes)
{
urlTypeSchemes.Add(s);
}
Dictionary<string, object> urlTypeDict = this.findUrlTypeByName(bundleUrlTypes, name);
if (urlTypeDict == null)
{
urlTypeDict = new Dictionary<string, object>();
urlTypeDict[BundleTypeRole] = role;
urlTypeDict[BundleUrlName] = name;
urlTypeDict[BundleUrlSchemes] = urlTypeSchemes;
bundleUrlTypes.Add(urlTypeDict);
}
else
{
urlTypeDict[BundleTypeRole] = role;
urlTypeDict[BundleUrlSchemes] = urlTypeSchemes;
}
plistModified = true;
}
dict[BundleUrlTypes] = bundleUrlTypes;
}
private Dictionary<string, object> findUrlTypeByName(List<object> bundleUrlTypes, string name)
{
if ((bundleUrlTypes == null) || (bundleUrlTypes.Count == 0))
return null;
foreach(Dictionary<string, object> dict in bundleUrlTypes)
{
string _n = (string)dict[BundleUrlName];
if (string.Compare(_n, name) == 0)
{
return dict;
}
}
return null;
}
}
}