Example #1
0
        void ToWorkshop(Func <VarCollection, Rule[]> addRules)
        {
            // Set up the variable collection.
            VarCollection.Setup();

            // Set up initial global and player rules.
            InitialGlobal = new TranslateRule(this, "Initial Global", RuleEvent.OngoingGlobal);
            InitialPlayer = new TranslateRule(this, "Initial Player", RuleEvent.OngoingPlayer);
            WorkshopRules = new List <Rule>();

            // Init called types.
            foreach (var type in Types.CalledTypes.Distinct())
            {
                type.WorkshopInit(this);
            }

            // Assign variables at the rule-set level.
            foreach (var variable in rulesetVariables)
            {
                // Assign the variable an index.
                var assigner = DefaultIndexAssigner.Add(VarCollection, variable, true, null) as IndexReference;

                // Assigner will be non-null if it is an IndexReference.
                if (assigner != null)
                {
                    DebugVariables.Add(variable, assigner);
                    // Initial value.
                    if (variable.InitialValue != null)
                    {
                        var addToInitialRule = GetInitialRule(variable.VariableType == VariableType.Global);

                        addToInitialRule.ActionSet.AddAction(assigner.SetVariable(
                                                                 (Element)variable.InitialValue.Parse(addToInitialRule.ActionSet)
                                                                 ));
                    }
                }
            }

            // Setup single-instance methods.
            foreach (var method in subroutines)
            {
                method.SetupSubroutine();
            }

            // Parse the rules.
            foreach (var rule in rules)
            {
                var  translate = new TranslateRule(this, rule);
                Rule newRule   = translate.GetRule();
                WorkshopRules.Add(newRule);
                rule.ElementCountLens.RuleParsed(newRule);
            }

            // Add built-in rules.
            // Initial player
            if (InitialPlayer.Actions.Count > 0)
            {
                WorkshopRules.Insert(0, InitialPlayer.GetRule());
            }

            // Initial global
            if (InitialGlobal.Actions.Count > 0)
            {
                WorkshopRules.Insert(0, InitialGlobal.GetRule());
            }

            // Additional
            if (addRules != null)
            {
                WorkshopRules.AddRange(addRules.Invoke(VarCollection).Where(rule => rule != null));
            }

            // Order the workshop rules by priority.
            WorkshopRules = WorkshopRules.OrderBy(wr => wr.Priority).ToList();

            // Get the final workshop string.
            WorkshopBuilder result = new WorkshopBuilder(Language);

            LanguageInfo.I18nWarningMessage(result, Language);

            // Get the custom game settings.
            if (Importer.MergedLobbySettings != null)
            {
                Ruleset settings = Ruleset.Parse(Importer.MergedLobbySettings);
                settings.ToWorkshop(result);
                result.AppendLine();
            }

            // Get the variables.
            VarCollection.ToWorkshop(result);
            result.AppendLine();

            // Print class identifiers.
            Types.PrintClassIdentifiers(result);

            // Get the subroutines.
            SubroutineCollection.ToWorkshop(result);

            // Get the rules.
            for (int i = 0; i < WorkshopRules.Count; i++)
            {
                WorkshopRules[i].ToWorkshop(result, OptimizeOutput);
                ElementCount += WorkshopRules[i].ElementCount(OptimizeOutput);
                if (i != WorkshopRules.Count - 1)
                {
                    result.AppendLine();
                }
            }

            WorkshopCode = result.ToString();
        }
        void ToWorkshop(Func <VarCollection, Rule[]> addRules)
        {
            // Set up the variable collection.
            VarCollection.Setup();

            // Set up initial global and player rules.
            InitialGlobal = new TranslateRule(this, "Initial Global", RuleEvent.OngoingGlobal);
            InitialPlayer = new TranslateRule(this, "Initial Player", RuleEvent.OngoingPlayer);
            WorkshopRules = new List <Rule>();

            // Assign static variables.
            foreach (var type in types)
            {
                type.WorkshopInit(this);
            }

            // Assign variables at the rule-set level.
            foreach (var variable in rulesetVariables)
            {
                // Assign the variable an index.
                DefaultIndexAssigner.Add(VarCollection, variable, true, null);

                var assigner = DefaultIndexAssigner[variable] as IndexReference;
                if (assigner != null && variable.InitialValue != null)
                {
                    var addToInitialRule = GetInitialRule(variable.VariableType == VariableType.Global);

                    addToInitialRule.ActionSet.AddAction(assigner.SetVariable(
                                                             (Element)variable.InitialValue.Parse(addToInitialRule.ActionSet)
                                                             ));
                }
            }

            // Setup single-instance methods.
            foreach (var method in subroutines)
            {
                method.SetupSubroutine();
            }

            // Parse the rules.
            foreach (var rule in rules)
            {
                var translate = new TranslateRule(this, rule);
                WorkshopRules.Add(translate.GetRule());
            }

            if (InitialPlayer.Actions.Count > 0)
            {
                WorkshopRules.Insert(0, InitialPlayer.GetRule());
            }

            if (InitialGlobal.Actions.Count > 0)
            {
                WorkshopRules.Insert(0, InitialGlobal.GetRule());
            }

            if (addRules != null)
            {
                WorkshopRules.AddRange(addRules.Invoke(VarCollection).Where(rule => rule != null));
            }

            // Order the workshop rules by priority.
            WorkshopRules = WorkshopRules.OrderBy(wr => wr.Priority).ToList();

            // Get the final workshop string.
            WorkshopBuilder result = new WorkshopBuilder(Language);

            LanguageInfo.I18nWarningMessage(result, Language);

            // Get the custom game settings.
            if (MergedLobbySettings != null)
            {
                Ruleset settings = Ruleset.Parse(MergedLobbySettings);
                settings.ToWorkshop(result);
                result.AppendLine();
            }

            // Get the variables.
            VarCollection.ToWorkshop(result);
            result.AppendLine();

            // Get the subroutines.
            SubroutineCollection.ToWorkshop(result);

            // Get the rules.
            foreach (var rule in WorkshopRules)
            {
                result.AppendLine(rule.ToWorkshop(Language, OptimizeOutput));
            }

            WorkshopCode = result.ToString();
        }
        void ToWorkshop(Func <VarCollection, Rule[]> addRules)
        {
            // Set up the variable collection.
            VarCollection.Setup();

            WorkshopConverter = new ToWorkshop(this);

            // Set up initial global and player rules.
            InitialGlobal = new TranslateRule(this, "Initial Global", RuleEvent.OngoingGlobal);
            InitialPlayer = new TranslateRule(this, "Initial Player", RuleEvent.OngoingPlayer);
            WorkshopRules = new List <Rule>();

            WorkshopConverter.InitStatic();

            // Init called types.
            foreach (var workshopInit in _workshopInit)
            {
                workshopInit.WorkshopInit(this);
            }

            // Assign variables at the rule-set level.
            foreach (var variable in rulesetVariables)
            {
                var addToInitialRule = GetInitialRule(variable.VariableType == VariableType.Global);

                // Assign the variable an index.
                IGettable value = variable
                                  .GetDefaultInstance(null)
                                  .GetAssigner(new(addToInitialRule.ActionSet))
                                  .GetValue(new GettableAssignerValueInfo(addToInitialRule.ActionSet)
                {
                    SetInitialValue = SetInitialValue.SetIfExists
                });
                DefaultIndexAssigner.Add(variable, value);

                if (value is IndexReference indexReference)
                {
                    DebugVariables.Add(variable, indexReference);
                }
            }

            // Parse the rules.
            foreach (var rule in rules)
            {
                var  translate = new TranslateRule(this, rule);
                Rule newRule   = GetRule(translate.GetRule());
                WorkshopRules.Add(newRule);
                rule.ElementCountLens.RuleParsed(newRule);
            }

            // Add built-in rules.
            // Initial player
            if (InitialPlayer.Actions.Count > 0)
            {
                WorkshopRules.Insert(0, GetRule(InitialPlayer.GetRule()));
            }

            // Initial global
            if (InitialGlobal.Actions.Count > 0)
            {
                WorkshopRules.Insert(0, GetRule(InitialGlobal.GetRule()));
            }

            // Additional
            if (addRules != null)
            {
                WorkshopRules.AddRange(addRules.Invoke(VarCollection).Where(rule => rule != null));
            }

            // Complete portable functions
            WorkshopConverter.LambdaBuilder.Complete();

            // Order the workshop rules by priority.
            WorkshopRules = WorkshopRules.OrderBy(wr => wr.Priority).ToList();

            // Get the final workshop string.
            WorkshopBuilder result = new WorkshopBuilder(Language);

            LanguageInfo.I18nWarningMessage(result, Language);

            // Get the custom game settings.
            if (Importer.MergedLobbySettings != null)
            {
                Ruleset settings = Ruleset.Parse(Importer.MergedLobbySettings);
                settings.ToWorkshop(result);
                result.AppendLine();
            }

            // Get the variables.
            VarCollection.ToWorkshop(result);
            result.AppendLine();

            // Get the subroutines.
            SubroutineCollection.ToWorkshop(result);

            // Get the rules.
            for (int i = 0; i < WorkshopRules.Count; i++)
            {
                WorkshopRules[i].ToWorkshop(result);
                ElementCount += WorkshopRules[i].ElementCount();
                if (i != WorkshopRules.Count - 1)
                {
                    result.AppendLine();
                }
            }

            WorkshopCode = result.GetResult();
        }