Exemple #1
0
        public void Generate()
        {
            _define.Load(PathConfig.DataXmlPath);

            var generatedFiles = new HashSet<string>();
            foreach (var template in _define.Templates)
            {
                var code = new SourceCode();
                code.Append("using Server.Core.Data;");
                code.Append("using System.Collections.Generic;");
                code.NewLine();

                code.Append("namespace Server.Data");
                code.BracketStart();

                WriteTemplateToCode(template, code);

                code.BracketEnd();
                code.NewLine();

                var dataFileName = template.Name + ".cs";
                var dataFilePath = Path.Combine(PathConfig.DataProjectPath, dataFileName);
                code.WriteToFile(dataFilePath);
                generatedFiles.Add(dataFileName);
            }

            GeneratorHelper.UpdateProjectFile(PathConfig.DataProjectFilePath, generatedFiles);
        }
Exemple #2
0
 public static SourceCode Parse(string code)
 {
     var source = new SourceCode();
     code = code.Replace("\r", "");
     code = code.Replace("    ", "\t");
     var prevTabCount = 0;
     foreach (var line in code.Split('\n'))
     {
         var tabCount = line.CountTab();
         if (prevTabCount > tabCount)
         {
             for (var i = 0; i < prevTabCount - tabCount; ++i)
                 source.IndentLeft();
         }
         else if (prevTabCount < tabCount)
         {
             for (var i = 0; i < tabCount - prevTabCount; ++i)
                 source.IndentRight();
         }
         prevTabCount = tabCount;
         var trimmed = line.Trim();
         if (trimmed.StartsWith("<%") && trimmed.EndsWith("%>"))
             source.AddMarker(trimmed.Strip("<%", "%>"));
         else source.Append(trimmed);
     }
     return source;
 }
Exemple #3
0
        private void WriteTemplateToCode(DataTemplate dataTemplate, SourceCode code)
        {
            var templateCanonicalName = _define.GetCanonicalName(dataTemplate);
            // root만 IData를 상속 받는다.
            code.Append(dataTemplate.Parent.IsRoot ? "public class {0} : IData" : "public class {0}",
                        templateCanonicalName);

            code.BracketStart();

            foreach (var child in dataTemplate.Children)
                WriteTemplateToCode(child, code);

            foreach (var attribute in dataTemplate.Attributes)
            {
                var valueType = DataTemplateHelper.InferenceType(attribute.Value);
                if (valueType == "enum")
                {
                    var enumName = attribute.Key + (attribute.Key.EndsWith("s") || attribute.Key.EndsWith("x") ? "es" : "s");
                    if (attribute.Key.Equals("id", StringComparison.OrdinalIgnoreCase))
                        code.Append("public int {0} {{ get; set; }}", attribute.Key);
                    else
                        code.Append("public {0} {1} {{ get; set; }}", enumName, attribute.Key);

                    code.Append("public enum {0}", enumName);
                    code.BracketStart();
                    foreach (var value in attribute.Value.Distinct())
                        code.Append("{0},", value.Substring(1).ToCamelCase());  // remove leading _ character.
                    code.BracketEnd();
                }
                else code.Append("public {0} {1} {{ get; set; }}", valueType, attribute.Key);
            }

            foreach (var attributeName in from e in dataTemplate.Attributes where e.Key.EndsWith("Ref") select e.Key)
            {
                // Ref를 제거하고 남은 이름
                var typeName = attributeName.Substring(0, attributeName.Length - 3);
                code.Append("public {0} {0} {{ get {{ return DataCenter.Instance[typeof ({0}), {1}] as {0}; }} }}",
                            typeName, attributeName);
            }

            foreach (var child in dataTemplate.Children)
                code.Append("public List<{0}> {0}s {{ get; private set; }}", _define.GetCanonicalName(child));

            if (dataTemplate.Children.Count > 0)
            {
                code.NewLine();
                code.Append("public {0}()", templateCanonicalName);
                code.BracketStart();
                foreach (var child in dataTemplate.Children)
                    code.Append("{0}s = new List<{0}>();", _define.GetCanonicalName(child));
                code.BracketEnd();
            }

            code.BracketEnd();
            code.NewLine();
        }
Exemple #4
0
        public static SourceCode Parse(string code)
        {
            var source = new SourceCode();

            code = code.Replace("\r", "");
            code = code.Replace("    ", "\t");
            var prevTabCount = 0;

            foreach (var line in code.Split('\n'))
            {
                var tabCount = line.CountTab();
                if (prevTabCount > tabCount)
                {
                    for (var i = 0; i < prevTabCount - tabCount; ++i)
                    {
                        source.IndentLeft();
                    }
                }
                else if (prevTabCount < tabCount)
                {
                    for (var i = 0; i < tabCount - prevTabCount; ++i)
                    {
                        source.IndentRight();
                    }
                }
                prevTabCount = tabCount;
                var trimmed = line.Trim();
                if (trimmed.StartsWith("<%") && trimmed.EndsWith("%>"))
                {
                    source.AddMarker(trimmed.Strip("<%", "%>"));
                }
                else
                {
                    source.Append(trimmed);
                }
            }
            return(source);
        }
