Exemple #1
0
        private void Generate()
        {
            var type = types[selectedType].type;

            if (type == null)
            {
                EditorUtility.DisplayDialog("Type not selected", "Type not selected. Please ensure you select a type", "Ok");
                return;
            }

            unsavedChanges = false;

            // Get the serializable fields of this class.
            //var fields = ES3Reflection.GetSerializableES3Fields(type);

            // The string that we suffix to the class name. i.e. UnityEngine_UnityEngine_Transform.
            string es3TypeSuffix = type.Name;
            // The string for the full C#-safe type name. This name must be suitable for going inside typeof().
            string fullType = GetFullTypeName(type);
            // The list of WriteProperty calls to write the properties of this type.
            string writes = GenerateWrites();
            // The list of case statements and Read calls to read the properties of this type.
            string reads = GenerateReads();
            // A comma-seperated string of fields we've supported in this type.
            string propertyNames = "";

            bool first = true;

            for (int i = 0; i < fields.Length; i++)
            {
                if (!fieldSelected[i])
                {
                    continue;
                }

                if (first)
                {
                    first = false;
                }
                else
                {
                    propertyNames += ", ";
                }
                propertyNames += "\"" + fields[i].Name + "\"";
            }

            var easySaveEditorPath = ES3Settings.PathToEasySaveFolder() + "Editor/";

            // Insert the relevant strings into the template.
            string template;

            if (typeof(Component).IsAssignableFrom(type))
            {
                template = File.ReadAllText(easySaveEditorPath + componentTemplateFile);
            }
            else if (ES3Reflection.IsValueType(type))
            {
                template = File.ReadAllText(easySaveEditorPath + valueTemplateFile);
            }
            else if (typeof(ScriptableObject).IsAssignableFrom(type))
            {
                template = File.ReadAllText(easySaveEditorPath + scriptableObjectTemplateFile);
            }
            else
            {
                template = File.ReadAllText(easySaveEditorPath + classTemplateFile);
            }
            template = template.Replace("[es3TypeSuffix]", es3TypeSuffix);
            template = template.Replace("[fullType]", fullType);
            template = template.Replace("[writes]", writes);
            template = template.Replace("[reads]", reads);
            template = template.Replace("[propertyNames]", propertyNames);

            // Create the output file.


            string outputFilePath = GetOutputPath(type);
            var    fileInfo       = new FileInfo(outputFilePath);

            fileInfo.Directory.Create();
            File.WriteAllText(outputFilePath, template);
            AssetDatabase.Refresh();
        }
Exemple #2
0
 public void WriteType(Type type)
 {
     WriteProperty(ES3Type.typeFieldName, ES3Reflection.GetShortAssemblyQualifiedName(type));
 }
Exemple #3
0
        private void TypePane()
        {
            if (selectedType < 0)
            {
                return;
            }

            var style = EditorStyle.Get;

            var typeListItem = types[selectedType];
            var type         = typeListItem.type;

            typePaneScrollPos = EditorGUILayout.BeginScrollView(typePaneScrollPos, style.area);

            GUILayout.Label(typeListItem.name, style.subheading);
            GUILayout.Label(typeListItem.namespaceName);

            EditorGUILayout.BeginVertical(style.area);

            bool hasParameterlessConstructor = ES3Reflection.HasParameterlessConstructor(type);
            bool isComponent = ES3Reflection.IsAssignableFrom(typeof(Component), type);

            string path = GetOutputPath(types[selectedType].type);

            // An ES3Type file already exists.
            if (File.Exists(path))
            {
                if (hasParameterlessConstructor || isComponent)
                {
                    EditorGUILayout.BeginHorizontal();
                    if (GUILayout.Button("Reset to Default"))
                    {
                        SelectNone(true, true);
                        AssetDatabase.MoveAssetToTrash("Assets" + path.Remove(0, Application.dataPath.Length));
                        SelectType(selectedType);
                    }
                    if (GUILayout.Button("Edit ES3Type Script"))
                    {
                        UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal(path, 1);
                    }
                    EditorGUILayout.EndHorizontal();
                }
                else
                {
                    EditorGUILayout.HelpBox("This type has no public parameterless constructors.\n\nTo support this type you will need to modify the ES3Type script to use a specific constructor instead of the parameterless constructor.", MessageType.Info);
                    if (GUILayout.Button("Click here to edit the ES3Type script"))
                    {
                        UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal(path, 1);
                    }
                    if (GUILayout.Button("Reset to Default"))
                    {
                        SelectAll(true, true);
                        File.Delete(path);
                        AssetDatabase.Refresh();
                    }
                }
            }
            // No ES3Type file and no fields.
            else if (fields.Length == 0)
            {
                if (!hasParameterlessConstructor && !isComponent)
                {
                    EditorGUILayout.HelpBox("This type has no public parameterless constructors.\n\nTo support this type you will need to create an ES3Type script and modify it to use a specific constructor instead of the parameterless constructor.", MessageType.Info);
                }

                if (GUILayout.Button("Create ES3Type Script"))
                {
                    Generate();
                }
            }
            // No ES3Type file, but fields are selectable.
            else
            {
                if (!hasParameterlessConstructor && !isComponent)
                {
                    EditorGUILayout.HelpBox("This type has no public parameterless constructors.\n\nTo support this type you will need to select the fields you wish to serialize below, and then modify the generated ES3Type script to use a specific constructor instead of the parameterless constructor.", MessageType.Info);
                    if (GUILayout.Button("Select all fields and generate ES3Type script"))
                    {
                        SelectAll(true, false);
                        Generate();
                    }
                }
                else
                {
                    if (GUILayout.Button("Create ES3Type Script"))
                    {
                        Generate();
                    }
                }
            }

            EditorGUILayout.EndVertical();

            PropertyPane();

            EditorGUILayout.EndScrollView();
        }
