static void drawCodeGenerators(CodeGeneratorConfig codeGeneratorConfig, Type[] codeGenerators)
        {
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Code Generators", EditorStyles.boldLabel);

            var enabledCodeGenerators = new HashSet<string>(codeGeneratorConfig.enabledCodeGenerators);

            var availableGeneratorNames = new HashSet<string>();
            foreach (var codeGenerator in codeGenerators) {
                availableGeneratorNames.Add(codeGenerator.Name);
                var isEnabled = enabledCodeGenerators.Contains(codeGenerator.Name);
                isEnabled = EditorGUILayout.Toggle(codeGenerator.Name, isEnabled);
                if (isEnabled) {
                    enabledCodeGenerators.Add(codeGenerator.Name);
                } else {
                    enabledCodeGenerators.Remove(codeGenerator.Name);
                }
            }

            foreach (var generatorName in codeGeneratorConfig.enabledCodeGenerators.ToArray()) {
                if (!availableGeneratorNames.Contains(generatorName)) {
                    enabledCodeGenerators.Remove(generatorName);
                }
            }

            var sortedCodeGenerators = enabledCodeGenerators.ToArray();
            Array.Sort(sortedCodeGenerators);
            codeGeneratorConfig.enabledCodeGenerators = sortedCodeGenerators;
        }
Пример #2
0
 static Dictionary<string, string> getSourceFiles(string path)
 {
     var config = new CodeGeneratorConfig(EntitasPreferencesEditor.LoadConfig());
     return Directory.GetFiles(path, "*.cs", SearchOption.AllDirectories)
     .Where(p => !p.Contains(config.generatedFolderPath))
     .ToDictionary(p => p, p => File.ReadAllText(p));
 }
