Пример #1
0
        /// <summary>
        /// Tries to restore the binding or find closest PCF.
        /// </summary>
        void RestoreBinding(string objId)
        {
            if (MLPersistentStore.Contains(objId))
            {
                MLContentBinding binding;

                MLResult result = MLPersistentStore.Load(objId, out binding);
                if (!result.IsOk)
                {
                    SetError(new MLResult(result.Code,
                                          string.Format("Error: MLPersistentPoint failed to load binding. Reason: {0}", result)));
                    SetComplete(false);
                }
                else
                {
                    Binding            = binding;
                    Binding.GameObject = this.gameObject;
                    MLContentBinder.Restore(Binding, HandleBindingRestore);
                }
            }
            else
            {
                StartCoroutine(BindToClosestPCF());
            }
        }
Пример #2
0
        /// <summary>
        /// Creates a binding to the closest PCF
        /// </summary>
        /// <returns>Must be executed as a Coroutine</returns>
        IEnumerator TryBindingToClosestPCF()
        {
            _done = false;

            MLResult returnResult = MLPersistentCoordinateFrames.FindClosestPCF(gameObject.transform.position, (result, returnPCF) =>
            {
                if (result.IsOk && returnPCF.CurrentResult == MLResultCode.Ok)
                {
                    Debug.Log("Binding to closest found PCF: " + returnPCF.CFUID);
                    Binding = MLContentBinder.BindToPCF(gameObject.name, gameObject, returnPCF);
                    MLPersistentStore.Save(Binding);
                    SetComplete(true);
                    _done = true;
                }
                else
                {
                    Debug.LogErrorFormat("Error: MLPersistentPoint failed to find closest PCF. Reason: {0}", result);
                    SetComplete(false);
                    _done = true;
                }
            });

            if (!returnResult.IsOk)
            {
                // Technically, if we reach this point, the system had a problem
                Debug.LogErrorFormat("Error: MLPersistentPoint failed to attempt to find closest PCF. Reason: {0}", returnResult);
                SetComplete(false);
                _done = true;
            }

            while (!_done)
            {
                yield return(null);
            }
        }
        /// <summary>
        /// Creates a binding to the closest PCF
        /// </summary>
        /// <returns>Must be executed as a Coroutine</returns>
        IEnumerator TryBindingToClosestPCF()
        {
            _done = false;
            MLResult returnResult = MLPersistentCoordinateFrames.FindClosestPCF(gameObject.transform.position, (pcfPositionResult, pcfWithPosition) =>
            {
                if (pcfPositionResult.IsOk && pcfWithPosition != null && pcfWithPosition.CurrentResult == MLResultCode.Ok)
                {
                    Debug.Log("Binding to closest found PCF: " + pcfWithPosition.CFUID);
                    Binding = MLContentBinder.BindToPCF(UniqueId, gameObject, pcfWithPosition);
                    MLPersistentStore.Save(Binding);
                    NotifyChangeOfStatus(Status.BINDING_CREATED, MLResult.ResultOk);
                    RegisterPCFEventHandlers();
                    _done = true;
                }
                else
                {
                    Debug.LogErrorFormat("Error: MLPersistentBehavior failed to get PCF position. Reason: {0}", pcfPositionResult);
                    NotifyChangeOfStatus(Status.BINDING_CREATE_FAILED, pcfPositionResult);
                    _done = true;
                }
            });

            if (!returnResult.IsOk)
            {
                Debug.LogErrorFormat("Error: MLPersistentBehavior failed to attempt to find closest PCF. Reason: {0}", returnResult);
                NotifyChangeOfStatus(Status.BINDING_CREATE_FAILED, returnResult);
                _done = true;
            }

            while (!_done)
            {
                yield return(null);
            }
        }
 /// <summary>
 /// Destroys the given binding
 /// </summary>
 /// <param name="binding">Binding</param>
 void DestroyBindingInternal(MLContentBinding binding)
 {
     if (binding != null)
     {
         MLPersistentStore.DeleteBinding(binding);
     }
 }
