Skip to content

Commit

Permalink
Call analysis improvements (SamboyCoding#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
ds5678 authored and CyberiaResurrection committed Dec 17, 2022
1 parent bdc1b84 commit 11b1aae
Show file tree
Hide file tree
Showing 136 changed files with 28,271 additions and 156 deletions.
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ packages/
\.idea/

Cpp2IL/bin/

Cpp2IL/obj/

LibCpp2IL/bin/

LibCpp2IL/obj/

LibCpp2ILTests/bin/

LibCpp2ILTests/obj/
Cpp2IL.Core/obj/

Cpp2IL.Core/obj/
Cpp2IL.Core/bin/

Cpp2IL.Core.Tests/obj/
Cpp2IL.Core.Tests/bin/

Il2CppBinaryAnalyzer/obj/
Il2CppBinaryAnalyzer/bin/

Expand Down
48 changes: 48 additions & 0 deletions Cpp2IL.Core.Tests/AccessibilityUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Linq;
using Cpp2IL.Core.Model.Contexts;
using Cpp2IL.Core.Utils;

namespace Cpp2IL.Core.Tests;

public class AccessibilityUtilsTests
{
[Test]
public void AccessibilityTests()
{
var appContext = GameLoader.LoadSimpleGame();
var mscorlib = appContext.AssembliesByName["mscorlib"];
var coreModule = appContext.AssembliesByName["UnityEngine.CoreModule"];

var console = GetTypeByFullName(mscorlib, "System.Console");//public
var consoleWindowsConsole = GetTypeByFullName(mscorlib, "System.Console.WindowsConsole");//private nested
var dateTimeFormat = GetTypeByFullName(mscorlib, "System.DateTimeFormat");//internal
var gameObject = GetTypeByFullName(coreModule, "UnityEngine.GameObject");//public
Assert.Multiple(() =>
{
AssertAccessibleTo(console, consoleWindowsConsole);
AssertAccessibleTo(consoleWindowsConsole, console);
AssertAccessibleTo(console, gameObject);
AssertNotAccessibleTo(consoleWindowsConsole, gameObject);
AssertNotAccessibleTo(gameObject, console);
AssertAccessibleTo(consoleWindowsConsole, consoleWindowsConsole);
AssertAccessibleTo(dateTimeFormat, consoleWindowsConsole);
AssertNotAccessibleTo(consoleWindowsConsole, dateTimeFormat);
AssertNotAccessibleTo(dateTimeFormat, gameObject);
});
}

private static void AssertAccessibleTo(TypeAnalysisContext type1, TypeAnalysisContext type2)
{
Assert.That(type1.IsAccessibleTo(type2), () => $"{type1.FullName} is not accessible to {type2.FullName}, but should be.");
}

private static void AssertNotAccessibleTo(TypeAnalysisContext type1, TypeAnalysisContext type2)
{
Assert.That(!type1.IsAccessibleTo(type2), () => $"{type1.FullName} is accessible to {type2.FullName}, but shouldn't be.");
}

private static TypeAnalysisContext GetTypeByFullName(AssemblyAnalysisContext assembly, string fullName)
{
return assembly.Types.FirstOrDefault(t => t.FullName == fullName) ?? throw new($"Could not find {fullName} in {assembly.CleanAssemblyName}.");
}
}
22 changes: 22 additions & 0 deletions Cpp2IL.Core.Tests/Cpp2IL.Core.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Cpp2IL.Core\Cpp2IL.Core.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.5.0" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions Cpp2IL.Core.Tests/Cpp2IlApiTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using NUnit.Framework;

namespace Cpp2IL.Core.Tests;
public class Cpp2IlApiTests
{
[Test]
public void UnityVersionIsCorrectlyDeterminedFromGlobalGameManagers()
{
var version = Cpp2IlApi.DetermineUnityVersion(null, Paths.SimpleGame.DataDirectory);
Assert.That(version.IsEqual(2019, 4, 34));
}
}
35 changes: 35 additions & 0 deletions Cpp2IL.Core.Tests/GameLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using AssetRipper.VersionUtilities;
using Cpp2IL.Core.Api;
using Cpp2IL.Core.CorePlugin;
using Cpp2IL.Core.Model.Contexts;
using LibCpp2IL;

namespace Cpp2IL.Core.Tests;

public static class GameLoader
{
static GameLoader()
{
InstructionSetRegistry.RegisterInstructionSet<X86InstructionSet>(DefaultInstructionSets.X86_32);
InstructionSetRegistry.RegisterInstructionSet<X86InstructionSet>(DefaultInstructionSets.X86_64);
InstructionSetRegistry.RegisterInstructionSet<WasmInstructionSet>(DefaultInstructionSets.WASM);
InstructionSetRegistry.RegisterInstructionSet<ArmV7InstructionSet>(DefaultInstructionSets.ARM_V7);
var useNewArm64 = true;
if (useNewArm64)
{
InstructionSetRegistry.RegisterInstructionSet<NewArmV8InstructionSet>(DefaultInstructionSets.ARM_V8);
}
else
{
InstructionSetRegistry.RegisterInstructionSet<Arm64InstructionSet>(DefaultInstructionSets.ARM_V8);
}

LibCpp2IlBinaryRegistry.RegisterBuiltInBinarySupport();
}

public static ApplicationAnalysisContext LoadSimpleGame()
{
Cpp2IlApi.InitializeLibCpp2Il(Paths.SimpleGame.GameAssembly, Paths.SimpleGame.Metadata, new UnityVersion(2019, 4, 34, UnityVersionType.Final, 1));
return Cpp2IlApi.CurrentAppContext;
}
}
16 changes: 16 additions & 0 deletions Cpp2IL.Core.Tests/Paths.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Cpp2IL.Core.Tests;
public static class Paths
{
public const string TestProjectDirectory = "../../../";
public const string RepositoryRootDirectory = "../" + TestProjectDirectory;
public const string TestFilesDirectory = RepositoryRootDirectory + "TestFiles/";

public static class SimpleGame
{
public const string RootDirectory = TestFilesDirectory + "Simple_2019_4_34/";
public const string DataDirectory = RootDirectory + "Simple_2019_4_34_Data/";
public const string ExecutableFile = RootDirectory + "Simple_2019_4_34.exe";
public const string GameAssembly = RootDirectory + "GameAssembly.dll";
public const string Metadata = DataDirectory + "il2cpp_data/Metadata/global-metadata.dat";
}
}
1 change: 1 addition & 0 deletions Cpp2IL.Core.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;
Loading

0 comments on commit 11b1aae

Please sign in to comment.