Skip to content

Commit 51d6bf1

Browse files
committed
clanker-assisted refactor to remove static state
1 parent 25552e0 commit 51d6bf1

94 files changed

Lines changed: 838 additions & 1226 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cpp2IL.Core.Tests/Cpp2IlApiTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using NUnit.Framework;
2-
31
namespace Cpp2IL.Core.Tests;
42

53
public class Cpp2IlApiTests

Cpp2IL.Core/Api/Cpp2IlPlugin.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4-
using Cpp2IL.Core.Logging;
54
using LibCpp2IL;
65

76
namespace Cpp2IL.Core.Api;

Cpp2IL.Core/Cpp2IlApi.cs

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static void ConfigureLib(bool allowUserToInputAddresses)
5151
LibCpp2IlMain.Settings.MetadataFixupFunc = Cpp2IlPluginManager.MetadataFixupFuncs is { } funcs ? (originalBytes, version) =>
5252
{
5353
Logger.InfoNewline("Received request for metadata fixup from LibCpp2Il. Calling registered plugin fixup functions...");
54-
54+
5555
foreach (var func in funcs)
5656
{
5757
try
@@ -68,7 +68,7 @@ public static void ConfigureLib(bool allowUserToInputAddresses)
6868
Logger.ErrorNewline($"Metadata fixup function threw an exception: {e}. Ignoring and trying next fixup function, if any...");
6969
}
7070
}
71-
71+
7272
//only get here if every fixup function returns null or throws.
7373
return null;
7474
} : null;
@@ -85,20 +85,16 @@ public static void InitializeLibCpp2Il(string assemblyPath, string metadataPath,
8585

8686
ConfigureLib(allowUserToInputAddresses);
8787

88-
#if !DEBUG
89-
try
90-
{
91-
#endif
92-
if (!LibCpp2IlMain.LoadFromFile(assemblyPath, metadataPath, unityVersion))
93-
throw new Exception("Initialization with LibCpp2Il failed");
94-
#if !DEBUG
95-
}
96-
catch (Exception e)
97-
{
98-
throw new LibCpp2ILInitializationException("Fatal Exception initializing LibCpp2IL!", e);
99-
}
100-
#endif
101-
OnLibInitialized();
88+
try
89+
{
90+
var context = LibCpp2IlMain.LoadFromFileAsContext(assemblyPath, metadataPath, unityVersion);
91+
OnLibInitialized(context);
92+
}
93+
catch (Exception e)
94+
{
95+
throw new LibCpp2ILInitializationException("Fatal Exception initializing LibCpp2IL!", e);
96+
}
97+
10298
}
10399

104100
[MemberNotNull(nameof(CurrentAppContext))]
@@ -112,27 +108,24 @@ public static void InitializeLibCpp2Il(byte[] assemblyData, byte[] metadataData,
112108

113109
try
114110
{
115-
if (!LibCpp2IlMain.Initialize(assemblyData, metadataData, unityVersion))
116-
throw new Exception("Initialization with LibCpp2Il failed");
111+
var context = LibCpp2IlMain.InitializeAsContext(assemblyData, metadataData, unityVersion);
112+
OnLibInitialized(context);
117113
}
118114
catch (Exception e)
119115
{
120116
throw new LibCpp2ILInitializationException("Fatal Exception initializing LibCpp2IL!", e);
121117
}
122-
123-
OnLibInitialized();
124118
}
125119

126120
[MemberNotNull(nameof(CurrentAppContext))]
127-
private static void OnLibInitialized()
121+
private static void OnLibInitialized(LibCpp2IlContext libContext)
128122
{
129-
MiscUtils.Init();
130-
LibCpp2IlMain.Binary!.AllCustomAttributeGenerators.ToList()
123+
libContext.Binary.AllCustomAttributeGenerators.ToList()
131124
.ForEach(ptr => SharedState.AttributeGeneratorStarts.Add(ptr));
132125

133126
var start = DateTime.Now;
134127
Logger.InfoNewline("Creating application model...");
135-
CurrentAppContext = new(LibCpp2IlMain.Binary, LibCpp2IlMain.TheMetadata!);
128+
CurrentAppContext = new(libContext);
136129
Logger.InfoNewline($"Application model created in {(DateTime.Now - start).TotalMilliseconds}ms");
137130
}
138131

