Skip to content

Commit 217f1d4

Browse files
committed
代码清理
1 parent 101f3c6 commit 217f1d4

23 files changed

+217
-257
lines changed

Il2CppDumper/ExecutableFormats/Macho.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ namespace Il2CppDumper
99
{
1010
public sealed class Macho : Il2Cpp
1111
{
12-
private List<MachoSection> sections = new List<MachoSection>();
1312
private static readonly byte[] FeatureBytes1 = { 0x0, 0x22 };//MOVS R2, #0
1413
private static readonly byte[] FeatureBytes2 = { 0x78, 0x44, 0x79, 0x44 };//ADD R0, PC and ADD R1, PC
15-
private ulong vmaddr;
14+
private readonly List<MachoSection> sections = new();
15+
private readonly ulong vmaddr;
1616

1717
public Macho(Stream stream) : base(stream)
1818
{

Il2CppDumper/ExecutableFormats/Macho64.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ namespace Il2CppDumper
99
{
1010
public sealed class Macho64 : Il2Cpp
1111
{
12-
private List<MachoSection64Bit> sections = new List<MachoSection64Bit>();
1312
private static readonly byte[] FeatureBytes1 = { 0x2, 0x0, 0x80, 0xD2 };//MOV X2, #0
1413
private static readonly byte[] FeatureBytes2 = { 0x3, 0x0, 0x80, 0x52 };//MOV W3, #0
15-
private ulong vmaddr;
14+
private readonly List<MachoSection64Bit> sections = new();
15+
private readonly ulong vmaddr;
1616

1717
public Macho64(Stream stream) : base(stream)
1818
{

Il2CppDumper/ExecutableFormats/MachoFat.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
using System;
1+
using System.Buffers.Binary;
22
using System.IO;
3-
using System.Linq;
43

54
namespace Il2CppDumper
65
{
@@ -10,16 +9,17 @@ public sealed class MachoFat : BinaryStream
109

1110
public MachoFat(Stream stream) : base(stream)
1211
{
13-
//BigEndian
1412
Position += 4;
15-
var size = BitConverter.ToInt32(ReadBytes(4).Reverse().ToArray(), 0);
13+
var size = BinaryPrimitives.ReadInt32BigEndian(ReadBytes(4));
1614
fats = new Fat[size];
1715
for (var i = 0; i < size; i++)
1816
{
1917
Position += 8;
20-
fats[i] = new Fat();
21-
fats[i].offset = BitConverter.ToUInt32(ReadBytes(4).Reverse().ToArray(), 0);
22-
fats[i].size = BitConverter.ToUInt32(ReadBytes(4).Reverse().ToArray(), 0);
18+
fats[i] = new Fat
19+
{
20+
offset = BinaryPrimitives.ReadUInt32BigEndian(ReadBytes(4)),
21+
size = BinaryPrimitives.ReadUInt32BigEndian(ReadBytes(4))
22+
};
2323
Position += 4;
2424
}
2525
for (var i = 0; i < size; i++)

Il2CppDumper/ExecutableFormats/NSO.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,25 @@ namespace Il2CppDumper
88
{
99
public sealed class NSO : Il2Cpp
1010
{
11-
private NSOHeader header;
12-
private bool isTextCompressed;
13-
private bool isRoDataCompressed;
14-
private bool isDataCompressed;
15-
private List<NSOSegmentHeader> segments = new List<NSOSegmentHeader>();
11+
private readonly NSOHeader header;
12+
private readonly bool isTextCompressed;
13+
private readonly bool isRoDataCompressed;
14+
private readonly bool isDataCompressed;
15+
private readonly List<NSOSegmentHeader> segments = new();
1616
private Elf64_Sym[] symbolTable;
17-
private List<Elf64_Dyn> dynamicSection = new List<Elf64_Dyn>();
18-
private bool isCompressed => isTextCompressed || isRoDataCompressed || isDataCompressed;
17+
private readonly List<Elf64_Dyn> dynamicSection = new();
18+
private bool IsCompressed => isTextCompressed || isRoDataCompressed || isDataCompressed;
1919

2020

2121
public NSO(Stream stream) : base(stream)
2222
{
23-
header = new NSOHeader();
24-
header.Magic = ReadUInt32();
25-
header.Version = ReadUInt32();
26-
header.Reserved = ReadUInt32();
27-
header.Flags = ReadUInt32();
23+
header = new NSOHeader
24+
{
25+
Magic = ReadUInt32(),
26+
Version = ReadUInt32(),
27+
Reserved = ReadUInt32(),
28+
Flags = ReadUInt32()
29+
};
2830
isTextCompressed = (header.Flags & 1) != 0;
2931
isRoDataCompressed = (header.Flags & 2) != 0;
3032
isDataCompressed = (header.Flags & 4) != 0;
@@ -76,7 +78,7 @@ public NSO(Stream stream) : base(stream)
7678
header.RoDataHash = ReadBytes(0x20);
7779
header.DataHash = ReadBytes(0x20);
7880

79-
if (!isCompressed)
81+
if (!IsCompressed)
8082
{
8183
Position = header.TextSegment.FileOffset + 4;
8284
var modOffset = ReadUInt32();

Il2CppDumper/ExecutableFormats/PE.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Il2CppDumper
77
{
88
public sealed class PE : Il2Cpp
99
{
10-
private SectionHeader[] sections;
10+
private readonly SectionHeader[] sections;
1111

1212
public PE(Stream stream) : base(stream)
1313
{

Il2CppDumper/ExecutableFormats/WebAssembly.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Il2CppDumper
55
{
66
public sealed class WebAssembly : BinaryStream
77
{
8-
private DataSection[] dataSections;
8+
private readonly DataSection[] dataSections;
99

1010
public WebAssembly(Stream stream) : base(stream)
1111
{
@@ -46,7 +46,7 @@ public WebAssembly(Stream stream) : base(stream)
4646

4747
public WebAssemblyMemory CreateMemory()
4848
{
49-
var last = dataSections[dataSections.Length - 1];
49+
var last = dataSections[^1];
5050
var bssStart = last.Offset + (uint)last.Data.Length;
5151
var stream = new MemoryStream(new byte[Length]);
5252
foreach (var dataSection in dataSections)

Il2CppDumper/ExecutableFormats/WebAssemblyMemory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Il2CppDumper
44
{
55
public sealed class WebAssemblyMemory : Il2Cpp
66
{
7-
private uint bssStart;
7+
private readonly uint bssStart;
88

99
public WebAssemblyMemory(Stream stream, uint bssStart) : base(stream)
1010
{

Il2CppDumper/Extensions/BinaryReaderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static string ReadString(this BinaryReader reader, int numChars)
1010
{
1111
var start = reader.BaseStream.Position;
1212
// UTF8 takes up to 4 bytes per character
13-
var str = Encoding.UTF8.GetString(reader.ReadBytes(numChars * 4)).Substring(0, numChars);
13+
var str = Encoding.UTF8.GetString(reader.ReadBytes(numChars * 4))[..numChars];
1414
// make our position what it would have been if we'd known the exact number of bytes needed.
1515
reader.BaseStream.Position = start;
1616
reader.ReadBytes(Encoding.UTF8.GetByteCount(str));

Il2CppDumper/IO/BinaryStream.cs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ public class BinaryStream : IDisposable
1212
public double Version;
1313
public bool Is32Bit;
1414
public ulong ImageBase;
15-
private Stream stream;
16-
private BinaryReader reader;
17-
private BinaryWriter writer;
18-
private MethodInfo readClass;
19-
private MethodInfo readClassArray;
20-
private Dictionary<Type, MethodInfo> genericMethodCache = new Dictionary<Type, MethodInfo>();
21-
private Dictionary<FieldInfo, VersionAttribute[]> attributeCache = new Dictionary<FieldInfo, VersionAttribute[]>();
15+
private readonly Stream stream;
16+
private readonly BinaryReader reader;
17+
private readonly BinaryWriter writer;
18+
private readonly MethodInfo readClass;
19+
private readonly MethodInfo readClassArray;
20+
private readonly Dictionary<Type, MethodInfo> genericMethodCache;
21+
private readonly Dictionary<FieldInfo, VersionAttribute[]> attributeCache;
2222

2323
public BinaryStream(Stream input)
2424
{
@@ -27,6 +27,8 @@ public BinaryStream(Stream input)
2727
writer = new BinaryWriter(stream, Encoding.UTF8, true);
2828
readClass = GetType().GetMethod("ReadClass", Type.EmptyTypes);
2929
readClassArray = GetType().GetMethod("ReadClassArray", new[] { typeof(long) });
30+
genericMethodCache = new();
31+
attributeCache = new();
3032
}
3133

3234
public bool ReadBoolean() => reader.ReadBoolean();
@@ -91,26 +93,17 @@ public ulong Position
9193

9294
private object ReadPrimitive(Type type)
9395
{
94-
var typename = type.Name;
95-
switch (typename)
96+
return type.Name switch
9697
{
97-
case "Int32":
98-
return ReadInt32();
99-
case "UInt32":
100-
return ReadUInt32();
101-
case "Int16":
102-
return ReadInt16();
103-
case "UInt16":
104-
return ReadUInt16();
105-
case "Byte":
106-
return ReadByte();
107-
case "Int64":
108-
return ReadIntPtr();
109-
case "UInt64":
110-
return ReadUIntPtr();
111-
default:
112-
throw new NotSupportedException();
113-
}
98+
"Int32" => ReadInt32(),
99+
"UInt32" => ReadUInt32(),
100+
"Int16" => ReadInt16(),
101+
"UInt16" => ReadUInt16(),
102+
"Byte" => ReadByte(),
103+
"Int64" => ReadIntPtr(),
104+
"UInt64" => ReadUIntPtr(),
105+
_ => throw new NotSupportedException()
106+
};
114107
}
115108

116109
public T ReadClass<T>(ulong addr) where T : new()
@@ -243,15 +236,16 @@ protected virtual void Dispose(bool disposing)
243236
{
244237
if (disposing)
245238
{
246-
reader.Close();
247-
writer.Close();
239+
reader.Dispose();
240+
writer.Dispose();
248241
stream.Close();
249242
}
250243
}
251244

252245
public void Dispose()
253246
{
254247
Dispose(true);
248+
GC.SuppressFinalize(this);
255249
}
256250
}
257251
}

Il2CppDumper/Il2Cpp/Il2Cpp.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ public abstract class Il2Cpp : BinaryStream
1717
public ulong[] unresolvedVirtualCallPointers;
1818
private ulong[] fieldOffsets;
1919
public Il2CppType[] types;
20-
private Dictionary<ulong, Il2CppType> typeDic = new Dictionary<ulong, Il2CppType>();
20+
private readonly Dictionary<ulong, Il2CppType> typeDic = new();
2121
public ulong[] metadataUsages;
2222
private Il2CppGenericMethodFunctionsDefinitions[] genericMethodTable;
2323
public ulong[] genericInstPointers;
2424
public Il2CppGenericInst[] genericInsts;
2525
public Il2CppMethodSpec[] methodSpecs;
26-
public Dictionary<int, List<Il2CppMethodSpec>> methodDefinitionMethodSpecs = new Dictionary<int, List<Il2CppMethodSpec>>();
27-
public Dictionary<Il2CppMethodSpec, ulong> methodSpecGenericMethodPointers = new Dictionary<Il2CppMethodSpec, ulong>();
26+
public Dictionary<int, List<Il2CppMethodSpec>> methodDefinitionMethodSpecs = new();
27+
public Dictionary<Il2CppMethodSpec, ulong> methodSpecGenericMethodPointers = new();
2828
private bool fieldOffsetsArePointers;
2929
protected long metadataUsagesCount;
3030
public Dictionary<string, Il2CppCodeGenModule> codeGenModules;

0 commit comments

Comments
 (0)