using System;
using System.Collections.Generic;
using UnityEngine;
namespace Rokid.UXR.Module
{
///
/// Manage the initialization of the Android module
///
public class ModuleManager : MonoSingleton
{
public class ModuleInfo
{
public string moduleName;
public AndroidJavaObject module;
public bool init;
}
///
/// This module can be initialized directly.
///
///
///
///
[SerializeField]
private Dictionary modules = new Dictionary();
///
/// This module needs to be initialized after the Slam initialization is completed.
///
///
///
///
[SerializeField]
private Dictionary afterSlamInitModuels = new Dictionary();
///
/// Is Slam initialization completed?
///
private bool slamInit = false;
private Action OnSlamInit;
private Action OnAfterSlamInit;
public void Initialze()
{
//TODO nothing
}
protected override void OnSingletonInit()
{
OnSlamInit += onSlamInit;
DontDestroyOnLoad(this.gameObject);
}
protected override void OnDestroy()
{
base.OnDestroy();
OnSlamInit -= onSlamInit;
//清空事件...
OnSlamInit = null;
}
private void onSlamInit()
{
foreach (var module in afterSlamInitModuels.Values)
{
if (!module.init)
{
RKLog.Info("====ModuleManager==== new moduleInfo: " + module.moduleName);
module.module = new AndroidJavaObject(module.moduleName);
module.init = true;
}
}
}
void Update()
{
if (slamInit == false)
{
//TODO Slam Init...
slamInit = true;
RKLog.Info("====ModuleManager==== Slam Inited !!!");
OnSlamInit?.Invoke();
OnAfterSlamInit?.Invoke();
}
}
///
/// Retrieve the initialized module.
///
///
///
public AndroidJavaObject GetModule(string moduleName)
{
ModuleInfo moduleInfo;
if (modules.TryGetValue(moduleName, out moduleInfo))
{
if (moduleInfo.init)
{
RKLog.Info($"====ModuleManager==== GetModule In modules {moduleName} ");
return moduleInfo.module;
}
}
if (afterSlamInitModuels.TryGetValue(moduleName, out moduleInfo))
{
if (moduleInfo.init)
{
RKLog.Info($"====ModuleManager==== GetModule In afterSlamInitModuels {moduleName} ");
return moduleInfo.module;
}
}
RKLog.Info($"====ModuleManager==== 模块未注册请注册模块 {moduleName} ");
return null;
}
///
/// Register the module.
///
///
///
public void RegistModule(string moduleName, bool afterSlamInit = false, Action registCallBack = null)
{
if (afterSlamInit)
{
if (!afterSlamInitModuels.ContainsKey(moduleName) && !modules.ContainsKey(moduleName))
{
if (slamInit)
{
RKLog.Info("====ModuleManager==== SalmInit 注册模块");
afterSlamInitModuels.Add(moduleName, new ModuleInfo()
{
moduleName = moduleName,
module = new AndroidJavaObject(moduleName),
init = true
});
registCallBack?.Invoke();
}
else
{
RKLog.Info($"====ModuleManager==== 添加到afterSlamInitModules集合中 {moduleName}");
afterSlamInitModuels.Add(moduleName, new ModuleInfo()
{
moduleName = moduleName,
init = false
});
OnAfterSlamInit += registCallBack;
}
}
else
{
RKLog.Info($"====ModuleManager==== afterSlamInit 该模块已经注册 {moduleName}");
ModuleInfo info = null;
if (afterSlamInitModuels.TryGetValue(moduleName, out info))
{
if (info.init)
{
registCallBack?.Invoke();
}
else
{
OnAfterSlamInit += registCallBack;
}
}
}
}
else
{
if (!modules.ContainsKey(moduleName) && !afterSlamInitModuels.ContainsKey(moduleName))
{
modules.Add(moduleName, new ModuleInfo()
{
moduleName = moduleName,
module = new AndroidJavaObject(moduleName),
init = true
});
}
else
{
RKLog.Info($"====ModuleManager==== 该模块已经注册 {moduleName}");
}
registCallBack?.Invoke();
}
}
}
}