ServerBrowser: Support bitwise operators in filter

This commit is contained in:
Palapeli 2025-03-04 04:00:24 -05:00
parent f8339dbf87
commit 34d13199b0
No known key found for this signature in database
GPG Key ID: 1FFE8F556A474925
3 changed files with 71 additions and 2 deletions

View File

@ -76,6 +76,16 @@ func (this *expression) switchFunction(node *TreeNode) int64 {
return this.evalMathOperator(this.evalMathPlus, node.Items())
case "-":
return this.evalMathOperator(this.evalMathMinus, node.Items())
case "&":
return this.evalMathOperator(this.evalMathAnd, node.Items())
case "|":
return this.evalMathOperator(this.evalMathOr, node.Items())
case "^":
return this.evalMathOperator(this.evalMathXor, node.Items())
case "<<":
return this.evalMathOperator(this.evalMathLShift, node.Items())
case ">>":
return this.evalMathOperator(this.evalMathRShift, node.Items())
case "and":
return this.evalAnd(node.Items())
@ -292,6 +302,26 @@ func (this *expression) evalMathLessOrEqual(val1, val2 int64) int64 {
return 0
}
func (this *expression) evalMathAnd(val1, val2 int64) int64 {
return val1 & val2
}
func (this *expression) evalMathOr(val1, val2 int64) int64 {
return val1 | val2
}
func (this *expression) evalMathXor(val1, val2 int64) int64 {
return val1 ^ val2
}
func (this *expression) evalMathLShift(val1, val2 int64) int64 {
return val1 << val2
}
func (this *expression) evalMathRShift(val1, val2 int64) int64 {
return val1 >> val2
}
func (this *expression) evalLike(args []*TreeNode) int64 {
cnt := len(args)
switch {

View File

@ -172,9 +172,8 @@ func (this OperatorPrecedence) All() []string {
}
var operators = OperatorPrecedence{
{"^"},
{"*", "/", "%"},
{"+", "-"},
{"+", "-", "&", "^", "|"},
{"==", "=", "!=", ">=", "<=", ">", "<"},
{"&&", "and"},
{"||", "or", "like"},

View File

@ -0,0 +1,40 @@
package serverbrowser
import (
"testing"
"wwfc/serverbrowser/filter"
)
func parseFilter(t *testing.T, expression string) error {
_, err := filter.Parse(expression)
if err != nil {
t.Error(err)
}
return err
}
func evalFilter(t *testing.T, expression string, queryGame string, context map[string]string) (int64, error) {
tree, err := filter.Parse(expression)
if err != nil {
t.Error(err)
return 0, err
}
ret, err := filter.Eval(tree, context, queryGame)
if err != nil {
t.Error(err)
return 0, err
}
return ret, err
}
func TestParseFilter(t *testing.T) {
parseFilter(t, `dwc_mver = 3 and dwc_pid != 1000004498 and maxplayers = 3 and numplayers < 3 and dwc_mtype = 0 and dwc_mresv != dwc_pid and (((20=auth)AND((1&mskdif)=mskdif)AND((14&mskstg)=mskstg)))`)
evalFilter(t, `dwc_mver = 3 and dwc_pid != 1000004498 and maxplayers = 3 and numplayers < 3 and dwc_mtype = 0 and dwc_mresv != dwc_pid and (((20=auth)AND((1&mskdif)=mskdif)AND((14&mskstg)=mskstg)))`, "fstarzerods", map[string]string{})
evalFilter(t, `1&mskdif`, "fstarzerods", map[string]string{
"mskdif": "1",
})
}