DownloadMgr.cs 28 KB

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