Skip to content

Commit ed6fb24

Browse files
committed
Start making the UnityHelper addon more editor friendly
1 parent e1081d5 commit ed6fb24

3 files changed

Lines changed: 98 additions & 64 deletions

File tree

Assets/Scripts/UnityHelper/ModSystem/ModLoader.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ public class ModLoader
1010
{
1111
private readonly ModScanner _modScanner;
1212

13-
public ModLoader(bool loadNow = false, ModScanner scanner = null)
13+
public ModLoader()
1414
{
15-
_modScanner = scanner ?? new ModScanner(true);
16-
if (!loadNow) return;
15+
_modScanner = new ModScanner();
1716
LoadMods();
1817
}
1918

Assets/Scripts/UnityHelper/ModSystem/ModScanner.cs

Lines changed: 4 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,63 +10,6 @@ namespace UnityHelper.ModSystem
1010
{
1111
public class ModScanner
1212
{
13-
// Instance field to store the setting
14-
private readonly bool _banAllInNamespace;
15-
16-
// --- Dangerous Types ---
17-
// Use StartsWith for namespaces (include trailing '.' if needed)
18-
// Use exact full names for specific classes
19-
private static readonly string[] DangerousTypes =
20-
{
21-
// Namespaces (match anything within)
22-
"System.IO.",
23-
"System.Net.",
24-
"System.Reflection.Emit.",
25-
"System.Runtime.InteropServices.",
26-
"System.Security.AccessControl.",
27-
"System.Management.",
28-
"System.IO.IsolatedStorage.",
29-
30-
// Specific Classes (match exactly)
31-
"System.Diagnostics.Process",
32-
"System.Reflection.Assembly" // Loading/interacting with assemblies directly
33-
// Add other specific dangerous classes here if needed
34-
};
35-
36-
// --- Dangerous Methods ---
37-
// These are checked *in combination* with DangerousTypes
38-
private static readonly string[] DangerousMethods =
39-
{
40-
// IO Methods
41-
"Delete", "Move", "CreateDirectory", "Copy", "AppendAllText", "WriteAllBytes", "WriteAllText",
42-
"SetAccessControl", "AddAccessRule", "RemoveAccessRule", "SetAccessRule", "GetAccessControl",
43-
"Open", "Create", // FileStream constructors are often just ".ctor", so check common factory methods too
44-
45-
// Process Methods
46-
"Start", "Kill", "GetProcesses",
47-
48-
// Reflection/Assembly Methods
49-
"Load", "LoadFrom", "LoadFile", "GetExecutingAssembly", "Invoke", // Check context carefully
50-
"CreateInstance", // System.Activator
51-
52-
// Networking Methods
53-
"Connect", "Bind", "Listen", "Send", "Receive", "GetResponse", "DownloadFile",
54-
"UploadFile", // Check common methods on Socket, TcpClient, WebClient etc.
55-
56-
// Interop Methods
57-
"GetDelegateForFunctionPointer", "PtrToStructure", "StructureToPtr",
58-
59-
// Add other specific dangerous method names
60-
".ctor" // Check constructors of dangerous types explicitly if needed
61-
};
62-
63-
64-
// Constructor
65-
public ModScanner(bool banAllInNamespace = false)
66-
{
67-
_banAllInNamespace = banAllInNamespace;
68-
}
69-
7013
public bool IsModSafe(string dllPath)
7114
{
7215
try
@@ -152,7 +95,7 @@ public bool IsModSafe(string dllPath)
15295
if (typeIsDangerous)
15396
{
15497
// If banning all in namespace is enabled, and the type is dangerous, fail immediately.
155-
if (_banAllInNamespace)
98+
if (UnityHelperAddon.UnityHelper.BanAllNamespaces())
15699
{
157100
Debug.LogWarning($"[SECURITY] Dangerous type '{methodRef.DeclaringType.FullName}' used (namespace ban active). Banned method call: {methodRef.FullName}");
158101
return false;
@@ -161,7 +104,7 @@ public bool IsModSafe(string dllPath)
161104
else
162105
{
163106
string methodName = methodRef.Name;
164-
bool methodIsDangerous = DangerousMethods.Any(dangerousMethodName => methodName == dangerousMethodName);
107+
bool methodIsDangerous = UnityHelperAddon.UnityHelper.DangerousMethods().Any(dangerousMethodName => methodName == dangerousMethodName);
165108
if (methodIsDangerous)
166109
{
167110
Debug.LogWarning($"[SECURITY] Dangerous method call detected in '{method.FullName}': Call to {methodRef.FullName} (Type and Method match)");
@@ -252,7 +195,7 @@ private bool IsDangerousType(TypeReference typeRef)
252195
fullName = fullName.Replace("&", ""); // Remove by-ref marker
253196

254197
// Inside IsDangerousType, before the final return
255-
var isMatch = DangerousTypes.Any(dangerous =>
198+
var isMatch = UnityHelperAddon.UnityHelper.DangerousTypes().Any(dangerous =>
256199
fullName.Equals(dangerous, StringComparison.Ordinal) ||
257200
(dangerous.EndsWith(".") && fullName.StartsWith(dangerous, StringComparison.Ordinal))
258201
);
@@ -279,7 +222,7 @@ private bool IsDangerousMethodCall(MethodReference methodRef)
279222
}
280223

281224
string methodName = methodRef.Name;
282-
bool methodIsDangerous = DangerousMethods.Any(dangerousMethodName => methodName == dangerousMethodName);
225+
bool methodIsDangerous = UnityHelperAddon.UnityHelper.DangerousMethods().Any(dangerousMethodName => methodName == dangerousMethodName);
283226
Debug.Log($"[ScannerDebug] IsDangerousMethodCall Check: MethodName='{methodName}', NameInList={methodIsDangerous}");
284227

285228
return methodIsDangerous;
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,101 @@
1+
using System;
12
using UnityEngine;
3+
using UnityHelper.ModSystem;
24

35
namespace UnityHelper
46
{
57
public class UnityHelperAddon : MonoBehaviour
68
{
9+
public static UnityHelperAddon UnityHelper { get; private set; }
710

11+
private ModLoader _modLoader;
12+
13+
[Header("Mod Loader Settings")]
14+
[SerializeField]
15+
[Tooltip("Do you want to enable UnityHelper's built-in mod support?")]
16+
private bool modLoaderEnabled = true;
17+
18+
[SerializeField]
19+
[Tooltip("When this is set to true, no matter what method is being accessed, if it is in a dangerous namespace, " +
20+
"don't load the mod.")]
21+
private bool banAllNamespaces = true;
22+
23+
// --- Dangerous Types ---
24+
// Use StartsWith for namespaces (include trailing '.' if needed)
25+
// Use exact full names for specific classes
26+
[SerializeField]
27+
[Tooltip(
28+
"Dangerous namespaces for mods to have access to, we have banned a lot by default, change them as you need.")]
29+
private string[] dangerousTypes =
30+
{
31+
// Namespaces (match anything within)
32+
"System.IO.",
33+
"System.Net.",
34+
"System.Reflection.Emit.",
35+
"System.Runtime.InteropServices.",
36+
"System.Security.AccessControl.",
37+
"System.Management.",
38+
"System.IO.IsolatedStorage.",
39+
40+
// Specific Classes (match exactly)
41+
"System.Diagnostics.Process",
42+
"System.Reflection.Assembly" // Loading/interacting with assemblies directly
43+
// Add other specific dangerous classes here if needed
44+
};
45+
46+
// --- Dangerous Methods ---
47+
// These are checked *in combination* with DangerousTypes
48+
[SerializeField]
49+
[Tooltip("Dangerous methods for mods to have to, we have banned a lot by default, change them as you need.")]
50+
private string[] dangerousMethods =
51+
{
52+
// IO Methods
53+
"Delete", "Move", "CreateDirectory", "Copy", "AppendAllText", "WriteAllBytes", "WriteAllText",
54+
"SetAccessControl", "AddAccessRule", "RemoveAccessRule", "SetAccessRule", "GetAccessControl",
55+
"Open", "Create", // FileStream constructors are often just ".ctor", so check common factory methods too
56+
57+
// Process Methods
58+
"Start", "Kill", "GetProcesses",
59+
60+
// Reflection/Assembly Methods
61+
"Load", "LoadFrom", "LoadFile", "GetExecutingAssembly", "Invoke", // Check context carefully
62+
"CreateInstance", // System.Activator
63+
64+
// Networking Methods
65+
"Connect", "Bind", "Listen", "Send", "Receive", "GetResponse", "DownloadFile",
66+
"UploadFile", // Check common methods on Socket, TcpClient, WebClient etc.
67+
68+
// Interop Methods
69+
"GetDelegateForFunctionPointer", "PtrToStructure", "StructureToPtr",
70+
71+
// Add other specific dangerous method names
72+
".ctor" // Check constructors of dangerous types explicitly if needed
73+
};
74+
75+
public bool ModLoaderEnabled() => modLoaderEnabled;
76+
77+
public bool BanAllNamespaces() => banAllNamespaces;
78+
79+
public string[] DangerousTypes() => dangerousTypes;
80+
81+
public string[] DangerousMethods() => dangerousMethods;
82+
83+
public ModLoader ModLoader() => _modLoader;
84+
85+
private void Awake()
86+
{
87+
if (UnityHelper == null)
88+
{
89+
UnityHelper = this;
90+
DontDestroyOnLoad(gameObject); // Keep this object alive across scenes
91+
}
92+
else
93+
{
94+
Destroy(gameObject); // Destroy duplicate instance
95+
}
96+
97+
if (ModLoaderEnabled())
98+
_modLoader = new ModLoader();
99+
}
8100
}
9101
}

0 commit comments

Comments
 (0)