Exemplo n.º 1
0
            public static void GenerateWrapper(string saveTo, ILoader loader = null)
            {
                if (Utils.filters == null)
                {
                    Utils.filters = Configure.GetFilters();

                    configure = Configure.GetConfigureByTags(new List <string>()
                    {
                        "Puerts.BindingAttribute",
                        "Puerts.BlittableCopyAttribute",
                        "Puerts.TypingAttribute",
                    });

                    genTypes = configure["Puerts.BindingAttribute"].Select(kv => kv.Key)
                               .Where(o => o is Type)
                               .Cast <Type>()
                               .Where(t => !t.IsGenericTypeDefinition && !t.Name.StartsWith("<"))
                               .Distinct()
                               .ToList();
                }

                var blittableCopyTypes = new HashSet <Type>(configure["Puerts.BlittableCopyAttribute"].Select(kv => kv.Key)
                                                            .Where(o => o is Type)
                                                            .Cast <Type>()
                                                            .Where(t => !t.IsPrimitive && Utils.isBlittableType(t))
                                                            .Distinct());

                if (loader == null)
                {
                    loader = new DefaultLoader();
                }
                using (var jsEnv = new JsEnv(loader))
                {
                    var wrapRender = jsEnv.Eval <Func <Wrapper.TypeGenInfo, string> >("require('puerts/templates/wrapper.tpl.cjs')");

                    var typeGenInfos = new List <Wrapper.TypeGenInfo>();

                    Dictionary <string, bool> makeFileUniqueMap = new Dictionary <string, bool>();
                    foreach (var type in genTypes)
                    {
                        if (type.IsEnum || type.IsArray || (Generator.Utils.IsDelegate(type) && type != typeof(Delegate)))
                        {
                            continue;
                        }
                        Wrapper.TypeGenInfo typeGenInfo = Wrapper.TypeGenInfo.FromType(type, genTypes);
                        typeGenInfo.BlittableCopy = blittableCopyTypes.Contains(type);
                        typeGenInfos.Add(typeGenInfo);
                        string filePath = saveTo + typeGenInfo.WrapClassName + ".cs";

                        int uniqueId = 1;
                        while (makeFileUniqueMap.ContainsKey(filePath.ToLower()))
                        {
                            filePath = saveTo + typeGenInfo.WrapClassName + "_" + uniqueId + ".cs";
                            uniqueId++;
                        }
                        makeFileUniqueMap.Add(filePath.ToLower(), true);

                        string fileContext = wrapRender(typeGenInfo);
                        using (StreamWriter textWriter = new StreamWriter(filePath, false, Encoding.UTF8))
                        {
                            textWriter.Write(fileContext);
                            textWriter.Flush();
                        }
                    }

                    var autoRegisterRender = jsEnv.Eval <Func <Wrapper.TypeGenInfo[], string> >("require('puerts/templates/wrapper-reg.tpl.cjs')");
                    using (StreamWriter textWriter = new StreamWriter(saveTo + "AutoStaticCodeRegister.cs", false, Encoding.UTF8))
                    {
                        string fileContext = autoRegisterRender(typeGenInfos.ToArray());
                        textWriter.Write(fileContext);
                        textWriter.Flush();
                    }
                }
            }
