Skip to content

Commit

Permalink
fix(UnityIntegration): tell Unity to reload assemblies when needed
Browse files Browse the repository at this point in the history
To handle the startup behavior of Unity a delay was added to only
start the weaving process once Unity is initialized properly. That
change introduced an issue: When Malimbe weaves the assemblies Unity
won't know about it and as such, even though the assembly on disk
was changed by Malimbe, Unity still uses the unchanged version it
compiled. The fix is to ask Unity to do a reload of the weaved
assemblies.
  • Loading branch information
Christopher - Marcel Böddecker committed Feb 5, 2019
1 parent 956a167 commit 6350a71
Showing 1 changed file with 45 additions and 21 deletions.
66 changes: 45 additions & 21 deletions Sources/FodyRunner.UnityIntegration/EditorWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using JetBrains.Annotations;
using UnityEditor;
using UnityEditor.Compilation;
using UnityEditorInternal;
using UnityEngine;

internal static class EditorWeaver
Expand Down Expand Up @@ -34,36 +35,56 @@ private static void ManuallyWeaveAllAssemblies()

private static void WeaveAllAssemblies()
{
IReadOnlyList<Assembly> assemblies = GetAllAssemblies().ToList();
IReadOnlyCollection<string> searchPaths = WeaverPathsHelper.GetSearchPaths().ToList();
Runner runner = new Runner(new Logger());
runner.Configure(searchPaths, searchPaths);
EditorApplication.LockReloadAssemblies();

for (int index = 0; index < assemblies.Count; index++)
try
{
Assembly assembly = assemblies[index];
IReadOnlyList<Assembly> assemblies = GetAllAssemblies().ToList();
IReadOnlyCollection<string> searchPaths = WeaverPathsHelper.GetSearchPaths().ToList();
Runner runner = new Runner(new Logger());
runner.Configure(searchPaths, searchPaths);

for (int index = 0; index < assemblies.Count; index++)
{
Assembly assembly = assemblies[index];
try
{
EditorUtility.DisplayProgressBar(
nameof(Malimbe),
$"Weaving '{assembly.name}'.",
(float)index / assemblies.Count);

InternalEditorUtility.RequestScriptReload();
}
catch
{
// ignored
}

if (!WeaveAssembly(assembly, runner))
{
continue;
}

string sourceFilePath = assembly.sourceFiles.FirstOrDefault();
if (sourceFilePath != null)
{
AssetDatabase.ImportAsset(sourceFilePath, ImportAssetOptions.ForceUpdate);
}
}

try
{
EditorUtility.DisplayProgressBar(
nameof(Malimbe),
$"Weaving '{assembly.name}'.",
(float)index / assemblies.Count);
EditorUtility.ClearProgressBar();
}
catch
{
// ignored
}

WeaveAssembly(assembly, runner);
}

try
{
EditorUtility.ClearProgressBar();
}
catch
finally
{
// ignored
EditorApplication.UnlockReloadAssemblies();
}
}

Expand All @@ -90,18 +111,21 @@ private static IEnumerable<Assembly> GetAllAssemblies() =>
.GroupBy(assembly => assembly.outputPath)
.Select(grouping => grouping.First());

private static void WeaveAssembly(Assembly assembly, Runner runner)
private static bool WeaveAssembly(Assembly assembly, Runner runner)
{
try
{
string assemblyPath = WeaverPathsHelper.AddProjectPathRootIfNeeded(assembly.outputPath);
IEnumerable<string> references =
assembly.allReferences.Select(WeaverPathsHelper.AddProjectPathRootIfNeeded);
runner.RunAsync(assemblyPath, references, assembly.defines.ToList(), true).GetAwaiter().GetResult();
return runner.RunAsync(assemblyPath, references, assembly.defines.ToList(), true)
.GetAwaiter()
.GetResult();
}
catch (Exception exception)
{
Debug.LogException(exception);
return false;
}
}
}
Expand Down

0 comments on commit 6350a71

Please sign in to comment.