Exemple #1
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));
            }
        }
        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));
            }
        }