예제 #1
0
 public void Import(object element, Type entityType, int i, IRow row, ClassDefine cd)
 {
     if (!IsComment)
     {
         ICell currCell = row.GetCell(i + 1);
         ImportValue(element, entityType, fieldName, i, currCell, cd);
     }
 }
예제 #2
0
        public bool Input(ICell cell)
        {
            string cellStringValue = cell.ToString();

            if (cell.ColumnIndex == 0)
            {
                if (cellStringValue.StartsWith("//", System.StringComparison.InvariantCulture))
                {
                    // comments. Ignore
                    return(false);
                }
                else if (cellStringValue.StartsWith("class ", System.StringComparison.InvariantCulture))
                {
                    if (mainClass == null)
                    {
                        mainClass = cellStringValue;
                    }
                    else
                    {
                        // class definition
                        currentClass = new ClassDefine()
                        {
                            className  = cellStringValue,
                            defineCell = cell
                        };
                        classDefines.Add(currentClass);
                    }
                }
                else if (!string.IsNullOrEmpty(cellStringValue.Trim()) && !cellStringValue.StartsWith("//", StringComparison.InvariantCulture))
                {
                    // Find the same field
                    FieldDefine fd = fields.Find((x) => x.GetFieldString() == cellStringValue);
                    if (!cellStringValue.Contains(" "))
                    {
                        // Use previos field
                        fd = lastField;
                    }
                    if (fd == null)
                    {
                        fd = new FieldDefine(cellStringValue);
                        fields.Add(fd);
                        lastField = fd;
                    }
                    if (fd != null)
                    {
                        fd.cellList.Add(cell);
                    }
                }
            }
            else
            {
                if (currentClass != null)
                {
                    currentClass.Input(cell);
                }
            }
            return(true);
        }
예제 #3
0
        public void Import(ScriptableObject sObj, Type textType, ScriptDefine scriptDefine)
        {
            FieldInfo fi = textType.GetField(fieldName);

            // Is a List<...> filed
            if (fi != null && fi.FieldType.IsGenericType && fi.FieldType.GetGenericTypeDefinition() == typeof(List <>))
            {
                // new List<'Data'>()
                var container = Activator.CreateInstance(fi.FieldType);
                fi.SetValue(sObj, container);

                Type entityType = fi.FieldType.GetGenericArguments()[0];

                // Find List.Add(...)
                MethodInfo mi = fi.FieldType.GetMethod("Add", new Type[] { entityType });
                if (mi != null)
                {
                    foreach (ICell cell in cellList)
                    {
                        IRow row = cell.Row;

                        // find this 'Data' class define
                        ClassDefine cd = scriptDefine.classDefines.Find((x) => x.className.Replace("class ", string.Empty) == elementTypeName);
                        if (cd != null && row != null)
                        {
                            // new a 'Data' class
                            var element = Activator.CreateInstance(entityType);

                            // for each fileds in this class
                            for (int i = 0; i < cd.fields.Count; i++)
                            {
                                FieldDefine assetField = cd.fields[i];
                                assetField.Import(element, entityType, i, row, cd);
                            }

                            // List.Add(element)
                            mi.Invoke(container, new object[] { element });
                        }
                    }
                }
            }
            else
            {
            }
        }
