protected void ReplaceTextureFile(string newFilePath) { var vm = CollectionViewSource.GetDefaultView(OpenFiles).CurrentItem as EdataFileViewModel; var destTgvFile = vm?.FilesCollectionView.CurrentItem as EdataContentFile; if (destTgvFile == null) { return; } var tgvReader = new TgvReader(); var data = vm.EdataManager.GetRawData(destTgvFile); var tgv = tgvReader.Read(data); var dispatcher = Dispatcher.CurrentDispatcher; Action <string> report = msg => StatusText = msg; var s = new Task(() => { try { dispatcher.Invoke(() => IsUIBusy = true); dispatcher.Invoke(report, $"Replacing {destTgvFile.Path}..."); byte[] sourceDds = File.ReadAllBytes(newFilePath); dispatcher.Invoke(report, "Converting DDS to TGV file format..."); var ddsReader = new TgvDDSReader(); var sourceTgvFile = ddsReader.ReadDDS(sourceDds); byte[] sourceTgvRawData; using (var tgvwriterStream = new MemoryStream()) { var tgvWriter = new TgvWriter(); tgvWriter.Write(tgvwriterStream, sourceTgvFile, tgv.SourceChecksum, tgv.IsCompressed); sourceTgvRawData = tgvwriterStream.ToArray(); } dispatcher.Invoke(report, "Replacing file in edata container..."); vm.EdataManager.ReplaceFile(destTgvFile, sourceTgvRawData); vm.LoadFile(vm.LoadedFile); dispatcher.Invoke(report, "Ready"); } catch (Exception ex) { dispatcher.Invoke(report, $"Replacing failed {ex.Message}"); Trace.TraceError("Unhandeled exception in Thread occoured: {0}", ex.ToString()); } finally { dispatcher.Invoke(() => IsUIBusy = false); } }); s.Start(); }
protected void ReplaceTextureExecute(object obj) { var vm = CollectionViewSource.GetDefaultView(OpenFiles).CurrentItem as EdataFileViewModel; if (vm == null) { return; } var destTgvFile = vm.FilesCollectionView.CurrentItem as EdataContentFile; if (destTgvFile == null) { return; } var tgvReader = new TgvReader(); var data = vm.EdataManager.GetRawData(destTgvFile); var tgv = tgvReader.Read(data); Settings settings = SettingsManager.Load(); var openfDlg = new OpenFileDialog { DefaultExt = ".dds", Multiselect = false, Filter = "DDS files (.dds)|*.dds" }; if (File.Exists(settings.LastOpenFolder)) { openfDlg.InitialDirectory = settings.LastOpenFolder; } if (openfDlg.ShowDialog().Value) { settings.LastOpenFolder = new FileInfo(openfDlg.FileName).DirectoryName; SettingsManager.Save(settings); var dispatcher = Dispatcher.CurrentDispatcher; Action <string> report = msg => StatusText = msg; var s = new Task(() => { try { dispatcher.Invoke(() => IsUIBusy = true); dispatcher.Invoke(report, string.Format("Replacing {0}...", destTgvFile.Path)); byte[] sourceDds = File.ReadAllBytes(openfDlg.FileName); dispatcher.Invoke(report, "Converting DDS to TGV file format..."); var ddsReader = new TgvDDSReader(); var sourceTgvFile = ddsReader.ReadDDS(sourceDds); byte[] sourceTgvRawData; using (var tgvwriterStream = new MemoryStream()) { var tgvWriter = new TgvWriter(); tgvWriter.Write(tgvwriterStream, sourceTgvFile, tgv.SourceChecksum, tgv.IsCompressed); sourceTgvRawData = tgvwriterStream.ToArray(); } dispatcher.Invoke(report, "Replacing file in edata container..."); vm.EdataManager.ReplaceFile(destTgvFile, sourceTgvRawData); vm.LoadFile(vm.LoadedFile); } catch (Exception ex) { Trace.TraceError("Unhandeled exception in Thread occoured: {0}", ex.ToString()); } finally { dispatcher.Invoke(report, "Ready"); dispatcher.Invoke(() => IsUIBusy = false); } }); s.Start(); } }