Пример #5
0
        /// <summary>
        /// Tries to restore the binding from persistent storage and PCF system
        /// </summary>
        IEnumerator RestoreBinding(string objId)
        {
            _state = State.RestoreBinding;

            if (MLPersistentStore.Contains(objId))
            {
                MLContentBinding binding;

                MLResult result = MLPersistentStore.Load(objId, out binding);
                if (!result.IsOk)
                {
                    SetError(result);
                    _state = State.BindingComplete;
                }
                else
                {
                    Binding = binding;
                    Debug.Log("binding result : " + Binding.PCF.CurrentResult);
                    Binding.GameObject = this.gameObject;
                    MLContentBinder.Restore(Binding, HandleBindingRestore);
                }
            }
            else
            {
                BindToAllPCFs();
            }

            while (_state != State.BindingComplete)
            {
                yield return(null);
            }
            yield break;
        }
        /// <summary>
        /// Handler for binding restore
        /// </summary>
        /// <param name="contentBinding">Content binding.</param>
        /// <param name="resultCode">Result code.</param>
        void HandleBindingRestore(MLContentBinding contentBinding, MLResult result)
        {
            if (!result.IsOk)
            {
                if (NumRetriesForRestore > 0)
                {
                    NumRetriesForRestore--;
                    Debug.LogWarningFormat("Failed to restore: {0} - {1}. Retries left: {2}. Result Code: {3}",
                                           gameObject.name, contentBinding.PCF.CFUID, NumRetriesForRestore, result);
                    NotifyChangeOfStatus(Status.RESTORE_RETRY, result);
                    StartCoroutine(TryRestore());
                }
                else
                {
                    string logMessage = string.Format("Failed to restore : {0} - {1}. Result code: {2}",
                                                      gameObject.name, contentBinding.PCF.CFUID, result);
                    if (result.Code == MLResultCode.SnapshotPoseNotFound)
                    {
                        // Content is bound to a PCF in a different map
                        Debug.LogWarning(logMessage);
                    }
                    else
                    {
                        Debug.LogError(logMessage);
                    }

                    NotifyChangeOfStatus(Status.RESTORE_FAILED, result);
                }
            }
            else
            {
                NotifyChangeOfStatus(Status.RESTORE_SUCCESSFUL, MLResult.ResultOk);
                RegisterPCFEventHandlers();
            }
        }
Пример #7
0
 /// <summary>
 /// Destroys the given binding
 /// </summary>
 /// <param name="binding">Binding</param>
 void DestroyBindingInternal(MLContentBinding binding)
 {
     if (binding != null)
     {
         NotifyChangeOfStatus(Status.BINDING_DESTROYED, MLResult.ResultOk);
         MLPersistentStore.DeleteBinding(binding);
     }
 }
Пример #8
0
 /// <summary>
 /// Bind this gameObject to a pcf
 /// </summary>
 /// <param name="pcf">The pcf to bind to</param>
 void BindToPCF(MLPCF pcf)
 {
     UnregisterPCFEventHandlers();
     DestroyBinding();
     Binding = MLContentBinder.BindToPCF(UniqueId, gameObject, pcf);
     MLPersistentStore.Save(Binding);
     NotifyChangeOfStatus(Status.BINDING_CREATED, MLResult.ResultOk);
     RegisterPCFEventHandlers();
     _pcfLost = false;
 }
Пример #9
0
 /// <summary>
 /// Handler for binding restore
 /// </summary>
 /// <param name="contentBinding">Content binding.</param>
 /// <param name="resultCode">Result code.</param>
 void HandleBindingRestore(MLContentBinding contentBinding, MLResult result)
 {
     _state = State.BindingComplete;
     Debug.Log("binding result : " + contentBinding.PCF.CurrentResult);
     if (!result.IsOk)
     {
         MLPersistentStore.DeleteBinding(contentBinding);
         Debug.LogFormat("Failed to restore : {0} - {1}. Result code:", gameObject.name, contentBinding.PCF.CFUID, result.Code);
     }
 }
 /// <summary>
 /// Restore the specified binding and call the callback when the restore is complete
 /// </summary>
 /// <param name="binding">Binding.</param>
 /// <param name="callback">callback.</param>
 /// <returns>
 /// MLResult.code will be MLResult.Ok when operation is successful
 ///
 /// MLResult.code will be MLResult.InvalidParam when callback is null
 ///
 /// MLResult.code will be MLResult.UnspecifiedFailure when MLPersistentCoordinateFrames is not started
 /// </returns>
 public static MLResult Restore(MLContentBinding binding, Action <MLContentBinding, MLResult> callback)
 {
     if (binding == null || callback == null)
     {
         MLResult result = new MLResult(MLResultCode.InvalidParam, "Parameters are null");
         MLPluginLog.ErrorFormat("MLContentBindings.Restore failed restoring binding. Reason: {0}", result);
         return(result);
     }
     return(binding.Restore(callback));
 }
