generated from vrchat-community/template-package
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: exceptions sometimes thrown when accessing R/O meshes from previ…
- Loading branch information
1 parent
e70e43b
commit 64c96e6
Showing
7 changed files
with
218 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
namespace nadena.dev.ndmf.preview | ||
{ | ||
/// <summary> | ||
/// The default unity synchronization context runs in the context of the Player Loop, which blocks access to R/O | ||
/// mesh data. As such, NDMF provides a synchronization context which runs in the context of | ||
/// `EditorApplication.delayCall` instead. | ||
/// </summary> | ||
public static class NDMFSyncContext | ||
{ | ||
public static SynchronizationContext Context = new Impl(); | ||
|
||
/// <summary> | ||
/// Switches to the NDMF synchronization context, and returns an IDisposable which will restore the prior | ||
/// synchronization context. | ||
/// </summary> | ||
/// <returns></returns> | ||
public static IDisposable Scope() | ||
{ | ||
return new SyncContextScope(); | ||
} | ||
|
||
private class SyncContextScope : IDisposable | ||
{ | ||
private readonly SynchronizationContext _prior = SynchronizationContext.Current; | ||
|
||
public SyncContextScope() | ||
{ | ||
SynchronizationContext.SetSynchronizationContext(Context); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
SynchronizationContext.SetSynchronizationContext(_prior); | ||
} | ||
} | ||
|
||
private class Impl : SynchronizationContext | ||
{ | ||
private readonly object _lock = new(); | ||
private readonly EditorApplication.CallbackFunction _turnDelegate; | ||
private int unityMainThreadId = -1; | ||
private readonly List<WorkRequest> asyncQueue = new(); | ||
private readonly List<WorkRequest> localQueue = new(); | ||
private bool isRegistered, isTurning; | ||
|
||
internal Impl() | ||
{ | ||
_turnDelegate = Turn; | ||
} | ||
|
||
// invoked under _lock | ||
private void RegisterCallback() | ||
{ | ||
if (isRegistered) return; | ||
isRegistered = true; | ||
EditorApplication.delayCall += _turnDelegate; | ||
} | ||
|
||
public void Turn() | ||
{ | ||
lock (_lock) | ||
{ | ||
unityMainThreadId = Thread.CurrentThread.ManagedThreadId; | ||
localQueue.AddRange(asyncQueue); | ||
asyncQueue.Clear(); | ||
isRegistered = false; | ||
isTurning = true; | ||
} | ||
|
||
while (localQueue.Count > 0 && !TaskThrottle.ShouldThrottle) | ||
{ | ||
foreach (var ev in localQueue) | ||
{ | ||
ev.Run(); | ||
} | ||
|
||
localQueue.Clear(); | ||
|
||
if (!TaskThrottle.ShouldThrottle) | ||
{ | ||
lock (_lock) | ||
{ | ||
localQueue.AddRange(asyncQueue); | ||
asyncQueue.Clear(); | ||
} | ||
} | ||
} | ||
|
||
lock (_lock) | ||
{ | ||
if (localQueue.Count > 0) | ||
{ | ||
RegisterCallback(); | ||
} | ||
|
||
isTurning = false; | ||
} | ||
} | ||
|
||
public override void Post(SendOrPostCallback d, object state) | ||
{ | ||
lock (_lock) | ||
{ | ||
asyncQueue.Add(new WorkRequest { callback = d, state = state }); | ||
RegisterCallback(); | ||
} | ||
} | ||
|
||
public override void Send(SendOrPostCallback d, object state) | ||
{ | ||
ManualResetEvent wait = null; | ||
var runLocally = false; | ||
lock (_lock) | ||
{ | ||
runLocally = unityMainThreadId == Thread.CurrentThread.ManagedThreadId && isTurning; | ||
if (!runLocally) | ||
{ | ||
wait = new ManualResetEvent(false); | ||
asyncQueue.Add(new WorkRequest { callback = d, state = state, waitHandle = wait }); | ||
RegisterCallback(); | ||
} | ||
} | ||
|
||
if (runLocally) | ||
{ | ||
try | ||
{ | ||
d(state); | ||
} | ||
catch (Exception e) | ||
{ | ||
Debug.LogException(e); | ||
} | ||
} | ||
else | ||
{ | ||
wait.WaitOne(); | ||
} | ||
} | ||
} | ||
|
||
private class WorkRequest | ||
{ | ||
public SendOrPostCallback callback; | ||
public object state; | ||
public ManualResetEvent waitHandle; | ||
|
||
public void Run() | ||
{ | ||
try | ||
{ | ||
callback(state); | ||
} | ||
catch (Exception e) | ||
{ | ||
Debug.LogException(e); | ||
} | ||
finally | ||
{ | ||
waitHandle?.Set(); | ||
} | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters