Skip to content

Commit

Permalink
fix: can't write generated assets to package directory in UPM packages (
Browse files Browse the repository at this point in the history
#89)

Closes: #77
  • Loading branch information
bdunderscore authored Dec 20, 2023
1 parent 86ce960 commit 684f7c9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

### Fixed
- Play mode processing fails when installed via UPM (#89)

### Changed

Expand Down
64 changes: 60 additions & 4 deletions Editor/AvatarProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using nadena.dev.ndmf.runtime;
using UnityEditor;
using UnityEngine;
Expand All @@ -12,6 +14,8 @@

namespace nadena.dev.ndmf
{
using UPMClient = UnityEditor.PackageManager.Client;
using UPM = UnityEditor.PackageManager.Requests;
#region

using UnityObject = Object;
Expand All @@ -20,16 +24,16 @@ namespace nadena.dev.ndmf

internal class OverrideTemporaryDirectoryScope : IDisposable
{
private string priorDirectory = AvatarProcessor.TemporaryAssetRoot;
private string priorDirectory = AvatarProcessor.OverrideAssetRoot;

public OverrideTemporaryDirectoryScope(string path)
{
AvatarProcessor.TemporaryAssetRoot = path;
AvatarProcessor.OverrideAssetRoot = path;
}

public void Dispose()
{
AvatarProcessor.TemporaryAssetRoot = priorDirectory;
AvatarProcessor.OverrideAssetRoot = priorDirectory;
}
}

Expand All @@ -43,7 +47,59 @@ internal class AvatarBuildStateTracker : MonoBehaviour
/// </summary>
public static class AvatarProcessor
{
internal static string TemporaryAssetRoot = "Packages/nadena.dev.ndmf/__Generated";
private static UPM.ListRequest _listRequest;

[InitializeOnLoadMethod]
static void GetPackageInfo()
{
_listRequest = UPMClient.List();
}

internal static string DefaultAssetRoot = null;
internal static string OverrideAssetRoot = null;

internal static string TemporaryAssetRoot
{
get
{
if (OverrideAssetRoot != null) return OverrideAssetRoot;
if (DefaultAssetRoot == null)
{
Stopwatch timer = new Stopwatch();
timer.Start();
while (!_listRequest.IsCompleted && timer.ElapsedMilliseconds < 10000)
{
Thread.Sleep(100);
// spin-wait - this is important in play mode as the UPM operation won't finish by the time we
// need to start processing the avatar.
}

if (!_listRequest.IsCompleted)
{
throw new Exception("UPM processing timed out");
}

var embeddedPkg = _listRequest.Result.FirstOrDefault(pkg =>
{
if (pkg.name != "nadena.dev.ndmf") return false;
return pkg.source == UnityEditor.PackageManager.PackageSource.Embedded;
});

if (embeddedPkg != null)
{
DefaultAssetRoot = embeddedPkg.assetPath + "/GeneratedAssets";
}
else
{
DefaultAssetRoot = "Assets/ZZZ_GeneratedAssets";
}

Debug.Log("NDMF: Using path " + DefaultAssetRoot + " for temporary assets");
}

return DefaultAssetRoot;
}
}

/// <summary>
/// Deletes all temporary assets after a build.
Expand Down

0 comments on commit 684f7c9

Please sign in to comment.