From d8f31b3c531cd9df9276f2de5facf6391e0ee49d Mon Sep 17 00:00:00 2001 From: ds5678 <49847914+ds5678@users.noreply.github.com> Date: Fri, 28 Nov 2025 21:45:52 -0800 Subject: [PATCH] Fix warnings --- .../Model/Contexts/HasCustomAttributes.cs | 4 +-- .../ControlFlowGraphOutputFormat.cs | 2 +- Cpp2IL/Program.cs | 33 +++++++++++-------- LibCpp2IL/BinarySearcher.cs | 4 +-- .../BinaryStructures/Il2CppRGCTXDefinition.cs | 10 +++--- LibCpp2IL/Metadata/Il2CppTypeDefinition.cs | 2 +- LibCpp2IL/NintendoSwitch/NsoFile.cs | 8 ++--- LibCpp2IL/NintendoSwitch/StreamExtensions.cs | 20 +++++++++++ 8 files changed, 54 insertions(+), 29 deletions(-) create mode 100644 LibCpp2IL/NintendoSwitch/StreamExtensions.cs diff --git a/Cpp2IL.Core/Model/Contexts/HasCustomAttributes.cs b/Cpp2IL.Core/Model/Contexts/HasCustomAttributes.cs index 2b693edd..ad4381bb 100644 --- a/Cpp2IL.Core/Model/Contexts/HasCustomAttributes.cs +++ b/Cpp2IL.Core/Model/Contexts/HasCustomAttributes.cs @@ -129,7 +129,7 @@ protected void InitCustomAttributeData() return null; var target = new Il2CppCustomAttributeDataRange() { token = Token }; - var caIndex = AppContext.Metadata.AttributeDataRanges.BinarySearch + var caIndex = AppContext.Metadata.AttributeDataRanges!.BinarySearch ( CustomAttributeAssembly.Definition.Image.customAttributeStart, (int)CustomAttributeAssembly.Definition.Image.customAttributeCount, @@ -189,7 +189,7 @@ private void InitPre29AttributeGeneratorAnalysis(int rangeIndex) if (generatorPtr == 0 || !AppContext.Binary.TryMapVirtualAddressToRaw(generatorPtr, out _)) { - Logger.WarnNewline($"Supposedly had custom attributes ({string.Join(", ", AttributeTypes)}), but generator was null for " + this, "CA Restore"); + Logger.WarnNewline($"Supposedly had custom attributes ({string.Join(", ", AttributeTypes ?? [])}), but generator was null for " + this, "CA Restore"); RawIl2CppCustomAttributeData = Memory.Empty; return; } diff --git a/Cpp2IL.Plugin.ControlFlowGraph/ControlFlowGraphOutputFormat.cs b/Cpp2IL.Plugin.ControlFlowGraph/ControlFlowGraphOutputFormat.cs index 8ce0a933..ea10f7d4 100644 --- a/Cpp2IL.Plugin.ControlFlowGraph/ControlFlowGraphOutputFormat.cs +++ b/Cpp2IL.Plugin.ControlFlowGraph/ControlFlowGraphOutputFormat.cs @@ -145,7 +145,7 @@ DotEdge GetOrAddEdge(DotNode from, DotNode to) private static string GetFilePathForMethod(string outputRoot, MethodAnalysisContext method, string assemblyNameClean) { - TypeAnalysisContext type = method.DeclaringType; + TypeAnalysisContext type = method.DeclaringType!; //Get root assembly directory diff --git a/Cpp2IL/Program.cs b/Cpp2IL/Program.cs index 53251941..97396db8 100644 --- a/Cpp2IL/Program.cs +++ b/Cpp2IL/Program.cs @@ -24,8 +24,7 @@ namespace Cpp2IL; -[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")] -internal class Program +internal static class Program { private static readonly List PathsToDeleteOnExit = []; @@ -33,13 +32,8 @@ internal class Program private static void ResolvePathsFromCommandLine(string? gamePath, string? inputExeName, ref Cpp2IlRuntimeArgs args) { - if (string.IsNullOrEmpty(gamePath)) + if (gamePath is null or { Length: 0 }) throw new SoftException("No force options provided, and no game path was provided either. Please provide a game path or use the --force- options."); - - //Somehow the above doesn't tell .net that gamePath can't be null on net472, so we do this stupid thing to avoid nullable warnings -#if NET472 - gamePath = gamePath!; -#endif Logger.VerboseNewline("Beginning path resolution..."); @@ -314,8 +308,7 @@ private static void HandleSingleApk(string gamePath, ref Cpp2IlRuntimeArgs args) var ggmBytes = new byte[0x40]; using var ggmStream = globalgamemanagers.Open(); - // ReSharper disable once MustUseReturnValue - ggmStream.Read(ggmBytes, 0, 0x40); + ggmStream.ReadExactly(ggmBytes, 0, 0x40); args.UnityVersion = Cpp2IlApi.GetVersionFromGlobalGameManagers(ggmBytes); } @@ -403,8 +396,7 @@ private static void HandleXapk(string gamePath, ref Cpp2IlRuntimeArgs args) var ggmBytes = new byte[0x40]; using var ggmStream = globalgamemanagers.Open(); - // ReSharper disable once MustUseReturnValue - ggmStream.Read(ggmBytes, 0, 0x40); + ggmStream.ReadExactly(ggmBytes, 0, 0x40); args.UnityVersion = Cpp2IlApi.GetVersionFromGlobalGameManagers(ggmBytes); } @@ -469,8 +461,7 @@ private static void HandleIpa(string gamePath, ref Cpp2IlRuntimeArgs args) var ggmBytes = new byte[0x40]; using var ggmStream = globalgamemanagers.Open(); - // ReSharper disable once MustUseReturnValue - ggmStream.Read(ggmBytes, 0, 0x40); + ggmStream.ReadExactly(ggmBytes, 0, 0x40); args.UnityVersion = Cpp2IlApi.GetVersionFromGlobalGameManagers(ggmBytes); } @@ -768,4 +759,18 @@ private static void CleanupExtractedFiles() } } } + +#if !NET7_0_OR_GREATER + private static void ReadExactly(this Stream stream, byte[] buffer, int offset, int count) + { + var totalRead = 0; + while (totalRead < count) + { + var bytesRead = stream.Read(buffer, offset + totalRead, count - totalRead); + if (bytesRead == 0) + throw new EndOfStreamException("Could not read enough bytes from stream"); + totalRead += bytesRead; + } + } +#endif } diff --git a/LibCpp2IL/BinarySearcher.cs b/LibCpp2IL/BinarySearcher.cs index 10b22333..bd3998f3 100644 --- a/LibCpp2IL/BinarySearcher.cs +++ b/LibCpp2IL/BinarySearcher.cs @@ -115,7 +115,7 @@ public ulong FindCodeRegistrationPre2019() LibLogger.VerboseNewline($"\t\t\tChecking for CodeRegistration at virtual address 0x{va:x}..."); var cr = binary.ReadReadableAtVirtualAddress(va); - if ((long)cr.customAttributeCount == LibCpp2IlMain.TheMetadata!.attributeTypeRanges.Count) + if ((long)cr.customAttributeCount == LibCpp2IlMain.TheMetadata!.attributeTypeRanges!.Count) return va; LibLogger.VerboseNewline($"\t\t\t\tNot a valid CodeRegistration - custom attribute count is {cr.customAttributeCount}, expecting {LibCpp2IlMain.TheMetadata!.attributeTypeRanges.Count}"); @@ -297,7 +297,7 @@ public ulong FindMetadataRegistrationPre24_5() { var mr = binary.ReadReadableAtVirtualAddress(potentialMetaRegPointer); - if (mr.metadataUsagesCount == (ulong)LibCpp2IlMain.TheMetadata!.metadataUsageLists.Length) + if (mr.metadataUsagesCount == (ulong)LibCpp2IlMain.TheMetadata!.metadataUsageLists!.Length) { LibLogger.VerboseNewline($"\t\t\tFound and selected probably valid metadata registration at 0x{potentialMetaRegPointer:X}."); return potentialMetaRegPointer; diff --git a/LibCpp2IL/BinaryStructures/Il2CppRGCTXDefinition.cs b/LibCpp2IL/BinaryStructures/Il2CppRGCTXDefinition.cs index 2a751d44..38f7a9dd 100644 --- a/LibCpp2IL/BinaryStructures/Il2CppRGCTXDefinition.cs +++ b/LibCpp2IL/BinaryStructures/Il2CppRGCTXDefinition.cs @@ -1,4 +1,4 @@ -using LibCpp2IL.Reflection; +using LibCpp2IL.Reflection; namespace LibCpp2IL.BinaryStructures; @@ -7,9 +7,9 @@ public class Il2CppRGCTXDefinition : ReadableClass public Il2CppRGCTXDataType type; public int _rawIndex; - public int MethodIndex => type == Il2CppRGCTXDataType.IL2CPP_RGCTX_DATA_CONSTRAINED ? _constrainedData.MethodIndex : _defData.MethodIndex; + public int MethodIndex => _defData?.MethodIndex ?? _constrainedData!.MethodIndex; - public int TypeIndex => type == Il2CppRGCTXDataType.IL2CPP_RGCTX_DATA_CONSTRAINED ? _constrainedData.TypeIndex : _defData.TypeIndex; + public int TypeIndex => _defData?.TypeIndex ?? _constrainedData!.TypeIndex; public Il2CppMethodSpec? MethodSpec => LibCpp2IlMain.Binary?.GetMethodSpec(MethodIndex); @@ -41,9 +41,9 @@ public override void Read(ClassReadingBinaryReader reader) } } [Version(Min = 27.2f)] - private Il2CppRGCTXConstrainedData _constrainedData; + private Il2CppRGCTXConstrainedData? _constrainedData; - private Il2CppRGCTXDefinitionData _defData; + private Il2CppRGCTXDefinitionData? _defData; public override void Read(ClassReadingBinaryReader reader) { type = IsLessThan(29) ? (Il2CppRGCTXDataType)reader.ReadInt32() : (Il2CppRGCTXDataType)reader.ReadInt64(); diff --git a/LibCpp2IL/Metadata/Il2CppTypeDefinition.cs b/LibCpp2IL/Metadata/Il2CppTypeDefinition.cs index 67c2401f..353e8ce7 100644 --- a/LibCpp2IL/Metadata/Il2CppTypeDefinition.cs +++ b/LibCpp2IL/Metadata/Il2CppTypeDefinition.cs @@ -139,7 +139,7 @@ public Il2CppRGCTXDefinition[] RgctXs if (LibCpp2IlMain.MetadataVersion < 24.2f) { //No codegen modules here. - return LibCpp2IlMain.TheMetadata!.RgctxDefinitions.Skip(RgctxStartIndex).Take(RgctxCount).ToArray(); + return LibCpp2IlMain.TheMetadata!.RgctxDefinitions!.Skip(RgctxStartIndex).Take(RgctxCount).ToArray(); } var cgm = CodeGenModule; diff --git a/LibCpp2IL/NintendoSwitch/NsoFile.cs b/LibCpp2IL/NintendoSwitch/NsoFile.cs index add73c0e..9c6f04d0 100644 --- a/LibCpp2IL/NintendoSwitch/NsoFile.cs +++ b/LibCpp2IL/NintendoSwitch/NsoFile.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IO; @@ -257,7 +257,7 @@ public NsoFile Decompress() var unCompressedData = new byte[_header.TextSegment.DecompressedSize]; using (var decoder = new Lz4DecodeStream(new MemoryStream(textBytes))) { - decoder.Read(unCompressedData, 0, unCompressedData.Length); + decoder.ReadExactly(unCompressedData, 0, unCompressedData.Length); } writer.Write(unCompressedData); @@ -273,7 +273,7 @@ public NsoFile Decompress() var unCompressedData = new byte[_header.RoDataSegment.DecompressedSize]; using (var decoder = new Lz4DecodeStream(new MemoryStream(roDataBytes))) { - decoder.Read(unCompressedData, 0, unCompressedData.Length); + decoder.ReadExactly(unCompressedData, 0, unCompressedData.Length); } writer.Write(unCompressedData); @@ -289,7 +289,7 @@ public NsoFile Decompress() var unCompressedData = new byte[_header.DataSegment.DecompressedSize]; using (var decoder = new Lz4DecodeStream(new MemoryStream(dataBytes))) { - decoder.Read(unCompressedData, 0, unCompressedData.Length); + decoder.ReadExactly(unCompressedData, 0, unCompressedData.Length); } writer.Write(unCompressedData); diff --git a/LibCpp2IL/NintendoSwitch/StreamExtensions.cs b/LibCpp2IL/NintendoSwitch/StreamExtensions.cs new file mode 100644 index 00000000..aa3a7584 --- /dev/null +++ b/LibCpp2IL/NintendoSwitch/StreamExtensions.cs @@ -0,0 +1,20 @@ +#if !NET7_0_OR_GREATER +using System.IO; + +namespace LibCpp2IL.NintendoSwitch; + +internal static class StreamExtensions +{ + public static void ReadExactly(this Stream stream, byte[] buffer, int offset, int count) + { + var totalRead = 0; + while (totalRead < count) + { + var bytesRead = stream.Read(buffer, offset + totalRead, count - totalRead); + if (bytesRead == 0) + throw new EndOfStreamException("Could not read enough bytes from stream"); + totalRead += bytesRead; + } + } +} +#endif