Skip to content

Commit

Permalink
Fix NRE on Unity in batch mode (#3373)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirinchev authored Jul 7, 2023
1 parent 9b29f1a commit be2208e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
### Fixed
* Fixed an issue on Unity on Windows when the weaver would trigger excessive terminal windows to open. (Issue [3364]https://github.com/realm/realm-dotnet/issues/3364)
* Fixed an issue on Unity on CI where weaving would fail with the following error: `Could not analyze the user's assembly. Cannot access a closed Stream.`. (Issue [3364]https://github.com/realm/realm-dotnet/issues/3364)
* Fixed a `NullReferenceException` when weaving classes on Unity in batch mode. (Issue [#3363](https://github.com/realm/realm-dotnet/issues/3363))

### Compatibility
* Realm Studio: 13.0.0 or later.
Expand Down
23 changes: 11 additions & 12 deletions Realm/Realm.UnityWeaver/UnityWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class UnityWeaver : IPostBuildPlayerScriptDLLs, IPreprocessBuildWithRepor
private const string UnityPackageName = "io.realm.unity";

private static readonly int UnityMajorVersion = int.Parse(Application.unityVersion.Split('.')[0]);
private static readonly TaskCompletionSource<string> _installMethodTask = new();

private static bool _analyticsEnabled;

Expand All @@ -66,7 +67,6 @@ private static bool AnalyticsEnabled

private static bool _weaveEditorAssemblies;
private static ListRequest? _listRequest;
private static TaskCompletionSource<string>? _installMethodTask;

private static bool WeaveEditorAssemblies
{
Expand All @@ -84,20 +84,19 @@ private static bool WeaveEditorAssemblies
[InitializeOnLoadMethod]
public static void Initialize()
{
if (Application.isBatchMode)
{
// In batch mode, `update` won't get called until compilation is complete,
// which means we'll deadlock when we block compilation on the tcs completing
_installMethodTask.TrySetResult(Metric.Unknown());
}

// We need to call that again after the editor is initialized to ensure that we populate the checkmark correctly.
EditorApplication.delayCall += () =>
{
_listRequest = Client.List();
_installMethodTask = new TaskCompletionSource<string>();
if (Application.isBatchMode)
{
// In batch mode, `update` won't get called until compilation is complete,
// which means we'll deadlock when we block compilation on the tcs completing
_installMethodTask.TrySetResult(Metric.Unknown());
}
else
if (!Application.isBatchMode)
{
EditorApplication.update += OnEditorApplicationUpdate;
}
Expand Down Expand Up @@ -149,7 +148,7 @@ private static void OnEditorApplicationUpdate()
};
}

_installMethodTask!.SetResult(installMethod);
_installMethodTask.TrySetResult(installMethod);
}

[MenuItem("Tools/Realm/Weave Assemblies")]
Expand Down Expand Up @@ -521,7 +520,7 @@ private static Config GetAnalyticsConfig(BuildTarget? target = null)
NetFrameworkTarget = netFrameworkInfo.Name,
NetFrameworkTargetVersion = netFrameworkInfo.Version,
AnalyticsCollection = analyticsEnabled ? AnalyticsCollection.Full : AnalyticsCollection.Disabled,
InstallationMethod = _installMethodTask!.Task.Wait(1000) ? _installMethodTask.Task.Result : Metric.Unknown(),
InstallationMethod = _installMethodTask.Task.Wait(1000) ? _installMethodTask.Task.Result : Metric.Unknown(),
FrameworkName = target == null ? Metric.Framework.UnityEditor : Metric.Framework.Unity,
FrameworkVersion = Application.unityVersion,
TargetArchitecture = GetCpuArchitecture(target),
Expand Down

0 comments on commit be2208e

Please sign in to comment.