欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Cosos2d热更新Md5文件生成

程序员文章站 2022-05-07 17:40:09
关于cocos2d使用AssertManager热更新md5文件的生成引言正文(上代码)引言项目开发中难免使用到热更新,现在的cocos提供了AssertManager帮我我们快速的实现热更新功能,代码网上有很多,只要注册号各个状态的回调就能实现功能。但是不知道怎么生成自己的热更新版本文件的朋友还是有很多。所以这里提供一个模板供大家参考,我会说其实是为了拿分?因为是模板所以大家用的时候要根据自己的工程和需求进行修改。拿来主义很香但是自己还是要动动脑子的。正文(上代码)//使用pythonimpo...

关于cocos2d使用AssertManager热更新md5文件的生成

引言

项目开发中难免使用到热更新,现在的cocos提供了AssertManager帮我我们快速的实现热更新功能,代码网上有很多,只要注册号各个状态的回调就能实现功能。但是不知道怎么生成自己的热更新版本文件的朋友还是有很多。所以这里提供一个模板供大家参考,我会说其实是为了拿分?因为是模板所以大家用的时候要根据自己的工程和需求进行修改。拿来主义很香但是自己还是要动动脑子的。

正文(上代码)

//使用python
import os
import hashlib
import time
import json

def getFileMd5(filename):
    if not os.path.isfile(filename):
        return
 
    f = open(filename,'rb')
    b = f.read()#get file content.
    if not b :
      return
    myhash = hashlib.md5()#create a md5 object  
    myhash.update(b)#encrypt the file
    f.close()
    return myhash.hexdigest()
#read version
def ReadVersion():
    f = open(os.getcwd() +"\\versions.json",'r')
    jstr = json.loads(f.read())
    f.close()
    return  jstr
#Game Version.Mainfest
txt =''
def GameFest(path):
    global txt
    versions =  ReadVersion()
    f1 = os.listdir(path)
    for f in f1:
        if __name__ == "__main__":
            timeStr = time.strftime("%Y%m%d%H%M%S",time.localtime(time.time()))
            if not os.path.exists(os.getcwd() + '\\manifest\\'+f):
                os.mkdir(os.getcwd() + '\\manifest\\'+f)
        f2 = file("manifest\\" +f+ "\\project.manifest","w+")
        ver = versions[f]
        txt = '{\
        \n\t"packageUrl" : "ip:port/version/",\
        \n\t"remoteVersionUrl" : "ip:port/asset/%s/version.manifest",\
        \n\t"remoteManifestUrl" : "ip:port/asset/%s/project.manifest",\
        \n\t"version" : "%s",\
        \n\t"engineVersion" : "Cocos2d-x v3.10",\
        \n\n\t"assets" : {'%(f,f,ver)
        FillGameFest(os.path.join(path,f),'res/games/'+f)
        txt = txt[:-2]
        txt += '\n\t},\
         \n\t"searchPaths" : [\
         \n\t]\
         \n}'
        f2.write(txt)
        txt2 = '{\
        \n\t"packageUrl" : "ip:port/version/",\
        \n\t"remoteVersionUrl" : "ip:port/asset/%s/version.manifest",\
        \n\t"remoteManifestUrl" : "ip:port/asset/%s/project.manifest",\
        \n\t"version" : "%s",\
        \n\t"engineVersion" : "Cocos2d-x v3.10"\n}'% (f,f,ver)
        f3 = file("manifest\\"+ f+ "\\version.manifest", "w+")
        f3.write(txt2)
        
#fill Game Version
def FillGameFest(path,prefix):
    global txt
    f1 = os.listdir(path)
    for f in f1:
        if os.path.isdir(os.path.join(path,f)):# if is a dir
            if prefix == '':
                FillGameFest(os.path.join(path,f), f)
            else:
                FillGameFest(os.path.join(path,f), prefix + '/' + f)
        else:
            md5 = getFileMd5(os.path.join(path,f))
            txt += "\n\t\t\"%s\" : {\n\t\t\t\"md5\" : \"%s\"\n\t\t}, " % (prefix + '/' + f, md5)
        
def walk(path, prefix):
    global xml
    fl = os.listdir(path) # get what we have in the dir.
    for f in fl:
       p = prefix + "/" + f
       if p == 'res/games':
            GameFest(os.path.join(path,f))
       else:     
           if os.path.isdir(os.path.join(path,f)): # if is a dir.
               if prefix == '':
                    walk(os.path.join(path,f), f)
               else:
                    walk(os.path.join(path,f), prefix + '/' + f)
           else:
               md5 = getFileMd5(os.path.join(path,f))
               xml += "\n\t\t\"%s\" : {\n\t\t\t\"md5\" : \"%s\"\n\t\t}, " % (prefix + '/' + f,md5)
if __name__ == "__main__":
    timeStr = time.strftime("%Y%m%d%H%M%S",time.localtime(time.time()))
    if not os.path.exists(os.getcwd() + '\\manifest'):
        os.mkdir(os.getcwd() + '\\manifest')
 #generate project.manifest
vers = ReadVersion()
mver = vers["main"]
xml = '{\
 \n\t"packageUrl" : "ip:port/version/",\
 \n\t"remoteVersionUrl" : "ip:port/asset/version.manifest",\
 \n\t"remoteManifestUrl" : "ip:port/asset/project.manifest",\
 \n\t"version" : "%s",\
 \n\t"engineVersion" : "Cocos2d-x v3.10",\
 \n\n\t"assets" : {' % mver
walk(os.getcwd() + '\\res', 'res')
walk(os.getcwd() + '\\src', 'src')
xml = xml[:-2]
xml += '\n\t},\
 \n\t"searchPaths" : [\
 \n\t]\
 \n}'
f = file("manifest\project.manifest", "w+")
f.write(xml)
print 'generate version.manifest finish.'
#generate version.manifest
xml = '{\
 \n\t"packageUrl" : "ip:port/version/",\
 \n\t"remoteVersionUrl" : "ip:port/asset/version.manifest",\
 \n\t"remoteManifestUrl" : "ip:port/asset/project.manifest",\
 \n\t"version" : "0.0.%s",\
 \n\t"engineVersion" : "Cocos2d-x v3.10"\n}' % timeStr
f = file("manifest\\version.manifest", "w+")
f.write(xml)     
print 'generate version.manifest finish.'       

#coding=UTF-8

//版本控制文件
{
"main":"0.0.1"
}

本文地址:https://blog.csdn.net/aishenqiu/article/details/107135577