diff --git a/Assets/Extra/ReceiveFromURL.cs b/Assets/Extra/ReceiveFromURL.cs deleted file mode 100644 index 4f42d45..0000000 --- a/Assets/Extra/ReceiveFromURL.cs +++ /dev/null @@ -1,153 +0,0 @@ -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(); - } - - [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(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(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 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; - } -} diff --git a/Assets/Extra/SendChildrenToSpeckle.cs b/Assets/Extra/SendChildrenToSpeckle.cs index 47f17f1..4de0ebc 100644 --- a/Assets/Extra/SendChildrenToSpeckle.cs +++ b/Assets/Extra/SendChildrenToSpeckle.cs @@ -2,11 +2,8 @@ using System.Collections.Concurrent; using System.Collections.Immutable; using System.Linq; -using System.Runtime.CompilerServices; using Speckle.ConnectorUnity; -using UnityEditor.Experimental; using UnityEngine; -using UnityEngine.Events; [RequireComponent(typeof(Sender)), ExecuteAlways] [Obsolete] @@ -23,7 +20,7 @@ void Awake() { sender = GetComponent(); } - + [ContextMenu(nameof(Send))] public void Send() { @@ -31,30 +28,32 @@ public void Send() .Where(t => t != this.transform) .Select(o => o.gameObject) .ToImmutableHashSet(); - + Debug.Log("starting send..."); - sender.Send(streamId, selected, null, branchName, createCommit, + sender.Send( + streamId, + selected, + null, + branchName, + createCommit, onErrorAction: OnError, onProgressAction: OnProgress, - onDataSentAction: OnSent); + onDataSentAction: OnSent + ); } private void OnSent(string objectId) { Debug.Log($"Data sent {objectId}", this); } - + private void OnError(string message, Exception e) { Debug.LogError($"Error while sending {message} \n {e}", this); - } + private void OnProgress(ConcurrentDictionary dict) { Debug.Log($"progress was made", this); } - - - - } diff --git a/Assets/InteractionLogic.cs b/Assets/InteractionLogic.cs index 5418810..b7b80ef 100644 --- a/Assets/InteractionLogic.cs +++ b/Assets/InteractionLogic.cs @@ -1,220 +1,244 @@ using System; -using System.Collections; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using Speckle.Core.Api; -using Speckle.Core.Logging; using UnityEngine; using UnityEngine.UI; using Text = UnityEngine.UI.Text; namespace Speckle.ConnectorUnity -{ +{ [Obsolete] - public class InteractionLogic : MonoBehaviour - { - private Receiver receiver; - - public void InitReceiver(Stream stream, bool autoReceive) + public class InteractionLogic : MonoBehaviour { - gameObject.name = $"receiver-{stream.id}-{Guid.NewGuid().ToString()}"; - InitRemove(); - - receiver = gameObject.AddComponent(); - receiver.Stream = stream; - - var btn = gameObject.transform.Find("Btn").GetComponentInChildren