public void SaveAll() { mustCancel = false; byte[] buffer = null; for (int i = 0; i < filesToSave.Length; i++) { fileIndex = i; var state = filesToSave[fileIndex]; OnWritingFile?.Invoke(this, new ModuleSaverWriteEventArgs(state.File, true)); fileProgress = null; switch (state.File.Type) { case SaveOptionsType.Module: Save((SaveModuleOptionsVM)state.File); break; case SaveOptionsType.Hex: Save((SaveHexOptionsVM)state.File, ref buffer); break; default: throw new InvalidOperationException(); } fileProgress = null; OnWritingFile?.Invoke(this, new ModuleSaverWriteEventArgs(state.File, false)); } fileIndex = filesToSave.Length; OnProgressUpdated?.Invoke(this, EventArgs.Empty); }
void Save(SaveModuleOptionsVM vm) { fileProgress = new ModuleFileProgress(); var opts = vm.CreateWriterOptions(); opts.Listener = this; opts.Logger = this; var filename = vm.FileName; if (opts is NativeModuleWriterOptions) { ((ModuleDefMD)vm.Module).NativeWrite(filename, (NativeModuleWriterOptions)opts); } else { vm.Module.Write(filename, (ModuleWriterOptions)opts); } }
void Save(SaveHexOptionsVM hex, ref byte[] buffer) { var progress = new HexFileProgress(GetSize(hex.Document.StartOffset, hex.Document.EndOffset)); fileProgress = progress; if (buffer == null) { buffer = new byte[64 * 1024]; } try { if (File.Exists(hex.FileName)) { File.Delete(hex.FileName); } using (var stream = File.OpenWrite(hex.FileName)) { ulong offs = hex.Document.StartOffset; ulong end = hex.Document.EndOffset; while (offs <= end) { ThrowIfCanceled(); ulong bytesLeft = GetSize(offs, end); int bytesToWrite = bytesLeft > (ulong)buffer.Length ? buffer.Length : (int)bytesLeft; hex.Document.Read(offs, buffer, 0, bytesToWrite); stream.Write(buffer, 0, bytesToWrite); progress.BytesWritten += (ulong)bytesToWrite; NotifyProgressUpdated(); ulong nextOffs = offs + (ulong)bytesToWrite; if (nextOffs < offs) { break; } offs = nextOffs; } } } catch { DeleteFile(hex.FileName); throw; } }
void Save(SaveModuleOptionsVM vm) { fileProgress = new ModuleFileProgress(); var opts = vm.CreateWriterOptions(); opts.ProgressUpdated += ModuleWriter_ProgressUpdated; opts.Logger = this; // Make sure the order of the interfaces don't change, see https://github.com/dotnet/roslyn/issues/3905 opts.MetadataOptions.Flags |= MetadataFlags.RoslynSortInterfaceImpl; var filename = vm.FileName; if (opts is NativeModuleWriterOptions) { ((ModuleDefMD)vm.Module).NativeWrite(filename, (NativeModuleWriterOptions)opts); } else { vm.Module.Write(filename, (ModuleWriterOptions)opts); } }
void Save(SaveHexOptionsVM hex, ref byte[] buffer) { var progress = new HexFileProgress(GetSize(hex.Buffer.Span.Start, hex.Buffer.Span.End)); fileProgress = progress; if (buffer == null) { buffer = new byte[64 * 1024]; } try { if (File.Exists(hex.FileName)) { File.Delete(hex.FileName); } using (var stream = File.OpenWrite(hex.FileName)) { var pos = hex.Buffer.Span.Start; var end = hex.Buffer.Span.End; while (pos < end) { ThrowIfCanceled(); ulong bytesLeft = GetSize(pos, end); int bytesToWrite = bytesLeft > (ulong)buffer.Length ? buffer.Length : (int)bytesLeft; hex.Buffer.ReadBytes(pos, buffer, 0, bytesToWrite); stream.Write(buffer, 0, bytesToWrite); progress.BytesWritten += (ulong)bytesToWrite; NotifyProgressUpdated(); pos = pos + (ulong)bytesToWrite; } } } catch { DeleteFile(hex.FileName); throw; } }