예제 #4
0
        public static void GeneratePathScript()
        {
            AssetDatabase.SaveAssets();

            IOUtils.CreateDirIfNotExists(EditorPathManager.DefaultPathScriptGenerateForder);

            string[] fullPathFileNames = Directory.GetFiles(EditorPathManager.DefaultPathConfigGenerateForder, "*PathConfig.asset", SearchOption.AllDirectories);

            foreach (string fullPathFileName in fullPathFileNames)
            {
                Debug.Log(fullPathFileName);
                if (!fullPathFileName.EndsWith(".meta"))
                {
                    Debug.Log("gen: " + fullPathFileName);

                    PathConfig config    = AssetDatabase.LoadAssetAtPath <PathConfig> (fullPathFileName);
                    NameSpace  nameSpace = new NameSpace();
                    nameSpace.Name        = string.IsNullOrEmpty(config.NameSpace) ? "QFramework" : config.NameSpace;
                    nameSpace.FileName    = config.name + ".cs";
                    nameSpace.GenerateDir = string.IsNullOrEmpty(config.ScriptGeneratePath) ? EditorPathManager.DefaultPathScriptGenerateForder : IOUtils.CreateDirIfNotExists("Assets/" + config.ScriptGeneratePath);
                    var classDefine = new ClassDefine();
                    classDefine.Comment = config.Description;
                    classDefine.Name    = config.name;
                    nameSpace.Classes.Add(classDefine);
                    Debug.Log(nameSpace.GenerateDir);
                    foreach (var pathItem in config.List)
                    {
                        if (!string.IsNullOrEmpty(pathItem.Name))
                        {
                            var variable = new Variable(AccessLimit.Private, CompileType.Const, VariableType.String, "m_" + pathItem.Name, pathItem.Path);
                            classDefine.Variables.Add(variable);

                            var property = new Property(AccessLimit.Public, CompileType.Static, VariableType.String, pathItem.Name, pathItem.PropertyGetCode, pathItem.Description);
                            classDefine.Properties.Add(property);
                        }
                    }
                    CodeGenerator.Generate(nameSpace);

                    EditorUtility.SetDirty(config);
                    Resources.UnloadAsset(config);
                }
            }

            AssetDatabase.SaveAssets();
        }
예제 #5
0
        public object Create(ClassDefine define, bool cannotify = true)
        {
            var dic = define.AllProperties.ToDictionary(property => property.Key,
                                                        property => new NotifyProperty {
                PropertyElement = property.Value
            });
            //防止初始化时notifyproperties为空,无法挂载事件//todo:

            /*var instance = (IOrmCell) _pg.CreateClassProxy(define.ClassType, new[] {typeof(IOrmCell)},
             *  new OrmInterceptor(cannotify, dic, Scheduler, define));*/
            var instance = (IOrmCell)_pg.CreateClassProxy(define.ClassType, new[] { typeof(IOrmCell) },
                                                          new OrmInterceptor()
            {
                IsRecordable     = true,
                NotifyProperties = new ConcurrentDictionary <string, NotifyProperty>(dic)
            });

            //初始化propertydefine
            return(instance);
        }
예제 #6
0
        public static void Generate(string ns, List <AssetBundleInfo> assetBundleInfos, string projectTag = null)
        {
            NameSpace nameSpace = new NameSpace();

            nameSpace.Name        = ns;
            nameSpace.GenerateDir = ABEditorPathConfig.ABCodeGeneratePath;
            nameSpace.FileName    = "QAssets.cs";

            for (int i = 0; i < assetBundleInfos.Count; i++)
            {
                AssetBundleInfo assetBundleInfo = assetBundleInfos [i];
                string          className       = assetBundleInfo.name;
                string          bundleName      = className.Substring(0, 1).ToUpper() + className.Substring(1);
                className = className.Substring(0, 1).ToUpper() + className.Substring(1).Replace("/", "_").Replace("@", "_").Replace("!", "_");
                if (!string.IsNullOrEmpty(projectTag))
                {
                    className  = className.Replace("_project_" + projectTag, "");
                    bundleName = bundleName.Replace("_project_" + projectTag, "");
                }
                className = className.ToUpper();

                ClassDefine classDefine = new ClassDefine();
                nameSpace.Classes.Add(classDefine);
                classDefine.Name = className;

                Variable variable = new Variable(AccessLimit.Public, CompileType.Const, VariableType.String, "BUNDLE_NAME", bundleName.ToUpperInvariant());
                classDefine.Variables.Add(variable);

                foreach (var asset in assetBundleInfo.assets)
                {
                    string content = Path.GetFileNameWithoutExtension(asset).ToUpperInvariant();

                    Variable assetVariable = new Variable(AccessLimit.Public, CompileType.Const, VariableType.String, content.Replace("@", "_").Replace("!", "_"), content);
                    classDefine.Variables.Add(assetVariable);
                }
            }

            CodeGenerator.Generate(nameSpace);
        }
