在网络游戏的入口,每次启动时都需要检测服务器是否有更新。
我们每次打AB包时,都会在Resource文件夹内生成ABMD5.bytes文件,记录最新的assetbundle文件的md5信息。这个ABMD5 .bytes文件还会根据版本和平台 拷贝到工程文件夹下的Version文件夹内。
我们打热更包时,就需要指定版本的ABMD5 .bytes文件,从而将筛选打出的热更包放进工程文件夹下的Hot文件夹内。注意每次打热更包时,我们都会先打一次AB包,但是Resource文件夹内的ABMD5.bytes文件和Version文件夹内的ABMD5不会更新 ,它们只有正常打AB包和发布标准包时才会更新。这也意味着我们打出来的热更包都是基于当前版本的,客户端只需要下载最新的热更包即可。
当我们发布标准包时,我们在Resources文件夹内还会添加一个Version.txt文件。这个文件就是客户端的版本信息,客户端通过这个文件来读取自己的版本信息来和服务器进行比对。
HotPatchManager 我们在ResFrame——Frames——ResourceFrm——Download文件夹下新建HotPatchManager
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.Networking;using System;using System.Linq;using System.IO;public class HotPatchManager : SingletonPattern <HotPatchManager >{ public int LoadFileCount { get ; set ; } = 0 ; public float LoadSumSize { get ; set ; } = 0 ; private MonoBehaviour m_Mono; private string m_DownLoadPath = Application.persistentDataPath + "/DownLoad" ; private string m_CurVersion; public string m_CurPackName; private string m_ServerInfoPath = Application.persistentDataPath + "/ServerInfo.xml" ; private string m_LocalInfoPath = Application.persistentDataPath + "/LocalInfo.xml" ; private ServerInfo m_ServerInfo; private ServerInfo m_LocalInfo; private VersionInfo m_GameVersion; public string CurVersion => m_CurVersion; private Patches m_CurrentPatches; public Patches CurrentPatches => m_CurrentPatches; private Dictionary<string ,Patch> m_HotFixDic = new Dictionary<string ,Patch>(); private List<Patch> m_DownloadList = new List<Patch>(); private Dictionary<string ,Patch> m_DownloadDic = new Dictionary<string ,Patch>(); public event Action ServerInfoError; public void Init (MonoBehaviour mono ) { m_Mono = mono; } public void CheckVersion (Action<bool > hotCallback = null ) { m_HotFixDic.Clear(); ReadVersion(); m_Mono.StartCoroutine(GetServerInfo(() => { if (m_ServerInfo == null ) { if (ServerInfoError != null ) { ServerInfoError.Invoke(); } return ; } foreach (VersionInfo versionInfo in m_ServerInfo.GameVersion) { if (versionInfo.Version == m_CurVersion) { m_GameVersion = versionInfo; break ; } } GetHotAB(); if (CheckLocalAndServerPatch()) { ComputeDownload(); if (File.Exists(m_ServerInfoPath)) { if (File.Exists(m_LocalInfoPath)) { File.Delete(m_LocalInfoPath); } File.Move(m_ServerInfoPath, m_LocalInfoPath); } } else { CheckLocalResource(); } LoadFileCount = m_DownloadList.Count; LoadSumSize = m_DownloadList.Sum(x => x.Size); hotCallback?.Invoke(m_DownloadList.Count > 0 ); })); } void CheckLocalResource () { m_DownloadList.Clear(); m_DownloadDic.Clear(); if (m_GameVersion != null && m_GameVersion.Patches != null && m_GameVersion.Patches.Length > 0 ) { m_CurrentPatches = m_GameVersion.Patches[m_GameVersion.Patches.Length - 1 ]; if (m_CurrentPatches.Files != null && m_CurrentPatches.Files.Count > 0 ) { foreach (Patch patch in m_CurrentPatches.Files) { if ((Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor) && patch.Platform.Contains("StandaloneWindows64" )) { AddDownloadList(patch); } else if ((Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.WindowsEditor) && patch.Platform.Contains("Android" )) { AddDownloadList(patch); } else if ((Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.WindowsEditor) && patch.Platform.Contains("IOS" )) { AddDownloadList(patch); } } } } } bool CheckLocalAndServerPatch () { if (!File.Exists(m_LocalInfoPath)) { return true ; } m_LocalInfo = BinarySerializeOpt.XmlDeserialize<ServerInfo>(m_LocalInfoPath); VersionInfo localGameVersion = null ; if (m_LocalInfo != null ) { foreach (VersionInfo versionInfo in m_LocalInfo.GameVersion) { if (versionInfo.Version == m_CurVersion) { localGameVersion = versionInfo; break ; } } } if (localGameVersion != null && m_GameVersion.Patches != null && localGameVersion.Patches != null && m_GameVersion.Patches.Length >0 && m_GameVersion.Patches[m_GameVersion.Patches.Length - 1 ].Version != localGameVersion.Patches[localGameVersion.Patches.Length - 1 ].Version) { return true ; } return false ; } void ReadVersion () { TextAsset versionTex = Resources.Load<TextAsset>("Version" ); if (versionTex == null ) { Debug.LogError("未读到本地版本!" ); return ; } string [] all = versionTex.text.Split('\n' ); if (all.Length > 0 ) { string [] infoList = all[0 ].Split(';' ); if (infoList.Length >= 2 ) { m_CurVersion = infoList[0 ].Split('|' )[1 ]; m_CurPackName = infoList[1 ].Split('|' )[1 ]; } } } IEnumerator GetServerInfo (Action callBack ) { string infoURL = "http://127.0.0.1/ServerInfo.xml" ; UnityWebRequest request = UnityWebRequest.Get(infoURL); request.timeout = 30 ; yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.DataProcessingError) { Debug.Log("Download Error:" + request.error); request.Dispose(); } else { FileTool.CreateFile(m_ServerInfoPath, request.downloadHandler.data); if (File.Exists(m_ServerInfoPath)) { m_ServerInfo = BinarySerializeOpt.XmlDeserialize<ServerInfo>(m_ServerInfoPath); } else { Debug.LogError("热更配置读取错误!" ); } request.Dispose(); } callBack?.Invoke(); } void GetHotAB () { if (m_GameVersion != null && m_GameVersion.Patches != null && m_GameVersion.Patches.Length > 0 ) { Patches lastPatches = m_GameVersion.Patches[m_GameVersion.Patches.Length - 1 ]; if (lastPatches != null && lastPatches.Files != null ) { foreach (Patch patch in lastPatches.Files) { m_HotFixDic.Add(patch.Name, patch); } } } } void ComputeDownload () { m_DownloadList.Clear(); m_DownloadDic.Clear(); if (m_GameVersion != null && m_GameVersion.Patches != null && m_GameVersion.Patches.Length > 0 ) { m_CurrentPatches = m_GameVersion.Patches[m_GameVersion.Patches.Length - 1 ]; if (m_CurrentPatches.Files != null && m_CurrentPatches.Files.Count > 0 ) { foreach (Patch patch in m_CurrentPatches.Files) { if ((Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor) && patch.Platform.Contains("StandaloneWindows64" )) { AddDownloadList(patch); } else if ((Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.WindowsEditor) && patch.Platform.Contains("Android" )) { AddDownloadList(patch); } else if ((Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.WindowsEditor) && patch.Platform.Contains("IOS" )) { AddDownloadList(patch); } } } } } void AddDownloadList (Patch patch ) { string filePath = m_DownLoadPath + "/" + patch.Name; if (File.Exists(filePath)) { string md5 = MD5Manager.Instance.BuildFileMD5(filePath); if (patch.MD5 != md5) { m_DownloadList.Add(patch); m_DownloadDic.Add(patch.Name, patch); } } else { m_DownloadList.Add(patch); m_DownloadDic.Add(patch.Name, patch); } } } public class FileTool { public static void CreateFile (string filePath, byte [] bytes ) { if (File.Exists(filePath)) { File.Delete(filePath); } FileInfo fileInfo = new FileInfo(filePath); FileStream fs = fileInfo.Create(); fs.Write(bytes, 0 , bytes.Length); fs.Close(); fs.Dispose(); } }
CheckVersion
是主要的入口代码,我们从这里理清楚热更流程
ReadVersion
:客户端读取在本地Resource文件夹内保存的Version版本。
开启GetServerInfo
协程:从服务器获取ServerInfo,这是确认热更的重要文件。
在GetServerInfo
的回调内:
先根据客户端当前版本获取到此版本的VersionInfo
GetHotAB
方法:获取当前版本的热更包信息,不管是否需要下载热更包都要获取此信息。用来进一步检验客户端是否正确加载了热更包。因为客户端可能非法加载已经不需要的热更包。
CheckLocalAndServerPatch
方法,检查客户端缓存的ServerInfo和服务器的ServerInfo来确定是否需要热更
ComputeDownload
,检验客户端本地的已有的包和服务器提供的Info,生成下载列表
CheckLocalResource
,检查本地资源是否与服务器下载列表信息是否一致,用于下载中断后重新开始,因为如果下载中途客户端断开,客户端本地缓存的ServerInfo是最新的,不进行二次检查的话热更就无法再下载了。
修正 将BinarySerializeOpt
脚本从ConfigFrm移到ResourcFrm文件夹内,因为我们的ResourcFrm文件夹内放有“Resource”程序集定义,而HotPatchManager
也在此程序集内,如果BinarySerializeOpt
脚本在ConfigFrm文件夹会导致无法引用。
将Script——Manager文件夹内的MD5Manager
移动到ResFrame——Frames——ResourceFrm文件夹内
在GameStart里初始化 1 2 3 4 5 6 7 8 protected override void Awake (){ base .Awake(); ObjectPoolManager.Instance.Init(recyclePoolTrans,sceneTrans); HotPatchManager.Instance.Init(this ); }