cocos2d动画工具:-hd.plist 生成 plist

来源:互联网 发布:linux piwik安装详解 编辑:程序博客网 时间:2024/04/27 11:39

直接上代码:

[java] view plaincopy
  1. /** 
  2.  * Java 正则表达式驱动,用这种,独立于网络~ 
  3.  * @param input     旧数据读入文件 
  4.  * @param output    新数据写出文件 
  5.  */  
  6. public static void divideLogic(File input, File output) {  
  7.     String raw = StringFileBridge.file2String(input, "UTF-8");  
  8.       
  9.     Pattern pattern = Pattern.compile("\\{\\s*\\d+\\s*,\\s{0,}\\d+\\s{0,}\\}");  
  10.     Matcher matcher = pattern.matcher(raw);  
  11.       
  12.     StringBuffer sb = new StringBuffer();  
  13.     while (matcher.find()) {  
  14.         String trimed = matcher.group().replaceAll("[\\s*|\\{|\\}]""");  
  15.         String[] strs = trimed.split(",");  
  16.         String s1, s2, handledItem;  
  17.         s1 = String.valueOf(Integer.parseInt(strs[0]) / 2);  
  18.         s2 = String.valueOf(Integer.parseInt(strs[1]) / 2);  
  19.         handledItem = "{" + s1 + "," + s2 + "}";  
  20.         matcher.appendReplacement(sb, handledItem);  
  21.     }  
  22.     matcher.appendTail(sb);  
  23.     StringFileBridge.string2File(sb.toString(), output);  
  24. }  

StringFileBridge.java

[java] view plaincopy
  1. package org.bruce.image.handler.divider;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.BufferedWriter;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileWriter;  
  8. import java.io.IOException;  
  9. import java.io.InputStream;  
  10. import java.io.InputStreamReader;  
  11. import java.io.LineNumberReader;  
  12. import java.io.Reader;  
  13. import java.io.StringReader;  
  14. import java.io.UnsupportedEncodingException;  
  15.   
  16. /** 
  17.  * 字符串与文件相互转换工具 
  18.  * @author leizhimin 2009-7-14 15:54:18 
  19.  */  
  20. public class StringFileBridge {  
  21.     /** 
  22.      * 读取文件为一个内存字符串,保持文件原有的换行格式 
  23.      * @param file 文件对象 
  24.      * @param charset 文件字符集编码 
  25.      * @return 文件内容的字符串 
  26.      */  
  27.     public static String file2String(File file, String charset) {  
  28.         StringBuffer sb = new StringBuffer();  
  29.         try {  
  30.             FileInputStream fis = new FileInputStream(file);  
  31.             InputStreamReader isr = new InputStreamReader(fis, charset);  
  32.             BufferedReader br = new BufferedReader(isr);  
  33.             LineNumberReader reader = new LineNumberReader(br);  
  34.               
  35.             String line;  
  36.             while ((line = reader.readLine()) != null) {  
  37.                 sb.append(line).append(System.getProperty("line.separator"));  
  38.             }  
  39.             reader.close();  
  40.         } catch (Exception e) {  
  41.             e.printStackTrace();  
  42.         }  
  43.         return sb.toString();  
  44.     }  
  45.       
  46.       
  47.     public static String changeEncode(String unicodeStr, String charset) {  
  48.         String utf8Str = null;  
  49.         try {  
  50.             utf8Str = new String(unicodeStr.getBytes(charset));  
  51.         } catch (UnsupportedEncodingException e) {  
  52.             e.printStackTrace();  
  53.         }  
  54.         return utf8Str;  
  55.     }  
  56.       
  57.       
  58.     /** 
  59.      * 将字符串存储为一个文件,当文件不存在时候,自动创建该文件,当文件已存在时候,重写文件的内容,特定情况下,还与操作系统的权限有关。 
  60.      * @param text 字符串 
  61.      * @param distFile 存储的目标文件 
  62.      * @return 当存储正确无误时返回true,否则返回false 
  63.      */  
  64.     public static boolean string2File(String text, File distFile) {  
  65.         if (!distFile.getParentFile().exists()) {  
  66.             distFile.getParentFile().mkdirs();  
  67.         }  
  68.         BufferedReader br = null;  
  69.         BufferedWriter bw = null;  
  70.         boolean flag = true;  
  71.         try {  
  72.             br = new BufferedReader(new StringReader(text));  
  73.             bw = new BufferedWriter(new FileWriter(distFile));  
  74.             char buf[] = new char[1024 * 64]; // 字符缓冲区  
  75.             int len;  
  76.             while ((len = br.read(buf)) != -1) {  
  77.                 bw.write(buf, 0, len);  
  78.             }  
  79.             bw.flush();  
  80.             br.close();  
  81.             bw.close();  
  82.         } catch (IOException e) {  
  83.             flag = false;  
  84.             e.printStackTrace();  
  85.             System.out.println("将字符串写入文件发生异常!");  
  86.         }  
  87.         return flag;  
  88.     }  
  89.       
  90.     /** 
  91.      * 文件转换为字符串 
  92.      * @param in        字节流 
  93.      * @param charset   文件的字符集 
  94.      * @return          文件内容 
  95.      */  
  96.     public static String stream2String(InputStream in, String charset) {  
  97.         StringBuffer sb = new StringBuffer();  
  98.         try {  
  99.             Reader reader = new InputStreamReader(in, charset);  
  100.             int length = 0;  
  101.             for (char[] c = new char[1024]; (length = reader.read(c)) != -1;) {  
  102.                 sb.append(c, 0, length);  
  103.             }  
  104.             reader.close();  
  105.         } catch (Exception e) {  
  106.             e.printStackTrace();  
  107.         }  
  108.         return sb.toString();  
  109.     }  
  110.       
  111.       
  112.     /** 
  113.      * @param args 
  114.      */  
  115.     public static void main(String[] args) {  
  116.         String x = file2String(new File("/Users/user/Desktop/123.java"), "GBK");  
  117.         System.out.println(x);  
  118.         boolean b = string2File(x, new File("/Users/user/Desktop/1234.java"));  
  119.         System.out.println(b);  
  120.     }  
  121. }  
