Java自动生成H5游戏资源版文件的版本号

来源:互联网 发布:爱奇艺会员淘宝购买 编辑:程序博客网 时间:2024/06/02 02:59

版本号自动化需求

H5游戏的庞大资源,每个资源的版本号不可能是手动维护,必须采用脚本或者软件来自动生成。具体的版本号管理的问题,可以看我上篇文章:H5手游页游的资源版本管理。

本文主要是用Java实现了读取所有的资源文件,并且根据文件的日期生成相应的版本号,保存在一个文件里面,最终生成全部文件的版本号(具备默认的日期),压缩成zip在H5游戏中使用。

本文例子下载

Java实现思路过程

这种其实也是简单粗暴,直接为每个文件生成对应的时间或者svn版本号,最终生成一个大文件。不过同一个时间最多的文件,是不会记录起来,当作默认版本号。
1. 读取资源路径的配置文件config.properties

path:../../assets/output:../../assets/assets.bin
  • 1
  • 2
  • 1
  • 2

可以是相对路径或者绝对路径,其中output是保存的文件,assets.bin为文本文件
这里写图片描述
2. 读取所有的文件,遍历并且存起来,同时把每个文件的时间和次数记起来。并且把最多的时间记起来。
这里写图片描述

//统计文件数量fileCount++;//数量+1versionBean.count++;if(maxCountVersion == null)    maxCountVersion = versionBean;//记录最大次数的版本if(versionBean.count > maxCountVersion.count)    maxCountVersion = versionBean;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  1. 遍历所有的文件,并且把文件给记录起来(去掉默认版本号),并且生成。

  2. 把assets.bin转换成assets.cfg(zip文件)
    一个bat脚本文件,自动执行版本程序,然后打包,并且上传到svn。

echo delete the assets.cfgdel ..\..\assets\assets.cfgdel ..\..\assets\assets.binecho Update the assets.cfg..\..\..\sofewares\svn1.8\svn.exe up  ..\..\assetsjava -jar VersionBuilder.jarcd ..cd ..set assetPath=%cd%echo zip the assets.bin to assets.cfg..\sofewares\7z\7za.exe a -tzip %assetPath%\assets\assets.cfg %assetPath%\assets\assets.bin..\sofewares\svn1.8\svn.exe commit assets\assets.bin -m "update assets.bin"..\sofewares\svn1.8\svn.exe commit assets\assets.cfg -m "update assets.cfg"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

最终生成的文本内容(部分):

20175177;assets.bin,20177228;assets.cfg,20177226;bless/B101/B101_b_idle_e.json,20175178;bless/B101/B101_b_idle_e.png,20175178;bless/B101/B101_b_idle_es.json,20175178;bless/B101/B101_b_idle_es.png,20175178;bless/B101/B101_b_idle_n.json,20175178;bless/B101/B101_b_idle_n.png,20175178;bless/B101/B101_b_idle_ne.json,20175178;bless/B101/B101_b_idle_ne.png,20175178;bless/B101/B101_b_idle_s.json,20175178;bless/B101/B101_b_idle_s.png,20175178;bless/B101/B101_b_run_e.json,20175178;
  • 1
  • 1

其实总的思路还是非常简单的,后面给出完整的java代码和打包好的jar以及相应的脚本。

Java实现全部代码

代码有比较详细的注释,有问题的还可以留言。这个代码是可以正常使用的。

开发工具:IntelliJ IDEA