Пример #11
0
 void InternalDeleteBinding(MLContentBinding binding)
 {
     if (_data != null && _data.Bindings != null && _data.Bindings.Contains(binding))
     {
         _data.Bindings.Remove(binding);
         _saveRequired = true;
     }
     if (_virtualIdToBindings != null && _virtualIdToBindings.ContainsKey(binding.ObjectId))
     {
         _virtualIdToBindings.Remove(binding.ObjectId);
     }
 }
Пример #12
0
 MLResult LoadInternal(string virtualObjId, out MLContentBinding binding)
 {
     binding = new MLContentBinding();
     if (string.IsNullOrEmpty(virtualObjId))
     {
         return(new MLResult(MLResultCode.InvalidParam, "virtual object id is not set."));
     }
     if (!ContainsInternal(virtualObjId))
     {
         return(new MLResult(MLResultCode.InvalidParam, "virtual object id not found."));
     }
     binding = _virtualIdToBindings[virtualObjId];
     return(MLResult.ResultOk);
 }
        /// <summary>
        /// Tries to restore the binding
        /// </summary>
        void RestoreBinding()
        {
            MLContentBinding binding;

            MLResult result = MLPersistentStore.Load(UniqueId, out binding);

            if (!result.IsOk)
            {
                Debug.LogErrorFormat("Error: MLPersistentBehavior failed to load binding. Reason: {0}", result);
                NotifyChangeOfStatus(Status.RESTORE_FAILED, result);
            }
            else
            {
                Binding            = binding;
                Binding.GameObject = this.gameObject;
                MLContentBinder.Restore(Binding, HandleBindingRestore);
            }
        }
Пример #14
0
        /// <summary>
        /// Handler for binding restore
        /// </summary>
        /// <param name="contentBinding">Content binding.</param>
        /// <param name="resultCode">Result code.</param>
        void HandleBindingRestore(MLContentBinding contentBinding, MLResult result)
        {
            _pcfRestoring = false;

            if (!result.IsOk)
            {
                string logMessage = string.Format("Failed to restore : {0} - {1}. Result code: {2}",
                                                  gameObject.name, contentBinding.PCF.CFUID, result);
                Debug.LogError(logMessage);
                NotifyChangeOfStatus(Status.RESTORE_FAILED, result);
            }
            else
            {
                NotifyChangeOfStatus(Status.RESTORE_SUCCESSFUL, MLResult.ResultOk);
                if (!_pcfLost)
                {
                    // Handles two cases, registering for pcf events on app start and registering for pcf events when a pcf is lost and regained during runtime
                    UnregisterPCFEventHandlers();
                    RegisterPCFEventHandlers();
                }
            }
        }
Пример #15
0
        /// <summary>
        /// Finds the closest pcf for this persistent point.
        /// </summary>
        void BindToAllPCFs()
        {
            _state = State.BindToAllPCFs;
            string suffix = "";
            int    count  = 0;

            // In the loop below we try to associate the persitent point with not only
            // the closest but all pcfs in the surrounding. This will increase the probablilty
            // of restoration on reboots. It's costly in terms of disk space so we will limit it to
            // a max
            foreach (MLPCF pcf in _allPCFs)
            {
                string objectName   = gameObject.name + suffix;
                var    returnResult = MLPersistentCoordinateFrames.GetPCFPosition(pcf, (result, returnPCF) =>
                {
                    if (result.IsOk && pcf.CurrentResult == MLResultCode.Ok)
                    {
                        Debug.Log("binding to PCF: " + pcf.CFUID);

                        Binding = MLContentBinder.BindToPCF(objectName, gameObject, pcf);
                        MLPersistentStore.Save(Binding);
                    }
                    else
                    {
                        Debug.LogWarningFormat("Failed to find the position for PCF {0}", returnPCF.CFUID);
                    }
                });
                if (!returnResult.IsOk)
                {
                    Debug.LogError("Failed to GetPCF");
                    break;
                }
                suffix = "-" + count;
                count++;
            }

            _state = State.BindingComplete;
        }
