Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Unity option to enable weaving of editor assemblies #2434

Merged
merged 1 commit into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ API. Example:
var newPerson = (RealmObject)(object)realm.DynamicApi.Create("Person", 123);
newPerson.DynamicApi.Set("FirstName", "Peter");
```
* Added a Unity Editor option to enable weaving editor assemblies. This should be "off" unless your project has Editor assemblies that reference Realm - for example, an EditMode test assembly that tests Realm-related functionality. Keeping it "on" may slow down builds a little as more assemblies will need to be evaluated for weaving. (Issue [#2346](https://github.com/realm/realm-dotnet/issues/2346))

### Compatibility
* Realm Studio: 10.0.0 or later.
Expand Down
51 changes: 44 additions & 7 deletions Realm/Realm.UnityWeaver/UnityWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public class UnityWeaver : IPostBuildPlayerScriptDLLs
private const string EnableAnalyticsPref = "realm_enable_analytics";
private const string EnableAnalyticsMenuItemPath = "Realm/Enable build-time analytics";

private const string WeaveEditorAssembliesPref = "realm_weave_editor_assemblies";
private const string WeaveEditorAssembliesMenuItemPath = "Realm/Process editor assemblies";

private static bool _analyticsEnabled;

private static bool AnalyticsEnabled
Expand All @@ -50,6 +53,19 @@ private static bool AnalyticsEnabled
}
}

private static bool _weaveEditorAssemblies;

private static bool WeaveEditorAssemblies
{
get => _weaveEditorAssemblies;
set
{
_weaveEditorAssemblies = value;
EditorPrefs.SetBool(WeaveEditorAssembliesPref, value);
Menu.SetChecked(WeaveEditorAssembliesMenuItemPath, value);
}
}

public int callbackOrder => 0;

[InitializeOnLoadMethod]
Expand All @@ -59,6 +75,8 @@ public static void Initialize()
EditorApplication.delayCall += () =>
{
AnalyticsEnabled = EditorPrefs.GetBool(EnableAnalyticsPref, defaultValue: true);
WeaveEditorAssemblies = EditorPrefs.GetBool(WeaveEditorAssembliesPref, defaultValue: false);
WeaveAssembliesOnEditorLaunch();
};

CompilationPipeline.assemblyCompilationFinished += (string assemblyPath, CompilerMessage[] _) =>
Expand All @@ -68,8 +86,7 @@ public static void Initialize()
return;
}

var assembly = CompilationPipeline.GetAssemblies()
.FirstOrDefault(p => p.outputPath == assemblyPath);
var assembly = GetAssemblies().FirstOrDefault(p => p.outputPath == assemblyPath);

if (assembly == null)
{
Expand All @@ -78,9 +95,6 @@ public static void Initialize()

WeaveAssemblyCore(assemblyPath, assembly.allReferences, "Unity Editor", GetTargetOSName(Application.platform));
};

AnalyticsEnabled = EditorPrefs.GetBool(EnableAnalyticsPref, defaultValue: true);
WeaveAssembliesOnEditorLaunch();
}

[MenuItem("Realm/Weave Assemblies")]
Expand All @@ -93,7 +107,20 @@ public static async void WeaveAllAssembliesMenuItem()
[MenuItem(EnableAnalyticsMenuItemPath)]
public static void DisableAnalyticsMenuItem()
{
AnalyticsEnabled = EditorPrefs.GetBool(EnableAnalyticsPref, defaultValue: true);
AnalyticsEnabled = !AnalyticsEnabled;
}

[MenuItem(WeaveEditorAssembliesMenuItemPath)]
public static void WeaveEditorAssembliesMenuItem()
{
WeaveEditorAssemblies = !WeaveEditorAssemblies;

// If we're switching weaving of editor assemblies on, we should re-weave all assemblies
// to pick up the editor assemblies as well.
if (WeaveEditorAssemblies)
{
WeaveAllAssembliesMenuItem();
}
}

private static void WeaveAssembliesOnEditorLaunch()
Expand All @@ -117,7 +144,7 @@ private static async Task<int> WeaveAllAssemblies()
try
{
EditorApplication.LockReloadAssemblies();
var weavingTasks = CompilationPipeline.GetAssemblies()
var weavingTasks = GetAssemblies()
.Select(assembly => Task.Run(() =>
{
if (!WeaveAssemblyCore(assembly.outputPath, assembly.allReferences, "Unity Editor", GetTargetOSName(Application.platform)))
Expand Down Expand Up @@ -246,6 +273,16 @@ public void OnPostBuildPlayerScriptDLLs(BuildReport report)
}
}

private static Assembly[] GetAssemblies()
{
if (WeaveEditorAssemblies)
{
return CompilationPipeline.GetAssemblies();
}

return CompilationPipeline.GetAssemblies(AssembliesType.Player);
}

private static string GetTargetOSName(BuildTarget target)
{
// These have to match Analytics.GetConfig(FrameworkName)
Expand Down
3 changes: 2 additions & 1 deletion Tools/SetupUnityPackage/CommandLineOptions/TestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class TestOptions : OptionsBase
"Realm.Fody",
"Fody",
"Realm",
"Microsoft.CSharp"
"Microsoft.CSharp",
"System.Configuration.ConfigurationManager"
};

public override PackageInfo[] Files { get; } = new[]
Expand Down