private static void UpdateTranslationFiles(TranslationProject project, BackgroundWorker worker) { GameFileContainer[] containers = project.Game.GetContainers(project.InstallationPath); foreach (GameFileContainer container in containers) { if (worker.CancellationPending) { worker.ReportProgress(0, "CANCELADO"); throw new UserCancelException(); } TranslationFileContainer translationContainer = project.FileContainers.FirstOrDefault(x => x.Path == container.Path && x.Type == container.Type); var addNewContainer = false; var addedFiles = 0; if (translationContainer == null) { translationContainer = new TranslationFileContainer(container.Path, container.Type); addNewContainer = true; } string extractionContainerPath = Path.Combine(project.ContainersFolder, translationContainer.Id); Directory.CreateDirectory(extractionContainerPath); string containerPath = Path.GetFullPath(Path.Combine(project.InstallationPath, container.Path)); worker.ReportProgress(0, $"Procesando {container.Path}..."); if (container.Type == ContainerType.CompressedFile) { if (File.Exists(containerPath)) { if (addNewContainer) { project.Game.PreprocessContainer(translationContainer, containerPath, extractionContainerPath); project.Game.ExtractFile(containerPath, extractionContainerPath); } foreach (GameFileSearch fileSearch in container.FileSearches) { worker.ReportProgress(0, $"Buscando {fileSearch.RelativePath}\\{fileSearch.SearchPattern}..."); string[] foundFiles = fileSearch.GetFiles(extractionContainerPath); #if DEBUG foreach (string f in foundFiles) #else Parallel.ForEach(foundFiles, f => #endif { string relativePath = PathHelper.GetRelativePath(extractionContainerPath, Path.GetFullPath(f)); Type type = fileSearch.FileType; TranslationFile translationFile = translationContainer.Files.FirstOrDefault(x => x.RelativePath == relativePath); if (translationFile == null) { ConstructorInfo constructorInfo = type.GetConstructor(new[] {typeof(string), typeof(string), typeof(string), typeof(Encoding)}); if (constructorInfo != null) { translationFile = (TranslationFile) constructorInfo.Invoke(new object[] {project.Game.Name, f, project.ChangesFolder, project.Game.FileEncoding}); } else { constructorInfo = type.GetConstructor(new[] {typeof(string), typeof(string), typeof(string)}); if (constructorInfo != null) { translationFile = (TranslationFile) constructorInfo.Invoke(new object[] {project.Game.Name, f, project.ChangesFolder}); } else { translationFile = new TranslationFile(project.Game.Name, f, project.ChangesFolder); } } translationFile.RelativePath = relativePath; if (translationFile.Type == FileType.TextFile) { if (translationFile.SubtitleCount > 0) { translationContainer.AddFile(translationFile); addedFiles++; } } else { translationContainer.AddFile(translationFile); addedFiles++; } } #if DEBUG } #else }); #endif } project.Game.PostprocessContainer(translationContainer, containerPath, extractionContainerPath); worker.ReportProgress(0, $"{addedFiles} ficheros encontrados y añadidos"); } else { worker.ReportProgress(0, $"ERROR: No existe el fichero comprimido {containerPath}"); continue; } } else { project.Game.PreprocessContainer(translationContainer, containerPath, extractionContainerPath); foreach (GameFileSearch fileSearch in container.FileSearches) { worker.ReportProgress(0, $"Buscando {fileSearch.RelativePath}\\{fileSearch.SearchPattern}..."); string[] foundFiles = fileSearch.GetFiles(containerPath); #if DEBUG foreach (string f in foundFiles) #else Parallel.ForEach(foundFiles, f => #endif { string relativePath = PathHelper.GetRelativePath(containerPath, Path.GetFullPath(f)); string destinationFileName = Path.GetFullPath(Path.Combine(extractionContainerPath, relativePath)); string destPath = Path.GetDirectoryName(destinationFileName); Directory.CreateDirectory(destPath); if (!File.Exists(destinationFileName)) { File.Copy(f, destinationFileName); } Type type = fileSearch.FileType; TranslationFile translationFile = translationContainer.Files.FirstOrDefault(x => x.RelativePath == relativePath); if (translationFile == null) { ConstructorInfo constructorInfo = type.GetConstructor(new[] {typeof(string), typeof(string), typeof(string), typeof(Encoding)}); if (constructorInfo != null) { translationFile = (TranslationFile)constructorInfo.Invoke(new object[]{project.Game.Name, destinationFileName, project.ChangesFolder, project.Game.FileEncoding}); } else { constructorInfo = type.GetConstructor(new[] {typeof(string), typeof(string), typeof(string)}); if (constructorInfo != null) { translationFile = (TranslationFile)constructorInfo.Invoke(new object[]{project.Game.Name, destinationFileName, project.ChangesFolder}); } else { translationFile = new TranslationFile(project.Game.Name, destinationFileName, project.ChangesFolder); } } translationFile.RelativePath = relativePath; if (translationFile.Type == FileType.TextFile) { if (translationFile.SubtitleCount > 0) { translationContainer.AddFile(translationFile); addedFiles++; } } else { translationContainer.AddFile(translationFile); addedFiles++; } } #if DEBUG } #else }); #endif project.Game.PostprocessContainer(translationContainer, containerPath, extractionContainerPath); worker.ReportProgress(0, $"{addedFiles} ficheros encontrados y añadidos"); } } if (addNewContainer && translationContainer.Files.Count > 0) { project.FileContainers.Add(translationContainer); } } }
public static TranslationProject Load(string path, PluginManager pluginManager, BackgroundWorker worker) { var types = new Dictionary<string, Type>(); var searchNewFiles = false; using (var fs = new FileStream(path, FileMode.Open)) using (var input = new ExtendedBinaryReader(fs, Encoding.UTF8)) { var result = new TranslationProject(); string gameId = input.ReadString(); IGame game = pluginManager.GetGame(gameId); result.Game = game ?? throw new Exception("No existe un plugin para cargar este fichero."); int pluginVersion = input.ReadInt32(); if (pluginVersion > game.Version) { throw new Exception("No coincide la versión del plugin instalado con la versión del que creó esta traducción."); } if (pluginVersion < game.Version) { searchNewFiles = true; } string installPath = input.ReadString(); if (!Directory.Exists(installPath)) { throw new Exception($"No se encuentra la carpeta de instalación: {installPath}"); } result.InstallationPath = installPath; result.WorkPath = Path.GetDirectoryName(path); int containersCount = input.ReadInt32(); for (var i = 0; i < containersCount; i++) { string containerId = input.ReadString(); string containerPath = input.ReadString(); var containerType = (ContainerType)input.ReadInt32(); var container = new TranslationFileContainer(containerId, containerPath, containerType); int fileCount = input.ReadInt32(); for (var j = 0; j < fileCount; j++) { string fileId = input.ReadString(); string filePath = input.ReadString(); string fileRelativePath = input.ReadString(); string fileName = input.ReadString(); string typeString = input.ReadString(); Type type = GetType(typeString, types); TranslationFile file; ConstructorInfo constructorInfo = type.GetConstructor(new[] {typeof(string), typeof(string), typeof(string), typeof(Encoding)}); if (constructorInfo != null) { file = (TranslationFile)constructorInfo.Invoke(new object[]{game.Name, filePath, result.ChangesFolder, result.Game.FileEncoding}); } else { constructorInfo = type.GetConstructor(new[] {typeof(string), typeof(string), typeof(string)}); if (constructorInfo != null) { file = (TranslationFile)constructorInfo.Invoke(new object[]{game.Name, filePath, result.ChangesFolder}); } else { file = new TranslationFile(game.Name, filePath, result.ChangesFolder); } } file.Id = fileId; file.Name = fileName; file.RelativePath = fileRelativePath; container.AddFile(file); } result.FileContainers.Add(container); } if (searchNewFiles) { UpdateTranslationFiles(result, worker); } return result; } }
private static void UpdateTranslationFiles(TranslationProject project, BackgroundWorker worker) { var containers = project.Game.GetContainers(project.InstallationPath); foreach (var container in containers) { if (worker.CancellationPending) { worker.ReportProgress(0, "CANCELADO"); throw new UserCancelException(); } var translationContainer = project.FileContainers.FirstOrDefault(x => x.Path == container.Path && x.Type == container.Type); var addNewContainer = false; var addedFiles = 0; if (translationContainer == null) { translationContainer = new TranslationFileContainer(container.Path, container.Type); addNewContainer = true; } var extractionContainerPath = Path.Combine(project.ContainersFolder, translationContainer.Id); Directory.CreateDirectory(extractionContainerPath); var containerPath = Path.GetFullPath(Path.Combine(project.InstallationPath, container.Path)); worker.ReportProgress(0, $"Procesando {container.Path}..."); if (container.Type == ContainerType.CompressedFile) { if (File.Exists(containerPath)) { if (addNewContainer) { project.Game.PreprocessContainer(translationContainer, containerPath, extractionContainerPath); project.Game.ExtractFile(containerPath, extractionContainerPath); } foreach (var fileSearch in container.FileSearches) { worker.ReportProgress(0, $"Buscando {fileSearch.RelativePath}\\{fileSearch.SearchPattern}..."); var foundFiles = fileSearch.GetFiles(extractionContainerPath); foreach (var f in foundFiles) { var relativePath = PathHelper.GetRelativePath(extractionContainerPath, Path.GetFullPath(f)); var type = fileSearch.FileType; var translationFile = translationContainer.Files.FirstOrDefault(x => x.RelativePath == relativePath); if (translationFile == null) { try { translationFile = (TranslationFile)Activator.CreateInstance(type, f, project.ChangesFolder, project.Game.FileEncoding); } catch (MissingMethodException e) { translationFile = (TranslationFile)Activator.CreateInstance(type, f, project.ChangesFolder); } catch (Exception e) { translationFile = new TranslationFile(f, project.ChangesFolder); } translationFile.RelativePath = relativePath; if (translationFile.Type == FileType.TextFile) { if (translationFile.SubtitleCount > 0) { translationContainer.AddFile(translationFile); addedFiles++; } } else { translationContainer.AddFile(translationFile); addedFiles++; } } } } project.Game.PostprocessContainer(translationContainer, containerPath, extractionContainerPath); worker.ReportProgress(0, $"{addedFiles} ficheros encontrados y añadidos"); } else { worker.ReportProgress(0, $"ERROR: No existe el fichero comprimido {containerPath}"); continue; } } else { project.Game.PreprocessContainer(translationContainer, containerPath, extractionContainerPath); foreach (var fileSearch in container.FileSearches) { worker.ReportProgress(0, $"Buscando {fileSearch.RelativePath}\\{fileSearch.SearchPattern}..."); var foundFiles = fileSearch.GetFiles(containerPath); foreach (var f in foundFiles) { var relativePath = PathHelper.GetRelativePath(containerPath, Path.GetFullPath(f)); var destinationFileName = Path.GetFullPath(Path.Combine(extractionContainerPath, relativePath)); var destPath = Path.GetDirectoryName(destinationFileName); Directory.CreateDirectory(destPath); if (!File.Exists(destinationFileName)) { File.Copy(f, destinationFileName); } var type = fileSearch.FileType; var translationFile = translationContainer.Files.FirstOrDefault(x => x.RelativePath == relativePath); if (translationFile == null) { try { translationFile = (TranslationFile)Activator.CreateInstance(type, destinationFileName, project.ChangesFolder, project.Game.FileEncoding); } catch (MissingMethodException e) { translationFile = (TranslationFile)Activator.CreateInstance(type, destinationFileName, project.ChangesFolder); } catch (Exception e) { translationFile = new TranslationFile(destinationFileName, project.ChangesFolder); } translationFile.RelativePath = relativePath; if (translationFile.Type == FileType.TextFile) { if (translationFile.SubtitleCount > 0) { translationContainer.AddFile(translationFile); addedFiles++; } } else { translationContainer.AddFile(translationFile); addedFiles++; } } } project.Game.PostprocessContainer(translationContainer, containerPath, extractionContainerPath); worker.ReportProgress(0, $"{addedFiles} ficheros encontrados y añadidos"); } } if (addNewContainer && translationContainer.Files.Count > 0) { project.FileContainers.Add(translationContainer); } } }