public void TheadEachRun(object obj) { // 进行拆箱. ThreadHandleInfo threadData = obj as ThreadHandleInfo; // 这里写的输出结果有问题. // 应该使用<string, List<string>>后面的是依赖项. // 每个线程有个结果 最终有个汇总结果. // List<string> findResult = new List<string>(); // 输出结果 Dictionary <string, List <string> > resultDic = new Dictionary <string, List <string> >(); // int nCheckFileNum = threadData.inputCheckData.Count; // int nHandleCheckIndex = 0; // string strShowProgress = string.Empty; // 这个inputCheckData没有做切割. foreach (var eachCheckInfo in threadData.inputCheckData) { // 记录处理的每一个文件 // nHandleCheckIndex++; // strShowProgress = string.Format("Thread {0} {1}/{2}", threadData.threadName, // nHandleCheckIndex, // nCheckFileNum); // Console.WriteLine(strShowProgress); foreach (var eachDepInfo in threadData.inputDepData) { if (Regex.IsMatch(eachDepInfo.strFileContent, eachCheckInfo.strGuid)) { if (resultDic.ContainsKey(eachCheckInfo.strFileName)) { // 如果包含Key 应该已经有了List<string> resultDic[eachCheckInfo.strFileName].Add(eachDepInfo.strFileName); } else { resultDic[eachCheckInfo.strFileName] = new List <string>(); resultDic[eachCheckInfo.strFileName].Add(eachDepInfo.strFileName); } } } } // 把结果写入. lock (locker) { Dictionary <string, List <string> > outputData = threadData.dicFileDeps; // 加锁来处理 其他线程会阻塞住等待. // 把resultDic里的结果导入outputData里. foreach (var threadResultItem in resultDic) { // threadData.outputData.Add(eachOutput); // 判断check项在outputData里是否存在. string strSaveKey = CommonUtils.GetAssetNameFromPath(threadResultItem.Key, m_unityDataPath); if (outputData.ContainsKey(threadResultItem.Key)) { } else { outputData[strSaveKey] = new List <string>(); } foreach (var eachDep in threadResultItem.Value) { // 改变eachDep为一个Asset的值. string strSaveDep = CommonUtils.GetAssetNameFromPath(eachDep, m_unityDataPath); outputData[strSaveKey].Add(strSaveDep); } } } string strShow = string.Format("Thread {0} Done", threadData.threadName); Console.WriteLine(strShow); }
public void Run() { // 开辟线程. 为不同线程分配不同任务. // 如何判断其他线程执行完毕. 切割任务. // 字典保存文件名和对应的文件字符串内容. // 目前认为 只有这6种资源才有可能包含其他资源. // ".prefab", ".unity", ".mat", ".asset", ".anim", ".controller" List <string> lstDepWithExt = new List <string>() { ".prefab", ".unity", ".mat", ".asset", ".anim", ".controller", ".guiskin", ".mixer", ".fontsettings", ".flare", ".playable", ".overrideController", }; string[] findFiles = Directory.GetFiles(m_DepFilesPath, "*.*", SearchOption.AllDirectories) .Where(s => lstDepWithExt.Contains(Path.GetExtension(s).ToLower())).ToArray(); // 对Check资源再进行一个筛选 List <string> lstCheckWithExt = new List <string>() { ".controller", ".asset", ".cs", ".json", ".dll", ".txt", ".png", ".guiskin", ".prefab", ".mixer", ".fontsettings", ".mat", ".TTF", ".ttf", ".psd", ".otf", ".shader", ".anim", ".ogg", ".cginc", ".FBX", ".jpg", ".tif", ".flare", ".fbx", ".mp3", ".playable", ".gif", ".aiff", ".wav", ".overrideController", ".bytes", ".spine", }; m_findFilesPath = m_findFilesPath.Where(s => lstCheckWithExt.Contains(Path.GetExtension(s).ToLower())).ToList(); foreach (var eachCheckFile in m_findFilesPath) { CustomFileInfoCheckFile customFileInfoCheckFile = new CustomFileInfoCheckFile(); customFileInfoCheckFile.strFileName = eachCheckFile; customFileInfoCheckFile.strGuid = CommonUtils.GetGuidFromFile(eachCheckFile); m_stackCheckFileInfos.Push(customFileInfoCheckFile); } // 读取文件从硬盘到内存. 这里如果不做过滤 会把所有文件都读入. foreach (var eachFindFile in findFiles) { CustomFileInfoBeenDep customFileInfoBeenDep = new CustomFileInfoBeenDep(); customFileInfoBeenDep.strFileName = eachFindFile; customFileInfoBeenDep.strFileContent = File.ReadAllText(eachFindFile); m_stackDepFileInfos.Push(customFileInfoBeenDep); } // 获得单个Guid // m_handleGuid = CommonUtils.GetGuidFromFile(m_filePath); // Console.WriteLine("Check Here"); // 自定义线程个数 12-24个 测试花费时间. int nThreadNum = m_ThreadNum; int cutDepNum = m_stackDepFileInfos.Count / nThreadNum; // int cutCheckNum = m_stackCheckFileInfos.Count / nThreadNum; for (int i = 0; i < nThreadNum; i++) { Thread eachThread = new Thread(new ParameterizedThreadStart(TheadEachRun)); lstThreads.Add(eachThread); ThreadHandleInfo threadHandleInfo = new ThreadHandleInfo(); threadHandleInfo.threadName = "threadID-" + i.ToString(); bool isLastThread = (i == (nThreadNum - 1)); // 进行切割. // threadHandleInfo.inputCheckData = CutCheckTasks(m_stackCheckFileInfos, cutCheckNum, isLastThread); // 这里做个深度拷贝 为每个线程准备一个额外的inputCheckData. threadHandleInfo.inputCheckData = m_stackCheckFileInfos.ToList(); threadHandleInfo.dicFileDeps = m_assetFileRef.dicAssetFileRefs; threadHandleInfo.inputDepData = CutDepTasks(m_stackDepFileInfos, cutDepNum, isLastThread); eachThread.Start(threadHandleInfo); } for (int i = 0; i < nThreadNum; i++) { lstThreads[i].Join(); } // 打印出结果. // m_lstResults // Console.WriteLine("Check Result"); // 直接把对象序列化为字符串. string strResult = JsonConvert.SerializeObject(m_assetFileRef.dicAssetFileRefs, Formatting.Indented); CommonUtils.WriteFile(m_outputPath, strResult); }