Пример #3
0
        public static void Generate()
        {
            checkCanGenerate();

            Debug.Log("Generating...");

            var codeGenerators = GetCodeGenerators();
            var codeGeneratorNames = codeGenerators.Select(cg => cg.Name).ToArray();
            var config = new CodeGeneratorConfig(EntitasPreferences.LoadConfig(), codeGeneratorNames);

            var enabledCodeGeneratorNames = config.enabledCodeGenerators;
            var enabledCodeGenerators = codeGenerators
                .Where(type => enabledCodeGeneratorNames.Contains(type.Name))
                .Select(type => (ICodeGenerator)Activator.CreateInstance(type))
                .ToArray();

            var assembly = Assembly.GetAssembly(typeof(Entity));
            var generatedFiles = TypeReflectionCodeGenerator.Generate(assembly, config.pools, config.generatedFolderPath, enabledCodeGenerators);

            AssetDatabase.Refresh();

            foreach (var file in generatedFiles) {
                Debug.Log(file.generatorName + ": " + file.fileName);
            }

            var totalGeneratedFiles = generatedFiles.Select(file => file.fileName).Distinct().Count();
            Debug.Log("Generated " + totalGeneratedFiles + " files.");
        }
        static void drawPools(CodeGeneratorConfig codeGeneratorConfig) {
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Pools");
            
            var pools = new List<string>(codeGeneratorConfig.pools);
            if (pools.Count == 0) {
                EditorGUI.BeginDisabledGroup(true);
                EditorGUILayout.TextField("Pool");
                EditorGUI.EndDisabledGroup();
            }

            for (int i = 0; i < pools.Count; i++) {
                EditorGUILayout.BeginHorizontal();
                pools[i] = EditorGUILayout.TextField(pools[i]);
                if (GUILayout.Button("-", GUILayout.Width(19), GUILayout.Height(14))) {
                    pools[i] = string.Empty;
                }
                EditorGUILayout.EndHorizontal();
            }

            if (GUILayout.Button("Add pool")) {
                pools.Add("PoolName");
            }

            if (pools.Count == 0) {
                EditorGUILayout.HelpBox("You can optimize the memory footprint of entities by creating multiple pools. " +
                    "The code generator generates subclasses of PoolAttribute for each pool name. " +
                    "You can assign components to a specific pool with the generated attribute, e.g. [UI] or [MetaGame], " +
                    "otherwise they are assigned to the default pool.", MessageType.Info);
            }

            codeGeneratorConfig.pools = pools.ToArray();
        }
    void when_creating_config()
    {
        it["creates config from EntitasPreferencesConfig"] = () => {
            var config = new CodeGeneratorConfig(new EntitasPreferencesConfig(configString));

            config.generatedFolderPath.should_be("path/to/folder/");
            config.pools.should_be(new [] { "Core", "Meta", "UI" });
        };

        it["gets default values when keys dont exist"] = () => {
            var config = new CodeGeneratorConfig(new EntitasPreferencesConfig(string.Empty));
            config.generatedFolderPath.should_be("Assets/Generated/");
            config.pools.should_be_empty();
        };

        it["sets values"] = () => {
            var config = new CodeGeneratorConfig(new EntitasPreferencesConfig(configString));
            config.generatedFolderPath = "new/path/";
            config.pools = new [] { "Other1", "Other2" };

            config.generatedFolderPath.should_be("new/path/");
            config.pools.should_be(new [] { "Other1", "Other2" });
        };

        it["gets string"] = () => {
            var config = new CodeGeneratorConfig(new EntitasPreferencesConfig(configString));
            config.generatedFolderPath = "new/path/";
            config.pools = new [] { "Other1", "Other2" };

            config.ToString().should_be(
                "Entitas.Unity.CodeGenerator.GeneratedFolderPath = new/path/\n" +
                "Entitas.Unity.CodeGenerator.Pools = Other1,Other2\n");
        };

        it["gets string from empty config"] = () => {
            var config = new CodeGeneratorConfig(new EntitasPreferencesConfig(string.Empty));
            config.ToString().should_be(
                "Entitas.Unity.CodeGenerator.GeneratedFolderPath = Assets/Generated/\n" +
                "Entitas.Unity.CodeGenerator.Pools = \n");
        };

        it["removes empty pools"] = () => {
            const string configString = "Entitas.Unity.CodeGenerator.Pools = ,,Core,,UI,,";
            var config = new CodeGeneratorConfig(new EntitasPreferencesConfig(configString));
            config.pools.should_be(new [] { "Core", "UI" });
        };

        it["removes trailing comma in pools string"] = () => {
            var config = new CodeGeneratorConfig(new EntitasPreferencesConfig(string.Empty));
            config.pools = new [] { "Meta", string.Empty };
            config.ToString().should_be(
                "Entitas.Unity.CodeGenerator.GeneratedFolderPath = Assets/Generated/\n" +
                "Entitas.Unity.CodeGenerator.Pools = Meta\n"
            );
        };
    }
        public void Draw(EntitasPreferencesConfig config) {
            EditorGUILayout.BeginVertical(GUI.skin.box);
            EditorGUILayout.LabelField("CodeGenerator", EditorStyles.boldLabel);

            var codeGenerators = CodeGenerator.GetCodeGenerators();
            var codeGeneratorNames = codeGenerators.Select(cg => cg.Name).ToArray();
            var codeGeneratorConfig = new CodeGeneratorConfig(config, codeGeneratorNames);
            drawGeneratedFolderPath(codeGeneratorConfig);
            drawPools(codeGeneratorConfig);
            drawCodeGenerators(codeGeneratorConfig, codeGenerators);
            drawGenerateButton();

            EditorGUILayout.EndVertical();
        }
        public static void Generate()
        {
            var types = Assembly.GetAssembly(typeof(Entity)).GetTypes();
            var config = new CodeGeneratorConfig(EntitasPreferencesEditor.LoadConfig());

            var disabledCodeGenerators = config.disabledCodeGenerators;
            var codeGenerators = GetCodeGenerators()
                .Where(type => !disabledCodeGenerators.Contains(type.Name))
                .Select(type => (ICodeGenerator)Activator.CreateInstance(type))
                .ToArray();

            Entitas.CodeGenerator.CodeGenerator.Generate(types, config.pools, config.generatedFolderPath, codeGenerators);

            AssetDatabase.Refresh();
        }
        public void Draw(EntitasPreferencesConfig config)
        {
            var codeGeneratorConfig = new CodeGeneratorConfig(config);

            EditorGUILayout.BeginVertical(GUI.skin.box);
            EditorGUILayout.LabelField("CodeGenerator", EditorStyles.boldLabel);

            // Generated Folder
            codeGeneratorConfig.generatedFolderPath = EditorGUILayout.TextField("Generated Folder", codeGeneratorConfig.generatedFolderPath);

            // Pools
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Pools");
            EditorGUI.BeginDisabledGroup(true);
            EditorGUILayout.TextField("DefaultPool");
            EditorGUI.EndDisabledGroup();

            var pools = new List<string>(codeGeneratorConfig.pools);
            for (int i = 0; i < pools.Count; i++) {
                EditorGUILayout.BeginHorizontal();
                pools[i] = EditorGUILayout.TextField(pools[i]);
                if (GUILayout.Button("-", GUILayout.Width(19), GUILayout.Height(14))) {
                    pools[i] = string.Empty;
                }
                EditorGUILayout.EndHorizontal();
            }

            if (GUILayout.Button("Add pool")) {
                pools.Add("PoolName");
            }

            if (pools.Count == 0) {
                EditorGUILayout.HelpBox("You can optimize the memory footprint of entities by creating multiple pools. " +
                "The code generator generates subclasses of PoolAttribute for each pool name. " +
                "You can assign components to a specific pool with the generated attribute, e.g. [UI] or [MetaGame], " +
                "otherwise they are assigned to the default pool.", MessageType.Info);
            }

            codeGeneratorConfig.pools = pools.ToArray();

            EditorGUILayout.Space();
            if (GUILayout.Button("Generate")) {
                CodeGeneratorEditor.Generate();
            }

            EditorGUILayout.EndVertical();
        }
        public void Initialize(EntitasPreferencesConfig config) {
            _codeGenerators = UnityCodeGenerator.GetCodeGenerators();
            var codeGeneratorNames = _codeGenerators.Select(cg => cg.Name).ToArray();
            _codeGeneratorConfig = new CodeGeneratorConfig(config, codeGeneratorNames);

            _pools = new List<string>(_codeGeneratorConfig.pools);

            _poolList = new UnityEditorInternal.ReorderableList(_pools, typeof(string), true, true, true, true);
            _poolList.drawHeaderCallback = rect => EditorGUI.LabelField(rect, "Custom Pools");;
            _poolList.drawElementCallback = (rect, index, isActive, isFocused) => {
                rect.width -= 20;
                _pools[index] = EditorGUI.TextField(rect, _pools[index]);
            };
            _poolList.onAddCallback = list => list.list.Add("New Pool");
            _poolList.onCanRemoveCallback = list => true;
            _poolList.onChangedCallback = list => GUI.changed = true;
        }
