Skip to content

Commit d8f31b3

Browse files
committed
Fix warnings
1 parent e66a74b commit d8f31b3

File tree

8 files changed

+54
-29
lines changed

8 files changed

+54
-29
lines changed

Cpp2IL.Core/Model/Contexts/HasCustomAttributes.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ protected void InitCustomAttributeData()
129129
return null;
130130

131131
var target = new Il2CppCustomAttributeDataRange() { token = Token };
132-
var caIndex = AppContext.Metadata.AttributeDataRanges.BinarySearch
132+
var caIndex = AppContext.Metadata.AttributeDataRanges!.BinarySearch
133133
(
134134
CustomAttributeAssembly.Definition.Image.customAttributeStart,
135135
(int)CustomAttributeAssembly.Definition.Image.customAttributeCount,
@@ -189,7 +189,7 @@ private void InitPre29AttributeGeneratorAnalysis(int rangeIndex)
189189

190190
if (generatorPtr == 0 || !AppContext.Binary.TryMapVirtualAddressToRaw(generatorPtr, out _))
191191
{
192-
Logger.WarnNewline($"Supposedly had custom attributes ({string.Join(", ", AttributeTypes)}), but generator was null for " + this, "CA Restore");
192+
Logger.WarnNewline($"Supposedly had custom attributes ({string.Join(", ", AttributeTypes ?? [])}), but generator was null for " + this, "CA Restore");
193193
RawIl2CppCustomAttributeData = Memory<byte>.Empty;
194194
return;
195195
}

Cpp2IL.Plugin.ControlFlowGraph/ControlFlowGraphOutputFormat.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ DotEdge GetOrAddEdge(DotNode from, DotNode to)
145145

146146
private static string GetFilePathForMethod(string outputRoot, MethodAnalysisContext method, string assemblyNameClean)
147147
{
148-
TypeAnalysisContext type = method.DeclaringType;
148+
TypeAnalysisContext type = method.DeclaringType!;
149149

150150

151151
//Get root assembly directory

Cpp2IL/Program.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,16 @@
2424

2525
namespace Cpp2IL;
2626

27-
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")]
28-
internal class Program
27+
internal static class Program
2928
{
3029
private static readonly List<string> PathsToDeleteOnExit = [];
3130

3231
public static readonly string Cpp2IlVersionString = typeof(Program).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()!.InformationalVersion;
3332

3433
private static void ResolvePathsFromCommandLine(string? gamePath, string? inputExeName, ref Cpp2IlRuntimeArgs args)
3534
{
36-
if (string.IsNullOrEmpty(gamePath))
35+
if (gamePath is null or { Length: 0 })
3736
throw new SoftException("No force options provided, and no game path was provided either. Please provide a game path or use the --force- options.");
38-
39-
//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
40-
#if NET472
41-
gamePath = gamePath!;
42-
#endif
4337

4438
Logger.VerboseNewline("Beginning path resolution...");
4539

@@ -314,8 +308,7 @@ private static void HandleSingleApk(string gamePath, ref Cpp2IlRuntimeArgs args)
314308
var ggmBytes = new byte[0x40];
315309
using var ggmStream = globalgamemanagers.Open();
316310

317-
// ReSharper disable once MustUseReturnValue
318-
ggmStream.Read(ggmBytes, 0, 0x40);
311+
ggmStream.ReadExactly(ggmBytes, 0, 0x40);
319312

320313
args.UnityVersion = Cpp2IlApi.GetVersionFromGlobalGameManagers(ggmBytes);
321314
}
@@ -403,8 +396,7 @@ private static void HandleXapk(string gamePath, ref Cpp2IlRuntimeArgs args)
403396
var ggmBytes = new byte[0x40];
404397
using var ggmStream = globalgamemanagers.Open();
405398

406-
// ReSharper disable once MustUseReturnValue
407-
ggmStream.Read(ggmBytes, 0, 0x40);
399+
ggmStream.ReadExactly(ggmBytes, 0, 0x40);
408400

409401
args.UnityVersion = Cpp2IlApi.GetVersionFromGlobalGameManagers(ggmBytes);
410402
}
@@ -469,8 +461,7 @@ private static void HandleIpa(string gamePath, ref Cpp2IlRuntimeArgs args)
469461
var ggmBytes = new byte[0x40];
470462
using var ggmStream = globalgamemanagers.Open();
471463

472-
// ReSharper disable once MustUseReturnValue
473-
ggmStream.Read(ggmBytes, 0, 0x40);
464+
ggmStream.ReadExactly(ggmBytes, 0, 0x40);
474465

475466
args.UnityVersion = Cpp2IlApi.GetVersionFromGlobalGameManagers(ggmBytes);
476467
}
@@ -768,4 +759,18 @@ private static void CleanupExtractedFiles()
768759
}
769760
}
770761
}
762+
763+
#if !NET7_0_OR_GREATER
764+
private static void ReadExactly(this Stream stream, byte[] buffer, int offset, int count)
765+
{
766+
var totalRead = 0;
767+
while (totalRead < count)
768+
{
769+
var bytesRead = stream.Read(buffer, offset + totalRead, count - totalRead);
770+
if (bytesRead == 0)
771+
throw new EndOfStreamException("Could not read enough bytes from stream");
772+
totalRead += bytesRead;
773+
}
774+
}
775+
#endif
771776
}

LibCpp2IL/BinarySearcher.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public ulong FindCodeRegistrationPre2019()
115115
LibLogger.VerboseNewline($"\t\t\tChecking for CodeRegistration at virtual address 0x{va:x}...");
116116
var cr = binary.ReadReadableAtVirtualAddress<Il2CppCodeRegistration>(va);
117117

