Beispiel #1
0
        public void ArgsManagerTestSimplePasses()
        {
            string sArg = "--name = Eriri --full-name = \"Sawamura Spencer Eriri\" --enable --CV= \"Oonishi Saori\"";

            string[] args = ArgsUtil.ParseArgsText(sArg).ToArray();
            var      mgr  = new ArgsManager();

            mgr.AddArgs(args);

            Assert.AreEqual(mgr.GetValue("name"), "Eriri");
            Assert.AreEqual(mgr.GetValue("full-name"), "Sawamura Spencer Eriri");
            Assert.IsTrue(mgr.GetBool("enable"));
            Assert.AreEqual(mgr.GetValue("CV"), "Oonishi Saori");
        }
Beispiel #2
0
        public async Task RunAsync()
        {
            if (m_Inited)
            {
                return;
            }

            Debug.Log("[TinaX Framework] TinaX - v." + FrameworkVersionName + "    | Nekonya Studio\nhttps://github.com/yomunsam/tinax\nCorala.Space Project \n Powerd by yomunsam: https://yomunchan.moe | https://github.com/yomunsam");

            //Command Line Args
            #region Command Line Args
            string[] Args = this.GetCommandLineArgs();
            m_ArgsManager.AddArgs(Args);
            m_ServiceContainer.CatApplication.Instance <ICommandLineArgs>(m_ArgsManager);    //将Args接口注册进依赖注入容器

            //编辑器下,使用Debug命令行配置覆盖真实参数
#if UNITY_EDITOR
            string debug_args_str = this.GetDebugCommandLineArgs();
            if (!debug_args_str.IsNullOrEmpty())
            {
                string[] debug_args = ArgsUtil.ParseArgsText(debug_args_str).ToArray();
                m_ArgsManager.AddArgs(debug_args);
            }
#endif

            #endregion

            //内置统一配置接口
            #region Configuration
            m_ConfigurationBuilder = new ConfigurationBuilder();
            m_ConfigurationBuilder.Add(new CommandLineConfigurationSource(m_ArgsManager)); //CommandLine | 命令行配置源

            m_ServiceContainer.Instance <IConfigurationBuilder>(m_ConfigurationBuilder);   //注册Builder到依赖注入容器
            #endregion

            //在Scene创建一个全局的base gameobject
            //TODO: 如果在ECS模式,应该是不需要这么个东西的
            BaseGameObject = GameObjectHelper.FindOrCreateGameObject(FrameworkConst.Frameowrk_Base_GameObject_Name)
                             .DontKillMe()
                             .SetPosition(new Vector3(-6000, -6000, -6000));

            //Profile
            var profile = XConfig.GetConfig <TinaX.Internal.XProfileConfig>(FrameworkConst.XProfile_Config_Path);
            if (profile != null)
            {
                this.ProfileName = profile.ActiveProfileName;
                this.DevelopMode = profile.DevelopMode;
            }


            //对Service进行排序
            int getServiceProviderOrder(ref IXServiceProvider provider)
            {
                Type p_type = provider.GetType();
                var  attr   = p_type.GetCustomAttribute <XServiceProviderOrderAttribute>();

                if (attr != null)
                {
                    return(attr.Order);
                }
                else
                {
                    return(100);
                }
            }

            m_XServiceProvidersList.Sort((x, y) => getServiceProviderOrder(ref x).CompareTo(getServiceProviderOrder(ref y)));

            //------------------触发Init阶段--------------------------------------------------------------
            //IXBootstrap获取启动引导
            var _b_type = typeof(IXBootstrap);
            var types   = AppDomain.CurrentDomain.GetAssemblies()
                          .SelectMany(a => a.GetTypes().Where(t => t.GetInterfaces().Contains(_b_type)))
                          .ToArray();
            foreach (var type in types)
            {
                m_XBootstrapList.Add((IXBootstrap)Activator.CreateInstance(type));
            }



            //Invoke Services "Init"
            Task <XException>[] arr_init_task = new Task <XException> [m_XServiceProvidersList.Count];
            for (int i = 0; i < m_XServiceProvidersList.Count; i++)
            {
                Debug.Log("    [XService Init]:" + m_XServiceProvidersList[i].ServiceName);
                arr_init_task[i] = m_XServiceProvidersList[i].OnInit(this);
            }
            await Task.WhenAll(arr_init_task);

            for (int i = 0; i < m_XServiceProvidersList.Count; i++)
            {
                var exception = arr_init_task[i].Result;
                if (exception != null)
                {
                    if (m_ServicesInitExceptionAction != null && m_ServicesInitExceptionAction.GetInvocationList().Length > 0)
                    {
                        m_ServicesInitExceptionAction?.Invoke(m_XServiceProvidersList[i].ServiceName, exception);
                    }
                    else
                    {
                        Debug.LogError($"[TinaX.Core] Exception when init xserver \"{m_XServiceProvidersList[i].ServiceName}\"");
                        throw exception;
                    }
                }
            }

            //试试看下面这个和上面这个实际跑起来哪个快
            //foreach (var provider in mList_XServiceProviders)
            //{
            //    Debug.Log("    [XService Init]:" + provider.ServiceName);
            //    var b = await provider.OnInit();
            //    if (!b)
            //    {
            //        var e = provider.GetInitException();
            //        if (mServicesInitExceptionAction != null)
            //            mServicesInitExceptionAction.Invoke(provider.ServiceName, e);
            //        else
            //            throw e;

            //    }
            //}



            //----------------------------------------------------------------------------------------------

            //Invoke Service "Register"
            foreach (var provider in m_XServiceProvidersList)
            {
                provider.OnServiceRegister(this);
            }

            //--------------------------------------------------------------------------------------------------
            //Invoke IXBootstrap "Init"
            foreach (var item in m_XBootstrapList)
            {
                item.OnInit(this);
            }

            //------------------触发Start阶段----------------------------------------------------------------

            //Build ConfigurationBuilder
            #region Build ConfigurationBuilder
            var configuration = await m_ConfigurationBuilder?.BuildAsync();

            //Remove builder from DI
            m_ServiceContainer.CatApplication.Release <IConfigurationBuilder>();
            //Register ConfigurationRoot to DI
            m_ServiceContainer.Instance <IConfiguration>(configuration);
            #endregion

            //Invoke Services "Start"
            foreach (var p in m_XServiceProvidersList)
            {
                Debug.Log("    [XService Start]:" + p.ServiceName);
                var exception = await p.OnStart(this);

                if (exception != null)
                {
                    if (exception == null)
                    {
                        m_ServicesInitExceptionAction?.Invoke(p.ServiceName, null);
                    }
                    else
                    {
                        m_ServicesStartExceptionAction?.Invoke(p.ServiceName, exception);
#if UNITY_EDITOR
                        Debug.LogError($"Serivce provider \"{p.ServiceName}\" start exception: " + exception.Message);
#endif
                    }
                }
            }

            //------------------------------------------------------------------------------------------------

            Debug.Log("[TinaX] Framework startup finish.");
            IsRunning = true;
            m_Inited  = true;

            //Invoke XBootstrap "Start"
            foreach (var xbs in m_XBootstrapList)
            {
                xbs.OnStart(this);
            }

            Debug.Log("[TinaX] App startup finish.");
        }