Java自動生成H5遊戲資源版文件的版本號

版本號自動化需求

H5遊戲的龐大資源,每個資源的版本號不可能是手動維護,必須採用腳本或者軟件來自動生成。具體的版本號管理的問題,可以看我上篇文章:H5手遊頁遊的資源版本管理

本文主要是用java實現了讀取所有的資源文件,並且根據文件的日期生成相應的版本號,保存在一個文件裏面,最終生成全部文件的版本號(具備默認的日期),壓縮成zip在H5遊戲中使用。

本文例子下載

Java實現思路過程

這種其實也是簡單粗暴,直接爲每個文件生成對應的時間或者svn版本號,最終生成一個大文件。不過同一個時間最多的文件,是不會記錄起來,當作默認版本號。
1. 讀取資源路徑的配置文件config.properties

path:../../assets/
output:../../assets/assets.bin

可以是相對路徑或者絕對路徑,其中output是保存的文件,assets.bin爲文本文件
這裏寫圖片描述
2. 讀取所有的文件,遍歷並且存起來,同時把每個文件的時間和次數記起來。並且把最多的時間記起來。
這裏寫圖片描述

//統計文件數量
fileCount++;
//數量+1
versionBean.count++;
if(maxCountVersion == null)
    maxCountVersion = versionBean;
//記錄最大次數的版本
if(versionBean.count > maxCountVersion.count)
    maxCountVersion = versionBean;
  1. 遍歷所有的文件,並且把文件給記錄起來(去掉默認版本號),並且生成。

  2. 把assets.bin轉換成assets.cfg(zip文件)
    一個bat腳本文件,自動執行版本程序,然後打包,並且上傳到svn。

echo delete the assets.cfg
del ..\..\assets\assets.cfg
del ..\..\assets\assets.bin
echo Update the assets.cfg
..\..\..\sofewares\svn1.8\svn.exe up  ..\..\assets
java -jar VersionBuilder.jar
cd ..
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"

最終生成的文本內容(部分):

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;

其實總的思路還是非常簡單的,後面給出完整的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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章