Пример #1
0
        public override void OnServerEventEvent(string name, ref MValueArray args, MValue[] mValues, object[] objects)
        {
            base.OnServerEventEvent(name, ref args, mValues, objects);

            if (AsyncEventHandlers.Count != 0 && AsyncEventHandlers.TryGetValue(name, out var eventHandlers))
            {
                if (mValues == null)
                {
                    mValues = args.ToArray();
                }

                if (objects == null)
                {
                    var length = mValues.Length;
                    objects = new object[length];
                    for (var i = 0; i < length; i++)
                    {
                        objects[i] = mValues[i].ToObject(BaseBaseObjectPool);
                    }
                }

                Task.Run(async() =>
                {
                    foreach (var eventHandler in eventHandlers)
                    {
                        var invokeValues = eventHandler.CalculateInvokeValues(objects);
                        if (invokeValues != null)
                        {
                            try
                            {
                                var task = eventHandler.InvokeTaskOrNull(invokeValues);
                                if (task != null)
                                {
                                    await task;
                                }
                            }
                            catch (Exception e)
                            {
                                AltAsync.Log($"Execution of {name} threw an error: {e}");
                            }
                        }
                        else
                        {
                            AltAsync.Log("Wrong function params for " + name);
                        }
                    }
                });
            }

            if (serverEventAsyncDelegateHandlers.Count != 0 &&
                serverEventAsyncDelegateHandlers.TryGetValue(name, out var eventDelegates))
            {
                if (mValues == null)
                {
                    mValues = args.ToArray();
                }

                if (objects == null)
                {
                    var length = mValues.Length;
                    objects = new object[length];
                    for (var i = 0; i < length; i++)
                    {
                        objects[i] = mValues[i].ToObject(BaseBaseObjectPool);
                    }
                }

                Task.Run(() =>
                {
                    foreach (var eventHandler in eventDelegates)
                    {
                        AsyncEventHandler <ServerEventAsyncDelegate> .ExecuteEventAsyncWithoutTask(eventHandler,
                                                                                                   @delegate => @delegate(objects));
                    }
                });
            }
        }
