private ResourceHandlingState ReadStateFile(string stateFile) { try { if (!String.IsNullOrEmpty(stateFile) && File.Exists(stateFile)) { using (FileStream fs = new FileStream(stateFile, FileMode.Open)) { BinaryFormatter formatter = new BinaryFormatter(); object deserializedObject = formatter.Deserialize(fs); ResourceHandlingState state = deserializedObject as ResourceHandlingState; if (state == null && deserializedObject != null) { Log.LogMessage(MessageImportance.Normal, Resources.Message_UnspecifiedStateFileCorrupted, stateFile); } return(state); } } else { return(null); } } catch (Exception e) { Log.LogMessage(MessageImportance.Low, Resources.Message_UnspecifiedReadStateFile, e.Message); return(null); } }
private void WriteStateFile(string stateFile, ResourceHandlingState state) { try { if (stateFile != null && stateFile.Length > 0) { using (FileStream fs = new FileStream(stateFile, FileMode.Create)) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(fs, state); } } } catch (Exception e) { Log.LogMessage(MessageImportance.Low, Resources.Message_UnspecifiedSaveStateFile, e.Message); } }
public override bool Execute() { ReswFileList = null; UnprocessedAssemblyList = null; List <ITaskItem> unprocessedAssemblyList = new List <ITaskItem>(); List <ITaskItem> reswList = new List <ITaskItem>(); _state = ReadStateFile(StateFile); if (_state == null) { _state = new ResourceHandlingState(); } _state.SetLogger(Log); using (_host = new PeReader.DefaultHost()) { try { // Separate main assemblies and satellite assemblies so main assemblies get processed first _mainAssemblies = new List <ITaskItem>(); _satelliteAssemblies = new List <ITaskItem>(); _processedAssemblies = new HashSet <String>(StringComparer.OrdinalIgnoreCase); foreach (ITaskItem item in AssemblyList) { if (_processedAssemblies.Contains(item.ItemSpec)) { continue; } _processedAssemblies.Add(item.ItemSpec); if (item.ItemSpec.EndsWith(".dll", StringComparison.OrdinalIgnoreCase)) { if (item.ItemSpec.EndsWith(".resources.dll", StringComparison.OrdinalIgnoreCase)) { _satelliteAssemblies.Add(item); } else { _mainAssemblies.Add(item); } } } foreach (ITaskItem assemblyFilePath in _mainAssemblies.Concat(_satelliteAssemblies)) { List <ResWInfo> resWInfoList = null; bool containsFrameworkResources = false; if (!_state.IsUpToDate(assemblyFilePath.ItemSpec, out containsFrameworkResources, out resWInfoList)) { resWInfoList = ExtractAssemblyResWList(assemblyFilePath.ItemSpec, out containsFrameworkResources); if (resWInfoList != null) { FileInfo fiAssembly = new FileInfo(assemblyFilePath.ItemSpec); _state.Save(assemblyFilePath.ItemSpec, fiAssembly.LastWriteTimeUtc, containsFrameworkResources, resWInfoList); } } if (resWInfoList != null) { foreach (ResWInfo reswInfo in resWInfoList) { TaskItem newTaskItem = new TaskItem(reswInfo.ResWPath); newTaskItem.SetMetadata("ResourceIndexName", reswInfo.ResourceIndexName); if (!String.IsNullOrEmpty(reswInfo.NeutralResourceLanguage)) { newTaskItem.SetMetadata("NeutralResourceLanguage", reswInfo.NeutralResourceLanguage); } if (!containsFrameworkResources) { newTaskItem.SetMetadata("OriginalItemSpec", reswInfo.ResWPath); // Original GenerateResource behavior creates this additional metadata item on processed non-framework assemblies reswList.Add(newTaskItem); } else if (!SkipFrameworkResources) { reswList.Add(newTaskItem); } } } } UnprocessedAssemblyList = unprocessedAssemblyList.ToArray(); // For now this list will always be empty ReswFileList = reswList.ToArray(); WriteStateFile(StateFile, _state); } catch (Exception e) { Log.LogError(Resources.Error_ResourceExtractionFailed, e.Message); return(false); } } return(true); }
public override bool Execute() { ReswFileList = null; UnprocessedAssemblyList = null; List <ITaskItem> unprocessedAssemblyList = new List <ITaskItem>(); List <ITaskItem> reswList = new List <ITaskItem>(); _state = ReadStateFile(StateFile); if (_state == null) { _state = new ResourceHandlingState(); } _state.SetLogger(Log); using (_host = new PeReader.DefaultHost()) { try { ITaskItem firstNonFrameworkAssembly = null; foreach (ITaskItem assemblyFilePath in AssemblyList) { string reswPath = null; bool containsResources = false; if (!_state.IsUpToDate(assemblyFilePath.ItemSpec, out reswPath) || !IsAtOutputFolder(reswPath)) { reswPath = ExtractFrameworkAssemblyResW(assemblyFilePath.ItemSpec, out containsResources); if (reswPath != null) { FileInfo fiAssembly = new FileInfo(assemblyFilePath.ItemSpec); FileInfo fiResW = new FileInfo(reswPath); _state.Save(assemblyFilePath.ItemSpec, reswPath, fiAssembly.LastWriteTimeUtc, fiResW.LastWriteTimeUtc); } } if (reswPath == null) { if (containsResources) { unprocessedAssemblyList.Add(assemblyFilePath); } if (unprocessedAssemblyList.Count == 0) { firstNonFrameworkAssembly = assemblyFilePath; } } else { TaskItem newTaskItem = new TaskItem(reswPath); newTaskItem.SetMetadata("NeutralResourceLanguage", "en-US"); newTaskItem.SetMetadata("ResourceIndexName", Path.GetFileNameWithoutExtension(reswPath)); reswList.Add(newTaskItem); } } UnprocessedAssemblyList = unprocessedAssemblyList.ToArray(); if (!SkipFrameworkResources) { ReswFileList = reswList.ToArray(); } // we make sure unprocessedAssemblyList has at least one item if ReswFileList is empty to avoid having _GeneratePrisForPortableLibraries // repopulate the assembly list and reprocess them if ((ReswFileList == null || ReswFileList.Length == 0) && UnprocessedAssemblyList.Length == 0 && firstNonFrameworkAssembly != null) { UnprocessedAssemblyList = new ITaskItem[1] { firstNonFrameworkAssembly }; } WriteStateFile(StateFile, _state); } catch (Exception e) { Log.LogError(Resources.Error_ResourceExtractionFailed, e.Message); return(false); } } return(true); }