import bean.DateVersionBean;import java.io.*;import java.util.*;/** * 资源版本管理器,用于生成游戏资源的版本信息 * Created by sodaChen on 2017/7/4. */public class VersionBuilder{    private static VersionBuilder versionBuilder;    public static void main(String[] args) throws Exception    {        versionBuilder = new VersionBuilder();        versionBuilder.start();    }    /** 属性配置 **/    private Properties properties = new Properties();    private DataOutputStream assetsOutput;    private Calendar calendar;    private HashMap<String, DateVersionBean> timeVersionMap;    private ArrayList<DateVersionBean> timeList;    private DateVersionBean maxCountVersion;    private StringBuffer assetsBuffer;    private int fileCount;    private void start() throws Exception    {        System.out.println(System.getProperty("user.dir"));        //读取配置文件config配置文件        readConfigHanle();        calendar = Calendar.getInstance();//        dateFormat =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        //读取资源目录        String assetPath = properties.getProperty("path");        timeVersionMap = new HashMap<String,DateVersionBean>();        timeList = new ArrayList<DateVersionBean>();        maxCountVersion = new DateVersionBean();        assetsBuffer = new StringBuffer();        fileCount = 0;        //资源根目录        File root = new File(assetPath);        File[] files = root.listFiles();        assetsOutput = new DataOutputStream(new FileOutputStream(properties.getProperty("output")));        long time = System.currentTimeMillis();        //找出日期最多的版本        readFilesTimeCount(files);        //把默认日期给存起来        assetsBuffer.append(maxCountVersion.lastTime).append(";");        System.out.println("遍历" + fileCount + "个文件,寻找最多日期版本费时:" + (System.currentTimeMillis() - time));        System.out.println("最多日期:" + maxCountVersion.lastTime + " 数量:" + maxCountVersion.count);        fileCount = 0;        time = System.currentTimeMillis();        //遍历所有的文件,并且把文件给记录起来        readFilesVersion(null,files);        System.out.println("记录版本费时:" + (System.currentTimeMillis() - time));        //保存        assetsOutput.writeBytes(assetsBuffer.toString());        System.out.println("版本建立完毕! 实际记录文件数量是:" + fileCount);        //自动退出        System.exit(0);    }    /**     * 检测不满足条件文件     * @param name     * @return     */    private boolean checkOut(String name)    {        if(name.indexOf(".svn") != -1)            return true;        if(name.indexOf("debug.json") != -1)            return true;        if(name.indexOf("debug") != -1)            return true;        return false;    }    private void readFilesVersion(String name,File[] files)    {        for (File file : files)        {            if(checkOut(file.getName()))                continue;            if (file.isDirectory())            {                //递归读文件夹                if(name == null)                    readFilesVersion(file.getName(),file.listFiles());                else                    readFilesVersion(name + "/" + file.getName(),file.listFiles());            }            else            {                String dateTime = getVersion(file);                //默认的不需要填                if(dateTime.equals(maxCountVersion.lastTime))                    continue;                if(name != null)                {                    //记录文件名                    assetsBuffer.append(name);                    assetsBuffer.append("/");                }                fileCount++;                assetsBuffer.append(file.getName());                //分割符                assetsBuffer.append(",");                DateVersionBean versionBean = timeVersionMap.get(dateTime);                //设置索引(目前暂时是时间)                assetsBuffer.append(versionBean.lastTime);                assetsBuffer.append(";");            }        }    }    /**     * 统计文件的时间数量,单位转换成天     * @param files     */    private void readFilesTimeCount(File[] files)    {        //这里需要作为一个key给保存起来        for (File file : files)        {            if(checkOut(file.getName()))                continue;            if (file.isDirectory())            {                //递归读文件夹                readFilesTimeCount(file.listFiles());            }            else            {                //记录文件的时间                String dateTime = getVersion(file);                DateVersionBean versionBean = timeVersionMap.get(dateTime);                if(versionBean == null)                {                    versionBean = new DateVersionBean();                    versionBean.lastTime = dateTime;                    timeList.add(versionBean);                    versionBean.index = timeList.size() - 1;                    timeVersionMap.put(dateTime,versionBean);                }                //统计文件数量                fileCount++;                //数量+1                versionBean.count++;                if(maxCountVersion == null)                    maxCountVersion = versionBean;                //记录最大次数的版本                if(versionBean.count > maxCountVersion.count)                    maxCountVersion = versionBean;            }        }    }    /**     * 获取文件的时间版本号     * @param file     * @return     */    private String getVersion(File file)    {        long time = file.lastModified();        Date date = new Date(time);        calendar.setTime(date);        StringBuffer stringBuffer = new StringBuffer();        stringBuffer.append(calendar.get(Calendar.YEAR));        stringBuffer.append(calendar.get(Calendar.MONDAY));        stringBuffer.append(calendar.get(Calendar.DAY_OF_YEAR));        return stringBuffer.toString();    }    /**     * 读取配置文件来设置属性     */    private void readConfigHanle()    {        String confPath = "config.properties";        try        {            InputStream in = new BufferedInputStream(new FileInputStream(confPath));            //加载属性列表            properties.load(in);            in.close();        } catch (Exception e)        {            System.out.println("读取配置文件错误");            e.printStackTrace();        }    }}

    最终运行结果:

    E:\workspaces\JavaProjects\ProjectTools\VersionBuilder遍历15597个文件,寻找最多日期版本费时:5048最多日期:20175177 数量:5100记录版本费时:1360版本建立完毕! 实际记录文件数量是:10497