public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            AttributeFormat output = new AttributeFormat();
            JToken          obj    = JToken.Load(reader);

            output.type       = serializer.Deserialize <string>(obj["type"].CreateReader());
            output.nameToType = new Dictionary <string, AttributeType>();
            JObject attributes = JObject.Load(obj["attributes"].CreateReader());

            foreach (JProperty property in attributes.Properties())
            {
                output.nameToType.Add(property.Name, serializer.Deserialize <AttributeType>(property.Value.CreateReader()));
            }

            return(output);
        }
Esempio n. 2
0
        private static TypeBuilder GetTypeBuilder(AttributeFormat format, Type parentClass)
        {
            string       typeSignature = format.type;
            AssemblyName assemblyName  = new AssemblyName(typeSignature);

            System.Reflection.Emit.AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
            ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MainModule");
            TypeBuilder   typeBuilder   = moduleBuilder.DefineType(
                typeSignature,
                TypeAttributes.Public |
                TypeAttributes.Class |
                TypeAttributes.AutoClass |
                TypeAttributes.AnsiClass |
                TypeAttributes.BeforeFieldInit |
                TypeAttributes.AutoLayout,
                parentClass);

            return(typeBuilder);
        }
Esempio n. 3
0
        //https://forum.unity.com/threads/how-to-create-c-files-as-editor-extension.434606/
        public static void CreateClassFile(AttributeFormat format, Type parent, bool force = false)
        {
            string[] guids = Selection.assetGUIDs;
            if (guids.Length == 0)
            {
                return;
            }

            string classname = char.ToUpper(format.type[0]) + format.type.Substring(1);

            if (Type.GetType(classname) != null && !force)
            {
                throw new System.Exception("type already exists. Cannot create file");
            }

            string template     = File.ReadAllText(Application.streamingAssetsPath + "/ClassTemplate.txt");
            string relativePath = "/Scripts/Model/" + classname + ".cs";

            template = template.Replace("##CLASSNAME##", format.type);
            string properties = "";

            foreach (string propertyName in format.nameToType.Keys)
            {
                AddPropertyToString(ref properties, propertyName, Convert.ToType(format.nameToType[propertyName]));
            }
            template = template.Replace("##PROPERTIES##", properties);
            template = template.Replace("Int32", "int");
            string errors = "Creating csharp files for card formats had errors: \n\n";

            if (ValidateString(template, ref errors))
            {
                File.WriteAllText(Application.dataPath + relativePath, template);
                AssetDatabase.ImportAsset("Assets" + relativePath);
            }
            else
            {
                throw new System.Exception(errors);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// creates a class  from an Attribute format -> this was a "Fingerübung" of reading and adapting questions & documentaiton
        /// </summary>
        /// <param name="format"></param>
        /// <param name="parent"></param>
        /// <returns></returns>
        public static Type CreateClass(AttributeFormat format, Type parent)
        {
            TypeBuilder typeBuilder = GetTypeBuilder(format, parent);
            //ConstructorBuilder constructor = typeBuilder.DefineConstructor(
            //    MethodAttributes.Public |
            //    MethodAttributes.SpecialName |
            //    MethodAttributes.RTSpecialName,
            //    CallingConventions.HasThis,
            //    format.GetTypes());

            ConstructorBuilder constructor = typeBuilder.DefineDefaultConstructor(
                MethodAttributes.Public |
                MethodAttributes.SpecialName |
                MethodAttributes.RTSpecialName);

            foreach (string attributeName in format.nameToType.Keys)
            {
                CreateProperty(typeBuilder, attributeName, Convert.ToType(format.nameToType[attributeName]));
            }

            Type objectType = typeBuilder.CreateType();

            return(objectType);
        }