示例#1
0
        private string GenerateWrites()
        {
            var    type        = types[selectedType].type;
            bool   isComponent = typeof(Component).IsAssignableFrom(type);
            string writes      = "";

            for (int i = 0; i < fields.Length; i++)
            {
                var field    = fields[i];
                var selected = fieldSelected[i];
                var es3Type  = ES3TypeMgr.GetES3Type(field.MemberType);

                if (!selected || isComponent && (field.Name == ES3Reflection.componentTagFieldName || field.Name == ES3Reflection.componentNameFieldName))
                {
                    continue;
                }

                string writeByRef   = ES3Reflection.IsAssignableFrom(typeof(UnityEngine.Object), field.MemberType) ? "ByRef" : "";
                string es3TypeParam = HasExplicitES3Type(es3Type) && writeByRef == "" ? ", " + es3Type.GetType().Name + ".Instance" : "";

                if (!field.IsPublic)
                {
                    string memberType = field.isProperty ? "Property" : "Field";
                    writes += String.Format("\n\t\t\twriter.WritePrivate{3}{1}(\"{0}\", instance);", field.Name, writeByRef, es3TypeParam, memberType);
                }
                else
                {
                    writes += String.Format("\n\t\t\twriter.WriteProperty{1}(\"{0}\", instance.{0}{2});", field.Name, writeByRef, es3TypeParam);
                }
            }
            return(writes);
        }
示例#2
0
        private void SelectType(int typeIndex)
        {
            selectedType = typeIndex;

            if (selectedType == -1)
            {
                SaveType("TypesWindowSelectedType", -1);
                return;
            }

            SaveType("TypesWindowSelectedType", selectedType);

            if (!recentTypes.Contains(typeIndex))
            {
                // If our recent type queue is full, remove an item before adding another.
                if (recentTypes.Count == recentTypeCount)
                {
                    recentTypes.RemoveAt(0);
                }
                recentTypes.Add(typeIndex);
                for (int j = 0; j < recentTypes.Count; j++)
                {
                    SaveType("TypesWindowRecentType" + j, recentTypes[j]);
                }
            }

            var type = types[selectedType].type;

            fields        = ES3Reflection.GetSerializableMembers(type, false);
            fieldSelected = new bool[fields.Length];

            var es3Type = ES3TypeMgr.GetES3Type(type);

            // If there's no ES3Type for this, only select fields which are supported by reflection.
            if (es3Type == null)
            {
                var safeFields = ES3Reflection.GetSerializableMembers(type, true);
                for (int i = 0; i < fields.Length; i++)
                {
                    fieldSelected[i] = safeFields.Any(item => item.Name == fields[i].Name);
                }
                return;
            }

            // Get fields and whether they're selected.
            var selectedFields     = new List <string>();
            var propertyAttributes = es3Type.GetType().GetCustomAttributes(typeof(ES3PropertiesAttribute), false);

            if (propertyAttributes.Length > 0)
            {
                selectedFields.AddRange(((ES3PropertiesAttribute)propertyAttributes[0]).members);
            }

            fieldSelected = new bool[fields.Length];

            for (int i = 0; i < fields.Length; i++)
            {
                fieldSelected[i] = selectedFields.Contains(fields[i].Name);
            }
        }
