コード例 #1
0
        private static TestScriptValue Eval(TestScriptExpression expression, TestScriptEvalContext context)
        {
            switch (expression.Type)
            {
            case TestScriptExpressionType.String:
                return(new TestScriptValue(((TestScriptString)expression).Text, TestScriptValueType.String));

            case TestScriptExpressionType.Integer:
                return(new TestScriptValue(((TestScriptInteger)expression).Value, TestScriptValueType.Long));

            case TestScriptExpressionType.EnumValue:
            {
                TestScriptEnumValue enumExp = (TestScriptEnumValue)expression;

                ProtoSpecModule module = context.ProtoSpec.Modules.GetByName(enumExp.Module, true);

                if (module == null)
                {
                    Error("上下文中并不包含模块“" + module + "”的定义");
                }

                long value = module.EnumValues.GetValueByName(enumExp.Name, true);

                if (value < 0)
                {
                    Error("模块“" + module + "”中并不包含枚举值“" + enumExp.Name + "”的定义");
                }

                return(TestScriptValue.CreateEnum(enumExp.Module, enumExp.Name, value));
            }

            case TestScriptExpressionType.FunctionCall:
            {
                TestScriptFunctionCall funExp = (TestScriptFunctionCall)expression;

                ProtoSpecModule module = context.ProtoSpec.Modules.GetByName(funExp.Module, true);

                if (module == null)
                {
                    Error("上下文中不存在模块“" + module + "”");
                }

                ProtoSpecAction action = module.Actions.GetByName(funExp.Function, true);

                if (action == null)
                {
                    Error("模块“" + module + "”中不存在操作“" + funExp.Function + "”");
                }

                return(SendData(context, action, funExp.ParamList));
            }
            }

            return(null);
        }
コード例 #2
0
        private static void ParseResponse(string module, byte[] data, ref int parseOffset, ProtoSpecSubset format, TestScriptValue recvValue)
        {
            foreach (ProtoSpecColumn column in format.Columns)
            {
                switch (column.ColumnType)
                {
                case ProtoSpecColumnType.Byte:
                {
                    byte value = data[parseOffset++];

                    recvValue.AddProperty(
                        column.Name,
                        new TestScriptValue(value, TestScriptValueType.Byte)
                        );
                }
                break;

                case ProtoSpecColumnType.Short:
                {
                    byte[] bytes = new byte[2];

                    bytes[1] = data[parseOffset++];
                    bytes[0] = data[parseOffset++];

                    short value = BitConverter.ToInt16(bytes, 0);

                    recvValue.AddProperty(
                        column.Name,
                        new TestScriptValue(value, TestScriptValueType.Short)
                        );
                }
                break;

                case ProtoSpecColumnType.Int:
                {
                    byte[] bytes = new byte[4];

                    bytes[3] = data[parseOffset++];
                    bytes[2] = data[parseOffset++];
                    bytes[1] = data[parseOffset++];
                    bytes[0] = data[parseOffset++];

                    int value = BitConverter.ToInt32(bytes, 0);

                    recvValue.AddProperty(
                        column.Name,
                        new TestScriptValue(value, TestScriptValueType.Integer)
                        );
                }
                break;

                case ProtoSpecColumnType.Long:
                {
                    byte[] bytes = new byte[8];

                    bytes[7] = data[parseOffset++];
                    bytes[6] = data[parseOffset++];
                    bytes[5] = data[parseOffset++];
                    bytes[4] = data[parseOffset++];
                    bytes[3] = data[parseOffset++];
                    bytes[2] = data[parseOffset++];
                    bytes[1] = data[parseOffset++];
                    bytes[0] = data[parseOffset++];

                    long value = BitConverter.ToInt64(bytes, 0);

                    recvValue.AddProperty(
                        column.Name,
                        new TestScriptValue(value, TestScriptValueType.Long)
                        );
                }
                break;

                case ProtoSpecColumnType.String:
                {
                    byte[] packhead = new byte[2];

                    packhead[1] = data[parseOffset++];
                    packhead[0] = data[parseOffset++];

                    short len = BitConverter.ToInt16(packhead, 0);

                    string text = Encoding.UTF8.GetString(data, parseOffset, len);

                    parseOffset += len;

                    recvValue.AddProperty(
                        column.Name,
                        new TestScriptValue(text, TestScriptValueType.String)
                        );
                }
                break;

                case ProtoSpecColumnType.List:
                {
                    byte[] packhead = new byte[2];

                    packhead[1] = data[parseOffset++];
                    packhead[0] = data[parseOffset++];

                    short len = BitConverter.ToInt16(packhead, 0);

                    TestScriptValue list = TestScriptValue.CreateList();

                    for (int i = 0; i < len; i++)
                    {
                        TestScriptValue item = TestScriptValue.CreateObject();

                        ParseResponse(module, data, ref parseOffset, column.Format, item);

                        list.AddItem(item);
                    }

                    recvValue.AddProperty(column.Name, list);
                }
                break;

                case ProtoSpecColumnType.Enum:
                {
                    byte value = data[parseOffset++];

                    recvValue.AddProperty(
                        column.Name,
                        TestScriptValue.CreateEnum(module, column.Values.GetNameByValue(value), value)
                        );
                }
                break;
                }
            }
        }
