CrabUI
Loading...
Searching...
No Matches
FakeModRunner.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5using System.Diagnostics;
6using System.Runtime.CompilerServices;
7using System.IO;
8
9using Barotrauma;
10using Microsoft.Xna.Framework;
11using Microsoft.Xna.Framework.Input;
12using Microsoft.Xna.Framework.Graphics;
13using HarmonyLib;
14using CrabUI;
15
16namespace CrabUITest
17{
18 public class FakeModRunner
19 {
20 public CUIComponent TestChamber => CUITest.TestChamber;
21 public IFakeMod ActiveMod;
22
23
24 public void RunTest(string name)
25 {
26 Assembly targetAssembly = Assembly.GetAssembly(typeof(FakeModRunner));
27
28 Type type = targetAssembly.GetTypes().Find(T => T.Name == name);
29 if (type == null)
30 {
31 CUITest.Log($"can't find fake mod {name}");
32 return;
33 }
34
35 RunTest(type);
36 }
37
38 public void RunTest<RawType>() => RunTest(typeof(RawType));
39
40 public void RunTest(Type type)
41 {
42 if (!type.IsAssignableTo(typeof(IFakeMod)))
43 {
44 CUITest.Log($"it's not a fake mod");
45 return;
46 }
47
48 IFakeMod mod = (IFakeMod)Activator.CreateInstance(type);
49
50 RunTest(mod);
51 }
52
53 public void RunTest(IFakeMod mod)
54 {
55 if (TestChamber == null) CUITest.CreateTestChamber();
56 if (TestChamber.Parent == null) CUITest.OpenTestChamber();
57
58
59 if (ActiveMod != null)
60 {
61 ActiveMod.Dispose();
62 }
63 ActiveMod = mod;
64
65 CUITest.ClearTestChamber();
66
67 try
68 {
69 mod.Initialize(TestChamber);
70 }
71 catch (Exception e) { CUITest.Log(e, Color.Orange); }
72 }
73
74 public void StopTest()
75 {
76 if (ActiveMod == null) return;
77 ActiveMod.Dispose();
78 CUITest.ClearTestChamber();
79 if (TestChamber.Parent != null) CUITest.CloseTestChamber();
80
81 ActiveMod = null;
82 }
83
84 public string[] FindAllFakeMods()
85 {
86 Assembly targetAssembly = Assembly.GetAssembly(typeof(FakeModRunner));
87
88 IEnumerable<Type> fakeModTypes = targetAssembly.GetTypes().Where(T => T.IsAssignableTo(typeof(IFakeMod)) && T != typeof(IFakeMod));
89
90 return fakeModTypes.Select(T => T.Name).ToArray();
91 }
92
93 public FakeModRunner()
94 {
95
96 }
97
98 }
99}
Base class for all components.