/* Recreates the ES2Init, adding to it the given type */ public static void AddTypeToES2Init(Type type) { string es2TypeList = ""; string userTypeList = ""; foreach (KeyValuePair <Type, ES2Type> es2Type in supportedTypes) { // Don't add the same type twice. if (es2Type.Key == type) { continue; } // Add ES2Types built in to ES2 to the list first, and then the user ones later. if (es2Type.Value.GetType().Assembly.GetName().Name == "ES2") { es2TypeList += @" ES2TypeManager.types[typeof("+ es2Type.Key.ToString() + ")] = new " + es2Type.Value.GetType().Name + "();\n"; } else { userTypeList += @" ES2TypeManager.types[typeof("+ es2Type.Key.ToString() + ")] = new " + es2Type.Value.GetType().Name + "();\n"; } } // Add the type specified as a parameter. if (type != null) { userTypeList += @" ES2TypeManager.types[typeof("+ type.ToString() + ")] = new " + file_prefix + GetES2TypeName(type) + "();\n"; } File.WriteAllText(pathToES2Init, String.Format(ES2EditorTemplates.GetTemplate("ES2InitTemplate"), es2TypeList + userTypeList)); }
/* * Adds support for a Type, and all of it's supportable variables. */ public static void AddType(Type type) { string writes = ""; string reads = ""; FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance); PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (FieldInfo field in fields) { if (!field.FieldType.IsValueType || !TypeIsSupported(field.FieldType) || !FieldIsSupportable(field)) { continue; } Type fieldType = field.FieldType; reads += CreateReadCall(fieldType, "data." + field.Name) + "\n"; writes += CreateWriteCall(fieldType, "data." + field.Name); } foreach (PropertyInfo property in properties) { if (!property.PropertyType.IsValueType || !TypeIsSupported(property.PropertyType) || !PropertyIsSupportable(property)) { continue; } Type propertyType = property.PropertyType; reads += CreateReadCall(propertyType, "data." + property.Name) + "\n"; writes += CreateWriteCall(propertyType, "data." + property.Name) + "\n"; } string namespace_string = ""; if (!String.IsNullOrEmpty(type.Namespace) && type.Namespace != "UnityEngine") { namespace_string = "using " + type.Namespace + ";"; } string create_instance = ""; // Used when creating a new instance of this object. if ((typeof(Component).IsAssignableFrom(type))) { create_instance = "GetOrCreate<" + type.ToString() + ">()"; } else if ((typeof(ScriptableObject).IsAssignableFrom(type))) { create_instance = "ScriptableObject.CreateInstance<" + type.ToString() + ">()"; } else { create_instance = "new " + type.ToString() + "()"; } string file; /*if(typeof(Component).IsAssignableFrom(type)) * file = String.Format(ES2EditorTemplates.es2ComponentTypeTemplate, file_prefix, GetES2TypeName(type), type.ToString(), writes, reads, namespace_string); * else*/ file = String.Format(ES2EditorTemplates.GetTemplate("ES2TypeTemplate"), file_prefix, GetES2TypeName(type), type.ToString(), writes, reads, namespace_string, create_instance); // Write the file. File.WriteAllText(GetPathToES2Type(type), file); AddTypeToES2Init(type); AssetDatabase.Refresh(); }
/* Creates an ES2Type file template with reader.Read and writer.Write calls automatically added, for the type at the given index. */ private static void CreateTypeFile(Type type, List <ES2EditorProperty> properties) { string writes = ""; string reads = ""; foreach (ES2EditorProperty property in properties) { if (type.IsEnum) { writes += @" writer.Write((int)data);"+ "\n"; break; } if (property.isSupported && property.isSelected) { writes += @" writer.Write(data."+ property.name + ");\n"; string typeName = GetTypeName(property.type); if (property.isCollection) { // Get the names of the collection types as comma seperated values. string genericParams = ""; for (int i = 0; i < property.collectionContentTypes.Length; i++) { if (i != 0) { genericParams += ","; } genericParams += GetTypeName(property.collectionContentTypes[i]); } reads += @" data."+ property.name + " = reader.Read" + property.collectionType + "<" + genericParams + ">();\n"; } else { reads += @" data."+ property.name + " = reader.Read<" + typeName + ">();\n"; } } } string namespace_string = ""; if (!String.IsNullOrEmpty(type.Namespace) && type.Namespace != "UnityEngine") { namespace_string = "using " + type.Namespace + ";"; } string create_instance = ""; // Used when creating a new instance of this object. if ((typeof(Component).IsAssignableFrom(type))) { create_instance = "GetOrCreate<" + GetTypeName(type) + ">()"; } else if ((typeof(ScriptableObject).IsAssignableFrom(type))) { create_instance = "ScriptableObject.CreateInstance<" + GetTypeName(type) + ">()"; } else if (type.IsEnum) { create_instance = "(" + GetTypeName(type) + ")reader.reader.ReadInt32()"; } else { create_instance = "new " + GetTypeName(type) + "()"; } // If it's a value type, use value type template. string template = type.IsValueType ? ES2EditorTemplates.GetTemplate("ES2ValueTypeTemplate") : ES2EditorTemplates.GetTemplate("ES2TypeTemplate"); string file; file = String.Format(template, file_prefix, GetES2TypeName(type), GetTypeName(type), writes, reads, namespace_string, create_instance); // Write the file. File.WriteAllText(GetPathToES2Type(types[selected]), file); CreateES2UserInit(type); AssetDatabase.Refresh(); }