Пример #1
0
 static void LoadResToCache()
 {
     string loon_default_font = "lfcache";
     IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication();
     if (!isoStorage.FileExists(loon_default_font))
     {
         IsolatedStorageFileStream outStream = isoStorage.CreateFile(loon_default_font);
         Stream gzip = null;
         try
         {
             gzip = new GZipInputStream(XNAConfig.LoadStream(LSystem.FRAMEWORK_IMG_NAME + "font.zip"));
             byte[] buffer = new byte[4096];
             int charsRead;
             while ((charsRead = gzip.Read(buffer, 0, buffer.Length)) != 0)
             {
                 outStream.Write(buffer, 0, charsRead);
             }
             outStream.Flush();
             if (gzip != null)
             {
                 gzip.Close();
                 gzip.Dispose();
                 gzip = null;
             }
             buffer = null;
         }
         catch (Exception)
         {
             if (outStream != null)
             {
                 outStream.Close();
                 outStream.Dispose();
                 outStream = null;
             }
             if (gzip != null)
             {
                 gzip.Close();
                 gzip.Dispose();
                 gzip = null;
             }
             IsolatedStorageFile.GetUserStoreForApplication().DeleteFile(loon_default_font);
         }
         finally
         {
             if (outStream != null)
             {
                 outStream.Close();
                 outStream = null;
             }
         }
     }
     Stream ins = null;
     try
     {
         ins = isoStorage.OpenFile(loon_default_font, FileMode.Open);
         DataInputStream resStream = new DataInputStream(ins);
         realsize = resStream.ReadUnsignedByte();
         offy = resStream.ReadByte();
         fontSpace = (realsize + offy) * 2;
         if (realsize > 0x10)
         {
             fontSpace *= 2;
         }
         int num = resStream.ReadInt();
         int num2 = resStream.ReadByte();
         byte[] bufferData = new byte[resStream.Available()];
         resStream.Read(bufferData);
         fontData = bufferData;
         if (resStream != null)
         {
             resStream.Close();
             resStream = null;
         }
     }
     catch (Exception)
     {
         fontData = null;
         if (ins != null)
         {
             ins.Close();
             ins.Dispose();
             ins = null;
         }
         LoadResToMemory();
     }
     finally
     {
         if (ins != null)
         {
             ins.Close();
             ins.Dispose();
             ins = null;
         }
     }
 }
Пример #2
0
        /// <summary>
        /// 根据TMX地图描述创建一个新层
        /// </summary>
        ///
        /// <param name="map"></param>
        /// <param name="element"></param>
        /// <exception cref="System.Exception"></exception>
        public TMXLayer(TMXTiledMap map, XMLElement element)
        {

            this.tmx = map;
            this.name = element.GetAttribute("name", "");
            this.width = element.GetIntAttribute("width", 0);
            this.height = element.GetIntAttribute("height", 0);
            this.data = new int[width, height, 3];
            this.MaxLightSize(width, height);

            // 获得当前图层属性
            XMLElement propsElement = element.GetChildrenByName("properties");

            if (propsElement != null)
            {
                props = new TMXProperty();
                List<XMLElement> properties = propsElement.List("property");
                for (int i = 0; i < properties.Count; i++)
                {
                    XMLElement propElement = properties[i];
                    string name_0 = propElement.GetAttribute("name", null);
                    string value_ren = propElement.GetAttribute("value", null);
                    props.SetProperty(name_0, value_ren);
                }
            }

            XMLElement dataNode = element.GetChildrenByName("data");
            string encoding = dataNode.GetAttribute("encoding", null);
            string compression = dataNode.GetAttribute("compression", null);

            // 进行base64的压缩解码
            if ("base64".Equals(encoding) && "gzip".Equals(compression))
            {
                try
                {

                    byte[] sdec = Base64Coder.DecodeBase64(dataNode.GetContents().Trim().ToCharArray());

                    ByteArrayInputStream mask0 = new ByteArrayInputStream(sdec);

                    GZipInputStream dis = new GZipInputStream(mask0);

                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            int tileId = 0;

                            tileId |= dis.ReadByte();
                            tileId |= dis.ReadByte() << 8;
                            tileId |= dis.ReadByte() << 16;
                            tileId |= dis.ReadByte() << 24;

                            if (tileId == 0)
                            {
                                data[x,y,0] = -1;
                                data[x,y,1] = 0;
                                data[x,y,2] = 0;
                            }
                            else
                            {

                                TMXTileSet set = map.FindTileSet(tileId);

                                if (set != null)
                                {
                               
                                    data[x,y,0] = set.index;
                                    data[x,y,1] = tileId - set.firstGID;
                                }
                                data[x,y,2] = tileId;
                            }

                        }
                    }
                }
                catch (Exception e)
                {
                    Loon.Utils.Debugging.Log.Exception(e);
                    throw new Exception("Unable to decode base64 !");
                }
            }
            else
            {
                throw new Exception("Unsupport tiled map type " + encoding
                        + "," + compression + " only gzip base64 Support !");
            }
        }