Skip to content

Commit de5c7d7

Browse files
committed
small fixes
1 parent c1357ed commit de5c7d7

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ private static void WrongReturnType(MethodInfo methodInfo, Type expected, Type g
1414
Console.WriteLine(
1515
$"{methodInfo.DeclaringType?.FullName}.{methodInfo.Name}({string.Join(", ", methodInfo.GetParameters().Select(m => $"{m.ParameterType.FullName} {m.Name}"))}): Expected {expected} return type, but got {got}");
1616
}
17+
private static void WrongReturnTypes(MethodInfo methodInfo, Type[] expected, Type got)
18+
{
19+
Console.WriteLine(
20+
$"{methodInfo.DeclaringType?.FullName}.{methodInfo.Name}({string.Join(", ", methodInfo.GetParameters().Select(m => $"{m.ParameterType.FullName} {m.Name}"))}): Expected {expected} return type, but got {got}");
21+
}
1722

1823
private static void WrongType(MethodInfo methodInfo, Type expected, Type got)
1924
{
@@ -40,7 +45,7 @@ public ScriptFunctionParameter(bool baseObjectCheck, Type parameterType)
4045
}
4146
}
4247

43-
public static ScriptFunction? Create(Delegate @delegate, Type[] types, Type? returnType = null, bool isAsync = false)
48+
public static ScriptFunction? Create(Delegate @delegate, Type[] types, Type[]? returnTypes = null, bool isAsync = false)
4449
{
4550
var parameters = @delegate.Method.GetParameters();
4651
if (parameters.Length != types.Length)
@@ -78,15 +83,29 @@ public ScriptFunctionParameter(bool baseObjectCheck, Type parameterType)
7883

7984
if (!isAsync)
8085
{
81-
if (returnType is null && !typeof(void).IsAssignableFrom(@delegate.Method.ReturnType))
86+
if (returnTypes is null && !typeof(void).IsAssignableFrom(@delegate.Method.ReturnType))
8287
{
8388
WrongReturnType(@delegate.Method, typeof(void), @delegate.Method.ReturnType);
8489
return null;
8590
}
86-
if (returnType is not null && !returnType.IsAssignableFrom(@delegate.Method.ReturnType))
91+
if (returnTypes is not null)
8792
{
88-
WrongReturnType(@delegate.Method, returnType, @delegate.Method.ReturnType);
89-
return null;
93+
var validType = false;
94+
95+
foreach (var returnType in returnTypes)
96+
{
97+
if (returnType.IsAssignableFrom(@delegate.Method.ReturnType))
98+
{
99+
validType = true;
100+
break;
101+
}
102+
}
103+
104+
if (!validType)
105+
{
106+
WrongReturnTypes(@delegate.Method, returnTypes, @delegate.Method.ReturnType);
107+
return null;
108+
}
90109
}
91110
}
92111

api/AltV.Net/Alt.RegisterEvents.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public static void RegisterEvents(object target)
327327
{
328328
typeof(IPlayer), typeof(IEntity), typeof(uint), typeof(ushort),
329329
typeof(Position), typeof(BodyPart)
330-
}, typeof(uint));
330+
}, new[] {typeof(uint), typeof(bool)});
331331
if (scriptFunction == null) return;
332332
OnWeaponDamage +=
333333
(player, targetEntity, weapon, damage, shotOffset, damageOffset) =>
@@ -338,9 +338,14 @@ public static void RegisterEvents(object target)
338338
scriptFunction.Set(damage);
339339
scriptFunction.Set(shotOffset);
340340
scriptFunction.Set(damageOffset);
341-
if (scriptFunction.Call() is uint value)
341+
if (scriptFunction.Call() is uint uintValue)
342342
{
343-
return value;
343+
return uintValue;
344+
}
345+
346+
if (scriptFunction.Call() is bool boolValue)
347+
{
348+
return boolValue;
344349
}
345350

346351
return 0;
@@ -370,7 +375,7 @@ public static void RegisterEvents(object target)
370375
{
371376
typeof(IPlayer), typeof(ExplosionType), typeof(Position), typeof(uint),
372377
typeof(IEntity)
373-
}, typeof(bool));
378+
}, new[] {typeof(bool) });
374379
if (scriptFunction == null) return;
375380
OnExplosion += (player, explosionType, position, explosionFx, targetEntity) =>
376381
{
@@ -394,7 +399,7 @@ public static void RegisterEvents(object target)
394399
new[]
395400
{
396401
typeof(IPlayer), typeof(FireInfo[])
397-
}, typeof(bool));
402+
}, new[] {typeof(bool) });
398403
if (scriptFunction == null) return;
399404
OnFire += (player, fireInfos) =>
400405
{
@@ -416,7 +421,7 @@ public static void RegisterEvents(object target)
416421
{
417422
typeof(IPlayer), typeof(Position), typeof(Position), typeof(uint),
418423
typeof(uint)
419-
}, typeof(bool));
424+
}, new[] {typeof(bool) });
420425
if (scriptFunction == null) return;
421426
OnStartProjectile += (player, startPosition, direction, ammoHash, weaponHash) =>
422427
{
@@ -530,7 +535,7 @@ public static void RegisterEvents(object target)
530535
new[]
531536
{
532537
typeof(IVehicle), typeof(IPlayer), typeof(bool)
533-
}, typeof(bool));
538+
}, new[] {typeof(bool) });
534539
if (scriptFunction == null) return;
535540
OnVehicleHorn += (targetVehicle, reporterPlayer, state) =>
536541
{
@@ -698,7 +703,7 @@ public static void RegisterEvents(object target)
698703
new[]
699704
{
700705
typeof(IPlayer), typeof(int)
701-
}, typeof(bool));
706+
}, new[] {typeof(bool) });
702707
if (scriptFunction == null) return;
703708
OnRequestSyncScene += (source, sceneId) =>
704709
{

api/AltV.Net/Core.Events.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ public virtual void OnWeaponDamageEvent(IntPtr eventPointer, IPlayer sourcePlaye
519519
{
520520
var result = @delegate(sourcePlayer, targetEntity, weapon, damage, shotOffset, bodyPart);
521521

522-
if (result is bool)
522+
if (result is bool and false)
523523
{
524524
cancel = true;
525525
}

0 commit comments

Comments
 (0)