|
1 |
| -using System.Linq; |
| 1 | +using System.Linq; |
2 | 2 | using System.Reflection;
|
3 |
| -using AssetRipper.Primitives; |
4 | 3 |
|
5 | 4 | namespace Cpp2IL.Core.Tests;
|
6 | 5 |
|
@@ -32,7 +31,7 @@ public void TestZeroArgumentMethodInjection()
|
32 | 31 | var baseType = appContext!.SystemTypes.SystemObjectType;
|
33 | 32 | var injectedType = appContext.InjectTypeIntoAllAssemblies("Cpp2ILInjected", "TestInjectedTypeWithMethods", baseType);
|
34 | 33 |
|
35 |
| - var methodsByAssembly = injectedType.InjectMethodToAllAssemblies("TestZeroArgMethod", false, appContext.SystemTypes.SystemVoidType, MethodAttributes.Public); |
| 34 | + var methodsByAssembly = injectedType.InjectMethodToAllAssemblies("TestZeroArgMethod", appContext.SystemTypes.SystemVoidType, MethodAttributes.Public); |
36 | 35 |
|
37 | 36 | Assert.That(methodsByAssembly, Has.Count.EqualTo(appContext.Assemblies.Count));
|
38 | 37 | Assert.That(methodsByAssembly.Values.First(), Has.Property("Name").EqualTo("TestZeroArgMethod").And.Property("ReturnTypeContext").EqualTo(appContext.SystemTypes.SystemVoidType));
|
@@ -62,7 +61,7 @@ public void TestMethodWithParametersInjection()
|
62 | 61 | var baseType = appContext!.SystemTypes.SystemObjectType;
|
63 | 62 | var injectedType = appContext.InjectTypeIntoAllAssemblies("Cpp2ILInjected", "TestInjectedTypeWithMethodsWithParameters", baseType);
|
64 | 63 |
|
65 |
| - var methodsByAssembly = injectedType.InjectMethodToAllAssemblies("TestMethodWithParameters", false, appContext.SystemTypes.SystemVoidType, MethodAttributes.Public, appContext.SystemTypes.SystemInt32Type, appContext.SystemTypes.SystemStringType); |
| 64 | + var methodsByAssembly = injectedType.InjectMethodToAllAssemblies("TestMethodWithParameters", appContext.SystemTypes.SystemVoidType, MethodAttributes.Public, appContext.SystemTypes.SystemInt32Type, appContext.SystemTypes.SystemStringType); |
66 | 65 |
|
67 | 66 | Assert.That(methodsByAssembly, Has.Count.EqualTo(appContext.Assemblies.Count));
|
68 | 67 | Assert.That(methodsByAssembly.Values.First(), Has.Property("Name").EqualTo("TestMethodWithParameters").And.Property("ReturnTypeContext").EqualTo(appContext.SystemTypes.SystemVoidType));
|
@@ -94,4 +93,78 @@ public void TestFieldInjection()
|
94 | 93 | Assert.That(fieldsByAssembly.Values.First(), Has.Property("Name").EqualTo("TestField").And.Property("FieldTypeContext").EqualTo(appContext.SystemTypes.SystemInt32Type));
|
95 | 94 | Assert.That(fieldsByAssembly.Values.First().DeclaringType, Has.Property("FullName").EqualTo("Cpp2ILInjected.TestInjectedTypeWithFields"));
|
96 | 95 | }
|
| 96 | + |
| 97 | + [Test] |
| 98 | + public void TestPropertyInjection() |
| 99 | + { |
| 100 | + var appContext = Cpp2IlApi.CurrentAppContext; |
| 101 | + |
| 102 | + var baseType = appContext!.SystemTypes.SystemObjectType; |
| 103 | + var injectedType = appContext.InjectTypeIntoAllAssemblies("Cpp2ILInjected", "TestInjectedTypeWithProperties", baseType); |
| 104 | + var gettersByAssembly = injectedType.InjectMethodToAllAssemblies("get_TestProperty", appContext.SystemTypes.SystemInt32Type, MethodAttributes.Public); |
| 105 | + var propertiesByAssembly = injectedType.InjectPropertyToAllAssemblies("TestProperty", appContext.SystemTypes.SystemInt32Type, gettersByAssembly, null, PropertyAttributes.None); |
| 106 | + |
| 107 | + Assert.That(propertiesByAssembly, Has.Count.EqualTo(appContext.Assemblies.Count)); |
| 108 | + Assert.That(propertiesByAssembly.Values.First(), Has.Property("Name").EqualTo("TestProperty").And.Property("PropertyTypeContext").EqualTo(appContext.SystemTypes.SystemInt32Type)); |
| 109 | + Assert.That(propertiesByAssembly.Values.First().DeclaringType, Has.Property("FullName").EqualTo("Cpp2ILInjected.TestInjectedTypeWithProperties")); |
| 110 | + } |
| 111 | + |
| 112 | + [Test] |
| 113 | + public void TestEventInjection() |
| 114 | + { |
| 115 | + var appContext = Cpp2IlApi.CurrentAppContext; |
| 116 | + |
| 117 | + var baseType = appContext!.SystemTypes.SystemObjectType; |
| 118 | + var injectedType = appContext.InjectTypeIntoAllAssemblies("Cpp2ILInjected", "TestInjectedTypeWithEvents", baseType); |
| 119 | + var addersByAssembly = injectedType.InjectMethodToAllAssemblies("add_TestEvent", appContext.SystemTypes.SystemInt32Type, MethodAttributes.Public); |
| 120 | + var removersByAssembly = injectedType.InjectMethodToAllAssemblies("remove_TestEvent", appContext.SystemTypes.SystemInt32Type, MethodAttributes.Public); |
| 121 | + var eventsByAssembly = injectedType.InjectEventToAllAssemblies("TestEvent", appContext.SystemTypes.SystemInt32Type, addersByAssembly, removersByAssembly, null, EventAttributes.None); |
| 122 | + |
| 123 | + Assert.That(eventsByAssembly, Has.Count.EqualTo(appContext.Assemblies.Count)); |
| 124 | + Assert.That(eventsByAssembly.Values.First(), Has.Property("Name").EqualTo("TestEvent").And.Property("EventTypeContext").EqualTo(appContext.SystemTypes.SystemInt32Type)); |
| 125 | + Assert.That(eventsByAssembly.Values.First().DeclaringType, Has.Property("FullName").EqualTo("Cpp2ILInjected.TestInjectedTypeWithEvents")); |
| 126 | + } |
| 127 | + |
| 128 | + [Test] |
| 129 | + public void TestNestedTypeInjection() |
| 130 | + { |
| 131 | + var appContext = Cpp2IlApi.CurrentAppContext; |
| 132 | + |
| 133 | + var baseType = appContext!.SystemTypes.SystemObjectType; |
| 134 | + var declaringType = appContext.InjectTypeIntoAllAssemblies("Cpp2ILInjected", "TestInjectedTypeWithEvents", baseType); |
| 135 | + var injectedType = declaringType.InjectNestedType("NestedType", baseType); |
| 136 | + |
| 137 | + Assert.That(injectedType.InjectedTypes, Has.Length.EqualTo(appContext.Assemblies.Count)); |
| 138 | + Assert.That(injectedType.InjectedTypes.First(), Has.Property("Name").EqualTo("NestedType").And.Property("Namespace").EqualTo("").And.Property("BaseType").EqualTo(appContext.SystemTypes.SystemObjectType)); |
| 139 | + Assert.That(injectedType.InjectedTypes.First().DeclaringType, Has.Property("FullName").EqualTo("Cpp2ILInjected.TestInjectedTypeWithEvents")); |
| 140 | + } |
| 141 | + |
| 142 | + [Test] |
| 143 | + public void TestNestedTypeInjectionSingle() |
| 144 | + { |
| 145 | + var appContext = Cpp2IlApi.CurrentAppContext; |
| 146 | + |
| 147 | + var declaringType = appContext!.SystemTypes.SystemObjectType; |
| 148 | + var injectedType = declaringType.InjectNestedType("NestedType", null); |
| 149 | + |
| 150 | + using (Assert.EnterMultipleScope()) |
| 151 | + { |
| 152 | + Assert.That(appContext.AllTypes, Contains.Item(injectedType)); |
| 153 | + Assert.That(declaringType.NestedTypes, Contains.Item(injectedType)); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + [Test] |
| 158 | + public void TestAssemblyInjection() |
| 159 | + { |
| 160 | + var appContext = Cpp2IlApi.CurrentAppContext; |
| 161 | + |
| 162 | + var assemblyCount = appContext!.Assemblies.Count; |
| 163 | + var injectedAssembly = appContext.InjectAssembly("TestInjectedAssembly"); |
| 164 | + using (Assert.EnterMultipleScope()) |
| 165 | + { |
| 166 | + Assert.That(appContext.Assemblies, Has.Count.EqualTo(assemblyCount + 1)); |
| 167 | + Assert.That(appContext.Assemblies, Contains.Item(injectedAssembly)); |
| 168 | + } |
| 169 | + } |
97 | 170 | }
|
0 commit comments