コード例 #3
0
ファイル: TestScriptEval.cs プロジェクト: thisperfect/dhsx
        private static TestScriptValue Eval(TestScriptExpression expression, TestScriptEvalContext context)
        {
            switch (expression.Type)
            {
            case TestScriptExpressionType.String:
                return(new TestScriptValue(((TestScriptString)expression).Text, TestScriptValueType.String));

            case TestScriptExpressionType.Integer:
                return(new TestScriptValue(((TestScriptInteger)expression).Value, TestScriptValueType.Long));

            case TestScriptExpressionType.EnumValue:
            {
                TestScriptEnumValue enumExp = (TestScriptEnumValue)expression;

                ProtoSpecModule module = context.ProtoSpec.Modules.GetByName(enumExp.Module, true);

                if (module == null)
                {
                    Error("上下文中并不包含模块“" + module + "”的定义");
                }

                long value = module.EnumValues.GetValueByName(enumExp.Name, true);

                if (value < 0)
                {
                    Error("模块“" + module + "”中并不包含枚举值“" + enumExp.Name + "”的定义");
                }

                return(TestScriptValue.CreateEnum(enumExp.Module, enumExp.Name, value));
            }

            case TestScriptExpressionType.FunctionCall:
            {
                TestScriptFunctionCall funExp = (TestScriptFunctionCall)expression;

                if (funExp.Module == "proto")
                {
                    switch (funExp.Function)
                    {
                    case "clean":
                        if (funExp.ParamList.Count == 0)
                        {
                            Clean(context, 1000);
                        }
                        else if (funExp.ParamList.Count == 1 && funExp.ParamList[0].Type == TestScriptExpressionType.Integer)
                        {
                            Clean(context, (int)((TestScriptInteger)funExp.ParamList[0]).Value);
                        }
                        else
                        {
                            Error("函数“proto:clean()”的参数个数应该为0个或1个");
                        }
                        break;
                    }

                    return(null);
                }
                else
                {
                    ProtoSpecModule module = context.ProtoSpec.Modules.GetByName(funExp.Module, true);

                    if (module == null)
                    {
                        Error("上下文中不存在模块“" + module + "”");
                    }

                    ProtoSpecAction action = module.Actions.GetByName(funExp.Function, true);

                    if (action == null)
                    {
                        Error("模块“" + module + "”中不存在操作“" + funExp.Function + "”");
                    }

                    return(SendData(context, action, funExp.ParamList));
                }
            }
            }

            return(null);
        }