@@ -144,8 +137,6 @@ public static void ResetInternalState()
144137

145138
AsmResolverUtils.Reset();
146139

147-
LibCpp2IlMain.Reset();
148-
149140
CurrentAppContext = null;
150141
}
151142

@@ -176,6 +167,6 @@ public static void ResetInternalState()
176167

177168
private static bool IsLibInitialized()
178169
{
179-
return LibCpp2IlMain.Binary != null && LibCpp2IlMain.TheMetadata != null;
170+
return CurrentAppContext != null;
180171
}
181172
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
using System;
2-
using LibCpp2IL;
1+
using System;
32

43
namespace Cpp2IL.Core.Exceptions;
54

6-
public class UnsupportedInstructionSetException : Exception
5+
public class UnsupportedInstructionSetException(string? instructionSetId = null) : Exception
76
{
8-
public override string Message => $"This action is not supported on the {LibCpp2IlMain.Binary?.InstructionSetId} instruction set yet. If running the CLI, try adding the --skip-analysis argument.";
7+
public override string Message => $"This action is not supported on the {instructionSetId ?? "unknown"} instruction set yet. If running the CLI, try adding the --skip-analysis argument.";
98
}

Cpp2IL.Core/Graphs/ISILControlFlowGraph.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
44
using System.Linq;
5-
using System.Text;
65
using Cpp2IL.Core.ISIL;
76

87
namespace Cpp2IL.Core.Graphs;

Cpp2IL.Core/Graphs/Processors/CallProcessor.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Linq;
21
using Cpp2IL.Core.Il2CppApiFunctions;
32
using Cpp2IL.Core.ISIL;
43
using Cpp2IL.Core.Model.Contexts;

Cpp2IL.Core/Graphs/Processors/MetadataProcessor.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using System.Linq;
21
using Cpp2IL.Core.ISIL;
32
using Cpp2IL.Core.Model.Contexts;
43
using Cpp2IL.Core.Utils;
5-
using LibCpp2IL;
64

75
namespace Cpp2IL.Core.Graphs.Processors;
86

@@ -26,11 +24,11 @@ public void Process(MethodAnalysisContext methodAnalysisContext, Block block)
2624
var memoryOp = (IsilMemoryOperand)instruction.Operands[1].Data;
2725
if (memoryOp.Base == null && memoryOp.Index == null && memoryOp.Scale == 0)
2826
{
29-
var val = LibCpp2IlMain.GetLiteralByAddress((ulong)memoryOp.Addend);
27+
var val = methodAnalysisContext.AppContext.LibCpp2IlContext.GetLiteralByAddress((ulong)memoryOp.Addend);
3028
if (val == null)
3129
{
3230
// Try instead check if its type metadata usage
33-
var metadataUsage = LibCpp2IlMain.GetTypeGlobalByAddress((ulong)memoryOp.Addend);
31+
var metadataUsage = methodAnalysisContext.AppContext.LibCpp2IlContext.GetTypeGlobalByAddress((ulong)memoryOp.Addend);
3432
if (metadataUsage != null && methodAnalysisContext.DeclaringType is not null)
3533
{
3634
var typeAnalysisContext = metadataUsage.ToContext(methodAnalysisContext.DeclaringType!.DeclaringAssembly);

Cpp2IL.Core/ISIL/InstructionSetIndependentInstruction.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System.Collections.Generic;
2-
31
namespace Cpp2IL.Core.ISIL;
42

53
public class InstructionSetIndependentInstruction : IsilOperandData

Cpp2IL.Core/ISIL/InstructionSetIndependentOpCode.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Linq;
32
using Cpp2IL.Core.Extensions;
43

Cpp2IL.Core/ISIL/IsilBuilder.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using Cpp2IL.Core.Exceptions;
54

65
namespace Cpp2IL.Core.ISIL;
76

0 commit comments

Comments
 (0)