예제 #1
0
        public static List <CustomFilter> Parse(string[] filtersLines)
        {
            var allFilters = new List <CustomFilter>();

            for (var i = 0; i < filtersLines.Length; ++i)
            {
                var filterLine = filtersLines[i];

                filterLine = filterLine.Replace("\t", "");

                if (filterLine.StartsWith(COMMENTSYMBOL))
                {
                    continue;
                }
                if (filterLine.StartsWith(COMMENTSYMBOLALT))
                {
                    continue;
                }

                if (filterLine.Replace(" ", "").Length == 0)
                {
                    continue;
                }

                var nameIndex = filterLine.IndexOf(SYMBOL_NAMEDIVIDE);
                if (nameIndex == -1)
                {
                    BasePlugin.LogMessage("Filter parser: Can't find filter name in line: " + (i + 1), 5);
                    continue;
                }
                var newFilter = new CustomFilter {
                    Name = filterLine.Substring(0, nameIndex)
                };
                TrimName(ref newFilter.Name);

                var filterCommandsLine = filterLine.Substring(nameIndex + 1);

                var submenuIndex = filterCommandsLine.IndexOf(SYMBOL_SUBMENUNAME);
                if (submenuIndex != -1)
                {
                    newFilter.SubmenuName = filterCommandsLine.Substring(submenuIndex + 1);
                    filterCommandsLine    = filterCommandsLine.Substring(0, submenuIndex);
                }

                var filterCommands = filterCommandsLine.Split(SYMBOL_COMMANDSDIVIDE);

                var filterErrorParse = false;

                foreach (var command in filterCommands)
                {
                    if (string.IsNullOrEmpty(command.Replace(" ", "")))
                    {
                        continue;
                    }

                    if (command.Contains(SYMBOL_COMMAND_FILTER_OR))
                    {
                        var orFilterCommands = command.Split(SYMBOL_COMMAND_FILTER_OR);
                        var newOrFilter      = new BaseFilter {
                            BAny = true
                        };
                        newFilter.Filters.Add(newOrFilter);

                        foreach (var t in orFilterCommands)
                        {
                            if (ProcessCommand(newOrFilter, t))
                            {
                                continue;
                            }
                            filterErrorParse = true;
                            break;
                        }

                        if (filterErrorParse)
                        {
                            break;
                        }
                    }
                    else
                    {
                        if (ProcessCommand(newFilter, command))
                        {
                            continue;
                        }

                        filterErrorParse = true;
                        break;
                    }
                }

                if (!filterErrorParse)
                {
                    allFilters.Add(newFilter);
                }
            }
            return(allFilters);
        }
예제 #2
0
        private static bool ProcessCommand(BaseFilter newFilter, string command)
        {
            TrimName(ref command);

            if (command.Contains(PARAMETER_IDENTIFIED))
            {
                var identCommand = new IdentifiedItemFilter {
                    BIdentified = command[0] != SYMBOL_NOT
                };
                newFilter.Filters.Add(identCommand);
                return(true);
            }

            if (command.Contains(PARAMETER_ISELDER))
            {
                var elderCommand = new ElderItemFiler {
                    IsElder = command[0] != SYMBOL_NOT
                };
                newFilter.Filters.Add(elderCommand);
                return(true);
            }

            if (command.Contains(PARAMETER_ISSHAPER))
            {
                var shaperCommand = new ShaperItemFiler {
                    IsShaper = command[0] != SYMBOL_NOT
                };
                newFilter.Filters.Add(shaperCommand);
                return(true);
            }

            if (!ParseCommand(command, out var parameter, out var operation, out var value))
            {
                BasePlugin.LogMessage("Filter parser: Can't parse filter part: " + command, 5);
                return(false);
            }

            var stringComp = new FilterParameterCompare {
                CompareString = value
            };
            var isNumeric = int.TryParse(value, out var n);

            if (isNumeric)
            {
                stringComp.CompareInt = n;
            }

            switch (parameter.ToLower())
            {
            case PARAMETER_CLASSNAME:
                stringComp.StringParameter = data => data.ClassName;
                break;

            case PARAMETER_BASENAME:
                stringComp.StringParameter = data => data.BaseName;
                break;

            case PARAMETER_PATH:
                stringComp.StringParameter = data => data.Path;
                break;

            case PARAMETER_RARITY:
                stringComp.StringParameter = data => data.Rarity.ToString();
                break;

            case PARAMETER_QUALITY:
                stringComp.IntParameter    = data => data.ItemQuality;
                stringComp.StringParameter = data => data.ItemQuality.ToString();
                break;

            case PARAMETER_MAP_TIER:
                stringComp.IntParameter    = data => data.MapTier;
                stringComp.StringParameter = data => data.MapTier.ToString();
                break;

            case PARAMETER_ILVL:
                stringComp.IntParameter    = data => data.ItemLevel;
                stringComp.StringParameter = data => data.ItemLevel.ToString();
                break;

            default:
                BasePlugin.LogMessage(
                    $"Filter parser: Parameter is not defined in code: {parameter}", 10);
                return(false);
            }

            switch (operation.ToLower())
            {
            case OPERATION_EQUALITY:
                stringComp.CompDeleg = data => stringComp.StringParameter(data).Equals(stringComp.CompareString);
                break;

            case OPERATION_NONEQUALITY:
                stringComp.CompDeleg = data => !stringComp.StringParameter(data).Equals(stringComp.CompareString);
                break;

            case OPERATION_CONTAINS:
                stringComp.CompDeleg = data => stringComp.StringParameter(data).Contains(stringComp.CompareString);
                break;

            case OPERATION_NOTCONTAINS:
                stringComp.CompDeleg = data => !stringComp.StringParameter(data).Contains(stringComp.CompareString);
                break;


            case OPERATION_BIGGER:
                if (stringComp.IntParameter == null)
                {
                    BasePlugin.LogMessage(
                        $"Filter parser error: Can't compare string parameter with {OPERATION_BIGGER} (numerical) operation. Statement: {command}",
                        10);
                    return(false);
                }
                stringComp.CompDeleg = data => stringComp.IntParameter(data) > stringComp.CompareInt;
                break;

            case OPERATION_LESS:
                if (stringComp.IntParameter == null)
                {
                    BasePlugin.LogMessage(
                        $"Filter parser error: Can't compare string parameter with {OPERATION_LESS} (numerical) operation. Statement: {command}",
                        10);
                    return(false);
                }
                stringComp.CompDeleg = data => stringComp.IntParameter(data) < stringComp.CompareInt;
                break;

            case OPERATION_LESSEQUAL:
                if (stringComp.IntParameter == null)
                {
                    BasePlugin.LogMessage(
                        $"Filter parser error: Can't compare string parameter with {OPERATION_LESSEQUAL} (numerical) operation. Statement: {command}",
                        10);
                    return(false);
                }
                stringComp.CompDeleg = data => stringComp.IntParameter(data) <= stringComp.CompareInt;
                break;

            case OPERATION_BIGGERQUAL:
                if (stringComp.IntParameter == null)
                {
                    BasePlugin.LogMessage(
                        $"Filter parser error: Can't compare string parameter with {OPERATION_BIGGERQUAL} (numerical) operation. Statement: {command}",
                        10);
                    return(false);
                }
                stringComp.CompDeleg = data => stringComp.IntParameter(data) >= stringComp.CompareInt;
                break;

            default:
                BasePlugin.LogMessage(
                    $"Filter parser: Operation is not defined in code: {operation}", 10);
                return(false);
            }

            newFilter.Filters.Add(stringComp);
            return(true);
        }