Beispiel #1
0
        public static void GeneratePropertyDefinitionsForAllTypes()
        {
            var entityTypeDefinitions = ScriptableObjectEditorUtils.FindAllOfType <EntityTypeDefinition>();

            foreach (var typeDefinition in entityTypeDefinitions)
            {
                GenerateGroupDefinitionsForType(typeDefinition);
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
        public static void SetupGroupDefinitions()
        {
            var existingGroupDefinitions = ScriptableObjectEditorUtils.FindAllOfType <EntityTypeDefinition>();

            foreach (var groupDefinition in existingGroupDefinitions)
            {
                if (groupDefinition.EntityType.TryToGroup(out EntityProperty entityProperty))
                {
                    groupDefinition.SetGroup(entityProperty);
                }
            }
        }
        public static void CleanupUnusedDefinitions()
        {
            var definitionNames          = Enum.GetNames(typeof(EntityProperty));
            var existingGroupDefinitions = ScriptableObjectEditorUtils.FindAllOfType <EntityPropertyDefinition>();

            if (definitionNames.Length != existingGroupDefinitions.Count)
            {
                foreach (var definition in existingGroupDefinitions)
                {
                    if (!definitionNames.Contains(definition.EntityType.ToString()))
                    {
                        AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(definition));
                    }
                }
            }
            AssetDatabase.Refresh();
        }
        public static void GenerateDefinitions()
        {
            var addedDefinitions         = new List <string>();
            var existingGroupDefinitions = ScriptableObjectEditorUtils.FindAllOfType <EntityPropertyDefinition>();

            var settings = ScriptableObjectEditorUtils.FindFirstOfType <GdOrganizerSettings>();

            var propertyDefinitionPath = Path.Combine(settings.DefinitionsRootPath, "EntityPropertyDefinitions");

            var definitionNames = Enum.GetNames(typeof(EntityProperty));

            foreach (var definitionName in definitionNames)
            {
                if (addedDefinitions.Contains(definitionName))
                {
                    continue;
                }

                if (!existingGroupDefinitions.Exists(x => x.EntityType.ToString().Equals(definitionName)))
                {
                    // create SO
                    var definitionInstance = ScriptableObject.CreateInstance <EntityPropertyDefinition>();
                    var groupType          = Enum.Parse(typeof(EntityType), definitionName);

                    definitionInstance.SetEntityType((EntityType)groupType);
                    if (!Directory.Exists(propertyDefinitionPath))
                    {
                        Directory.CreateDirectory(propertyDefinitionPath);
                    }

                    string fullPath = $"{Path.Combine(propertyDefinitionPath, definitionName)}.asset";
                    if (File.Exists(fullPath))
                    {
                        return;
                    }
                    AssetDatabase.CreateAsset(definitionInstance, fullPath);
                }
                addedDefinitions.Add(definitionName);
            }

            SetupGroupDefinitions();

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
Beispiel #5
0
        public static void OnCompileScripts()
        {
            if (!EditorPrefs.GetBool(RegenerationTriggeredKey))
            {
                return;
            }
            var entityGroupConfig = ScriptableObjectEditorUtils.FindFirstOfType <EntityPropertyConfig>();
            var entityTypeConfig  = ScriptableObjectEditorUtils.FindFirstOfType <EntityTypeConfig>();

            EntityTypeGenerator.CleanupUnusedDefinitions();
            EntityPropertyGenerator.CleanupUnusedDefinitions();

            EntityTypeGenerator.GenerateDefinitions();
            EntityPropertyGenerator.GenerateDefinitions();

            entityTypeConfig.EntityTypeDefinitions   = ScriptableObjectEditorUtils.FindAllOfType <EntityTypeDefinition>();
            entityGroupConfig.EntityGroupDefinitions = ScriptableObjectEditorUtils.FindAllOfType <EntityPropertyDefinition>();

            EditorPrefs.SetBool(RegenerationTriggeredKey, false);
        }
        public static void GenerateLookup()
        {
            string equalityCompararContent = "";
            string typePropLookupContent   = "";
            string propTypeLookupContent   = "";

            Dictionary <EntityProperty, List <EntityType> > PropertyTypeMap = new Dictionary <EntityProperty, List <EntityType> >();
            Dictionary <EntityType, List <EntityProperty> > TypePropertyMap = new Dictionary <EntityType, List <EntityProperty> >();

            string fileContent            = "";
            string typePropertyLookupName = "TypePropertiesLookup";
            string propertyTypeLookupName = "PropertyTypeLookup";
            var    path = "Assets/Modules/O.M.A.Games/GDOrganizer/Generated";

            var existingTypeDefinitions     = ScriptableObjectEditorUtils.FindAllOfType <EntityTypeDefinition>();
            var existingPropertyDefinitions = ScriptableObjectEditorUtils.FindAllOfType <EntityPropertyDefinition>();

            foreach (var propertyDefinition in existingPropertyDefinitions)
            {
                if (Enum.TryParse(propertyDefinition.EntityType.ToString(), out EntityProperty entityProperty))
                {
                    PropertyTypeMap.Add(entityProperty, new List <EntityType>());
                }
                else
                {
                    Debug.LogError("Cannot cast " + propertyDefinition);
                }
            }
            foreach (var typeDefinition in existingTypeDefinitions)
            {
                var properties = new List <EntityProperty>();
                foreach (var property in typeDefinition.EntityProperties.Properties)
                {
                    properties.Add(property);
                    if (PropertyTypeMap.ContainsKey(property))
                    {
                        PropertyTypeMap[property].Add(typeDefinition.EntityType);
                    }
                    else
                    {
                        Debug.LogError("Cannot find property " + property);
                    }
                }
                TypePropertyMap.Add(typeDefinition.EntityType, properties);
            }


            var settings = ScriptableObjectEditorUtils.FindFirstOfType <GdOrganizerSettings>();
            var fullPath = Path.Combine(path, GeneratedFileName);

            equalityCompararContent += GenerateEqualityComparer <EntityType>();
            equalityCompararContent += GenerateEqualityComparer <EntityProperty>();

            typePropLookupContent += GenerateLookupMap(typePropertyLookupName, TypePropertyMap);
            propTypeLookupContent += GenerateLookupMap(propertyTypeLookupName, PropertyTypeMap);

            AppendContent(CodeGenerationHelper.GetLine("using System;"), ref fileContent);
            AppendContent(CodeGenerationHelper.GetLine("using System.Collections.Generic;"), ref fileContent);
            AppendContent(CodeGenerationHelper.GetLine("using System.Linq;"), ref fileContent);
            AppendContent(CodeGenerationHelper.GetLine(""), ref fileContent);
            AppendContent(CodeGenerationHelper.GetLine("namespace O.M.A.Games.GDOrganizer.Generated"), ref fileContent);
            AppendContent(CodeGenerationHelper.GetLine("{"), ref fileContent);
            AppendContent(CodeGenerationHelper.GetLine("public static class EntityLookup", 1), ref fileContent);
            AppendContent(CodeGenerationHelper.GetLine("{", 1), ref fileContent);
            AppendContent(CodeGenerationHelper.GetLine(equalityCompararContent), ref fileContent);

            AppendContent(CodeGenerationHelper.GetLine(GenerateHasLookupMethod <EntityType, EntityProperty>("HasProperty", typePropertyLookupName), 2), ref fileContent);
            AppendContent(CodeGenerationHelper.GetLine(GenerateHasLookupMethod <EntityProperty, EntityType>("HasType", propertyTypeLookupName), 2), ref fileContent);
            AppendContent(CodeGenerationHelper.GetLine(GenerateGetLookupMethod <EntityType, EntityProperty>("GetProperties", typePropertyLookupName), 2), ref fileContent);
            AppendContent(CodeGenerationHelper.GetLine(GenerateGetLookupMethod <EntityProperty, EntityType>("GetTypes", propertyTypeLookupName), 2), ref fileContent);

            AppendContent(CodeGenerationHelper.GetLine(typePropLookupContent, 2), ref fileContent);
            AppendContent(CodeGenerationHelper.GetLine(propTypeLookupContent, 2), ref fileContent);
            AppendContent(CodeGenerationHelper.GetLine("}", 1), ref fileContent);
            AppendContent(CodeGenerationHelper.GetLine("}"), ref fileContent);

            if (!Directory.Exists(fullPath))
            {
                Directory.CreateDirectory(path);
            }
            File.WriteAllText(fullPath, fileContent);

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }