public void CallCommandHandler(ITextBuilder responseBuilder, String methodName, String arguments)
        {
            //
            // Parse Parameters
            //
            List <String> parametersList = new List <String>();

            try
            {
                Npc.ParseParameters(arguments, parametersList);
            }
            catch (FormatException e)
            {
                callback.GotInvalidData(clientString, e.Message);
                AppendNpcError(responseBuilder, NpcErrorCode.InvalidCallParameters, e.Message);
                return;
            }

            String[] parameters = (parametersList == null) ? null : parametersList.ToArray();
            try
            {
                NpcReturnObjectOrException returnObject = npcExecutor.ExecuteWithStrings(methodName, parameters);
                if (returnObject.exception != null)
                {
                    callback.FunctionCallThrewException(clientString, methodName, returnObject.exception);
                }
                else
                {
                    callback.FunctionCall(clientString, methodName);
                }

                returnObject.AppendNpcReturnLine(responseBuilder);
                return;
            }
            catch (NpcErrorException ne)
            {
                callback.ExceptionDuringExecution(clientString, methodName, ne);
                responseBuilder.Clear();
                AppendNpcError(responseBuilder, ne.errorCode, ne.Message);
            }
            catch (Exception e)
            {
                callback.ExceptionDuringExecution(clientString, methodName, e);
                responseBuilder.Clear();
                AppendNpcError(responseBuilder, NpcErrorCode.UnhandledException, e.GetType().Name + ": " + e.Message);
            }
        }
Exemple #2
0
        public void GenerateCallPage(ITextBuilder htmlBuilder, string call)
        {
            Int32 questionMarkIndex = call.IndexOf('?');

            String methodName;

            String[] parameters = null;
            if (questionMarkIndex < 0)
            {
                methodName = call;
            }
            else
            {
                methodName = call.Remove(questionMarkIndex);
                String parametersQueryString = (questionMarkIndex >= call.Length - 1) ? null :
                                               call.Substring(questionMarkIndex + 1);

                if (String.IsNullOrEmpty(methodName))
                {
                    throw new FormatException(String.Format("Call '{0}' is missing method name", call));
                }
                else if (!String.IsNullOrEmpty(parametersQueryString))
                {
                    parameters = parametersQueryString.Split('&');
                    for (int i = 0; i < parameters.Length; i++)
                    {
                        Int32 equalIndex = parameters[i].IndexOf('=');
                        if (equalIndex > 0)
                        {
                            parameters[i] = (equalIndex >= parameters[i].Length - 1) ? "" :
                                            parameters[i].Substring(equalIndex + 1);
                        }
                    }
                }
            }

            UInt16 parameterCount = (parameters == null) ? (UInt16)0 : (UInt16)parameters.Length;

            NpcExecutionObject executionObject;
            NpcMethodInfo      npcMethodInfo = npcExecutor.GetNpcMethodInfo(methodName, parameterCount, out executionObject);

            NpcReturnObjectOrException returnObject = npcExecutor.ExecuteWithStrings(executionObject, npcMethodInfo, parameters);

            if (returnObject.exception == null)
            {
                htmlBuilder.AppendAscii("<div style=\"background:#333;color:#0f5;padding:5px;\"><h1>Success</h1></div>");
            }
            else
            {
                htmlBuilder.AppendAscii("<div style=\"background:#333;color:#f00;padding:5px;\"><h1>Exception</h1></div>");
            }

            htmlBuilder.AppendAscii("<br/>");

            //htmlBuilder.AppendFormat("<div><div><span class=\"SectionTitle\">Function Called</span><hr/></div> {0}&nbsp;<span class=\"bold\">{1}</span>(", TypeAsHtml(npcMethodInfo.methodInfo.ReturnType), methodName);
            htmlBuilder.AppendAscii("<div><div><span class=\"SectionTitle\">Function Called</span><hr/></div> ");
            htmlBuilder.AppendAscii(TypeAsHtml(npcMethodInfo.methodInfo.ReturnType));
            htmlBuilder.AppendAscii("&nbsp;<span class=\"bold\">");
            htmlBuilder.AppendAscii(methodName);
            htmlBuilder.AppendAscii("</span>(");
            if (parameterCount > 0)
            {
                htmlBuilder.AppendAscii("<table>");
                for (int i = 0; i < parameters.Length; i++)
                {
                    ParameterInfo parameterInfo   = npcMethodInfo.parameters[i];
                    String        parameterString = parameters[i];
                    //htmlBuilder.AppendFormat("<tr><td>&nbsp;{0}</td><td>&nbsp;{1}</td><td>&nbsp;=&nbsp;</td><td>{2}</td></tr>",
                    //    TypeAsHtml(parameterInfo.ParameterType), parameterInfo.Name, parameterString);
                    htmlBuilder.AppendAscii("<tr><td>&nbsp;");
                    htmlBuilder.AppendAscii(TypeAsHtml(parameterInfo.ParameterType));
                    htmlBuilder.AppendAscii("</td><td>&nbsp;");
                    htmlBuilder.AppendAscii(parameterInfo.Name);
                    htmlBuilder.AppendAscii("</td><td>&nbsp;=&nbsp;</td><td>");
                    htmlBuilder.AppendAscii(parameterString);
                    htmlBuilder.AppendAscii("</td></tr>");
                }
                htmlBuilder.AppendAscii("</table>");
            }
            htmlBuilder.AppendAscii(")</div>");

            htmlBuilder.AppendAscii("<br/>");

            if (returnObject.exception == null)
            {
                if (returnObject.type != typeof(void))
                {
                    //htmlBuilder.AppendFormat("<div><span class=\"SectionTitle\">Return Value</span>&nbsp;{0}<hr/></div>", TypeAsHtml(returnObject.type));
                    htmlBuilder.AppendAscii("<div><span class=\"SectionTitle\">Return Value</span>&nbsp;");
                    htmlBuilder.AppendAscii(TypeAsHtml(returnObject.type));
                    htmlBuilder.AppendAscii("<hr/></div>");
                    GenerateHtmlValue(htmlBuilder, returnObject.value);
                }
            }
            else
            {
                //htmlBuilder.AppendFormat("<div><span class=\"SectionTitle\">Exception</span>&nbsp;<span class=\"cstype\">{0}</span><hr/></div>", returnObject.type.FullName);
                htmlBuilder.AppendAscii("<div><span class=\"SectionTitle\">Exception</span>&nbsp;<span class=\"cstype\">");
                htmlBuilder.AppendAscii(returnObject.type.FullName);
                htmlBuilder.AppendAscii("</span><hr/></div>");
                GenerateExceptionHtml(htmlBuilder, returnObject.exception);
            }
        }