Beispiel #1
0
        public Type GetMixinType(string modelName)
        {
            FlatType entityType = index.GetType(modelName);
            // First try to directly lookup type as a library type
            Type mixinType = GetType($"I{modelName}");

            if (mixinType != null)
            {
                return(mixinType);
            }

            List <FlatType> requiredMixins = entityType.RequiredMixin().ToList();

            if (requiredMixins.Count != 1)
            {
                throw new InvalidOperationException($"Cannot find single mixin for: {entityType}");
            }

            FlatType requiredMixin = requiredMixins.First();

            mixinType = GetType($"I{requiredMixin.Name}");
            if (mixinType == null)
            {
                throw new InvalidOperationException($"Mixin type: I{requiredMixin.Name} does not exist.");
            }

            return(mixinType);
        }
        public string GenerateClass(string @namespace, FlatType type)
        {
            List <string> mixinTypes = type.RequiredMixin()
                                       .Select(mixin => mixin.Name)
                                       .ToList();

            var builder = new StringBuilder();

            builder.AppendLine($"namespace Maple2.File.Flat.{@namespace} {{");
            List <string> mixinInterfaces = mixinTypes.Select(mixin => $"I{mixin}").ToList();

            if (mixinInterfaces.Count > 0)
            {
                builder.AppendLine($"\tpublic class {type.Name} : {string.Join(",", mixinInterfaces)} {{");
            }
            else
            {
                builder.AppendLine($"\tpublic class {type.Name} : IMapEntity {{");
            }

            builder.AppendLine($"\t\tstring ModelName => \"{type.Name}\";");
            foreach (FlatProperty property in type.GetProperties())
            {
                string typeStr   = NormalizeType(property.Value.GetType().ToString());
                string typeValue = property.ValueCodeString();
                builder.AppendLine($"\t\tpublic {typeStr} {property.Name} {{ get; set; }} = {typeValue};");
            }

            foreach (FlatProperty property in type.GetInheritedProperties())
            {
                string typeStr   = NormalizeType(property.Value.GetType().ToString());
                string typeValue = property.ValueCodeString();
                builder.AppendLine($"\t\t{typeStr} {property.Name} {{ get; set; }} = {typeValue};");
            }

            builder.AppendLine("\t}"); // class
            builder.AppendLine("}");   // namespace

            return(builder.ToString());
        }
        public string GenerateInterface(string @namespace, FlatType type)
        {
            List <string> mixinTypes = type.RequiredMixin()
                                       .Select(mixin => mixin.Name)
                                       .ToList();
            var inheritedProperties = new Dictionary <string, object>();

            foreach (string mixinType in mixinTypes)
            {
                foreach (FlatProperty property in index.GetType(mixinType).GetAllProperties())
                {
                    if (inheritedProperties.ContainsKey(property.Name))
                    {
                        inheritedProperties[property.Name] = null;
                    }
                    else
                    {
                        inheritedProperties.Add(property.Name, property.Value);
                    }
                }
            }

            var builder = new StringBuilder();

            builder.AppendLine($"namespace Maple2.File.Flat.{@namespace} {{");

            List <string> mixinInterfaces = mixinTypes.Select(mixin => $"I{mixin}").ToList();

            if (mixinInterfaces.Count > 0)
            {
                builder.AppendLine($"\tpublic interface I{type.Name} : {string.Join(",", mixinInterfaces)} {{");
            }
            else
            {
                builder.AppendLine($"\tpublic interface I{type.Name} : IMapEntity {{");
            }

            builder.AppendLine($"\t\tstring ModelName => \"{type.Name}\";");
            foreach (FlatProperty property in type.GetProperties())
            {
                // Inherited properties don't need to be declared on interface
                if (inheritedProperties.TryGetValue(property.Name, out object propertyValue))
                {
                    if (propertyValue != null)
                    {
                        if (Equals(propertyValue, property.Value))
                        {
                            continue;
                        }

                        // Since the dictionaries are always empty, just doing count comparison to shortcut
                        if (propertyValue is IDictionary dict1 && property.Value is IDictionary dict2 &&
                            dict1.Count == dict2.Count)
                        {
                            continue;
                        }
                    }
                }

                string typeStr   = NormalizeType(property.Value.GetType().ToString());
                string typeValue = property.ValueCodeString();
                builder.AppendLine($"\t\t{typeStr} {property.Name} => {typeValue};");
            }

            builder.AppendLine("\t}"); // class
            builder.AppendLine("}");   // namespace

            return(builder.ToString());
        }