Example #1
0
        /** 执行一个Adapater */
        private static void doOneClass(ILClassInfo clsInfo, String path, bool isShine, StringBuilder writer)
        {
            string str = _templete;

            if (isShine)
            {
                str = str.Replace("$namespace", "namespace ShineEngine" + Enter + "{");
                str = str.Replace("$_namespace", "}");
            }
            else
            {
                str = str.Replace("$namespace", "using ShineEngine;" + Enter);
                str = str.Replace("$_namespace", "");
            }

            str = str.Replace("$clsName", clsInfo.clsName);

            StringBuilder sb1   = StringBuilderPool.create();
            StringBuilder sb2   = StringBuilderPool.create();
            IntSet        marks = new IntSet();

            for (int i = 0, len = clsInfo.methods.size(); i < len; ++i)
            {
                doOneMethod(clsInfo.methods.get(i), i, sb1, sb2, marks);
            }

            str = str.Replace("$1", StringBuilderPool.releaseStr(sb1));
            str = str.Replace("$2", StringBuilderPool.releaseStr(sb2));

            FileUtils.writeFileForUTF(path + "/" + clsInfo.clsName + "Adapter.cs", str);

            writer.Append("appdomain.RegisterCrossBindingAdaptor(new " + clsInfo.clsName + "Adapter());");
            endClsLine(writer);
        }
