public void SaveObject(string filename, object configuration) { if (maps == null || !maps.Any()) { return; } DdsSaveConfig config = configuration as DdsSaveConfig ?? new DdsSaveConfig(FileFormat.Unknown, 0, 0, false, false); FileFormat format; MipMap mipMap = maps.Where(mm => mm.uncompressedData != null && mm.uncompressedData.Length > 0).OrderByDescending(mm => mm.sizeX > mm.sizeY ? mm.sizeX : mm.sizeY).FirstOrDefault(); if (mipMap == null) { return; } Stream memory = buildDdsImage(maps.IndexOf(mipMap), out format); if (memory == null) { return; } DdsFile ddsImage = new DdsFile(GetObjectStream()); FileStream ddsStream = new FileStream(filename, FileMode.Create); config.FileFormat = format; ddsImage.Save(ddsStream, config); ddsStream.Close(); memory.Close(); }
public void SaveObject(string filename, object configuration) { FileStream ddsStream = null; try { if (maps == null || !maps.Any()) { return; } DdsSaveConfig config = configuration as DdsSaveConfig ?? new DdsSaveConfig(FileFormat.Unknown, 0, 0, false, false); config.FileFormat = parsedImageFormat; //parse uncompressed image DdsFile ddsImage = new DdsFile(GetObjectStream()); ddsStream = new FileStream(filename, FileMode.Create); ddsImage.Save(ddsStream, config); ddsStream.Close(); ddsStream.Dispose(); } finally { if (ddsStream != null) { ddsStream.Dispose(); } } }
public static void importTexture(GpkExport export, string file) { var texture2d = export.Payload as Texture2D; var image = new DdsFile(); var config = new DdsSaveConfig(texture2d.GetFormat(), 0, 0, false, false); image.Load(file); if (image.MipMaps.Count == 0 || Settings.Default.GenerateMipMaps) { image.GenerateMipMaps(); } texture2d.maps = new List <MipMap>(); foreach (DdsMipMap mipMap in image.MipMaps.OrderByDescending(mip => mip.Width)) { byte[] outputData = image.WriteMipMap(mipMap, config); var textureMipMap = new MipMap(); textureMipMap.compFlag = (int)CompressionTypes.LZO; textureMipMap.uncompressedData = outputData; textureMipMap.uncompressedSize = outputData.Length; textureMipMap.uncompressedSize_chunkheader = outputData.Length; textureMipMap.sizeX = mipMap.Width; textureMipMap.sizeY = mipMap.Height; textureMipMap.generateBlocks(); texture2d.maps.Add(textureMipMap); } }
private async Task onSaveObjectAsExecute() { VistaSaveFileDialog sfd = new VistaSaveFileDialog { FileName = export.NameTableIndex.Name, DefaultExt = export.DomainObject.FileExtension, Filter = $"{export.DomainObject.FileTypeDesc}|*{export.DomainObject.FileExtension}", Title = "Save Object As..." }; bool?result = sfd.ShowDialog(); if (!result.HasValue || !result.Value) { return; } int compressor = menuViewModel.IsCompressorClusterFit ? 0 : menuViewModel.IsCompressorRangeFit ? 1 : 2; int errorMetric = menuViewModel.IsErrorMetricPerceptual ? 0 : 1; DdsSaveConfig config = new DdsSaveConfig(FileFormat.Unknown, compressor, errorMetric, menuViewModel.IsWeightColorByAlpha, false); await export.DomainObject.SaveObject(sfd.FileName, config); }
public static void importTexture(GpkExport export, string file) { try { var texture2d = export.Payload as Texture2D; var image = new DdsFile(); var config = new DdsSaveConfig(texture2d.parsedImageFormat, 0, 0, false, false); image.Load(file); if (image.MipMaps.Count == 0 || CoreSettings.Default.GenerateMipMaps) { image.GenerateMipMaps(); } texture2d.maps = new List <MipMap>(); foreach (DdsMipMap mipMap in image.MipMaps.OrderByDescending(mip => mip.Width)) { byte[] outputData = image.WriteMipMap(mipMap, config); var textureMipMap = new MipMap(); textureMipMap.flags = (int)CompressionTypes.LZO; //textureMipMap.flags = 0; textureMipMap.uncompressedData = outputData; textureMipMap.uncompressedSize = outputData.Length; textureMipMap.uncompressedSize_chunkheader = outputData.Length; textureMipMap.sizeX = mipMap.Width; textureMipMap.sizeY = mipMap.Height; if (textureMipMap.flags != 0) { textureMipMap.generateBlocks(); } texture2d.maps.Add(textureMipMap); } int mipTailBaseIdx = (int)Math.Log(image.Width > image.Height ? image.Width : image.Height, 2); ((GpkIntProperty)export.GetProperty("MipTailBaseIdx")).SetValue(mipTailBaseIdx.ToString()); logger.Info("Imported image from {0}, size {1}x{2}, target format {3}, mipTailBaseIdx {4}", file, image.Width, image.Height, config.FileFormat, mipTailBaseIdx); } catch (Exception ex) { logger.Error(ex, "Failed to import texture"); logger.Error(ex); } }
public override async Task SaveObject(string filename, object configuration) { if (MipMaps == null || !MipMaps.Any()) { return; } DdsSaveConfig config = configuration as DdsSaveConfig ?? new DdsSaveConfig(FileFormat.Unknown, 0, 0, false, false); FileFormat format; DomainMipMap mipMap = MipMaps.Where(mm => mm.ImageData != null && mm.ImageData.Length > 0).OrderByDescending(mm => mm.Width > mm.Height ? mm.Width : mm.Height).FirstOrDefault(); if (mipMap == null) { return; } Stream memory = buildDdsImage(MipMaps.IndexOf(mipMap), out format); if (memory == null) { return; } DdsFile ddsImage = new DdsFile(memory); FileStream ddsStream = new FileStream(filename, FileMode.Create); config.FileFormat = format; await Task.Run(() => ddsImage.Save(ddsStream, config)); ddsStream.Close(); memory.Close(); }
private async Task rebuildExports() { Dictionary <ExportedObjectViewEntity, List <ExportedObjectViewEntity> > filesToMod = viewModel.ExportsTree?.Traverse(e => Path.HasExtension(e.Filename) && e.IsChecked) .GroupBy(e => e.Parent) .ToDictionary(g => g.Key, g => g.ToList()); if (filesToMod == null || !filesToMod.Any()) { return; } LoadProgressMessage message = new LoadProgressMessage { Text = "Rebuilding...", Total = filesToMod.Count }; foreach (KeyValuePair <ExportedObjectViewEntity, List <ExportedObjectViewEntity> > pair in filesToMod) { string gameFilename = $"{pair.Key.Filename.Replace(settings.ExportPath, null)}.upk"; DomainUpkFile file = allFiles.SingleOrDefault(f => f.GameFilename.Equals(gameFilename)); if (file == null) { continue; } DomainHeader header = await repository.LoadUpkFile(Path.Combine(settings.PathToGame, file.GameFilename)); await header.ReadHeaderAsync(null); message.Current++; foreach (ExportedObjectViewEntity entity in pair.Value) { DomainExportTableEntry export = header.ExportTable.SingleOrDefault(ex => ex.NameTableIndex.Name.Equals(Path.GetFileNameWithoutExtension(entity.Filename), StringComparison.CurrentCultureIgnoreCase)); if (export == null) { continue; } await export.ParseDomainObject(header, false, false); int compressor = menuViewModel.IsCompressorClusterFit ? 0 : menuViewModel.IsCompressorRangeFit ? 1 : 2; int errorMetric = menuViewModel.IsErrorMetricPerceptual ? 0 : 1; FileFormat fileFormat = menuViewModel.IsDdsDefault ? FileFormat.Unknown : menuViewModel.IsDdsFormat1 ? FileFormat.DXT1 : menuViewModel.IsDdsFormat5 ? FileFormat.DXT5 : FileFormat.A8R8G8B8; DdsSaveConfig config = new DdsSaveConfig(fileFormat, compressor, errorMetric, menuViewModel.IsWeightColorByAlpha, false); await export.DomainObject.SetObject(entity.Filename, header.NameTable, config); message.StatusText = entity.Filename; messenger.Send(message); } string directory = Path.Combine(settings.PathToGame, Path.GetDirectoryName(file.GameFilename), "mod"); string filename = Path.Combine(directory, Path.GetFileName(file.GameFilename)); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } await repository.SaveUpkFile(header, filename); DomainUpkFile upkFile = new DomainUpkFile { GameFilename = filename.Replace(settings.PathToGame, null), FileSize = new FileInfo(filename).Length, Package = GetFileNameWithoutExtension(filename).ToLowerInvariant() }; messenger.Send(new ModFileBuiltMessage { UpkFile = upkFile }); } message.IsComplete = true; message.StatusText = null; messenger.Send(message); }
private async Task exportFileObjects(List <DomainUpkFile> files) { LoadProgressMessage message = new LoadProgressMessage { Text = "Exporting...", Total = files.Count }; int compressor = menuViewModel.IsCompressorClusterFit ? 0 : menuViewModel.IsCompressorRangeFit ? 1 : 2; int errorMetric = menuViewModel.IsErrorMetricPerceptual ? 0 : 1; DdsSaveConfig config = new DdsSaveConfig(FileFormat.Unknown, compressor, errorMetric, menuViewModel.IsWeightColorByAlpha, false); foreach (DomainUpkFile file in files) { FileViewEntity fileEntity = viewModel.Files.Single(fe => fe.Id == file.Id); string directory = Path.Combine(settings.ExportPath, Path.GetDirectoryName(file.GameFilename), Path.GetFileNameWithoutExtension(file.GameFilename)); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } DomainHeader header = file.Header; if (header == null) { try { header = await repository.LoadUpkFile(Path.Combine(settings.PathToGame, file.GameFilename)); await Task.Run(() => header.ReadHeaderAsync(null)); } catch (Exception ex) { messenger.Send(new ApplicationErrorMessage { HeaderText = "Error Loading UPK File", ErrorMessage = $"Filename: {file.GameFilename}", Exception = ex }); fileEntity.IsErrored = true; continue; } } message.Current++; foreach (DomainExportTableEntry export in header.ExportTable) { if (export.DomainObject == null) { try { await export.ParseDomainObject(header, false, false); } catch (Exception ex) { messenger.Send(new ApplicationErrorMessage { HeaderText = "Error Parsing Object", ErrorMessage = $"Filename: {header.Filename}\nExport Name: {export.NameTableIndex.Name}\nType: {export.TypeReferenceNameIndex.Name}", Exception = ex }); fileEntity.IsErrored = true; continue; } } if (!export.DomainObject.IsExportable) { continue; } string filename = Path.Combine(directory, $"{export.NameTableIndex.Name}{export.DomainObject.FileExtension}"); message.StatusText = filename; messenger.Send(message); await export.DomainObject.SaveObject(filename, config); } file.Header = null; } message.IsComplete = true; message.StatusText = null; messenger.Send(message); }
public override async Task SetObject(string filename, List <DomainNameTableEntry> nameTable, object configuration) { DdsSaveConfig config = configuration as DdsSaveConfig ?? new DdsSaveConfig(FileFormat.Unknown, 0, 0, false, false); DdsFile image = await Task.Run(() => new DdsFile(filename)); bool skipFirstMip = false; int width = image.Width; int height = image.Height; if (MipMaps[0].ImageData == null || MipMaps[0].ImageData.Length == 0) { width *= 2; height *= 2; skipFirstMip = true; } DomainPropertyIntValue sizeX = PropertyHeader.GetProperty("SizeX").FirstOrDefault()?.Value as DomainPropertyIntValue; DomainPropertyIntValue sizeY = PropertyHeader.GetProperty("SizeY").FirstOrDefault()?.Value as DomainPropertyIntValue; sizeX?.SetPropertyValue(skipFirstMip ? width * 2 : width); sizeY?.SetPropertyValue(skipFirstMip ? height * 2 : height); DomainPropertyIntValue mipTailBaseIdx = PropertyHeader.GetProperty("MipTailBaseIdx").FirstOrDefault()?.Value as DomainPropertyIntValue; int indexSize = width > height ? width : height; mipTailBaseIdx?.SetPropertyValue((int)Math.Log(skipFirstMip ? indexSize * 2 : indexSize, 2)); DomainPropertyStringValue filePath = PropertyHeader.GetProperty("SourceFilePath").FirstOrDefault()?.Value as DomainPropertyStringValue; DomainPropertyStringValue fileTime = PropertyHeader.GetProperty("SourceFileTimestamp").FirstOrDefault()?.Value as DomainPropertyStringValue; filePath?.SetPropertyValue(filename); fileTime?.SetPropertyValue(File.GetLastWriteTime(filename).ToString("yyyy-MM-dd hh:mm:ss")); DomainPropertyByteValue pfFormat = PropertyHeader.GetProperty("Format").FirstOrDefault()?.Value as DomainPropertyByteValue; FileFormat imageFormat = FileFormat.Unknown; if (pfFormat != null) { imageFormat = DdsPixelFormat.ParseFileFormat(pfFormat.PropertyString); } if (imageFormat == FileFormat.Unknown) { throw new Exception($"Unknown DDS File Format ({pfFormat?.PropertyString ?? "Unknown"})."); } config.FileFormat = imageFormat; MipMaps.Clear(); if (skipFirstMip) { MipMaps.Add(new DomainMipMap { ImageData = null, Width = width, Height = height }); } image.GenerateMipMaps(4, 4); foreach (DdsMipMap mipMap in image.MipMaps.OrderByDescending(mip => mip.Width)) { MipMaps.Add(new DomainMipMap { ImageData = image.WriteMipMap(mipMap, config), Width = mipMap.Width, Height = mipMap.Height }); } }