Пример #2
0
        public override void OnServerEventEvent(string name, IntPtr[] args, MValueConst[] mValues, object[] objects)
        {
            base.OnServerEventEvent(name, args, mValues, objects);

            var length = args.Length;

            if (asyncEventBusServer.Count != 0 && asyncEventBusServer.TryGetValue(name, out var eventHandlersServer))
            {
                if (mValues == null)
                {
                    mValues = new MValueConst[length];
                    for (var i = 0; i < length; i++)
                    {
                        mValues[i] = new MValueConst(args[i]);
                    }
                }

                if (objects == null)
                {
                    objects = new object[length];
                    for (var i = 0; i < length; i++)
                    {
                        objects[i] = mValues[i].ToObject();
                    }
                }

                Task.Factory.StartNew(async obj =>
                {
                    var(taskObjects, taskEventHandlers, taskName) =
                        (ValueTuple <object[], HashSet <Function>, string>)obj;
                    foreach (var eventHandler in taskEventHandlers)
                    {
                        var invokeValues = eventHandler.CalculateInvokeValues(taskObjects);
                        if (invokeValues != null)
                        {
                            try
                            {
                                var task = eventHandler.InvokeTaskOrNull(invokeValues);
                                if (task != null)
                                {
                                    await task;
                                }
                            }
                            catch (Exception e)
                            {
                                AltAsync.Log($"Execution of {taskName} threw an error: {e}");
                            }
                        }
                        else
                        {
                            AltAsync.Log("Wrong function params for " + taskName);
                        }
                    }
                }, new ValueTuple <object[], HashSet <Function>, string>(objects, eventHandlersServer, name));
            }

            if (asyncEventBus.Count != 0 && asyncEventBus.TryGetValue(name, out var eventHandlers))
            {
                if (mValues == null)
                {
                    mValues = new MValueConst[length];
                    for (var i = 0; i < length; i++)
                    {
                        mValues[i] = new MValueConst(args[i]);
                    }
                }

                if (objects == null)
                {
                    objects = new object[length];
                    for (var i = 0; i < length; i++)
                    {
                        objects[i] = mValues[i].ToObject();
                    }
                }

                Task.Factory.StartNew(async obj =>
                {
                    try
                    {
                        var(taskObjects, taskEventHandlers, taskName) =
                            (ValueTuple <object[], HashSet <Function>, string>)obj;
                        foreach (var eventHandler in taskEventHandlers)
                        {
                            var invokeValues = eventHandler.CalculateInvokeValues(taskObjects);
                            if (invokeValues != null)
                            {
                                try
                                {
                                    var task = eventHandler.InvokeTaskOrNull(invokeValues);
                                    if (task != null)
                                    {
                                        await task;
                                    }
                                }
                                catch (Exception e)
                                {
                                    AltAsync.Log($"Execution of {taskName} threw an error: {e}");
                                }
                            }
                            else
                            {
                                AltAsync.Log("Wrong function params for " + taskName);
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        Console.WriteLine($"Execution of async events threw an error: {exception}");
                    }
                }, new ValueTuple <object[], HashSet <Function>, string>(objects, eventHandlers, name));
            }

            if (asyncEventBusServerDelegate.Count != 0 &&
                asyncEventBusServerDelegate.TryGetValue(name, out var eventDelegates))
            {
                if (mValues == null)
                {
                    mValues = new MValueConst[length];
                    for (var i = 0; i < length; i++)
                    {
                        mValues[i] = new MValueConst(args[i]);
                    }
                }

                if (objects == null)
                {
                    objects = new object[length];
                    for (var i = 0; i < length; i++)
                    {
                        objects[i] = mValues[i].ToObject();
                    }
                }

                Task.Factory.StartNew(obj =>
                {
                    var(taskObjects, taskEventHandlers, taskName) =
                        (ValueTuple <object[], HashSet <ServerEventAsyncDelegate>, string>)obj;
                    foreach (var eventHandler in taskEventHandlers)
                    {
                        AsyncEventHandler <ServerEventAsyncDelegate> .ExecuteEventAsyncWithoutTask(eventHandler,
                                                                                                   @delegate => @delegate(taskObjects));
                    }
                }, new ValueTuple <object[], HashSet <ServerEventAsyncDelegate>, string>(objects, eventDelegates, name));
            }
        }
Пример #3
0
        //TODO: we could write mvalue's to own onion struct in cpp to better share it but we would need to execute at least getorcreate entity when it contains a entity type in main thread
        //TODO: or lock entities dictionary so entity can't get removed until thread got it from dictionary
        //TODO: lock dictionary for async maybe as well for use cases like this
        public override void OnClientEventEvent(IPlayer player, string name, IntPtr[] args, MValueConst[] mValues,
                                                object[] objects)
        {
            base.OnClientEventEvent(player, name, args, mValues, objects);
            var length = args.Length;

            if (asyncEventBusClient.Count != 0 && asyncEventBusClient.TryGetValue(name, out var eventHandlersClient))
            {
                if (mValues == null)
                {
                    mValues = new MValueConst[length];
                    for (var i = 0; i < length; i++)
                    {
                        mValues[i] = new MValueConst(args[i]);
                    }
                }

                if (objects == null)
                {
                    objects = new object[length];
                    for (var i = 0; i < length; i++)
                    {
                        objects[i] = mValues[i].ToObject();
                    }
                }

                Task.Factory.StartNew(async obj =>
                {
                    var(taskPlayer, taskObjects, taskEventHandlers, taskName, playerRef) =
                        (ValueTuple <IPlayer, object[], HashSet <Function>, string, PlayerRef>)obj;
                    playerRef.DebugCountUp();
                    foreach (var eventHandler in taskEventHandlers)
                    {
                        try
                        {
                            var invokeValues = eventHandler.CalculateInvokeValues(taskPlayer, taskObjects);
                            if (invokeValues != null)
                            {
                                var task = eventHandler.InvokeTaskOrNull(invokeValues);
                                if (task != null)
                                {
                                    await task;
                                }
                            }
                            else
                            {
                                AltAsync.Log("Wrong function params for " + taskName);
                            }
                        }
                        catch (Exception e)
                        {
                            AltAsync.Log($"Execution of {taskName} threw an error: {e}");
                        }
                    }

                    playerRef.DebugCountDown();
                    playerRef.Dispose();
                },
                                      new ValueTuple <IPlayer, object[], HashSet <Function>, string, PlayerRef>(player, objects,
                                                                                                                eventHandlersClient,
                                                                                                                name, new PlayerRef(player)));
            }

            if (PlayerClientEventAsyncEventHandler.HasEvents())
            {
                if (mValues == null)
                {
                    mValues = new MValueConst[length];
                    for (var i = 0; i < length; i++)
                    {
                        mValues[i] = new MValueConst(args[i]);
                    }
                }

                if (objects == null)
                {
                    objects = new object[length];
                    for (var i = 0; i < length; i++)
                    {
                        objects[i] = mValues[i].ToObject();
                    }
                }

                Task.Factory.StartNew(obj =>
                {
                    var(taskPlayer, taskObjects, taskEventHandlers, taskName, playerRef) =
                        (ValueTuple <IPlayer, object[], AsyncEventHandler <PlayerClientEventAsyncDelegate>, string,
                                     PlayerRef>)
                        obj;
                    playerRef.DebugCountUp();
                    foreach (var eventHandler in taskEventHandlers.GetEvents())
                    {
                        AsyncEventHandler <PlayerClientEventAsyncDelegate> .ExecuteEventAsyncWithoutTask(eventHandler,
                                                                                                         @delegate => @delegate(taskPlayer, taskName, taskObjects));
                    }

                    playerRef.DebugCountDown();
                    playerRef.Dispose();
                },
                                      new ValueTuple <IPlayer, object[], AsyncEventHandler <PlayerClientEventAsyncDelegate>, string,
                                                      PlayerRef>(player,
                                                                 objects, PlayerClientEventAsyncEventHandler, name, new PlayerRef(player)));
            }
        }
Пример #4
0
        public override void OnClientEventEvent(IPlayer player, string name, ref MValueArray args, MValue[] mValues,
                                                object[] objects)
        {
            base.OnClientEventEvent(player, name, ref args, mValues, objects);

            if (AsyncEventHandlers.Count != 0 && AsyncEventHandlers.TryGetValue(name, out var eventHandlers))
            {
                if (mValues == null)
                {
                    mValues = args.ToArray();
                }

                if (objects == null)
                {
                    var length = mValues.Length;
                    objects = new object[length];
                    for (var i = 0; i < length; i++)
                    {
                        objects[i] = mValues[i].ToObject(BaseBaseObjectPool);
                    }
                }

                Task.Run(() =>
                {
                    foreach (var eventHandler in eventHandlers)
                    {
                        var invokeValues = eventHandler.CalculateInvokeValues(objects);
                        if (invokeValues != null)
                        {
                            eventHandler.InvokeNoResult(invokeValues);
                        }
                        else
                        {
                            AltAsync.Log("Wrong function params for " + name);
                        }
                    }
                });
            }

            if (clientEventAsyncDelegateHandlers.Count != 0 &&
                clientEventAsyncDelegateHandlers.TryGetValue(name, out var eventDelegates))
            {
                if (mValues == null)
                {
                    mValues = args.ToArray();
                }

                if (objects == null)
                {
                    var length = mValues.Length;
                    objects = new object[length];
                    for (var i = 0; i < length; i++)
                    {
                        objects[i] = mValues[i].ToObject(BaseBaseObjectPool);
                    }
                }

                Task.Run(() =>
                {
                    foreach (var eventHandler in eventDelegates)
                    {
                        AsyncEventHandler <ClientEventAsyncDelegate> .ExecuteEventAsyncWithoutTask(eventHandler,
                                                                                                   @delegate => @delegate(player, objects));
                    }
                });
            }

            if (PlayerClientEventAsyncEventHandler.HasEvents())
            {
                if (mValues == null)
                {
                    mValues = args.ToArray();
                }

                if (objects == null)
                {
                    var length = mValues.Length;
                    objects = new object[length];
                    for (var i = 0; i < length; i++)
                    {
                        objects[i] = mValues[i].ToObject(BaseBaseObjectPool);
                    }
                }

                Task.Run(() =>
                {
                    foreach (var eventHandler in PlayerClientEventAsyncEventHandler.GetEvents())
                    {
                        AsyncEventHandler <PlayerClientEventAsyncDelegate> .ExecuteEventAsyncWithoutTask(eventHandler,
                                                                                                         @delegate => @delegate(player, name, objects));
                    }
                });
            }
        }