Skip to content

Commit dc89bf9

Browse files
committed
sync script events now need have the definded return type
1 parent bb864de commit dc89bf9

File tree

6 files changed

+68
-56
lines changed

6 files changed

+68
-56
lines changed

api/AltV.Net.Async/AltAsync.RegisterEvents.cs

Lines changed: 37 additions & 37 deletions
Large diffs are not rendered by default.

api/AltV.Net.Shared/FunctionParser/ScriptFunction.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public ScriptFunctionParameter(bool baseObjectCheck, Type parameterType)
4040
}
4141
}
4242

43-
public static ScriptFunction? Create(Delegate @delegate, Type[] types, bool isAsync = false)
43+
public static ScriptFunction? Create(Delegate @delegate, Type[] types, Type? returnType = null, bool isAsync = false)
4444
{
4545
var parameters = @delegate.Method.GetParameters();
4646
if (parameters.Length != types.Length)
@@ -76,6 +76,20 @@ public ScriptFunctionParameter(bool baseObjectCheck, Type parameterType)
7676
return null;
7777
}
7878

79+
if (!isAsync)
80+
{
81+
if (returnType is null && !typeof(void).IsAssignableFrom(@delegate.Method.ReturnType))
82+
{
83+
WrongReturnType(@delegate.Method, typeof(void), @delegate.Method.ReturnType);
84+
return null;
85+
}
86+
if (returnType is not null && !returnType.IsAssignableFrom(@delegate.Method.ReturnType))
87+
{
88+
WrongReturnType(@delegate.Method, returnType, @delegate.Method.ReturnType);
89+
return null;
90+
}
91+
}
92+
7993
if (isAsync && !typeof(Task).IsAssignableFrom(@delegate.Method.ReturnType))
8094
{
8195
WrongReturnType(@delegate.Method, typeof(Task), @delegate.Method.ReturnType);

api/AltV.Net/Alt.RegisterEvents.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public static void RegisterEvents(object target)
311311
{
312312
typeof(IPlayer), typeof(IEntity), typeof(uint), typeof(ushort),
313313
typeof(Position), typeof(BodyPart)
314-
});
314+
}, typeof(uint));
315315
if (scriptFunction == null) return;
316316
OnWeaponDamage +=
317317
(player, targetEntity, weapon, damage, shotOffset, damageOffset) =>
@@ -322,12 +322,12 @@ public static void RegisterEvents(object target)
322322
scriptFunction.Set(damage);
323323
scriptFunction.Set(shotOffset);
324324
scriptFunction.Set(damageOffset);
325-
if (scriptFunction.Call() is bool value)
325+
if (scriptFunction.Call() is uint value)
326326
{
327327
return value;
328328
}
329329

330-
return true;
330+
return 0;
331331
};
332332
break;
333333
}
@@ -354,7 +354,7 @@ public static void RegisterEvents(object target)
354354
{
355355
typeof(IPlayer), typeof(ExplosionType), typeof(Position), typeof(uint),
356356
typeof(IEntity)
357-
});
357+
}, typeof(bool));
358358
if (scriptFunction == null) return;
359359
OnExplosion += (player, explosionType, position, explosionFx, targetEntity) =>
360360
{
@@ -378,7 +378,7 @@ public static void RegisterEvents(object target)
378378
new[]
379379
{
380380
typeof(IPlayer), typeof(FireInfo[])
381-
});
381+
}, typeof(bool));
382382
if (scriptFunction == null) return;
383383
OnFire += (player, fireInfos) =>
384384
{
@@ -400,7 +400,7 @@ public static void RegisterEvents(object target)
400400
{
401401
typeof(IPlayer), typeof(Position), typeof(Position), typeof(uint),
402402
typeof(uint)
403-
});
403+
}, typeof(bool));
404404
if (scriptFunction == null) return;
405405
OnStartProjectile += (player, startPosition, direction, ammoHash, weaponHash) =>
406406
{
@@ -424,7 +424,7 @@ public static void RegisterEvents(object target)
424424
new[]
425425
{
426426
typeof(IPlayer), typeof(uint), typeof(uint)
427-
});
427+
}, typeof(bool));
428428
if (scriptFunction == null) return;
429429
OnPlayerWeaponChange += (player, oldWeapon, newWeapon) =>
430430
{
@@ -519,7 +519,7 @@ public static void RegisterEvents(object target)
519519
new[]
520520
{
521521
typeof(IVehicle), typeof(IPlayer), typeof(bool)
522-
});
522+
}, typeof(bool));
523523
if (scriptFunction == null) return;
524524
OnVehicleHorn += (targetVehicle, reporterPlayer, state) =>
525525
{
@@ -687,7 +687,7 @@ public static void RegisterEvents(object target)
687687
new[]
688688
{
689689
typeof(IPlayer), typeof(int)
690-
});
690+
}, typeof(bool));
691691
if (scriptFunction == null) return;
692692
OnRequestSyncScene += (source, sceneId) =>
693693
{

api/AltV.Net/Core.Events.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -454,15 +454,13 @@ public virtual void OnWeaponDamageEvent(IntPtr eventPointer, IPlayer sourcePlaye
454454
uint weapon, ushort damage,
455455
Position shotOffset, BodyPart bodyPart)
456456
{
457-
var cancel = false;
457+
uint? weaponDamage = null;
458458
foreach (var @delegate in WeaponDamageEventHandler.GetEvents())
459459
{
460460
try
461461
{
462-
if (!@delegate(sourcePlayer, targetEntity, weapon, damage, shotOffset, bodyPart))
463-
{
464-
cancel = true;
465-
}
462+
var result = @delegate(sourcePlayer, targetEntity, weapon, damage, shotOffset, bodyPart);
463+
weaponDamage ??= result;
466464
}
467465
catch (TargetInvocationException exception)
468466
{
@@ -474,11 +472,11 @@ public virtual void OnWeaponDamageEvent(IntPtr eventPointer, IPlayer sourcePlaye
474472
}
475473
}
476474

477-
if (cancel)
475+
if (weaponDamage is not null)
478476
{
479477
unsafe
480478
{
481-
Alt.Core.Library.Shared.Event_Cancel(eventPointer);
479+
Alt.Core.Library.Server.Event_WeaponDamageEvent_SetDamageValue(eventPointer, weaponDamage.Value);
482480
}
483481
}
484482
}

api/AltV.Net/Events/Events.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public delegate void PlayerConnectDeniedDelegate(PlayerConnectDeniedReason reaso
5555
public delegate bool ExplosionDelegate(IPlayer player, ExplosionType explosionType, Position position,
5656
uint explosionFx, IEntity targetEntity);
5757

58-
public delegate bool WeaponDamageDelegate(IPlayer player, IEntity target, uint weapon, ushort damage,
58+
public delegate uint WeaponDamageDelegate(IPlayer player, IEntity target, uint weapon, ushort damage,
5959
Position shotOffset, BodyPart bodyPart);
6060

6161
public delegate void VehicleDestroyDelegate(IVehicle vehicle);

runtime

0 commit comments

Comments
 (0)