118-
if ((long)cr.customAttributeCount == LibCpp2IlMain.TheMetadata!.attributeTypeRanges.Count)
118+
if ((long)cr.customAttributeCount == LibCpp2IlMain.TheMetadata!.attributeTypeRanges!.Count)
119119
return va;
120120

121121
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()
297297
{
298298
var mr = binary.ReadReadableAtVirtualAddress<Il2CppMetadataRegistration>(potentialMetaRegPointer);
299299

300-
if (mr.metadataUsagesCount == (ulong)LibCpp2IlMain.TheMetadata!.metadataUsageLists.Length)
300+
if (mr.metadataUsagesCount == (ulong)LibCpp2IlMain.TheMetadata!.metadataUsageLists!.Length)
301301
{
302302
LibLogger.VerboseNewline($"\t\t\tFound and selected probably valid metadata registration at 0x{potentialMetaRegPointer:X}.");
303303
return potentialMetaRegPointer;

LibCpp2IL/BinaryStructures/Il2CppRGCTXDefinition.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using LibCpp2IL.Reflection;
1+
using LibCpp2IL.Reflection;
22

33
namespace LibCpp2IL.BinaryStructures;
44

@@ -7,9 +7,9 @@ public class Il2CppRGCTXDefinition : ReadableClass
77
public Il2CppRGCTXDataType type;
88
public int _rawIndex;
99

10-
public int MethodIndex => type == Il2CppRGCTXDataType.IL2CPP_RGCTX_DATA_CONSTRAINED ? _constrainedData.MethodIndex : _defData.MethodIndex;
10+
public int MethodIndex => _defData?.MethodIndex ?? _constrainedData!.MethodIndex;
1111

12-
public int TypeIndex => type == Il2CppRGCTXDataType.IL2CPP_RGCTX_DATA_CONSTRAINED ? _constrainedData.TypeIndex : _defData.TypeIndex;
12+
public int TypeIndex => _defData?.TypeIndex ?? _constrainedData!.TypeIndex;
1313

1414
public Il2CppMethodSpec? MethodSpec => LibCpp2IlMain.Binary?.GetMethodSpec(MethodIndex);
1515

@@ -41,9 +41,9 @@ public override void Read(ClassReadingBinaryReader reader)
4141
}
4242
}
4343
[Version(Min = 27.2f)]
44-
private Il2CppRGCTXConstrainedData _constrainedData;
44+
private Il2CppRGCTXConstrainedData? _constrainedData;
4545

46-
private Il2CppRGCTXDefinitionData _defData;
46+
private Il2CppRGCTXDefinitionData? _defData;
4747
public override void Read(ClassReadingBinaryReader reader)
4848
{
4949
type = IsLessThan(29) ? (Il2CppRGCTXDataType)reader.ReadInt32() : (Il2CppRGCTXDataType)reader.ReadInt64();

LibCpp2IL/Metadata/Il2CppTypeDefinition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public Il2CppRGCTXDefinition[] RgctXs
139139
if (LibCpp2IlMain.MetadataVersion < 24.2f)
140140
{
141141
//No codegen modules here.
142-
return LibCpp2IlMain.TheMetadata!.RgctxDefinitions.Skip(RgctxStartIndex).Take(RgctxCount).ToArray();
142+
return LibCpp2IlMain.TheMetadata!.RgctxDefinitions!.Skip(RgctxStartIndex).Take(RgctxCount).ToArray();
143143
}
144144

145145
var cgm = CodeGenModule;

LibCpp2IL/NintendoSwitch/NsoFile.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics.CodeAnalysis;
44
using System.IO;
@@ -257,7 +257,7 @@ public NsoFile Decompress()
257257
var unCompressedData = new byte[_header.TextSegment.DecompressedSize];
258258
using (var decoder = new Lz4DecodeStream(new MemoryStream(textBytes)))
259259
{
260-
decoder.Read(unCompressedData, 0, unCompressedData.Length);
260+
decoder.ReadExactly(unCompressedData, 0, unCompressedData.Length);
261261
}
262262

263263
writer.Write(unCompressedData);
@@ -273,7 +273,7 @@ public NsoFile Decompress()
273273
var unCompressedData = new byte[_header.RoDataSegment.DecompressedSize];
274274
using (var decoder = new Lz4DecodeStream(new MemoryStream(roDataBytes)))
275275
{
276-
decoder.Read(unCompressedData, 0, unCompressedData.Length);
276+
decoder.ReadExactly(unCompressedData, 0, unCompressedData.Length);
277277
}
278278

279279
writer.Write(unCompressedData);
@@ -289,7 +289,7 @@ public NsoFile Decompress()
289289
var unCompressedData = new byte[_header.DataSegment.DecompressedSize];
290290
using (var decoder = new Lz4DecodeStream(new MemoryStream(dataBytes)))
291291
{
292-
decoder.Read(unCompressedData, 0, unCompressedData.Length);
292+
decoder.ReadExactly(unCompressedData, 0, unCompressedData.Length);
293293
}
294294

295295
writer.Write(unCompressedData);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#if !NET7_0_OR_GREATER
2+
using System.IO;
3+
4+
namespace LibCpp2IL.NintendoSwitch;
5+
6+
internal static class StreamExtensions
7+
{
8+
public static void ReadExactly(this Stream stream, byte[] buffer, int offset, int count)
9+
{
10+
var totalRead = 0;
11+
while (totalRead < count)
12+
{
13+
var bytesRead = stream.Read(buffer, offset + totalRead, count - totalRead);
14+
if (bytesRead == 0)
15+
throw new EndOfStreamException("Could not read enough bytes from stream");
16+
totalRead += bytesRead;
17+
}
18+
}
19+
}
20+
#endif

0 commit comments

Comments
 (0)