Пример #10
0
        public static void Generate()
        {
            var types = Assembly.GetAssembly(typeof(Entity)).GetTypes();
            var config = new CodeGeneratorConfig(EntitasPreferencesEditor.LoadConfig());

            var codeGenerators = new ICodeGenerator[] {
                new ComponentExtensionsGenerator(),
                new IndicesLookupGenerator(),
                new PoolAttributeGenerator(),
                new PoolsGenerator(),
                new SystemExtensionsGenerator()
            };

            Entitas.CodeGenerator.CodeGenerator.Generate(types, config.pools, config.generatedFolderPath, codeGenerators);

            AssetDatabase.Refresh();
        }
Пример #11
0
        public static void Generate() {
            assertCanGenerate();

            var codeGenerators = GetCodeGenerators();
            var codeGeneratorNames = codeGenerators.Select(cg => cg.Name).ToArray();
            var config = new CodeGeneratorConfig(EntitasPreferences.LoadConfig(), codeGeneratorNames);

            var enabledCodeGeneratorNames = config.enabledCodeGenerators;
            var enabledCodeGenerators = codeGenerators
                .Where(type => enabledCodeGeneratorNames.Contains(type.Name))
                .Select(type => (ICodeGenerator)Activator.CreateInstance(type))
                .ToArray();

            var assembly = Assembly.GetAssembly(typeof(Entity));
            TypeReflectionCodeGenerator.Generate(assembly, config.pools, config.generatedFolderPath, enabledCodeGenerators);

            AssetDatabase.Refresh();
        }