Exemplo n.º 2
0
        public static void GenerateCode(string saveTo, bool tsOnly = false)
        {
            filters = Configure.GetFilters();
            var configure = Configure.GetConfigureByTags(new List <string>()
            {
                "Puerts.BindingAttribute",
                "Puerts.BlittableCopyAttribute",
                "Puerts.TypingAttribute",
            });

            var genTypes = configure["Puerts.BindingAttribute"].Select(kv => kv.Key)
                           .Where(o => o is Type)
                           .Cast <Type>()
                           .Where(t => !t.IsGenericTypeDefinition);

            var blittableCopyTypes = new HashSet <Type>(configure["Puerts.BlittableCopyAttribute"].Select(kv => kv.Key)
                                                        .Where(o => o is Type)
                                                        .Cast <Type>()
                                                        .Where(t => t.IsValueType && !t.IsPrimitive)
                                                        .Distinct());

            var tsTypes = configure["Puerts.TypingAttribute"].Select(kv => kv.Key)
                          .Where(o => o is Type)
                          .Cast <Type>()
                          .Where(t => !t.IsGenericTypeDefinition)
                          .Concat(genTypes)
                          .Distinct();

            using (var jsEnv = new JsEnv())
            {
                var templateGetter = jsEnv.Eval <Func <string, Func <object, string> > >("require('puerts/gencode/main.js')");
                var wrapRender     = templateGetter("type.tpl");

                if (!tsOnly)
                {
                    var typeGenInfos = new List <TypeGenInfo>();
                    foreach (var type in genTypes)
                    {
                        if (type.IsEnum || type.IsArray || (IsDelegate(type) && type != typeof(Delegate)))
                        {
                            continue;
                        }
                        TypeGenInfo typeGenInfo = ToTypeGenInfo(type);
                        typeGenInfo.BlittableCopy = blittableCopyTypes.Contains(type);
                        typeGenInfos.Add(typeGenInfo);
                        string filePath    = saveTo + typeGenInfo.WrapClassName + ".cs";
                        string fileContext = wrapRender(typeGenInfo);
                        using (StreamWriter textWriter = new StreamWriter(filePath, false, Encoding.UTF8))
                        {
                            textWriter.Write(fileContext);
                            textWriter.Flush();
                        }
                    }

                    var autoRegisterRender = templateGetter("autoreg.tpl");
                    using (StreamWriter textWriter = new StreamWriter(saveTo + "AutoStaticCodeRegister.cs", false, Encoding.UTF8))
                    {
                        string fileContext = autoRegisterRender(typeGenInfos.ToArray());
                        textWriter.Write(fileContext);
                        textWriter.Flush();
                    }
                }

                var typingRender = templateGetter("typing.tpl");
                using (StreamWriter textWriter = new StreamWriter(saveTo + "Typing/csharp/index.d.ts", false, Encoding.UTF8))
                {
                    string fileContext = typingRender(ToTypingGenInfo(tsTypes));
                    textWriter.Write(fileContext);
                    textWriter.Flush();
                }
            }
        }
Exemplo n.º 3
0
        public async Task <XException> Start()
        {
            if (m_Inited)
            {
                return(null);
            }
            m_Config = XConfig.GetConfig <TSConfig>(TSConst.ConfigPath_Resources);
            if (m_Config == null)
            {
                return(new XException($"[{TSConst.ServiceName}] Connot found config file."));
            }

            if (!m_Config.Enable)
            {
                return(null);
            }

            //框架内部Js文件加载路径
            m_Internal_Js_Folder_Load_Path = m_Config.FrameworkInternalJsFolderLoadPath;
            if (!m_Internal_Js_Folder_Load_Path.IsNullOrEmpty())
            {
                if (m_Internal_Js_Folder_Load_Path.EndsWith("/"))
                {
                    m_Internal_Js_Folder_Load_Path = m_Internal_Js_Folder_Load_Path.Substring(0, m_Internal_Js_Folder_Load_Path.Length - 1);
                }
                m_Internal_Js_Folder_Load_Path_withSlash = m_Internal_Js_Folder_Load_Path + "/";
            }
            //Js文件加载名称
            m_JsExtension = m_Config.JsFileExtensionName;
            if (!m_JsExtension.StartsWith("."))
            {
                m_JsExtension = "." + m_JsExtension;
            }

            if (m_Assets == null)
            {
                return(new XException($"[{TSConst.ServiceName}]" + (m_Core.IsCmnHans() ? "没有任何服务实现了Framework中的内置的资产加载接口" : "No service implements the built-in asset loading interface in Framework")));
            }

            //初始化Js的运行环境
            m_JsEnv = new JsEnv(new XTSLoader(m_Assets, c_InternalJsSign, c_Internal_Js_Extension, m_Internal_Js_Folder_Load_Path, m_JsExtension));

            //try
            //{
            //    await InitInternalEntry();
            //}
            //catch(XException e)
            //{
            //    return e;
            //}

            //准备好入口文件
            if (!m_Config.EntryJsFileLoadPath.IsNullOrEmpty())
            {
                try
                {
                    TextAsset entry_ta = await m_Assets.LoadAsync <TextAsset>(m_Config.EntryJsFileLoadPath);

                    m_EntryCodeContent = entry_ta.text;
                    //m_EntryFunc = m_JsEnv.LoadString<LuaFunction>(entry_ta.bytes, m_Config.EntryLuaFileLoadPath);
                    m_Assets.Release(entry_ta);
                }
                catch (XException e)
                {
                    return(e);
                }
            }

            if (m_UpdateTicket != null)
            {
                m_UpdateTicket.Unregister();
            }
            m_UpdateTicket = TimeMachine.RegisterUpdate(OnUpdate);

            m_Inited = true;
            return(null);
        }