Пример #1
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 (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 =>
                {
                    var(taskPlayer, taskObjects, taskEventHandlers, taskName, playerRef) =
                        (ValueTuple <IPlayer, object[], HashSet <Function>, string, PlayerRef>)obj;

                    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.Dispose();
                },
                                      new ValueTuple <IPlayer, object[], HashSet <Function>, string, PlayerRef>(player, objects,
                                                                                                                eventHandlers,
                                                                                                                name, new PlayerRef(player)));
            }

            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;
                    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.Dispose();
                },
                                      new ValueTuple <IPlayer, object[], HashSet <Function>, string, PlayerRef>(player, objects,
                                                                                                                eventHandlersClient,
                                                                                                                name, new PlayerRef(player)));
            }

            if (asyncEventBusClientDelegate.Count != 0 &&
                asyncEventBusClientDelegate.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(taskPlayer, taskObjects, taskEventHandlers, playerRef) =
                        (ValueTuple <IPlayer, object[], HashSet <ClientEventAsyncDelegate>, PlayerRef>)obj;
                    foreach (var eventHandler in taskEventHandlers)
                    {
                        AsyncEventHandler <ClientEventAsyncDelegate> .ExecuteEventAsyncWithoutTask(eventHandler,
                                                                                                   @delegate => @delegate(taskPlayer, taskObjects));
                    }
                    playerRef.Dispose();
                },
                                      new ValueTuple <IPlayer, object[], HashSet <ClientEventAsyncDelegate>, PlayerRef>(player, objects,
                                                                                                                        eventDelegates, 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;
                    foreach (var eventHandler in taskEventHandlers.GetEvents())
                    {
                        AsyncEventHandler <PlayerClientEventAsyncDelegate> .ExecuteEventAsyncWithoutTask(eventHandler,
                                                                                                         @delegate => @delegate(taskPlayer, taskName, taskObjects));
                    }
                    playerRef.Dispose();
                },
                                      new ValueTuple <IPlayer, object[], AsyncEventHandler <PlayerClientEventAsyncDelegate>, string, PlayerRef>(player,
                                                                                                                                                objects, PlayerClientEventAsyncEventHandler, name, new PlayerRef(player)));
            }
        }
Пример #2
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));
                    }
                });
            }
        }
Пример #3
0
 public override void OnScriptLoaded(IScript script)
 {
     AltAsync.RegisterEvents(script);
 }
Пример #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));
                    }
                });
            }
        }