程序效果:

输入文件:

blurCloud-hd.plist

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">  
  3. <plist version="1.0">  
  4. <dict>  
  5.     <key>frames</key>  
  6.     <dict>  
  7.         <key>blurCloud0.png</key>  
  8.         <dict>  
  9.             <key>frame</key>  
  10.             <string>{{0,8}, {177,92}}</string>  
  11.             <key>offset</key>  
  12.             <string>{0, 0}</string>  
  13.         </dict>  
  14.         <key>blurCloud1.png</key>  
  15.         <dict>  
  16.             <key>frame</key>  
  17.             <string>{{185,13}, {137,80}}</string>  
  18.             <key>offset</key>  
  19.             <string>{0, 0}</string>  
  20.         </dict>  
  21.         <key>blurCloud2.png</key>  
  22.         <dict>  
  23.             <key>frame</key>  
  24.             <string>{{340,1}, {172,112}}</string>  
  25.             <key>offset</key>  
  26.             <string>{0, 0}</string>  
  27.         </dict>  
  28.     </dict>  
  29.     <key>metadata</key>  
  30.     <dict>  
  31.         <key>textureFileName</key>  
  32.         <string>blurCloud-hd.pvr.ccz</string>  
  33.         <key>format</key>  
  34.         <integer>1</integer>  
  35.         <key>size</key>  
  36.         <string>{512, 313}</string>  
  37.     </dict>  
  38. </dict>  
  39. </plist>  

输出文件:

blurCloud.plist

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">  
  3. <plist version="1.0">  
  4. <dict>  
  5.     <key>frames</key>  
  6.     <dict>  
  7.         <key>blurCloud0.png</key>  
  8.         <dict>  
  9.             <key>frame</key>  
  10.             <string>{{0,4}, {88,46}}</string>  
  11.             <key>offset</key>  
  12.             <string>{0,0}</string>  
  13.         </dict>  
  14.         <key>blurCloud1.png</key>  
  15.         <dict>  
  16.             <key>frame</key>  
  17.             <string>{{92,6}, {68,40}}</string>  
  18.             <key>offset</key>  
  19.             <string>{0,0}</string>  
  20.         </dict>  
  21.         <key>blurCloud2.png</key>  
  22.         <dict>  
  23.             <key>frame</key>  
  24.             <string>{{170,0}, {86,56}}</string>  
  25.             <key>offset</key>  
  26.             <string>{0,0}</string>  
  27.         </dict>  
  28.     </dict>  
  29.     <key>metadata</key>  
  30.     <dict>  
  31.         <key>textureFileName</key>  
  32.         <string>blurCloud-hd.pvr.ccz</string>  
  33.         <key>format</key>  
  34.         <integer>1</integer>  
  35.         <key>size</key>  
  36.         <string>{256,156}</string>  
  37.     </dict>  
  38. </dict>  
  39. </plist>