Ejemplo n.º 1
0
        public string GenerateCode()
        {
            _text            = new StringBuilder(1024);
            _generatedBlocks = new List <GeneratedBlockInfo>();

            foreach (var block in BlockList.OrderBy(it => it.InputNodes.Count))
            {
                AddBlock(block);
            }
            AppendClassDeclarationCode();

            _text.AppendLine(Ident(3) + "//Declaring the blocks");
            foreach (var blockInfo in _generatedBlocks)
            {
                GenerateDeclarationCode(blockInfo);
            }
            _text.AppendLine();

            _text.AppendLine(Ident(3) + "//Connecting the blocks");
            foreach (var blockInfo in _generatedBlocks)
            {
                GenerateConnectionCode(blockInfo);
            }
            _text.AppendLine();

            _text.AppendLine(Ident(3) + "//Appending the blocks to a block list and execute all");
            _text.AppendLine(Ident(3) + "var blockList = new BlockList();");
            foreach (var blockInfo in _generatedBlocks)
            {
                _text.AppendLine(Ident(3) + "blockList.Add(" + blockInfo.VariableName + ");");
            }
            _text.AppendLine(Ident(3) + "blockList.ExecuteAll();");
            AppendClassEndCode();
            return(_text.ToString());
        }
Ejemplo n.º 2
0
        public virtual void Deserialize(Player player, string currentPattern)
        {
            // See documentation: https://worldedit.enginehub.org/en/latest/usage/general/patterns/

            if (currentPattern.StartsWith("x"))
            {
                currentPattern = currentPattern.Remove(0, 1);                                             // remove starting x
            }
            OriginalPattern = currentPattern.Trim();

            var patternsEx = new Regex(@",(?![^\[]*])");

            foreach (string pattern in patternsEx.Split(currentPattern.Trim()))
            {
                Log.Debug($"Matching {pattern}");
                var blockDataEntry = new BlockDataEntry();

                var regex   = new Regex(@"(?<pattern>((?<weight>\d+)%)?((?<blockId>\d+)|(?<blockName>(minecraft:)?\w+)){1}(:(?<meta>\d+))?(\[(?<states>[a-zA-Z0-9_=,]*)])?)*");
                var stateEx = new Regex(@"(?<name>\w+)\=(?<value>\w+)");

                Match match = regex.Match(pattern.Trim());
                if (match.Success)
                {
                    foreach (Group matchGroup in match.Groups)
                    {
                        if (matchGroup.Name == "weight" && matchGroup.Success)
                        {
                            Log.Debug($"Matched weight group {matchGroup.Value}");
                            if (int.TryParse(matchGroup.Value.Trim(), out int weight))
                            {
                                blockDataEntry.Weight = weight;
                            }
                        }
                        else if (matchGroup.Name == "blockName" && matchGroup.Success)
                        {
                            Log.Debug($"Matched blockName group {matchGroup.Value}");
                            blockDataEntry.Id = BlockFactory.GetBlockIdByName(matchGroup.Value.Trim());
                        }
                        if (matchGroup.Name == "blockId" && matchGroup.Success)
                        {
                            Log.Debug($"Matched blockId group {matchGroup.Value}");
                            if (int.TryParse(matchGroup.Value.Trim(), out int id))
                            {
                                blockDataEntry.Id = id;
                            }
                        }
                        else if (matchGroup.Name == "meta" && matchGroup.Success)
                        {
                            Log.Debug($"Matched meta group {matchGroup.Value}");
                            if (byte.TryParse(matchGroup.Value.Trim(), out byte metadata))
                            {
                                blockDataEntry.Metadata       = metadata;
                                blockDataEntry.HasBlockStates = true;
                            }
                        }
                        else if (matchGroup.Name == "states" && matchGroup.Success)
                        {
                            Log.Debug($"Matched states group {matchGroup.Value}");

                            // Parse block states
                            var stateMatches = stateEx.Matches(matchGroup.Value.Trim());
                            {
                                foreach (Match stateMatch in stateMatches)
                                {
                                    Log.Debug($"State:{stateMatch.Value}");
                                    blockDataEntry.BlockStates.Add(new BlockStateEntry()
                                    {
                                        Name  = stateMatch.Groups.Values.First(g => g.Name == "name").Value,
                                        Value = stateMatch.Groups.Values.First(g => g.Name == "value").Value
                                    });
                                    blockDataEntry.HasBlockStates = true;
                                }
                            }
                        }
                    }
                }
                else
                {
                    throw new Exception("Deprecated code used to be here.");
                }

                BlockList.Add(blockDataEntry);
            }

            int acc = 0;

            foreach (var entry in BlockList.OrderBy(entry => entry.Weight))
            {
                acc += entry.Weight;
                entry.Accumulated = acc;
            }

            BlockList = BlockList.OrderBy(entry => entry.Accumulated).ToList();
        }