// /*===============================================================================
// Copyright (C) 2020 PhantomsXR Ltd. All Rights Reserved.
//
// This file is part of the XR-MOD SDK.
//
// The XR-MOD SDK cannot be copied, distributed, or made available to
// third-parties for commercial purposes without written permission of PhantomsXR Ltd.
//
// Contact nswell@phantomsxr.com for licensing requests.
// ===============================================================================*/
using System;
using System.Collections.Generic;
using Phantom.XRMOD.ActionNotification.Runtime;
using Phantom.XRMOD.Core.Runtime;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Phantom.XRMOD.UnityFusion.Runtime.CodeHook
{
///
/// Defines the different phases of the binding process.
///
public enum BindingPhase
{
/// Adding the runtime script to the GameObject.
AddClass,
/// Setting public field values on the script.
SetValue,
/// Activating/enabling the script.
Activate,
/// Updating rendering state if necessary.
SetRenderer
}
///
/// Singleton manager that orchestrates the binding of MonoBehaviours across the scene.
/// High-level entry point for processing components.
///
public class BinderManager : MonoBehaviourSingleton
{
private BindingOrchestrator bindingOrchestrator;
private void Awake()
{
var tmp_BindingSteps = new List()
{
new AddRuntimeScriptStep(),
new SetValueStep(),
new ActivateStep(),
new RenderGameObjectStep()
};
bindingOrchestrator = new BindingOrchestrator(tmp_BindingSteps);
ActionNotificationCenter.DefaultCenter.AddObserver(DoBindingBatch,
nameof(ActionParameterDataType.ProcessMonoBinderBatch));
}
protected override void OnDestroy()
{
base.OnDestroy();
ActionNotificationCenter.DefaultCenter.RemoveObserver(
nameof(ActionParameterDataType.ProcessMonoBinderBatch), DoBindingBatch);
}
private void DoBindingBatch(BaseNotificationData _obj)
{
var tmp_MonoBinders =
Object.FindObjectsByType(FindObjectsInactive.Include, FindObjectsSortMode.InstanceID);
DoBind(tmp_MonoBinders);
}
///
/// Initiates binding for a list of MonoBinders.
///
/// The list of binders to process.
public void DoBind(List _monoBinders)
{
DoBind(_monoBinders.ToArray());
}
///
/// Initiates binding for an array of MonoBinders asynchronously.
///
/// The array of binders to process.
public async void DoBind(MonoBinder[] _monoBinders)
{
try
{
//var tmp_Reverse = _monoBinders.Reverse();
await bindingOrchestrator.BindScriptBatch(_monoBinders);
}
catch (Exception tmp_E)
{
Debug.LogException(tmp_E);
throw;
}
}
///
/// Initiates binding for a single MonoBinder asynchronously.
///
/// The binder to process.
public async void DoBind(MonoBinder _monoBinder)
{
try
{
await bindingOrchestrator.BindScript(_monoBinder);
}
catch (Exception tmp_E)
{
Debug.LogException(tmp_E);
throw;
}
}
///
/// Initiates binding for all components currently in the scene.
///
public async void DoBind()
{
try
{
var tmp_AllMonoBinders =
FindObjectsByType(FindObjectsInactive.Include, FindObjectsSortMode.None);
if (tmp_AllMonoBinders is {Length: > 0})
{
await bindingOrchestrator.BindScriptBatch(tmp_AllMonoBinders);
}
}
catch (Exception e)
{
Debug.LogException(e);
throw;
}
}
}
}