-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from specklesystems/jrm/traversal-refactor
implementation of traversal refactor
- Loading branch information
Showing
41 changed files
with
1,364 additions
and
634 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
File renamed without changes.
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,153 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Speckle.ConnectorUnity.Components; | ||
using Speckle.ConnectorUnity.Utils; | ||
using Speckle.Core.Api; | ||
using Speckle.Core.Credentials; | ||
using Speckle.Core.Logging; | ||
using Speckle.Core.Models; | ||
using UnityEngine; | ||
|
||
[AddComponentMenu("Speckle/Extras/Receive From Url")] | ||
[RequireComponent(typeof(RecursiveConverter)), ExecuteAlways] | ||
public class ReceiveFromURL : MonoBehaviour | ||
{ | ||
[Tooltip("Url of your speckle object/commit/branch/stream")] | ||
public string url; | ||
|
||
private RecursiveConverter _converter; | ||
|
||
#nullable enable | ||
private CancellationTokenSource? _tokenSource; | ||
void Awake() | ||
{ | ||
_converter = GetComponent<RecursiveConverter>(); | ||
} | ||
|
||
[ContextMenu(nameof(Receive))] | ||
public void Receive() | ||
{ | ||
StartCoroutine(Receive_Routine()); | ||
} | ||
|
||
public IEnumerator Receive_Routine() | ||
{ | ||
if (IsBusy()) throw new InvalidOperationException("A receive operation has already started"); | ||
_tokenSource = new CancellationTokenSource(); | ||
try | ||
{ | ||
StreamWrapper sw = new(url); | ||
|
||
if (!sw.IsValid) | ||
throw new InvalidOperationException("Speckle url input is not a valid speckle stream/branch/commit"); | ||
|
||
var accountTask = new Utils.WaitForTask<Account>(async () => await GetAccount(sw)); | ||
yield return accountTask; | ||
|
||
_tokenSource.Token.ThrowIfCancellationRequested(); | ||
using Client c = new(accountTask.Result); | ||
|
||
var objectIdTask = new Utils.WaitForTask<(string, Commit?)>(async () => await GetObjectID(sw, c)); | ||
yield return objectIdTask; | ||
(string objectId, Commit? commit) = objectIdTask.Result; | ||
|
||
Debug.Log($"Receiving from {sw.ServerUrl}..."); | ||
|
||
var receiveTask = new Utils.WaitForTask<Base>(async () => await SpeckleReceiver.ReceiveAsync( | ||
c, | ||
sw.StreamId, | ||
objectId, | ||
commit, | ||
cancellationToken: _tokenSource.Token)); | ||
yield return receiveTask; | ||
|
||
Debug.Log("Converting to native..."); | ||
_converter.RecursivelyConvertToNative_Sync(receiveTask.Result, transform); | ||
} | ||
finally | ||
{ | ||
_tokenSource.Dispose(); | ||
_tokenSource = null; | ||
} | ||
} | ||
|
||
|
||
private async Task<(string objectId, Commit? commit)> GetObjectID(StreamWrapper sw, Client client) | ||
{ | ||
string objectId; | ||
Commit? commit = null; | ||
//OBJECT URL | ||
if (!string.IsNullOrEmpty(sw.ObjectId)) | ||
{ | ||
objectId = sw.ObjectId; | ||
} | ||
//COMMIT URL | ||
else if (!string.IsNullOrEmpty(sw.CommitId)) | ||
{ | ||
commit = await client.CommitGet(sw.StreamId, sw.CommitId).ConfigureAwait(false); | ||
objectId = commit.referencedObject; | ||
} | ||
//BRANCH URL OR STREAM URL | ||
else | ||
{ | ||
var branchName = string.IsNullOrEmpty(sw.BranchName) ? "main" : sw.BranchName; | ||
|
||
var branch = await client.BranchGet(sw.StreamId, branchName, 1).ConfigureAwait(false); | ||
if (!branch.commits.items.Any()) | ||
throw new SpeckleException("The selected branch has no commits."); | ||
|
||
commit = branch.commits.items[0]; | ||
objectId = branch.commits.items[0].referencedObject; | ||
} | ||
|
||
return (objectId, commit); | ||
} | ||
[ContextMenu(nameof(Cancel))] | ||
public void Cancel() | ||
{ | ||
if (IsNotBusy()) throw new InvalidOperationException("There are no pending receive operations to cancel"); | ||
_tokenSource!.Cancel(); | ||
} | ||
|
||
[ContextMenu(nameof(Cancel), true)] | ||
public bool IsBusy() | ||
{ | ||
return _tokenSource is not null; | ||
} | ||
|
||
[ContextMenu(nameof(Receive), true)] | ||
internal bool IsNotBusy() => !IsBusy(); | ||
|
||
private void OnDisable() | ||
{ | ||
_tokenSource?.Cancel(); | ||
} | ||
|
||
|
||
private async Task<Account> GetAccount(StreamWrapper sw) | ||
{ | ||
Account account; | ||
try | ||
{ | ||
account = await sw.GetAccount().ConfigureAwait(false); | ||
} | ||
catch (SpeckleException e) | ||
{ | ||
if (string.IsNullOrEmpty(sw.StreamId)) | ||
throw e; | ||
|
||
//Fallback to a non authed account | ||
account = new Account | ||
{ | ||
token = "", | ||
serverInfo = new ServerInfo { url = sw.ServerUrl }, | ||
userInfo = new UserInfo() | ||
}; | ||
} | ||
|
||
return account; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
Assets/Tests/Editor/PerformanceTest.cs.meta → Assets/Extra/ReceiveFromURL.cs.meta
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"name": "Speckle.Extra", | ||
"references":[ "GUID:eed1b8b83e2c0074d9e5de2348e3ff72", "GUID:e6adfdc4e436206479f48eafc82f32b5", "GUID:d274441ecc3eb3f43b093eec1503d681" ] | ||
"references":[ "GUID:eed1b8b83e2c0074d9e5de2348e3ff72", "GUID:e6adfdc4e436206479f48eafc82f32b5", "GUID:d274441ecc3eb3f43b093eec1503d681", "GUID:50d889142fdf9de4b8501c6eaa4b3225" ] | ||
} |
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
Oops, something went wrong.