// // JoinFileDir.m // JOINFILE // // Created by 杜家兑 on 14/12/31. // Copyright (c) 2014年 mac. All rights reserved. // #import "JoinFileDir.h" #include "JoinFile.h" #import "ZGSynthesizeSingleton.h" #import "ZGFileUtils.h" #import "ZGJsonUtils.h" #import #define NSTR(format,...) [NSString stringWithFormat:format,__VA_ARGS__] #define DDLOG(...) NSLog(__VA_ARGS__) #define FMGR [NSFileManager defaultManager] #define LaJiYinXiao_PATH @"/Users/dujiadui/work/UUTDATA/lajiyinxiao/" @implementation JoinFileDir static NSString* music_fileName=@""; static NSString* _projectPath=@""; static NSString* _srcPath=@""; static NSString* _goalPath=@""; static NSMutableArray* _maUsedFileName=[[NSMutableArray alloc] init]; ZG_SYNTHESIZE_SINGLETON_FOR_CLASS(JoinFileDir); -(NSString *)randString:(int)_yushu weishu:(int)_weishu{ NSString *rs = @""; int len=arc4random() %_yushu+_weishu; NSArray *array=[NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z",nil]; for (int i=0; idoJoinFile(src_filePath,tar_filePath.c_str(),"b",tmpName,[_projectPath UTF8String]); } else if (!strcmp(fileExt, "jpg")||!strcmp(fileExt, "png")||!strcmp(fileExt, "ccbi")) { int arryKey = arc4random() % [arrFileName_b count]; NSString *tar_oc_tarFilePath=[NSString stringWithFormat:@"%@/%@",tarDir,[arrFileName_b objectAtIndex:arryKey]]; std::string tar_filePath = [tar_oc_tarFilePath UTF8String]; tar_filePath=tar_filePath+".ccbi"; NSString *oc_tmpName=[NSString stringWithFormat:@"%@.%s", [self randFileName],fileExt]; const char* tmpName = [oc_tmpName UTF8String]; _joinFile->doJoinFile(src_filePath,tar_filePath.c_str(),"b",tmpName,[_projectPath UTF8String]); }else if ( !strcmp(fileExt, "txt") ||!strcmp(fileExt, "lua") ||!strcmp(fileExt, "fnt") ||(!_isIos && !strcmp(fileExt, "plist")) ||!strcmp(fileExt, "json") ||!strcmp(fileExt, "js") ){ int arryKey = arc4random() % [arrFileName_t count]; NSString *tar_oc_tarFilePath=[NSString stringWithFormat:@"%@/%@",tarDir,[arrFileName_t objectAtIndex:arryKey]]; std::string tar_filePath = [tar_oc_tarFilePath UTF8String]; tar_filePath=tar_filePath+".lua"; NSString *oc_tmpName=[NSString stringWithFormat:@"%@.%s", [self randFileName],fileExt]; const char* tmpName = [oc_tmpName UTF8String]; _joinFile->doJoinFile(src_filePath,tar_filePath.c_str(),"t",tmpName,[_projectPath UTF8String]); } else{ NSString *tar_oc_tarFilePath=[NSString stringWithFormat:@"%@/%@",tarDir,oc_fileName]; [ZGFileUtils copyFileFromPath:oc_filePath toPath:tar_oc_tarFilePath]; } } else { [self joinFileByDirRecursion:oc_filePath isIos:_isIos]; } } } -(void)joinFileProjectDirTAO:(NSString*)projectPath isIos:(BOOL)_isIos{ _projectPath=[NSString stringWithString:projectPath]; _projectPath = [_projectPath stringByReplacingOccurrencesOfString:@"//" withString:@"/"]; _srcPath=[NSString stringWithFormat:@"%@/assets",projectPath]; _srcPath = [_srcPath stringByReplacingOccurrencesOfString:@"//" withString:@"/"]; _goalPath=[NSString stringWithFormat:@"%@/assets_tmp",projectPath]; _goalPath = [_goalPath stringByReplacingOccurrencesOfString:@"//" withString:@"/"]; [ZGFileUtils deleteFileOrDirectoryAtPath:_goalPath]; [ZGFileUtils createDirectoryIfNotExist:_goalPath]; // [self addSomeFileToAssets]; //开始混淆 [self joinFileByDirRecursionTAO:_srcPath isIos:_isIos]; //把混淆后的assets_tmp文件夹子替换成assets文件夹 // [ZGFileUtils deleteFileOrDirectoryAtPath:[NSString stringWithFormat:@"%@/",_srcPath]]; // [ZGFileUtils moveFileAtPath:[NSString stringWithFormat:@"%@/",_goalPath] toPath:[NSString stringWithFormat:@"%@/",_srcPath]]; } -(void)joinFileByDirRecursionTAO:(NSString*)dirPath isIos:(BOOL)_isIos{ JoinFile * _joinFile=JoinFile::getInstance(); NSArray *fileArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirPath error:nil]; for (int i = 0; i < [fileArray count]; i++) { NSString *oc_filePath = [NSString stringWithFormat:@"%@/%@", dirPath, [fileArray objectAtIndex:i]]; oc_filePath = [oc_filePath stringByReplacingOccurrencesOfString:@"//" withString:@"/"]; if (![ZGFileUtils pathIsDirectory:oc_filePath]) { const char* src_filePath = [oc_filePath UTF8String]; NSString * oc_fileName=[oc_filePath lastPathComponent]; NSString *extension=[oc_filePath pathExtension]; NSString * pathBefore=[oc_filePath stringByDeletingLastPathComponent]; NSRange range = [pathBefore rangeOfString:_srcPath];//查找子串,找不到返回NSNotFound 找到返回location和length // if (range.location != NSNotFound) { // // NSLog(@"%ld %ld",range.location, range.length); // // } NSString * ptr2 = [pathBefore substringFromIndex:range.length]; const char* fileExt = [extension UTF8String]; NSString *tarDir=[NSString stringWithFormat:@"%@%@",_goalPath,ptr2]; if (![ZGFileUtils fileExist:tarDir]) { [ZGFileUtils createDirectoryIfNotExist:tarDir]; } if (!_isIos&&(!strcmp(fileExt, "mp3")||!strcmp(fileExt, "wav"))){ NSString *tar_oc_tarFilePath=[NSString stringWithFormat:@"%@/%@",tarDir,[self getMusicFileName]]; // NSString *tar_oc_tarFilePath=[NSString stringWithFormat:@"%@/%@",tarDir,[self randFileName]]; std::string tar_filePath = [tar_oc_tarFilePath UTF8String]; tar_filePath=tar_filePath+".mp3"; NSString *oc_tmpName=[NSString stringWithFormat:@"%@.%s", [self randFileName],fileExt]; const char* tmpName = [oc_tmpName UTF8String]; _joinFile->doJoinFile(src_filePath,tar_filePath.c_str(),"b",tmpName,[_projectPath UTF8String]); } else if (!strcmp(fileExt, "jpg")||!strcmp(fileExt, "png")||!strcmp(fileExt, "ccbi")) { NSString *tar_oc_tarFilePath=[NSString stringWithFormat:@"%@/%@",tarDir,[self randFileName]]; std::string tar_filePath = [tar_oc_tarFilePath UTF8String]; tar_filePath=tar_filePath+".ccbi"; NSString *oc_tmpName=[NSString stringWithFormat:@"%@.%s", [self randFileName],fileExt]; const char* tmpName = [oc_tmpName UTF8String]; _joinFile->doJoinFile(src_filePath,tar_filePath.c_str(),"b",tmpName,[_projectPath UTF8String]); }else if ( !strcmp(fileExt, "txt") ||!strcmp(fileExt, "lua") ||!strcmp(fileExt, "fnt") ||(!_isIos && !strcmp(fileExt, "plist")) ||!strcmp(fileExt, "json") ||!strcmp(fileExt, "js") ){ NSString *tar_oc_tarFilePath=[NSString stringWithFormat:@"%@/%@",tarDir,[self randFileName]]; std::string tar_filePath = [tar_oc_tarFilePath UTF8String]; tar_filePath=tar_filePath+".lua"; NSString *oc_tmpName=[NSString stringWithFormat:@"%@.%s", [self randFileName],fileExt]; const char* tmpName = [oc_tmpName UTF8String]; _joinFile->doJoinFile(src_filePath,tar_filePath.c_str(),"t",tmpName,[_projectPath UTF8String]); } else{ NSString *tar_oc_tarFilePath=[NSString stringWithFormat:@"%@/%@",tarDir,oc_fileName]; [ZGFileUtils copyFileFromPath:oc_filePath toPath:tar_oc_tarFilePath]; } } else { [self joinFileByDirRecursion:oc_filePath isIos:_isIos]; } } } -(NSString *)getMusicFileName{ if ([music_fileName isEqual:@""]) { music_fileName=[self randFileName]; } return music_fileName; } -(NSArray *)randFileNameArr{ int arrLen=arc4random() % 4+4; NSMutableArray *arrFileName=[NSMutableArray arrayWithCapacity:arrLen]; for (int i=0; isetPackName([packname UTF8String]); } -(void)createFileFromConf:(NSString *)projectPath{ JoinFile * _joinFile=JoinFile::getInstance(); _joinFile->createFileFromConf([projectPath UTF8String]); } NSString * getMD5FromString(NSString * str) { const char *cStr = [str UTF8String]; unsigned char result[16]; CC_MD5( cStr, strlen(cStr), result ); return [NSString stringWithFormat: @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15] ]; } -(void)dealAssetsYt:(NSString*)projectPath isAndroid:(BOOL)_isAndroid packgeName:(NSString*)packagename typeAddDic:(NSDictionary*)dic{ JoinFile::getInstance()->setPackName([packagename UTF8String]); NSMutableArray * skipFileArr = [NSMutableArray arrayWithObjects:@".DS_Store", nil]; projectPath = [projectPath stringByReplacingOccurrencesOfString:@"//" withString:@"/"]; time(0); NSString * confFileName = @"mainData"; NSMutableArray * txtFileTypeArr = [NSMutableArray arrayWithObjects:@"txt",@"fnt",@"json",@"lua",@"js", nil]; NSMutableArray * binaryFileTypeArr = [NSMutableArray arrayWithObjects:@"jpg",@"png",@"ccbi", nil]; //@"jsc", NSMutableArray * musicFileTypeArr = [NSMutableArray arrayWithObjects:@"wav",@"mp3",@"WAV",@"MP3", nil];// NSMutableArray * dirCpTypeArr =[NSMutableArray arrayWithObjects:@"ttf",@"TTF",nil];// [txtFileTypeArr addObjectsFromArray:dic[@"txt"]]; [binaryFileTypeArr addObjectsFromArray:dic[@"binary"]]; [musicFileTypeArr addObjectsFromArray:dic[@"music"]]; [dirCpTypeArr addObjectsFromArray:dic[@"cp"]]; if (_isAndroid) { [txtFileTypeArr addObject:@"plist"]; } NSMutableDictionary * pathDic = [NSMutableDictionary dictionary]; NSMutableDictionary * decDic = [NSMutableDictionary dictionary]; NSString * srcPath = projectPath;//NSTR(@"%@assets/",projectPath); NSString * dataDir = @"bin/Data"; NSString * goalPath = NSTR(@"%@/%@_tmp",[projectPath stringByDeletingLastPathComponent],[projectPath lastPathComponent]); NSString * dataPath = NSTR(@"%@/%@",goalPath,dataDir); [ZGFileUtils deleteFileOrDirectoryAtPath:dataPath]; [ZGFileUtils createDirectoryIfNotExist:dataPath]; //开始混淆 NSArray *fileArray = [ZGFileUtils arrayOfFileAtDirRecursion:srcPath]; for(NSString * strFile in fileArray){ bool needRec = true; bool needDec = false; NSString *file = [strFile stringByReplacingOccurrencesOfString:@"//" withString:@"/"]; NSString * fileName=[file lastPathComponent]; if([skipFileArr containsObject:fileName]){ continue; } NSString * fileExt=[file pathExtension]; NSString * subDir = [file substringFromIndex:[file rangeOfString:srcPath].length+1]; NSString * armFileName = NSTR(@"%@/%@",dataDir,getMD5FromString(NSTR(@"%@_%@",subDir,[self randFileName])));//;fileName if ([musicFileTypeArr containsObject:fileExt]) { armFileName = [armFileName stringByAppendingString:@".jpg"]; } NSString * armPath = NSTR(@"%@/%@",goalPath,armFileName); NSMutableDictionary * item = [NSMutableDictionary dictionaryWithObjectsAndKeys:armFileName,@"fileName", nil]; if([txtFileTypeArr containsObject:fileExt]){ JoinFile::getInstance()->encryptSingleFile([file UTF8String], [armPath UTF8String], ""); [item setObject:@"t" forKey:@"type"]; needDec = true; } else if([binaryFileTypeArr containsObject:fileExt]){ JoinFile::getInstance()->encryptSingleFile([file UTF8String], [armPath UTF8String], "b"); [item setObject:@"b" forKey:@"type"]; needDec = true; } else if( [musicFileTypeArr containsObject:fileExt] ){ FILE* musicFileHandle = fopen([armPath UTF8String], "wb"); unsigned long nSize = 0; unsigned char *pBuffer =JoinFile::getInstance()->getFileData([file UTF8String],"rb", &nSize); NSString *lajiStr = [self randFileName]; fwrite(pBuffer, sizeof(unsigned char), nSize, musicFileHandle); fwrite([lajiStr UTF8String], sizeof(char), [lajiStr length], musicFileHandle); [item setObject:@(0) forKey:@"start"]; [item setObject:@(nSize) forKey:@"len"]; fclose(musicFileHandle); } else if([dirCpTypeArr containsObject:fileExt]){ DDLOG(@"不能处理的文件:%@,不变文件名,直接拷贝!",subDir); [ZGFileUtils copyFileFromPath:file toPath:NSTR(@"%@/%@",goalPath,subDir)]; [item setObject:@"n" forKey:@"type"]; needRec = false; } else{ DDLOG(@"未知类型:%@,改下名字,直接拷贝!",subDir); [ZGFileUtils copyFileFromPath:file toPath:armPath]; [item setObject:@"n" forKey:@"type"]; } if(needRec){ [pathDic setObject: item forKey:subDir]; } if (needDec) { [decDic setObject:@"true" forKey:armFileName]; } } //写配置表文件... NSString * armConfPath = NSTR(@"%@/%@",dataPath,confFileName); [[ZGJsonUtils dictionaryToString:@{@"path":pathDic,@"decode":decDic}] writeToFile:armConfPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; JoinFile::getInstance()->encryptSingleFile([armConfPath UTF8String], [armConfPath UTF8String], ""); //目录替换. if(_isAndroid){ NSString * tmpDir = NSTR(@"%@_",projectPath); [ZGFileUtils deleteFileOrDirectoryAtPath:tmpDir]; [FMGR moveItemAtPath:projectPath toPath:tmpDir error:nil]; [FMGR moveItemAtPath:goalPath toPath:projectPath error:nil]; } } @end