public static void RegisterEvents(object target) { ModuleScriptMethodIndexer.Index(target, new[] { typeof(AsyncServerEventAttribute), typeof(AsyncClientEventAttribute), typeof(AsyncScriptEventAttribute) }, (baseEvent, eventMethod, eventMethodDelegate) => { switch (baseEvent) { case AsyncScriptEventAttribute scriptEvent: var scriptEventType = scriptEvent.EventType; ScriptFunction scriptFunction; switch (scriptEventType) { case ScriptEventType.Checkpoint: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(ICheckpoint), typeof(IEntity), typeof(bool) }, true); if (scriptFunction == null) { return; } OnCheckpoint += (checkpoint, entity, state) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(checkpoint); currScriptFunction.Set(entity); currScriptFunction.Set(state); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.PlayerConnect: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(string) }, true); if (scriptFunction == null) { return; } OnPlayerConnect += (player, reason) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(player); currScriptFunction.Set(reason); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.PlayerBeforeConnect: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(PlayerConnectionInfo), typeof(string) }, true); if (scriptFunction == null) { return; } OnPlayerBeforeConnect += (connectionInfo, reason) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(connectionInfo); currScriptFunction.Set(reason); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.PlayerDamage: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(IEntity), typeof(ushort), typeof(ushort), typeof(ushort), typeof(ushort), typeof(uint), typeof(ushort), typeof(ushort) }, true); if (scriptFunction == null) { return; } OnPlayerDamage += (player, attacker, oldHealth, oldArmor, oldMaxHealth, oldMaxArmor, weapon, healthDamage, armourDamage) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(player); currScriptFunction.Set(attacker); currScriptFunction.Set(oldHealth); currScriptFunction.Set(oldArmor); currScriptFunction.Set(oldMaxHealth); currScriptFunction.Set(oldMaxArmor); currScriptFunction.Set(weapon); currScriptFunction.Set(healthDamage); currScriptFunction.Set(armourDamage); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.PlayerDead: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(IEntity), typeof(uint) }, true); if (scriptFunction == null) { return; } OnPlayerDead += (player, attacker, weapon) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(player); currScriptFunction.Set(attacker); currScriptFunction.Set(weapon); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.PlayerDisconnect: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(string) }, true); if (scriptFunction == null) { return; } OnPlayerDisconnect += (player, reason) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(player); currScriptFunction.Set(reason); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.PlayerRemove: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer) }, true); if (scriptFunction == null) { return; } OnPlayerRemove += player => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(player); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.VehicleRemove: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle) }, true); if (scriptFunction == null) { return; } OnVehicleRemove += vehicle => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(vehicle); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.PlayerChangeVehicleSeat: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle), typeof(IPlayer), typeof(byte), typeof(byte) }, true); if (scriptFunction == null) { return; } OnPlayerChangeVehicleSeat += (vehicle, player, seat, newSeat) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(vehicle); currScriptFunction.Set(player); currScriptFunction.Set(seat); currScriptFunction.Set(newSeat); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.PlayerEnterVehicle: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle), typeof(IPlayer), typeof(byte) }, true); if (scriptFunction == null) { return; } OnPlayerEnterVehicle += (vehicle, player, seat) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(vehicle); currScriptFunction.Set(player); currScriptFunction.Set(seat); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.PlayerEnteringVehicle: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle), typeof(IPlayer), typeof(byte) }, true); if (scriptFunction == null) { return; } OnPlayerEnteringVehicle += (vehicle, player, seat) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(vehicle); currScriptFunction.Set(player); currScriptFunction.Set(seat); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.PlayerLeaveVehicle: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle), typeof(IPlayer), typeof(byte) }, true); if (scriptFunction == null) { return; } OnPlayerLeaveVehicle += (vehicle, player, seat) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(vehicle); currScriptFunction.Set(player); currScriptFunction.Set(seat); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.PlayerEvent: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(string), typeof(object[]) }, true); if (scriptFunction == null) { return; } OnPlayerEvent += (player, name, args) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(player); currScriptFunction.Set(name); currScriptFunction.Set(args); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.PlayerCustomEvent: Alt.Log("PlayerCustomEvent does not exists in async"); break; case ScriptEventType.ServerEvent: Alt.Log("PlayerCustomEvent does not exists in async"); break; case ScriptEventType.ServerCustomEvent: Alt.Log("PlayerCustomEvent does not exists in async"); break; case ScriptEventType.ConsoleCommand: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(string), typeof(string[]) }, true); if (scriptFunction == null) { return; } OnConsoleCommand += (name, args) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(name); currScriptFunction.Set(args); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.MetaDataChange: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IEntity), typeof(string), typeof(object) }, true); if (scriptFunction == null) { return; } OnMetaDataChange += (entity, key, value) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(entity); currScriptFunction.Set(key); currScriptFunction.Set(value); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.SyncedMetaDataChange: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IEntity), typeof(string), typeof(object) }, true); if (scriptFunction == null) { return; } OnSyncedMetaDataChange += (entity, key, value) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(entity); currScriptFunction.Set(key); currScriptFunction.Set(value); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.ColShape: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IColShape), typeof(IEntity), typeof(bool) }, true); if (scriptFunction == null) { return; } OnColShape += (shape, entity, state) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(shape); currScriptFunction.Set(entity); currScriptFunction.Set(state); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.WeaponDamage: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(IEntity), typeof(uint), typeof(ushort), typeof(Position), typeof(BodyPart) }, true); if (scriptFunction == null) { return; } OnWeaponDamage += (player, targetEntity, weapon, damage, shotOffset, damageOffset) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(player); currScriptFunction.Set(targetEntity); currScriptFunction.Set(weapon); currScriptFunction.Set(damage); currScriptFunction.Set(shotOffset); currScriptFunction.Set(damageOffset); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.VehicleDestroy: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle) }, true); if (scriptFunction == null) { return; } OnVehicleDestroy += vehicle => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(vehicle); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.Explosion: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(ExplosionType), typeof(Position), typeof(uint), typeof(IEntity) }, true); if (scriptFunction == null) { return; } OnExplosion += (player, explosionType, position, explosionFx, targetEntity) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(player); currScriptFunction.Set(explosionType); currScriptFunction.Set(position); currScriptFunction.Set(explosionFx); currScriptFunction.Set(targetEntity); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.Fire: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(FireInfo[]) }, true); if (scriptFunction == null) { return; } OnFire += (player, fireInfos) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(player); currScriptFunction.Set(fireInfos); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.StartProjectile: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(Position), typeof(Position), typeof(uint), typeof(uint) }, true); if (scriptFunction == null) { return; } OnStartProjectile += (player, startPosition, direction, ammoHash, weaponHash) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(player); currScriptFunction.Set(startPosition); currScriptFunction.Set(direction); currScriptFunction.Set(ammoHash); currScriptFunction.Set(weaponHash); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.PlayerWeaponChange: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(uint), typeof(uint) }, true); if (scriptFunction == null) { return; } OnPlayerWeaponChange += (player, oldWeapon, newWeapon) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(player); currScriptFunction.Set(oldWeapon); currScriptFunction.Set(newWeapon); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.NetOwnerChange: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IEntity), typeof(IPlayer), typeof(IPlayer) }, true); if (scriptFunction == null) { return; } OnNetworkOwnerChange += (targetEntity, oldNetOwner, newNetOwner) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(targetEntity); currScriptFunction.Set(oldNetOwner); currScriptFunction.Set(newNetOwner); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.VehicleAttach: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle), typeof(IVehicle) }, true); if (scriptFunction == null) { return; } OnVehicleAttach += (targetVehicle, attachedVehicle) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(targetVehicle); currScriptFunction.Set(attachedVehicle); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.VehicleDetach: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle), typeof(IVehicle) }, true); if (scriptFunction == null) { return; } OnVehicleDetach += (targetVehicle, detachedVehicle) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(targetVehicle); currScriptFunction.Set(detachedVehicle); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.VehicleDamage: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle), typeof(IEntity), typeof(uint), typeof(uint), typeof(uint), typeof(uint), typeof(uint) }, true); if (scriptFunction == null) { return; } OnVehicleDamage += (vehicle, targetEntity, bodyHealthDamage, additionalBodyHealthDamage, engineHealthDamage, petrolTankDamage, weaponHash) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(vehicle); currScriptFunction.Set(targetEntity); currScriptFunction.Set(bodyHealthDamage); currScriptFunction.Set(additionalBodyHealthDamage); currScriptFunction.Set(engineHealthDamage); currScriptFunction.Set(petrolTankDamage); currScriptFunction.Set(weaponHash); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.ConnectionQueueAdd: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IConnectionInfo) }, true); if (scriptFunction == null) { return; } OnConnectionQueueAdd += (connectionInfo) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(connectionInfo); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.ConnectionQueueRemove: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IConnectionInfo) }, true); if (scriptFunction == null) { return; } OnConnectionQueueRemove += (connectionInfo) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(connectionInfo); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.ServerStarted: scriptFunction = ScriptFunction.Create(eventMethodDelegate, Array.Empty <Type>(), true); if (scriptFunction == null) { return; } OnServerStarted += () => { var currScriptFunction = scriptFunction.Clone(); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.PlayerRequestControl: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IEntity), typeof(IPlayer) }, true); if (scriptFunction == null) { return; } OnPlayerRequestControl += (entity, player) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(entity); currScriptFunction.Set(player); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.PlayerChangeAnimation: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new [] { typeof(IEntity), typeof(uint), typeof(uint), typeof(uint), typeof(uint) }, true); if (scriptFunction == null) { return; } OnPlayerChangeAnimation += (entity, oldDict, newDict, oldName, newName) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(entity); currScriptFunction.Set(oldDict); currScriptFunction.Set(newDict); currScriptFunction.Set(oldName); currScriptFunction.Set(newName); return(currScriptFunction.CallAsync()); }; break; case ScriptEventType.PlayerChangeInterior: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new [] { typeof(IEntity), typeof(uint), typeof(uint) }, true); if (scriptFunction == null) { return; } OnPlayerChangeInterior += (entity, oldIntLoc, newIntLoc) => { var currScriptFunction = scriptFunction.Clone(); currScriptFunction.Set(entity); currScriptFunction.Set(oldIntLoc); currScriptFunction.Set(newIntLoc); return(currScriptFunction.CallAsync()); }; break; } break; case AsyncServerEventAttribute @event: var serverEventName = @event.Name ?? eventMethod.Name; Core.OnServer(serverEventName, Function.Create(Core, eventMethodDelegate)); break; case AsyncClientEventAttribute @event: var clientEventName = @event.Name ?? eventMethod.Name; Core.OnClient(clientEventName, Function.Create(Core, eventMethodDelegate)); break; } }); }
public static void RegisterEvents(object target) { #pragma warning disable 612, 618 ModuleScriptMethodIndexer.Index(target, new[] { typeof(EventAttribute), typeof(ServerEventAttribute), typeof(ClientEventAttribute), typeof(ScriptEventAttribute) }, #pragma warning restore 612, 618 (baseEvent, eventMethod, eventMethodDelegate) => { switch (baseEvent) { case ScriptEventAttribute scriptEvent: var scriptEventType = scriptEvent.EventType; ScriptFunction scriptFunction; switch (scriptEventType) { case ScriptEventType.Checkpoint: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(ICheckpoint), typeof(IEntity), typeof(bool) }); if (scriptFunction == null) { return; } OnCheckpoint += (checkpoint, entity, state) => { scriptFunction.Set(checkpoint); scriptFunction.Set(entity); scriptFunction.Set(state); scriptFunction.Call(); }; break; case ScriptEventType.PlayerConnect: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(string) }); if (scriptFunction == null) { return; } OnPlayerConnect += (player, reason) => { scriptFunction.Set(player); scriptFunction.Set(reason); scriptFunction.Call(); }; break; case ScriptEventType.PlayerDamage: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(IEntity), typeof(uint), typeof(ushort) }); if (scriptFunction == null) { return; } OnPlayerDamage += (player, attacker, weapon, damage) => { scriptFunction.Set(player); scriptFunction.Set(attacker); scriptFunction.Set(weapon); scriptFunction.Set(damage); scriptFunction.Call(); }; break; case ScriptEventType.PlayerDead: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(IEntity), typeof(uint) }); if (scriptFunction == null) { return; } OnPlayerDead += (player, attacker, weapon) => { scriptFunction.Set(player); scriptFunction.Set(attacker); scriptFunction.Set(weapon); scriptFunction.Call(); }; break; case ScriptEventType.PlayerDisconnect: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(string) }); if (scriptFunction == null) { return; } OnPlayerDisconnect += (player, reason) => { scriptFunction.Set(player); scriptFunction.Set(reason); scriptFunction.Call(); }; break; case ScriptEventType.PlayerRemove: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer) }); if (scriptFunction == null) { return; } OnPlayerRemove += player => { scriptFunction.Set(player); scriptFunction.Call(); }; break; case ScriptEventType.VehicleRemove: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle) }); if (scriptFunction == null) { return; } OnVehicleRemove += vehicle => { scriptFunction.Set(vehicle); scriptFunction.Call(); }; break; case ScriptEventType.PlayerChangeVehicleSeat: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle), typeof(IPlayer), typeof(byte), typeof(byte) }); if (scriptFunction == null) { return; } OnPlayerChangeVehicleSeat += (vehicle, player, seat, newSeat) => { scriptFunction.Set(vehicle); scriptFunction.Set(player); scriptFunction.Set(seat); scriptFunction.Set(newSeat); scriptFunction.Call(); }; break; case ScriptEventType.PlayerEnterVehicle: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle), typeof(IPlayer), typeof(byte) }); if (scriptFunction == null) { return; } OnPlayerEnterVehicle += (vehicle, player, seat) => { scriptFunction.Set(vehicle); scriptFunction.Set(player); scriptFunction.Set(seat); scriptFunction.Call(); }; break; case ScriptEventType.PlayerLeaveVehicle: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle), typeof(IPlayer), typeof(byte) }); if (scriptFunction == null) { return; } OnPlayerLeaveVehicle += (vehicle, player, seat) => { scriptFunction.Set(vehicle); scriptFunction.Set(player); scriptFunction.Set(seat); scriptFunction.Call(); }; break; case ScriptEventType.PlayerEvent: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(string), typeof(object[]) }); if (scriptFunction == null) { return; } OnPlayerEvent += (player, name, args) => { scriptFunction.Set(player); scriptFunction.Set(name); scriptFunction.Set(args); scriptFunction.Call(); }; break; case ScriptEventType.PlayerCustomEvent: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(string), typeof(MValueConst[]) }); if (scriptFunction == null) { return; } OnPlayerCustomEvent += (player, name, array) => { scriptFunction.Set(player); scriptFunction.Set(name); scriptFunction.Set(array); scriptFunction.Call(); }; break; case ScriptEventType.ServerEvent: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(string), typeof(object[]) }); if (scriptFunction == null) { return; } OnServerEvent += (scriptEventName, scriptEventArgs) => { scriptFunction.Set(scriptEventName); scriptFunction.Set(scriptEventArgs); scriptFunction.Call(); }; break; case ScriptEventType.ServerCustomEvent: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(string), typeof(MValueConst[]) }); if (scriptFunction == null) { return; } OnServerCustomEvent += (name, array) => { scriptFunction.Set(name); scriptFunction.Set(array); scriptFunction.Call(); }; break; case ScriptEventType.ConsoleCommand: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(string), typeof(string[]) }); if (scriptFunction == null) { return; } OnConsoleCommand += (name, args) => { scriptFunction.Set(name); scriptFunction.Set(args); scriptFunction.Call(); }; break; case ScriptEventType.MetaDataChange: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IEntity), typeof(string), typeof(object) }); if (scriptFunction == null) { return; } OnMetaDataChange += (entity, key, value) => { scriptFunction.Set(entity); scriptFunction.Set(key); scriptFunction.Set(value); scriptFunction.Call(); }; break; case ScriptEventType.SyncedMetaDataChange: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IEntity), typeof(string), typeof(object) }); if (scriptFunction == null) { return; } OnSyncedMetaDataChange += (entity, key, value) => { scriptFunction.Set(entity); scriptFunction.Set(key); scriptFunction.Set(value); scriptFunction.Call(); }; break; case ScriptEventType.ColShape: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IColShape), typeof(IEntity), typeof(bool) }); if (scriptFunction == null) { return; } OnColShape += (shape, entity, state) => { scriptFunction.Set(shape); scriptFunction.Set(entity); scriptFunction.Set(state); scriptFunction.Call(); }; break; case ScriptEventType.WeaponDamage: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(IEntity), typeof(uint), typeof(ushort), typeof(Position), typeof(BodyPart) }); if (scriptFunction == null) { return; } OnWeaponDamage += (player, targetEntity, weapon, damage, shotOffset, damageOffset) => { scriptFunction.Set(player); scriptFunction.Set(targetEntity); scriptFunction.Set(weapon); scriptFunction.Set(damage); scriptFunction.Set(shotOffset); scriptFunction.Set(damageOffset); if (scriptFunction.Call() is bool value) { return(value); } return(true); }; break; default: throw new ArgumentOutOfRangeException(); } break; #pragma warning disable 612, 618 case EventAttribute @event: var eventName = @event.Name ?? eventMethod.Name; Module.On(eventName, Function.Create(eventMethodDelegate)); break; #pragma warning restore 612, 618 case ServerEventAttribute @event: var serverEventName = @event.Name ?? eventMethod.Name; Module.OnServer(serverEventName, Function.Create(eventMethodDelegate)); break; case ClientEventAttribute @event: var clientEventName = @event.Name ?? eventMethod.Name; Module.OnClient(clientEventName, Function.Create(eventMethodDelegate)); break; } }); }
public static void RegisterEvents(object target) { ModuleScriptMethodIndexer.Index(target, new[] { typeof(ServerEventAttribute), typeof(ClientEventAttribute), typeof(ScriptEventAttribute) }, (baseEvent, eventMethod, eventMethodDelegate) => { switch (baseEvent) { case ScriptEventAttribute scriptEvent: var scriptEventType = scriptEvent.EventType; ScriptFunction scriptFunction; switch (scriptEventType) { case ScriptEventType.Checkpoint: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(ICheckpoint), typeof(IEntity), typeof(bool) }); if (scriptFunction == null) { return; } OnCheckpoint += (checkpoint, entity, state) => { scriptFunction.Set(checkpoint); scriptFunction.Set(entity); scriptFunction.Set(state); scriptFunction.Call(); }; break; case ScriptEventType.PlayerConnect: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(string) }); if (scriptFunction == null) { return; } OnPlayerConnect += (player, reason) => { scriptFunction.Set(player); scriptFunction.Set(reason); scriptFunction.Call(); }; break; case ScriptEventType.PlayerDamage: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(IEntity), typeof(uint), typeof(ushort) }); if (scriptFunction == null) { return; } OnPlayerDamage += (player, attacker, weapon, damage) => { scriptFunction.Set(player); scriptFunction.Set(attacker); scriptFunction.Set(weapon); scriptFunction.Set(damage); scriptFunction.Call(); }; break; case ScriptEventType.PlayerDead: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(IEntity), typeof(uint) }); if (scriptFunction == null) { return; } OnPlayerDead += (player, attacker, weapon) => { scriptFunction.Set(player); scriptFunction.Set(attacker); scriptFunction.Set(weapon); scriptFunction.Call(); }; break; case ScriptEventType.PlayerDisconnect: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(string) }); if (scriptFunction == null) { return; } OnPlayerDisconnect += (player, reason) => { scriptFunction.Set(player); scriptFunction.Set(reason); scriptFunction.Call(); }; break; case ScriptEventType.PlayerRemove: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer) }); if (scriptFunction == null) { return; } OnPlayerRemove += player => { scriptFunction.Set(player); scriptFunction.Call(); }; break; case ScriptEventType.VehicleRemove: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle) }); if (scriptFunction == null) { return; } OnVehicleRemove += vehicle => { scriptFunction.Set(vehicle); scriptFunction.Call(); }; break; case ScriptEventType.PlayerChangeVehicleSeat: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle), typeof(IPlayer), typeof(byte), typeof(byte) }); if (scriptFunction == null) { return; } OnPlayerChangeVehicleSeat += (vehicle, player, seat, newSeat) => { scriptFunction.Set(vehicle); scriptFunction.Set(player); scriptFunction.Set(seat); scriptFunction.Set(newSeat); scriptFunction.Call(); }; break; case ScriptEventType.PlayerEnterVehicle: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle), typeof(IPlayer), typeof(byte) }); if (scriptFunction == null) { return; } OnPlayerEnterVehicle += (vehicle, player, seat) => { scriptFunction.Set(vehicle); scriptFunction.Set(player); scriptFunction.Set(seat); scriptFunction.Call(); }; break; case ScriptEventType.PlayerEnteringVehicle: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle), typeof(IPlayer), typeof(byte) }); if (scriptFunction == null) { return; } OnPlayerEnteringVehicle += (vehicle, player, seat) => { scriptFunction.Set(vehicle); scriptFunction.Set(player); scriptFunction.Set(seat); scriptFunction.Call(); }; break; case ScriptEventType.PlayerLeaveVehicle: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle), typeof(IPlayer), typeof(byte) }); if (scriptFunction == null) { return; } OnPlayerLeaveVehicle += (vehicle, player, seat) => { scriptFunction.Set(vehicle); scriptFunction.Set(player); scriptFunction.Set(seat); scriptFunction.Call(); }; break; case ScriptEventType.PlayerEvent: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(string), typeof(object[]) }); if (scriptFunction == null) { return; } OnPlayerEvent += (player, name, args) => { scriptFunction.Set(player); scriptFunction.Set(name); scriptFunction.Set(args); scriptFunction.Call(); }; break; case ScriptEventType.PlayerCustomEvent: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(string), typeof(MValueConst[]) }); if (scriptFunction == null) { return; } OnPlayerCustomEvent += (player, name, array) => { scriptFunction.Set(player); scriptFunction.Set(name); scriptFunction.Set(array); scriptFunction.Call(); }; break; case ScriptEventType.ServerEvent: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(string), typeof(object[]) }); if (scriptFunction == null) { return; } OnServerEvent += (scriptEventName, scriptEventArgs) => { scriptFunction.Set(scriptEventName); scriptFunction.Set(scriptEventArgs); scriptFunction.Call(); }; break; case ScriptEventType.ServerCustomEvent: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(string), typeof(MValueConst[]) }); if (scriptFunction == null) { return; } OnServerCustomEvent += (name, array) => { scriptFunction.Set(name); scriptFunction.Set(array); scriptFunction.Call(); }; break; case ScriptEventType.ConsoleCommand: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(string), typeof(string[]) }); if (scriptFunction == null) { return; } OnConsoleCommand += (name, args) => { scriptFunction.Set(name); scriptFunction.Set(args); scriptFunction.Call(); }; break; case ScriptEventType.MetaDataChange: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IEntity), typeof(string), typeof(object) }); if (scriptFunction == null) { return; } OnMetaDataChange += (entity, key, value) => { scriptFunction.Set(entity); scriptFunction.Set(key); scriptFunction.Set(value); scriptFunction.Call(); }; break; case ScriptEventType.SyncedMetaDataChange: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IEntity), typeof(string), typeof(object) }); if (scriptFunction == null) { return; } OnSyncedMetaDataChange += (entity, key, value) => { scriptFunction.Set(entity); scriptFunction.Set(key); scriptFunction.Set(value); scriptFunction.Call(); }; break; case ScriptEventType.ColShape: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IColShape), typeof(IEntity), typeof(bool) }); if (scriptFunction == null) { return; } OnColShape += (shape, entity, state) => { scriptFunction.Set(shape); scriptFunction.Set(entity); scriptFunction.Set(state); scriptFunction.Call(); }; break; case ScriptEventType.WeaponDamage: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(IEntity), typeof(uint), typeof(ushort), typeof(Position), typeof(BodyPart) }); if (scriptFunction == null) { return; } OnWeaponDamage += (player, targetEntity, weapon, damage, shotOffset, damageOffset) => { scriptFunction.Set(player); scriptFunction.Set(targetEntity); scriptFunction.Set(weapon); scriptFunction.Set(damage); scriptFunction.Set(shotOffset); scriptFunction.Set(damageOffset); if (scriptFunction.Call() is bool value) { return(value); } return(true); }; break; case ScriptEventType.VehicleDestroy: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle) }); if (scriptFunction == null) { return; } OnVehicleDestroy += vehicle => { scriptFunction.Set(vehicle); scriptFunction.Call(); }; break; case ScriptEventType.Explosion: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(ExplosionType), typeof(Position), typeof(uint), typeof(IEntity) }); if (scriptFunction == null) { return; } OnExplosion += (player, explosionType, position, explosionFx, targetEntity) => { scriptFunction.Set(player); scriptFunction.Set(explosionType); scriptFunction.Set(position); scriptFunction.Set(explosionFx); scriptFunction.Set(targetEntity); if (scriptFunction.Call() is bool value) { return(value); } return(true); }; break; case ScriptEventType.Fire: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(FireInfo[]) }); if (scriptFunction == null) { return; } OnFire += (player, fireInfos) => { scriptFunction.Set(player); scriptFunction.Set(fireInfos); if (scriptFunction.Call() is bool value) { return(value); } return(true); }; break; case ScriptEventType.StartProjectile: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(Position), typeof(Position), typeof(uint), typeof(uint) }); if (scriptFunction == null) { return; } OnStartProjectile += (player, startPosition, direction, ammoHash, weaponHash) => { scriptFunction.Set(player); scriptFunction.Set(startPosition); scriptFunction.Set(direction); scriptFunction.Set(ammoHash); scriptFunction.Set(weaponHash); if (scriptFunction.Call() is bool value) { return(value); } return(true); }; break; case ScriptEventType.PlayerWeaponChange: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(uint), typeof(uint) }); if (scriptFunction == null) { return; } OnPlayerWeaponChange += (player, oldWeapon, newWeapon) => { scriptFunction.Set(player); scriptFunction.Set(oldWeapon); scriptFunction.Set(newWeapon); if (scriptFunction.Call() is bool value) { return(value); } return(true); }; break; case ScriptEventType.NetOwnerChange: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IEntity), typeof(IPlayer), typeof(IPlayer) }); if (scriptFunction == null) { return; } OnNetworkOwnerChange += (targetEntity, oldNetOwner, newNetOwner) => { scriptFunction.Set(targetEntity); scriptFunction.Set(oldNetOwner); scriptFunction.Set(newNetOwner); scriptFunction.Call(); }; break; case ScriptEventType.VehicleAttach: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle), typeof(IVehicle) }); if (scriptFunction == null) { return; } OnVehicleAttach += (targetVehicle, attachedVehicle) => { scriptFunction.Set(targetVehicle); scriptFunction.Set(attachedVehicle); scriptFunction.Call(); }; break; case ScriptEventType.VehicleDetach: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle), typeof(IVehicle) }); if (scriptFunction == null) { return; } OnVehicleDetach += (targetVehicle, detachedVehicle) => { scriptFunction.Set(targetVehicle); scriptFunction.Set(detachedVehicle); scriptFunction.Call(); }; break; default: throw new ArgumentOutOfRangeException(); } break; case ServerEventAttribute @event: var serverEventName = @event.Name ?? eventMethod.Name; Module.OnServer(serverEventName, Function.Create(eventMethodDelegate)); break; case ClientEventAttribute @event: var clientEventName = @event.Name ?? eventMethod.Name; Module.OnClient(clientEventName, Function.Create(eventMethodDelegate)); break; } }); }
public static void RegisterEvents(object target) { ModuleScriptMethodIndexer.Index(target, new[] { typeof(Event), typeof(ScriptEvent) }, (baseEvent, eventMethod, eventMethodDelegate) => { switch (baseEvent) { case ScriptEvent scriptEvent: var scriptEventType = scriptEvent.EventType; ScriptFunction scriptFunction; switch (scriptEventType) { case ScriptEventType.Checkpoint: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(ICheckpoint), typeof(IEntity), typeof(bool) }); if (scriptFunction == null) { return; } OnCheckpoint += (checkpoint, entity, state) => { scriptFunction.Set(checkpoint); scriptFunction.Set(entity); scriptFunction.Set(state); scriptFunction.Call(); }; break; case ScriptEventType.PlayerConnect: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(string) }); if (scriptFunction == null) { return; } OnPlayerConnect += (player, reason) => { scriptFunction.Set(player); scriptFunction.Set(reason); scriptFunction.Call(); }; break; case ScriptEventType.PlayerDamage: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(IEntity), typeof(uint), typeof(ushort) }); if (scriptFunction == null) { return; } OnPlayerDamage += (player, attacker, weapon, damage) => { scriptFunction.Set(player); scriptFunction.Set(attacker); scriptFunction.Set(weapon); scriptFunction.Set(damage); scriptFunction.Call(); }; break; case ScriptEventType.PlayerDead: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(IEntity), typeof(uint) }); if (scriptFunction == null) { return; } OnPlayerDead += (player, attacker, weapon) => { scriptFunction.Set(player); scriptFunction.Set(attacker); scriptFunction.Set(weapon); scriptFunction.Call(); }; break; case ScriptEventType.PlayerDisconnect: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(string) }); if (scriptFunction == null) { return; } OnPlayerDisconnect += (player, reason) => { scriptFunction.Set(player); scriptFunction.Set(reason); scriptFunction.Call(); }; break; case ScriptEventType.PlayerRemove: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer) }); if (scriptFunction == null) { return; } OnPlayerRemove += player => { scriptFunction.Set(player); scriptFunction.Call(); }; break; case ScriptEventType.VehicleRemove: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle) }); if (scriptFunction == null) { return; } OnVehicleRemove += vehicle => { scriptFunction.Set(vehicle); scriptFunction.Call(); }; break; case ScriptEventType.PlayerChangeVehicleSeat: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle), typeof(IPlayer), typeof(byte), typeof(byte) }); if (scriptFunction == null) { return; } OnPlayerChangeVehicleSeat += (vehicle, player, seat, newSeat) => { scriptFunction.Set(vehicle); scriptFunction.Set(player); scriptFunction.Set(seat); scriptFunction.Set(newSeat); scriptFunction.Call(); }; break; case ScriptEventType.PlayerEnterVehicle: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle), typeof(IPlayer), typeof(byte) }); if (scriptFunction == null) { return; } OnPlayerEnterVehicle += (vehicle, player, seat) => { scriptFunction.Set(vehicle); scriptFunction.Set(player); scriptFunction.Set(seat); scriptFunction.Call(); }; break; case ScriptEventType.PlayerLeaveVehicle: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IVehicle), typeof(IPlayer), typeof(byte) }); if (scriptFunction == null) { return; } OnPlayerLeaveVehicle += (vehicle, player, seat) => { scriptFunction.Set(vehicle); scriptFunction.Set(player); scriptFunction.Set(seat); scriptFunction.Call(); }; break; case ScriptEventType.PlayerEvent: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(string), typeof(object[]) }); if (scriptFunction == null) { return; } OnPlayerEvent += (player, name, args) => { scriptFunction.Set(player); scriptFunction.Set(name); scriptFunction.Set(args); scriptFunction.Call(); }; break; case ScriptEventType.PlayerCustomEvent: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(string), typeof(MValueArray) }); if (scriptFunction == null) { return; } OnPlayerCustomEvent += (IPlayer player, string name, ref MValueArray array) => { scriptFunction.Set(player); scriptFunction.Set(name); scriptFunction.Set(array); scriptFunction.Call(); }; break; case ScriptEventType.ServerEvent: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(string), typeof(object[]) }); if (scriptFunction == null) { return; } OnServerEvent += (serverEventName, serverEventArgs) => { scriptFunction.Set(serverEventName); scriptFunction.Set(serverEventArgs); scriptFunction.Call(); }; break; case ScriptEventType.ServerCustomEvent: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(string), typeof(MValueArray) }); if (scriptFunction == null) { return; } OnServerCustomEvent += (string name, ref MValueArray array) => { scriptFunction.Set(name); scriptFunction.Set(array); scriptFunction.Call(); }; break; case ScriptEventType.ConsoleCommand: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(string), typeof(string[]) }); if (scriptFunction == null) { return; } OnConsoleCommand += (name, args) => { scriptFunction.Set(name); scriptFunction.Set(args); scriptFunction.Call(); }; break; case ScriptEventType.MetaDataChange: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IEntity), typeof(string), typeof(object) }); if (scriptFunction == null) { return; } OnMetaDataChange += (entity, key, value) => { scriptFunction.Set(entity); scriptFunction.Set(key); scriptFunction.Set(value); scriptFunction.Call(); }; break; case ScriptEventType.SyncedMetaDataChange: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IEntity), typeof(string), typeof(object) }); if (scriptFunction == null) { return; } OnSyncedMetaDataChange += (entity, key, value) => { scriptFunction.Set(entity); scriptFunction.Set(key); scriptFunction.Set(value); scriptFunction.Call(); }; break; case ScriptEventType.ColShape: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IColShape), typeof(IEntity), typeof(bool) }); if (scriptFunction == null) { return; } OnColShape += (shape, entity, state) => { scriptFunction.Set(shape); scriptFunction.Set(entity); scriptFunction.Set(state); scriptFunction.Call(); }; break; } break; case Event @event: var eventName = @event.Name ?? eventMethod.Name; Module.On(eventName, Function.Create(eventMethodDelegate)); break; } }); }
private void RegisterEvents(object target /*, Chat chat*/) { ModuleScriptMethodIndexer.Index(target, new[] { typeof(Command), typeof(CommandEvent) }, (baseEvent, eventMethod, eventMethodDelegate) => { if (baseEvent is Command command) { var commandName = command.Name ?? eventMethod.Name; Handles.AddLast(GCHandle.Alloc(eventMethodDelegate)); var function = Function.Create(eventMethodDelegate); if (function == null) { Alt.Log("Unsupported Command method: " + eventMethod); return; } Functions.AddLast(function); /*chat.RegisterCommand(commandName, * (player, arguments) => * { * function.InvokeNoResult(function.CalculateStringInvokeValues(arguments, player)); * });*/ LinkedList <CommandDelegate> delegates; if (!commandDelegates.TryGetValue(commandName, out delegates)) { delegates = new LinkedList <CommandDelegate>(); commandDelegates[commandName] = delegates; } if (command.GreedyArg) { delegates.AddLast((player, arguments) => { function.Call(player, new[] { string.Join(" ", arguments) }); }); } else { delegates.AddLast((player, arguments) => { function.Call(player, arguments); }); } var aliases = command.Aliases; if (aliases != null) { foreach (var alias in aliases) { if (!commandDelegates.TryGetValue(alias, out delegates)) { delegates = new LinkedList <CommandDelegate>(); commandDelegates[alias] = delegates; } if (command.GreedyArg) { delegates.AddLast((player, arguments) => { function.Call(player, new[] { string.Join(" ", arguments) }); }); } else { delegates.AddLast((player, arguments) => { function.Call(player, arguments); }); } } } } else if (baseEvent is CommandEvent commandEvent) { var commandEventType = commandEvent.EventType; ScriptFunction scriptFunction; switch (commandEventType) { case CommandEventType.CommandNotFound: scriptFunction = ScriptFunction.Create(eventMethodDelegate, new[] { typeof(IPlayer), typeof(string) }); if (scriptFunction == null) { return; } AltChat.OnCommandDoesNotExists += (player, commandName) => { scriptFunction.Set(player); scriptFunction.Set(commandName); scriptFunction.Call(); }; break; } } }); }