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)); } }
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)); } }