DownloadMgr.cs 28 KB

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