Пример #12
0
        public static void Generate()
        {
            var types  = Assembly.GetAssembly(typeof(Entity)).GetTypes();
            var config = new CodeGeneratorConfig(EntitasPreferencesEditor.LoadConfig());

            var componentCodeGenerators = new IComponentCodeGenerator [] {
                new IndicesLookupGenerator(),
                new ComponentExtensionsGenerator()
            };

            var poolCodeGenerators = new IPoolCodeGenerator [] {
                new PoolAttributeGenerator()
            };

            Entitas.CodeGenerator.CodeGenerator.Generate(types, config.pools, config.generatedFolderPath,
                                                         componentCodeGenerators, poolCodeGenerators);

            AssetDatabase.Refresh();
        }
Пример #13
0
        public static void Generate()
        {
            assertCanGenerate();

            var types              = Assembly.GetAssembly(typeof(Entity)).GetTypes();
            var codeGenerators     = GetCodeGenerators();
            var codeGeneratorNames = codeGenerators.Select(cg => cg.Name).ToArray();
            var config             = new CodeGeneratorConfig(EntitasPreferences.LoadConfig(), codeGeneratorNames);

            var enabledCodeGeneratorNames = config.enabledCodeGenerators;
            var enabledCodeGenerators     = codeGenerators
                                            .Where(type => enabledCodeGeneratorNames.Contains(type.Name))
                                            .Select(type => (ICodeGenerator)Activator.CreateInstance(type))
                                            .ToArray();

            Entitas.CodeGenerator.CodeGenerator.Generate(types, config.pools, config.generatedFolderPath, enabledCodeGenerators);

            AssetDatabase.Refresh();
        }
        public override void Initialize(EntitasPreferencesConfig config) {
            var enabledDataProviderNames = initPhase<ICodeGeneratorDataProvider>(out _availableDataProviderTypes, out _availableDataProviderNames);
            var enabledGeneratorNames = initPhase<ICodeGenerator>(out _availableGeneratorTypes, out _availableGeneratorNames);
            var enabledPostProcessorNames = initPhase<ICodeGenFilePostProcessor>(out _availablePostProcessorTypes, out _availablePostProcessorNames);

            _codeGeneratorConfig = new CodeGeneratorConfig(config, enabledDataProviderNames, enabledGeneratorNames, enabledPostProcessorNames);

            _contexts = new List<string>(_codeGeneratorConfig.contexts);

            _contextList = new UnityEditorInternal.ReorderableList(_contexts, typeof(string), true, true, true, true);
            _contextList.drawHeaderCallback = rect => EditorGUI.LabelField(rect, "Contexts");
            _contextList.drawElementCallback = (rect, index, isActive, isFocused) => {
                rect.width -= 20;
                _contexts[index] = EditorGUI.TextField(rect, _contexts[index]);
            };
            _contextList.onAddCallback = list => list.list.Add("New Context");
            _contextList.onCanRemoveCallback = list => list.count > 1;
            _contextList.onChangedCallback = list => GUI.changed = true;
        }
        public void Initialize(EntitasPreferencesConfig config)
        {
            _codeGenerators = UnityCodeGenerator.GetCodeGenerators();
            var codeGeneratorNames = _codeGenerators.Select(cg => cg.Name).ToArray();

            _codeGeneratorConfig = new CodeGeneratorConfig(config, codeGeneratorNames);

            _pools = new List <string>(_codeGeneratorConfig.pools);

            _poolList = new UnityEditorInternal.ReorderableList(_pools, typeof(string), true, true, true, true);
            _poolList.drawHeaderCallback  = rect => EditorGUI.LabelField(rect, "Custom Pools");
            _poolList.drawElementCallback = (rect, index, isActive, isFocused) => {
                rect.width   -= 20;
                _pools[index] = EditorGUI.TextField(rect, _pools[index]);
            };
            _poolList.onAddCallback       = list => list.list.Add("New Pool");
            _poolList.onCanRemoveCallback = list => true;
            _poolList.onChangedCallback   = list => GUI.changed = true;
        }
        void drawPools(CodeGeneratorConfig codeGeneratorConfig)
        {
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Pools");

            var pools = new List <string>(codeGeneratorConfig.pools);

            if (pools.Count == 0)
            {
                EditorGUI.BeginDisabledGroup(true);
                EditorGUILayout.TextField("Pool");
                EditorGUI.EndDisabledGroup();
            }

            for (int i = 0; i < pools.Count; i++)
            {
                EditorGUILayout.BeginHorizontal();
                pools[i] = EditorGUILayout.TextField(pools[i]);
                if (GUILayout.Button("-", GUILayout.Width(19), GUILayout.Height(14)))
                {
                    pools[i] = string.Empty;
                }
                EditorGUILayout.EndHorizontal();
            }

            if (GUILayout.Button("Add pool"))
            {
                pools.Add("PoolName");
            }

            if (pools.Count == 0)
            {
                EditorGUILayout.HelpBox("You can optimize the memory footprint of entities by creating multiple pools. " +
                                        "The code generator generates subclasses of PoolAttribute for each pool name. " +
                                        "You can assign components to a specific pool with the generated attribute, e.g. [UI] or [MetaGame], " +
                                        "otherwise they are assigned to the default pool.", MessageType.Info);
            }

            codeGeneratorConfig.pools = pools.ToArray();
        }
