DownloadMgr.cs 28 KB

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