DownloadMgr.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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.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 "en";
  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,false);
  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. DownloadDataEntity.OnCsvLoad(csvReader);
  360. List<DownloadDataEntity> resList = DownloadDataEntity.AllData();
  361. DownloadDataEntity.Clear();
  362. return resList;
  363. }
  364. private void GetRemoteResList()
  365. {
  366. string remoteversionpath = downloadUrl + assetsFileName;
  367. AssetDownloader.Instance.DownLoadFileByCoroutine(remoteversionpath, webRqst =>
  368. {
  369. if (webRqst == null)
  370. {
  371. SetCheckFinishAddDownError();
  372. return;
  373. }
  374. byte[] data = webRqst.downloadHandler.data;
  375. // mainfestData = new byte[data.Length];
  376. // data.CopyTo(mainfestData,0);
  377. //mainfestData = webRqst;
  378. List<DownloadDataEntity> reResList = SerizlizeResList(data);
  379. if (reResList != null)
  380. {
  381. OnRomoteListDownloaded(reResList);
  382. }
  383. });
  384. }
  385. private void OnRomoteListDownloaded(List<DownloadDataEntity> downloadDatas)
  386. {
  387. remoteResList.AddRange(downloadDatas);
  388. int size = downloadDatas.Count;
  389. for (int i = 0; i < size; i++)
  390. {
  391. remoteResMap.Add(downloadDatas[i].FullName, downloadDatas[i]);
  392. }
  393. //#if UNITY_IOS
  394. // ReadDataFromFile(assetsFileName, OnloadedLocalResList, false, 1);
  395. //#else
  396. // ReadDataFromFile(assetsFileName, OnloadedLocalResList);
  397. //#endif
  398. ReadDataFromFile(assetsFileName, OnloadedLocalResList);
  399. }
  400. //uint major
  401. //uint minor
  402. //uint release
  403. //uint patch
  404. private void SetCheckFinish()
  405. {
  406. versionData = null;
  407. CheckFinish = true;
  408. }
  409. private void SetCheckFinishAddDownError()
  410. {
  411. if (isQiangDownload && tryGetVersionCount <= 10)
  412. {
  413. CheckVersion();
  414. }
  415. else
  416. {
  417. CheckFinish = true;
  418. DowmloadError = true;
  419. }
  420. }
  421. private string GetLocalResPath()
  422. {
  423. string localPath = FileSystem.LocalDocumentPath;
  424. //#if UNITY_IOS
  425. // localPath = "file://" + localPath;
  426. //#endif
  427. return localPath;
  428. }
  429. private string GetStreamResPath()
  430. {
  431. string streamPath = FileSystem.LocalPackagePath;
  432. #if UNITY_IOS
  433. streamPath = "file://" + streamPath;
  434. #endif
  435. return streamPath;
  436. }
  437. private void OnloadedLocalVersion(byte[] datas, string version)
  438. {
  439. string lVersionstr = "0.0.0.0";
  440. if (datas != null)
  441. {
  442. lVersionstr = Encoding.UTF8.GetString(datas);
  443. }
  444. VersionCode lVersionCode = lVersionstr;//本地 local version
  445. VersionCode RVersionCode = version;// 远端 version
  446. string streamPath = GetStreamResPath();
  447. AssetDownloader.Instance.AssetLoadByCorutine(streamPath + versionFileName, filedatas =>
  448. {
  449. if (filedatas == null)
  450. {
  451. GetRemoteResList();
  452. Debug.Log("====== SVersionstr 为空 =====");
  453. return;
  454. }
  455. VersionCode SVersionCode;// stream data version
  456. string SVersionstr = Encoding.UTF8.GetString(filedatas);
  457. SVersionCode = SVersionstr;
  458. VersionCode localMaxCode = lVersionCode > SVersionCode ? lVersionCode : SVersionCode;
  459. ResVersion = localMaxCode;
  460. if (localMaxCode == RVersionCode)
  461. {
  462. Debug.Log($"lVersionstr = {lVersionstr} SVersionstr = {SVersionstr} ===== version = {version} 不需要更新");
  463. SetCheckFinish();
  464. return;
  465. }
  466. if (localMaxCode >= RVersionCode)
  467. {
  468. if (SVersionCode == localMaxCode)
  469. {
  470. Debug.Log($"lVersionstr = {lVersionstr} SVersionstr = {SVersionstr} ===== version = {version} 不需要跟新 需要删除之前下载资源");
  471. versionData = null;
  472. ReadDataFromFile(downloadedAssetsFileName, dlocaldatas =>
  473. {
  474. if (dlocaldatas != null)
  475. {
  476. List<DownloadDataEntity> localdownloadedList = SerizlizeResList(dlocaldatas);
  477. if (localdownloadedList != null && localdownloadedList.Count > 0)
  478. {
  479. downloadSucessList.AddRange(localdownloadedList);
  480. }
  481. DeleteDownloadFiles();
  482. }
  483. else
  484. {
  485. SetCheckFinish();
  486. }
  487. }, true);
  488. }
  489. else
  490. {
  491. Debug.Log($"lVersionstr = {lVersionstr} SVersionstr = {SVersionstr} ===== version = {version} 不需要更新");
  492. SetCheckFinish();
  493. }
  494. }
  495. else
  496. {
  497. Debug.Log($"lVersionstr = {lVersionstr} SVersionstr = {SVersionstr} ===== version = {version} 需要更新");
  498. GetRemoteResList();
  499. //CheckFinish = true;
  500. }
  501. });
  502. }
  503. private void OnloadedVersion(byte[] datas,string version)
  504. {
  505. if (datas == null)
  506. {
  507. GetRemoteResList();
  508. Debug.Log("====== data 为空 =====");
  509. }
  510. else
  511. {
  512. string lVersionstr = Encoding.UTF8.GetString(datas);
  513. Debug.Log($"lVersionstr = {lVersionstr} ===== version = {version} ");
  514. if (lVersionstr == version)
  515. {
  516. Debug.Log($"lVersionstr = {lVersionstr} ===== version = {version} 不需要更新");
  517. SetCheckFinish();
  518. return;
  519. }
  520. else
  521. {
  522. VersionCode lVersionCode = lVersionstr;//本地 local version
  523. VersionCode RVersionCode = version;// 远端 version
  524. if (RVersionCode < lVersionCode)
  525. {
  526. SetCheckFinish();
  527. }
  528. else if (RVersionCode.major > lVersionCode.major || (RVersionCode.major == lVersionCode.major && RVersionCode.minor > lVersionCode.minor))
  529. {
  530. AssetDownloader.Instance.AssetLoadByCorutine(GetStreamResPath() + versionFileName, filedatas =>
  531. {
  532. VersionCode SVersionCode;// stream data version
  533. string SVersionstr = Encoding.UTF8.GetString(filedatas);
  534. SVersionCode = SVersionstr;
  535. if (SVersionCode >= RVersionCode && SVersionCode >= lVersionCode)
  536. {
  537. Debug.Log($"lVersionstr = {SVersionstr} ===== version = {version} 重新安装后需要删除之前资源");
  538. versionData = null;
  539. ReadDataFromFile(downloadedAssetsFileName, dlocaldatas=>
  540. {
  541. if (dlocaldatas != null)
  542. {
  543. List<DownloadDataEntity> localdownloadedList = SerizlizeResList(dlocaldatas);
  544. if (localdownloadedList != null && localdownloadedList.Count > 0)
  545. {
  546. downloadSucessList.AddRange(localdownloadedList);
  547. }
  548. DeleteDownloadFiles();
  549. }
  550. });
  551. }
  552. else
  553. {
  554. Debug.Log($"lVersionstr = {lVersionstr} ===== version = {version} 需要更新");
  555. GetRemoteResList();
  556. //CheckFinish = true;
  557. }
  558. });
  559. }
  560. else if (RVersionCode.release > lVersionCode.release || (RVersionCode.release == lVersionCode.release && RVersionCode.patch > lVersionCode.patch))
  561. {
  562. Debug.Log($"lVersionstr = {lVersionstr} ===== version = {version} 需要更新");
  563. GetRemoteResList();
  564. }
  565. else
  566. {
  567. SetCheckFinish();
  568. }
  569. }
  570. }
  571. }
  572. private void OnloadedLocalResList(byte[] datas)
  573. {
  574. if (datas == null)
  575. {
  576. }
  577. else
  578. {
  579. List<DownloadDataEntity> localList = SerizlizeResList(datas);
  580. if (localList != null)
  581. {
  582. int size = localList.Count;
  583. for (int i = 0; i < size; i++)
  584. {
  585. localResMap.Add(localList[i].FullName, localList[i]);
  586. }
  587. }
  588. }
  589. ReadDataFromFile(downloadedAssetsFileName, OnloadedDownloadResList);
  590. }
  591. private void OnloadedDownloadResList(byte[] datas)
  592. {
  593. if (datas != null)
  594. {
  595. List<DownloadDataEntity> localdownloadedList = SerizlizeResList(datas);
  596. if (localdownloadedList != null && localdownloadedList.Count > 0)
  597. {
  598. downloadSucessList.AddRange(localdownloadedList);
  599. }
  600. }
  601. GetNeedDownloadlist();
  602. DownLoadRemoteRes();
  603. }
  604. private List<DownloadDataEntity> GetLoacalResList(string path)
  605. {
  606. List<DownloadDataEntity> resList = null;
  607. if (System.IO.File.Exists(path))
  608. {
  609. byte[] data = System.IO.File.ReadAllBytes(path);
  610. resList = SerizlizeResList(data);
  611. }
  612. return resList;
  613. }
  614. private void GetNeedDownloadlist()
  615. {
  616. string ver = "Version";
  617. if (localResMap.ContainsKey(ver) && remoteResMap.ContainsKey(ver)&& localResMap[ver].MD5 == remoteResMap[ver].MD5)
  618. {
  619. Debug.Log("版本为最新状态");
  620. SetCheckFinish();
  621. //string savepath = localFilePath + downloadedAssetsFileName;
  622. //FileHelper.DeleteFile(savepath);
  623. return;
  624. }
  625. if (localResMap == null || localResMap.Count <= 0)
  626. {
  627. foreach (var item in remoteResMap)
  628. {
  629. needDownloadList.Add(item.Value);
  630. }
  631. }
  632. else
  633. {
  634. foreach (var item in remoteResMap)
  635. {
  636. if (item.Key == ver)
  637. {
  638. continue;
  639. }
  640. if (!localResMap.ContainsKey(item.Key) || localResMap[item.Key].MD5 != item.Value.MD5)
  641. {
  642. Debug.Log($"需跟新文件:{item.Value.FullName}");
  643. needDownloadList.Add(item.Value);
  644. }
  645. }
  646. }
  647. CheckFinish = true;
  648. }
  649. private string GetRemoteUrl( string name)
  650. {
  651. return downloadUrl + name;
  652. }
  653. private LocalFilePathInfo GetLocalFilePath(string name, bool onlyLocal = false, uint streamPerPath = 0)
  654. {
  655. LocalFilePathInfo ret = new LocalFilePathInfo();
  656. string path = LocalFilePath + name;
  657. ret.IsLocalRW = true;
  658. if (!FileSystem.Exists(path))
  659. {
  660. Debug.Log($" ========= LocalFilePath Path = {path} 不存在");
  661. if (!onlyLocal)
  662. {
  663. string lpPath = GetStreamResPath();
  664. for (int i = 0; i < streamPerPath; i++)
  665. {
  666. lpPath += "../";
  667. }
  668. path = lpPath + name;
  669. }
  670. ret.IsLocalRW = false;
  671. }
  672. ret.Path = path;
  673. Debug.Log($" ========= local Path = {path} ");
  674. return ret;
  675. }
  676. public void ReadDataFromFile(string name,Action<byte[]> callback,bool onlyLocal = false,uint streamPerPath = 0)
  677. {
  678. LocalFilePathInfo dlocalPath = GetLocalFilePath(name,onlyLocal, streamPerPath);
  679. if (dlocalPath.IsLocalRW)
  680. {
  681. try
  682. {
  683. byte[] dlocaldatas = File.ReadAllBytes(dlocalPath.Path);
  684. callback?.Invoke(dlocaldatas);
  685. }
  686. catch
  687. {
  688. Debug.Log("读取失败" + dlocalPath.Path);
  689. callback?.Invoke(null);
  690. }
  691. }
  692. else if(onlyLocal)
  693. {
  694. callback?.Invoke(null);
  695. }
  696. else
  697. {
  698. AssetDownloader.Instance.AssetLoadByCorutine(dlocalPath.Path, callback);
  699. }
  700. }
  701. private IEnumerator DeleteFiles()
  702. {
  703. ;
  704. int length = downloadSucessList.Count;
  705. string savepath = null;
  706. for (int i = 0; i < length; i++)
  707. {
  708. if (downloadSucessList[i].FullName == "font.unity3d")
  709. {
  710. Debug.Log("跳过删除");
  711. continue;
  712. }
  713. savepath = localFilePath + downloadSucessList[i].FullName;
  714. Debug.Log(savepath);
  715. FileHelper.DeleteFile(savepath);
  716. yield return null;
  717. }
  718. FileHelper.DeleteFile(localFilePath + downloadedAssetsFileName);
  719. yield return null;
  720. FileHelper.DeleteFile(localFilePath + versionFileName);
  721. yield return null;
  722. FileHelper.DeleteFile(LocalFilePath + assetsFileName);
  723. yield return null;
  724. SetCheckFinish();
  725. }
  726. public void DeleteDownloadFiles()
  727. {
  728. AssetDownloader.Instance.DoByCorutine(DeleteFiles);
  729. }
  730. public void Free()
  731. {
  732. if (!DowmloadError)
  733. {
  734. if (!string.IsNullOrEmpty(versionData))
  735. {
  736. FileHelper.WirteStringToFile(localFilePath + versionFileName, versionData);
  737. ResVersion = versionData;
  738. }
  739. if (remoteResList.Count > 0)
  740. {
  741. CsvWriter<DownloadDataEntity> csvWriter = new CsvWriter<DownloadDataEntity>(LocalFilePath + assetsFileName, "", remoteResList, downloadFormatInfo,false);
  742. csvWriter.Write();
  743. }
  744. }
  745. needDownloadList = null;
  746. downloadSucessList = null;
  747. remoteResList = null;
  748. localResMap = null;
  749. remoteResMap = null;
  750. versionData = null;
  751. //mainfestData = null;
  752. AssetDownloader.Instance.ClearTasks();
  753. }
  754. }