void ParseVersion2(PlistDictionary animations) { CCSpriteFrameCache frameCache = CCSpriteFrameCache.SharedSpriteFrameCache; foreach (var pElement in animations) { string name = pElement.Key; PlistDictionary animationDict = pElement.Value.AsDictionary; int loops = animationDict["loops"].AsInt; bool restoreOriginalFrame = animationDict["restoreOriginalFrame"].AsBool; PlistArray frameArray = animationDict["frames"].AsArray; if (frameArray == null) { CCLog.Log( "CocosSharp: CCAnimationCache: Animation '{0}' found in dictionary without any frames - cannot add to animation cache.", name); continue; } // Array of AnimationFrames var array = new List <CCAnimationFrame>(frameArray.Count); foreach (PlistObjectBase pObj in frameArray) { PlistDictionary entry = pObj.AsDictionary; string spriteFrameName = entry["spriteframe"].AsString; CCSpriteFrame spriteFrame = frameCache[spriteFrameName]; if (spriteFrame == null) { CCLog.Log( "cocos2d: CCAnimationCache: Animation '{0}' refers to frame '{1}' which is not currently in the CCSpriteFrameCache. This frame will not be added to the animation.", name, spriteFrameName); continue; } float delayUnits = entry["delayUnits"].AsFloat; PlistDictionary userInfo = entry["notification"].AsDictionary; var animFrame = new CCAnimationFrame(spriteFrame, delayUnits, userInfo); array.Add(animFrame); } float delayPerUnit = animationDict["delayPerUnit"].AsFloat; var animation = new CCAnimation(array, delayPerUnit, (uint)loops); animation.RestoreOriginalFrame = restoreOriginalFrame; this.AddAnimation(animation, name); } }
void ParseVersion1(PlistDictionary animations) { CCSpriteFrameCache frameCache = CCSpriteFrameCache.SharedSpriteFrameCache; foreach (var pElement in animations) { PlistDictionary animationDict = pElement.Value.AsDictionary; PlistArray frameNames = animationDict["frames"].AsArray; float delay = animationDict["delay"].AsFloat; if (frameNames == null) { CCLog.Log( "CocosSharp: CCAnimationCache: Animation '{0}' found in dictionary without any frames - cannot add to animation cache.", pElement.Key); continue; } var frames = new List <CCAnimationFrame>(frameNames.Count); foreach (PlistObjectBase pObj in frameNames) { string frameName = pObj.AsString; CCSpriteFrame spriteFrame = frameCache[frameName]; if (spriteFrame == null) { CCLog.Log( "cocos2d: CCAnimationCache: Animation '{0}' refers to frame '%s' which is not currently in the CCSpriteFrameCache. This frame will not be added to the animation.", pElement.Key, frameName); continue; } var animFrame = new CCAnimationFrame(spriteFrame, 1, null); frames.Add(animFrame); } if (frames.Count == 0) { CCLog.Log( "CocosSharp: CCAnimationCache: None of the frames for animation '{0}' were found in the CCSpriteFrameCache. Animation is not being added to the Animation Cache.", pElement.Key); continue; } else if (frames.Count != frameNames.Count) { CCLog.Log( "CocosSharp: CCAnimationCache: An animation in your dictionary refers to a frame which is not in the CCSpriteFrameCache. Some or all of the frames for the animation '{0}' may be missing.", pElement.Key); } CCAnimation animation = new CCAnimation(frames, delay, 1); this.AddAnimation(animation, pElement.Key); } }
protected PlistObjectBase ReadValue(ContentReader input) { var type = (ValueType)input.ReadByte(); switch (type) { case ValueType.Array: var count = input.ReadInt32(); var array = new PlistArray(count); for (int i = 0; i < count; i++) { array.Add(ReadValue(input)); } return(array); case ValueType.Bool: return(new PlistBoolean(input.ReadBoolean())); case ValueType.Data: count = input.ReadInt32(); return(new PlistData(input.ReadBytes(count))); case ValueType.Date: return(new PlistDate(input.ReadObject <DateTime>())); case ValueType.Dictionary: count = input.ReadInt32(); var dict = new PlistDictionary(); for (int i = 0; i < count; i++) { string key = stringPool[input.ReadInt32()]; dict.Add(key, ReadValue(input)); } return(dict); case ValueType.Integer: return(new PlistInteger(input.ReadInt32())); case ValueType.Null: return(new PlistNull()); case ValueType.Real: return(new PlistReal(input.ReadSingle())); case ValueType.String: return(new PlistString(stringPool[input.ReadInt32()])); default: throw new InvalidOperationException(); } }
internal void AddAnimations(PlistDictionary animationDict) { PlistDictionary animations = animationDict["animations"].AsDictionary; if (animations == null) { CCLog.Log("CocosSharp: CCAnimationCache: No animations were found in provided dictionary."); return; } PlistDictionary properties = animationDict["properties"].AsDictionary; if (properties != null) { int version = properties["format"].AsInt; PlistArray spritesheets = properties["spritesheets"].AsArray; foreach (PlistObjectBase pObj in spritesheets) { string name = pObj.AsString; CCSpriteFrameCache.SharedSpriteFrameCache.AddSpriteFrames(name); } switch (version) { case 1: ParseVersion1(animations); break; case 2: ParseVersion2(animations); break; default: Debug.Assert(false, "Invalid animation format"); break; } } }
PlistArray ParseBinaryArray(int objRef) { var buffer = new PlistArray(); List <int> refs = new List <int>(); int refCount = 0; byte arrayByte = objectTable[offsetTable[objRef]]; int refStartPosition; refCount = GetCount(offsetTable[objRef], out refStartPosition); if (refCount < 15) { refStartPosition = offsetTable[objRef] + 1; } else { //The following integer has a header as well so we increase the refStartPosition by two to account for that. refStartPosition = offsetTable[objRef] + 2 + RegulateNullBytes(BitConverter.GetBytes(refCount), 1).Length; } for (int i = refStartPosition; i < refStartPosition + refCount * objRefSize; i += objRefSize) { byte[] refBuffer = objectTable.GetRange(i, objRefSize).ToArray(); Array.Reverse(refBuffer); refs.Add(BitConverter.ToInt32(RegulateNullBytes(refBuffer, 4), 0)); } for (int i = 0; i < refCount; i++) { buffer.Add(ParseBinary(refs[i])); } return(buffer); }
PlistObjectBase LoadFromNode(XmlReader reader) { Debug.Assert(reader.NodeType == XmlNodeType.Element); bool isEmpty = reader.IsEmptyElement; switch (reader.LocalName) { case "dict": var dict = new PlistDictionary(true); if (!isEmpty) { if (reader.ReadToDescendant("key")) { dict = LoadDictionaryContents(reader, dict); } reader.ReadEndElement(); } return(dict); case "array": if (isEmpty) { return(new PlistArray()); } //advance to first node reader.ReadStartElement(); while (reader.Read() && reader.NodeType != XmlNodeType.Element) { ; } // HACK: plist data in iPods is not even valid in some cases! Way to go Apple! // This hack checks to see if they really meant for this array to be a dict. if (reader.LocalName == "key") { var ret = LoadDictionaryContents(reader, new PlistDictionary(true)); reader.ReadEndElement(); return(ret); } var arr = new PlistArray(); do { if (reader.NodeType == XmlNodeType.Element) { var val = LoadFromNode(reader); if (val != null) { arr.Add(val); } } } while (reader.Read() && reader.NodeType != XmlNodeType.EndElement); reader.ReadEndElement(); return(arr); case "string": return(new PlistString(reader.ReadElementContentAsString())); case "integer": return(new PlistInteger(reader.ReadElementContentAsInt())); case "real": return(new PlistReal(reader.ReadElementContentAsFloat())); case "false": reader.ReadStartElement(); if (!isEmpty) { reader.ReadEndElement(); } return(new PlistBoolean(false)); case "true": reader.ReadStartElement(); if (!isEmpty) { reader.ReadEndElement(); } return(new PlistBoolean(true)); case "data": return(new PlistData(reader.ReadElementContentAsString())); case "date": #if NETFX_CORE return(new PlistDate(DateTime.Parse(reader.ReadElementContentAsString()))); #else return(new PlistDate(reader.ReadElementContentAsDateTime())); #endif default: throw new XmlException(String.Format("Plist Node `{0}' is not supported", reader.LocalName)); } }
internal void AddSpriteFrames(PlistDictionary pobDictionary, CCTexture2D pobTexture) { /* * Supported Zwoptex Formats: * * ZWTCoordinatesFormatOptionXMLLegacy = 0, // Flash Version * ZWTCoordinatesFormatOptionXML1_0 = 1, // Desktop Version 0.0 - 0.4b * ZWTCoordinatesFormatOptionXML1_1 = 2, // Desktop Version 1.0.0 - 1.0.1 * ZWTCoordinatesFormatOptionXML1_2 = 3, // Desktop Version 1.0.2+ */ PlistDictionary metadataDict = null; if (pobDictionary.ContainsKey("metadata")) { metadataDict = pobDictionary["metadata"].AsDictionary; } PlistDictionary framesDict = null; if (pobDictionary.ContainsKey("frames")) { framesDict = pobDictionary["frames"].AsDictionary; } int format = 0; // get the format if (metadataDict != null) { format = metadataDict["format"].AsInt; } // check the format if (format < 0 || format > 3) { throw (new NotSupportedException("PList format " + format + " is not supported.")); } foreach (var pair in framesDict) { PlistDictionary frameDict = pair.Value.AsDictionary; CCSpriteFrame spriteFrame = null; if (format == 0) { float x = 0f, y = 0f, w = 0f, h = 0f; x = frameDict["x"].AsFloat; y = frameDict["y"].AsFloat; w = frameDict["width"].AsFloat; h = frameDict["height"].AsFloat; float ox = 0f, oy = 0f; ox = frameDict["offsetX"].AsFloat; oy = frameDict["offsetY"].AsFloat; int ow = 0, oh = 0; ow = frameDict["originalWidth"].AsInt; oh = frameDict["originalHeight"].AsInt; // check ow/oh if (ow == 0 || oh == 0) { CCLog.Log( "cocos2d: WARNING: originalWidth/Height not found on the CCSpriteFrame. AnchorPoint won't work as expected. Regenerate the .plist or check the 'format' metatag"); } // abs ow/oh ow = Math.Abs(ow); oh = Math.Abs(oh); // create frame spriteFrame = new CCSpriteFrame( new CCSize(ow, oh), pobTexture, new CCRect(x, y, w, h), new CCSize(ow, oh), false, new CCPoint(ox, oy) ); } else if (format == 1 || format == 2) { CCRect frame = CCRect.Parse(frameDict["frame"].AsString); bool rotated = false; // rotation if (format == 2) { if (frameDict.ContainsKey("rotated")) { rotated = frameDict["rotated"].AsBool; } } CCPoint offset = CCPoint.Parse(frameDict["offset"].AsString); CCSize sourceSize = CCSize.Parse(frameDict["sourceSize"].AsString); // create frame spriteFrame = new CCSpriteFrame( sourceSize, pobTexture, frame, sourceSize, rotated, offset ); } else if (format == 3) { // get values CCSize spriteSize = CCSize.Parse(frameDict["spriteSize"].AsString); CCPoint spriteOffset = CCPoint.Parse(frameDict["spriteOffset"].AsString); CCSize spriteSourceSize = CCSize.Parse(frameDict["spriteSourceSize"].AsString); CCRect textureRect = CCRect.Parse(frameDict["textureRect"].AsString); bool textureRotated = false; if (frameDict.ContainsKey("textureRotated")) { textureRotated = frameDict["textureRotated"].AsBool; } // get aliases PlistArray aliases = frameDict["aliases"].AsArray; string frameKey = pair.Key; foreach (PlistObjectBase item2 in aliases) { string oneAlias = item2.AsString; if (spriteFramesAliases.ContainsKey(oneAlias)) { if (spriteFramesAliases[oneAlias] != null) { CCLog.Log("CocosSharp: WARNING: an alias with name {0} already exists", oneAlias); } } if (!spriteFramesAliases.ContainsKey(oneAlias)) { spriteFramesAliases.Add(oneAlias, frameKey); } } // create frame spriteFrame = new CCSpriteFrame( spriteSourceSize, pobTexture, new CCRect(textureRect.Origin.X, textureRect.Origin.Y, spriteSize.Width, spriteSize.Height), spriteSourceSize, textureRotated, spriteOffset ); } // add sprite frame string key = pair.Key; if (!AllowFrameOverwrite && spriteFrames.ContainsKey(key)) { CCLog.Log("Frame named " + key + " already exists in the animation cache. Not overwriting existing record."); } else if (AllowFrameOverwrite || !spriteFrames.ContainsKey(key)) { spriteFrames[key] = spriteFrame; } } }