Example #2
0
        private static void doOneGenerate(XML xml, string controlClsPath, string adapterOutPath, int projectType)
        {
            clearGenerate();

            string[] fileList = FileUtils.getFileList(adapterOutPath, "cs");

            SMap <string, string> fileMap = new SMap <string, string>();

            foreach (string s in fileList)
            {
                string fN = FileUtils.getFileNameWithoutEx(s);

                if (fN.EndsWith("Adapter"))
                {
                    fileMap.put(fN.Substring(0, fN.Length - 7), s);
                }
            }

            _isShine = projectType == 0;

            int aLen = "Assets/".Length;

            //先处理包路径
            foreach (XML xl in xml.getChildrenByName("root"))
            {
                SList <string> ignoreList = new SList <string>();

                foreach (XML pXml in xl.getChildrenByName("ignorePath"))
                {
                    ignoreList.add(pXml.getProperty("path"));
                }

                StringIntMap hotfixPathDic = new StringIntMap();

                foreach (XML pXml in xl.getChildrenByName("hotfixPath"))
                {
                    hotfixPathDic.put(pXml.getProperty("path"), pXml.getProperty("needFactory").Equals("true") ? 1 : 0);
                }

                string rootPath = "Assets/" + xl.getProperty("path");

                string[] deepFileList = FileUtils.getDeepFileList(rootPath, "cs");

                foreach (string s in deepFileList)
                {
                    //编辑器类的跳过
                    if (s.Contains("/Editor/"))
                    {
                        continue;
                    }

                    string tailPath = s.Substring(rootPath.Length + 1);

                    bool failed = false;

                    //排除组
                    foreach (string ig in ignoreList)
                    {
                        if (tailPath.StartsWith(ig))
                        {
                            failed = true;
                            break;
                        }
                    }

                    if (failed)
                    {
                        continue;
                    }

                    string fQName = FileUtils.getFileFrontName(s.Substring(aLen));

                    String fName = FileUtils.getFileName(fQName);

                    _clsNameToPathDic.put(fName, fQName);

                    string[] keys   = hotfixPathDic.getKeys();
                    int[]    values = hotfixPathDic.getValues();
                    string   k;
                    int      v;

                    for (int i = keys.Length - 1; i >= 0; --i)
                    {
                        if ((k = keys[i]) != null)
                        {
                            v = values[i];

                            //需要
                            if (tailPath.StartsWith(k))
                            {
                                _adapterClsDic.add(fName);

                                if (v == 1)
                                {
                                    _factoryClsDic.add(fName);
                                }

                                break;
                            }
                        }
                    }
                }
            }


            string clsStr = FileUtils.readFileForUTF(controlClsPath);

            string projectMark    = getProjectMark(projectType);
            string factoryClsPath = getFactoryClsPath(projectType);

            ClsAnalysis.FactoryClassInfo factoryClass = null;
            string factoryClsName = null;

            if (!_isShine)
            {
                factoryClass   = ClsAnalysis.readFactory(factoryClsPath);
                factoryClsName = FileUtils.getFileFrontName(FileUtils.getFileName(factoryClsPath));

                //遍历父
                for (int i = projectType - 1; i >= Project_Common; i--)
                {
                    ClsAnalysis.FactoryClassInfo parentFactoryCls = ClsAnalysis.readFactory(getFactoryClsPath(i));

                    factoryClass.addParent(parentFactoryCls);
                }
            }

            StringBuilder sb = new StringBuilder();

            endClsLine(sb);

            if (!_isShine)
            {
                sb.Append("base.initOtherAdapters(appdomain);");
                endClsLine(sb);
            }


            TypeInfo factoryTypeInfo = null;

            if (!_isShine)
            {
                //工厂最后执行
                factoryTypeInfo = getTypeInfo(factoryClsName);

                _clsNameToPathDic.remove(factoryClsName);
            }

            //ex
            foreach (XML xl in xml.getChildrenByName("customClass"))
            {
                ILClassInfo cls = new ILClassInfo(xl.getProperty("name"));

                foreach (XML mXml in xl.getChildrenByName("method"))
                {
                    string visitTypeStr = mXml.getProperty("visitType");
                    int    visitType    = visitTypeStr.isEmpty() ? VisitType.Protected : VisitType.getTypeByName(visitTypeStr);

                    string needBaseCallStr = mXml.getProperty("needBaseCall");
                    bool   needBaseCall    = needBaseCallStr.isEmpty() ? true : StringUtils.strToBoolean(needBaseCallStr);

                    string   argsStr = mXml.getProperty("args");
                    string[] args    = argsStr.isEmpty() ? ObjectUtils.EmptyStringArr : argsStr.Split(',');

                    cls.addMethod(mXml.getProperty("name"), mXml.getProperty("returnType"), visitType, needBaseCall, args);
                }

                doOneClass(cls, adapterOutPath, _isShine, sb);
                fileMap.remove(cls.clsName);
            }

            foreach (string k in _clsNameToPathDic.getSortedMapKeys())
            {
                TypeInfo typeInfo = getTypeInfo(k);

                if (typeInfo == null)
                {
                    if (!_isShine)
                    {
                        Ctrl.throwError("不该找不到类型", k);
                    }
                }
                else
                {
                    if (typeInfo.hasHotfixMark || !typeInfo.isEmpty())
                    {
                        //需要工厂,并且不是虚类
                        if (!_isShine && !typeInfo.iCls.isAbstract && (typeInfo.needFactory || _factoryClsDic.contains(k)))
                        {
                            ClsAnalysis.FMethod method = factoryClass.addMethod(k, projectMark);
                            //补方法
                            factoryTypeInfo.iCls.addMethod("create" + method.name, method.returnType, VisitType.Public, true);
                        }

                        //需要适配器
                        if (typeInfo.hasHotfixMark || _adapterClsDic.contains(k))
                        {
                            doOneClass(typeInfo.iCls, adapterOutPath, _isShine, sb);
                            fileMap.remove(typeInfo.clsName);
                        }
                    }
                }
            }

            if (!_isShine)
            {
                doOneClass(factoryTypeInfo.iCls, adapterOutPath, _isShine, sb);
                fileMap.remove(factoryTypeInfo.clsName);
            }

            //去掉最后一个tab
            sb.Remove(sb.Length - 1, 1);

            clsStr = replaceMethod(clsStr, "protected " + (_isShine ? "virtual" : "override") + " void initOtherAdapters(AppDomain appdomain)", sb.ToString());

            if (!_isShine)
            {
                factoryClass.writeToPath(factoryClsPath);
            }

            FileUtils.writeFileForUTF(controlClsPath, clsStr);

            if (!fileMap.isEmpty())
            {
                fileMap.forEach((k, v) =>
                {
                    FileUtils.deleteFile(v);
                });
            }
        }
