public Rule(string name, Subroutine subroutine)
 {
     Name       = name;
     RuleEvent  = RuleEvent.Subroutine;
     RuleType   = RuleType.Subroutine;
     Subroutine = subroutine;
 }
        public void ToWorkshop(WorkshopBuilder builder, bool optimize)
        {
            if (Disabled)
            {
                builder.AppendKeyword("disabled")
                .Append(" ");
            }
            builder.AppendKeyword("rule")
            .AppendLine("(\"" + Name + "\")")
            .AppendLine("{")
            .AppendLine()
            .Indent()
            .AppendKeywordLine("event")
            .AppendLine("{")
            .Indent()
            .AppendLine(EnumData.GetEnumValue(RuleEvent).ToWorkshop(builder.OutputLanguage, ToWorkshopContext.Other) + ";");

            // Add attributes.
            switch (RuleType)
            {
            case RuleType.PlayerBased:
                // Player based attributes
                builder.AppendLine(EnumData.GetEnumValue(Team).ToWorkshop(builder.OutputLanguage, ToWorkshopContext.Other) + ";");     // Team attribute
                builder.AppendLine(EnumData.GetEnumValue(Player).ToWorkshop(builder.OutputLanguage, ToWorkshopContext.Other) + ";");   // Player attribute
                break;

            case RuleType.Subroutine:
                builder.AppendLine(Subroutine.ToWorkshop(builder.OutputLanguage, ToWorkshopContext.Other) + ";");     // Attribute name
                break;
            }
            builder.Unindent()
            .AppendLine("}");

            if (Conditions?.Length > 0)
            {
                builder.AppendLine()
                .AppendKeywordLine("conditions")
                .AppendLine("{")
                .Indent();

                foreach (var condition in Conditions)
                {
                    builder.AppendLine(condition.ToWorkshop(builder.OutputLanguage, optimize) + ";");
                }

                builder.Unindent()
                .AppendLine("}");
            }

            // Add actions.
            if (Actions?.Length > 0)
            {
                builder.AppendLine()
                .AppendLine("// Action count: " + Actions.Length)     // Action count comment.
                .AppendKeywordLine("actions")
                .AppendLine("{")
                .Indent();

                foreach (var action in Actions)
                {
                    if (optimize)
                    {
                        builder.AppendLine(action.Optimize().ToWorkshop(builder.OutputLanguage, ToWorkshopContext.Action));
                    }
                    else
                    {
                        builder.AppendLine(action.ToWorkshop(builder.OutputLanguage, ToWorkshopContext.Action));
                    }
                }

                builder.Unindent()
                .AppendLine("}");
            }
            builder.Unindent()
            .AppendLine("}");
        }
        public string ToWorkshop(OutputLanguage language, bool optimize)
        {
            var builder = new TabStringBuilder(true);

            builder.Indent = 0;
            if (Disabled)
            {
                builder.Append(LanguageInfo.Translate(language, "disabled") + " ");
            }
            builder.AppendLine($"{LanguageInfo.Translate(language, "rule")}(\"{Name}\")");
            builder.AppendLine("{");
            builder.AppendLine();

            builder.Indent = 1;
            builder.AppendLine(LanguageInfo.Translate(language, "event"));
            builder.AppendLine("{");
            builder.Indent = 2;
            builder.AppendLine(EnumData.GetEnumValue(RuleEvent)
                               .ToWorkshop(language) + ";");

            // Add attributes.
            switch (RuleType)
            {
            case RuleType.PlayerBased:
                // Player based attributes
                builder.AppendLine(EnumData.GetEnumValue(Team).ToWorkshop(language) + ";");     // Team attribute
                builder.AppendLine(EnumData.GetEnumValue(Player).ToWorkshop(language) + ";");   // Player attribute
                break;

            case RuleType.Subroutine:
                builder.AppendLine(Subroutine.ToWorkshop(language) + ";");     // Attribute name
                break;
            }
            builder.Indent = 1;
            builder.AppendLine("}");

            if (Conditions?.Length > 0)
            {
                builder.AppendLine();
                builder.AppendLine(LanguageInfo.Translate(language, "conditions"));
                builder.AppendLine("{");
                builder.Indent = 2;
                foreach (var condition in Conditions)
                {
                    builder.AppendLine(condition.ToWorkshop(language, optimize) + ";");
                }
                builder.Indent = 1;
                builder.AppendLine("}");
            }

            // Add actions.
            if (Actions?.Length > 0)
            {
                builder.AppendLine();
                builder.AppendLine("// Action count: " + Actions.Length); // Action count comment.
                builder.AppendLine(LanguageInfo.Translate(language, "actions"));
                builder.AppendLine("{");
                builder.Indent = 2;

                foreach (var action in Actions)
                {
                    if (optimize)
                    {
                        builder.AppendLine(action.Optimize().ToWorkshop(language));
                    }
                    else
                    {
                        builder.AppendLine(action.ToWorkshop(language));
                    }
                }

                builder.Indent = 1;
                builder.AppendLine("}");
            }
            builder.Indent = 0;
            builder.AppendLine("}");

            return(builder.ToString());
        }
Пример #4
0
        public void ToWorkshop(WorkshopBuilder builder)
        {
            if (Disabled)
            {
                builder.AppendKeyword("disabled")
                .Append(" ");
            }
            builder.AppendKeyword("rule")
            .AppendLine("(\"" + Name + "\")")
            .AppendLine("{")
            .AppendLine()
            .Indent()
            .AppendKeywordLine("event")
            .AppendLine("{")
            .Indent();

            ElementRoot.Instance.GetEnumValue("Event", RuleEvent.ToString()).ToWorkshop(builder, ToWorkshopContext.Other);
            builder.Append(";").AppendLine();

            // Add attributes.
            switch (RuleType)
            {
            case RuleType.PlayerBased:
                // Player based attributes
                ElementEnumMember.Team(Team).ToWorkshop(builder, ToWorkshopContext.Other);                                   // Team attribute
                builder.Append(";").AppendLine();
                ElementRoot.Instance.GetEnumValue("Player", Player.ToString()).ToWorkshop(builder, ToWorkshopContext.Other); // Player attribute
                builder.Append(";").AppendLine();
                break;

            case RuleType.Subroutine:
                Subroutine.ToWorkshop(builder, ToWorkshopContext.Other);     // Attribute name
                builder.Append(";").AppendLine();
                break;
            }
            builder.Outdent()
            .AppendLine("}");

            if (Conditions?.Length > 0)
            {
                builder.AppendLine()
                .AppendKeywordLine("conditions")
                .AppendLine("{")
                .Indent();

                foreach (var condition in Conditions)
                {
                    condition.ToWorkshop(builder);
                }

                builder.Outdent().AppendLine("}");
            }

            // Add actions.
            if (Actions?.Length > 0)
            {
                builder.AppendLine()
                .AppendLine("// Action count: " + Actions.Length)     // Action count comment.
                .AppendKeywordLine("actions")
                .AppendLine("{")
                .Indent();

                foreach (var action in Actions)
                {
                    action.ToWorkshop(builder, ToWorkshopContext.Action);
                }

                builder.Outdent().AppendLine("}");
            }
            builder.Outdent().AppendLine("}");
        }