/// <summary> /// <para> /// Creates a NSObject representing the given .NET Object. /// </para><para> /// Numerics of type <see cref="bool"/>, <see cref="int"/>, <see cref="long"/>, <see cref="short"/>, <see cref="byte"/>, <see cref="float"/> or <see cref="double"/> are wrapped as NSNumber objects. /// </para><para> /// Strings are wrapped as <see cref="NSString"/> objects and byte arrays as <see cref="NSData"/> objects. /// </para><para> /// DateTime objects are wrapped as <see cref="NSDate"/> objects. /// </para><para> /// Serializable classes are serialized and their data is stored in <see cref="NSData"/> objects. /// </para><para> /// Arrays and Collection objects are converted to <see cref="NSArray"/> where each array member is wrapped into a <see cref="NSObject"/>. /// </para><para> /// Dictionaries are converted to <see cref="NSDictionary"/>. Each key is converted to a string and each value wrapped into a <see cref="NSObject"/>. /// </para> /// </summary> /// <param name="o">The object to represent.</param> ///<returns>A NSObject equivalent to the given object.</returns> public static NSObject Wrap(Object o) { if (o == null) throw new NullReferenceException("A null object cannot be wrapped as a NSObject"); if (o is NSObject) return (NSObject)o; Type c = o.GetType(); if (typeof(bool).Equals(c)) { return Wrap((bool)o); } if (typeof(Byte).Equals(c)) { return Wrap((int)(Byte)o); } if (typeof(short).Equals(c)) { return Wrap((int)(short)o); } if (typeof(int).Equals(c)) { return Wrap((int)(int)o); } if (typeof(long).IsAssignableFrom(c)) { return Wrap((long)o); } if (typeof(float).Equals(c)) { return Wrap((double)(float)o); } if (typeof(double).IsAssignableFrom(c)) { return Wrap((double)o); } if (typeof(string).Equals(c)) { return new NSString((string)o); } if (typeof(DateTime).Equals(c)) { return new NSDate((DateTime)o); } if (c.IsArray) { Type cc = c.GetElementType(); if (cc.Equals(typeof(byte))) { return Wrap((byte[])o); } if (cc.Equals(typeof(bool))) { bool[] array = (bool[])o; NSArray nsa = new NSArray(array.Length); for (int i = 0; i < array.Length; i++) nsa.SetValue(i, Wrap(array[i])); return nsa; } if (cc.Equals(typeof(float))) { float[] array = (float[])o; NSArray nsa = new NSArray(array.Length); for (int i = 0; i < array.Length; i++) nsa.SetValue(i, Wrap(array[i])); return nsa; } if (cc.Equals(typeof(double))) { double[] array = (double[])o; NSArray nsa = new NSArray(array.Length); for (int i = 0; i < array.Length; i++) nsa.SetValue(i, Wrap(array[i])); return nsa; } if (cc.Equals(typeof(short))) { short[] array = (short[])o; NSArray nsa = new NSArray(array.Length); for (int i = 0; i < array.Length; i++) nsa.SetValue(i, Wrap(array[i])); return nsa; } if (cc.Equals(typeof(int))) { int[] array = (int[])o; NSArray nsa = new NSArray(array.Length); for (int i = 0; i < array.Length; i++) nsa.SetValue(i, Wrap(array[i])); return nsa; } if (cc.Equals(typeof(long))) { long[] array = (long[])o; NSArray nsa = new NSArray(array.Length); for (int i = 0; i < array.Length; i++) nsa.SetValue(i, Wrap(array[i])); return nsa; } return Wrap((Object[])o); } if (typeof(Dictionary<string,Object>).IsAssignableFrom(c)) { Dictionary<string,Object> netDict = (Dictionary<string,Object>)o; NSDictionary dict = new NSDictionary(); foreach (KeyValuePair<string, Object> kvp in netDict) { dict.Add(kvp.Key, Wrap(kvp.Value)); } return dict; } if (typeof(List<Object>).IsAssignableFrom(c)) return Wrap(((List<Object>)o).ToArray()); return WrapSerialized(o); }
public static void AddBundleURLType(IIgorModule ModuleInst, string PlistPath, string NewURLScheme) { if(IgorAssert.EnsureTrue(ModuleInst, File.Exists(PlistPath), "Plist " + PlistPath + " doesn't exist!")) { FileInfo PlistFileInfo = new FileInfo(PlistPath); NSObject PlistRoot = PropertyListParser.Parse(PlistFileInfo); if(IgorAssert.EnsureTrue(ModuleInst, PlistRoot != null, "Plist " + PlistPath + " could not be parsed!")) { if(IgorAssert.EnsureTrue(ModuleInst, typeof(NSDictionary).IsAssignableFrom(PlistRoot.GetType()), "Plist " + PlistPath + " root object is not a dictionary.")) { NSDictionary RootDictionary = (NSDictionary)PlistRoot; if(IgorAssert.EnsureTrue(ModuleInst, RootDictionary != null, "Plist root is not a dictionary.")) { NSSet BundleURLTypes = null; if(RootDictionary.ContainsKey("CFBundleURLTypes")) { NSObject BundleURLTypesObj = RootDictionary.Get("CFBundleURLTypes"); if(IgorAssert.EnsureTrue(ModuleInst, BundleURLTypesObj != null, "CFBundleURLTypes wasn't found in the root dictionary even though the key exists.")) { if(IgorAssert.EnsureTrue(ModuleInst, typeof(NSArray).IsAssignableFrom(BundleURLTypesObj.GetType()), "CFBundleURLTypes isn't an NSArray.")) { BundleURLTypes = new NSSet(((NSArray)BundleURLTypesObj).GetArray()); } } } if(BundleURLTypes == null) { BundleURLTypes = new NSSet(); } bool bAlreadyExists = false; foreach(NSObject CurrentURLType in BundleURLTypes) { if(bAlreadyExists) { break; } if(IgorAssert.EnsureTrue(ModuleInst, typeof(NSDictionary).IsAssignableFrom(CurrentURLType.GetType()), "One of the CFBundleURLTypes isn't an NSDictionary.")) { NSDictionary CurrentURLTypeDict = (NSDictionary)CurrentURLType; if(IgorAssert.EnsureTrue(ModuleInst, CurrentURLTypeDict != null, "One of the CFBundleURLTypes didn't cast to NSDictionary correctly.")) { if(CurrentURLTypeDict.ContainsKey("CFBundleURLSchemes")) { NSObject CurrentURLSchemesArrayObj = CurrentURLTypeDict.Get("CFBundleURLSchemes"); if(IgorAssert.EnsureTrue(ModuleInst, typeof(NSArray).IsAssignableFrom(CurrentURLSchemesArrayObj.GetType()), "A CFBundleURLSchemes key exists for a given CFBundleURLType, but it's not an NSArray type.")) { NSArray CurrentURLSchemesArray = (NSArray)CurrentURLSchemesArrayObj; if(IgorAssert.EnsureTrue(ModuleInst, CurrentURLSchemesArray != null, "The CFBundleURLSchemes object didn't cast to NSDictionary correctly.")) { NSSet CurrentURLSchemesSet = new NSSet(CurrentURLSchemesArray.GetArray()); foreach(NSObject CurrentURLSchemeObj in CurrentURLSchemesSet) { if(IgorAssert.EnsureTrue(ModuleInst, typeof(NSString).IsAssignableFrom(CurrentURLSchemeObj.GetType()), "One of the CFBundleURLSchemes is not an NSString.")) { NSString CurrentURLScheme = (NSString)CurrentURLSchemeObj; if(IgorAssert.EnsureTrue(ModuleInst, CurrentURLScheme != null, "A CFBundleURLScheme entry didn't cast to NSString correctly.")) { if(CurrentURLScheme.GetContent() == NewURLScheme) { bAlreadyExists = true; IgorDebug.Log(ModuleInst, "URL scheme " + NewURLScheme + " is already in " + PlistPath); break; } } } } } } } } } } if(!bAlreadyExists) { NSString NewSchemeString = new NSString(NewURLScheme); NSArray NewSchemeArray = new NSArray(1); NewSchemeArray.SetValue(0, NewSchemeString); NSDictionary NewTypeDictionary = new NSDictionary(); NewTypeDictionary.Add("CFBundleURLSchemes", NewSchemeArray); BundleURLTypes.AddObject(NewTypeDictionary); NSArray BundleURLTypesArray = new NSArray(BundleURLTypes.AllObjects()); if(RootDictionary.ContainsKey("CFBundleURLTypes")) { RootDictionary["CFBundleURLTypes"] = BundleURLTypesArray; IgorDebug.Log(ModuleInst, "Updated CFBundleURLTypes to add " + NewURLScheme + "."); } else { RootDictionary.Add("CFBundleURLTypes", BundleURLTypesArray); IgorDebug.Log(ModuleInst, "Added CFBundleURLTypes to add " + NewURLScheme + "."); } IgorRuntimeUtils.DeleteFile(PlistPath); PropertyListParser.SaveAsXml(RootDictionary, PlistFileInfo); } } } } } }
/// <summary> /// Creates a NSArray with the contents of the given array. /// </summary> /// <param name="value">The value to represent as a NSObject.</param> /// <returns>A NSObject representing the given value.</returns> /// <exception cref="SystemException">When one of the objects contained in the array cannot be represented by a NSObject.</exception> public static NSArray Wrap(Object[] value) { NSArray arr = new NSArray(value.Length); for (int i = 0; i < value.Length; i++) { arr.SetValue(i, Wrap(value[i])); } return arr; }
private NSArray GetManagedUpdates(string condition = null) { var managedUpdates = new List<Models.MunkiManifestManagedUpdate>(); foreach (var templateId in _templateIds) { if (!string.IsNullOrEmpty(condition)) managedUpdates.AddRange( BLL.MunkiManagedUpdate.GetAllManagedUpdatesForMt(templateId) .Where(x => x.Condition == condition)); else { managedUpdates.AddRange(BLL.MunkiManagedUpdate.GetAllManagedUpdatesForMt(templateId) .Where(x => string.IsNullOrEmpty(x.Condition))); } } var orderedManagedUpdates = managedUpdates.GroupBy(x => x.Name).Select(g => g.First()).OrderBy(x => x.Name); NSArray plManagedUpdates = new NSArray(orderedManagedUpdates.Count()); var counter = 0; foreach (var managedUninstall in orderedManagedUpdates) { plManagedUpdates.SetValue(counter, managedUninstall.Name); counter++; } return plManagedUpdates; }
private NSArray GetOptionlInstalls(string condition = null) { var optionalInstalls = new List<Models.MunkiManifestOptionInstall>(); foreach (var templateId in _templateIds) { if (!string.IsNullOrEmpty(condition)) optionalInstalls.AddRange( BLL.MunkiOptionalInstall.GetAllOptionalInstallsForMt(templateId) .Where(x => x.Condition == condition)); else { optionalInstalls.AddRange(BLL.MunkiOptionalInstall.GetAllOptionalInstallsForMt(templateId) .Where(x => string.IsNullOrEmpty(x.Condition))); } } var orderedOptionalInstalls = optionalInstalls.GroupBy(x => x.Name).Select(g => g.OrderByDescending(x => x.Version).First()).OrderBy(x => x.Name); NSArray plOptionalInstalls = new NSArray(orderedOptionalInstalls.Count()); var counter = 0; foreach (var optionalInstall in orderedOptionalInstalls) { if (optionalInstall.IncludeVersion == 1) plOptionalInstalls.SetValue(counter, optionalInstall.Name + "-" + optionalInstall.Version); else { plOptionalInstalls.SetValue(counter, optionalInstall.Name); } counter++; } return plOptionalInstalls; }
private NSArray GetIncludedManifests(string condition = null) { var includedManifests = new List<Models.MunkiManifestIncludedManifest>(); foreach (var templateId in _templateIds) { if(!string.IsNullOrEmpty(condition)) includedManifests.AddRange(BLL.MunkiIncludedManifest.GetAllIncludedManifestsForMt(templateId) .Where(x => x.Condition == condition)); else { includedManifests.AddRange(BLL.MunkiIncludedManifest.GetAllIncludedManifestsForMt(templateId) .Where(x => string.IsNullOrEmpty(x.Condition))); } } var orderedManifests = includedManifests.GroupBy(x => x.Name).Select(s => s.First()).OrderBy(x => x.Name); NSArray plIncludedManifests = new NSArray(orderedManifests.Count()); var counter = 0; foreach (var includedManifest in orderedManifests) { plIncludedManifests.SetValue(counter, includedManifest.Name); counter++; } return plIncludedManifests; }
private NSArray GetConditionals() { var uniqueConditions = GetAllUniqueConditions(); NSArray conditionalItems = new NSArray(uniqueConditions.Count); var uniqueConditionsCounter = 0; foreach (var uniqueCondition in uniqueConditions) { NSDictionary condition = new NSDictionary(); condition.Add("condition", uniqueCondition); conditionalItems.SetValue(uniqueConditionsCounter, condition); uniqueConditionsCounter++; NSArray plIncludedManifests = GetIncludedManifests(uniqueCondition); NSArray plManagedInstalls = GetManagedInstalls(uniqueCondition); NSArray plManagedUninstalls = GetManagedUninstalls(uniqueCondition); NSArray plManagedUpdates = GetManagedUpdates(uniqueCondition); NSArray plOptionalInstalls = GetOptionlInstalls(uniqueCondition); if (plIncludedManifests.Count > 0) condition.Add("included_manifests", plIncludedManifests); if (plManagedInstalls.Count > 0) condition.Add("managed_installs", plManagedInstalls); if (plManagedUninstalls.Count > 0) condition.Add("managed_uninstalls", plManagedUninstalls); if (plManagedUpdates.Count > 0) condition.Add("managed_updates", plManagedUpdates); if (plOptionalInstalls.Count > 0) condition.Add("optional_installs", plOptionalInstalls); } return conditionalItems; }
private NSArray GetCatalogs() { var catalogs = new List<Models.MunkiManifestCatalog>(); foreach (var templateId in _templateIds) { catalogs.AddRange(BLL.MunkiCatalog.GetAllCatalogsForMt(templateId)); } var orderedCatalogs = catalogs.Distinct().OrderBy(x => x.Priority).ThenBy(x => x.Name).ToList(); orderedCatalogs = orderedCatalogs.GroupBy(x => x.Name).Select(s => s.First()).ToList(); NSArray plCatalogs = new NSArray(orderedCatalogs.Count); var counter = 0; foreach (var catalog in orderedCatalogs) { plCatalogs.SetValue(counter, catalog.Name); counter++; } return plCatalogs; }
/// <summary> /// Parses a node in the XML structure and returns the corresponding NSObject /// </summary> /// <returns>The corresponding NSObject.</returns> /// <param name="n">The XML node.</param> static NSObject ParseObject(XmlNode n) { if (n.Name.Equals("dict")) { NSDictionary dict = new NSDictionary(); List<XmlNode> children = FilterElementNodes(n.ChildNodes); for (int i = 0; i < children.Count; i += 2) { XmlNode key = children[i]; XmlNode val = children[i + 1]; string keyString = GetNodeTextContents(key); dict.Add(keyString, ParseObject(val)); } return dict; } if (n.Name.Equals("array")) { List<XmlNode> children = FilterElementNodes(n.ChildNodes); NSArray array = new NSArray(children.Count); for (int i = 0; i < children.Count; i++) { array.SetValue(i, ParseObject(children[i])); } return array; } if (n.Name.Equals("true")) return new NSNumber(true); if (n.Name.Equals("false")) return new NSNumber(false); if (n.Name.Equals("integer")) return new NSNumber(GetNodeTextContents(n)); if (n.Name.Equals("real")) return new NSNumber(GetNodeTextContents(n)); if (n.Name.Equals("string")) return new NSString(GetNodeTextContents(n)); if (n.Name.Equals("data")) return new NSData(GetNodeTextContents(n)); return n.Name.Equals("date") ? new NSDate(GetNodeTextContents(n)) : null; }
/// <summary> /// Parses an object inside the currently parsed binary property list. /// For the format specification check /// <a href="http://www.opensource.apple.com/source/CF/CF-855.17/CFBinaryPList.c"> /// Apple's binary property list parser implementation</a>. /// </summary> /// <returns>The parsed object.</returns> /// <param name="obj">The object ID.</param> /// <exception cref="PropertyListFormatException">When the property list's format could not be parsed.</exception> NSObject ParseObject(int obj) { int offset = offsetTable[obj]; byte type = bytes[offset]; int objType = (type & 0xF0) >> 4; //First 4 bits int objInfo = (type & 0x0F); //Second 4 bits switch (objType) { case 0x0: { //Simple switch (objInfo) { case 0x0: { //null object (v1.0 and later) return null; } case 0x8: { //false return new NSNumber(false); } case 0x9: { //true return new NSNumber(true); } case 0xC: { //URL with no base URL (v1.0 and later) //TODO Implement binary URL parsing (not yet even implemented in Core Foundation as of revision 855.17) break; } case 0xD: { //URL with base URL (v1.0 and later) //TODO Implement binary URL parsing (not yet even implemented in Core Foundation as of revision 855.17) break; } case 0xE: { //16-byte UUID (v1.0 and later) //TODO Implement binary UUID parsing (not yet even implemented in Core Foundation as of revision 855.17) break; } case 0xF: { //filler byte return null; } } break; } case 0x1: { //integer int length = (int)Math.Pow(2, objInfo); return new NSNumber(CopyOfRange(bytes, offset + 1, offset + 1 + length), NSNumber.INTEGER); } case 0x2: { //real int length = (int)Math.Pow(2, objInfo); return new NSNumber(CopyOfRange(bytes, offset + 1, offset + 1 + length), NSNumber.REAL); } case 0x3: { //Date if (objInfo != 0x3) { throw new PropertyListFormatException("The given binary property list contains a date object of an unknown type (" + objInfo + ")"); } return new NSDate(CopyOfRange(bytes, offset + 1, offset + 9)); } case 0x4: { //Data int[] lengthAndOffset = ReadLengthAndOffset(objInfo, offset); int length = lengthAndOffset[0]; int dataoffset = lengthAndOffset[1]; return new NSData(CopyOfRange(bytes, offset + dataoffset, offset + dataoffset + length)); } case 0x5: { //ASCII String int[] lengthAndOffset = ReadLengthAndOffset(objInfo, offset); int length = lengthAndOffset[0]; //Each character is 1 byte int stroffset = lengthAndOffset[1]; return new NSString(CopyOfRange(bytes, offset + stroffset, offset + stroffset + length), "ASCII"); } case 0x6: { //UTF-16-BE String int[] lengthAndOffset = ReadLengthAndOffset(objInfo, offset); int length = lengthAndOffset[0]; int stroffset = lengthAndOffset[1]; //UTF-16 characters can have variable length, but the Core Foundation reference implementation //assumes 2 byte characters, thus only covering the Basic Multilingual Plane length *= 2; return new NSString(CopyOfRange(bytes, offset + stroffset, offset + stroffset + length), "UTF-16BE"); } case 0x7: { //UTF-8 string (v1.0 and later) int[] lengthAndOffset = ReadLengthAndOffset(objInfo, offset); int strOffset = lengthAndOffset[1]; int characters = lengthAndOffset[0]; //UTF-8 characters can have variable length, so we need to calculate the byte length dynamically //by reading the UTF-8 characters one by one int length = CalculateUtf8StringLength(bytes, offset + strOffset, characters); return new NSString(CopyOfRange(bytes, offset + strOffset, offset + strOffset + length), "UTF-8"); } case 0x8: { //UID (v1.0 and later) int length = objInfo + 1; return new UID(obj.ToString(), CopyOfRange(bytes, offset + 1, offset + 1 + length)); } case 0xA: { //Array int[] lengthAndOffset = ReadLengthAndOffset(objInfo, offset); int length = lengthAndOffset[0]; int arrayOffset = lengthAndOffset[1]; NSArray array = new NSArray(length); for (int i = 0; i < length; i++) { int objRef = (int)ParseUnsignedInt(CopyOfRange(bytes, offset + arrayOffset + i * objectRefSize, offset + arrayOffset + (i + 1) * objectRefSize)); array.SetValue(i, ParseObject(objRef)); } return array; } case 0xB: { //Ordered set (v1.0 and later) int[] lengthAndOffset = ReadLengthAndOffset(objInfo, offset); int length = lengthAndOffset[0]; int contentOffset = lengthAndOffset[1]; NSSet set = new NSSet(true); for (int i = 0; i < length; i++) { int objRef = (int)ParseUnsignedInt(CopyOfRange(bytes, offset + contentOffset + i * objectRefSize, offset + contentOffset + (i + 1) * objectRefSize)); set.AddObject(ParseObject(objRef)); } return set; } case 0xC: { //Set (v1.0 and later) int[] lengthAndOffset = ReadLengthAndOffset(objInfo, offset); int length = lengthAndOffset[0]; int contentOffset = lengthAndOffset[1]; NSSet set = new NSSet(); for (int i = 0; i < length; i++) { int objRef = (int)ParseUnsignedInt(CopyOfRange(bytes, offset + contentOffset + i * objectRefSize, offset + contentOffset + (i + 1) * objectRefSize)); set.AddObject(ParseObject(objRef)); } return set; } case 0xD: { //Dictionary int[] lengthAndOffset = ReadLengthAndOffset(objInfo, offset); int length = lengthAndOffset[0]; int contentOffset = lengthAndOffset[1]; //System.out.println("Parsing dictionary #"+obj); NSDictionary dict = new NSDictionary(); for (int i = 0; i < length; i++) { int keyRef = (int)ParseUnsignedInt(CopyOfRange(bytes, offset + contentOffset + i * objectRefSize, offset + contentOffset + (i + 1) * objectRefSize)); int valRef = (int)ParseUnsignedInt(CopyOfRange(bytes, offset + contentOffset + (length * objectRefSize) + i * objectRefSize, offset + contentOffset + (length * objectRefSize) + (i + 1) * objectRefSize)); NSObject key = ParseObject(keyRef); NSObject val = ParseObject(valRef); dict.Add(key.ToString(), val); } return dict; } default: { Console.WriteLine("WARNING: The given binary property list contains an object of unknown type (" + objType + ")"); break; } } return null; }