Skip to content

Commit 2372c00

Browse files
committed
Include assemblies from assets in options
1 parent 7598a12 commit 2372c00

2 files changed

Lines changed: 36 additions & 22 deletions

File tree

scripts/Editor/DllManipulatorEditor.cs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public class DllManipulatorEditor : Editor
5454

5555
private bool _showLoadedLibraries = true;
5656
private bool _showTargetAssemblies = true;
57-
private DateTime _lastGetAssemblyCall;
58-
private Assembly[] _allAssembliesCache = null;
57+
private string[] _allKnownAssemblies = null;
58+
private DateTime _lastKnownAssembliesRefreshTime;
5959

6060
public DllManipulatorEditor()
6161
{
@@ -187,16 +187,31 @@ private void DrawOptions(DllManipulatorOptions options)
187187
var prevIndent1 = EditorGUI.indentLevel;
188188
EditorGUI.indentLevel++;
189189

190-
if (_allAssembliesCache == null || _lastGetAssemblyCall + ASSEMBLIES_REFRESH_INTERVAL < DateTime.Now)
190+
if (_allKnownAssemblies == null || _lastKnownAssembliesRefreshTime + ASSEMBLIES_REFRESH_INTERVAL < DateTime.Now)
191191
{
192-
_allAssembliesCache = CompilationPipeline.GetAssemblies();
193-
_lastGetAssemblyCall = DateTime.Now;
192+
var playerCompiledAssemblies = CompilationPipeline.GetAssemblies(AssembliesType.Player)
193+
.Select(a => PathUtils.NormallizeUnityAssemblyPath(a.outputPath));
194+
195+
var editorCompiledAssemblies = CompilationPipeline.GetAssemblies(AssembliesType.Editor)
196+
.Select(a => PathUtils.NormallizeUnityAssemblyPath(a.outputPath));
197+
198+
var assemblyAssets = Resources.FindObjectsOfTypeAll<PluginImporter>()
199+
.Where(p => !p.isNativePlugin)
200+
.Select(p => PathUtils.NormallizeUnityAssemblyPath(p.assetPath));
201+
202+
string[] defaultAssemblyPrefixes = { "UnityEngine.", "UnityEditor.", "Unity.", "com.unity.", "Mono." , "nunit."};
203+
204+
_allKnownAssemblies = playerCompiledAssemblies
205+
.Concat(assemblyAssets)
206+
.Concat(editorCompiledAssemblies)
207+
.OrderBy(path => Array.FindIndex(defaultAssemblyPrefixes, p => path.Substring(path.LastIndexOf('/') + 1).StartsWith(p)))
208+
.ToArray();
209+
_lastKnownAssembliesRefreshTime = DateTime.Now;
194210
}
195-
var allAssemblies = _allAssembliesCache;
196211

197212
if (options.assemblyPaths.Length == 0)
198213
{
199-
var first = GetFirstAssemblyPath(allAssemblies);
214+
var first = GetFirstAssemblyToList(_allKnownAssemblies);
200215
if(first != null)
201216
options.assemblyPaths = new[] { first };
202217
}
@@ -207,17 +222,19 @@ private void DrawOptions(DllManipulatorOptions options)
207222
var prevIndent2 = EditorGUI.indentLevel;
208223
EditorGUI.indentLevel++;
209224

210-
var selectedAssemblyPaths = options.assemblyPaths.Where(p => allAssemblies.Any(a => PathUtils.DllPathsEquals(a.outputPath, p)));
211-
var selectedAssemblies = selectedAssemblyPaths.Select(p => allAssemblies.First(a => PathUtils.DllPathsEquals(a.outputPath, p))).ToList();
212-
var notSelectedAssemblies = allAssemblies.Except(selectedAssemblies).ToArray();
225+
var selectedAssemblies = options.assemblyPaths.Where(p => _allKnownAssemblies.Any(a => PathUtils.DllPathsEqual(a, p))).ToList();
226+
var notSelectedAssemblies = _allKnownAssemblies.Except(selectedAssemblies).ToArray();
213227
DrawList(selectedAssemblies, i =>
214228
{
215-
var values = new List<string> { selectedAssemblies[i].name };
216-
values.AddRange(notSelectedAssemblies.Select(a => a.name));
217-
var selectedIndex = EditorGUILayout.Popup(0, values.ToArray());
229+
var values = new[] { selectedAssemblies[i] }
230+
.Concat(notSelectedAssemblies)
231+
.Select(a => a.Substring(a.LastIndexOf('/') + 1))
232+
.ToArray();
233+
234+
var selectedIndex = EditorGUILayout.Popup(0, values);
218235
return selectedIndex == 0 ? selectedAssemblies[i] : notSelectedAssemblies[selectedIndex - 1];
219236
}, notSelectedAssemblies.Length > 0, () => notSelectedAssemblies[0]);
220-
options.assemblyPaths = selectedAssemblies.Select(a => PathUtils.NormallizeUnityAssemblyPath(a.outputPath)).ToArray();
237+
options.assemblyPaths = selectedAssemblies.ToArray();
221238

222239
EditorGUI.indentLevel = prevIndent2;
223240
}
@@ -281,20 +298,17 @@ private void DrawList<T>(IList<T> elements, Func<int, T> drawElement, bool canAd
281298
GUILayout.BeginHorizontal();
282299
GUILayout.Space(EditorGUI.indentLevel * 15);
283300
var prevGuiEnabled = GUI.enabled;
284-
GUI.enabled = canAddNewElement;
301+
GUI.enabled = prevGuiEnabled && canAddNewElement;
285302
if (GUILayout.Button("Add", GUILayout.Width(40)))
286303
elements.Add(getNewElement());
287304
GUI.enabled = prevGuiEnabled;
288305
GUILayout.EndHorizontal();
289306
}
290307

291-
static string GetFirstAssemblyPath(Assembly[] allAssemblies)
308+
static string GetFirstAssemblyToList(string[] allAssemblies)
292309
{
293-
var path = allAssemblies.FirstOrDefault(a => PathUtils.DllPathsEquals(a.outputPath, typeof(DllManipulator).Assembly.Location))?.outputPath
294-
?? allAssemblies.FirstOrDefault()?.outputPath;
295-
if (path == null)
296-
return null;
297-
return PathUtils.NormallizeUnityAssemblyPath(path);
310+
return allAssemblies.FirstOrDefault(a => PathUtils.DllPathsEqual(a, typeof(DllManipulator).Assembly.Location))
311+
?? allAssemblies.FirstOrDefault();
298312
}
299313
}
300314
}

scripts/PathUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static class PathUtils
99
/// </summary>
1010
/// <param name="outputPath">Value returned from <see cref="UnityEditor.Compilation.Assembly.outputPath"/></param>
1111
/// <param name="location">Value returned from <see cref="System.Reflection.Assembly.Location"/></param>
12-
public static bool DllPathsEquals(string outputPath, string location)
12+
public static bool DllPathsEqual(string outputPath, string location)
1313
{
1414
return NormallizeUnityAssemblyPath(outputPath) == NormallizeSystemAssemblyPath(location);
1515
}

0 commit comments

Comments
 (0)