Пример #1
0
        public void CallExitEvent(LuaFile luaFile)
        {
            var exitCallbacks = RegisteredFunctions
                                .ForFile(luaFile)
                                .ForEvent("OnExit");

            foreach (var exitCallback in exitCallbacks)
            {
                exitCallback.Call();
            }
        }
Пример #2
0
        private void LoadList( )
        {
            listBox1.Items.Clear();
            Excel.Application app = ExcelDnaUtil.Application as Excel.Application;
            dynamic           wb  = app.ActiveWorkbook;
            dynamic           ws  = app.ActiveSheet;

            if (TAG == "xll")
            {
                dynamic RegisteredFunctions;
                RegisteredFunctions = app.RegisteredFunctions;
                if (RegisteredFunctions == null)
                {
                    return;
                }
                for (int i = RegisteredFunctions.GetLowerBound(0); i <= RegisteredFunctions.GetUpperBound(0); i++)
                {
                    if (!this.listBox1.Items.Contains(RegisteredFunctions[i, RegisteredFunctions.GetLowerBound(1)].ToString()))
                    {
                        listBox1.Items.Add(RegisteredFunctions[i, RegisteredFunctions.GetLowerBound(1)].ToString());
                    }
                }
            }
            else if (TAG == "com2")
            {
                foreach (dynamic j in app.AddIns2)
                {
                    if (!this.listBox1.Items.Contains(j.FullName))
                    {
                        listBox1.Items.Add(j.FullName);
                    }
                }
            }
            else if (TAG == "com1")
            {
                foreach (dynamic k in app.AddIns)
                {
                    if (!this.listBox1.Items.Contains(k.FullName))
                    {
                        listBox1.Items.Add(k.FullName);
                    }
                }
            }
        }
Пример #3
0
        public static void RegisterMethod(MethodInfo method)
        {
            var attr = method.GetCustomAttribute <Registered>();

            if (attr == null)
            {
                return;
            }
            if (RegisteredFunctions.ContainsKey(attr.Name))
            {
                throw new Exception("Key Duplicated");
            }
            if (RegisteredVariables.ContainsKey(attr.Name))
            {
                throw new Exception("Key Duplicated");
            }
            Func <object[], object> func = objs => method.Invoke(null, new object[] { objs });

            RegisteredFunctions.Add(attr.Name, func);
            RegisteredVariables.Add(attr.Name, func);
        }
Пример #4
0
        private void HandleMessage(string message)
        {
            List <object> data    = JsonConvert.DeserializeObject <List <object> >(message);
            string        msgType = (string)data[0];

            if (msgType == "call-reply")
            {
                int callID = Convert.ToInt32(data[1]);
                if (ActiveCalls.ContainsKey(callID))
                {
                    bool   success           = (bool)data[2];
                    object retValOrException = data[3];
                    ActiveCalls[callID].SetResult(success ? "result" : "exception",
                                                  retValOrException);
                }
                else
                {
                    throw new Error(ErrorCode.CONNECTION_ERROR,
                                    "Received a response for an unrecognized call id: " + callID);
                }
            }
            else if (msgType == "call")
            {
                int    callID     = Convert.ToInt32(data[1]);
                string methodName = (string)data[2];
                if (RegisteredFunctions.ContainsKey(methodName))
                {
                    Delegate        nativeMethod = RegisteredFunctions[methodName];
                    ParameterInfo[] paramInfo    = nativeMethod.Method.GetParameters();
                    if (paramInfo.Length != data.Count - 3)
                    {
                        throw new Error(ErrorCode.INVALID_ARGUMENT,
                                        "Incorrect number of arguments for method: " + methodName +
                                        ". Expected: " + paramInfo.Length + ". Got: " +
                                        (data.Count - 3));
                    }
                    List <object> parameters = new List <object>();
                    for (int i = 0; i < paramInfo.Length; i++)
                    {
                        parameters.Add(ConversionUtils.CastJObject(
                                           data[i + 3], paramInfo[i].ParameterType));
                    }

                    object returnValue = null;
                    object exception   = null;
                    bool   success     = true;

                    try
                    {
                        returnValue = nativeMethod.DynamicInvoke(parameters.ToArray());
                    }
                    catch (Exception e)
                    {
                        exception = e;
                        success   = false;
                    }

                    if (!IsOneWay(methodName))
                    {
                        // Send call-reply message.
                        List <object> callReplyMessage = new List <object>();
                        callReplyMessage.Add("call-reply");
                        callReplyMessage.Add(callID);
                        callReplyMessage.Add(success);

                        if (!success)
                        {
                            callReplyMessage.Add(exception);
                        }
                        else if (nativeMethod.Method.ReturnType != typeof(void))
                        {
                            callReplyMessage.Add(returnValue);
                        }

                        Connection.Send(JsonConvert.SerializeObject(callReplyMessage));
                    }
                }
                else
                {
                    throw new Error(ErrorCode.CONNECTION_ERROR,
                                    "Received a call for an unregistered method: " + methodName);
                }
            }
            else
            {
                throw new Error(ErrorCode.CONNECTION_ERROR, "Unknown message type: " + msgType);
            }
        }
Пример #5
0
 public static void RegisterMethod(string name, Func <object[], object> method)
 {
     RegisteredFunctions.Add(name, method);
     RegisteredVariables.Add(name, method);
 }