From 4c8c3381de1f16e2bb2319bf1b8145a5d9ba9b36 Mon Sep 17 00:00:00 2001 From: 0xd4d Date: Tue, 22 Sep 2020 21:41:20 +0200 Subject: [PATCH] NRT updates --- .../DnlibDialogs/MethodSigCreatorVM.cs | 2 +- .../dnSpy.AsmEditor/Module/ModuleOptionsVM.cs | 4 +-- .../SaveModule/SaveModuleOptionsVM.cs | 4 +-- .../Impl/COMD/DmdComMetadataReader.cs | 2 ++ .../Impl/DmdDeclSecurityReader.cs | 2 ++ .../Impl/MD/DmdEcma335MetadataReader.cs | 2 ++ .../Exceptions/ExceptionsFileReader.cs | 32 +++++++++---------- dnSpy/dnSpy.Console/Program.cs | 6 ++-- .../TreeView/Resources/Deserializer.cs | 2 ++ .../Resources/SerializationUtilities.cs | 4 +++ .../SerializedResourceElementNode.cs | 2 ++ .../Decompiler/XmlDoc/XmlDocRenderer.cs | 5 +-- .../XmlDoc/XmlDocumentationProvider.cs | 15 +++++---- .../MSBuild/SettingsProjectFile.cs | 6 ++-- .../Dialog/AppSettingsPageImpl.cs | 28 ++++++++-------- .../DisassemblyCodeStyleAppSettingsPage.cs | 2 +- .../Documents/Tabs/DefaultDocumentList.cs | 16 +++++----- .../DocViewer/ToolTips/CodeToolTipWriter.cs | 16 +++++----- .../dnSpy/Extension/ExtensionConfigReader.cs | 2 +- dnSpy/dnSpy/Search/SearchControlVM.cs | 2 +- dnSpy/dnSpy/Settings/XmlSettingsReader.cs | 6 ++-- dnSpy/dnSpy/Settings/XmlSettingsWriter.cs | 2 +- dnSpy/dnSpy/Settings/XmlUtils.cs | 4 +-- dnSpy/dnSpy/Themes/Theme.cs | 3 +- dnSpy/dnSpy/Themes/ThemeService.cs | 2 +- 25 files changed, 93 insertions(+), 78 deletions(-) diff --git a/Extensions/dnSpy.AsmEditor/DnlibDialogs/MethodSigCreatorVM.cs b/Extensions/dnSpy.AsmEditor/DnlibDialogs/MethodSigCreatorVM.cs index 8bdd91f290..9530eb6375 100644 --- a/Extensions/dnSpy.AsmEditor/DnlibDialogs/MethodSigCreatorVM.cs +++ b/Extensions/dnSpy.AsmEditor/DnlibDialogs/MethodSigCreatorVM.cs @@ -166,7 +166,7 @@ public MethodSigCreatorVM(MethodSigCreatorOptions options) { if (!IsMethodSig) throw new InvalidOperationException(); CallingConvention = (CallingConvention & ~dnlib.DotNet.CallingConvention.Mask) | - (dnlib.DotNet.CallingConvention)(MethodCallingConv)MethodCallingConv.SelectedItem!; + (dnlib.DotNet.CallingConvention)(MethodCallingConv)MethodCallingConv!.SelectedItem!; }); if (!CanHaveSentinel) { MethodCallingConv.Items.RemoveAt(MethodCallingConv.GetIndex(DnlibDialogs.MethodCallingConv.VarArg)); diff --git a/Extensions/dnSpy.AsmEditor/Module/ModuleOptionsVM.cs b/Extensions/dnSpy.AsmEditor/Module/ModuleOptionsVM.cs index d875091c8d..75cf9978f8 100644 --- a/Extensions/dnSpy.AsmEditor/Module/ModuleOptionsVM.cs +++ b/Extensions/dnSpy.AsmEditor/Module/ModuleOptionsVM.cs @@ -428,10 +428,10 @@ public ModuleOptionsVM(ModuleDef module, ModuleOptions options, IDecompilerServi this.options = new ModuleOptions(); origOptions = options; ModuleKind = new EnumListVM(SaveModule.SaveModuleOptionsVM.moduleKindList, (a, b) => { - Characteristics = SaveModule.CharacteristicsHelper.GetCharacteristics(Characteristics, (dnlib.DotNet.ModuleKind)ModuleKind.SelectedItem!); + Characteristics = SaveModule.CharacteristicsHelper.GetCharacteristics(Characteristics, (dnlib.DotNet.ModuleKind)ModuleKind!.SelectedItem!); }); Machine = new EnumListVM(SaveModule.PEHeadersOptionsVM.machineList, (a, b) => { - Characteristics = SaveModule.CharacteristicsHelper.GetCharacteristics(Characteristics, (dnlib.PE.Machine)Machine.SelectedItem!); + Characteristics = SaveModule.CharacteristicsHelper.GetCharacteristics(Characteristics, (dnlib.PE.Machine)Machine!.SelectedItem!); }); Mvid = new NullableGuidVM(a => HasErrorUpdated()); EncId = new NullableGuidVM(a => HasErrorUpdated()); diff --git a/Extensions/dnSpy.AsmEditor/SaveModule/SaveModuleOptionsVM.cs b/Extensions/dnSpy.AsmEditor/SaveModule/SaveModuleOptionsVM.cs index 7730703a9d..37f01ec7d3 100644 --- a/Extensions/dnSpy.AsmEditor/SaveModule/SaveModuleOptionsVM.cs +++ b/Extensions/dnSpy.AsmEditor/SaveModule/SaveModuleOptionsVM.cs @@ -156,7 +156,7 @@ public SaveModuleOptionsVM(IDsDocument document) { ModuleKind = new EnumListVM(moduleKindList, (a, b) => { OnPropertyChanged(nameof(Extension)); - PEHeadersOptions.Subsystem.SelectedItem = GetSubsystem((ModuleKind)ModuleKind.SelectedItem!); + PEHeadersOptions.Subsystem.SelectedItem = GetSubsystem((ModuleKind)ModuleKind!.SelectedItem!); PEHeadersOptions.Characteristics = CharacteristicsHelper.GetCharacteristics(PEHeadersOptions.Characteristics ?? 0, (ModuleKind)ModuleKind.SelectedItem!); }); @@ -288,7 +288,7 @@ public PEHeadersOptionsVM(Machine defaultMachine, Subsystem defaultSubsystem) { this.defaultMachine = defaultMachine; this.defaultSubsystem = defaultSubsystem; Machine = new EnumListVM(machineList, (a, b) => { - Characteristics = CharacteristicsHelper.GetCharacteristics(Characteristics ?? 0, (dnlib.PE.Machine)Machine.SelectedItem!); + Characteristics = CharacteristicsHelper.GetCharacteristics(Characteristics ?? 0, (dnlib.PE.Machine)Machine!.SelectedItem!); }); TimeDateStamp = new NullableUInt32VM(a => HasErrorUpdated()); PointerToSymbolTable = new NullableUInt32VM(a => HasErrorUpdated()); diff --git a/Extensions/dnSpy.Debugger/dnSpy.Debugger.DotNet.Metadata/Impl/COMD/DmdComMetadataReader.cs b/Extensions/dnSpy.Debugger/dnSpy.Debugger.DotNet.Metadata/Impl/COMD/DmdComMetadataReader.cs index 765d90ef25..bf21e3be5c 100644 --- a/Extensions/dnSpy.Debugger/dnSpy.Debugger.DotNet.Metadata/Impl/COMD/DmdComMetadataReader.cs +++ b/Extensions/dnSpy.Debugger/dnSpy.Debugger.DotNet.Metadata/Impl/COMD/DmdComMetadataReader.cs @@ -1115,6 +1115,7 @@ DmdCustomAttributeData[] ReadSecurityAttributesCore(uint token) { return COMThread(() => ReadSecurityAttributesCore_COMThread(token)); } +#pragma warning disable SYSLIB0003 // SecurityAction internal DmdCustomAttributeData[] ReadSecurityAttributesCore_COMThread(uint token) { dispatcher.VerifyAccess(); var tokens = MDAPI.GetPermissionSetTokens(MetaDataImport, token); @@ -1166,6 +1167,7 @@ internal DmdCustomAttributeData[] ReadSecurityAttributesCore_COMThread(uint toke throw new InvalidOperationException(); return sas; } +#pragma warning restore SYSLIB0003 // SecurityAction internal DmdMarshalType? ReadFieldMarshalType_COMThread(int metadataToken, DmdModule module, IList? genericTypeArguments) { dispatcher.VerifyAccess(); diff --git a/Extensions/dnSpy.Debugger/dnSpy.Debugger.DotNet.Metadata/Impl/DmdDeclSecurityReader.cs b/Extensions/dnSpy.Debugger/dnSpy.Debugger.DotNet.Metadata/Impl/DmdDeclSecurityReader.cs index d6bbc2b9c9..47ad763a35 100644 --- a/Extensions/dnSpy.Debugger/dnSpy.Debugger.DotNet.Metadata/Impl/DmdDeclSecurityReader.cs +++ b/Extensions/dnSpy.Debugger/dnSpy.Debugger.DotNet.Metadata/Impl/DmdDeclSecurityReader.cs @@ -17,6 +17,8 @@ You should have received a copy of the GNU General Public License along with dnSpy. If not, see . */ +#pragma warning disable SYSLIB0003 // SecurityAction + using System; using System.Collections.Generic; using System.Diagnostics; diff --git a/Extensions/dnSpy.Debugger/dnSpy.Debugger.DotNet.Metadata/Impl/MD/DmdEcma335MetadataReader.cs b/Extensions/dnSpy.Debugger/dnSpy.Debugger.DotNet.Metadata/Impl/MD/DmdEcma335MetadataReader.cs index 5406f712b0..b93c94041a 100644 --- a/Extensions/dnSpy.Debugger/dnSpy.Debugger.DotNet.Metadata/Impl/MD/DmdEcma335MetadataReader.cs +++ b/Extensions/dnSpy.Debugger/dnSpy.Debugger.DotNet.Metadata/Impl/MD/DmdEcma335MetadataReader.cs @@ -622,6 +622,7 @@ DmdCustomAttributeData[] ReadCustomAttributesCore(Table table, uint rid) { protected override DmdCustomAttributeData[] ReadTypeDefSecurityAttributes(uint rid) => ReadSecurityAttributesCore(Table.TypeDef, rid); protected override DmdCustomAttributeData[] ReadMethodSecurityAttributes(uint rid) => ReadSecurityAttributesCore(Table.Method, rid); +#pragma warning disable SYSLIB0003 // SecurityAction DmdCustomAttributeData[] ReadSecurityAttributesCore(Table table, uint rid) { var ridList = Metadata.GetDeclSecurityRidList(table, rid); if (ridList.Count == 0) @@ -670,6 +671,7 @@ DmdCustomAttributeData[] ReadSecurityAttributesCore(Table table, uint rid) { throw new InvalidOperationException(); return sas; } +#pragma warning restore SYSLIB0003 // SecurityAction internal DmdMarshalType? ReadMarshalType(int metadataToken, DmdModule module, IList? genericTypeArguments) { if (!TablesStream.TryReadFieldMarshalRow(Metadata.GetFieldMarshalRid((Table)((uint)metadataToken >> 24), (uint)metadataToken & 0x00FFFFFF), out var row)) diff --git a/Extensions/dnSpy.Debugger/dnSpy.Debugger/Exceptions/ExceptionsFileReader.cs b/Extensions/dnSpy.Debugger/dnSpy.Debugger/Exceptions/ExceptionsFileReader.cs index 65aa3e9a92..549229cc21 100644 --- a/Extensions/dnSpy.Debugger/dnSpy.Debugger/Exceptions/ExceptionsFileReader.cs +++ b/Extensions/dnSpy.Debugger/dnSpy.Debugger/Exceptions/ExceptionsFileReader.cs @@ -35,31 +35,31 @@ public void Read(string filename) { return; var doc = XDocument.Load(filename, LoadOptions.None); var root = doc.Root; - if (root.Name == "Exceptions") { + if (root?.Name == "Exceptions") { foreach (var categoryDefElem in root.Elements("CategoryDef")) { - var name = (string)categoryDefElem.Attribute("Name"); - var displayName = (string)categoryDefElem.Attribute("DisplayName"); - var shortDisplayName = (string)categoryDefElem.Attribute("ShortDisplayName"); - var flagsAttr = (string)categoryDefElem.Attribute("Flags"); - if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(displayName) || string.IsNullOrWhiteSpace(shortDisplayName)) + var name = (string?)categoryDefElem.Attribute("Name"); + var displayName = (string?)categoryDefElem.Attribute("DisplayName"); + var shortDisplayName = (string?)categoryDefElem.Attribute("ShortDisplayName"); + var flagsAttr = (string?)categoryDefElem.Attribute("Flags"); + if (string2.IsNullOrWhiteSpace(name) || string2.IsNullOrWhiteSpace(displayName) || string2.IsNullOrWhiteSpace(shortDisplayName)) continue; var flags = ParseCategoryFlags(flagsAttr); CategoryDefinitions.Add(new DbgExceptionCategoryDefinition(flags, name, displayName, shortDisplayName)); } foreach (var exDefCollElem in root.Elements("ExceptionDefs")) { - var category = (string)exDefCollElem.Attribute("Category"); - if (string.IsNullOrWhiteSpace(category)) + var category = (string?)exDefCollElem.Attribute("Category"); + if (string2.IsNullOrWhiteSpace(category)) continue; foreach (var exDefElem in exDefCollElem.Elements("Exception")) { - var name = (string)exDefElem.Attribute("Name"); - var code = (string)exDefElem.Attribute("Code"); - string? description = (string)exDefElem.Attribute("Description"); - if (string.IsNullOrWhiteSpace(description)) + var name = (string?)exDefElem.Attribute("Name"); + var code = (string?)exDefElem.Attribute("Code"); + string? description = (string?)exDefElem.Attribute("Description"); + if (string2.IsNullOrWhiteSpace(description)) description = null; - var flagsAttr = (string)exDefElem.Attribute("Flags"); + var flagsAttr = (string?)exDefElem.Attribute("Flags"); DbgExceptionId id; if (code is null) { - if (string.IsNullOrWhiteSpace(name)) + if (string2.IsNullOrWhiteSpace(name)) continue; id = new DbgExceptionId(category, name); } @@ -95,7 +95,7 @@ public void Read(string filename) { } static readonly char[] flagsSeparators = new char[] { ',' }; - static DbgExceptionCategoryDefinitionFlags ParseCategoryFlags(string flagsAttr) { + static DbgExceptionCategoryDefinitionFlags ParseCategoryFlags(string? flagsAttr) { var flags = DbgExceptionCategoryDefinitionFlags.None; if (!(flagsAttr is null)) { foreach (var name in flagsAttr.Split(flagsSeparators, StringSplitOptions.RemoveEmptyEntries)) { @@ -109,7 +109,7 @@ static DbgExceptionCategoryDefinitionFlags ParseCategoryFlags(string flagsAttr) return flags; } - static DbgExceptionDefinitionFlags ParseExceptionFlags(string flagsAttr) { + static DbgExceptionDefinitionFlags ParseExceptionFlags(string? flagsAttr) { var flags = DbgExceptionDefinitionFlags.None; if (!(flagsAttr is null)) { foreach (var name in flagsAttr.Split(flagsSeparators, StringSplitOptions.RemoveEmptyEntries)) { diff --git a/dnSpy/dnSpy.Console/Program.cs b/dnSpy/dnSpy.Console/Program.cs index 149da960cf..3a0a0d5052 100644 --- a/dnSpy/dnSpy.Console/Program.cs +++ b/dnSpy/dnSpy.Console/Program.cs @@ -188,7 +188,7 @@ sealed class DnSpyDecompiler : IMSBuildProjectWriterLogger { readonly DecompilationContext decompilationContext; readonly ModuleContext moduleContext; readonly AssemblyResolver assemblyResolver; - readonly IBamlDecompiler bamlDecompiler; + readonly IBamlDecompiler? bamlDecompiler; readonly HashSet reservedOptions; #if NETCOREAPP readonly dnSpy.MainApp.NetCoreAssemblyLoader netCoreAssemblyLoader = new dnSpy.MainApp.NetCoreAssemblyLoader(System.Runtime.Loader.AssemblyLoadContext.Default); @@ -245,7 +245,7 @@ static IEnumerable GetLanguagesInAssembly(string asmName) { } } - static IBamlDecompiler TryLoadBamlDecompiler() => TryCreateType("dnSpy.BamlDecompiler.x", "dnSpy.BamlDecompiler.BamlDecompiler"); + static IBamlDecompiler? TryLoadBamlDecompiler() => TryCreateType("dnSpy.BamlDecompiler.x", "dnSpy.BamlDecompiler.BamlDecompiler"); static Assembly? TryLoad(string asmName) { try { @@ -256,7 +256,7 @@ static IEnumerable GetLanguagesInAssembly(string asmName) { return null; } - static T TryCreateType(string asmName, string typeFullName) { + static T? TryCreateType(string asmName, string typeFullName) where T : class { var asm = TryLoad(asmName); var type = asm?.GetType(typeFullName); return type is null ? default! : (T)Activator.CreateInstance(type)!; diff --git a/dnSpy/dnSpy.Contracts.DnSpy/Documents/TreeView/Resources/Deserializer.cs b/dnSpy/dnSpy.Contracts.DnSpy/Documents/TreeView/Resources/Deserializer.cs index 8326d228a0..3b30754354 100644 --- a/dnSpy/dnSpy.Contracts.DnSpy/Documents/TreeView/Resources/Deserializer.cs +++ b/dnSpy/dnSpy.Contracts.DnSpy/Documents/TreeView/Resources/Deserializer.cs @@ -89,7 +89,9 @@ public DontDeserializeType(SerializationInfo info, StreamingContext context) { public static Dictionary Deserialize(string asmName, string typeName, byte[] data) { var fmt = new BinaryFormatter(); fmt.Binder = new MyBinder(asmName, typeName); +#pragma warning disable SYSLIB0011 var obj = fmt.Deserialize(new MemoryStream(data)) as DeserializedType; +#pragma warning restore SYSLIB0011 Debug2.Assert(!(obj is null)); if (obj is null) return new Dictionary(); diff --git a/dnSpy/dnSpy.Contracts.DnSpy/Documents/TreeView/Resources/SerializationUtilities.cs b/dnSpy/dnSpy.Contracts.DnSpy/Documents/TreeView/Resources/SerializationUtilities.cs index 69f4e41cc2..a7e7269c51 100644 --- a/dnSpy/dnSpy.Contracts.DnSpy/Documents/TreeView/Resources/SerializationUtilities.cs +++ b/dnSpy/dnSpy.Contracts.DnSpy/Documents/TreeView/Resources/SerializationUtilities.cs @@ -72,7 +72,9 @@ public static byte[] Serialize(object obj) { // module is eg. a .NET 2.0 asm, you should replace the versions from 4.0.0.0 to 2.0.0.0. var formatter = new BinaryFormatter(); var outStream = new MemoryStream(); +#pragma warning disable SYSLIB0011 formatter.Serialize(outStream, obj); +#pragma warning restore SYSLIB0011 return outStream.ToArray(); } @@ -84,7 +86,9 @@ public static byte[] Serialize(object obj) { /// public static string Deserialize(byte[] data, out object? obj) { try { +#pragma warning disable SYSLIB0011 obj = new BinaryFormatter().Deserialize(new MemoryStream(data)); +#pragma warning restore SYSLIB0011 return string.Empty; } catch (Exception ex) { diff --git a/dnSpy/dnSpy.Contracts.DnSpy/Documents/TreeView/Resources/SerializedResourceElementNode.cs b/dnSpy/dnSpy.Contracts.DnSpy/Documents/TreeView/Resources/SerializedResourceElementNode.cs index f85c76b464..bafa92ab79 100644 --- a/dnSpy/dnSpy.Contracts.DnSpy/Documents/TreeView/Resources/SerializedResourceElementNode.cs +++ b/dnSpy/dnSpy.Contracts.DnSpy/Documents/TreeView/Resources/SerializedResourceElementNode.cs @@ -94,7 +94,9 @@ public void Deserialize() { var serializedData = ((BinaryResourceData)ResourceElement.ResourceData).Data; var formatter = new BinaryFormatter(); try { +#pragma warning disable SYSLIB0011 deserializedData = formatter.Deserialize(new MemoryStream(serializedData)); +#pragma warning restore SYSLIB0011 } catch { return; diff --git a/dnSpy/dnSpy.Contracts.Logic/Decompiler/XmlDoc/XmlDocRenderer.cs b/dnSpy/dnSpy.Contracts.Logic/Decompiler/XmlDoc/XmlDocRenderer.cs index 82abd9bcfa..1c521989e9 100644 --- a/dnSpy/dnSpy.Contracts.Logic/Decompiler/XmlDoc/XmlDocRenderer.cs +++ b/dnSpy/dnSpy.Contracts.Logic/Decompiler/XmlDoc/XmlDocRenderer.cs @@ -16,6 +16,7 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +using System; using System.IO; using System.Text; using System.Text.RegularExpressions; @@ -185,8 +186,8 @@ static void AddXmlDocumentation(IXmlDocOutput output, XmlReader xml) { /// /// /// - public static string GetCref(string cref) { - if (string.IsNullOrWhiteSpace(cref)) + public static string GetCref(string? cref) { + if (string2.IsNullOrWhiteSpace(cref)) return string.Empty; if (cref.Length < 2) { return cref.Trim(); diff --git a/dnSpy/dnSpy.Contracts.Logic/Decompiler/XmlDoc/XmlDocumentationProvider.cs b/dnSpy/dnSpy.Contracts.Logic/Decompiler/XmlDoc/XmlDocumentationProvider.cs index 3855d90d91..fe2dab7f6d 100644 --- a/dnSpy/dnSpy.Contracts.Logic/Decompiler/XmlDoc/XmlDocumentationProvider.cs +++ b/dnSpy/dnSpy.Contracts.Logic/Decompiler/XmlDoc/XmlDocumentationProvider.cs @@ -143,12 +143,13 @@ public XmlDocumentationProvider(string fileName) using (XmlTextReader xmlReader = new XmlTextReader(fs)) { xmlReader.XmlResolver = null; // no DTD resolving xmlReader.MoveToContent(); - if (string.IsNullOrEmpty(xmlReader.GetAttribute("redirect"))) { + var redirectAttr = (string?)xmlReader.GetAttribute("redirect"); + if (string2.IsNullOrEmpty(redirectAttr)) { this.fileName = fileName; - encoding = GetEncoding(xmlReader.Encoding); + encoding = GetEncoding(xmlReader.Encoding ?? throw new InvalidOperationException()); ReadXmlDoc(xmlReader); } else { - string? redirectionTarget = GetRedirectionTarget(fileName, xmlReader.GetAttribute("redirect")); + var redirectionTarget = GetRedirectionTarget(fileName, redirectAttr); if (!(redirectionTarget is null)) { //Debug.WriteLine("XmlDoc " + fileName + " is redirecting to " + redirectionTarget); using (FileStream redirectedFs = new FileStream(redirectionTarget, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete)) { @@ -156,12 +157,12 @@ public XmlDocumentationProvider(string fileName) redirectedXmlReader.XmlResolver = null; // no DTD resolving redirectedXmlReader.MoveToContent(); this.fileName = redirectionTarget; - encoding = GetEncoding(redirectedXmlReader.Encoding); + encoding = GetEncoding(redirectedXmlReader.Encoding ?? throw new InvalidOperationException()); ReadXmlDoc(redirectedXmlReader); } } } else { - throw new XmlException("XmlDoc " + fileName + " is redirecting to " + xmlReader.GetAttribute("redirect") + ", but that file was not found."); + throw new XmlException("XmlDoc " + fileName + " is redirecting to " + redirectAttr + ", but that file was not found."); } } } @@ -301,7 +302,7 @@ static void ReadMembersSection(XmlTextReader reader, LinePositionMapper linePosM case XmlNodeType.Element: if (reader.LocalName == "member") { int pos = linePosMapper.GetPositionForLine(reader.LineNumber) + Math.Max(reader.LinePosition - 2, 0); - string memberAttr = reader.GetAttribute("name"); + var memberAttr = (string?)reader.GetAttribute("name"); if (!(memberAttr is null)) indexList.Add(new IndexEntry(GetHashCode(memberAttr), pos)); reader.Skip(); @@ -421,7 +422,7 @@ static int GetHashCode(string key) r.XmlResolver = null; // no DTD resolving while (r.Read()) { if (r.NodeType == XmlNodeType.Element) { - string memberAttr = r.GetAttribute("name"); + var memberAttr = (string?)r.GetAttribute("name"); if (memberAttr == key) { return r.ReadInnerXml(); } else { diff --git a/dnSpy/dnSpy.Decompiler/MSBuild/SettingsProjectFile.cs b/dnSpy/dnSpy.Decompiler/MSBuild/SettingsProjectFile.cs index 66b1d01fa4..a3b8cebd06 100644 --- a/dnSpy/dnSpy.Decompiler/MSBuild/SettingsProjectFile.cs +++ b/dnSpy/dnSpy.Decompiler/MSBuild/SettingsProjectFile.cs @@ -251,12 +251,12 @@ void InitializeConnectionStringDesignTimeValues() { var doc = XDocument.Load(configFile, LoadOptions.None); var prefix = type.ReflectionFullName + "."; foreach (var e in doc.XPathSelectElements("/configuration/connectionStrings/add")) { - var name = (string)e.Attribute("name"); + var name = (string?)e.Attribute("name"); if (name is null || !name.StartsWith(prefix, StringComparison.Ordinal)) continue; - var connectionString = (string)e.Attribute("connectionString"); - var providerName = (string)e.Attribute("providerName"); + var connectionString = (string?)e.Attribute("connectionString"); + var providerName = (string?)e.Attribute("providerName"); if (connectionString is null || providerName is null) continue; diff --git a/dnSpy/dnSpy/BackgroundImage/Dialog/AppSettingsPageImpl.cs b/dnSpy/dnSpy/BackgroundImage/Dialog/AppSettingsPageImpl.cs index 47aa532fe8..474c97d323 100644 --- a/dnSpy/dnSpy/BackgroundImage/Dialog/AppSettingsPageImpl.cs +++ b/dnSpy/dnSpy/BackgroundImage/Dialog/AppSettingsPageImpl.cs @@ -198,20 +198,20 @@ public AppSettingsPageImpl(IBackgroundImageSettingsService backgroundImageSettin this.pickFilename = pickFilename ?? throw new ArgumentNullException(nameof(pickFilename)); this.pickDirectory = pickDirectory ?? throw new ArgumentNullException(nameof(pickDirectory)); Settings = new ObservableCollection(settings.OrderBy(a => a.Lazy.Value.UIOrder).Select(a => new Settings(a))); - stretchVM = new EnumListVM(EnumVM.Create(false, typeof(Stretch)), (a, b) => currentItem.RawSettings.Stretch = (Stretch)stretchVM.SelectedItem!); - stretchDirectionVM = new EnumListVM(stretchDirectionList, (a, b) => currentItem.RawSettings.StretchDirection = (StretchDirection)stretchDirectionVM.SelectedItem!); - imagePlacementVM = new EnumListVM(imagePlacementList, (a, b) => currentItem.RawSettings.ImagePlacement = (ImagePlacement)imagePlacementVM.SelectedItem!); - opacityVM = new DoubleVM(a => { if (!opacityVM.HasError) currentItem.RawSettings.Opacity = FilterOpacity(opacityVM.Value); }); - horizontalOffsetVM = new DoubleVM(a => { if (!horizontalOffsetVM.HasError) currentItem.RawSettings.HorizontalOffset = FilterOffset(horizontalOffsetVM.Value); }); - verticalOffsetVM = new DoubleVM(a => { if (!verticalOffsetVM.HasError) currentItem.RawSettings.VerticalOffset = FilterOffset(verticalOffsetVM.Value); }); - leftMarginWidthPercentVM = new DoubleVM(a => { if (!leftMarginWidthPercentVM.HasError) currentItem.RawSettings.LeftMarginWidthPercent = FilterMarginPercent(leftMarginWidthPercentVM.Value); }); - rightMarginWidthPercentVM = new DoubleVM(a => { if (!rightMarginWidthPercentVM.HasError) currentItem.RawSettings.RightMarginWidthPercent = FilterMarginPercent(rightMarginWidthPercentVM.Value); }); - topMarginHeightPercentVM = new DoubleVM(a => { if (!topMarginHeightPercentVM.HasError) currentItem.RawSettings.TopMarginHeightPercent = FilterMarginPercent(topMarginHeightPercentVM.Value); }); - bottomMarginHeightPercentVM = new DoubleVM(a => { if (!bottomMarginHeightPercentVM.HasError) currentItem.RawSettings.BottomMarginHeightPercent = FilterMarginPercent(bottomMarginHeightPercentVM.Value); }); - maxHeightVM = new DoubleVM(a => { if (!maxHeightVM.HasError) currentItem.RawSettings.MaxHeight = FilterLength(maxHeightVM.Value); }); - maxWidthVM = new DoubleVM(a => { if (!maxWidthVM.HasError) currentItem.RawSettings.MaxWidth = FilterLength(maxWidthVM.Value); }); - zoomVM = new DoubleVM(a => { if (!zoomVM.HasError) currentItem.RawSettings.Zoom = FilterZoom(zoomVM.Value); }); - intervalVM = new DefaultConverterVM(a => { if (!intervalVM.HasError) currentItem.RawSettings.Interval = intervalVM.Value; }); + stretchVM = new EnumListVM(EnumVM.Create(false, typeof(Stretch)), (a, b) => currentItem!.RawSettings.Stretch = (Stretch)stretchVM!.SelectedItem!); + stretchDirectionVM = new EnumListVM(stretchDirectionList, (a, b) => currentItem!.RawSettings.StretchDirection = (StretchDirection)stretchDirectionVM!.SelectedItem!); + imagePlacementVM = new EnumListVM(imagePlacementList, (a, b) => currentItem!.RawSettings.ImagePlacement = (ImagePlacement)imagePlacementVM!.SelectedItem!); + opacityVM = new DoubleVM(a => { if (!opacityVM!.HasError) currentItem!.RawSettings.Opacity = FilterOpacity(opacityVM.Value); }); + horizontalOffsetVM = new DoubleVM(a => { if (!horizontalOffsetVM!.HasError) currentItem!.RawSettings.HorizontalOffset = FilterOffset(horizontalOffsetVM.Value); }); + verticalOffsetVM = new DoubleVM(a => { if (!verticalOffsetVM!.HasError) currentItem!.RawSettings.VerticalOffset = FilterOffset(verticalOffsetVM.Value); }); + leftMarginWidthPercentVM = new DoubleVM(a => { if (!leftMarginWidthPercentVM!.HasError) currentItem!.RawSettings.LeftMarginWidthPercent = FilterMarginPercent(leftMarginWidthPercentVM.Value); }); + rightMarginWidthPercentVM = new DoubleVM(a => { if (!rightMarginWidthPercentVM!.HasError) currentItem!.RawSettings.RightMarginWidthPercent = FilterMarginPercent(rightMarginWidthPercentVM.Value); }); + topMarginHeightPercentVM = new DoubleVM(a => { if (!topMarginHeightPercentVM!.HasError) currentItem!.RawSettings.TopMarginHeightPercent = FilterMarginPercent(topMarginHeightPercentVM.Value); }); + bottomMarginHeightPercentVM = new DoubleVM(a => { if (!bottomMarginHeightPercentVM!.HasError) currentItem!.RawSettings.BottomMarginHeightPercent = FilterMarginPercent(bottomMarginHeightPercentVM.Value); }); + maxHeightVM = new DoubleVM(a => { if (!maxHeightVM!.HasError) currentItem!.RawSettings.MaxHeight = FilterLength(maxHeightVM.Value); }); + maxWidthVM = new DoubleVM(a => { if (!maxWidthVM!.HasError) currentItem!.RawSettings.MaxWidth = FilterLength(maxWidthVM.Value); }); + zoomVM = new DoubleVM(a => { if (!zoomVM!.HasError) currentItem!.RawSettings.Zoom = FilterZoom(zoomVM.Value); }); + intervalVM = new DefaultConverterVM(a => { if (!intervalVM!.HasError) currentItem!.RawSettings.Interval = intervalVM.Value; }); CurrentItem = Settings.FirstOrDefault(a => a.Id == backgroundImageSettingsService.LastSelectedId) ?? Settings[0]; } diff --git a/dnSpy/dnSpy/Disassembly/X86/DisassemblyCodeStyleAppSettingsPage.cs b/dnSpy/dnSpy/Disassembly/X86/DisassemblyCodeStyleAppSettingsPage.cs index 12b323f4f3..5034cd5722 100644 --- a/dnSpy/dnSpy/Disassembly/X86/DisassemblyCodeStyleAppSettingsPage.cs +++ b/dnSpy/dnSpy/Disassembly/X86/DisassemblyCodeStyleAppSettingsPage.cs @@ -161,7 +161,7 @@ protected DisassemblyCodeStyleAppSettingsPage(DisassemblySettings global_x86Disa GasSpaceAfterMemoryOperandComma = AddDisasmBoolSetting(() => Settings.GasSpaceAfterMemoryOperandComma, value => Settings.GasSpaceAfterMemoryOperandComma = value, Instruction.Create(Code.Mov_rm64_r64, new MemoryOperand(Register.RAX, Register.RDI, 4, 0x12345678, 8), Register.RCX)); OperandColumnVM = new Int32VM(x86DisassemblySettings.FirstOperandCharIndex + 1, a => { - if (!OperandColumnVM.HasError) + if (!OperandColumnVM!.HasError) this.x86DisassemblySettings.FirstOperandCharIndex = OperandColumnVM.Value - 1; }, useDecimal: true) { Min = 1, Max = 100 }; diff --git a/dnSpy/dnSpy/Documents/Tabs/DefaultDocumentList.cs b/dnSpy/dnSpy/Documents/Tabs/DefaultDocumentList.cs index 3d00e00243..1ec1411fad 100644 --- a/dnSpy/dnSpy/Documents/Tabs/DefaultDocumentList.cs +++ b/dnSpy/dnSpy/Documents/Tabs/DefaultDocumentList.cs @@ -175,7 +175,7 @@ public RefFileList(string filename) { var refFilePath = Path.GetDirectoryName(Path.GetDirectoryName(filename)); var doc = XDocument.Load(filename, LoadOptions.None); var root = doc.Root; - if (root.Name != "FileList") + if (root?.Name != "FileList") throw new InvalidOperationException(); foreach (var attr in root.Attributes()) { switch (attr.Name.ToString()) { @@ -395,19 +395,19 @@ public DefaultDocumentList[] Find() { try { var doc = XDocument.Load(filename, LoadOptions.None); var root = doc.Root; - if (root.Name != "FileList") + if (root?.Name != "FileList") return null; - var name = (string)root.Attribute("name"); - if (string.IsNullOrWhiteSpace(name)) + var name = (string?)root.Attribute("name"); + if (string2.IsNullOrWhiteSpace(name)) return null; bool? isDefault = (bool?)root.Attribute("default"); var l = new DefaultDocumentList(name); foreach (var sect in root.Elements("File")) { - var name2 = (string)sect.Attribute("name"); - if (string.IsNullOrWhiteSpace(name2)) + var name2 = (string?)sect.Attribute("name"); + if (string2.IsNullOrWhiteSpace(name2)) return null; - var type = (string)sect.Attribute("type") ?? "gac"; - var guidStr = (string)sect.Attribute("guid"); + var type = (string?)sect.Attribute("type") ?? "gac"; + var guidStr = (string?)sect.Attribute("guid"); Guid guid = Guid.Empty; bool hasGuid = !(guidStr is null) && Guid.TryParse(guidStr, out guid); if (type.Equals("file")) diff --git a/dnSpy/dnSpy/Documents/Tabs/DocViewer/ToolTips/CodeToolTipWriter.cs b/dnSpy/dnSpy/Documents/Tabs/DocViewer/ToolTips/CodeToolTipWriter.cs index 667c43241f..dec5195d8d 100644 --- a/dnSpy/dnSpy/Documents/Tabs/DocViewer/ToolTips/CodeToolTipWriter.cs +++ b/dnSpy/dnSpy/Documents/Tabs/DocViewer/ToolTips/CodeToolTipWriter.cs @@ -133,8 +133,8 @@ static bool WriteXmlDoc(IXmlDocOutput output, string? xmlDoc, string? name, stri return false; try { var xml = XDocument.Load(new StringReader("" + xmlDoc + ""), LoadOptions.None); - foreach (var pxml in xml.Root.Elements(xmlElemName)) { - if ((string)pxml.Attribute("name") == name) { + foreach (var pxml in xml.Root?.Elements(xmlElemName) ?? Array.Empty()) { + if ((string?)pxml.Attribute("name") == name) { WriteXmlDocParameter(output, pxml); return true; } @@ -152,17 +152,17 @@ static void WriteXmlDocParameter(IXmlDocOutput output, XElement xml) { else if (elem is XElement xelem) { switch (xelem.Name.ToString().ToUpperInvariant()) { case "SEE": - var cref = xelem.Attribute("cref"); + var cref = (string?)xelem.Attribute("cref"); if (!(cref is null)) - output.Write(XmlDocRenderer.GetCref((string)cref), BoxedTextColor.Text); - var langword = xelem.Attribute("langword"); + output.Write(XmlDocRenderer.GetCref(cref), BoxedTextColor.Text); + var langword = (string?)xelem.Attribute("langword"); if (!(langword is null)) - output.Write(((string)langword).Trim(), BoxedTextColor.Keyword); + output.Write(langword.Trim(), BoxedTextColor.Keyword); break; case "PARAMREF": - var nameAttr = xml.Attribute("name"); + var nameAttr = (string?)xml.Attribute("name"); if (!(nameAttr is null)) - output.Write(((string)nameAttr).Trim(), BoxedTextColor.Parameter); + output.Write(nameAttr.Trim(), BoxedTextColor.Parameter); break; case "BR": case "PARA": diff --git a/dnSpy/dnSpy/Extension/ExtensionConfigReader.cs b/dnSpy/dnSpy/Extension/ExtensionConfigReader.cs index a284be7cff..3b22d421aa 100644 --- a/dnSpy/dnSpy/Extension/ExtensionConfigReader.cs +++ b/dnSpy/dnSpy/Extension/ExtensionConfigReader.cs @@ -40,7 +40,7 @@ public static ExtensionConfig Read(string filename) { return config; var doc = XDocument.Load(filename, LoadOptions.None); var root = doc.Root; - if (root.Name == XML_ROOT_NAME) + if (root?.Name == XML_ROOT_NAME) Read(root, config); return config; } diff --git a/dnSpy/dnSpy/Search/SearchControlVM.cs b/dnSpy/dnSpy/Search/SearchControlVM.cs index cded8a7b6e..278161c7f6 100644 --- a/dnSpy/dnSpy/Search/SearchControlVM.cs +++ b/dnSpy/dnSpy/Search/SearchControlVM.cs @@ -138,7 +138,7 @@ public SearchControlVM(IDocumentSearcherProvider fileSearcherProvider, IDocument SearchResults = new ObservableCollection(); searchResultsCollectionView = (ListCollectionView)CollectionViewSource.GetDefaultView(SearchResults); searchResultsCollectionView.CustomSort = new SearchResult_Comparer(); - SearchLocationVM = new EnumListVM(searchLocationList, (a, b) => SearchSettings.SearchLocation = (SearchLocation)SearchLocationVM.SelectedItem!); + SearchLocationVM = new EnumListVM(searchLocationList, (a, b) => SearchSettings.SearchLocation = (SearchLocation)SearchLocationVM!.SelectedItem!); SearchLocationVM.SelectedItem = SearchSettings.SearchLocation; Add(SearchType.AssemblyDef, dnSpy_Resources.SearchWindow_Assembly, DsImages.Assembly, null, VisibleMembersFlags.AssemblyDef); diff --git a/dnSpy/dnSpy/Settings/XmlSettingsReader.cs b/dnSpy/dnSpy/Settings/XmlSettingsReader.cs index 94e9e48950..69a3fbb5cc 100644 --- a/dnSpy/dnSpy/Settings/XmlSettingsReader.cs +++ b/dnSpy/dnSpy/Settings/XmlSettingsReader.cs @@ -38,13 +38,13 @@ public void Read() { return; var doc = XDocument.Load(filename, LoadOptions.None); var root = doc.Root; - if (root.Name == XmlSettingsConstants.XML_ROOT_NAME) + if (root?.Name == XmlSettingsConstants.XML_ROOT_NAME) Read(root); } void Read(XElement root) { foreach (var xmlSect in root.Elements(XmlSettingsConstants.SECTION_NAME)) { - var name = XmlUtils.UnescapeAttributeValue((string)xmlSect.Attribute(XmlSettingsConstants.SECTION_ATTRIBUTE_NAME)); + var name = XmlUtils.UnescapeAttributeValue((string?)xmlSect.Attribute(XmlSettingsConstants.SECTION_ATTRIBUTE_NAME)); if (name is null) continue; if (!Guid.TryParse(name, out var guid)) @@ -66,7 +66,7 @@ void ReadSection(XElement xml, ISettingsSection section, int recursionCounter) { } foreach (var xmlSect in xml.Elements(XmlSettingsConstants.SECTION_NAME)) { - var name = XmlUtils.UnescapeAttributeValue((string)xmlSect.Attribute(XmlSettingsConstants.SECTION_ATTRIBUTE_NAME)); + var name = XmlUtils.UnescapeAttributeValue((string?)xmlSect.Attribute(XmlSettingsConstants.SECTION_ATTRIBUTE_NAME)); if (name is null) continue; var childSection = section.CreateSection(name); diff --git a/dnSpy/dnSpy/Settings/XmlSettingsWriter.cs b/dnSpy/dnSpy/Settings/XmlSettingsWriter.cs index 7f02edc2ba..d68fd66ba7 100644 --- a/dnSpy/dnSpy/Settings/XmlSettingsWriter.cs +++ b/dnSpy/dnSpy/Settings/XmlSettingsWriter.cs @@ -37,7 +37,7 @@ public XmlSettingsWriter(ISettingsService mgr, string? filename = null) { public void Write() { Directory.CreateDirectory(Path.GetDirectoryName(filename)!); var doc = new XDocument(new XElement(XmlSettingsConstants.XML_ROOT_NAME)); - Write(doc.Root); + Write(doc.Root!); doc.Save(filename); } diff --git a/dnSpy/dnSpy/Settings/XmlUtils.cs b/dnSpy/dnSpy/Settings/XmlUtils.cs index fe4b574f65..46a68990e4 100644 --- a/dnSpy/dnSpy/Settings/XmlUtils.cs +++ b/dnSpy/dnSpy/Settings/XmlUtils.cs @@ -25,7 +25,7 @@ namespace dnSpy.Settings { static class XmlUtils { const char ESCAPE_CHAR = '©'; - public static string? EscapeAttributeValue(string s) { + public static string? EscapeAttributeValue(string? s) { if (s is null) return null; var sb = new StringBuilder(s.Length); @@ -38,7 +38,7 @@ static class XmlUtils { return sb.ToString(); } - public static string? UnescapeAttributeValue(string s) { + public static string? UnescapeAttributeValue(string? s) { if (s is null) return null; if (s.IndexOf(ESCAPE_CHAR) < 0) diff --git a/dnSpy/dnSpy/Themes/Theme.cs b/dnSpy/dnSpy/Themes/Theme.cs index 2ded694ffa..0c234f08bf 100644 --- a/dnSpy/dnSpy/Themes/Theme.cs +++ b/dnSpy/dnSpy/Themes/Theme.cs @@ -95,8 +95,7 @@ public Theme(XElement root) { throw new Exception("Missing or empty guid attribute"); Guid = new Guid(guid.Value); - var name = root.Attribute("name"); - Name = name is null ? string.Empty : (string)name; + Name = (string?)root.Attribute("name") ?? string.Empty; var menuName = root.Attribute("menu-name"); if (menuName is null || string.IsNullOrEmpty(menuName.Value)) diff --git a/dnSpy/dnSpy/Themes/ThemeService.cs b/dnSpy/dnSpy/Themes/ThemeService.cs index b97e14bfa1..4d66c49f97 100644 --- a/dnSpy/dnSpy/Themes/ThemeService.cs +++ b/dnSpy/dnSpy/Themes/ThemeService.cs @@ -167,7 +167,7 @@ void Load() { Theme? Load(string filename) { try { var root = XDocument.Load(filename).Root; - if (root.Name != "theme") + if (root?.Name != "theme") return null; var theme = new Theme(root);