Skip to content

Commit 042c4aa

Browse files
committed
assemblyPaths small fixes, editor gui adapted
EditorGUI for the target assemblies is now just a string array. Removed code to find _allKnownAssemblies. assemblyPaths renamed to assemblyNames is now a list. By default only Assembly-CSharp and the Editor variant is targeted. The assembly containing this tool is always included so we find the attributes.
1 parent a5fd455 commit 042c4aa

3 files changed

Lines changed: 25 additions & 53 deletions

File tree

scripts/DllManipulator.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ public partial class DllManipulator
1717
public const string DLL_PATH_PATTERN_ASSETS_MACRO = "{assets}";
1818
public const string DLL_PATH_PATTERN_PROJECT_MACRO = "{proj}";
1919
private const string CRASH_FILE_NAME_PREFIX = "unityNativeCrash_";
20+
public static readonly string[] DEFAULT_ASSEMBLY_NAMES = {"Assembly-CSharp"
21+
#if UNITY_EDITOR
22+
, "Assembly-CSharp-Editor"
23+
#endif
24+
};
25+
public static readonly string[] INTERNAL_ASSEMBLY_NAMES = {"MCpiroman.UnityNativeTool"
26+
#if UNITY_EDITOR
27+
, "MCpiroman.UnityNativeTool.Editor"
28+
#endif
29+
};
2030

2131
public static DllManipulatorOptions Options { get; set; }
2232
private static int _unityMainThreadId;
@@ -47,11 +57,11 @@ internal static void Initialize(int unityMainThreadId, string assetsPath)
4757

4858
LowLevelPluginManager.ResetStubPlugin();
4959

50-
IEnumerable<string> assemblyPathsTemp = Options.assemblyPaths;
60+
IEnumerable<string> assemblyPathsTemp = Options.assemblyNames;
5161
if (!assemblyPathsTemp.Any())
52-
assemblyPathsTemp = new[] {"Assembly-CSharp", "Assembly-CSharp-Editor"};
62+
assemblyPathsTemp = DEFAULT_ASSEMBLY_NAMES;
5363

54-
assemblyPathsTemp = assemblyPathsTemp.Concat(new [] { "MCpiroman.UnityNativeTool", "MCpiroman.UnityNativeTool.Editor" });
64+
assemblyPathsTemp = assemblyPathsTemp.Concat(INTERNAL_ASSEMBLY_NAMES);
5565

5666
var allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
5767
var assemblies = allAssemblies.Where(a => !a.IsDynamic && assemblyPathsTemp.Any(p => p == Path.ChangeExtension(a.ManifestModule.Name, null))).ToArray();
@@ -642,7 +652,7 @@ private static IntPtr SysGetDllProcAddress(IntPtr libHandle, string symbol)
642652
public class DllManipulatorOptions
643653
{
644654
public string dllPathPattern;
645-
public string[] assemblyPaths; //empty means only executing assembly
655+
public List<string> assemblyNames; // empty means only default assemblies
646656
public DllLoadingMode loadingMode;
647657
public PosixDlopenFlags posixDlopenFlags;
648658
public bool threadSafe;

scripts/DllManipulatorScript.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Reflection;
34
using System.Threading;
45
using System.Linq;
@@ -21,7 +22,7 @@ public class DllManipulatorScript : MonoBehaviour
2122
#else // UNITY_STANDALONE_WIN Windows fallback
2223
"{assets}/Plugins/__{name}.dll",
2324
#endif
24-
assemblyPaths = new string[0],
25+
assemblyNames = new List<string>(),
2526
loadingMode = DllLoadingMode.Lazy,
2627
posixDlopenFlags = PosixDlopenFlags.Lazy,
2728
threadSafe = false,

scripts/Editor/DllManipulatorEditor.cs

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ public class DllManipulatorEditor : Editor
1717
private static readonly GUIContent TARGET_ALL_NATIVE_FUNCTIONS_GUI_CONTENT = new GUIContent("All native functions",
1818
"If true, all found native functions will be mocked.\n\n" +
1919
$"If false, you have to select them by using [{nameof(MockNativeDeclarationsAttribute)}] or [{nameof(MockNativeDeclarationAttribute)}].");
20-
private static readonly GUIContent TARGET_ONLY_EXECUTING_ASSEMBLY_GUI_CONTENT = new GUIContent("Only executing assembly",
21-
"If true, native functions will be mocked only in assembly that contains DllManipulator (usually Assembly-CSharp)");
20+
private static readonly GUIContent ONLY_ASSEMBLY_CSHARP_GUI_CONTENT = new GUIContent("Only Assembly-CSharp(-Editor)",
21+
"If true, native functions will be mocked only in Assembly-CSharp and Assembly-CSharp-Editor. Alternatively enter a list of assembly names.");
2222
private static readonly GUIContent ONLY_IN_EDITOR = new GUIContent("Only in editor",
2323
"Whether to run only inside editor (which is recommended).");
2424
private static readonly GUIContent TARGET_ASSEMBLIES_GUI_CONTENT = new GUIContent("Target assemblies",
25-
"Choose from which assemblies to mock native functions");
25+
"List of assembly names to mock native functions in (no file extension).");
2626
private static readonly GUIContent DLL_PATH_PATTERN_GUI_CONTENT = new GUIContent("DLL path pattern",
2727
"Available macros:\n\n" +
2828
$"{DllManipulator.DLL_PATH_PATTERN_DLL_NAME_MACRO} - name of DLL as specified in [DllImport] attribute.\n\n" +
@@ -53,7 +53,6 @@ public class DllManipulatorEditor : Editor
5353
"Use only if you are sure no other thread will be call mocked natives.");
5454
private static readonly GUIContent UNLOAD_ALL_DLLS_AND_PAUSE_WITH_THREAD_SAFETY_GUI_CONTENT = new GUIContent("Unload all DLLs & Pause [dangerous]",
5555
"Use only if you are sure no other thread will be call mocked natives.");
56-
private static readonly TimeSpan ASSEMBLIES_REFRESH_INTERVAL = TimeSpan.FromSeconds(1);
5756