Example #3
0
            public void init()
            {
                if (_inited)
                {
                    return;
                }

                _inited = true;

                iCls = new ILClassInfo(clsName);

                Type baseType = clsType.BaseType;

                if (baseType != null)
                {
                    baseTypeInfo = getTypeInfo(baseType.Name);

                    if (baseTypeInfo != null)
                    {
                        // //类型居然不匹配
                        // if(bInfo.clsType!=baseType)
                        // {
                        //     Ctrl.throwError("类型居然不匹配",baseType.Name);
                        //     return;
                        // }

                        if (baseTypeInfo.needHotfixChildren)
                        {
                            //从基类传递
                            hasHotfixMark = baseTypeInfo.hasHotfixMark;
                            needFactory   = baseTypeInfo.needFactory;
                        }

                        //添加基类进来
                        foreach (ILMethodInfo ilMethodInfo in baseTypeInfo.iCls.methods)
                        {
                            if (ilMethodInfo.isAbstract)
                            {
                                doAddMethod(ilMethodInfo.clone());
                            }
                            else
                            {
                                doAddMethod(ilMethodInfo);
                            }
                        }
                    }
                }

                //接口和枚举除外
                if (!clsType.IsInterface && !clsType.IsEnum)
                {
                    //是否虚类
                    iCls.isAbstract = clsType.IsAbstract;

                    Attribute customAttribute = Attribute.GetCustomAttribute(clsType, typeof(Hotfix), false);

                    if (customAttribute != null)
                    {
                        Hotfix hotfixAttribute = (Hotfix)customAttribute;
                        hasHotfixMark      = true;
                        needFactory        = hotfixAttribute.needFactory;
                        needHotfixChildren = hotfixAttribute.needChildren;
                    }

                    MethodInfo[] methodInfos = clsType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

                    foreach (MethodInfo methodInfo in methodInfos)
                    {
                        //自己声明的
                        if (!methodInfo.IsPrivate && !methodInfo.IsStatic && !methodInfo.IsFinal && (methodInfo.IsVirtual || methodInfo.IsAbstract))
                        {
                            switch (methodInfo.Name)
                            {
                            case "Equals":
                            case "GetHashCode":
                            case "ToString":
                            case "Finalize":
                            {
                            }
                            break;

                            default:
                            {
                                ILMethodInfo iMethod = new ILMethodInfo();
                                iMethod.name         = methodInfo.Name;
                                iMethod.visitType    = methodInfo.IsPublic ? VisitType.Public : VisitType.Protected;
                                iMethod.isAbstract   = methodInfo.IsAbstract;
                                iMethod.needBaseCall = !methodInfo.IsAbstract;  //虚函数不需要
                                iMethod.returnType   = getTypeName(methodInfo.ReturnType);

                                foreach (ParameterInfo parameterInfo in methodInfo.GetParameters())
                                {
                                    MethodArgInfo iArg = new MethodArgInfo();
                                    iArg.name  = parameterInfo.Name;
                                    iArg.type  = getTypeName(parameterInfo.ParameterType);
                                    iArg.isIn  = parameterInfo.IsIn;
                                    iArg.isOut = parameterInfo.IsOut;
                                    // iArg.isRef=parameterInfo.ParameterType.Name.EndsWith("&");
                                    iArg.isRef = parameterInfo.IsRetval;

                                    // if(parameterInfo.IsRetval)
                                    // {
                                    //     Ctrl.print("A1",methodInfo.Name,parameterInfo.Name);
                                    // }

                                    // iArg.autoLength=parameterInfo.IsOptional;
                                    iArg.defaultValue = parameterInfo.DefaultValue != null?parameterInfo.DefaultValue.ToString() : "";

                                    iMethod.args.add(iArg);
                                }

                                //TODO:还是采用clone的方式,来对needBaseCall赋值

                                if (methodInfo.DeclaringType == clsType)
                                {
                                    doAddMethod(iMethod);
                                }
                                else
                                {
                                    if (methodInfo.IsAbstract)
                                    {
                                        iCls.methodKeys.get(iMethod.getKey()).needBaseCall = false;
                                    }
                                }
                                break;
                            }
                            }
                        }
                    }
                }
            }
