1616#import " VPUPRSAUtil.h"
1717#import " VPUPBase64Util.h"
1818#import " VPLuaSDK.h"
19+
1920#import " VPUPAESUtil.h"
2021#import " VPUPPathUtil.h"
2122#import " VPUPMD5Util.h"
@@ -32,6 +33,7 @@ @interface VPLuaScriptManager ()
3233@property (nonatomic , copy ) NSString *tempVersionFilePath;
3334@property (nonatomic , weak ) id <VPUPHTTPAPIManager>apiManager;
3435@property (nonatomic , strong ) VPUPPrefetchManager *prefetchManager;
36+ @property (nonatomic , strong ) dispatch_queue_t luaScriptQueue;
3537
3638@end
3739
@@ -48,6 +50,7 @@ - (instancetype)initWithLuaStorePath:(NSString *)path
4850 _versionFilePath = [path stringByAppendingPathComponent: @" version.json" ];
4951 _nativeVersion = nativeVersion;
5052 _apiManager = apiManager;
53+ _luaScriptQueue = dispatch_queue_create (" com.videopls.lua.scriptManager" , DISPATCH_QUEUE_SERIAL);
5154 [self getLuaVersionInfoWithVersionUrl: url];
5255 }
5356
@@ -119,7 +122,7 @@ - (void)downloadWithFileUrl:(NSDictionary *)data {
119122 [weakSelf error: error type: VPLuaScriptManagerErrorTypeDownloadFile];
120123 return ;
121124 }
122- // [weakSelf unzipWithFilePath:filePath.relativePath data:data];
125+ // [weakSelf unzipWithFilePath:filePath.relativePath data:data];
123126 NSString *fileMD5 = [VPUPMD5Util md5File: [filePath path ] size: 0 ];
124127 if ([fileMD5 isEqualToString: [data objectForKey: @" fileMd5" ]]) {
125128 [weakSelf checkFilesChange ];
@@ -133,37 +136,40 @@ - (void)downloadWithFileUrl:(NSDictionary *)data {
133136}
134137
135138- (void )checkFilesChange {
136- NSString *localFilesPath = [self .luaPath stringByAppendingPathComponent: @" manifest.json" ];
137- NSString *localFilesString = [NSString stringWithContentsOfURL: [NSURL fileURLWithPath: localFilesPath] encoding: NSUTF8StringEncoding error: nil ];
138- // NSDictionary *localFiles = VPUP_JsonToDictionary(localFilesString);
139- // NSArray *localFilesList = [localFiles objectForKey:@"data"];
140- NSArray *localFilesList = (NSArray *)VPUP_JsonToDictionary (localFilesString);
141-
142- NSString *downloadFilesPath = [self .tempVersionFilePath stringByAppendingPathComponent: @" manifest.json" ];
143- NSString *downloadFilesString = [NSString stringWithContentsOfURL: [NSURL fileURLWithPath: downloadFilesPath] encoding: NSUTF8StringEncoding error: nil ];
144- NSArray *downloadFilesList = (NSArray *)VPUP_JsonToDictionary (downloadFilesString);
145- // NSDictionary *downloadFiles = VPUP_JsonToDictionary(downloadFilesString);
146- // NSArray *downloadFilesList = [downloadFiles objectForKey:@"data"];
147-
148- NSMutableArray *needDownloadFilesList = [NSMutableArray arrayWithCapacity: 0 ];
149- for (NSDictionary *downloadFile in downloadFilesList) {
150- BOOL needDownload = YES ;
151- for (NSDictionary *localFile in localFilesList) {
152- if ([[downloadFile objectForKey: @" name" ] isEqualToString: [localFile objectForKey: @" name" ]] && [[downloadFile objectForKey: @" md5" ] isEqualToString: [localFile objectForKey: @" md5" ]]) {
153- needDownload = NO ;
154- break ;
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];
155164 }
156165 }
157- if (needDownload ) {
158- [needDownloadFilesList addObject: downloadFile ];
166+ if (needDownloadFilesList. count > 0 ) {
167+ [self downloadFilesList: needDownloadFilesList ];
159168 }
160- }
161- if (needDownloadFilesList.count > 0 ) {
162- [self downloadFilesList: needDownloadFilesList];
163- }
164- else {
165- [self downloadSuccess: YES ];
166- }
169+ else {
170+ [self downloadSuccess: YES ];
171+ }
172+ });
167173}
168174
169175- (void )downloadFilesList : (NSArray *)filesList {
@@ -203,38 +209,40 @@ - (void)downloadFilesList:(NSArray *)filesList {
203209}
204210
205211- (void )checkDownLoadFiles : (NSArray *)filesList {
206- if (!filesList || filesList.count == 0 ) {
207- [self downloadSuccess: YES ];
208- return ;
209- }
210-
211- NSError *error = nil ;
212- for (NSDictionary *file in filesList) {
213- NSString *filePath = [self .tempVersionFilePath stringByAppendingPathComponent: [file objectForKey: @" name" ]];
214- NSString *fileMD5 = [VPUPMD5Util md5File: filePath size: 0 ];
215- if (![fileMD5 isEqualToString: [file objectForKey: @" md5" ]]) {
216- error = [NSError errorWithDomain: VPLuaScriptManagerErrorDomain code: -3003 userInfo: @{NSLocalizedDescriptionKey :[NSString stringWithFormat: @" filePath:%@ , file:%@ , download file md5 error" , filePath, file]}];
217- [[NSFileManager defaultManager ] removeItemAtPath: filePath error: nil ];
218- }
219- }
220-
221- if (error) {
222- [self error: error type: VPLuaScriptManagerErrorTypFileMD5];
223- }
224- else {
225- [self copyFileFromPath: self .tempVersionFilePath toPath: self .luaPath];
226- // 将版本写入文件,若失败删除
227- NSError *writeError = nil ;
228- [self .versionData writeToFile: self .versionFilePath atomically: YES encoding: NSUTF8StringEncoding error: &writeError];
229- if (writeError) {
230- [self removeAllFileAtLuaPath ];
231- [self error: writeError type: VPLuaScriptManagerErrorTypeWriteVersionFile];
212+ dispatch_async (_luaScriptQueue, ^{
213+ if (!filesList || filesList.count == 0 ) {
214+ [self downloadSuccess: YES ];
232215 return ;
233216 }
234- // 最终完成
235- [self downloadSuccess: YES ];
236- [[NSFileManager defaultManager ] removeItemAtPath: self .tempVersionFilePath error: nil ];
237- }
217+
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+ }
226+ }
227+
228+ if (error) {
229+ [self error: error type: VPLuaScriptManagerErrorTypFileMD5];
230+ }
231+ 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 ];
244+ }
245+ });
238246}
239247
240248- (void )unzipWithFilePath : (NSString *)path data : (NSDictionary *)data {
@@ -245,7 +253,7 @@ - (void)unzipWithFilePath:(NSString *)path data:(NSDictionary *)data {
245253 NSFileManager *fileManager = [[NSFileManager alloc ] init ];
246254 NSArray * array = [fileManager contentsOfDirectoryAtPath: luaZipFilesPath error: nil ];
247255 for (int i = 0 ; i<[array count ]; i++) {
248-
256+
249257 NSString *fullPath = [luaZipFilesPath stringByAppendingPathComponent: [array objectAtIndex: i]];
250258 if ([[fullPath lastPathComponent ] containsString: @" zip" ]) {
251259 LVZipArchive *archive = [LVZipArchive archiveWithData: [NSData dataWithContentsOfFile: fullPath]];
0 commit comments