Exemple #1
0
        /// <summary>
        /// 	* Inflates either zlib or gzip deflated memory. The inflated memory is
        ///	* expected to be freed by the caller.
        ///	*
        ///	* It will allocate 256k for the destination buffer. If it is not enought it will multiply the previous buffer size per 2, until there is enough memory.
        ///	* @returns the length of the deflated buffer
        ///	*
        ///	@since v0.8.1
        /// </summary>
        /// <param name="parameterin"></param>
        /// <param name="inLength"></param>
        /// <param name="parameterout"></param>
        /// <returns></returns>
        public static int InflateMemory(byte[] parameterin, uint inLength, byte[] parameterout)
        {
            MemoryStream ms = new MemoryStream(parameterout, true);
            try
            {
                GZipStream gs = new GZipStream(new MemoryStream(parameterin, false)); // , CompressionMode.Decompress, false);
#if XBOX
                byte[] b = new byte[8096];
                while (gs.CanRead)
                {
                    int amt = gs.Read(b, 0, b.Length);
                    if (amt <= 0)
                    {
                        break;
                    }
                    ms.Write(b, 0, amt);
                }
#else
                gs.CopyTo(ms);
#endif
                return ((int)ms.Length);
            }
            catch (Exception)
            {
                // Nog a gzip stream, could be zlib stream
            }
            return (0);
        }
Exemple #2
0
        public void EndElement(object ctx, string elementName)
        {
            CCTMXMapInfo pTMXMapInfo = this;
            byte[] encoded = null;

            if (elementName == "data" && (pTMXMapInfo.LayerAttribs & (int) CCTMXLayerAttrib.Base64) != 0)
            {
                pTMXMapInfo.StoringCharacters = false;
                CCTMXLayerInfo layer = pTMXMapInfo.Layers.LastOrDefault();
                if ((pTMXMapInfo.LayerAttribs & ((int) (CCTMXLayerAttrib.Gzip) | (int) CCTMXLayerAttrib.Zlib)) != 0)
                {
                    //gzip compress
                    if ((pTMXMapInfo.LayerAttribs & (int) CCTMXLayerAttrib.Gzip) != 0)
                    {
                        try
                        {
                            GZipStream inGZipStream = new GZipStream(new MemoryStream(pTMXMapInfo.CurrentString));

                            var outMemoryStream = new MemoryStream();

                            var buffer = new byte[1024];
                            while (true)
                            {
                                int bytesRead = inGZipStream.Read(buffer, 0, buffer.Length);
                                if (bytesRead == 0)
                                    break;
                                outMemoryStream.Write(buffer, 0, bytesRead);
                            }
                            encoded = outMemoryStream.ToArray();
                        }
                        catch (Exception ex)
                        {
                            CCLog.Log("failed to decompress embedded data object in TMX file.");
                            CCLog.Log(ex.ToString());
                        }
                    }

                    //zlib
                    if ((pTMXMapInfo.LayerAttribs & (int) CCTMXLayerAttrib.Zlib) != 0)
                    {
                        var inZInputStream = new ZInputStream(new MemoryStream(pTMXMapInfo.CurrentString));

                        var outMemoryStream = new MemoryStream();

                        var buffer = new byte[1024];
                        while (true)
                        {
                            int bytesRead = inZInputStream.Read(buffer, 0, buffer.Length);
                            if (bytesRead == 0)
                                break;
                            outMemoryStream.Write(buffer, 0, bytesRead);
                        }

                        encoded = outMemoryStream.ToArray();
                    }
                }
                else
                {
                    encoded = pTMXMapInfo.CurrentString;
                }

                for (int i = 0; i < layer.Tiles.Length; i++)
                {
                    int i4 = i * 4;
                    var gid = (uint) (
                                         encoded[i4] |
                                         encoded[i4 + 1] << 8 |
                                         encoded[i4 + 2] << 16 |
                                         encoded[i4 + 3] << 24);

                    layer.Tiles[i] = gid;
                }

                pTMXMapInfo.CurrentString = null;
            }
            else if (elementName == "map")
            {
                // The map element has ended
                pTMXMapInfo.ParentElement = (int) CCTMXProperty.None;
            }
            else if (elementName == "layer")
            {
                // The layer element has ended
                pTMXMapInfo.ParentElement = (int) CCTMXProperty.None;
            }
            else if (elementName == "objectgroup")
            {
                // The objectgroup element has ended
                pTMXMapInfo.ParentElement = (int) CCTMXProperty.None;
            }
            else if (elementName == "object")
            {
                // The object element has ended
                pTMXMapInfo.ParentElement = (int) CCTMXProperty.None;
            }
        }