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

版本号自动化需求

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++;
//数量+1
versionBean.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.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"
   
   
  • 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
         
         
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个 Java 本的微信 H5 支付异步通知的示例代码: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; public class WeChatH5NotifyHandler { public static String handleNotify(HttpServletRequest request) { try { InputStream inputStream = request.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line); } String notifyData = sb.toString(); // 解析 XML 数据 Map<String, String> notifyMap = parseXml(notifyData); // 验证签名 if (verifySignature(notifyMap)) { // 处理支付成功的逻辑 // ... // 返回成功响应给微信服务器 return "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>"; } else { // 验证签名失败,返回失败响应给微信服务器 return "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[Signature verification failed.]]></return_msg></xml>"; } } catch (IOException e) { e.printStackTrace(); // 返回失败响应给微信服务器 return "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[IOException occurred.]]></return_msg></xml>"; } } private static Map<String, String> parseXml(String xmlData) { // 使用合适的 XML 解析库解析 XML 数据并将其转换为 Map 对象 // 这里仅作示例,假设已经解析成功并返回了 Map 对象 Map<String, String> notifyMap = new HashMap<>(); notifyMap.put("appid", "your_appid"); notifyMap.put("mch_id", "your_mch_id"); // ... return notifyMap; } private static boolean verifySignature(Map<String, String> notifyMap) { // 验证签名逻辑 // ... return true; // 假设签名验证成功 } } ``` 在上述代码,我们创建了一个名为 `WeChatH5NotifyHandler` 的类,其的 `handleNotify` 方法用于处理微信支付的异步通知。该方法接收 `HttpServletRequest` 对象作为参数,从请求获取异步通知的数据,并进行相应的处理逻辑。 在 `handleNotify` 方法,我们首先获取请求的数据,并将其解析为 Map 对象(示例使用 `parseXml` 方法模拟解析 XML 数据)。接下来,我们验证通知的签名是否正确(示例代码使用 `verifySignature` 方法模拟签名验证)。如果签名验证成功,则表示支付成功,可以进行相应的处理逻辑,并返回成功响应给微信服务器。如果签名验证失败,则返回失败响应给微信服务器。 请注意,真实的代码需要根据实际情况进行相应的处理逻辑和签名验证。具体的实现可能涉及到与微信服务器的交互、加密解密、验签等复杂操作,需要仔细阅读微信支付文档,并使用微信提供的 SDK 或工具类来简化开发。以上示例代码仅供参考,需要根据实际情况进行修改和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值