Пример #16
0
 /// <summary>
 /// Saves the binding. The binding is checked for validity and also
 /// </summary>
 /// <param name="binding">Binding.</param>
 void InternalSave(MLContentBinding binding)
 {
     if (!binding.IsValid)
     {
         MLPluginLog.Error("MLPersistentStore.InternalSave failed. Reason: Binding is invalid");
         return;
     }
     if (_data != null)
     {
         // note: bindings are currently looked up by object references
         // because once created they are passed around as references
         // if we previously saved a binding
         if (!_data.Bindings.Contains(binding))
         {
             _data.Bindings.Add(binding);
         }
         _saveRequired = true;
     }
     if (_virtualIdToBindings != null && !_virtualIdToBindings.ContainsKey(binding.ObjectId))
     {
         _virtualIdToBindings.Add(binding.ObjectId, binding);
     }
 }
        /// <summary>
        /// Creates a binding between the virtual object and the specified PCF.
        /// </summary>
        /// <param name="virtualObjId">Virtual object identifier.</param>
        /// <param name="go">GameObject representing the virtual object. (Note: This is not serialized)</param>
        /// <param name="pcf">PCF to bind to</param>
        /// <returns>
        ///  MLContentBinding object with mapping between virtual object and PCF. Please note that
        /// just calling this function is not enough to persist the binding. Call MLPersistentStore.Save to persist this mapping.
        /// </returns>
        public static MLContentBinding BindToPCF(string virtualObjId, GameObject go, MLPCF pcf)
        {
            if (pcf == null || go == null)
            {
                MLPluginLog.Error("MLContentBindings.BindToPCF failed, either GameObject or PCF is null.");
                return(null);
            }
            if (pcf.CurrentResult != MLResult.Code.Ok)
            {
                MLPluginLog.Error("MLContentBindings.BindToPCF failed, invalid PCF.");
                return(null);
            }
            MLContentBinding newBinding = new MLContentBinding();

            newBinding.GameObject  = go;
            newBinding.ObjectId    = virtualObjId;
            newBinding.PCF         = pcf;
            newBinding.BindingType = MLContentBindingType.PCF;
            newBinding.Update();

            //MLPersistentCoordinateFrames.QueueForUpdates(newBinding.PCF);

            return(newBinding);
        }
Пример #18
0
 /// <summary>
 /// Handler for binding restore
 /// </summary>
 /// <param name="contentBinding">Content binding.</param>
 /// <param name="resultCode">Result code.</param>
 void HandleBindingRestore(MLContentBinding contentBinding, MLResult result)
 {
     if (!result.IsOk)
     {
         if (NumRetriesForRestore > 0)
         {
             NumRetriesForRestore--;
             Debug.LogWarningFormat("Failed to restore: {0} - {1}. Retries left: {2}. Result Code: {3}",
                                    gameObject.name, contentBinding.PCF.CFUID, NumRetriesForRestore, result);
             StartCoroutine(TryRestore());
         }
         else
         {
             Debug.LogErrorFormat("Failed to restore : {0} - {1}. Deleting Binding. Result code: {2}",
                                  gameObject.name, contentBinding.PCF.CFUID, result);
             MLPersistentStore.DeleteBinding(contentBinding);
             SetComplete(false);
         }
     }
     else
     {
         SetComplete(true);
     }
 }
Пример #19
0
 /// <summary>
 /// Load the Content binding for the specified virtualObjId
 /// </summary>
 /// <param name="virtualObjId">Virtual object identifier.</param>
 /// <param name="binding">Binding.</param>
 /// <returns>
 /// MLResult.Code will be MLResultCode.Ok if successful.
 ///
 /// MLResult.Code will be MLResultCode.InvalidParam if failed due to an invalid input parameter.
 /// </returns>
 public static MLResult Load(string virtualObjId, out MLContentBinding binding)
 {
     return(Instance.LoadInternal(virtualObjId, out binding));
 }
Пример #20
0
 /// <summary>
 /// Save the specified binding. Note : currently this function saves to the file system
 /// everytime you call it, so it can be slow.
 /// </summary>
 /// <param name="binding">Binding.</param>
 public static void Save(MLContentBinding binding)
 {
     Instance.InternalSave(binding);
 }
Пример #21
0
 /// <summary>
 /// Deletes the binding if it exists. otherwise it's a no op.
 /// </summary>
 /// <param name="binding">Binding.</param>
 public static void DeleteBinding(MLContentBinding binding)
 {
     Instance.InternalDeleteBinding(binding);
 }