示例#3
0
        private string GenerateWrites()
        {
            var    type        = types[selectedType].type;
            bool   isComponent = typeof(Component).IsAssignableFrom(type);
            string writes      = "";

            for (int i = 0; i < fields.Length; i++)
            {
                var field    = fields[i];
                var selected = fieldSelected[i];
                var es3Type  = ES3TypeMgr.GetES3Type(field.MemberType);

                if (!selected || isComponent && (field.Name == ES3Reflection.componentTagFieldName || field.Name == ES3Reflection.componentNameFieldName))
                {
                    continue;
                }

                string writeByRef   = ES3Reflection.IsAssignableFrom(typeof(UnityEngine.Object), field.MemberType) ? "ByRef" : "";
                string es3TypeParam = HasExplicitES3Type(es3Type) && writeByRef == "" ? ", " + es3Type.GetType().Name + ".Instance" : (writeByRef == "" ? ", ES3Internal.ES3TypeMgr.GetOrCreateES3Type(typeof(" + GetFullTypeName(field.MemberType) + "))" : "");
                // If this is static, access the field through the class name rather than through an instance.
                string instance = (field.IsStatic) ? GetFullTypeName(type) : "instance";

                if (!field.IsPublic)
                {
                    string memberType = field.isProperty ? "Property" : "Field";
                    writes += String.Format("\r\n\t\t\twriter.WritePrivate{2}{1}(\"{0}\", instance);", field.Name, writeByRef, memberType);
                }
                else
                {
                    writes += String.Format("\r\n\t\t\twriter.WriteProperty{1}(\"{0}\", {3}.{0}{2});", field.Name, writeByRef, es3TypeParam, instance);
                }
            }
            return(writes);
        }
        public override void WriteObject(object obj, ES3Writer writer, ES3.ReferenceMode mode)
        {
            if (WriteUsingDerivedType(obj, writer))
            {
                return;
            }

            var instance = (UnityEngine.GameObject)obj;

            if (ES3ReferenceMgrBase.Current != null && mode != ES3.ReferenceMode.ByValue)
            {
                var es3Prefab = instance.GetComponent <ES3Prefab>();
                if (es3Prefab != null)
                {
                    writer.WriteProperty(prefabPropertyName, es3Prefab, ES3Type_ES3PrefabInternal.Instance);
                }
                else
                {
                    writer.WriteRef(instance);
                    // Write the ID of this Transform so we can assign it's ID when we load.
                    writer.WriteProperty(transformPropertyName, ES3ReferenceMgrBase.Current.Add(instance.transform));
                }
                if (mode == ES3.ReferenceMode.ByRef)
                {
                    return;
                }
            }

            var es3AutoSave = instance.GetComponent <ES3AutoSave>();

            //if(es3AutoSave == null || es3AutoSave.saveLayer)
            writer.WriteProperty("layer", instance.layer, ES3Type_int.Instance);
            //if(es3AutoSave == null || es3AutoSave.saveTag)
            writer.WriteProperty("tag", instance.tag, ES3Type_string.Instance);
            //if(es3AutoSave == null || es3AutoSave.saveName)
            writer.WriteProperty("name", instance.name, ES3Type_string.Instance);
            //if(es3AutoSave == null || es3AutoSave.saveHideFlags)
            writer.WriteProperty("hideFlags", instance.hideFlags);
            if (es3AutoSave == null || es3AutoSave.saveChildren)
            {
                writer.WriteProperty("children", GetChildren(instance));
            }

            var components = new List <Component>();

            foreach (var component in instance.GetComponents <Component>())
            {
                var componentType = component.GetType();
                // Only save explicitly-supported Components, /*or those explicitly marked as Serializable*/.
                if (ES3TypeMgr.GetES3Type(componentType) != null /*|| ES3Reflection.AttributeIsDefined(componentType, ES3Reflection.serializableAttributeType)*/)
                {
                    components.Add(component);
                }
            }

            writer.WriteProperty("components", components);
        }
示例#5
0
        private void SelectType(int i)
        {
            selectedType = i;

            if (selectedType == -1)
            {
                SaveType("TypesWindowSelectedType", -1);
                return;
            }

            SaveType("TypesWindowSelectedType", selectedType);

            if (!recentTypes.Contains(i))
            {
                // If our recent type queue is full, remove an item before adding another.
                if (recentTypes.Count == recentTypeCount)
                {
                    recentTypes.RemoveAt(0);
                }
                recentTypes.Add(i);
                for (int j = 0; j < recentTypes.Count; j++)
                {
                    SaveType("TypesWindowRecentType" + j, recentTypes[j]);
                }
            }

            var type = types[selectedType].type;

            fields        = ES3Reflection.GetSerializableES3Fields(type);
            fieldSelected = new bool[fields.Length];

            var es3Type = ES3TypeMgr.GetES3Type(type);

            // If there's no ES3Type for this, no fields will be selected so we can exit early.
            if (es3Type == null)
            {
                return;
            }

            // Get fields and whether they're selected.
            var selectedFields     = new List <string>();
            var propertyAttributes = es3Type.GetType().GetCustomAttributes(typeof(ES3PropertiesAttribute), false);

            if (propertyAttributes.Length > 0)
            {
                selectedFields.AddRange(((ES3PropertiesAttribute)propertyAttributes[0]).properties);
            }

            fieldSelected = new bool[fields.Length];

            for (int j = 0; j < fields.Length; j++)
            {
                fieldSelected[j] = selectedFields.Contains(fields[j].Name);
            }
        }
