Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cpp2IL.Core/Model/Contexts/HasCustomAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<byte>.Empty;
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 19 additions & 14 deletions Cpp2IL/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,16 @@

namespace Cpp2IL;

[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")]
internal class Program
internal static class Program
{
private static readonly List<string> PathsToDeleteOnExit = [];

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

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...");

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions LibCpp2IL/BinarySearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public ulong FindCodeRegistrationPre2019()
LibLogger.VerboseNewline($"\t\t\tChecking for CodeRegistration at virtual address 0x{va:x}...");
var cr = binary.ReadReadableAtVirtualAddress<Il2CppCodeRegistration>(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}");
Expand Down Expand Up @@ -297,7 +297,7 @@ public ulong FindMetadataRegistrationPre24_5()
{
var mr = binary.ReadReadableAtVirtualAddress<Il2CppMetadataRegistration>(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;
Expand Down
10 changes: 5 additions & 5 deletions LibCpp2IL/BinaryStructures/Il2CppRGCTXDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using LibCpp2IL.Reflection;
using LibCpp2IL.Reflection;

namespace LibCpp2IL.BinaryStructures;

Expand All @@ -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);

Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion LibCpp2IL/Metadata/Il2CppTypeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions LibCpp2IL/NintendoSwitch/NsoFile.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions LibCpp2IL/NintendoSwitch/StreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -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
Loading