参考范例Starforce
1、进入ProcedureCheckVersion,下载资源信息,地址:https://gameframework.cn//version.txt
version.txt内容:
{"ForceGameUpdate": false,
"LatestGameVersion": "0.1.0",
"InternalGameVersion": 1,
"InternalResourceVersion": 1,
"GameUpdateUrl": "http://gameframework.cn/starforce/resources",
"VersionListLength": 11680,
"VersionListHashCode": -1817039085,
"VersionListZipLength": 4147,
"VersionListZipHashCode": -1778538039,
"END_OF_JSON": ""}
2、比较版本号
与本地ReadWritePath的version.dat比较InternalResourceVersion,判断是否需要更新
3、需要更新,下载资源信息文件version.Crc32校验码.dat
根据VersionListHashCode算出Crc32校验码,得到完整资源信息文件下载地址:GameUpdateUrl + version.Crc32校验码.dat
下载资源信息文件成功后,比较version.txt中的VersionListLength、VersionListZipLength、VersionListZipHashCode,校验资源信息文件正确性,已经判断了解压前的VersionListZipHashCode、VersionListZipLength,和解压后VersionListLength,所以就不判断VersionListHashCode。
4、资源信息文件校验正确,进入ProcedureUpdateResource流程、CheckResources
其中
ParseVersionList:解析当前最新的资源信息文件version.dat
ParseReadOnlyList:解析Packed部分的资源信息文件list.dat
ParseReadWriteList: 解析本地生成的热更资源信息文件list.dat
RefreshCheckInfoStatus:
比较version.dat和Packed的list.dat,比较version.dat和热更的list.dat,设置已有资源的信息,下载需要更新的资源
更新本地的热更资源信息文件list.dat
5、判断是否需要下载,ResourceNeedUpdate
添加到m_UpdateCandidateInfo
6、下载完成后,校验文件正确性,比较文件长度和hashcode
其中需要注意
LoadFromMemoryXXX加密解密过程
加密
原始数据byte 通过Crc32 生成hashcode
int hashCode = Utility.Verifier.GetCrc32(bytes);
生成hashcode对应的hashbyte
byte[] hashBytes = Utility.Converter.GetBytes(hashCode);
byte和hashbyte异或生成加密后的byte
bytes = GetQuickXorBytes(bytes, hashBytes);
解密
用加密时生成的hashcode生成对应的hashbyte
Utility.Converter.GetBytes(updateInfo.HashCode, m_CachedHashBytes);
下载数据fileStream与hashbyte异或生成解密后byte
解密后的byte 通过Crc32 生成hashcode
hashCode = Utility.Verifier.GetCrc32(fileStream, m_CachedHashBytes, Utility.Encryption.QuickEncryptLength);
比较解密后的hashcode与加密时的hashcode是否一致
注意GetCrc32(Stream stream, byte[] code, int length)这个方法,内部包含了异或计算和Crc32
校验文件的一致性,因为Crc32产生的hashcode不能保证唯一性,所以这里同时判断文件长度和hashcode