private void HandleRemoteInvoke(CommandContextEx cmdContext) { CLRLogger.GetInstance().LogDevInfo("Handle remote invoke, sender is " + cmdContext.sSender); byte[] request = cmdContext.sSerializeObject; _currentRemoteInvoker.Value = cmdContext.sSender; BinaryFormatter formatter = new BinaryFormatter(); RemoteMethodInvokeArgument invokeArgs = null; using (MemoryStream inStream = new MemoryStream(request)) { invokeArgs = formatter.Deserialize(inStream) as RemoteMethodInvokeArgument; } if (invokeArgs == null) { throw new Exception("Invalid remote argument"); } object obj = NativeInvoke(invokeArgs.ObjectName, invokeArgs.Method, invokeArgs.Parameters); using (MemoryStream outStream = new MemoryStream()) { formatter.Serialize(outStream, obj); byte[] response = outStream.GetBuffer(); int errorCode = cmdContext.Reply(response); if (errorCode != 0) { throw new CommunicationException(errorCode, "fail to reply to " + cmdContext.sSender); } } }
private async Task ExecuteMethodAsync(CommandContextEx context, MethodInfo methodInfo, object[] parameters, bool silent = false) { if (parameters == null) { parameters = new object[] { } } ; if (methodInfo == null) { return; } try { if (methodInfo.DeclaringType.CreateInstance() is ModuleBaseClass instance) { instance.Context = context; var @return = methodInfo.Invoke(instance, parameters); if (@return is Task) { await((Task)@return).ConfigureAwait(false); } var cmdAttribute = methodInfo.GetCustomAttribute <DiscordCommandAttribute>(); if (cmdAttribute?.DeleteUserMessage == true) { await context.Message.DeleteAfterAsync(cmdAttribute?.DeleteUserMessageTimer ?? TimeSpan.Zero); } } } catch (Exception ex) { if (!silent) { Log.Error("Error occurred at ExecuteMethodAsync()", ex); } } }
private Task ExecuteMethodAsync(CommandContextEx context, MethodInfo methodInfo, IEnumerable <object> parameters, bool silent = false) => ExecuteMethodAsync(context, methodInfo, parameters.ToArray(), silent);