Exemple #4
0
 internal Type ReadType()
 {
     return(ES3Reflection.GetType(Read <string>(ES3Type_string.Instance)));
 }
        protected override object ReadObject <T>(ES3Reader reader)
        {
            UnityEngine.Object obj = null;

            // Read the intial properties regarding the instance we're loading.
            while (true)
            {
                var refMgr = ES3ReferenceMgrBase.Current;
                if (refMgr == null)
                {
                    reader.Skip();
                    continue;
                }

                var propertyName = ReadPropertyName(reader);
                if (propertyName == ES3Type.typeFieldName)
                {
                    return(ES3TypeMgr.GetOrCreateES3Type(reader.ReadType()).Read <T>(reader));
                }

                else if (propertyName == ES3ReferenceMgrBase.referencePropertyName)
                {
                    if (refMgr == null)
                    {
                        reader.Skip();
                        continue;
                    }

                    long id = reader.Read <long>(ES3Type_long.Instance);
                    obj = refMgr.Get(id);

                    if (obj == null)
                    {
                        // If an instance isn't already registered for this object, create an instance and register the reference.
                        obj = new GameObject();
                        refMgr.Add(obj, id);
                    }
                    else if (!ES3Reflection.IsAssignableFrom(typeof(T), obj.GetType()))
                    {
                        throw new MissingReferenceException("The instance with ID \"" + id + "\" is a different type than expected. Expected \"" + typeof(T) + "\", found \"" + obj.GetType() + "\"");
                    }
                }

                else if (propertyName == transformPropertyName)
                {
                    if (refMgr == null)
                    {
                        reader.Skip();
                        continue;
                    }

                    // Now load the Transform's ID and assign it to the Transform of our object.
                    long transformID = reader.Read <long>(ES3Type_long.Instance);
                    refMgr.Add(((GameObject)obj).transform, transformID);
                }

                else if (propertyName == prefabPropertyName)
                {
                    if (ES3ReferenceMgrBase.Current == null)
                    {
                        reader.Skip();
                    }
                    else
                    {
                        obj = reader.Read <GameObject>(ES3Type_ES3PrefabInternal.Instance);
                    }
                }
                else if (propertyName == null)
                {
                    return(obj);
                }
                else
                {
                    reader.overridePropertiesName = propertyName;
                    break;
                }
            }

            if (obj == null)
            {
                obj = new GameObject();
            }

            ReadInto <T>(reader, obj);
            return(obj);
        }
Exemple #6
0
 public ES3ReflectedType(Type type)
 {
     this.type        = type;
     this.isValueType = ES3Reflection.IsValueType(type);
     isReflectedType  = true;
 }
Exemple #7
0
        public override object Read(ES3Reader reader)
        {
            if (reader.StartReadCollection())
            {
                return(null);
            }

            // Create a List to store the items as a 1D array, which we can work out the positions of by calculating the lengths of the two dimensions.
            var items   = new List <object>();
            int length1 = 0;
            int length2 = 0;

            // Iterate through each sub-array
            while (true)
            {
                if (!reader.StartReadCollectionItem())
                {
                    break;
                }
                reader.StartReadCollection();

                length1++;

                while (true)
                {
                    if (!reader.StartReadCollectionItem())
                    {
                        break;
                    }

                    ReadICollection <object>(reader, items, elementType);
                    length2++;

                    if (reader.EndReadCollectionItem())
                    {
                        break;
                    }
                }

                reader.EndReadCollection();
                if (reader.EndReadCollectionItem())
                {
                    break;
                }
            }

            reader.EndReadCollection();

            length2 = length2 / length1;
            int length3 = items.Count / length2 / length1;

            var array = ES3Reflection.ArrayCreateInstance(elementType.type, new int[] { length1, length2, length3 });

            for (int i = 0; i < length1; i++)
            {
                for (int j = 0; j < length2; j++)
                {
                    for (int k = 0; k < length3; k++)
                    {
                        array.SetValue(items[i * (length2 * length3) + (j * length3) + k], i, j, k);
                    }
                }
            }

            return(array);
        }