示例#6
0
        /* Whether this type has an explicit ES3Type. For example, ES3ArrayType would return false, but ES3Vector3ArrayType would return true */
        private static bool HasExplicitES3Type(Type type)
        {
            var es3Type = ES3TypeMgr.GetES3Type(type);

            if (es3Type == null)
            {
                return(false);
            }
            // If this ES3Type has a static Instance property, return true.
            if (es3Type.GetType().GetField("Instance", BindingFlags.Public | BindingFlags.Static) != null)
            {
                return(true);
            }
            return(false);
        }
        private string GenerateReads()
        {
            var    type        = types[selectedType].type;
            bool   isComponent = typeof(Component).IsAssignableFrom(type);
            string reads       = "";

            for (int i = 0; i < fields.Length; i++)
            {
                var field    = fields[i];
                var selected = fieldSelected[i];

                if (!selected || isComponent && (field.Name == "tag" || field.Name == "name"))
                {
                    continue;
                }

                string fieldTypeName = GetFullTypeName(field.MemberType);
                string es3TypeParam  = HasExplicitES3Type(field.MemberType) ? ES3TypeMgr.GetES3Type(field.MemberType).GetType().Name + ".Instance" : "";
                // If this is static, access the field through the class name rather than through an instance.
                string instance = (field.IsStatic) ? GetFullTypeName(type) : "instance";

                // If we're writing a private field or property, we need to write it using a different method.
                if (!field.IsPublic)
                {
                    es3TypeParam = ", " + es3TypeParam;
                    if (field.isProperty)
                    {
                        reads += String.Format("\r\n\t\t\t\t\tcase \"{0}\":\r\n\t\t\t\t\treader.SetPrivateProperty(\"{0}\", reader.Read<{1}>(), instance);\r\n\t\t\t\t\tbreak;", field.Name, fieldTypeName);
                    }
                    else
                    {
                        reads += String.Format("\r\n\t\t\t\t\tcase \"{0}\":\r\n\t\t\t\t\treader.SetPrivateField(\"{0}\", reader.Read<{1}>(), instance);\r\n\t\t\t\t\tbreak;", field.Name, fieldTypeName);
                    }
                }
                else
                {
                    reads += String.Format("\r\n\t\t\t\t\tcase \"{0}\":\r\n\t\t\t\t\t\t{3}.{0} = reader.Read<{1}>({2});\r\n\t\t\t\t\t\tbreak;", field.Name, fieldTypeName, es3TypeParam, instance);
                }
            }
            return(reads);
        }
示例#8
0
        public override void WriteObject(object obj, ES3Writer writer, ES3.ReferenceMode mode)
        {
            if (WriteUsingDerivedType(obj, writer))
            {
                return;
            }
            var instance = (UnityEngine.GameObject)obj;

            if (mode != ES3.ReferenceMode.ByValue)
            {
                writer.WriteRef(instance);

                var es3Prefab = instance.GetComponent <ES3Prefab>();
                if (es3Prefab != null)
                {
                    writer.WriteProperty(prefabPropertyName, es3Prefab, ES3Type_ES3PrefabInternal.Instance);
                }
                // Write the ID of this Transform so we can assign it's ID when we load.
                writer.WriteProperty(transformPropertyName, ES3ReferenceMgrBase.Current.Add(instance.transform));
                if (mode == ES3.ReferenceMode.ByRef)
                {
                    return;
                }
            }

            var es3AutoSave = instance.GetComponent <ES3AutoSave>();

            writer.WriteProperty("layer", instance.layer, ES3Type_int.Instance);
            writer.WriteProperty("tag", instance.tag, ES3Type_string.Instance);
            writer.WriteProperty("name", instance.name, ES3Type_string.Instance);
            writer.WriteProperty("hideFlags", instance.hideFlags);
            writer.WriteProperty("active", instance.activeSelf);

            if (saveChildren || (es3AutoSave != null && es3AutoSave.saveChildren))
            {
                writer.WriteProperty("children", GetChildren(instance), ES3.ReferenceMode.ByRefAndValue);
            }

            List <Component> components;

            // If there's an ES3AutoSave attached and Components are marked to be saved, save these.
            var autoSave = instance.GetComponent <ES3AutoSave>();

            if (autoSave != null && autoSave.componentsToSave != null && autoSave.componentsToSave.Count > 0)
            {
                components = autoSave.componentsToSave;
            }
            // Otherwise, only save explicitly-supported Components, /*or those explicitly marked as Serializable*/.
            else
            {
                components = new List <Component>();
                foreach (var component in instance.GetComponents <Component>())
                {
                    if (component != null && ES3TypeMgr.GetES3Type(component.GetType()) != null)
                    {
                        components.Add(component);
                    }
                }
            }

            writer.WriteProperty("components", components, ES3.ReferenceMode.ByRefAndValue);
        }