예제 #7
0
    // import Array
    static void ImportArray(object arrayObj, FieldInfo efi, int i, ICell currCell, ClassDefine cd)
    {
        Type  elementType = efi.FieldType.GetElementType();
        IRow  defineRow   = cd.defineCell.Row;
        ICell defineCell  = defineRow.GetCell(i + 1);

        // Get array
        Array array = efi.GetValue(arrayObj) as Array;

        if (array == null)
        {
            // count array length
            int numElement = 0;
            foreach (ICell countCell in defineRow)
            {
                if (countCell.ToString() == defineCell.ToString())
                {
                    numElement++;
                }
            }

            // new Array
            array = Array.CreateInstance(elementType, numElement);

            // Assign array
            efi.SetValue(arrayObj, array);
        }
        if (array != null)
        {
            int idxElement = 0;
            foreach (ICell countCell in defineRow)
            {
                if (countCell == defineCell)
                {
                    break;
                }
                if (countCell.ToString() == defineCell.ToString())
                {
                    idxElement++;
                }
            }

            // set element value
            if (elementType == typeof(int))
            {
                int result;
                if (currCell.CellType == CellType.Numeric || (currCell.CellType == CellType.Formula && currCell.CachedFormulaResultType == CellType.Numeric))
                {
                    result = (int)currCell.NumericCellValue;
                }
                else if (int.TryParse(currCell.ToString(), out result))
                {
                }
                array.SetValue(result, idxElement);
            }
            else if (elementType == typeof(bool))
            {
                bool result;
                if (currCell.CellType == CellType.Numeric || (currCell.CellType == CellType.Formula && currCell.CachedFormulaResultType == CellType.Numeric))
                {
                    result = (int)currCell.NumericCellValue != 0;
                }
                else if (bool.TryParse(currCell.ToString(), out result))
                {
                }
                array.SetValue(result, idxElement);
            }
            else if (elementType == typeof(float))
            {
                float result;
                if (currCell.CellType == CellType.Numeric || (currCell.CellType == CellType.Formula && currCell.CachedFormulaResultType == CellType.Numeric))
                {
                    result = (float)currCell.NumericCellValue;
                }
                else if (float.TryParse(currCell.ToString(), out result))
                {
                }
                array.SetValue(result, idxElement);
            }
            else if (elementType == typeof(string))
            {
                string result = "";
                if (currCell.CellType == CellType.Formula && currCell.CachedFormulaResultType == CellType.String)
                {
                    result = currCell.StringCellValue;
                }
                else
                {
                    result = currCell.ToString();
                }
                array.SetValue(result, idxElement);
            }
        }
    }
예제 #8
0
    // Import value
    static void ImportValue(object element, Type entityType, string fieldName, int i, ICell currCell, ClassDefine cd)
    {
        FieldInfo efi = entityType.GetField(fieldName);

        if (efi != null && currCell != null)
        {
            if (efi.FieldType.IsArray)
            {
                ImportArray(element, efi, i, currCell, cd);
            }
            else
            {
                SetValue(element, efi, currCell);
            }
        }
    }
예제 #9
0
        private void Register(Type type)
        {
            var ormAttribute = type.GetCustomAttribute <OrmAttribute>();

            if (ormAttribute == null)
            {
                throw new ArgumentException("未标记可被映射");
            }
            if (ClassDefines.ContainsKey(type))
            {
                return;
            }
            var mainClassDefine = new ClassDefine {
                Name      = type.Name,
                Table     = ormAttribute.Table,
                ClassType = type
            };

            //预先插入防止双向引用
            ClassDefines.TryAdd(type, mainClassDefine);
            var propertyInfos = type.GetProperties();
            var enumType      = typeof(Enum);
            var nullableType  = typeof(Nullable <>);

            foreach (var propertyInfo in propertyInfos)
            {
                var propertyAttribute = propertyInfo.GetCustomAttribute <PropertyAttribute>();
                if (propertyAttribute != null)
                {
                    var basePropertyElement = new BasePropertyElement {
                        Column       = propertyAttribute.Column,
                        PropertyInfo = propertyInfo,
                    };
                    if (enumType.IsAssignableFrom(propertyInfo.PropertyType))
                    {
                        basePropertyElement.IsEnum = true;
                    }
                    else if (propertyInfo.PropertyType
                             .IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition() == nullableType)
                    {
                        basePropertyElement.IsNullable       = true;
                        basePropertyElement.NullableBaseType = propertyInfo.PropertyType.GenericTypeArguments[0];
                    }
                    mainClassDefine.AllProperties.Add(propertyInfo.Name, basePropertyElement);
                    continue;
                }
                var oneToOneAttribute = propertyInfo.GetCustomAttribute <OneToOneAttribute>();
                if (oneToOneAttribute != null)
                {
                    var onetoOne = new OnetoOne {
                        Column       = oneToOneAttribute.ForeignKey,
                        IsNeedUpdate = oneToOneAttribute.Update,
                        PropertyInfo = propertyInfo
                    };
                    if (!ClassDefines.ContainsKey(propertyInfo.PropertyType))
                    {
                        Register(propertyInfo.PropertyType);
                    }
                    var classDefine = ClassDefines[propertyInfo.PropertyType];
                    onetoOne.ReferClass = classDefine;
                    mainClassDefine.AllProperties.Add(propertyInfo.Name, onetoOne);
                    continue;
                }
                var manyToManyAttribute = propertyInfo.GetCustomAttribute <ManyToManyAttribute>();
                if (manyToManyAttribute != null)
                {
                    var manytoMany = new ManytoMany {
                        Column       = manyToManyAttribute.PrimaryKay,
                        ReferColumn  = manyToManyAttribute.ForeignKey,
                        Table        = manyToManyAttribute.Table,
                        IsNeedUpdate = manyToManyAttribute.Update,
                        PropertyInfo = propertyInfo
                    };
                    if (!ClassDefines.ContainsKey(propertyInfo.PropertyType.GenericTypeArguments[0]))
                    {
                        Register(propertyInfo.PropertyType.GenericTypeArguments[0]);
                    }
                    var define = ClassDefines[propertyInfo.PropertyType.GenericTypeArguments[0]];
                    manytoMany.ReferClass = define;
                    mainClassDefine.AllProperties.Add(propertyInfo.Name, manytoMany);
                    continue;
                }
                var manyToOneAttribute = propertyInfo.GetCustomAttribute <ManyToOneAttribute>();
                if (manyToOneAttribute != null)
                {
                    var manytoOne = new ManytoOne {
                        Column       = manyToOneAttribute.ForeignKey,
                        PropertyInfo = propertyInfo
                    };
                    if (!ClassDefines.ContainsKey(propertyInfo.PropertyType))
                    {
                        Register(propertyInfo.PropertyType);
                    }
                    var define = ClassDefines[propertyInfo.PropertyType];
                    manytoOne.ReferClass = define;
                    mainClassDefine.AllProperties.Add(propertyInfo.Name, manytoOne);
                    continue;
                }
                var oneToManyAttribute = propertyInfo.GetCustomAttribute <OneToManyAttribute>();
                if (oneToManyAttribute != null)
                {
                    var onetoMany = new OnetoMany {
                        ReferColumn  = oneToManyAttribute.ForeignKey,
                        IsNeedUpdate = oneToManyAttribute.Update,
                        PropertyInfo = propertyInfo
                    };
                    if (!ClassDefines.ContainsKey(propertyInfo.PropertyType.GenericTypeArguments[0]))
                    {
                        Register(propertyInfo.PropertyType.GenericTypeArguments[0]);
                    }
                    var define = ClassDefines[propertyInfo.PropertyType.GenericTypeArguments[0]];
                    onetoMany.ReferClass = define;
                    mainClassDefine.AllProperties.Add(propertyInfo.Name, onetoMany);
                    continue;
                }
                var foreignOneAttribute = propertyInfo.GetCustomAttribute <ForeignOneAttribute>();
                if (foreignOneAttribute != null)
                {
                    var foreignOne = new ForeignOne {
                        Column       = foreignOneAttribute.ForeignKey,
                        PropertyInfo = propertyInfo
                    };
                    if (!ClassDefines.ContainsKey(propertyInfo.PropertyType))
                    {
                        Register(propertyInfo.PropertyType);
                    }
                    var define = ClassDefines[propertyInfo.PropertyType];
                    foreignOne.ReferClass = define;
                    mainClassDefine.AllProperties.Add(propertyInfo.Name, foreignOne);
                }
            }
        }
예제 #10
0
 public OrmMix(OrmInterceptor interceptor, ClassDefine classDefine)
 {
     this.ClassDefine    = classDefine;
     this.OrmInterceptor = interceptor;
     ModelStatus         = ModelStatus.Default;
 }