Skip to content

Commit

Permalink
Merge pull request #93 from specklesystems/jrm/traversal-refactor
Browse files Browse the repository at this point in the history
implementation of traversal refactor
  • Loading branch information
JR-Morgan authored Jun 26, 2023
2 parents 73ef71f + 62875c0 commit 5db1f45
Show file tree
Hide file tree
Showing 41 changed files with 1,364 additions and 634 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Threading.Tasks;
using Speckle.ConnectorUnity;
Expand All @@ -7,6 +8,7 @@
using Speckle.Core.Transports;
using UnityEngine;

[AddComponentMenu("Speckle/Extras/Manual Receiver")]
[RequireComponent(typeof(RecursiveConverter))]
public class ManualReceive : MonoBehaviour
{
Expand All @@ -30,7 +32,8 @@ IEnumerator Start()
if(Time.timeSinceLevelLoad > 20) yield return null;
Receive();
}


[ContextMenu(nameof(Receive))]
public void Receive()
{
var account = new Account()
Expand All @@ -48,14 +51,17 @@ public void Receive()
objectId,
remoteTransport: transport,
localTransport: localTransport,
onErrorAction: (m, e)=> Debug.LogError(m + e),
onErrorAction: (m, e) => Debug.LogError(m + e),
disposeTransports: true
);
if (@base == null) throw new Exception("received data was null!");
Dispatcher.Instance().Enqueue(() =>
{
var parentObject = new GameObject(name);
receiver.RecursivelyConvertToNative(@base, parentObject.transform);
receiver.RecursivelyConvertToNative_Sync(@base, parentObject.transform);
Debug.Log($"Receive {objectId} completed");
});
Expand Down
File renamed without changes.
153 changes: 153 additions & 0 deletions Assets/Extra/ReceiveFromURL.cs
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;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Assets/Extra/SendChildrenToSpeckle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using UnityEngine.Events;

[RequireComponent(typeof(Sender)), ExecuteAlways]
[Obsolete]
public class SendChildrenToSpeckle : MonoBehaviour
{
public LayerMask layerMask;
Expand Down
2 changes: 1 addition & 1 deletion Assets/Extra/Speckle.Extra.asmdef
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" ]
}
5 changes: 3 additions & 2 deletions Assets/InteractionLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
using Text = UnityEngine.UI.Text;

namespace Speckle.ConnectorUnity
{
{
[Obsolete]
public class InteractionLogic : MonoBehaviour
{
private Receiver receiver;
Expand Down Expand Up @@ -216,4 +217,4 @@ private void InitRemove()
});
}
}
}
}
4 changes: 3 additions & 1 deletion Assets/SpeckleExamples.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using Speckle.Core.Credentials;
Expand All @@ -8,6 +9,7 @@

namespace Speckle.ConnectorUnity
{
[Obsolete]
public class SpeckleExamples : MonoBehaviour
{
public Text SelectStreamText;
Expand Down Expand Up @@ -129,4 +131,4 @@ private void ReorderStreamPrefabs()
}
}
}
}
}
Loading

0 comments on commit 5db1f45

Please sign in to comment.