DownloadMgr.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. using Game.Config;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Reflection;
  7. using System.Text;
  8. using UnityEngine;
  9. using UnityEngine.Networking;
  10. class LocalFilePathInfo
  11. {
  12. public string Path;
  13. /// <summary>
  14. /// 是否可读可写文件夹
  15. /// </summary>
  16. public bool IsLocalRW;
  17. public LocalFilePathInfo()
  18. {
  19. Path = "";
  20. IsLocalRW = false;
  21. }
  22. public LocalFilePathInfo(string path,bool rw)
  23. {
  24. Path = path;
  25. IsLocalRW = rw;
  26. }
  27. }
  28. public class DownloadMgr : Singleton<DownloadMgr>
  29. {
  30. public const int TimeOut = 60;
  31. private string downloadBaseUrl;
  32. private string downloadUrl;
  33. public string DownloadUrl { get => downloadUrl; }
  34. private int downloadTaskMaxNum;
  35. public int DownloadTaskMaxNum { get => downloadTaskMaxNum; }
  36. private string localFilePath;
  37. public string LocalFilePath { get => localFilePath; }
  38. private List<DownloadDataEntity> needDownloadList;
  39. private Dictionary<DownloadDataEntity,ulong> downloadingList;
  40. private List<DownloadDataEntity> downloadSucessList;
  41. private List<DownloadDataEntity> remoteResList;
  42. private Dictionary<string, DownloadDataEntity> localResMap;
  43. private Dictionary<string, DownloadDataEntity> remoteResMap;
  44. private bool isInited = false;
  45. private GameDataFormatInfo downloadFormatInfo;
  46. private string versionFileName;
  47. private string assetsFileName;
  48. private string downloadedAssetsFileName;
  49. private ulong downloadSize;
  50. private ulong downloadingSize;
  51. public ulong DownloadSize { get => downloadSize + GetDownloadingSize(); }
  52. private ulong totalSize;//总大小
  53. public ulong TotalSize { get => totalSize; }
  54. public bool CheckFinish { get;private set; }
  55. public bool DowmloadError { get; private set; }
  56. //private byte[] versionData;
  57. //private byte[] mainfestData;
  58. private string versionData;
  59. //private UnityWebRequest mainfestData;
  60. private int tryGetVersionCount;
  61. public bool isQiangDownload;
  62. public string ResVersion { get; private set; }
  63. public override void Init()
  64. {
  65. InitField();
  66. }
  67. private ulong GetDownloadingSize()
  68. {
  69. downloadingSize = 0;
  70. foreach (var item in downloadingList)
  71. {
  72. downloadingSize += item.Value;
  73. }
  74. return downloadingSize;
  75. }
  76. private string GetIOSDownloadPath()
  77. {
  78. return "res/IosRes/";
  79. //#if GAME_DEBUG
  80. //#else
  81. //#endif
  82. }
  83. private string GetAndroidDownloadPath()
  84. {
  85. #if GAME_ONE
  86. return "res/WDAndroidRes/";
  87. #elif GAME_DEBUG
  88. return "res/TestServerRes/";
  89. #else
  90. return "res/AndroidNewRes/";
  91. #endif
  92. }
  93. private string GetDownloadBaseUrl()
  94. {
  95. #if GAME_DEBUG
  96. return "http://110.40.223.119:88/";
  97. #else
  98. #if UNITY_EDITOR
  99. return "http://165.154.29.92:88/";
  100. #else
  101. return "http://cxzcdn.hkhappygame.com/";
  102. #endif
  103. #endif
  104. }
  105. private string GetBaseUrl()
  106. {
  107. #if GAME_DEBUG
  108. return "http://110.40.223.119:81/";
  109. #else
  110. #if UNITY_EDITOR
  111. return "http://165.154.29.92:81//";
  112. #else
  113. return "http://165.154.29.92:81//";
  114. #endif
  115. #endif
  116. }
  117. private string GetDownloadUrl()
  118. {
  119. #if UNITY_IOS
  120. return GetDownloadBaseUrl() + GetIOSDownloadPath();
  121. #else
  122. return GetDownloadBaseUrl() + GetAndroidDownloadPath();
  123. #endif
  124. }
  125. private string GetDownloadUrlFormServer(string baseUrl)
  126. {
  127. #if UNITY_IOS
  128. return baseUrl + GetIOSDownloadPath();
  129. #else
  130. return baseUrl + GetAndroidDownloadPath();
  131. #endif
  132. }
  133. private void InitField()
  134. {
  135. if (!isInited)
  136. {
  137. downloadUrl = GetDownloadUrl();
  138. downloadTaskMaxNum = 5;
  139. localFilePath = GetLocalResPath();//FileSystem.LocalDocumentPath;
  140. #if UNITY_IOS
  141. versionFileName = "afivs";
  142. assetsFileName = "afimft";
  143. #else
  144. versionFileName = "Version";
  145. assetsFileName = "mainfest";
  146. #endif
  147. downloadedAssetsFileName = "downloaded";
  148. needDownloadList = new List<DownloadDataEntity>(100);
  149. downloadSucessList = new List<DownloadDataEntity>(100);
  150. downloadingList = new Dictionary<DownloadDataEntity,ulong>(downloadTaskMaxNum);
  151. remoteResList = new List<DownloadDataEntity>(1024);
  152. localResMap = new Dictionary<string, DownloadDataEntity>(1024);
  153. remoteResMap = new Dictionary<string, DownloadDataEntity>(1024);
  154. isInited = true;
  155. CheckFinish = false;
  156. DowmloadError = false;
  157. downloadSize = 0;
  158. downloadingSize = 0;
  159. totalSize = 0;
  160. tryGetVersionCount = 0;
  161. isQiangDownload = true;
  162. ResVersion = "0.0.0.0";
  163. }
  164. }
  165. //
  166. private string GetPlatform()
  167. {
  168. #if UNITY_EDITOR
  169. return "IOS";// "PC";
  170. #else
  171. #if UNITY_IOS
  172. return "IOS";//
  173. #else
  174. return "Android";//
  175. #endif
  176. #endif
  177. }
  178. public void CheckVersion()
  179. {
  180. string language = AssetsMgr.Instance.GetReslanguage();
  181. if (!string.IsNullOrEmpty(language)&& language != "base")
  182. {
  183. versionFileName = language + versionFileName;
  184. assetsFileName = language + assetsFileName;
  185. }
  186. string baseUrl = GetBaseUrl();
  187. if (!string.IsNullOrEmpty(baseUrl) && tryGetVersionCount <= 3)
  188. {
  189. baseUrl += $"serverlist/download?platform={GetPlatform()}";
  190. AssetDownloader.Instance.DownLoadFileByCoroutine(baseUrl,webReq=>
  191. {
  192. if (webReq == null)
  193. {
  194. downloadUrl = GetDownloadUrl();
  195. StartCheck();
  196. return;
  197. }
  198. string url = webReq.downloadHandler.text;
  199. url = url.Substring(1, url.Length - 2);
  200. if (url.IndexOf("http://") < 0)
  201. {
  202. url = "http://"+ url;
  203. }
  204. Debug.Log("请求得到:" + url);
  205. downloadUrl = GetDownloadUrlFormServer(url + "/");
  206. StartCheck();
  207. });
  208. }
  209. else
  210. {
  211. downloadUrl = GetDownloadUrl();
  212. StartCheck();
  213. }
  214. }
  215. private void StartCheck()
  216. {
  217. ReadDataFromFile(versionFileName, lVdatas =>
  218. {
  219. if (lVdatas!= null)
  220. {
  221. ResVersion = Encoding.UTF8.GetString(lVdatas);
  222. }
  223. AssetDownloader.Instance.DownLoadFileByCoroutine(GetRemoteUrl(versionFileName), webRqst =>
  224. {
  225. tryGetVersionCount++;
  226. if (webRqst == null)
  227. {
  228. SetCheckFinishAddDownError();
  229. return;
  230. }
  231. byte[] data = webRqst.downloadHandler.data;
  232. string version = Encoding.UTF8.GetString(data);
  233. Debug.Log("服务器 version = " + version);
  234. versionData = version;
  235. OnloadedLocalVersion(lVdatas, version);
  236. });
  237. }, true);
  238. }
  239. public void DownLoadRemoteRes()
  240. {
  241. int size = needDownloadList.Count;
  242. int downsize = downloadSucessList.Count;
  243. downloadSize = 0;
  244. totalSize = 0;
  245. for (int i = 0; i < size; i++)
  246. {
  247. if (needDownloadList[i].FullName == "Version")
  248. {
  249. needDownloadList[i].State = DownloadTaskState.DownloadSuccess;
  250. continue;
  251. }
  252. totalSize += needDownloadList[i].Size;
  253. DownloadDataEntity entity = downloadSucessList.FindFirst(it=>it.FullName == needDownloadList[i].FullName);
  254. if (downsize > 0 && entity != null && entity.MD5 == needDownloadList[i].MD5)
  255. {
  256. needDownloadList[i].State = DownloadTaskState.DownloadSuccess;
  257. downloadSize += needDownloadList[i].Size;
  258. }
  259. else
  260. {
  261. needDownloadList[i].Callback = DownloadedCallback;
  262. needDownloadList[i].UpDateCallback = DownloadedUpdateCallback;
  263. AssetDownloader.Instance.AddDownloadTask(needDownloadList[i]);
  264. }
  265. }
  266. }
  267. public bool DownLoadFinish()
  268. {
  269. bool ret = true;
  270. int size = needDownloadList.Count;
  271. for (int i = 0; i < size; i++)
  272. {
  273. if (needDownloadList[i].State != DownloadTaskState.DownloadSuccess)
  274. {
  275. ret = false;
  276. }
  277. }
  278. return ret || DowmloadError || !AssetDownloader.Instance.HasTask();
  279. }
  280. public bool CheckNeedDownload()
  281. {
  282. return downloadSize < totalSize;
  283. }
  284. private void DownloadedUpdateCallback(DownloadTask task)
  285. {
  286. if (task == null)
  287. {
  288. // DowmloadError = true;
  289. //AssetDownloader.Instance.CancelDownLoad = true;
  290. return;
  291. }
  292. Log($"下载:{task.downloadSize}/{task.CurDownloadEntity.Size} 进度:{task.CurLoadProgress*100}%");
  293. //downloadingSize = task.downloadSize;
  294. if (!downloadingList.ContainsKey(task.CurDownloadEntity))
  295. {
  296. downloadingList.Add(task.CurDownloadEntity, task.downloadSize);
  297. }
  298. else
  299. {
  300. downloadingList[task.CurDownloadEntity] = task.downloadSize;
  301. }
  302. }
  303. public void Log(string str)
  304. {
  305. #if UNITY_EDITOR
  306. Debug.Log(str);
  307. #endif
  308. }
  309. private void DownloadedCallback(DownloadTask task)
  310. {
  311. if (task == null || task.State == DownloadTaskState.Error)
  312. {
  313. //DowmloadError = true;
  314. //AssetDownloader.Instance.CancelDownLoad = true;
  315. if (task != null &&task.CurDownloadEntity.DownloadErrCount >= 3)
  316. {
  317. task.CurDownloadEntity.Url = GetDownloadUrl() + task.CurDownloadEntity.FullName;
  318. Log("重新设置Url = " + task.CurDownloadEntity.Url);
  319. }
  320. else if (task != null && task.CurDownloadEntity.DownloadErrCount >= 10)
  321. {
  322. DowmloadError = true;
  323. }
  324. return;
  325. }
  326. DownloadDataEntity entity = downloadSucessList.FindFirst(it => it.FullName == task.CurDownloadEntity.FullName);
  327. if (entity != null)
  328. {
  329. entity.MD5 = task.CurDownloadEntity.MD5;
  330. entity.Size = task.CurDownloadEntity.Size;
  331. }
  332. else
  333. {
  334. downloadSucessList.Add(task.CurDownloadEntity);
  335. }
  336. if (downloadingList.ContainsKey(task.CurDownloadEntity))
  337. {
  338. downloadingList.Remove(task.CurDownloadEntity);
  339. }
  340. downloadingSize = 0;
  341. downloadSize += task.downloadSize;
  342. string savepath = localFilePath + downloadedAssetsFileName;
  343. CsvWriter<DownloadDataEntity> csvWriter = new CsvWriter<DownloadDataEntity>(savepath, "", downloadSucessList, downloadFormatInfo);
  344. csvWriter.Write();
  345. }
  346. private List<DownloadDataEntity> SerizlizeResList(byte[] data)
  347. {
  348. CsvReader csvReader = new CsvReader(FileHelper.GetName(assetsFileName), data);
  349. if (downloadFormatInfo == null)
  350. {
  351. downloadFormatInfo = new GameDataFormatInfo(csvReader.Fields(), csvReader.Types());
  352. }
  353. Type type = typeof(DownloadDataEntity);
  354. MethodInfo methodInfo = type.GetMethod("OnCsvLoad", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  355. methodInfo?.Invoke(null, new object[] { csvReader });
  356. List<DownloadDataEntity> resList = DownloadDataEntity.AllData();
  357. DownloadDataEntity.Clear();
  358. return resList;
  359. }
  360. private void GetRemoteResList()
  361. {
  362. string remoteversionpath = downloadUrl + assetsFileName;
  363. AssetDownloader.Instance.DownLoadFileByCoroutine(remoteversionpath, webRqst =>
  364. {
  365. if (webRqst == null)
  366. {
  367. SetCheckFinishAddDownError();
  368. return;
  369. }
  370. byte[] data = webRqst.downloadHandler.data;
  371. // mainfestData = new byte[data.Length];
  372. // data.CopyTo(mainfestData,0);
  373. //mainfestData = webRqst;
  374. List<DownloadDataEntity> reResList = SerizlizeResList(data);
  375. if (reResList != null)
  376. {
  377. OnRomoteListDownloaded(reResList);
  378. }
  379. });
  380. }
  381. private void OnRomoteListDownloaded(List<DownloadDataEntity> downloadDatas)
  382. {
  383. remoteResList.AddRange(downloadDatas);
  384. int size = downloadDatas.Count;
  385. for (int i = 0; i < size; i++)
  386. {
  387. remoteResMap.Add(downloadDatas[i].FullName, downloadDatas[i]);
  388. }
  389. //#if UNITY_IOS
  390. // ReadDataFromFile(assetsFileName, OnloadedLocalResList, false, 1);
  391. //#else
  392. // ReadDataFromFile(assetsFileName, OnloadedLocalResList);
  393. //#endif
  394. ReadDataFromFile(assetsFileName, OnloadedLocalResList);
  395. }
  396. //uint major
  397. //uint minor
  398. //uint release
  399. //uint patch
  400. private void SetCheckFinish()
  401. {
  402. versionData = null;
  403. CheckFinish = true;
  404. }
  405. private void SetCheckFinishAddDownError()
  406. {
  407. if (isQiangDownload && tryGetVersionCount <= 10)
  408. {
  409. CheckVersion();
  410. }
  411. else
  412. {
  413. CheckFinish = true;
  414. DowmloadError = true;
  415. }
  416. }
  417. private string GetLocalResPath()
  418. {
  419. string localPath = FileSystem.LocalDocumentPath;
  420. //#if UNITY_IOS
  421. // localPath = "file://" + localPath;
  422. //#endif
  423. return localPath;
  424. }
  425. private string GetStreamResPath()
  426. {
  427. string streamPath = FileSystem.LocalPackagePath;
  428. #if UNITY_IOS
  429. streamPath = "file://" + streamPath;
  430. #endif
  431. return streamPath;
  432. }
  433. private void OnloadedLocalVersion(byte[] datas, string version)
  434. {
  435. string lVersionstr = "0.0.0.0";
  436. if (datas != null)
  437. {
  438. lVersionstr = Encoding.UTF8.GetString(datas);
  439. }
  440. VersionCode lVersionCode = lVersionstr;//本地 local version
  441. VersionCode RVersionCode = version;// 远端 version
  442. string streamPath = GetStreamResPath();
  443. AssetDownloader.Instance.AssetLoadByCorutine(streamPath + versionFileName, filedatas =>
  444. {
  445. if (filedatas == null)
  446. {
  447. GetRemoteResList();
  448. Debug.Log("====== SVersionstr 为空 =====");
  449. return;
  450. }
  451. VersionCode SVersionCode;// stream data version
  452. string SVersionstr = Encoding.UTF8.GetString(filedatas);
  453. SVersionCode = SVersionstr;
  454. VersionCode localMaxCode = lVersionCode > SVersionCode ? lVersionCode : SVersionCode;
  455. ResVersion = localMaxCode;
  456. if (localMaxCode == RVersionCode)
  457. {
  458. Debug.Log($"lVersionstr = {lVersionstr} SVersionstr = {SVersionstr} ===== version = {version} 不需要更新");
  459. SetCheckFinish();
  460. return;
  461. }
  462. if (localMaxCode >= RVersionCode && SVersionCode == localMaxCode)
  463. {
  464. Debug.Log($"lVersionstr = {lVersionstr} SVersionstr = {SVersionstr} ===== version = {version} 不需要跟新 需要删除之前下载资源");
  465. versionData = null;
  466. ReadDataFromFile(downloadedAssetsFileName, dlocaldatas =>
  467. {
  468. if (dlocaldatas != null)
  469. {
  470. List<DownloadDataEntity> localdownloadedList = SerizlizeResList(dlocaldatas);
  471. if (localdownloadedList != null && localdownloadedList.Count > 0)
  472. {
  473. downloadSucessList.AddRange(localdownloadedList);
  474. }
  475. DeleteDownloadFiles();
  476. }
  477. else
  478. {
  479. SetCheckFinish();
  480. }
  481. },true);
  482. }
  483. else
  484. {
  485. Debug.Log($"lVersionstr = {lVersionstr} SVersionstr = {SVersionstr} ===== version = {version} 需要更新");
  486. GetRemoteResList();
  487. //CheckFinish = true;
  488. }
  489. });
  490. }
  491. private void OnloadedVersion(byte[] datas,string version)
  492. {
  493. if (datas == null)
  494. {
  495. GetRemoteResList();
  496. Debug.Log("====== data 为空 =====");
  497. }
  498. else
  499. {
  500. string lVersionstr = Encoding.UTF8.GetString(datas);
  501. Debug.Log($"lVersionstr = {lVersionstr} ===== version = {version} ");
  502. if (lVersionstr == version)
  503. {
  504. Debug.Log($"lVersionstr = {lVersionstr} ===== version = {version} 不需要更新");
  505. SetCheckFinish();
  506. return;
  507. }
  508. else
  509. {
  510. VersionCode lVersionCode = lVersionstr;//本地 local version
  511. VersionCode RVersionCode = version;// 远端 version
  512. if (RVersionCode < lVersionCode)
  513. {
  514. SetCheckFinish();
  515. }
  516. else if (RVersionCode.major > lVersionCode.major || (RVersionCode.major == lVersionCode.major && RVersionCode.minor > lVersionCode.minor))
  517. {
  518. AssetDownloader.Instance.AssetLoadByCorutine(GetStreamResPath() + versionFileName, filedatas =>
  519. {
  520. VersionCode SVersionCode;// stream data version
  521. string SVersionstr = Encoding.UTF8.GetString(filedatas);
  522. SVersionCode = SVersionstr;
  523. if (SVersionCode >= RVersionCode && SVersionCode >= lVersionCode)
  524. {
  525. Debug.Log($"lVersionstr = {SVersionstr} ===== version = {version} 重新安装后需要删除之前资源");
  526. versionData = null;
  527. ReadDataFromFile(downloadedAssetsFileName, dlocaldatas=>
  528. {
  529. if (dlocaldatas != null)
  530. {
  531. List<DownloadDataEntity> localdownloadedList = SerizlizeResList(dlocaldatas);
  532. if (localdownloadedList != null && localdownloadedList.Count > 0)
  533. {
  534. downloadSucessList.AddRange(localdownloadedList);
  535. }
  536. DeleteDownloadFiles();
  537. }
  538. });
  539. }
  540. else
  541. {
  542. Debug.Log($"lVersionstr = {lVersionstr} ===== version = {version} 需要更新");
  543. GetRemoteResList();
  544. //CheckFinish = true;
  545. }
  546. });
  547. }
  548. else if (RVersionCode.release > lVersionCode.release || (RVersionCode.release == lVersionCode.release && RVersionCode.patch > lVersionCode.patch))
  549. {
  550. Debug.Log($"lVersionstr = {lVersionstr} ===== version = {version} 需要更新");
  551. GetRemoteResList();
  552. }
  553. else
  554. {
  555. SetCheckFinish();
  556. }
  557. }
  558. }
  559. }
  560. private void OnloadedLocalResList(byte[] datas)
  561. {
  562. if (datas == null)
  563. {
  564. }
  565. else
  566. {
  567. List<DownloadDataEntity> localList = SerizlizeResList(datas);
  568. if (localList != null)
  569. {
  570. int size = localList.Count;
  571. for (int i = 0; i < size; i++)
  572. {
  573. localResMap.Add(localList[i].FullName, localList[i]);
  574. }
  575. }
  576. }
  577. ReadDataFromFile(downloadedAssetsFileName, OnloadedDownloadResList);
  578. }
  579. private void OnloadedDownloadResList(byte[] datas)
  580. {
  581. if (datas != null)
  582. {
  583. List<DownloadDataEntity> localdownloadedList = SerizlizeResList(datas);
  584. if (localdownloadedList != null && localdownloadedList.Count > 0)
  585. {
  586. downloadSucessList.AddRange(localdownloadedList);
  587. }
  588. }
  589. GetNeedDownloadlist();
  590. DownLoadRemoteRes();
  591. }
  592. private List<DownloadDataEntity> GetLoacalResList(string path)
  593. {
  594. List<DownloadDataEntity> resList = null;
  595. if (System.IO.File.Exists(path))
  596. {
  597. byte[] data = System.IO.File.ReadAllBytes(path);
  598. resList = SerizlizeResList(data);
  599. }
  600. return resList;
  601. }
  602. private void GetNeedDownloadlist()
  603. {
  604. string ver = "Version";
  605. if (localResMap.ContainsKey(ver) && remoteResMap.ContainsKey(ver)&& localResMap[ver].MD5 == remoteResMap[ver].MD5)
  606. {
  607. Debug.Log("版本为最新状态");
  608. SetCheckFinish();
  609. //string savepath = localFilePath + downloadedAssetsFileName;
  610. //FileHelper.DeleteFile(savepath);
  611. return;
  612. }
  613. if (localResMap == null || localResMap.Count <= 0)
  614. {
  615. foreach (var item in remoteResMap)
  616. {
  617. needDownloadList.Add(item.Value);
  618. }
  619. }
  620. else
  621. {
  622. foreach (var item in remoteResMap)
  623. {
  624. if (item.Key == ver)
  625. {
  626. continue;
  627. }
  628. if (!localResMap.ContainsKey(item.Key) || localResMap[item.Key].MD5 != item.Value.MD5)
  629. {
  630. Debug.Log($"需跟新文件:{item.Value.FullName}");
  631. needDownloadList.Add(item.Value);
  632. }
  633. }
  634. }
  635. CheckFinish = true;
  636. }
  637. private string GetRemoteUrl( string name)
  638. {
  639. return downloadUrl + name;
  640. }
  641. private LocalFilePathInfo GetLocalFilePath(string name, bool onlyLocal = false, uint streamPerPath = 0)
  642. {
  643. LocalFilePathInfo ret = new LocalFilePathInfo();
  644. string path = LocalFilePath + name;
  645. ret.IsLocalRW = true;
  646. if (!FileSystem.Exists(path))
  647. {
  648. Debug.Log($" ========= LocalFilePath Path = {path} 不存在");
  649. if (!onlyLocal)
  650. {
  651. string lpPath = GetStreamResPath();
  652. for (int i = 0; i < streamPerPath; i++)
  653. {
  654. lpPath += "../";
  655. }
  656. path = lpPath + name;
  657. }
  658. ret.IsLocalRW = false;
  659. }
  660. ret.Path = path;
  661. Debug.Log($" ========= local Path = {path} ");
  662. return ret;
  663. }
  664. public void ReadDataFromFile(string name,Action<byte[]> callback,bool onlyLocal = false,uint streamPerPath = 0)
  665. {
  666. LocalFilePathInfo dlocalPath = GetLocalFilePath(name,onlyLocal, streamPerPath);
  667. if (dlocalPath.IsLocalRW)
  668. {
  669. try
  670. {
  671. byte[] dlocaldatas = File.ReadAllBytes(dlocalPath.Path);
  672. callback?.Invoke(dlocaldatas);
  673. }
  674. catch
  675. {
  676. Debug.Log("读取失败" + dlocalPath.Path);
  677. callback?.Invoke(null);
  678. }
  679. }
  680. else if(onlyLocal)
  681. {
  682. callback?.Invoke(null);
  683. }
  684. else
  685. {
  686. AssetDownloader.Instance.AssetLoadByCorutine(dlocalPath.Path, callback);
  687. }
  688. }
  689. private IEnumerator DeleteFiles()
  690. {
  691. ;
  692. int length = downloadSucessList.Count;
  693. string savepath = null;
  694. for (int i = 0; i < length; i++)
  695. {
  696. if (downloadSucessList[i].FullName == "font.unity3d")
  697. {
  698. Debug.Log("跳过删除");
  699. continue;
  700. }
  701. savepath = localFilePath + downloadSucessList[i].FullName;
  702. Debug.Log(savepath);
  703. FileHelper.DeleteFile(savepath);
  704. yield return null;
  705. }
  706. FileHelper.DeleteFile(localFilePath + downloadedAssetsFileName);
  707. yield return null;
  708. FileHelper.DeleteFile(localFilePath + versionFileName);
  709. yield return null;
  710. FileHelper.DeleteFile(LocalFilePath + assetsFileName);
  711. yield return null;
  712. SetCheckFinish();
  713. }
  714. public void DeleteDownloadFiles()
  715. {
  716. AssetDownloader.Instance.DoByCorutine(DeleteFiles);
  717. }
  718. public void Free()
  719. {
  720. if (!DowmloadError)
  721. {
  722. if (!string.IsNullOrEmpty(versionData))
  723. {
  724. FileHelper.WirteStringToFile(localFilePath + versionFileName, versionData);
  725. ResVersion = versionData;
  726. }
  727. if (remoteResList.Count > 0)
  728. {
  729. CsvWriter<DownloadDataEntity> csvWriter = new CsvWriter<DownloadDataEntity>(LocalFilePath + assetsFileName, "", remoteResList, downloadFormatInfo);
  730. csvWriter.Write();
  731. }
  732. }
  733. needDownloadList = null;
  734. downloadSucessList = null;
  735. remoteResList = null;
  736. localResMap = null;
  737. remoteResMap = null;
  738. versionData = null;
  739. //mainfestData = null;
  740. AssetDownloader.Instance.ClearTasks();
  741. }
  742. }