Skip to content

Commit

Permalink
Support batchmode (apple/unityplugin apple#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
VMaldecoasago committed Nov 27, 2024
1 parent 856bcea commit eda8e63
Showing 1 changed file with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using UnityEditor.PackageManager;
using UnityEditor.PackageManager.Requests;
using UnityEngine;
using System.Threading;

namespace Apple.Core
{
Expand Down Expand Up @@ -38,9 +39,8 @@ static ApplePlatformID()
}
}

[InitializeOnLoad]
public static class ApplePlugInEnvironment
{
public class ApplePlugInEnvironment : AssetPostprocessor
{
/// <summary>
/// Name of the folder that the Apple Unity Plug-Ins will use for storing permanent data assets and helper objects
/// </summary>
Expand Down Expand Up @@ -106,6 +106,7 @@ public static class ApplePlugInEnvironment
/// </summary>
private enum UpdateState
{
NotInitialized,
Initializing,
Updating
}
Expand All @@ -129,10 +130,14 @@ private enum UpdateState
private static string _trackedApplePlatform;

/// <summary>
/// Static constructor used by Unity for initialization of the ApplePlugInEnvironment.
/// Initialize the ApplePlugInEnvironment after all assets finished processing, so we can alter our own.
/// </summary>
static ApplePlugInEnvironment()
private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths, bool didDomainReload)
{
if (_updateState != UpdateState.NotInitialized) { return; }

_updateState = UpdateState.Initializing;

// Ensure that the necessary Apple Unity Plug-In support folders exist and let user know if any have been created.
string createFolderMessage = "[Apple Unity Plug-ins] Creating support folders:\n";
bool foldersCreated = false;
Expand Down Expand Up @@ -171,12 +176,23 @@ static ApplePlugInEnvironment()
_packageManagerListRequest = Client.List(false, true);

// Initialize state tracking
_updateState = UpdateState.Initializing;
_trackedAppleConfig = GetAppleBuildConfig();
_trackedApplePlatform = GetApplePlatformID(EditorUserBuildSettings.activeBuildTarget);

EditorApplication.update += OnEditorUpdate;
Events.registeringPackages += OnPackageManagerRegistrationUpdate;

if (Application.isBatchMode) {
// when in -batchmode -quit, EditorUpdate is not called, so we have to
// lock the main thread until the packages are downloaded
static long nowMilis() => DateTimeOffset.Now.ToUnixTimeMilliseconds();
long start = nowMilis();
const long timeout = 10000;
while (_updateState != UpdateState.Updating && nowMilis() - start < timeout) {
Thread.Sleep(100);
OnEditorUpdate();
}
}
}

/// <summary>
Expand Down

0 comments on commit eda8e63

Please sign in to comment.