UnityExplorer/src/Tests/TestClass.cs

287 lines
12 KiB
C#
Raw Normal View History

2022-05-26 06:19:50 +10:00
using System.Collections;
#if CPP
2022-06-23 06:03:58 +10:00
#if INTEROP
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppInterop.Runtime;
#else
using UnhollowerRuntimeLib;
using UnhollowerBaseLib;
#endif
2022-06-23 06:03:58 +10:00
#endif
namespace UnityExplorer.Tests
{
2022-01-08 17:56:56 +11:00
public class TestClass
{
2021-07-21 20:48:21 +10:00
static TestClass()
{
2021-07-21 20:48:21 +10:00
Init_Mono();
#if CPP
Init_IL2CPP();
#endif
}
2022-03-29 22:36:17 +11:00
#region MONO
public static object LiterallyAnything = null;
2022-04-11 17:37:03 +10:00
public static string Exception
{
get
{
if (!shouldThrow)
{
shouldThrow = true;
throw new Exception("This is a test.");
}
else
{
shouldThrow = false;
return "No exception";
}
}
}
static bool shouldThrow;
2021-07-21 20:48:21 +10:00
// Test enumerables
public static int[,,] MultiDimensionalArray = new int[45, 45, 45];
2021-07-21 20:48:21 +10:00
public static List<object> ListOfInts;
public static List<List<List<string>>> NestedList;
public static IDictionary MixedDictionary;
public static Hashtable Hashtable;
public static byte[] ByteArray = new byte[16];
public static List<short> ABigList = new(10000);
2021-07-21 20:48:21 +10:00
// Test const behaviour (should be a readonly field)
public const int ConstantInt5 = 5;
2021-07-21 20:48:21 +10:00
// Testing other InteractiveValues
public static BindingFlags EnumTest;
public static CameraClearFlags EnumTest2;
2021-07-21 20:48:21 +10:00
public static Color Color = Color.magenta;
public static Color32 Color32 = Color.red;
public static string ALongString = new('#', 10000);
public static float[] AParseTest(ref List<float[,,]> arg0, ref float[,] arg1)
{
return new float[] { 1, 2, 3 };
}
public static List<object> RandomList
{
get
{
List<object> list = new();
int count = UnityEngine.Random.Range(0, 100);
for (int i = 0; i < count; i++)
list.Add(GetRandomObject());
return list;
}
}
2022-01-08 17:56:56 +11:00
public int this[int index]
{
get => UnityEngine.Random.Range(0, int.MaxValue);
set => ExplorerCore.Log(index);
}
2021-07-21 20:48:21 +10:00
// Test methods
2021-05-05 21:27:09 +10:00
private static object GetRandomObject()
{
return UnityEngine.Random.Range(0, 7) switch
{
0 => null,
1 => 123,
2 => true,
3 => "hello",
4 => 50.5f,
5 => CameraClearFlags.Color,
6 => new List<string> { "one", "two" },
_ => null,
};
}
2021-07-21 20:48:21 +10:00
public static void TestComponent<T>() where T : Component
{
ExplorerCore.Log($"Test3 {typeof(T).FullName}");
}
2022-03-29 22:36:17 +11:00
public static void TestArgumentParse(string _string,
int integer,
Color color,
CameraClearFlags flags,
Vector3 vector,
Quaternion quaternion,
2022-01-02 19:55:09 +11:00
object obj,
2022-01-08 17:56:56 +11:00
Type type,
GameObject go)
2021-07-21 20:48:21 +10:00
{
2022-01-02 19:55:09 +11:00
ExplorerCore.Log($"_string: {_string}, integer: {integer}, color: {color.ToString()}, flags: {flags}, " +
$"vector: {vector.ToString()}, quaternion: {quaternion.ToString()}, obj: {obj?.ToString() ?? "null"}," +
2022-01-08 17:56:56 +11:00
$"type: {type?.FullName ?? "null"}, go: {go?.ToString() ?? "null"}");
2021-07-21 20:48:21 +10:00
}
2021-07-21 20:48:21 +10:00
private static void Init_Mono()
{
ExplorerCore.Log($"1: Basic list");
ListOfInts = new List<object> { 1, 2, 3, 4, 5 };
2021-05-16 21:46:38 +10:00
2021-07-21 20:48:21 +10:00
ExplorerCore.Log($"2: Nested list");
NestedList = new List<List<List<string>>>
{
new List<List<string>> {
new List<string> { "1", "2", "3" },
new List<string> { "4", "5", "6" },
},
new List<List<string>>
{
new List<string> { "7", "8", "9" }
}
};
2021-07-21 20:48:21 +10:00
ExplorerCore.Log($"3: Dictionary");
MixedDictionary = new Dictionary<object, object>
{
{ 1, 2 },
{ "one", "two" },
{ true, false },
{ new Vector3(0,1,2), new Vector3(1,2,3) },
{ CameraClearFlags.Depth, CameraClearFlags.Color },
{ "################################################\r\n##########", null },
{ "subdict", new Dictionary<object,object> { { "key", "value" } } }
};
ExplorerCore.Log($"4: Hashtable");
Hashtable = new Hashtable { { "One", 1 }, { "Two", 2 } };
ExplorerCore.Log($"5: Big list");
for (int i = 0; i < ABigList.Capacity; i++)
2021-08-23 18:35:31 +10:00
ABigList.Add((short)UnityEngine.Random.Range(0, short.MaxValue));
2021-07-21 20:48:21 +10:00
ExplorerCore.Log("Finished TestClass Init_Mono");
}
2022-03-29 22:36:17 +11:00
#endregion
2021-07-21 20:48:21 +10:00
#if CPP
2022-03-05 07:32:32 +11:00
public static Il2CppSystem.Collections.Generic.Dictionary<string, string> IL2CPP_Dict;
public static Il2CppSystem.Collections.Generic.HashSet<string> IL2CPP_HashSet;
2021-07-21 20:48:21 +10:00
public static Il2CppSystem.Collections.Generic.List<string> IL2CPP_ListString;
2022-03-05 07:32:32 +11:00
public static Il2CppSystem.Collections.Hashtable IL2CPP_HashTable;
public static List<Il2CppSystem.Object> IL2CPP_listOfBoxedObjects;
public static Il2CppStructArray<int> IL2CPP_structArray;
public static Il2CppReferenceArray<Il2CppSystem.Object> IL2CPP_ReferenceArray;
2021-07-21 20:48:21 +10:00
public static Il2CppSystem.Collections.IDictionary IL2CPP_IDict;
public static Il2CppSystem.Collections.IList IL2CPP_IList;
public static Dictionary<Il2CppSystem.Object, Il2CppSystem.Object> IL2CPP_BoxedDict;
2022-04-18 19:12:42 +10:00
public static Il2CppSystem.Array IL2CPP_NonGenericArray;
2022-03-29 22:36:17 +11:00
public static Il2CppSystem.Object IL2CPP_BoxedInt;
public static Il2CppSystem.Int32 IL2CPP_Int;
public static Il2CppSystem.Decimal IL2CPP_Decimal;
public static Il2CppSystem.Object IL2CPP_DecimalBoxed;
public static Il2CppSystem.Object IL2CPP_Vector3Boxed;
2021-07-21 20:48:21 +10:00
public static string IL2CPP_systemString = "Test";
public static Il2CppSystem.Object IL2CPP_objectString = "string boxed as cpp object";
public static Il2CppSystem.String IL2CPP_il2cppString = "string boxed as cpp string";
public static string nullString = null;
2021-07-21 20:48:21 +10:00
private static void Init_IL2CPP()
{
2022-04-18 19:12:42 +10:00
ExplorerCore.Log("IL2CPP 0: Non-generic array");
IL2CPP_NonGenericArray = new Il2CppStructArray<int>(5).TryCast<Il2CppSystem.Array>();
2021-07-21 20:48:21 +10:00
ExplorerCore.Log($"IL2CPP 1: Il2Cpp Dictionary<string, string>");
IL2CPP_Dict = new Il2CppSystem.Collections.Generic.Dictionary<string, string>();
IL2CPP_Dict.Add("key1", "value1");
IL2CPP_Dict.Add("key2", "value2");
IL2CPP_Dict.Add("key3", "value3");
2022-03-05 07:32:32 +11:00
ExplorerCore.Log($"IL2CPP 6: Il2Cpp HashSet of strings");
IL2CPP_HashSet = new Il2CppSystem.Collections.Generic.HashSet<string>();
IL2CPP_HashSet.Add("one");
IL2CPP_HashSet.Add("two");
2021-07-21 20:48:21 +10:00
ExplorerCore.Log($"IL2CPP 2: Il2Cpp Hashtable");
IL2CPP_HashTable = new Il2CppSystem.Collections.Hashtable();
IL2CPP_HashTable.Add("key1", "value1");
IL2CPP_HashTable.Add("key2", "value2");
IL2CPP_HashTable.Add("key3", "value3");
2022-03-29 22:36:17 +11:00
2021-07-21 20:48:21 +10:00
ExplorerCore.Log($"IL2CPP 3: Il2Cpp IDictionary");
Il2CppSystem.Collections.Generic.Dictionary<string, string> dict2 = new Il2CppSystem.Collections.Generic.Dictionary<string, string>();
dict2.Add("key1", "value1");
IL2CPP_IDict = dict2.TryCast<Il2CppSystem.Collections.IDictionary>();
2022-03-29 22:36:17 +11:00
2021-07-21 20:48:21 +10:00
ExplorerCore.Log($"IL2CPP 4: Il2Cpp List of Il2Cpp Object");
Il2CppSystem.Collections.Generic.List<Il2CppSystem.Object> list = new Il2CppSystem.Collections.Generic.List<Il2CppSystem.Object>(5);
2021-05-16 21:46:38 +10:00
list.Add("one");
list.Add("two");
IL2CPP_IList = list.TryCast<Il2CppSystem.Collections.IList>();
2022-03-29 22:36:17 +11:00
2021-07-21 20:48:21 +10:00
ExplorerCore.Log($"IL2CPP 5: Il2Cpp List of strings");
IL2CPP_ListString = new Il2CppSystem.Collections.Generic.List<string>();
IL2CPP_ListString.Add("hello,");
IL2CPP_ListString.Add("world!");
2022-03-29 22:36:17 +11:00
2021-07-21 20:48:21 +10:00
ExplorerCore.Log($"IL2CPP 7: Dictionary of Il2Cpp String and Il2Cpp Object");
IL2CPP_BoxedDict = new();
IL2CPP_BoxedDict[(Il2CppSystem.String)"one"] = new Il2CppSystem.Int32 { m_value = 1 }.BoxIl2CppObject();
IL2CPP_BoxedDict[(Il2CppSystem.String)"two"] = new Il2CppSystem.Int32 { m_value = 2 }.BoxIl2CppObject();
IL2CPP_BoxedDict[(Il2CppSystem.String)"three"] = new Il2CppSystem.Int32 { m_value = 3 }.BoxIl2CppObject();
IL2CPP_BoxedDict[(Il2CppSystem.String)"four"] = new Il2CppSystem.Int32 { m_value = 4 }.BoxIl2CppObject();
2022-03-29 22:36:17 +11:00
2021-07-21 20:48:21 +10:00
ExplorerCore.Log($"IL2CPP 8: List of boxed Il2Cpp Objects");
IL2CPP_listOfBoxedObjects = new List<Il2CppSystem.Object>();
IL2CPP_listOfBoxedObjects.Add((Il2CppSystem.String)"boxedString");
IL2CPP_listOfBoxedObjects.Add(new Il2CppSystem.Int32 { m_value = 5 }.BoxIl2CppObject());
IL2CPP_listOfBoxedObjects.Add(Color.red.BoxIl2CppObject());
2021-07-21 20:48:21 +10:00
// boxed enum test
try
{
Il2CppSystem.Type cppType = Il2CppType.Of<CameraClearFlags>();
2022-04-14 01:25:59 +10:00
if (cppType is not null)
{
Il2CppSystem.Object boxedEnum = Il2CppSystem.Enum.Parse(cppType, "Color");
IL2CPP_listOfBoxedObjects.Add(boxedEnum);
}
2022-03-29 22:36:17 +11:00
Il2CppSystem.Object structBox = Vector3.one.BoxIl2CppObject();
IL2CPP_listOfBoxedObjects.Add(structBox);
2022-03-29 22:36:17 +11:00
}
catch (Exception ex)
{
2021-07-21 20:48:21 +10:00
ExplorerCore.LogWarning($"Boxed enum test fail: {ex}");
}
2022-03-29 22:36:17 +11:00
2021-07-21 20:48:21 +10:00
ExplorerCore.Log($"IL2CPP 9: Il2Cpp struct array of ints");
2022-06-23 06:03:58 +10:00
IL2CPP_structArray = new Il2CppStructArray<int>(5);
IL2CPP_structArray[0] = 0;
IL2CPP_structArray[1] = 1;
IL2CPP_structArray[2] = 2;
IL2CPP_structArray[3] = 3;
IL2CPP_structArray[4] = 4;
2022-03-29 22:36:17 +11:00
2021-07-21 20:48:21 +10:00
ExplorerCore.Log($"IL2CPP 10: Il2Cpp reference array of boxed objects");
2022-06-23 06:03:58 +10:00
IL2CPP_ReferenceArray = new Il2CppReferenceArray<Il2CppSystem.Object>(3);
IL2CPP_ReferenceArray[0] = new Il2CppSystem.Int32 { m_value = 5 }.BoxIl2CppObject();
IL2CPP_ReferenceArray[1] = null;
IL2CPP_ReferenceArray[2] = (Il2CppSystem.String)"whats up";
2022-03-29 22:36:17 +11:00
2021-07-21 20:48:21 +10:00
ExplorerCore.Log($"IL2CPP 11: Misc il2cpp members");
IL2CPP_BoxedInt = new Il2CppSystem.Int32() { m_value = 5 }.BoxIl2CppObject();
IL2CPP_Int = new Il2CppSystem.Int32 { m_value = 420 };
IL2CPP_Decimal = new Il2CppSystem.Decimal(1f);
IL2CPP_DecimalBoxed = new Il2CppSystem.Decimal(1f).BoxIl2CppObject();
IL2CPP_Vector3Boxed = Vector3.down.BoxIl2CppObject();
2021-07-21 20:48:21 +10:00
ExplorerCore.Log($"Finished Init_Il2Cpp");
}
2022-03-29 22:36:17 +11:00
2021-07-21 20:48:21 +10:00
#endif
}
}