Beispiel #1
0
    public static void OnBegin()
    {
        GeneratorHelp.ClearTypeInfo();

        //读取JSBCodeGenSetting下所有CsExportedMethodAttribute属性
        var eportedMethodAtrs = typeof(JSGenerator).Assembly.GetCustomAttributes(typeof(CsExportedMethodAttribute),
                                                                                 false);

        CsExportedMethodDic = new Dictionary <Type, Dictionary <string, CsExportedMethodAttribute> >();
        foreach (var obj in eportedMethodAtrs)
        {
            var methodAttribute = obj as CsExportedMethodAttribute;
            if (!CsExportedMethodDic.ContainsKey(methodAttribute.TargetType))
            {
                CsExportedMethodDic[methodAttribute.TargetType] = new Dictionary <string, CsExportedMethodAttribute>();
            }
            CsExportedMethodDic[methodAttribute.TargetType].Add(methodAttribute.TargetMethodName, methodAttribute);
        }

        _streamWriter = OpenFile(JSPathSettings.csExportJsFile);
        _streamWriter.Write(@"
if (typeof(JsTypes) == ""undefined"")
    var JsTypes = [];

//this.Enum = {};
");
    }
Beispiel #2
0
    //static string enumFile = JSBindingSettings.jsGeneratedDir + "/enum" + JSBindingSettings.jsExtension;
    //static string tempFile = JSBindingSettings.jsDir + "/temp"+JSBindingSettings.jsExtension;

    public static void OnBegin()
    {
        GeneratorHelp.ClearTypeInfo();

//        if (Directory.Exists(JSBindingSettings.jsGeneratedDir))
//        {
//            // delete all last generated files
//            string[] files = Directory.GetFiles(JSBindingSettings.jsGeneratedDir);
//            for (int i = 0; i < files.Length; i++)
//            {
//                File.Delete(files[i]);
//            }
//        }
//        else
//        {
//            // create directory
//            Directory.CreateDirectory(JSBindingSettings.jsGeneratedDir);
//        }

        // clear generated enum files
        W = OpenFile(JSBindingSettings.jsGenFiles, false);
    }
        public static void GenWraps(Type[] arrClasses, ISet <string> blackList, Func <Type, bool> filter)
        {
            GeneratorHelp.ClearTypeInfo();

            Dictionary <Type, TypeStatus> dict = new Dictionary <Type, TypeStatus>();
            Action <Type> onNewType            = null;

            onNewType = (nt) =>
            {
                while (true)
                {
                    if (nt.IsByRef || nt.IsArray)
                    {
                        nt = nt.GetElementType();
                        continue;
                    }
                    if (nt.IsGenericType && !nt.IsGenericTypeDefinition)
                    {
                        foreach (var ga in nt.GetGenericArguments())
                        {
                            onNewType(ga);
                        }

                        nt = nt.GetGenericTypeDefinition();
                        continue;
                    }
                    if (nt.IsGenericParameter)
                    {
                        return;
                    }
                    break;
                }

                if (!blackList.Contains(nt.FullName) && filter(nt) &&
                    !dict.ContainsKey(nt) &&
                    (nt.Namespace == null || !nt.FullName.StartsWith("System.")))
                {
                    if (nt.DeclaringType != null)
                    {
                        if (!blackList.Contains(nt.DeclaringType.FullName))
                        {
                            dict.Add(nt, new TypeStatus());
                        }
                    }
                    else
                    {
                        dict.Add(nt, new TypeStatus());
                    }
                }
            };

            Func <Type, TypeStatus> getParent = (type) =>
            {
                if (dict.ContainsKey(type))
                {
                    return(dict[type]);
                }
                return(null);
            };

            foreach (var type in arrClasses)
            {
                onNewType(type);
            }

            while (true)
            {
                Type[] keys = new Type[dict.Count];
                dict.Keys.CopyTo(keys, 0);

                foreach (Type type in keys)
                {
                    TypeStatus ts = dict[type];
                    if (ts.status != TypeStatus.Status.Wait)
                    {
                        continue;
                    }

                    if (ShouldIgnoreType(type))
                    {
                        ts.status = TypeStatus.Status.Ignored;
                        continue;
                    }

                    if (type.IsEnum)
                    {
                        GenEnum(type, ts, getParent, onNewType);
                    }
                    else if (typeof(Delegate).IsAssignableFrom(type))
                    {
                        GenDelegate(type, ts, getParent, onNewType);
                    }
                    else
                    {
                        GenInterfaceOrStructOrClass(type, ts, getParent, onNewType);
                    }
                }

                bool bContinue = false;
                foreach (var kv in dict)
                {
                    if (kv.Value.status == TypeStatus.Status.Wait)
                    {
                        bContinue = true;
                        break;
                    }
                }

                if (!bContinue)
                {
                    break;
                }
            }

            TextFile tfAll = new TextFile();

            tfAll.Add("#if UNITY_WAGAME");
            tfAll.Add("using Bridge;");
            tfAll.Add("using System;");
            foreach (var kv in dict)
            {
                if (kv.Value.status == TypeStatus.Status.Exported &&
                    !kv.Value.IsInnerType)
                {
                    tfAll.Add(kv.Value.tf.Ch);
                    tfAll.AddLine();
                }
            }
            tfAll.Add("#endif");
            File.WriteAllText(JSBindingSettings.CswFilePath, tfAll.Format(-1));
        }