You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.0 KiB

  1. -- 文件下载器
  2. local FileDownloader = class("FileDownloader")
  3. -- urls : 下载地址
  4. -- fileName :保存在本地的文件名
  5. -- onFinishCallback : 完成后的回调
  6. function FileDownloader:ctor(urls, atts, onFinishCallback)
  7. self.urls = urls
  8. self.atts = atts
  9. self.onFinishCallback = onFinishCallback;
  10. end
  11. -- 开启下载
  12. function FileDownloader:start()
  13. local priority = 0
  14. local startPos = self.atts.offset or 0
  15. local dataSize = self.atts.compressedSize or 0
  16. local timebegin;
  17. local index = 0;
  18. local function downloadFileFromUrl()
  19. local function onUpdateFirst(status , data)
  20. if status == "successed" then
  21. -- 打印下载文件的耗时
  22. local timeEnd = os.time()
  23. logI(string.format("FileDownloader() cost time = %4s , size = %8s , file = %s", timeEnd - timebegin, dataSize, self.atts.name))
  24. -- 下载成功后将数据保存为本地文件
  25. self:onDownloadSuccessed(data);
  26. else
  27. -- 下载失败后再次请求下载
  28. downloadFileFromUrl()
  29. end
  30. end
  31. index = index + 1
  32. if not self.urls[index] then
  33. logE("FileDownloader:start() url is nil")
  34. return
  35. end
  36. timebegin = os.time()
  37. self.task = cc.CURLManager:getInstance():createTask(self.urls[index], onUpdateFirst , priority , startPos , dataSize);
  38. end
  39. downloadFileFromUrl();
  40. end
  41. -- 取消下载
  42. function FileDownloader:cancel()
  43. if self.task then
  44. cc.CURLManager:getInstance():cancelTask(self.task);
  45. self.task = nil
  46. end
  47. end
  48. function FileDownloader:onDownloadSuccessed(data)
  49. local targetFilename = cc.FileUtils:getInstance():getWritablePath() .. self.atts.md5
  50. local uncompressData = lzma.uncompress(data);
  51. -- 解压成功
  52. if uncompressData then
  53. local targetFile = io.open(targetFilename , "wb");
  54. if targetFile == nil then
  55. return;
  56. end
  57. targetFile:write(uncompressData);
  58. targetFile:close();
  59. else
  60. logE("文件解压失败:" , self.atts.name , "压缩文件大小:" , #data , "/" , self.atts.compressedSize) ;
  61. return;
  62. end
  63. -- win32平台下面文件系统是不会起作用的
  64. if not isWin32Platform() then
  65. -- 加到文件系统中
  66. self:addFile();
  67. --[[
  68. -- 检查MD5
  69. local md5Result = cc.FilePackage:calcFileMd5(targetFilename);
  70. if md5Result ~= self.atts.md5 then
  71. logE("文件下载完毕,但是文件的md5跟服务器上面记录的md5不一致,删除本地文件:", targetFilename)
  72. os.remove(targetFilename);
  73. return;
  74. end
  75. --]]
  76. end
  77. self:onFinish()
  78. end
  79. -- 添加文件信息到打包文件系统中
  80. function FileDownloader:addFile()
  81. local info =
  82. {
  83. isRomFile = false;
  84. origin = self.atts.origin;
  85. name = self.atts.name;
  86. md5 = self.atts.md5;
  87. size = tonumber(self.atts.size),
  88. realFilePath = getWritableFullName(self.atts.md5);
  89. };
  90. print("加到文件系统" , info.realFilePath , self.atts.md5 , self.atts.name);
  91. cc.FilePackage:getInstance():addFile(info);
  92. end
  93. -- 下载完成
  94. function FileDownloader:onFinish()
  95. if self.onFinishCallback then
  96. self.onFinishCallback()
  97. end
  98. end
  99. return FileDownloader