DownloadMgr.cs 26 KB

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