Example #4
0
        public void initSAdapters(SList <ILClassInfo> list)
        {
            ILClassInfo cls;

            list.add(cls = new ILClassInfo("MonoBehaviour"));
            cls.addMethod("Start", null, VisitType.Private, false);
            cls.addMethod("Update", null, VisitType.Private, false);
            cls.addMethod("FixedUpdate", null, VisitType.Private, false);
            cls.addMethod("OnGUI", null, VisitType.Private, false);
            cls.addMethod("OnDestroy", null, VisitType.Private, false);

            list.add(cls = new ILClassInfo("BaseData"));
            cls.addMethod("toReadBytesFull", VisitType.Protected, "BytesReadStream", "stream");
            cls.addMethod("toWriteBytesFull", VisitType.Protected, "BytesWriteStream", "stream");
            cls.addMethod("toReadBytesSimple", VisitType.Protected, "BytesReadStream", "stream");
            cls.addMethod("toWriteBytesSimple", VisitType.Protected, "BytesWriteStream", "stream");
            cls.addMethod("toCopy", VisitType.Protected, "BaseData", "data");
            cls.addMethod("toShadowCopy", VisitType.Protected, "BaseData", "data");
            cls.addMethod("toDataEquals", "bool", VisitType.Protected, true, "BaseData", "data");
            cls.addMethod("getDataClassName", "string", VisitType.Public, true);
            cls.addMethod("toWriteDataString", VisitType.Protected, "DataWriter", "writer");
            cls.addMethod("initDefault", VisitType.Public);
            cls.addMethod("beforeWrite", VisitType.Protected);
            cls.addMethod("afterRead", VisitType.Protected);

            list.add(cls = new ILClassInfo("BaseRequest"));
            cls.addMethod("copyData", VisitType.Protected);
            cls.addMethod("toWriteBytesSimple", VisitType.Protected, "BytesWriteStream", "stream");
            cls.addMethod("doWriteToStream", VisitType.Protected, "BytesWriteStream", "stream");
            cls.addMethod("doWriteBytesSimple", VisitType.Protected, "BytesWriteStream", "stream");


            list.add(cls = new ILClassInfo("BaseResponse"));
            cls.addMethod("toReadBytesSimple", VisitType.Protected, "BytesReadStream", "stream");
            cls.addMethod("readFromStream", "BaseResponse", VisitType.Public, true, "BytesReadStream", "stream");
            cls.addMethod("preExecute", VisitType.Protected);
            cls.addMethod("execute", null, VisitType.Protected, false);

            list.add(cls = new ILClassInfo("BytesHttpRequest"));
            cls.addMethod("copyData", VisitType.Protected);
            cls.addMethod("toWriteBytesSimple", VisitType.Protected, "BytesWriteStream", "stream");
            cls.addMethod("toRead", null, VisitType.Protected, false);
            cls.addMethod("onComplete", null, VisitType.Protected, false);

            list.add(new ILClassInfo("DataMaker"));

            list.add(cls = new ILClassInfo("DebugControl"));
            cls.addMethod("init", VisitType.Public);
            cls.addMethod("dispose", VisitType.Public);
            cls.addMethod("onPrint", VisitType.Public, "string", "str");

            list.add(cls = new ILClassInfo("UIContainer"));
            cls.addMethod("init", VisitType.Public, "GameObject", "obj");

            list.add(cls = new ILClassInfo("UIElement"));
            cls.addMethod("init", VisitType.Public, "GameObject", "obj");

            list.add(cls = new ILClassInfo("UIModel"));
            cls.addMethod("init", VisitType.Public, "GameObject", "obj");

            list.add(cls = new ILClassInfo("PoolObject"));
            cls.addMethod("clear", null, VisitType.Public, false);
        }