2121#import " VPUPPathUtil.h"
2222#import " VPUPMD5Util.h"
2323#import " VPUPPrefetchManager.h"
24+ #import " VPLuaLoader.h"
2425
2526NSString *const VPLuaScriptManagerErrorDomain = @" VPLuaScriptManager.Error" ;
2627
@@ -51,7 +52,7 @@ - (instancetype)initWithLuaStorePath:(NSString *)path
5152 _nativeVersion = nativeVersion;
5253 _apiManager = apiManager;
5354 _luaScriptQueue = dispatch_queue_create (" com.videopls.lua.scriptManager" , DISPATCH_QUEUE_SERIAL);
54- [self getLuaVersionInfoWithVersionUrl : url];
55+ [self preloadLuaFilesWithUrl : url];
5556 }
5657
5758 return self;
@@ -64,7 +65,7 @@ - (VPUPPrefetchManager *)prefetchManager {
6465 return _prefetchManager;
6566}
6667
67- - (void )getLuaVersionInfoWithVersionUrl : (NSString *)url {
68+ - (void )preloadLuaFilesWithUrl : (NSString *)url {
6869 __weak typeof (self) weakSelf = self;
6970 VPUPHTTPBusinessAPI *api = [[VPUPHTTPBusinessAPI alloc ] init ];
7071 api.baseUrl = url;
@@ -88,235 +89,39 @@ - (void)getLuaVersionInfoWithVersionUrl:(NSString *)url {
8889 strongSelf.versionData = dataString;
8990 NSDictionary *data = VPUP_JsonToDictionary (dataString);
9091
91- NSString *version = [data objectForKey: @" version" ];
92- NSString *versionFileString = [NSString stringWithContentsOfURL: [NSURL fileURLWithPath: strongSelf.versionFilePath] encoding: NSUTF8StringEncoding error: nil ];
93- NSDictionary *versionFile = VPUP_JsonToDictionary (versionFileString);
94- NSString *localVersion = [versionFile objectForKey: @" version" ];
95-
96- if (!localVersion || ![localVersion isEqualToString: version]) {
97-
98- NSString *url = [data objectForKey: @" downloadUrl" ];
99- if (!url || [url isEqual: [NSNull null ]]) {
100- [strongSelf error: error type: VPLuaScriptManagerErrorTypeDownloadFile];
92+ if ([[data objectForKey: @" resCode" ] isEqualToString: @" 00" ] && [data objectForKey: @" luaList" ] > 0 ) {
93+ NSArray *luaList = [data objectForKey: @" luaList" ];
94+ if (luaList.count > 0 ) {
95+ [strongSelf downloadFilesList: luaList];
10196 return ;
10297 }
103-
104- // 下载并删除之前下载的所有文件
105- // [weakSelf removeAllFileAtLuaPath];
106- [strongSelf downloadWithFileUrl: data];
107- return ;
10898 }
109- // 无需下载,本地已经是最新版本
99+
100+ // 无需下载,没有下载的文件
110101 [strongSelf downloadSuccess: YES ];
111102 };
112103 [_apiManager sendAPIRequest: api];
113104}
114105
115- - (void )downloadWithFileUrl : (NSDictionary *)data {
116- __weak typeof (self) weakSelf = self;
117- NSString *url = [data objectForKey: @" downloadUrl" ];
118- self.tempVersionFilePath = [VPUPPathUtil subPathOfLua: [NSString stringWithFormat: @" /%@ -template" ,[data objectForKey: @" version" ]]];
119-
120- VPUPResumeDownloader *downloader = [[VPUPResumeDownloader alloc ] initWithDownloadUrl: url resumePath: self .tempVersionFilePath progress: nil completionHandler: ^(VPUPResumeDownloader *downloader, NSURL *filePath, NSError *error) {
121- if (error) {
122- [weakSelf error: error type: VPLuaScriptManagerErrorTypeDownloadFile];
123- return ;
124- }
125- // [weakSelf unzipWithFilePath:filePath.relativePath data:data];
126- NSString *fileMD5 = [VPUPMD5Util md5File: [filePath path ] size: 0 ];
127- if ([fileMD5 isEqualToString: [data objectForKey: @" fileMd5" ]]) {
128- [weakSelf checkFilesChange ];
129- }
130- else {
131- error = [NSError errorWithDomain: VPLuaScriptManagerErrorDomain code: -3001 userInfo: @{NSLocalizedDescriptionKey :[NSString stringWithFormat: @" filePath:%@ , file:%@ , download file md5 error" , filePath, data]}];
132- [weakSelf error: error type: VPLuaScriptManagerErrorTypFileMD5];
133- }
134- }];
135- [downloader resume ];
136- }
137-
138- - (void )checkFilesChange {
139- dispatch_async (_luaScriptQueue, ^{
140-
141- NSString *localFilesPath = [self .luaPath stringByAppendingPathComponent: @" manifest.json" ];
142- NSString *localFilesString = [NSString stringWithContentsOfURL: [NSURL fileURLWithPath: localFilesPath] encoding: NSUTF8StringEncoding error: nil ];
143- // NSDictionary *localFiles = VPUP_JsonToDictionary(localFilesString);
144- // NSArray *localFilesList = [localFiles objectForKey:@"data"];
145- NSArray *localFilesList = (NSArray *)VPUP_JsonToDictionary (localFilesString);
146-
147- NSString *downloadFilesPath = [self .tempVersionFilePath stringByAppendingPathComponent: @" manifest.json" ];
148- NSString *downloadFilesString = [NSString stringWithContentsOfURL: [NSURL fileURLWithPath: downloadFilesPath] encoding: NSUTF8StringEncoding error: nil ];
149- NSArray *downloadFilesList = (NSArray *)VPUP_JsonToDictionary (downloadFilesString);
150- // NSDictionary *downloadFiles = VPUP_JsonToDictionary(downloadFilesString);
151- // NSArray *downloadFilesList = [downloadFiles objectForKey:@"data"];
152-
153- NSMutableArray *needDownloadFilesList = [NSMutableArray arrayWithCapacity: 0 ];
154- for (NSDictionary *downloadFile in downloadFilesList) {
155- BOOL needDownload = YES ;
156- for (NSDictionary *localFile in localFilesList) {
157- if ([[downloadFile objectForKey: @" name" ] isEqualToString: [localFile objectForKey: @" name" ]] && [[downloadFile objectForKey: @" md5" ] isEqualToString: [localFile objectForKey: @" md5" ]]) {
158- needDownload = NO ;
159- break ;
160- }
161- }
162- if (needDownload) {
163- [needDownloadFilesList addObject: downloadFile];
164- }
165- }
166- if (needDownloadFilesList.count > 0 ) {
167- [self downloadFilesList: needDownloadFilesList];
168- }
169- else {
170- [self downloadSuccess: YES ];
171- }
172- });
173- }
174-
175106- (void )downloadFilesList : (NSArray *)filesList {
176107 if (!filesList || filesList.count == 0 ) {
177108 [self downloadSuccess: YES ];
178109 return ;
179110 }
180- NSMutableArray *filesUrl = [NSMutableArray arrayWithCapacity: 0 ];
181- NSMutableArray *filesName = [NSMutableArray arrayWithCapacity: 0 ];
182- for (NSDictionary *file in filesList) {
183- [filesUrl addObject: [file objectForKey: @" url" ]];
184- [filesName addObject: [file objectForKey: @" name" ]];
185- }
186- static NSInteger count = 0 ;
187111 __weak typeof (self) weakSelf = self;
188-
189- [self .prefetchManager prefetchURLs: filesUrl
190- fileNames: filesName
191- destinationPath: self .tempVersionFilePath
192- completionBlock: ^(NSUInteger numberOfFinishedUrls, NSUInteger numberOfSkippedUrls) {
193- if (numberOfSkippedUrls > 0 ) {
194- if (count < 3 ) {
195- [weakSelf downloadFilesList: filesList];
196- }
197- else {
198- count = 0 ;
199- NSError *error = [NSError errorWithDomain: VPLuaScriptManagerErrorDomain code: -3002 userInfo: @{NSLocalizedDescriptionKey :[NSString stringWithFormat: @" Download file error, success count %ld , faild count %ld " , numberOfFinishedUrls, numberOfSkippedUrls]}];
200- [weakSelf error: error type: VPLuaScriptManagerErrorTypeDownloadFile];
201- }
202- }
203- else {
204- count = 0 ;
205- [weakSelf checkDownLoadFiles: filesList];
206- }
207- }];
208- count ++;
209- }
210-
211- - (void )checkDownLoadFiles : (NSArray *)filesList {
212- dispatch_async (_luaScriptQueue, ^{
213- if (!filesList || filesList.count == 0 ) {
214- [self downloadSuccess: YES ];
215- return ;
216- }
112+ [[VPLuaLoader sharedLoader ] checkAndDownloadFilesList: filesList complete: ^(NSError * _Nonnull error, VPUPTrafficStatisticsList *trafficList) {
217113
218- NSError *error = nil ;
219- for (NSDictionary *file in filesList) {
220- NSString *filePath = [self .tempVersionFilePath stringByAppendingPathComponent: [file objectForKey: @" name" ]];
221- NSString *fileMD5 = [VPUPMD5Util md5File: filePath size: 0 ];
222- if (![fileMD5 isEqualToString: [file objectForKey: @" md5" ]]) {
223- error = [NSError errorWithDomain: VPLuaScriptManagerErrorDomain code: -3003 userInfo: @{NSLocalizedDescriptionKey :[NSString stringWithFormat: @" filePath:%@ , file:%@ , download file md5 error" , filePath, file]}];
224- [[NSFileManager defaultManager ] removeItemAtPath: filePath error: nil ];
225- }
114+ if (trafficList) {
115+ [VPUPTrafficStatistics sendTrafficeStatistics: trafficList type: VPUPTrafficTypeInitApp];
226116 }
227117
228118 if (error) {
229- [self error: error type: VPLuaScriptManagerErrorTypFileMD5 ];
119+ [weakSelf error: error type: VPLuaScriptManagerErrorTypeDownloadFile ];
230120 }
231121 else {
232- [self copyFileFromPath: self .tempVersionFilePath toPath: self .luaPath];
233- // 将版本写入文件,若失败删除
234- NSError *writeError = nil ;
235- [self .versionData writeToFile: self .versionFilePath atomically: YES encoding: NSUTF8StringEncoding error: &writeError];
236- if (writeError) {
237- [self removeAllFileAtLuaPath ];
238- [self error: writeError type: VPLuaScriptManagerErrorTypeWriteVersionFile];
239- return ;
240- }
241- // 最终完成
242- [self downloadSuccess: YES ];
243- [[NSFileManager defaultManager ] removeItemAtPath: self .tempVersionFilePath error: nil ];
122+ [weakSelf downloadSuccess: YES ];
244123 }
245- });
246- }
247-
248- - (void )unzipWithFilePath : (NSString *)path data : (NSDictionary *)data {
249- LVZipArchive *archive = [LVZipArchive archiveWithData: [NSData dataWithContentsOfFile: path]];
250- if ([archive unzipToDirectory: self .luaPath]) {
251- NSString *luaZipFilesPath = [self .luaPath stringByAppendingString: [NSString stringWithFormat: @" /%@ -template" ,[data objectForKey: @" version" ]]];
252-
253- NSFileManager *fileManager = [[NSFileManager alloc ] init ];
254- NSArray * array = [fileManager contentsOfDirectoryAtPath: luaZipFilesPath error: nil ];
255- for (int i = 0 ; i<[array count ]; i++) {
256-
257- NSString *fullPath = [luaZipFilesPath stringByAppendingPathComponent: [array objectAtIndex: i]];
258- if ([[fullPath lastPathComponent ] containsString: @" zip" ]) {
259- LVZipArchive *archive = [LVZipArchive archiveWithData: [NSData dataWithContentsOfFile: fullPath]];
260- [archive unzipToDirectory: luaZipFilesPath];
261- }
262- }
263-
264- [self copyFileFromPath: luaZipFilesPath toPath: self .luaPath];
265- NSString *jsonData = VPUP_DictionaryToJson (data);
266- // 将版本写入文件,若失败删除
267- NSError *writeError = nil ;
268- [jsonData writeToFile: self .versionFilePath atomically: YES encoding: NSUTF8StringEncoding error: &writeError];
269- if (writeError) {
270- [self removeAllFileAtLuaPath ];
271- [self error: writeError type: VPLuaScriptManagerErrorTypeWriteVersionFile];
272- return ;
273- }
274- // 最终完成
275- [self downloadSuccess: YES ];
276- [fileManager removeItemAtPath: luaZipFilesPath error: nil ];
277- }
278- else {
279- [self error: nil type: VPLuaScriptManagerErrorTypeUnzip];
280- }
281- [[NSFileManager defaultManager ] removeItemAtPath: path error: nil ];
282- }
283-
284- - (void )copyFileFromPath : (NSString *)sourcePath toPath : (NSString *)toPath {
285-
286- NSFileManager *fileManager = [[NSFileManager alloc ] init ];
287- NSArray * array = [fileManager contentsOfDirectoryAtPath: sourcePath error: nil ];
288-
289- for (int i = 0 ; i<[array count ]; i++) {
290-
291- NSString *fullPath = [sourcePath stringByAppendingPathComponent: [array objectAtIndex: i]];
292- NSString *fullToPath = [toPath stringByAppendingPathComponent: [array objectAtIndex: i]];
293-
294- if ([fileManager fileExistsAtPath: fullToPath]) {
295- [fileManager removeItemAtPath: fullToPath error: nil ];
296- }
297-
298- // 判断是不是文件夹
299- BOOL isFolder = NO ;
300-
301- // 判断是不是存在路径 并且是不是文件夹
302- BOOL isExist = [fileManager fileExistsAtPath: fullPath isDirectory: &isFolder];
303- if (isExist) {
304- NSError *err = nil ;
305- [[NSFileManager defaultManager ] copyItemAtPath: fullPath toPath: fullToPath error: &err];
306- NSLog (@" %@ " ,err);
307- if (isFolder) {
308- [self copyFileFromPath: fullPath toPath: fullToPath];
309- }
310- }
311- }
312- }
313-
314- - (void )removeAllFileAtLuaPath {
315- NSArray *paths = [[NSFileManager defaultManager ] subpathsAtPath: _luaPath];
316- for (NSString *path in paths) {
317- NSString *filePath = [_luaPath stringByAppendingPathComponent: path];
318- [[NSFileManager defaultManager ] removeItemAtPath: filePath error: nil ];
319- }
124+ }];
320125}
321126
322127#pragma mark - protocol used
0 commit comments