diff --git a/Addons/Entitas.CodeGeneration.CodeGenerator.CLI/Program.cs b/Addons/Entitas.CodeGeneration.CodeGenerator.CLI/Program.cs index 7481cf217..a2a9a6f28 100644 --- a/Addons/Entitas.CodeGeneration.CodeGenerator.CLI/Program.cs +++ b/Addons/Entitas.CodeGeneration.CodeGenerator.CLI/Program.cs @@ -170,7 +170,7 @@ static void fixConfig() { var fileContent = File.ReadAllText(Preferences.configPath); var properties = new Properties(fileContent); - foreach(var key in getMissingKey(properties)) { + foreach(var key in getMissingKeys(properties)) { _logger.Info("Add missing key: '" + key + "' ? (y / n)"); if(getUserDecision()) { properties[key] = string.Empty; @@ -209,15 +209,39 @@ static void status() { _logger.Debug(config.ToString()); - foreach(var key in getUnusedKeys(properties)) { + Type[] types = null; + string[] configurableKeys = null; + + try { + types = CodeGeneratorUtil.LoadTypesFromCodeGeneratorAssemblies(); + configurableKeys = getConfigurableKeys( + CodeGeneratorUtil.GetUsed(types, config.dataProviders), + CodeGeneratorUtil.GetUsed(types, config.codeGenerators), + CodeGeneratorUtil.GetUsed(types, config.postProcessors) + ); + } catch(Exception ex) { + foreach(var key in getUnusedKeys(properties)) { + _logger.Info("Unused key: " + key); + } + + foreach(var key in getMissingKeys(properties)) { + _logger.Warn("Missing key: " + key); + } + + throw ex; + } + + foreach(var key in getUnusedKeys(properties).Where(key => !configurableKeys.Contains(key))) { _logger.Info("Unused key: " + key); } - foreach(var key in getMissingKey(properties)) { + foreach(var key in getMissingKeys(properties)) { _logger.Warn("Missing key: " + key); } - var types = CodeGeneratorUtil.LoadTypesFromCodeGeneratorAssemblies(); + foreach(var key in configurableKeys.Where(key => !properties.HasKey(key))) { + _logger.Warn("Missing key: " + key); + } printUnavailable(CodeGeneratorUtil.GetUnavailable(types, config.dataProviders)); printUnavailable(CodeGeneratorUtil.GetUnavailable(types, config.codeGenerators)); @@ -237,12 +261,22 @@ static string[] getUnusedKeys(Properties properties) { .ToArray(); } - static string[] getMissingKey(Properties properties) { + static string[] getMissingKeys(Properties properties) { return CodeGeneratorConfig.keys .Where(key => !properties.HasKey(key)) .ToArray(); } + static string[] getConfigurableKeys(ICodeGeneratorDataProvider[] dataProviders, ICodeGenerator[] codeGenerators, ICodeGenFilePostProcessor[] postProcessors) { + return dataProviders.OfType() + .Concat(codeGenerators.OfType()) + .Concat(postProcessors.OfType()) + .Select(instance => instance.defaultProperties) + .SelectMany(props => props.Keys) + .Distinct() + .ToArray(); + } + static void scanDlls() { if(File.Exists(Preferences.configPath)) { printTypes(CodeGeneratorUtil.LoadTypesFromCodeGeneratorAssemblies()); diff --git a/Addons/Entitas.CodeGeneration.CodeGenerator/Entitas.CodeGeneration.CodeGenerator/CodeGenerator/CodeGeneratorUtil.cs b/Addons/Entitas.CodeGeneration.CodeGenerator/Entitas.CodeGeneration.CodeGenerator/CodeGenerator/CodeGeneratorUtil.cs index ac18d92c2..c7df479fa 100644 --- a/Addons/Entitas.CodeGeneration.CodeGenerator/Entitas.CodeGeneration.CodeGenerator/CodeGenerator/CodeGeneratorUtil.cs +++ b/Addons/Entitas.CodeGeneration.CodeGenerator/Entitas.CodeGeneration.CodeGenerator/CodeGenerator/CodeGeneratorUtil.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using Entitas.Utils; @@ -84,5 +84,11 @@ public static string[] GetUnavailable(Type[] types, string[] enabledTypeNames .Where(typeName => !typeNames.Contains(typeName)) .ToArray(); } + + public static T[] GetUsed(Type[] types, string[] enabledTypeNames) where T : ICodeGeneratorInterface { + return GetOrderedInstances(types) + .Where(instance => enabledTypeNames.Contains(instance.GetType().ToCompilableString())) + .ToArray(); + } } } diff --git a/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/ComponentContextGenerator.cs b/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/ComponentContextGenerator.cs index f7ddbe209..c7f07dd7c 100644 --- a/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/ComponentContextGenerator.cs +++ b/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/ComponentContextGenerator.cs @@ -1,16 +1,37 @@ -using System.IO; +using System.Collections.Generic; +using System.IO; using System.Linq; using Entitas.Utils; namespace Entitas.CodeGeneration.Plugins { - public class ComponentContextGenerator : ICodeGenerator { + public class ComponentContextGenerator : ICodeGenerator, IConfigurable { public string name { get { return "Component (Context API)"; } } public int priority { get { return 0; } } public bool isEnabledByDefault { get { return true; } } public bool runInDryMode { get { return true; } } + const string IGNORE_NAMESPACES_KEY = "Entitas.CodeGeneration.Plugins.IgnoreNamespaces"; + + public Dictionary defaultProperties { + get { return new Dictionary { { IGNORE_NAMESPACES_KEY, "false" } }; } + } + + bool ignoreNamespaces { get { return properties[IGNORE_NAMESPACES_KEY] == "true"; } } + + Dictionary properties { + get { + if(_properties == null) { + _properties = defaultProperties; + } + + return _properties; + } + } + + Dictionary _properties; + const string STANDARD_COMPONENT_TEMPLATE = @"public partial class ${ContextName}Context { @@ -70,7 +91,11 @@ public bool ${prefixedComponentName} { } "; - public CodeGenFile[] Generate(CodeGeneratorData[] data) { + public void Configure(Dictionary properties) { + _properties = properties; + } + + public CodeGenFile[] Generate(CodeGeneratorData[] data) { return data .OfType() .Where(d => d.ShouldGenerateMethods()) @@ -86,8 +111,8 @@ CodeGenFile[] generateExtensions(ComponentData data) { } CodeGenFile generateExtension(string contextName, ComponentData data) { - var memberData = data.GetMemberData(); - var componentName = data.GetFullTypeName().ToComponentName(); + var memberData = data.GetMemberData(); + var componentName = data.GetFullTypeName().ToComponentName(ignoreNamespaces); var template = memberData.Length == 0 ? FLAG_COMPONENT_TEMPLATE : STANDARD_COMPONENT_TEMPLATE; diff --git a/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/ComponentEntityGenerator.cs b/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/ComponentEntityGenerator.cs index 15df69b05..5e1b11bd1 100644 --- a/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/ComponentEntityGenerator.cs +++ b/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/ComponentEntityGenerator.cs @@ -1,16 +1,37 @@ +using System.Collections.Generic; using System.IO; using System.Linq; using Entitas.Utils; namespace Entitas.CodeGeneration.Plugins { - public class ComponentEntityGenerator : ICodeGenerator { + public class ComponentEntityGenerator : ICodeGenerator, IConfigurable { public string name { get { return "Component (Entity API)"; } } public int priority { get { return 0; } } public bool isEnabledByDefault { get { return true; } } public bool runInDryMode { get { return true; } } + const string IGNORE_NAMESPACES_KEY = "Entitas.CodeGeneration.Plugins.IgnoreNamespaces"; + + public Dictionary defaultProperties { + get { return new Dictionary { { IGNORE_NAMESPACES_KEY, "false" } }; } + } + + bool ignoreNamespaces { get { return properties[IGNORE_NAMESPACES_KEY] == "true"; } } + + Dictionary properties { + get { + if(_properties == null) { + _properties = defaultProperties; + } + + return _properties; + } + } + + Dictionary _properties; + const string STANDARD_COMPONENT_TEMPLATE = @"public partial class ${ContextName}Entity { @@ -63,7 +84,11 @@ public bool ${prefixedName} { } "; - public CodeGenFile[] Generate(CodeGeneratorData[] data) { + public void Configure(Dictionary properties) { + _properties = properties; + } + + public CodeGenFile[] Generate(CodeGeneratorData[] data) { return data .OfType() .Where(d => d.ShouldGenerateMethods()) @@ -77,8 +102,8 @@ CodeGenFile[] generateExtensions(ComponentData data) { .ToArray(); } - CodeGenFile generateExtension(string contextName, ComponentData data) { - var componentName = data.GetFullTypeName().ToComponentName(); + CodeGenFile generateExtension(string contextName, ComponentData data) { + var componentName = data.GetFullTypeName().ToComponentName(ignoreNamespaces); var index = contextName + ComponentsLookupGenerator.COMPONENTS_LOOKUP + "." + componentName; var memberData = data.GetMemberData(); var template = memberData.Length == 0 diff --git a/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/ComponentMatcherGenerator.cs b/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/ComponentMatcherGenerator.cs index 7ca8b7a23..77142fdf0 100644 --- a/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/ComponentMatcherGenerator.cs +++ b/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/ComponentMatcherGenerator.cs @@ -1,15 +1,36 @@ -using System.IO; +using System.Collections.Generic; +using System.IO; using System.Linq; namespace Entitas.CodeGeneration.Plugins { - public class ComponentMatcherGenerator : ICodeGenerator { + public class ComponentMatcherGenerator : ICodeGenerator, IConfigurable { public string name { get { return "Component (Matcher API)"; } } public int priority { get { return 0; } } public bool isEnabledByDefault { get { return true; } } public bool runInDryMode { get { return true; } } + const string IGNORE_NAMESPACES_KEY = "Entitas.CodeGeneration.Plugins.IgnoreNamespaces"; + + public Dictionary defaultProperties { + get { return new Dictionary { { IGNORE_NAMESPACES_KEY, "false" } }; } + } + + bool ignoreNamespaces { get { return properties[IGNORE_NAMESPACES_KEY] == "true"; } } + + Dictionary properties { + get { + if(_properties == null) { + _properties = defaultProperties; + } + + return _properties; + } + } + + Dictionary _properties; + const string STANDARD_COMPONENT_TEMPLATE = @"public sealed partial class ${ContextName}Matcher { @@ -29,6 +50,10 @@ public static Entitas.IMatcher<${ContextName}Entity> ${ComponentName} { } "; + public void Configure(Dictionary properties) { + _properties = properties; + } + public CodeGenFile[] Generate(CodeGeneratorData[] data) { return data .OfType() @@ -44,7 +69,7 @@ CodeGenFile[] generateMatcher(ComponentData data) { } CodeGenFile generateMatcher(string contextName, ComponentData data) { - var componentName = data.GetFullTypeName().ToComponentName(); + var componentName = data.GetFullTypeName().ToComponentName(ignoreNamespaces); var index = contextName + ComponentsLookupGenerator.COMPONENTS_LOOKUP + "." + componentName; var componentNames = contextName + ComponentsLookupGenerator.COMPONENTS_LOOKUP + ".componentNames"; diff --git a/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/ComponentsLookupGenerator.cs b/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/ComponentsLookupGenerator.cs index 18a410121..4d155c034 100644 --- a/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/ComponentsLookupGenerator.cs +++ b/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/ComponentsLookupGenerator.cs @@ -1,16 +1,36 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.IO; namespace Entitas.CodeGeneration.Plugins { - public class ComponentsLookupGenerator : ICodeGenerator { + public class ComponentsLookupGenerator : ICodeGenerator, IConfigurable { public string name { get { return "Components Lookup"; } } public int priority { get { return 0; } } public bool isEnabledByDefault { get { return true; } } public bool runInDryMode { get { return true; } } + const string IGNORE_NAMESPACES_KEY = "Entitas.CodeGeneration.Plugins.IgnoreNamespaces"; + + public Dictionary defaultProperties { + get { return new Dictionary { { IGNORE_NAMESPACES_KEY, "false" } }; } + } + + bool ignoreNamespaces { get { return properties[IGNORE_NAMESPACES_KEY] == "true"; } } + + Dictionary properties { + get { + if(_properties == null) { + _properties = defaultProperties; + } + + return _properties; + } + } + + Dictionary _properties; + public const string COMPONENTS_LOOKUP = "ComponentsLookup"; const string COMPONENTS_LOOKUP_TEMPLATE = @@ -42,6 +62,10 @@ public class ComponentsLookupGenerator : ICodeGenerator { const string COMPONENT_TYPES_TEMPLATE = @" typeof(${ComponentType})"; + public void Configure(Dictionary properties) { + _properties = properties; + } + public CodeGenFile[] Generate(CodeGeneratorData[] data) { var files = new List(); @@ -105,7 +129,7 @@ CodeGenFile generateComponentsLookupClass(string contextName, ComponentData[] da var componentConstants = string.Join("\n", data .Select((d, index) => { return COMPONENT_CONSTANTS_TEMPLATE - .Replace("${ComponentName}", d.GetFullTypeName().ToComponentName()) + .Replace("${ComponentName}", d.GetFullTypeName().ToComponentName(ignoreNamespaces)) .Replace("${Index}", index.ToString()); }).ToArray()); @@ -114,7 +138,7 @@ CodeGenFile generateComponentsLookupClass(string contextName, ComponentData[] da var componentNames = string.Join(",\n", data .Select(d => COMPONENT_NAMES_TEMPLATE - .Replace("${ComponentName}", d.GetFullTypeName().ToComponentName()) + .Replace("${ComponentName}", d.GetFullTypeName().ToComponentName(ignoreNamespaces)) ).ToArray()); var componentTypes = string.Join(",\n", data diff --git a/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/EntityIndexGenerator.cs b/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/EntityIndexGenerator.cs index a5d067396..ce8704f9d 100644 --- a/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/EntityIndexGenerator.cs +++ b/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/CodeGenerators/EntityIndexGenerator.cs @@ -1,16 +1,36 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using Entitas.Utils; namespace Entitas.CodeGeneration.Plugins { - public class EntityIndexGenerator : ICodeGenerator { + public class EntityIndexGenerator : ICodeGenerator, IConfigurable { public string name { get { return "Entity Index"; } } public int priority { get { return 0; } } public bool isEnabledByDefault { get { return true; } } public bool runInDryMode { get { return true; } } + const string IGNORE_NAMESPACES_KEY = "Entitas.CodeGeneration.Plugins.IgnoreNamespaces"; + + public Dictionary defaultProperties { + get { return new Dictionary { { IGNORE_NAMESPACES_KEY, "false" } }; } + } + + bool ignoreNamespaces { get { return properties[IGNORE_NAMESPACES_KEY] == "true"; } } + + Dictionary properties { + get { + if(_properties == null) { + _properties = defaultProperties; + } + + return _properties; + } + } + + Dictionary _properties; + const string CLASS_TEMPLATE = @"public partial class Contexts { @@ -54,6 +74,10 @@ public static class ContextsExtensions { } "; + public void Configure(Dictionary properties) { + _properties = properties; + } + public CodeGenFile[] Generate(CodeGeneratorData[] data) { var entityIndexData = data.OfType().ToArray(); return entityIndexData.Length == 0 @@ -116,7 +140,7 @@ string generateMethods(EntityIndexData data, string contextName) { .Replace("${KeyType}", data.GetKeyType()) .Replace("${ComponentType}", data.GetComponentType()) .Replace("${MemberName}", data.GetMemberName()) - .Replace("${componentName}", data.GetComponentType().ToComponentName().LowercaseFirst()); + .Replace("${componentName}", data.GetComponentType().ToComponentName(ignoreNamespaces).LowercaseFirst()); } string generateGetMethods(EntityIndexData data) { diff --git a/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/DataProviders/Components/ComponentData.cs b/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/DataProviders/Components/ComponentData.cs index 1c68293f8..8715b6c57 100644 --- a/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/DataProviders/Components/ComponentData.cs +++ b/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/DataProviders/Components/ComponentData.cs @@ -7,22 +7,10 @@ public class ComponentData : CodeGeneratorData { public static class ComponentDataExtension { - public static string ToComponentName(this string fullTypeName) { - return shouldIgnoreNamespaces() + public static string ToComponentName(this string fullTypeName, bool ignoreNamespaces) { + return ignoreNamespaces ? fullTypeName.ShortTypeName().RemoveComponentSuffix() : fullTypeName.RemoveDots().RemoveComponentSuffix(); } - - public const string IGNORE_NAMESPACES_KEY = "Entitas.CodeGeneration.Plugins.IgnoreNamespaces"; - - static Config _config; - - static bool shouldIgnoreNamespaces() { - if(_config == null) { - _config = Preferences.LoadConfig(); - } - - return _config.GetValueOrDefault(IGNORE_NAMESPACES_KEY, "false") == "true"; - } } } diff --git a/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/DataProviders/EntityIndex/EntityIndexDataProvider.cs b/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/DataProviders/EntityIndex/EntityIndexDataProvider.cs index b9ee815b0..9360950dc 100644 --- a/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/DataProviders/EntityIndex/EntityIndexDataProvider.cs +++ b/Addons/Entitas.CodeGeneration.Plugins/Entitas.CodeGeneration.Plugins/Plugins/DataProviders/EntityIndex/EntityIndexDataProvider.cs @@ -8,13 +8,32 @@ namespace Entitas.CodeGeneration.Plugins { - public class EntityIndexDataProvider : ICodeGeneratorDataProvider { + public class EntityIndexDataProvider : ICodeGeneratorDataProvider, IConfigurable { public string name { get { return "Entity Index"; } } public int priority { get { return 0; } } public bool isEnabledByDefault { get { return true; } } public bool runInDryMode { get { return true; } } + const string IGNORE_NAMESPACES_KEY = "Entitas.CodeGeneration.Plugins.IgnoreNamespaces"; + + public Dictionary defaultProperties { + get { return new Dictionary { { IGNORE_NAMESPACES_KEY, "false" } }; } + } + + bool ignoreNamespaces { get { return properties[IGNORE_NAMESPACES_KEY] == "true"; } } + + Dictionary properties { + get { + if(_properties == null) { + _properties = defaultProperties; + } + + return _properties; + } + } + + Dictionary _properties; Type[] _types; public EntityIndexDataProvider() : this(null) { @@ -24,6 +43,10 @@ public EntityIndexDataProvider(Type[] types) { _types = types; } + public void Configure(Dictionary properties) { + _properties = properties; + } + public CodeGeneratorData[] GetData() { if(_types == null) { _types = CodeGeneratorUtil.LoadTypesFromAssemblies(); @@ -56,7 +79,7 @@ EntityIndexData createEntityIndexData(Type type, List infos) { data.SetEntityIndexType(getEntityIndexType(attribute)); data.IsCustom(false); - data.SetEntityIndexName(type.ToCompilableString().ToComponentName()); + data.SetEntityIndexName(type.ToCompilableString().ToComponentName(ignoreNamespaces)); data.SetKeyType(info.type.ToCompilableString()); data.SetComponentType(type.ToCompilableString()); data.SetMemberName(info.name); diff --git a/Addons/Entitas.CodeGeneration/Entitas.CodeGeneration.csproj b/Addons/Entitas.CodeGeneration/Entitas.CodeGeneration.csproj index bfd2753a9..04748e0bc 100644 --- a/Addons/Entitas.CodeGeneration/Entitas.CodeGeneration.csproj +++ b/Addons/Entitas.CodeGeneration/Entitas.CodeGeneration.csproj @@ -37,6 +37,7 @@ + diff --git a/Addons/Entitas.CodeGeneration/Entitas.CodeGeneration/CodeGeneration/Interfaces/IConfigurable.cs b/Addons/Entitas.CodeGeneration/Entitas.CodeGeneration/CodeGeneration/Interfaces/IConfigurable.cs new file mode 100644 index 000000000..a93b0f7d8 --- /dev/null +++ b/Addons/Entitas.CodeGeneration/Entitas.CodeGeneration/CodeGeneration/Interfaces/IConfigurable.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace Entitas.CodeGeneration { + + public interface IConfigurable : ICodeGeneratorInterface { + + Dictionary defaultProperties { get; } + + void Configure(Dictionary properties); + } +}