Exemple #1
0
        private static object[] ParseParameters(MethodInfo mi, JsonElement body)
        {
            Dictionary <string, object> parameters = LCEngine.Decode(body);
            List <object> ps = new List <object>();

            if (mi.GetParameters().Length > 0)
            {
                if (Array.Exists(mi.GetParameters(),
                                 p => p.GetCustomAttribute <LCEngineFunctionParamAttribute>() != null))
                {
                    // 如果包含 LCEngineFunctionParamAttribute 的参数,则按照配对方式传递参数
                    foreach (ParameterInfo pi in mi.GetParameters())
                    {
                        LCEngineFunctionParamAttribute attr = pi.GetCustomAttribute <LCEngineFunctionParamAttribute>();
                        if (attr != null)
                        {
                            string paramName = attr.ParamName;
                            ps.Add(parameters[paramName]);
                        }
                    }
                }
                else
                {
                    ps.Add(LCDecoder.Decode(LCEngine.Decode(body)));
                }
            }

            return(ps.ToArray());
        }
Exemple #2
0
        public async Task <object> RPC(string funcName, JsonElement body)
        {
            try {
                LCLogger.Debug($"RPC: {funcName}");
                LCLogger.Debug(body.ToString());

                if (Functions.TryGetValue(funcName, out MethodInfo mi))
                {
                    LCEngine.InitRequestContext(Request);

                    object[] ps     = ParseParameters(mi, body);
                    object   result = await LCEngine.Invoke(mi, ps);

                    if (result != null)
                    {
                        return(new Dictionary <string, object> {
                            { "result", LCCloud.Encode(result) }
                        });
                    }
                }
                return(body);
            } catch (Exception e) {
                return(StatusCode(500, e.Message));
            }
        }
Exemple #3
0
 public object GetFunctions()
 {
     try {
         return(LCEngine.GetFunctions(Request));
     } catch (Exception e) {
         return(StatusCode(500, e.Message));
     }
 }
        private static async Task <object> Invoke(MethodInfo mi, Dictionary <string, object> dict)
        {
            LCObjectData objectData = LCObjectData.Decode(dict["object"] as Dictionary <string, object>);

            objectData.ClassName = "_User";

            LCObject user = LCObject.Create("_User");

            user.Merge(objectData);

            return(await LCEngine.Invoke(mi, new object[] { user }) as LCObject);
        }
        public async Task <object> Hook(string className, string hookName, JsonElement body)
        {
            try {
                LCLogger.Debug($"Hook: {className}#{hookName}");
                LCLogger.Debug(body.ToString());

                LCEngine.CheckHookKey(Request);

                string classHookName = GetClassHookName(className, hookName);
                if (ClassHooks.TryGetValue(classHookName, out MethodInfo mi))
                {
                    Dictionary <string, object> data = LCEngine.Decode(body);

                    LCObjectData objectData = LCObjectData.Decode(data["object"] as Dictionary <string, object>);
                    objectData.ClassName = className;
                    LCObject obj = LCObject.Create(className);
                    obj.Merge(objectData);

                    // 避免死循环
                    if (hookName.StartsWith("before"))
                    {
                        obj.DisableBeforeHook();
                    }
                    else
                    {
                        obj.DisableAfterHook();
                    }

                    LCEngine.InitRequestContext(Request);

                    LCUser user = null;
                    if (data.TryGetValue("user", out object userObj) &&
                        userObj != null)
                    {
                        user = new LCUser();
                        user.Merge(LCObjectData.Decode(userObj as Dictionary <string, object>));
                        LCEngineRequestContext.CurrentUser = user;
                    }

                    LCObject result = await LCEngine.Invoke(mi, new object[] { obj }) as LCObject;

                    if (result != null)
                    {
                        return(LCCloud.Encode(result));
                    }
                }
                return(body);
            } catch (Exception e) {
                return(StatusCode(500, e.Message));
            }
        }
        public async Task <object> HookLogin(JsonElement body)
        {
            try {
                LCLogger.Debug(LCEngine.OnLogin);
                LCLogger.Debug(body.ToString());

                LCEngine.CheckHookKey(Request);

                if (UserHooks.TryGetValue(LCEngine.OnLogin, out MethodInfo mi))
                {
                    LCEngine.InitRequestContext(Request);

                    Dictionary <string, object> dict = LCEngine.Decode(body);
                    return(await Invoke(mi, dict));
                }
                return(body);
            } catch (Exception e) {
                return(StatusCode(500, e.Message));
            }
        }