XCPlist.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. namespace UnityEditor.XCodeEditor
  6. {
  7. public class XCPlist
  8. {
  9. string plistPath;
  10. bool plistModified;
  11. // URLTypes constant --- plist
  12. const string BundleUrlTypes = "CFBundleURLTypes";
  13. const string BundleTypeRole = "CFBundleTypeRole";
  14. const string BundleUrlName = "CFBundleURLName";
  15. const string BundleUrlSchemes = "CFBundleURLSchemes";
  16. // URLTypes constant --- projmods
  17. const string PlistUrlType = "urltype";
  18. const string PlistRole = "role";
  19. const string PlistEditor = "Editor";
  20. const string PlistName = "name";
  21. const string PlistSchemes = "schemes";
  22. public XCPlist(string plistPath)
  23. {
  24. this.plistPath = plistPath;
  25. }
  26. public void Process(Hashtable plist)
  27. {
  28. if (plist == null) return;
  29. Dictionary<string, object> dict = (Dictionary<string, object>)PlistCS.Plist.readPlist(plistPath);
  30. foreach( DictionaryEntry entry in plist)
  31. {
  32. this.AddPlistItems((string)entry.Key, entry.Value, dict);
  33. }
  34. if (plistModified)
  35. {
  36. PlistCS.Plist.writeXml(dict, plistPath);
  37. }
  38. }
  39. // http://stackoverflow.com/questions/20618809/hashtable-to-dictionary
  40. public static Dictionary<K,V> HashtableToDictionary<K,V> (Hashtable table)
  41. {
  42. Dictionary<K,V> dict = new Dictionary<K,V>();
  43. foreach(DictionaryEntry kvp in table)
  44. dict.Add((K)kvp.Key, (V)kvp.Value);
  45. return dict;
  46. }
  47. public void AddPlistItems(string key, object value, Dictionary<string, object> dict)
  48. {
  49. Debug.Log ("AddPlistItems: key=" + key);
  50. if (key.CompareTo(PlistUrlType) == 0)
  51. {
  52. processUrlTypes((ArrayList)value, dict);
  53. }
  54. else
  55. {
  56. dict[key] = HashtableToDictionary<string, object>((Hashtable)value);
  57. plistModified = true;
  58. }
  59. }
  60. private void processUrlTypes(ArrayList urltypes, Dictionary<string, object> dict)
  61. {
  62. List<object> bundleUrlTypes;
  63. if (dict.ContainsKey(BundleUrlTypes))
  64. {
  65. bundleUrlTypes = (List<object>)dict[BundleUrlTypes];
  66. }
  67. else
  68. {
  69. bundleUrlTypes = new List<object>();
  70. }
  71. foreach(Hashtable table in urltypes)
  72. {
  73. string role = (string)table[PlistRole];
  74. if (string.IsNullOrEmpty(role))
  75. {
  76. role = PlistEditor;
  77. }
  78. string name = (string)table[PlistName];
  79. ArrayList shcemes = (ArrayList)table[PlistSchemes];
  80. // new schemes
  81. List<object> urlTypeSchemes = new List<object>();
  82. foreach(string s in shcemes)
  83. {
  84. urlTypeSchemes.Add(s);
  85. }
  86. Dictionary<string, object> urlTypeDict = this.findUrlTypeByName(bundleUrlTypes, name);
  87. if (urlTypeDict == null)
  88. {
  89. urlTypeDict = new Dictionary<string, object>();
  90. urlTypeDict[BundleTypeRole] = role;
  91. urlTypeDict[BundleUrlName] = name;
  92. urlTypeDict[BundleUrlSchemes] = urlTypeSchemes;
  93. bundleUrlTypes.Add(urlTypeDict);
  94. }
  95. else
  96. {
  97. urlTypeDict[BundleTypeRole] = role;
  98. urlTypeDict[BundleUrlSchemes] = urlTypeSchemes;
  99. }
  100. plistModified = true;
  101. }
  102. dict[BundleUrlTypes] = bundleUrlTypes;
  103. }
  104. private Dictionary<string, object> findUrlTypeByName(List<object> bundleUrlTypes, string name)
  105. {
  106. if ((bundleUrlTypes == null) || (bundleUrlTypes.Count == 0))
  107. return null;
  108. foreach(Dictionary<string, object> dict in bundleUrlTypes)
  109. {
  110. string _n = (string)dict[BundleUrlName];
  111. if (string.Compare(_n, name) == 0)
  112. {
  113. return dict;
  114. }
  115. }
  116. return null;
  117. }
  118. }
  119. }