Пример #17
0
        static void drawCodeGenerators(CodeGeneratorConfig codeGeneratorConfig, Type[] codeGenerators)
        {
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Code Generators", EditorStyles.boldLabel);

            var enabledCodeGenerators = new HashSet <string>(codeGeneratorConfig.enabledCodeGenerators);

            var availableGeneratorNames = new HashSet <string>();

            foreach (var codeGenerator in codeGenerators)
            {
                availableGeneratorNames.Add(codeGenerator.Name);
                var isEnabled = enabledCodeGenerators.Contains(codeGenerator.Name);
                isEnabled = EditorGUILayout.Toggle(codeGenerator.Name, isEnabled);
                if (isEnabled)
                {
                    enabledCodeGenerators.Add(codeGenerator.Name);
                }
                else
                {
                    enabledCodeGenerators.Remove(codeGenerator.Name);
                }
            }

            foreach (var generatorName in codeGeneratorConfig.enabledCodeGenerators.ToArray())
            {
                if (!availableGeneratorNames.Contains(generatorName))
                {
                    enabledCodeGenerators.Remove(generatorName);
                }
            }

            var sortedCodeGenerators = enabledCodeGenerators.ToArray();

            Array.Sort(sortedCodeGenerators);
            codeGeneratorConfig.enabledCodeGenerators = sortedCodeGenerators;
        }
        void drawCodeGenerators(CodeGeneratorConfig codeGeneratorConfig)
        {
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Code Generators", EditorStyles.boldLabel);

            var codeGenerators         = CodeGeneratorEditor.GetCodeGenerators();
            var disabledCodeGenerators = new HashSet <string>(codeGeneratorConfig.disabledCodeGenerators);

            foreach (var codeGenerator in codeGenerators)
            {
                var isEnabled = !disabledCodeGenerators.Contains(codeGenerator.Name);
                isEnabled = EditorGUILayout.Toggle(codeGenerator.Name, isEnabled);
                if (isEnabled)
                {
                    disabledCodeGenerators.Remove(codeGenerator.Name);
                }
                else
                {
                    disabledCodeGenerators.Add(codeGenerator.Name);
                }
            }

            codeGeneratorConfig.disabledCodeGenerators = disabledCodeGenerators.ToArray();
        }
 static void drawGeneratedFolderPath(CodeGeneratorConfig codeGeneratorConfig) {
     codeGeneratorConfig.generatedFolderPath = EditorGUILayout.TextField("Generated Folder", codeGeneratorConfig.generatedFolderPath);
 }
 void drawGeneratedFolderPath(CodeGeneratorConfig codeGeneratorConfig)
 {
     codeGeneratorConfig.generatedFolderPath = EditorGUILayout.TextField("Generated Folder", codeGeneratorConfig.generatedFolderPath);
 }