Example #1
0
        public void GetPackableFields(Type type, TypeInfo currTypeInfo, PackObjectAttribute packObjAttr)
        {
            int nestedFieldCount = 0;
            int localFieldCount  = 0;

            var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);

            var defaultInclusion = packObjAttr.defaultInclusion;

            int cnt = fields.Length;

            for (int i = 0; i < cnt; ++i)
            {
                var fieldInfo = fields[i];

                ///// Don't include any nested packObjs that will produce a nested loop
                //if (type.CheckForNestedLoop())
                //{
                //	Debug.LogWarning("<b>" + type.Name + "</b> contains a nested loop with field <b>" + fieldInfo.FieldType + " " + fieldInfo.Name + "</b>. Will not be included in serialization.");
                //	continue;
                //}

                var attrs = fieldInfo.GetCustomAttributes(typeof(PackBaseAttribute), false);

                /// Only pack if marked with a Pack, or if we are set to capture all public
                if (defaultInclusion == DefaultPackInclusion.Explicit && attrs.Length == 0)
                {
                    continue;
                }

                /// Count up fields in nested
                var nestedAttrs = fieldInfo.FieldType.GetCustomAttributes(typeof(PackObjectAttribute), false);
                if (nestedAttrs.Length != 0)
                {
                    bool haschanged     = false;
                    bool alreadyCurrent = (tempProcessedTypes.Contains(fieldInfo.FieldType));
                    var  nestedTypeInfo = (alreadyCurrent) ? catalogue.GetTypeInfo(fieldInfo.FieldType) : MakeRecordCurrent(fieldInfo.FieldType, ref haschanged);

                    if (nestedTypeInfo != null)
                    {
                        nestedFieldCount += nestedTypeInfo.totalFieldCount;
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                {
                    localFieldCount++;
                }
            }

            currTypeInfo.localFieldCount = localFieldCount;
            currTypeInfo.totalFieldCount = localFieldCount + nestedFieldCount;
        }
        public TypeInfo GenerateAndRecord(Type type, TypeInfo typeInfo, PackObjectAttribute packObjAttr)
        {
            string filepath = typeInfo.filepath;

            Debug.Log("Generating codegen for PackObj <b>" + type.FullName + "</b> to file: " + filepath);
            StringBuilder sb = type.GeneratePackCode(typeInfo, packObjAttr);

            File.WriteAllText(filepath, sb.ToString());

            typeInfo.codegenFileWriteTime = File.GetLastWriteTime(filepath).Ticks;
            catalogue.Add(type, typeInfo);

            EditorUtility.SetDirty(this);
            //AssetDatabase.SaveAssets();

            return(typeInfo);
        }