Exemple #5
0
        public void Generate()
        {
            // ReSharper disable AssignNullToNotNullAttribute
            // ReSharper disable PossibleNullReferenceException
            var document = new XmlDocument();
            document.Load(PathConfig.MessageXmlPath);

            var postfix = document.SelectSingleNode("/mmo-msgs/@postfix").Value.ToCamelCase();

            var generatedFiles = new List<string>();
            var groupIndex = 0;
            foreach (var groupNode in document.SelectNodes("//group").OfType<XmlElement>())
            {
                ++groupIndex;

                var groupName = groupNode.GetAttribute("name").ToCamelCase();
                var groupPath = Path.Combine(PathConfig.MessageProjectPath, groupName);
                IoHelper.CreateDirectory(groupPath);

                var typeIndex = 0;
                foreach (var msgNode in groupNode.SelectNodes("msg").OfType<XmlElement>())
                {
                    var typeId = groupIndex*1000 + typeIndex;
                    ++typeIndex;

                    if (msgNode.GetAttribute("internal").Equals("true"))
                        continue;

                    var messageName = msgNode.GetAttribute("name");
                    var messageClassName = messageName.ToCamelCase() + postfix;

                    var code = new SourceCode();
                    code.Append("using System.Collections.Generic;");
                    code.Append("using Server.Core.Component;");
                    code.Append("using Server.Core.Messaging;");
                    code.NewLine();
                    code.Append("namespace Server.Message.{0}", groupName);
                    code.BracketStart();

                    code.Append("public class {0} : IMessage", messageClassName);
                    code.BracketStart();
                    code.Append("public const int TypeId = {0};", typeId);
                    code.NewLine();

                    var listMembers = new Dictionary<string, string>();
                    foreach (var fieldNode in msgNode.SelectNodes("field").OfType<XmlElement>())
                    {
                        var fieldType = fieldNode.GetAttribute("type");
                        var fieldName = fieldNode.GetAttribute("name").ToCamelCase();
                        if (fieldType.Equals("bin", StringComparison.OrdinalIgnoreCase))
                            fieldType = "byte[]";

                        if (fieldName.Equals("id", StringComparison.OrdinalIgnoreCase))
                        {
                            code.Append("[Attribute(EntityId = true)]");
                        }

                        if (fieldNode.HasAttribute("attribute"))
                        {
                            var attributeBind = fieldNode.GetAttribute("attribute").Split('.');
                            var attributeClassName = attributeBind[0].ToCamelCase();
                            var attributeFieldName = attributeBind[1].ToCamelCase();
                            code.Append("[Attribute(Attribute = \"{0}\", Field = \"{1}\")]", attributeClassName, attributeFieldName);
                        }

                        code.Append("public {0} {1} {{ get; set; }}", fieldType, fieldName);
                        code.NewLine();
                    }

                    foreach (var refNode in msgNode.SelectNodes("ref").OfType<XmlElement>())
                    {
                        var refName = refNode.GetAttribute("msg").ToCamelCase();
                        var onceRef = refNode.GetAttribute<bool>("once");
                        if (onceRef)
                        {
                            code.Append("public {0}{1} {0} {{ get; set; }}", refName, postfix);
                        }
                        else
                        {
                            var listTypeName = string.Format("List<{0}{1}>", refName, postfix);
                            var listMemberName = string.Format("{0}List", refName);
                            code.Append("public {0} {1} {{ get; private set; }}", listTypeName, listMemberName);
                            listMembers.Add(listMemberName, listTypeName);
                        }
                    }

                    if (listMembers.Count > 0)
                    {
                        code.NewLine();
                        code.Append("public {0}()", messageClassName);
                        code.BracketStart();
                        foreach (var pair in listMembers)
                        {
                            code.Append("{0} = new {1}();", pair.Key, pair.Value);
                        }
                        code.BracketEnd();
                    }
                    code.BracketEnd();
                    code.NewLine();

                    code.BracketEnd();
                    code.NewLine();

                    var messageFileName = messageClassName + ".cs";
                    var messageFilePath = Path.Combine(groupPath, messageFileName);
                    code.WriteToFile(messageFilePath);

                    generatedFiles.Add(Path.Combine(groupName, messageFileName));
                }
            }

            GeneratorHelper.UpdateProjectFile(PathConfig.MessageProjectFilePath, generatedFiles);

            // ReSharper restore AssignNullToNotNullAttribute
            // ReSharper restore PossibleNullReferenceException
        }
Exemple #6
0
 public SourceCode Append(SourceCode other)
 {
     other._main.Codes.ForEach(c => Append(c));
     other._main.Parent = _currentBlock;
     return(this);
 }
Exemple #7
0
 public SourceCode Append(SourceCode other)
 {
     other._main.Codes.ForEach(c => Append(c));
     other._main.Parent = _currentBlock;
     return this;
 }