egret官方推荐 资源版本处理

一. 生成版本资源

引入 scripts/resplugin.ts

通过修改GameVersion生成不同后缀version文件。

/**
 * 示例自定义插件,您可以查阅 http://developer.egret.com/cn/github/egret-docs/Engine2D/projectConfig/cmdExtensionPlugin/index.html
 * 了解如何开发一个自定义插件
 */
const GameVersion = 1025;
const crc32 = require("./crc32");
export class ResPlugin implements plugins.Command {
   

    // 版本控制信息
    private versionConfig = {
   };
    // 需要进行版本控制的文件夹
    private versionPath = "resource/assets/";
    // 版本信息保存路径,建议放入resource包里面,因为这个文件每次都需要加载,不需要放在cdn上。
    private versionConfigPath = "resource/version.json";
    constructor() {
   
        const name = "resource/version_" + GameVersion + ".json";
        this.versionConfigPath = name;
    }

    async onFile(file: plugins.File) {
   
        //除去json可能存在的空格,如不需要,开发者可自行删除
        if (file.extname === ".json") {
   
            file.contents = new Buffer(JSON.stringify(JSON.parse(file.contents.toString())));
        }
        var path = file.origin;
        //对resource/assets下面的资源进行版本控制
        if (path.indexOf(this.versionPath) != -1 && (file.extname === ".mp3" || file.extname === ".fnt" || file.extname === ".json" || file.extname === ".png" || file.extname === ".jpg")) {
   
            path = path.replace(this.versionPath, "");
            this.versionConfig[path] = crc32(file.contents.toString());
            // 原始的文件夹+crc32码+后缀扩展名
            file.path = this.versionPath + this.versionConfig[path] + file.extname;
        }
        return file;
    }

    async onFinish(commandContext: plugins.CommandContext) {
   
        commandContext.createFile(this.versionConfigPath, new Buffer(JSON.stringify(this.versionConfig)));
    }
}

引入 scripts/crc32.js

(function () {
   
	'use strict';

	var table = [],
		poly = 0xEDB88320; // reverse polynomial

	// build the table
	function makeTable() {
   
		var c, n, k;

		for (n = 0; n < 256; n += 1) {
   
			c = n;
			for (k = 0; k < 8; k += 1) {
   
				if (c & 1) {
   
					c = poly ^ (c >>> 1);
				} else {
   
					c = c 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值