5857
private bool _showLoadedLibraries = true;
5958
private bool _showTargetAssemblies = true;
@@ -181,63 +180,25 @@ private void DrawOptions(DllManipulatorOptions options)
181180
GUI.enabled = false;
182181
options.mockAllNativeFunctions = EditorGUILayout.Toggle(TARGET_ALL_NATIVE_FUNCTIONS_GUI_CONTENT, options.mockAllNativeFunctions);
183182

184-
if (EditorGUILayout.Toggle(TARGET_ONLY_EXECUTING_ASSEMBLY_GUI_CONTENT, options.assemblyPaths.Length == 0))
183+
if (EditorGUILayout.Toggle(ONLY_ASSEMBLY_CSHARP_GUI_CONTENT, options.assemblyNames.Count == 0))
185184
{
186-
options.assemblyPaths = new string[0];
185+
options.assemblyNames.Clear();
187186
}
188187
else
189188
{
190189
var prevIndent1 = EditorGUI.indentLevel;
191190
EditorGUI.indentLevel++;
192191

193-
if (_allKnownAssemblies == null || _lastKnownAssembliesRefreshTime + ASSEMBLIES_REFRESH_INTERVAL < DateTime.Now)
194-
{
195-
var playerCompiledAssemblies = CompilationPipeline.GetAssemblies(AssembliesType.Player)
196-
.Select(a => PathUtils.NormallizeUnityAssemblyPath(a.outputPath));
197-
198-
var editorCompiledAssemblies = CompilationPipeline.GetAssemblies(AssembliesType.Editor)
199-
.Select(a => PathUtils.NormallizeUnityAssemblyPath(a.outputPath));
200-
201-
var assemblyAssets = Resources.FindObjectsOfTypeAll<PluginImporter>()
202-
.Where(p => !p.isNativePlugin)
203-
.Select(p => PathUtils.NormallizeUnityAssemblyPath(p.assetPath));
204-
205-
string[] defaultAssemblyPrefixes = { "UnityEngine.", "UnityEditor.", "Unity.", "com.unity.", "Mono." , "nunit."};
206-
207-
_allKnownAssemblies = playerCompiledAssemblies
208-
.Concat(assemblyAssets)
209-
.Concat(editorCompiledAssemblies)
210-
.OrderBy(path => Array.FindIndex(defaultAssemblyPrefixes, p => path.Substring(path.LastIndexOf('/') + 1).StartsWith(p)))
211-
.ToArray();
212-
_lastKnownAssembliesRefreshTime = DateTime.Now;
213-
}
214-
215-
if (options.assemblyPaths.Length == 0)
216-
{
217-
var first = GetFirstAssemblyToList(_allKnownAssemblies);
218-
if(first != null)
219-
options.assemblyPaths = new[] { first };
220-
}
192+
if (options.assemblyNames.Count == 0)
193+
options.assemblyNames.AddRange(DllManipulator.DEFAULT_ASSEMBLY_NAMES);
221194

222195
_showTargetAssemblies = EditorGUILayout.Foldout(_showTargetAssemblies, TARGET_ASSEMBLIES_GUI_CONTENT);
223196
if (_showTargetAssemblies)
224197
{
225198
var prevIndent2 = EditorGUI.indentLevel;
226199
EditorGUI.indentLevel++;
227200

228-
var selectedAssemblies = options.assemblyPaths.Where(p => _allKnownAssemblies.Any(a => PathUtils.DllPathsEqual(a, p))).ToList();
229-
var notSelectedAssemblies = _allKnownAssemblies.Except(selectedAssemblies).ToArray();
230-
DrawList(selectedAssemblies, i =>
231-
{
232-
var values = new[] { selectedAssemblies[i] }
233-
.Concat(notSelectedAssemblies)
234-
.Select(a => a.Substring(a.LastIndexOf('/') + 1))
235-
.ToArray();
236-
237-
var selectedIndex = EditorGUILayout.Popup(0, values);
238-
return selectedIndex == 0 ? selectedAssemblies[i] : notSelectedAssemblies[selectedIndex - 1];
239-
}, notSelectedAssemblies.Length > 0, () => notSelectedAssemblies[0]);
240-
options.assemblyPaths = selectedAssemblies.ToArray();
201+
DrawList(options.assemblyNames, i => EditorGUILayout.TextField(options.assemblyNames[i]), true, () => "");
241202

242203
EditorGUI.indentLevel = prevIndent2;
243204
}

0 commit comments

Comments
 (0)