refactor hooks with new LuaResult

This commit is contained in:
Evil Factory
2021-09-21 17:11:49 -03:00
parent 784baf550f
commit b07e5d9b0b
9 changed files with 119 additions and 206 deletions

View File

@@ -117,19 +117,13 @@ namespace Barotrauma.Networking
return;
}
var should = GameMain.Lua.hook.Call("chatMessage", new object[] { txt, c, type });
var should = new LuaResult(GameMain.Lua.hook.Call("chatMessage", new object[] { txt, c, type }));
if(should != null)
{
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
if (should.Bool())
{
return;
}
if (type == ChatMessageType.Order)
{

View File

@@ -3112,18 +3112,11 @@ namespace Barotrauma.Networking
var hookChatMsg = ChatMessage.Create(senderName, message, (ChatMessageType)type, senderCharacter, senderClient, changeType);
var should = GameMain.Lua.hook.Call("modifyChatMessage", new object[] { hookChatMsg, senderRadio });
var should = new LuaResult(GameMain.Lua.hook.Call("modifyChatMessage", new object[] { hookChatMsg, senderRadio }));
if (should != null)
{
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
if (should.Bool())
return;
//check which clients can receive the message and apply distance effects
foreach (Client client in ConnectedClients)

View File

@@ -94,29 +94,20 @@ namespace Barotrauma.Networking
ChatMessage.CanUseRadio(sender.Character, out WifiComponent senderRadio) &&
ChatMessage.CanUseRadio(recipient.Character, out WifiComponent recipientRadio))
{
var should = GameMain.Lua.hook.Call("canUseVoiceRadio", new object[] { sender, recipient });
var should = new LuaResult(GameMain.Lua.hook.Call("canUseVoiceRadio", new object[] { sender, recipient }));
if (should != null)
{
if (should is DynValue dyn)
{
return dyn.CastToBool();
}
}
if (!should.IsNull() && should.Bool())
return should.Bool();
if (recipientRadio.CanReceive(senderRadio)) { return true; }
}
var should2 = GameMain.Lua.hook.Call("changeLocalVoiceRange", new object[] { sender, recipient });
var should2 = new LuaResult(GameMain.Lua.hook.Call("changeLocalVoiceRange", new object[] { sender, recipient }));
float range = 1.0f;
if (should2 != null)
{
if (should2 is DynValue dyn)
{
range = (float)dyn.CastToNumber();
}
}
if (!should2.IsNull())
range = should2.Float();
//otherwise do a distance check
return ChatMessage.GetGarbleAmount(recipient.Character, sender.Character, ChatMessage.SpeakRange) < range;

View File

@@ -712,18 +712,12 @@ namespace Barotrauma
float impactDamage = Math.Min((impact - ImpactTolerance) * ImpactDamageMultiplayer, character.MaxVitality * MaxImpactDamage);
#if SERVER
var should = new LuaResult(GameMain.Lua.hook.Call("changeFallDamage", new object[] { impactDamage, character, impactPos, velocity }));
var should = GameMain.Lua.hook.Call("changeFallDamage", new object[] { impactDamage, character, impactPos, velocity });
if (should != null)
{
if (should is DynValue dyn)
{
impactDamage = (float)dyn.CastToNumber();
}
if (!should.IsNull())
{
impactDamage = should.Float();
}
#endif
character.LastDamageSource = null;
character.AddDamage(impactPos, AfflictionPrefab.ImpactDamage.Instantiate(impactDamage).ToEnumerable(), 0.0f, true);

View File

@@ -409,20 +409,11 @@ namespace Barotrauma
{
if (targetLimb == null)
{
#if SERVER
var should = GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, affliction });
var should = new LuaResult(GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, affliction }));
if (should.Bool())
return;
if (should != null)
{
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
//if a limb-specific affliction is applied to no specific limb, apply to all limbs
foreach (LimbHealth limbHealth in limbHealths)
@@ -433,40 +424,20 @@ namespace Barotrauma
}
else
{
#if SERVER
var should = GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, affliction, targetLimb });
var should = new LuaResult(GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, affliction, targetLimb }));
if (should != null)
{
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
if (should.Bool())
return;
AddLimbAffliction(targetLimb, affliction);
}
}
else
{
#if SERVER
var should = GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, affliction });
var should = new LuaResult(GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, affliction }));
if (should != null)
{
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
if (should.Bool())
return;
AddAffliction(affliction);
}
@@ -544,20 +515,10 @@ namespace Barotrauma
return;
}
#if SERVER
var should = GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, attackResult, hitLimb });
var should = new LuaResult(GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, attackResult, hitLimb }));
if (should != null)
{
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
if (should.Bool())
return;
foreach (Affliction newAffliction in attackResult.Afflictions)
{

View File

@@ -186,20 +186,10 @@ namespace Barotrauma.Items.Components
var senderComponent = signal.source?.GetComponent<WifiComponent>();
if (senderComponent != null && !CanReceive(senderComponent)) { return; }
#if SERVER
var should = GameMain.Lua.hook.Call("wifiSignalTransmitted", new object[] { this, signal, sentFromChat });
var should = new LuaResult(GameMain.Lua.hook.Call("wifiSignalTransmitted", new object[] { this, signal, sentFromChat }));
if (should != null)
{
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
if (should.Bool())
return;
bool chatMsgSent = false;

View File

@@ -2225,20 +2225,10 @@ namespace Barotrauma
if (condition == 0.0f) { return; }
#if SERVER
var should = GameMain.Lua.hook.Call("itemUse", new object[] { this, character, targetLimb });
var should = new LuaResult(GameMain.Lua.hook.Call("itemUse", new object[] { this, character, targetLimb }));
if (should != null)
{
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
if (should.Bool())
return;
bool remove = false;
@@ -2273,20 +2263,11 @@ namespace Barotrauma
{
if (condition == 0.0f) { return; }
#if SERVER
var should = GameMain.Lua.hook.Call("itemSecondaryUse", new object[] { this, character});
var should = new LuaResult(GameMain.Lua.hook.Call("itemSecondaryUse", new object[] { this, character}));
if (should.Bool())
return;
if (should != null)
{
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
bool remove = false;
@@ -2319,20 +2300,10 @@ namespace Barotrauma
public void ApplyTreatment(Character user, Character character, Limb targetLimb)
{
#if SERVER
var should = GameMain.Lua.hook.Call("itemApplyTreatment", new object[] { this, user, character, targetLimb });
var should = new LuaResult(GameMain.Lua.hook.Call("itemApplyTreatment", new object[] { this, user, character, targetLimb }));
if (should != null)
{
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
if (should.Bool())
return;
//can't apply treatment to dead characters
if (character.IsDead) return;
@@ -2390,20 +2361,10 @@ namespace Barotrauma
public void Drop(Character dropper, bool createNetworkEvent = true)
{
#if SERVER
var should = GameMain.Lua.hook.Call("itemDrop", new object[] { this, dropper});
var should = new LuaResult(GameMain.Lua.hook.Call("itemDrop", new object[] { this, dropper}));
if (should != null)
{
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
if (should.Bool())
return;
if (createNetworkEvent)
{
@@ -2455,20 +2416,10 @@ namespace Barotrauma
public void Equip(Character character)
{
#if SERVER
var should = GameMain.Lua.hook.Call("itemEquip", new object[] { this, character});
var should = new LuaResult(GameMain.Lua.hook.Call("itemEquip", new object[] { this, character}));
if (should != null)
{
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
if (should.Bool())
return;
if (Removed)
{
@@ -2481,20 +2432,10 @@ namespace Barotrauma
public void Unequip(Character character)
{
#if SERVER
var should = GameMain.Lua.hook.Call("itemUnequip", new object[] { this, character });
var should = new LuaResult(GameMain.Lua.hook.Call("itemUnequip", new object[] { this, character }));
if (should != null)
{
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
if (should.Bool())
return;
foreach (ItemComponent ic in components) { ic.Unequip(character); }
}

View File

@@ -559,8 +559,12 @@ namespace Barotrauma
try
{
if (hf.function is Closure)
lastResult = env.lua.Call(hf.function, args);
// else if (hf.function is NLua.LuaFunction luaFunction)
{
var result = env.lua.Call(hf.function, args);
if (result.CastToBool() != false)
lastResult = result;
}
//else if (hf.function is NLua.LuaFunction luaFunction)
// lastResult = luaFunction.Call(args);
}
catch (Exception e)
@@ -572,5 +576,60 @@ namespace Barotrauma
return lastResult;
}
}
}
public class LuaResult
{
object result;
public LuaResult(object arg)
{
result = arg;
}
public bool IsNull()
{
if (result == null)
return true;
if (result is DynValue dynValue)
return dynValue.IsNil();
return false;
}
public bool Bool()
{
if (result is DynValue dynValue)
{
return dynValue.CastToBool();
}
return false;
}
public float Float()
{
if (result is DynValue dynValue)
{
var num = dynValue.CastToNumber();
if (num == null) { return 0f; }
return (float)num;
}
return 0f;
}
public double Double()
{
if (result is DynValue dynValue)
{
var num = dynValue.CastToNumber();
if (num == null) { return 0f; }
return (double)num;
}
return 0f;
}
}
}

View File

@@ -650,20 +650,10 @@ namespace Barotrauma
if (Math.Max(hull1.WorldSurface + hull1.WaveY[hull1.WaveY.Length - 1], hull2.WorldSurface + hull2.WaveY[0]) > WorldRect.Y) { return; }
}
#if SERVER
var should = GameMain.Lua.hook.Call("gapOxygenUpdate", new object[] { this, hull1, hull2 });
var should = new LuaResult(GameMain.Lua.hook.Call("gapOxygenUpdate", new object[] { this, hull1, hull2 }));
if (should != null)
{
if (should is DynValue dyn)
{
if (dyn.CastToBool())
{
return;
}
}
}
#endif
if (should.Bool())
return;
float totalOxygen = hull1.Oxygen + hull2.Oxygen;
float totalVolume = (hull1.Volume + hull2.Volume);