DownloadMgr.cs 25 KB

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